Encoded Coordinates#6421

Encoded Coordinates

1 second
64 megabytes
Hard

In a distant world, a mysterious puzzle competition is being held. In each round, participants must decode secret coordinates, which are used as the answers to some intricate riddle. You are given a series of puzzles for which you must deduce the missing y-coordinates, given that you have already worked out the x-coordinates for each one.

Every coordinate in each puzzle is based on a separate and value, each being a non-negative integer less than a given prime number . Each coordinate is calculated using a certain encoding process. A manual gives you information on how to decode a single coordinate. The input to this decoding process consists of five numbers: , , , , and .

Central to the decoding process is a collection of three functions, defined in terms of each other and the input value :

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

G(n + 1) = K \cdot F(n) + H(n - 1)

H(n + 1) = F(n) + K \cdot G(n)

The values , and 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.

However, you notice some crucial information is missing: you need the value of to calculate . Although this number is unknown, you know that the x and y coordinates are constructed using the same value for . Luckily, you've already uncovered the x-coordinates for each puzzle. Some participants believe this is not enough to determine the y-coordinates, but you want to prove them wrong.

Input

The first line contains a positive integer: the number of test cases, at most . After that, for each test case:

  • one line with the prime number , where 2 \leq P \leq 19997.

  • one line with five space-separated integers A_x, B_x, C_x, K_x and N_x (0 \leq A_x, B_x, C_x, K_x < P and 1 \leq N_x \leq 10^9): the parameters for the x coordinate.

  • one line with five space-separated integers A_y, B_y, C_y, K_y and N_y (0 \leq A_y, B_y, C_y, K_y < P and 1 \leq N_y \leq 10^9): the parameters for the y coordinate.

  • one line with one integer (0 \leq x < P): the x coordinate.

Output

For each test case:

  • one line with one integer: the recovered y coordinate. If there is not exactly one possible y coordinate, print "UNKNOWN" (without quotes) on a single line.

Examples

Example 1

Loading
Loading