GOV01 - BINARY CHALLENGE

no tags 

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

hide comments
Arika Saputro: 2013-05-08 08:18:45

I learn something new from this problem ;D

yaswanth: 2013-04-01 11:23:01

soo similar question!! AC :)
agree with ravindra jadeja!!

Ouditchya Sinha: 2013-02-19 10:49:17

nice question... finally AC! :)

preetam: 2013-01-14 20:43:33

finally...... ACCCCC!!!
Take care of input... long long and also take care of mod...

Very nice problem!!!

god_father: 2013-01-03 18:02:06

be careful about mod operations.

Akb: 2013-01-01 20:35:58

tip: analyze a little and you will convert it into a problem which has a pretty decent standard algorithm... :)

gourav: 2012-12-30 16:22:52

please do this question .... u will learn alot if you don't know the concept which this question want to tell you... :-)

Rohit: 2012-12-30 05:23:53

nice question..:))

unK.nO.wn: 2012-12-28 19:37:49

very nicely designed :-)

Aditya Pande: 2012-12-28 10:03:20

nicely framed problem..:)


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