#P10954. [2015杭电多校]MZL's Border

[2015杭电多校]MZL's Border

MZL's Border

Problem Description

As is known to all, MZL is an extraordinarily lovely girl. One day, MZL was playing with her favorite data structure, strings. MZL is really like Fibonacci SequenceFibonacci~Sequence, so she defines Fibonacci StringsFibonacci~Strings in the similar way. The definition of Fibonacci StringsFibonacci~Strings is given below.

  1. fib1=bfib_1 = b
  2. fib2=afib_2 = a
  3. fibi=fibi1fibi2, i>2fib_i = fib_{i - 1}fib_{i - 2},~i > 2 For instance, fib3=ab, fib4=aba, fib5=abaabfib_3 = ab,~fib_4 = aba,~fib_5 = abaab. Assume that a string ss whose length is nn is s1s2s3...sns_1s_2s_3...s_n. Then sisi+1si+2si+3...sjs_is_{i + 1}s_{i + 2}s_{i + 3}...s_j is called as a substring of ss, which is written as s[i:j]s[i : j]. Assume that i<ni < n. If s[1:i]=s[ni+1:n]s[1 : i] = s[n - i + 1 : n], then s[1:i]s[1 : i] is called as a BorderBorder of ss. In BordersBorders of ss, the longest BorderBorder is called as ss' LBorderLBorder. Moreover, s[1:i]s[1 : i]'s LBorderLBorder is called as LBorderiLBorder_i. Now you are given 2 numbers nn and mm. MZL wonders what LBordermLBorder_m of fibnfib_n is. For the number can be very big, you should just output the number modulo 258280327(=2×317+1)258280327(=2 \times 3 ^ {17} + 1). Note that $1\leq T\leq 100,~1\leq n\leq 10^3,~1\leq m\leq |fib_n|$.

Input

The first line of the input is a number TT, which means the number of test cases. Then for the following TT lines, each has two positive integers nn and mm, whose meanings are described in the description.

Output

The output consists of TT lines. Each has one number, meaning fibnfib_n's LBordermLBorder_m modulo 258280327(=2×317+1)258280327(=2 \times 3 ^ {17} + 1).

Sample Input

2
4 3
5 5

Sample Output

1
2

Author

SXYZ

Source

2015 Multi-University Training Contest 5