SMRT Information Sign The MRT Information System tells commuters when the next train will arrive. However, Bob is worried that displaying the next train's arrival time too directly may encourage commuters to run towards the platform, which is dangerous.
Bob is a software developer working on the next iteration of these information signs with safety in mind.
A day starts at time $$$0$$$. There are $$$n$$$ platforms. For platform $$$j$$$, trains arrive every $$$t_j$$$ minutes; that is, trains serving platform $$$j$$$ arrive at times $$$$$$0, t_j, 2t_j, 3t_j, \cdots$$$$$$ The station also has $$$m$$$ information signs. A commuter is estimated to take $$$d_{i,j}$$$ minutes to walk from sign $$$i$$$ to platform $$$j$$$.
Suppose a commuter leaves sign $$$i$$$ at time $$$x$$$ and walks to platform $$$j$$$. The sign displays the number of minutes from time $$$x$$$ until the earliest train they can catch. Formally, a commuter who starts walking from sign $$$i$$$ reaches platform $$$j$$$ at time $$$$$$ x + d_{i,j}. $$$$$$ The sign should display the difference between time $$$x$$$ and the arrival time of the earliest train at platform $$$j$$$ that arrives at or after time $$$x + d_{i,j}$$$.
For example, suppose it takes $$$d_{1,1} = 5$$$ minutes to walk from sign $$$1$$$ to platform $$$1$$$, and trains arrive at platform 1 every $$$t_{1} = 6$$$ minutes.
Help Bob answer $$$q$$$ queries. For each query $$$(x,i,j)$$$, output the number of minutes displayed on sign $$$i$$$ for platform $$$j$$$ at time $$$x$$$ minutes from the start of the day.
$$$q$$$ lines, where the $$$i$$$-th line ($$$1\leq i \leq q$$$) contains the answer to the $$$i$$$-th query.
3 25 7 11 2 35 7 1080 1 14 1 15 1 16 1 27 2 21000 1 30 2 13 2 1
5 1 5 8 7 3 5 7