#include int main () { int t = 0; long long n = 0LL; char s[8] = ""; int res = 0; res = scanf("%d", &t); while (t > 0) { long long ans = 0LL; long long sd = 0LL; int idx = 0; int p = -1; res = scanf("%lld", &n); res = scanf("%s", s); while (s[idx] != '\0') { if (s[idx] == '.') { p = idx; } else { sd *= 10LL; sd += (long long)(s[idx]-'0'); } idx++; } if (p < 0) { sd *= 1000LL; } else if (idx-p == 2) { sd *= 100LL; } else if (idx-p == 3) { sd *= 10LL; } for (long long i = 1LL; i <= 1000LL; i += 1LL) { if (i <= n && (1000LL-(i*sd)%1000LL)%1000LL < i) { ans += 1LL; } } if (n > 1000LL) { ans += n-1000LL; } printf("%lld\n", ans); t--; } return 0; }