T = int(input()) assert 1 <= T <= 1000 for _ in range(T): N, S = map(str, input().split()) IN = int(N) assert 1 <= IN <= 10 ** 13 IS = int(S[:-4]) * 1000 + int(S[-3:]) assert 0 <= IS <= 100000 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)