#include using namespace std; using ll = long long; void solve() { ll N; string s; cin >> N >> s; ll a = stoll(s.substr(s.size() - 3, 3)); ll ans = max(0ll, N - 999); for (ll i = 1; i < min(1000ll, N + 1); i ++) { ll l = i * a, r = i * a + i; ll b = ((l + 999) / 1000) * 1000; if (b < r) { ans ++; } } cout << ans << endl; } int main () { int t; cin >> t; while(t--) solve(); }