Longest Common Subsequence

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

Given a sequence ss of length nn and a sequence tt of length mm, find the length of the longest common subsequence of ss and tt.

입력

There are multiple test cases. The first line of input contains an integer TT (1T1031\le T\le 10^3), the number of test cases.

For each test case:

The only line contains seven integers: nn, mm, pp, xx, aa, bb, and cc (1n,m1061 \le n, m \le 10^6, 0x,a,b,c<p1090 \le x, a, b, c < p\le 10^9). Here, nn is the length of ss, and mm is the length of tt.

To avoid large input, you should generate the sequences as follows:

For each i=1,2,,ni = 1, 2, \ldots, n in order, update xx to (ax2+bx+c)modp(a x^2 + b x + c) \bmod p, and then set s_is\_i to xx. And then, for each i=1,2,,mi = 1, 2, \ldots, m in order, update xx to (ax2+bx+c)modp(a x^2 + b x + c) \bmod p, and then set t_it\_i to xx.

It is guaranteed that both the sum of nn and the sum of mm over all test cases do not exceed 10610^6.

출력

For each test case:

Output a single line with a single integer: the length of the longest common subsequence of ss and tt.

힌트

In the first sample, s=\[3,13,183,905]s=\[3,13,183,905] and t=\[731,565,303]t=\[731,565,303].

In the second sample, s=\[0,0,0]s=\[0,0,0] and t=\[0,0,0,0]t=\[0,0,0,0].