GOV01 - BINARY CHALLENGE

Govind is very fond of playing with binary sequences. One day his brother Mukund challenged him to solve a problem on binary sequences. Since Govind do not have time to solve the problem, he needs your assistance. Help him find the answer to the problem.

The problem is as follows:

A function f(x) is defined such it is equal to the number of binary sequences of length x that do not contain pattern 11.

For example:

  • f(1) = 2 (the only sequences possible are 0, 1)
  • f(3) = 5 (the sequences are 000, 001, 010, 100, 101)

Another function S(n) is defined such that

S(n) = f(1) + f(2) + f(3) + ... + f(n-1) + f(n)

Your task is to find the value of S for the given values of n.

As the S(n) can get too large, you have to print the result mod M

Input

First line of input contains an intger t (number of test cases.)

Then follows t lines each containing 2 space separated numbers n and M.

Output

For each test case output a single integer S(n) mod M.

Constraints

t <= 100

1 <= n, M <= 10^8

Example

Input:
2
1 107
3 2

Output:
2
0

Added by:Govind Lahoti
Date:2012-12-27
Time limit:1s
Source limit:10000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:self

hide comments
2013-05-08 08:18:45 Arika Saputro
I learn something new from this problem ;D
2013-04-01 11:23:01 yaswanth
soo similar question!! AC :)
agree with ravindra jadeja!!
2013-02-19 10:49:17 Ouditchya Sinha
nice question... finally AC! :)
2013-01-14 20:43:33 preetam
finally...... ACCCCC!!!
Take care of input... long long and also take care of mod...

Very nice problem!!!
2013-01-03 18:02:06 god_father
be careful about mod operations.
2013-01-01 20:35:58 Akb
tip: analyze a little and you will convert it into a problem which has a pretty decent standard algorithm... :)
2012-12-30 16:22:52 gourav
please do this question .... u will learn alot if you don't know the concept which this question want to tell you... :-)
2012-12-30 05:23:53 Rohit
nice question..:))
2012-12-28 19:37:49 unK.nO.wn
very nicely designed :-)
2012-12-28 10:03:20 Aditya Pande
nicely framed problem..:)
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.