Under construction

Still being built. Things may change

Fibonacci Strings 2

Interview

Time limit2sMemory limit1024 MB

Summary
For each test case, count the letter a among the first k characters of the Fibonacci string F_n, where F_0 is a, F_1 is b, and F_i is F_(i-2) followed by F_(i-1).
Level

Medium5 of 10

Topics
Divide and conquer, Recursion, String
Solved
No attempts yet

Problem

Recurrence relations are often used in mathematics. They usually define numerical sequences, but they can also define sequences of strings.

One example of strings defined by a recurrence is the Fibonacci strings F0F_0, F1F_1, …\ldots They are defined as follows: F0=aF_0=a, F1=bF_1=b, Fi=Fi−2Fi−1F_i=F_{i-2}F_{i-1} for i>1i > 1. The first seven Fibonacci strings are a, b, ab, bab, abbab, bababbab, and abbabbababbab.

Dima attends an olympiad programming club and is interested in string algorithms. He recently learned about Fibonacci strings. He quickly realized that their length grows very fast as i increases, so finding all characters of FiF_i requires too much memory. Last time he only looked for some characters of FiF_i. This time he set himself a harder task: count how many times the letter a appears among the first kk characters of FiF_i.

Write a program that solves this problem.

Input

The first line of the input contains the number of test cases TT (1≤T≤1001 \le T \le 100). Each of the next TT lines contains two integers nn and kk (0≤n≤450 \le n \le 45, 1≤k≤∣Fn∣1 \le k \le |F_n|), where ∣Fn∣|F_n| is the length of the string FnF_n. Character positions are numbered from 1.

Output

Print TT lines. Each line contains one number, the answer for the corresponding test case: the number of a characters among the first kk characters of FnF_n.

Examples1

  1. Example 1

    Input
    4
    0 1
    1 1
    3 2
    7 7
    
    Expected output
    1
    0
    1
    3