n = int(input())
for i in range(n):
p, a, b, c = map(int,input().split())
a1 = a - ((p-1) % a +1)
b1 = b - ((p-1) % b +1)
c1 = c - ((p-1) % c +1)
print(min(a1,b1,c1))
#include <iostream>
#include <string>
#include <cmath>
#include <utility>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <algorithm>
#include <iterator>
#include <fstream>
#define ll long long
#define stream stringstream
#define bismillah ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define testcase ll t; cin>>t; while(t--)
using namespace std;
int main() {
bismillah;
testcase
{
ll p,a,b,c; cin >> p >> a >> b >> c;
a = (p % a == 0 ? 0 : a - (p % a));
b = (p % b == 0 ? 0 : b - (p % b));
c = (p % c == 0 ? 0 : c - (p % c));
ll m = min(a,min(b,c));
cout << m << endl;
}
return 0;
}
901A - Hashing Trees | 1283A - Minutes Before the New Year |
1654D - Potion Brewing Class | 1107B - Digital root |
25A - IQ test | 785A - Anton and Polyhedrons |
1542B - Plus and Multiply | 306A - Candies |
1651C - Fault-tolerant Network | 870A - Search for Pretty Integers |
1174A - Ehab Fails to Be Thanos | 1169A - Circle Metro |
780C - Andryusha and Colored Balloons | 1153A - Serval and Bus |
1487C - Minimum Ties | 1136A - Nastya Is Reading a Book |
1353B - Two Arrays And Swaps | 1490E - Accidental Victory |
1335A - Candies and Two Sisters | 96B - Lucky Numbers (easy) |
1151B - Dima and a Bad XOR | 1435B - A New Technique |
1633A - Div 7 | 268A - Games |
1062B - Math | 1294C - Product of Three Numbers |
749A - Bachgold Problem | 1486B - Eastern Exhibition |
1363A - Odd Selection | 131B - Opposites Attract |