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:
'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''


nummap = {}

if __name__ == '__main__':
    T = int(raw_input().strip())

    for j in range(T):
        cool_nos = 0
        cool_nos2 = 0
        (r, k) = raw_input().split()
        r = int(r)
        k = int(k)
        #print "Testcase %d: finding coolnos between 1 and %d, k=%d"%(T,r,k)
        for x in range(5,r+1):
            if not nummap.has_key(x):
                nummap[x] = [0,0]
                #coolness = 0
                #print "finding the coolness of %d"%x
                i = x
                while i >= 5:
                    if nummap.has_key(i) and nummap[i][0] == 1:
                        nummap[x][1] += nummap[i][1]
                        break
                    else:
                        if ((i & 4) and (i & 1) and not (i & 2)):
                            nummap[x][1] += 1
                            #coolness += 1
                        i = i >> 1

                #print "coolness of %d is %d"%(x,nummap[x][1])
                nummap[x][0] = 1


            if nummap[x][1] >= k:
                #print "coolness of %d is %d %d"%(x,coolness,nummap[x][1])
                cool_nos += 1


        print cool_nos