ENCODE - Encoded Coordinates

You have been monitoring a terrorist cell that is planning a big attack somewhere close. The terrorists have been sending encoded coordinates to each other, and you suspect that the attack will happen at one of the locations described by these coordinates. You have intercepted the relevant communications for a series of locations you want to investigate. Every location consists of a separate x and y coordinate, each being a non-negative integer smaller than some prime number P. Both coordinates are encoded separately using the same process. A source has given you information on the process of decoding a single coordinate. The input of this process consists of five values: A, B, C, K and N. Central to the decoding process is a collection of three functions, defined in terms of each other and the input value K:

F(n + 1) = G(n) + H(n)

G(n + 1) = K * F(n) + H(n - 1)

H(n + 1) = F(n) + K * G(n)

The values A, B and C are used to initialize these functions as follows:

F(1) = A, G(1) = B, H(1) = C

The decoded coordinate is the value of F(N) mod P. As you might have noticed, there is some crucial information missing: you need the value of H(0) to calculate G(2). While you do not have this value, you do know that the x and y coordinate use the same value for H(0). Luckily, you have managed to already obtain the x coordinates for the locations. Your colleagues have suggested that this does not give you enough information to calculate the y coordinates, but you want to prove them wrong.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

# one line with the prime number P (2 ≤ P ≤ 19997).

# one line with five space-separated integers Ax, Bx, Cx, Kx and Nx (0 ≤ Ax, Bx, Cx, Kx < P and 1 ≤ Nx ≤ 10^9): the parameters for the x coordinate.

# one line with five space-separated integers Ay, By, Cy, Ky and Ny (0 ≤ Ay, By, Cy, Ky < P and 1 ≤ Ny ≤ 10^9): the parameters for the y coordinate.

# one line with one integer x (0 ≤ x < P): the x coordinate.

Output

Per test case:

# one line with one integer: the calculated y coordinate. If your colleagues are right, and there is not exactly one valid y coordinate, print “UNKNOWN” on a single line instead.

Example

Input:
2
11
2 3 5 7 4
4 6 8 9 7
5
7
1 6 2 5 1
3 4 2 5 3
1

Output:
7
UNKNOWN

Added by:Bidhan
Date:2013-11-03
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:BAPC 2013

hide comments
2018-10-03 22:01:07 :D
Great problem, thanks for setting it up Bidhan.

PS. I tested extra spaces in the output and it doesn't affect the AC status. This note can be removed from description.

[Simes]: Done.

Last edit: 2023-07-19 16:06:16
2013-12-30 20:20:54 Derek Illchuk
2 <= P <= 19997
0 <= Ax,Bx,Cx,Kx < P and 1 <= Nx <= 10^9
0 <= x < P
2013-11-07 16:25:39 Andrey Maksimenko
Please, edit input description.

Last edit: 2013-11-07 16:26:07
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.