Sort the Strings Revision
题号:NC209390
时间限制:C/C++/Rust/Pascal 3秒,其他语言6秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

There are n+1 strings of length n --- . The characters in the string are numbered from 0 to n-1 (from left to right). The character i of s_0 is the digit i modulo 10.

For example, if n = 5, s_0 is "01234", and if n=14, s_0 is "01234567890123".

You are given a permutation of  : p[0], p[1], ..., p[n-1] and a sequence of digits of length n : d[0], d[1], ..., d[n-1]. can be obtained by replacing character p[i] of s_i with digit d[i]. Note that if d[i] is as same as the character p[i] of s_i, then will be the same as s_i.

Now we want to sort these n+1 strings. The string with index i should be placed to the left of string with index j if and only if s_i is lexicographically smaller than s_j, or s_i equal to s_j and i < j. Let r_i be the new position of string s_i from the left, that is,  is the i-th string from the left after sorting. (Positions are number start from 0.)

For example, if n = 5, p = [4, 3, 0, 1, 2] and d = [5, 0, 0, 0, 0], then "01234", "01235", "01205", "01205", "00205", "00005". So , , , , , and .

Please calculate all r_i for i from 0 to n.

输入描述:

The first line contains one integer t () --- the number of test cases.

The first line of each test contains one positive integer n ().

The second line of each test contains four integers (). Use the following pseudocode for generating the p.
seed =  for i = 0 to n - 1:
    p[i] = i
for i = 1 to n - 1:
    swap the value of  and p[i]
    seed = (seed * p_a + p_b) modulo  
The third line of each test contains four integers  (). Use the following pseudocode for generating the d.

seed =  for i = 0 to n-1:
    d[i] = seed modulo 10
    seed = (seed * d_a + d_b) modulo 

The sum of n across the test cases doesn't exceed .



输出描述:

For each test, You don't need to output the whole sequence of r_i. Instead, you should output a non-negative integer is the result of .
示例1

输入

复制
2
5
1 3 1 4
5 2 0 170
1
0 0 0 1
1000000000 1000000006 1000000006 1000000007

输出

复制
26717147
10000019

说明

The first test corresponds to the example in the statement.