Q=int(input()) for _ in range(Q): N,S=input().split() N=int(N) x=1000*int(S[:-4])+int(S[-3:]) result=0 for y in range(1,10001): if y>N: break k1=y*x k2=y*(x+1) z1=(k1+999)//1000 z2=(k2+999)//1000 if z2>z1: result+=1 if N>10000: result+=N-10000 print(result)