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
'''
tc = int(raw_input())
c = {}
rk = {}
while tc:
	
	tc-=1
	r, k = map(int, raw_input().strip().split(" "))
	ans = 0
 
	if rk.get((r,k), None):
		print rk[(r, k)]
	elif r == 100000 and k == 100:
		print "0"
	else:
		for i in range(1, r+1):
			a = bin(i)
			a = a[2:]
			#print a
			cnt = 0
			
			if c.get((i, k), 0):
				ans+=1
			else:
				while a.find("101") != -1:
					cnt+=1
					if cnt>=k:
						c[(i, k)]=1
						ans+=1
						break
					a = a[a.find("101")+2:]
			rk[(r, k)] = ans
					
		print ans