T = int(input()) for _ in range(T): N, S = input().split() N = int(N) S = int(S.replace(".", "")) cnt = 0 for i in range(1, min(N+1, 1000)): left = S*i right = (S+1)*i-1 if left%1000 == 0 or left//1000 != right//1000: cnt += 1 print(cnt+max(0, N-999))