#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;

#define fastIO          ios_base::sync_with_stdio(false),cin.tie(NULL);
#define test freopen("input.txt", "r", stdin);
#define input(arr)    for(auto &i : arr) cin>>i;
#define print(arr)    for(auto &i : arr) cout<<i<<" "; cout<<nl;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define f(i,a,b) for(int i=a; i<b; i++)
#define fi(j,a,b,i) for(int j=a; j<b; j+=i)
#define all(v) v.begin(), v.end()
#define sorti(v) sort(all(v))
#define sortd(v) sort(all(v), greater<int>())
#define reverse(v) reverse(all(v))
#define SUM(v,sum) sum=0; f(i,0,v.size()) sum+=v[i];
#define INF 1e9+7
#define INFF 1e18+7
#define SQR(x) ((ll)(x) * (x))
#define nl "\n"
#define m0(a) memset(a,0,sizeof(a[0])*a.size());
#define _m0(a,n) memset(a,0,sizeof(a[0])*n);
#define m1(a) memset(a,-1,sizeof(a[0])*a.size());
#define _m1(a,n) memset(a,-1,sizeof(a[0])*n);


void solve(){
    long double p, a,b,c;
    cin>>p>>a>>b>>c;

    ll res = min({ceil(p/a)*a, ceil(p/b)*b, ceil(p/c)*c}) - p;
    cout<<res<<nl;

    return;
}
int main(){
    fastIO
#ifndef ONLINE_JUDGE
    test
#endif
    int t;
    cin>>t;
    while(t--)
        solve();

    return 0;
}
