Deni is working at the accounting department of the Shumen university and was given the following task: during the summer the university has sent M
students, numbered from 1
to M
, on ethnographic expeditions in different places. Due to the circumstances, all places are near a road, that starts from Shumen, and student with number i
is located in place that is kilometers away from Shumen. All are non-negative integers and we have that .
Because of the intense science work during the summer π the students have spent all their money and have made a request to the university for more money so that they can return to Shumen. Each student will use the road, leading to Shumen, in the direction from his place to Shumen. He can walk and use a bus from some of the places near Shumen. When he walks, student with number i
needs levs for each kilometer for food and other needs. Also, there are buses in N
places which the students can rent. A bus in a place, which is kilometers away from Shumen, costs levs to be rent and it can transport the students, that are using it, to return to Shumen. If a bus is rent once, then it can be used by several students, which have gathered at this place, paying the cost only once. The buses travel directly to Shumen without intermediate stops for other people to get in. The university has set the condition that every student should return to Shumen with a bus so it will be certain that every student will get off at the stop of the university and will go the university to give the materials that have been collected during the expedition.
Task
Write a program to help Deni calculate the minimum total amount of money which is needed so that all students can return to Shumen. Furthermore β the university management wants to know what are the minimal amounts for the return of only the first student (who is located in the nearest place), only the first 2
students (with numbers 1
and 2
) and so on, lastly of all the M
students.
Input data
The first line of the standard input contains the positive integer N
β the number of places that have buses for rent.
The following N
lines contain two non-negative integers, separated by a space, and the j
-th of them contains:
- β the distance from Shumen for the corresponding place in which a bus can be rent;
- β the cost for renting a bus in that place.
The next line contains the positive integer M
β the number of students which are sent on ethnographic expeditions.
The last M
lines contain two non-negative integers, separated by a space, and the i
-th of them contains:
- β the distance from Shumen for the place where the corresponding student was sent on ethnographic expedition;
- β the amount of money that student needs for walking one kilometer.
Output data
On the only line of the standard output print M
numbers, separated by spaces β the minimum amounts of money which are needed for the return of only the first student (who is located in the nearest place), only the first 2
students (with numbers 1
and 2
) and so on, lastly of all the M
students.
Constraints and clarifications
- and for all
1 β€ i < N
; for all1 β€ j < M
Subtask 1 (11 points)
N β€ 10
M β€ 6
Subtask 2 (26 points)
- Every student has to pay for renting a bus even if several students travel together! Also, all are equal.
Subtask 3 (17 points)
N β€ 14
M β€ 100
Subtask 4 (23 points)
N β€ 1 000
M β€ 100
Subtask 5 (23 points)
M β€ 1000
Example 1
stdin
6
1 3
2 10
3 100
4 100
5 15
6 10
3
2 5
4 9
8 3
stdout
8 28 44
Explanation
Here the answers are for the original statement. The optimal solutions are the following:
- for the first student β student
1
goes to place1
and his return costs - for the first
2
students β student1
and2
go to place2
and their return costs - for all students β students
1
and2
again go to place2
and their return costs28
and student3
goes to place6
and his return costs and so the total amount is44
Example 2
stdin
6
1 3
2 10
3 100
4 100
5 15
6 10
3
2 7
4 7
8 7
stdout
10 34 58
Explanation
Here the answers are for the condition of the second subtask. The optimal solutions are the following:
- for the first student β student
1
goes to place2
and his return costs - for the first
2
students β students1
and2
go to place2
and their return costs - for all students β students
1
and2
again go to place2
and their return costs34
and student3
goes to place6
and his return costs and so the total amount is58