Valentina had a birthday recently. She got a special piano from parents. They told her about the piano melodies and the connection with the mathematical sequences. She got amazed and started to use piano keys to create her own musical sequences.
The piano has m keys, numbered 0 through m−1.
Valentina has already hit n keys a0,a1,…,an−1 (0≤ai≤m−1), not necessarily distinct. To get a nice melody, each next key to hit must be chosen according to the following formula for k≥n: ak=(∑ni=1(−1)i+1⋅ak−i)%m where % denotes the modulo operation.
E.g., for n=4 it is ak=(ak−1−ak−2+ak−3−ak−4)%m.
Given n, m and z, are you able to find az, i.e. the z-th key hit by Valentina?
The first line of the input contains one integer T denoting the number of test cases.
The first line of each test case description contains three integers n, m and z.
The second line contains n integers a0,a1,…,an−1 denoting keys already hit by Valentina.
For each test case, output the answer in a separate line.
Extra constraints | Points | Which tests |
---|---|---|
z≤106 | 30 | 1-3 |
n≤10 | 40 | 4-7 |
no extra constraints | 30 | 8-10 |
In the first sample test case, we know the first n=4 keys hit by Valentina a0=1,a1=3,a2=3,a3=7. We are asked to find a4. According to the given formula, a4=(a3−a2+a1−a0)%m=(7−3+3−1)%10=6.
In the second sample test case, we are given the same first keys but we are asked to find a5 this time. So, a5=(a4−a3+a2−a1)%m=(6−7+3−3)%10=9.
In the third sample test case, Valentina will hit the same key over and over, i.e. a0=a1=a2=…=1.
Stack Limit for C++ is 8MB. You are allowed to increase it in your code, e.g. using setrlimit().