def solve(): n, s = map(float, input().split()) n = int(n) s = round(s * 1000) s %= 1000 ans = max(0, n - 1000) n = min(n, 1000) for k in range(1, n + 1): x = (k * (s + 1) - 1) // 1000 if k * s <= 1000 * x: ans += 1 print(ans) for _ in range(int(input())): solve()