This page is still under construction.

Parts of this page are still being built. What you see may change.

Pseudo-random Numbers

Time limit1sMemory limit128 MB

Summary
Given the first L digits of a pseudo-random sequence generated by repeatedly summing adjacent base-B digits, decide whether the T-th element is forced, or report impossible or unpredictable.
Level

Hard8 of 10

Topics
Math, Implementation, Number theory, Brute force
Solved
No attempts yet

Problem

Many applications need good random numbers, cryptography most of all. Radioactive decay is sometimes used as a source of true randomness, but it delivers numbers slowly. Many applications also have to reproduce the same "random" sequence in two different places. A pseudo-random sequence is used instead. Such a sequence is not random, yet it is very hard to tell apart from a truly random one, and it should be hard to predict: knowing the first few elements should not make it easy to work out a later element nobody has seen yet.

The Association of Cryptographic Machinery (ACM) designed an algorithm that produces pseudo-random sequences, and nobody there knows how good it really is, so they want you to test it.

Every element the algorithm produces is an integer between 00 and B−1B - 1 inclusive. The algorithm works as follows.

  1. Start with any seed written in base BB. The seed is a nonempty string of base BB digits, it may begin with zeros, and it can be hundreds of digits long.
  2. Output the last digit (the least significant one) as the next element of the sequence.
  3. Build a new number by writing down the sums of neighbouring digits from left to right. Each sum is written in base BB, so a sum of BB or more takes two digits. With B=10B = 10 the number 845845 becomes 129129, because 8+4=128 + 4 = 12 and 4+5=94 + 5 = 9.
  4. Repeat steps 2 and 3 until the number has a single base BB digit. That digit is the last element of the sequence and the algorithm stops.

With B=10B = 10 and the seed 845845 the numbers are 845845, 129129, 311311 (1+2=31 + 2 = 3 and 2+9=112 + 9 = 11), 4242 (3+1=43 + 1 = 4 and 1+1=21 + 1 = 2) and 66 (4+2=64 + 2 = 6). The last one has a single digit, so the algorithm stops there. The pseudo-random digits it produced are 55, 99, 11, 22 and 66.

You test the generator like this. You are given the first LL elements the generator produced and an integer T>LT > L. Decide whether the first LL elements completely determine the first TT elements. If some seed that produces the given LL elements yields a sequence of fewer than TT elements, then the first TT elements are not determined. The ACM also slipped in a few impossible sequences that no seed can produce, to see how robust your test is.

Input

The first line has one positive integer NN, the number of test cases. Each test case takes three lines. The first line has the base BB (2≤B≤10002 \le B \le 1000). The second line has an integer LL (1≤L≤1001 \le L \le 100) followed by the first LL elements of some sequence; the elements are written in base 10 and lie between 00 and B−1B - 1 inclusive. The third line has the integer TT (L<T≤100000L < T \le 100000), the position of the element to predict.

Output

For each test case print one line:

  • impossible if no seed produces the given sequence;
  • unpredictable if some seed produces the given sequence but the first LL elements do not completely determine the first TT elements;
  • otherwise the TT-th element of the sequence, written in base 10.

Examples1

  1. Example 1

    Input
    3
    10
    5 5 9 6 7 0
    7
    16
    4 11 7 8 4
    12
    2
    5 0 1 1 1 0
    10
    
    Expected output
    8
    unpredictable
    impossible