import sys input = sys.stdin.readline T = int(input()) for _ in range(T): N, S = input().split() N = int(N) S = int(S.split(".")[1]) ans = max(0, N - 999) for i in range(1, min(N+1, 1000)): l = (S * i + 999) // 1000 r = ((S + 1) * i - 1) // 1000 if l <= r: ans += 1 print(ans)