#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int


int main()
 {
   ll  i, j, k, l, p,q,r, sum, t, z,f, n,x1,y1,g,m,maxi,mini,d,x,y,c,a;
   cin>>t;
   while(t--)
   {
     ll arr[3];
     ll b[3];
     cin>>p>>arr[0]>>arr[1]>>arr[2];

     for(i=0;i<3;i++)
     {
       b[i]=(p+arr[i]-1)/arr[i];
       b[i]=b[i]*arr[i]-p;
     }

     sort(b,b+3);
     cout<<b[0]<<endl;

     
    
   }
  return 0;
 }