P17545 [JAG 2026 Summer Camp #2] Robot Damage
Description
A laboratory has a straight test track, which can be regarded as a number line, and a robot that moves along it. At the beginning of each experiment, the robot is stationary at the origin.
Two walls are placed on the track, one at a negative coordinate and the other at a positive coordinate. The robot cannot pass through the walls and can move only within the closed interval between them.
The robot has a button. When the button is pressed, the robot performs the following $N$ phases in order.
- Suppose that the robot is at coordinate $X$ at the beginning of phase $i$. It attempts to move along the number line toward coordinate $X+V_i$. It moves to the right if $V_i>0$ and to the left if $V_i
Input Format
The input contains one or more test cases. The first line contains an integer $t$, the number of test cases ($1\le t\le 10^5$). The descriptions of the test cases follow, each in the following format.
```text
N
V_1 V_2 ... V_N
Q
W_1 T_1
W_2 T_2
...
W_Q T_Q
```
The integer $N$ is the number of phases performed on each button press ($1\le N\le 2\times 10^5$). For each $i=1,\ldots,N$, the integer $V_i$ represents the displacement attempted in phase $i$ ($-10^9\le V_i\le 10^9$, $V_i\ne 0$).
The integer $Q$ is the number of queries ($1\le Q\le 2\times 10^5$). For each $i=1,\ldots,Q$, the integers $W_i$ and $T_i$ represent the distance from the origin to each wall and the number of button presses, respectively, in the $i$-th query ($1\le W_i\le 10^{16}$, $1\le T_i\le 10^{12}$).
The sum of $N$ over all test cases does not exceed $4\times 10^5$. The sum of $Q$ over all test cases does not exceed $4\times 10^5$.
Output Format
For each test case, output $Q$ lines. The $i$-th line should contain the answer to the $i$-th query.
Explanation/Hint
In the first test case, query $1$ causes collisions at $-5$ in phases $2$ and $4$, for $2$ points of damage. In query $2$, both collisions at $-9$ occur during the second button press.
In the second test case, the robot never hits a wall at positive speed, so it takes no damage.