T = int(input()) for _ in range(T): N, S = map(str, input().split()) IN = int(N) IS = int(S[:-4]) * 1000 + int(S[-3:]) ans = 0 for i in range(min(IN + 1, 1000)): if (IS * i - 1) // 1000 != ((IS + 1) * i - 1) // 1000: ans += 1 ans += max(0, IN - 1000 + 1) print(ans)