logo AlgoBeat OnlineJudge
登录 注册

#206174. [CERC2013] Captain Obvious and the Rabbit-Man

内存限制:256 MiB 时间限制:6000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

It's you, Captain Obvious! - cried the evil Rabbit-Man - you came here to foil my evil plans!

Yes, it's me. - said Captain Obvious.

But... how did you know that would be here, on Sunflower Street?! Did you crack my evil code?

I did. Three days ago, you robbed a bank on Sunflower Street, the next day you blew up Sunflower Street, and yesterday you left quite a mess under number . These are all powers of . And last year you pulled a similar stunt with powers of . You seem to have a knack for Fibonacci numbers, Rabbit-Man.

That's not over! will learn... arithmetics! - Rabbit-Man screamed as he was dragged into custody - You will never know what to expect... Owww! Not my ears, you morons!

Maybe, but right now you are being arrested. - Captain added proudly.

Unfortunately, Rabbit-Man has now indeed learned some more advanced arithmetics. To understand it, let us define the sequence (being not completely unlike the Fibonacci sequence):

,

,

for .

Rabbit-Man has combined all his previous evil ideas into one master plan. On the i-th day, he does a malicious act on the spot number , defined as follows:

The number and the integer coefficients , ak are fixed. Captain Obvious learned , but does not know the coefficients. Given , help him to determine p(k . To avoid overwhelmingly large numbers, do all the calculations modulo a fixed prime number . You may assume that are pairwise distinct modulo . You may also assume that there always exists a unique solution for the given input.

输入格式

The first line of input contains the number of test cases . The descriptions of the test cases follow:

The first line of each test case contains two integers and The second line contains space-separated integers the values of modulo .

输出格式

Print the answers to the test cases in the order in which they appear in the input. For each test case print a single line containing one integer: the value of modulo .

样例

样例输入 1

2
4 619
5 25 125 6
3 101
5 11 29

样例输出 1

30
83

数据范围与提示

Time limit: 6 s, Memory limit: 128 MB.