Problem: Monocarp is going to host a party for his friends. He prepared dishes and is about to serve them. First, he has to add some powdered pepper to each of them — otherwise, the dishes will be pretty tasteless.
The -th dish has two values and — its tastiness with red pepper added or black pepper added, respectively. Monocarp won't add both peppers to any dish, won't add any pepper multiple times, and won't leave any dish without the pepper added.
Before adding the pepper, Monocarp should first purchase the said pepper in some shop. There are shops in his local area. The -th of them has packages of red pepper sufficient for servings and packages of black pepper sufficient for servings.
Monocarp goes to exactly one shop, purchases multiple (possibly, zero) packages of each pepper in such a way that each dish will get the pepper added once, and no pepper is left. More formally, if he purchases red pepper packages and black pepper packages, then and should be non-negative and should be equal to .
For each shop, determine the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it's impossible to purchase the packages in the said way, print -1.
Input Format: The first line contains a single integer () — the number of dishes.
The -th of the next lines contains two integers and () — the tastiness of the -th dish with red pepper added or black pepper added, respectively.
The next line contains a single integer () — the number of shops.
The -th of the next lines contains two integers and () — the number of servings the red and the black pepper packages are sufficient for in the -th shop, respectively.
Output Format: Print integers. For each shop, print the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it's impossible to purchase the packages so that each dish will get the pepper added once and no pepper is left, print -1.
Note: Consider the first example.
In the first shop, Monocarp can only buy red pepper packages and black pepper package. Black pepper added to all dishes will sum up to .
In the second shop, Monocarp can buy any number of red and black pepper packages: and , and , and or and . The optimal choice turns out to be either and or and . Monocarp can add black pepper to the first dish, red pepper to the second dish and any pepper to the third dish, the total is .
In the third shop, Monocarp can only buy red pepper package and black pepper packages. Red pepper added to all dishes will sum up to .
In the fourth shop, Monocarp can only buy an even total number of packages. Since is odd, it's impossible to get exactly packages. Thus, the answer is .