Encoded Coordinates

No attempts yetTime limit5sMemory limit128 MB

Problem

Sangkeun was tapping a terrorist's phone when he learned that a large attack had been announced. The terrorists send encoded coordinates to each other, and the attack will happen at one of them. Sangkeun managed to intercept a coordinate.

A coordinate splits into an x value and a y value, and both are non-negative integers smaller than a prime PP. The x value and the y value are encoded separately, by the same procedure.

Encoding one value needs AA, BB, CC, KK, NN. The procedure is written with three functions.

  • F(n+1)=G(n)+H(n)F(n+1) = G(n) + H(n)
  • G(n+1)=K×F(n)+H(n1)G(n+1) = K \times F(n) + H(n-1)
  • H(n+1)=F(n)+K×G(n)H(n+1) = F(n) + K \times G(n)

AA, BB, CC are the initial values of the functions.

  • F(1)=AF(1) = A
  • G(1)=BG(1) = B
  • H(1)=CH(1) = C

The value becomes F(N)modPF(N) \bmod P.

One very important piece of information is missing from the procedure above. Computing G(2)G(2) needs H(0)H(0), and nothing reveals H(0)H(0). All that is known about it is that the same H(0)H(0) is used when computing x and when computing y. H(0)H(0) is also a non-negative integer smaller than PP.

Sangkeun happened to get the x value without decoding it. Write a program that recovers H(0)H(0) from x and then computes y.

Input

The first line contains the number of test cases, which is at most 100.

Each test case takes four lines. The first line contains a prime PP (2P199972 \le P \le 19997). The second line contains AxA_x, BxB_x, CxC_x, KxK_x, NxN_x, the values used to encode the x value (0Ax,Bx,Cx,Kx<P0 \le A_x, B_x, C_x, K_x < P, 1Nx1091 \le N_x \le 10^9). The third line contains AyA_y, ByB_y, CyC_y, KyK_y, NyN_y, the values used to encode the y value (0Ay,By,Cy,Ky<P0 \le A_y, B_y, C_y, K_y < P, 1Ny1091 \le N_y \le 10^9). The fourth line contains the intercepted x value (0x<P0 \le x < P).

The intercepted x value really came out of the encoding, so at least one H(0)H(0) produces it.

Output

For each test case, print the y value on its own line. If several H(0)H(0) produce the given x value and they do not all lead to the same y value, print UNKNOWN instead of the y value.