#P1810G. The Maximum Prefix

The Maximum Prefix

No submission language available for this problem.

Description

You're going to generate an array aa with a length of at most nn, where each aia_{i} equals either 11 or 1-1.

You generate this array in the following way.

  • First, you choose some integer kk (1kn1\le k \le n), which decides the length of aa.
  • Then, for each ii (1ik1\le i \le k), you set ai=1a_{i} = 1 with probability pip_{i}, otherwise set ai=1a_{i} = -1 (with probability 1pi1 - p_{i}).

After the array is generated, you calculate si=a1+a2+a3++ais_{i} = a_{1} + a_{2} + a_{3}+ \ldots + a_{i}. Specially, s0=0s_{0} = 0. Then you let SS equal to maxi=0ksi\displaystyle \max_{i=0}^{k}{s_{i}}. That is, SS is the maximum prefix sum of the array aa.

You are given n+1n+1 integers h0,h1,,hnh_{0} , h_{1}, \ldots ,h_{n}. The score of an array aa with maximum prefix sum SS is hSh_{S}. Now, for each kk, you want to know the expected score for an array of length kk modulo 109+710^9+7.

Each test contains multiple test cases. The first line contains a single integer tt (1t50001 \le t \le 5000) — the number of test cases. Their description follows.

The first line contains an integer nn (1n50001\le n \le 5000).

Then for the following nn lines, each line contains two integers xix_{i} and yiy_{i} (0xi<109+70 \le x_{i} < 10^9 + 7, 1yi<109+71\le y_{i} < 10^9 + 7, xiyix_{i} \le y_{i}), indicating pi=xiyip_{i} = \frac{x_{i}}{y_{i}}.

The next line contains n+1n+1 integers h0,h1,,hnh_{0},h_{1}, \ldots, h_{n} (0hi<109+70 \le h_{i} < 10^9 + 7).

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

For each test case, output nn integers in one single line, the ii-th of which denotes the expected score for an array of length ii, modulo 109+710^9 + 7.

Formally, let M=109+7M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to pq1modMp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0x<M0 \le x < M and xqp(modM)x \cdot q \equiv p \pmod{M}.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1t50001 \le t \le 5000) — the number of test cases. Their description follows.

The first line contains an integer nn (1n50001\le n \le 5000).

Then for the following nn lines, each line contains two integers xix_{i} and yiy_{i} (0xi<109+70 \le x_{i} < 10^9 + 7, 1yi<109+71\le y_{i} < 10^9 + 7, xiyix_{i} \le y_{i}), indicating pi=xiyip_{i} = \frac{x_{i}}{y_{i}}.

The next line contains n+1n+1 integers h0,h1,,hnh_{0},h_{1}, \ldots, h_{n} (0hi<109+70 \le h_{i} < 10^9 + 7).

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

Output

For each test case, output nn integers in one single line, the ii-th of which denotes the expected score for an array of length ii, modulo 109+710^9 + 7.

Formally, let M=109+7M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to pq1modMp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0x<M0 \le x < M and xqp(modM)x \cdot q \equiv p \pmod{M}.

Sample Input 1

4
2
1 2
1 2
1 2 3
3
1 3
1 4
5 5
1 1 1 1
3
2 5
4 6
0 2
4 3 2 1
5
5 6
5 7
1 6
1 3
4 7
9 0 4 5 2 4

Sample Output 1

500000005 750000007 
1 1 1 
200000005 333333339 333333339 
500000005 880952391 801587311 781746041 789304620

Note

In the first test case, if we choose k=1k=1, there are 22 possible arrays with equal probabilities: [1][1] and [1][-1]. The SS values for them are 11 and 00. So the expected score is 12h0+12h1=32\frac{1}{2}h_{0} + \frac{1}{2}h_{1} = \frac{3}{2}. If we choose k=2k=2, there are 44 possible arrays with equal probabilities: [1,1][1,1], [1,1][1,-1], [1,1][-1,1], [1,1][-1,-1], and the SS values for them are 2,1,0,02,1,0,0. So the expected score is 12h0+14h1+14h2=74\frac{1}{2}h_{0} + \frac{1}{4}h_{1} + \frac{1}{4}h_{2} = \frac{7}{4}.

In the second test case, no matter what the SS value is, the score is always 11, so the expected score is always 11.