Shooting Range

0

0 votes
Data Structures, Medium, Segment Trees
Problem

Three friends Snow Gnome, Small Boy and Xephy were having fun in the biggest park in Lalalandia, when suddenly they saw the shooting range with prizes! As always, Small Boy asked for the biggest prize, and Snow Gnome said he would get it.

The shooting range consists of N towers each denoted by Ki lines (1iN). Towers are described by blocks from the bottom to the top. Each block consists of Aij cubes and the shooter gets Bij coins for taking one cube in this block for 1jKi (see the picture for more explanation).

The shooter gets Q shots. He shoots the bullets in straight line on height Cl. After the bullet shoots the cube, it gets destroyed, and all cubes above it move down. Note that bullet, even after penetrating one tower, continues to go through and never goes down.

Xephy now wants to know how many points for each shot Snow Gnome got. Can you help him with that?

Input format

First line contains single integer N - the number of towers.

Next N lines describe the tower i (1iN) - each contains single integer Ki. Next Ki lines contain the numbers Aij and Bij for 1jKi - the number of cubes and the coins for shooting one cube from this block, respectively.

Next line contains Q - the number of Snow Gnome's shots. The Q lines describe the lth shot (1lQ) - each containing one integer Cl - the height of the lth shot.

Output format

The output should contain Q lines. lth line should contain single integer - points for Snow Gnome's lth shot (1lQ).

Constraints:

1N105

1Ki10

1Aij108

1Bij109

1Q105

1Cl109

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The picture for the first test:

explanation for 1st test

For the first shot Snow Gnome got 8 points:

shot #1

For the second shot - 4:

shot #2

And the last one he missed:

shot #3

Editor Image

?