Formula One

Time limit: 2s
Memory limit: 512MB
Input:
Output:

Formula One (also known as Formula 1 or F1) is the highest class of international racing for open-wheel single-seater formula racing cars sanctioned by the Federation Internationale de l’Automobile (FIA). Matthew is a die-hard fan of F1 and an outstanding programmer. As he watched an F1 race with NN cars, he found some strange patterns. Mainly, he observed that the distance traveled by the i-th car up to the moment of time TT is equal to T2ai+Tbi+ciT^2 \cdot a_i + T \cdot b_i + c_i, where aia_i, bib_i and cic_i are some constants.

Matthew decided to implement an interactive scoreboard, where someone can see what car occupies a certain position at a certain moment. Strictly speaking, there are QQ queries (P,TP, T), which ask for the index of the car occupying the PP-th position at the moment TT. The position of a car is related to the distance it traveled, more distance meaning a better place (the first car traveled the most, the second car traveled the second most, and so on). If two cars travel the same distance, the lower index one is ahead.

As Matthew is busy watching the races, he asks you to implement the scoreboard for him.

Input data

The input contains on the first line an integer N, the number of cars taking part in the race.

On each of the following NN lines there are 33 integers separated by a space: aia_i, bib_i and cic_i.

The next line of the input contains an integer QQ, the number of queries you need to answer.

On each of the following QQ lines, there are 22 integers separated by a space: PP and TT

Output data

The output will contain QQ lines, containing the answer for each query.

Constraints and clarifications

  • 1PN2 0001 \leq P \leq N \leq 2 \ 000
  • 0T,ai,bi,ci1060 \leq T, a_i, b_i, c_i \leq 10^6
  • 1Q21051 \leq Q \leq 2 \cdot 10^5
  • For tests worth 20 points, 1N101 \leq N \leq 10
  • For tests worth 30 more points, 1Q51041 \leq Q \leq 5 \cdot 10^4

Example 1

stdin

5
5 7 1
4 7 2
3 3 3
7 6 9
10 2 3
10
1 0
3 1
4 2
5 3
1 4
5 656300
2 632744
3 549544
5 60828
2 67803

stdout

4
1
2
3
5
3
4
1
3
4

Log in or sign up to be able to send submissions!