#Lutece2878. Easy Math

Easy Math

Migrated from Lutece 2878 Easy Math

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

Yukikaze is learning number theory.

Yukikaze have QQ questions about primes. Each of them is like:


Given 55 integers p,a,b,w,kp, a, b, w, k, find the number of roots (x,y)(x,y) of the equation

(x+yw)ka+bw(modp)(x+y \sqrt {w})^k \equiv a+b\sqrt w \pmod p

If the answer is greater than 00, output any one of them.

Input

The first line contains an integer Q (1Q10)Q\ (1 \leq Q \leq 10).

Each of the next QQ lines contains 55 integers $p, a, b, w, k\ (2 \leq p \leq 10^9, 0 \leq a, b, w < p, 1 \leq k < p^2 - 1)$, pp is a prime, and ww is not a quadratic residue modulo pp. That is, no integer ii satisfies i2w(modp)i^2 \equiv w \pmod p.

Output

For each of the QQ lines, if the root exists, output cnt x y\text{cnt}\ x\ y in a single line. Here xx and yy are an arbitrary root of the corresponding equation, and cnt\text{cnt} is the number of such roots. Otherwise, output 00 in a single line.

Samples

7
5 3 2 2 2
5 0 4 2 3
5 4 0 3 12
5 4 3 2 2
5 4 0 2 13
19260817 6854583 1145415 30095 114514
998244353 994180708 696075960 7729 12784162037563392
2 1 1
3 3 1
12 2 2
0
1 4 0
2 5472139 619723
672850633555968 292438259 192012292

Note

For the first question of the example, the equation is

(x+2y)2=3+22(mod5)(x+\sqrt{2}y)^2=3+2\sqrt{2} \pmod 5

The roots are x=1,y=1x=1,y=1 and x=4,y=4x=4,y=4.

For the second question of the example, the equation is

(x+2y)3=42(mod5)(x+\sqrt{2}y)^3=4\sqrt{2} \pmod 5

The roots are x=0,y=3x=0,y=3, x=2,y=1x=2,y=1 and x=3,y=1x=3,y=1.

You may use __int128 instead of long long to compute products and modulus of large integers when using GCC as the compiler.

Resources

The 20th UESTC Programming Contest Preliminary