Problem: For a number X, let its "Coolness" be defined as the number of "101"s occurring in its binary representation. For example, the number 21 has Coolness 2, since its binary representation is 101012, and the string "101" occurs twice in this representation. 

A number is defined as Very Cool if its Coolness is greater than or equal to K.  Please, output the number of Very Cool integers between 1 and R. 

Input: 
The first line contains an integer T, the number of test cases. 
The next T lines contains two space-separated integers, R and K. 

Output: 
Output T lines, the answer for each test case. 

Constraints: 
1 ≤ T ≤ 100

1 ≤ R ≤ 10^5 
1 ≤ K ≤ 100

SAMPLE INPUT
1
5 1

SAMPLE OUTPUT
1

Solution (in PYTHON):

Solution:
def cool_fact(num):
    s = bin(num)[2:]
    count = 0
    if len(s) < 3:
        return 0
    elif len(s) == 3:
        if s[0] == '1' and s[1] == '0' and s[2] == '1':
            return 1
    elif len(s) > 3:
        for i in range(len(s) - 2):
            if s[i] == '1' and s[i + 1] == '0' and s[i + 2] == '1':
                count += 1
    return count

coolness = [None] * (10 ** 5 + 1)
for i in range(1, 10 ** 5 + 1):
    coolness[i] = cool_fact(i)

t = int(raw_input())
while t > 0:
    t -= 1
    l = map(int, raw_input().split(' '))
    r = l[0]
    k = l[1]
    count = 0
    for i in range(1,r):
        if coolness[i + 1] >= k:
            count += 1
    print count