import sys input = sys.stdin.readline T=int(input()) for tests in range(T): N,S=input().split() N=int(N) x=S.index(".") S=int(S[x+1:]) ANS=0 for i in range(1,min(N,1500)+1): if (S*i)%1000==0: ANS+=1 else: if (S*i)//1000!=(S+1)*i//1000: ANS+=1 if N>1500: ANS+=N-1500 print(ANS)