#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 998244353;
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define faster ios::sync_with_stdio(false);cin.tie(nullptr)

int main() {
    ll T;
    cin >> T;
    while(T--){
        ll N;
        cin >> N;
        ll X1,X2;
        char c;
        cin >> X1 >> c >> X2;
        ll X=X1*1000+X2;
        ll ans=0;
        if(N>=1000) ans+=N-1000;
        for(ll i=1;i<=min(N,(ll)1000);i++){
            ll x1=X*i;
            ll x2=(X+1)*i-1;
            if(x1%1000==0||x1/1000!=x2/1000) ans++;
        }
        cout << ans << endl;
    }
    return 0;
}