Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.

It takes the first swimmer exactly $a$ minutes to swim across the entire pool and come back, exactly $b$ minutes for the second swimmer and $c$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $0$, $a$, $2a$, $3a$, ... minutes after the start time, the second one will be at $0$, $b$, $2b$, $3b$, ... minutes, and the third one will be on the left side of the pool after $0$, $c$, $2c$, $3c$, ... minutes.

You came to the left side of the pool exactly $p$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.


-----Input-----

The first line of the input contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Next $t$ lines contains test case descriptions, one per line.

Each line contains four integers $p$, $a$, $b$ and $c$ ($1 \leq p, a, b, c \leq 10^{18}$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.


-----Output-----

For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.


-----Examples-----

Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8


-----Note-----

In the first test case, the first swimmer is on the left side in $0, 5, 10, 15, \ldots$ minutes after the start time, the second swimmer is on the left side in $0, 4, 8, 12, \ldots$ minutes after the start time, and the third swimmer is on the left side in $0, 8, 16, 24, \ldots$ minutes after the start time. You arrived at the pool in $9$ minutes after the start time and in a minute you will meet the first swimmer on the left side.

In the second test case, the first swimmer is on the left side in $0, 6, 12, 18, \ldots$ minutes after the start time, the second swimmer is on the left side in $0, 10, 20, 30, \ldots$ minutes after the start time, and the third swimmer is on the left side in $0, 9, 18, 27, \ldots$ minutes after the start time. You arrived at the pool $2$ minutes after the start time and after $4$ minutes meet the first swimmer on the left side.

In the third test case, you came to the pool $10$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!

In the fourth test case, all swimmers are located on the left side in $0, 9, 18, 27, \ldots$ minutes after the start time. You arrived at the pool $10$ minutes after the start time and after $8$ minutes meet all three swimmers on the left side.

Difficulty: EASY
Tags: Mathematics

Starter code:

Solutions:
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	l = [p % a, p % b, p % c]
	if 0 in l:
		print(0)
	else:
		l1 = [a - l[0], b - l[1], c - l[2]]
		print(min(l1))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	print(min(-p % a, -p % b, -p % c))


t = int(input())
for k in range(t):
	(p, a, b, c) = map(int, input().split())
	a1 = ((p - 1) // a + 1) * a - p
	b1 = ((p - 1) // b + 1) * b - p
	c1 = ((p - 1) // c + 1) * c - p
	print(min(a1, b1, c1))


import math
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	r = []
	for item in [a, b, c]:
		if p > item:
			if p % item == 0:
				r.append(0)
			else:
				r.append(item - p % item)
		elif p < item:
			r.append(item - p)
		else:
			r.append(0)
	print(min(r))


t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	a1 = p // a + 1
	b1 = p // b + 1
	c1 = p // c + 1
	l = [a1 * a - p, b1 * b - p, c1 * c - p]
	if (p % a != 0 and p % b != 0 and (p % c != 0)) and l != [a, b, c]:
		print(min(l))
	else:
		print(0)


from math import *
from collections import *
from functools import lru_cache
N = int(1000000000.0 + 7)

def check():
	pass

def read():
	return tuple(map(int, input().strip().split(' ')))
(n2,) = read()
for _ in range(n2):
	time = read()
	p = time[0]
	ans = 1e+18
	for t in time[1:]:
		if p % t != 0:
			ans = min(ans, t * (p // t + 1) - p)
		else:
			ans = 0
	print(ans)


ints_fn = lambda : int(input())
strs_fn = lambda : input()
ints_arr = lambda : [int(val) for val in input().split(' ')]

def solve(a, b, c, p):
	if not p % a or not p % b or (not p % c):
		print(0)
		return
	ans = float('inf')
	for val in (a, b, c):
		ans = min(ans, val - p % val)
	print(ans)
	return
for _ in range(ints_fn()):
	(p, a, b, c) = ints_arr()
	solve(a, b, c, p)


def mytest():
	for s in [*open(0)][1:]:
		(p, *a) = map(int, s.split())
		print(min((-p % x for x in a)))
mytest()


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	t1 = (a - p % a + a) % a
	t2 = (b - p % b + b) % b
	t3 = (c - p % c + c) % c
	ans = min(t1, t2, t3)
	print(ans)


t = int(input())
while t:
	(p, a, b, c) = map(int, input().split())
	q = p // a
	w = p // b
	e = p // c
	if p % a:
		q += 1
	if p % b:
		w += 1
	if p % c:
		e += 1
	q = q * a - p
	w = w * b - p
	e = e * c - p
	mn = min(q, w, e)
	print(mn)
	t -= 1


def tejava(p, a, b, c):
	if p % a == 0 or p % b == 0 or p % c == 0:
		return 0
	return min(a - p % a, b - p % b, c - p % c)
t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	print(tejava(p, a, b, c))


t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split(' '))
	pa = a
	pb = b
	pc = c
	ra = 0
	rb = 0
	rc = 0
	if p <= a:
		a -= p
	elif p % a == 0:
		a = 0
	else:
		a -= p % a
	if p <= b:
		b -= p
	elif p % b == 0:
		b = 0
	else:
		b -= p % b
	if p <= c:
		c -= p
	elif p % c == 0:
		c = 0
	else:
		c -= p % c
	print(min(a, b, c))


num = int(input())
for i in range(num):
	info = input()
	info = info.split(' ')
	p = int(info[0])
	a = int(info[1])
	b = int(info[2])
	c = int(info[3])
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		temp = []
		if a > p:
			temp.append(a - p)
		else:
			temps = p // a + 1
			temps = a * temps
			temp.append(temps - p)
		if b > p:
			temp.append(b - p)
		else:
			temps = p // b + 1
			temps = b * temps
			temp.append(temps - p)
		if c > p:
			temp.append(c - p)
		else:
			temps = p // c + 1
			temps = c * temps
			temp.append(temps - p)
		print(min(temp))


t = int(input())
for _ in range(t):
	(p, a, b, c) = (int(i) for i in input().split())
	res = min((a - p) % a, (b - p) % b, (c - p) % c)
	print(res)


import math
from collections import defaultdict

def get(a, b):
	return int(a / b + (a % b != 0))
t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	d1 = get(p, a) * a
	d2 = get(p, b) * b
	d3 = get(p, c) * c
	print(min(d1 - p, d2 - p, d3 - p))


t = int(input())
for i1 in range(t):
	(p, a, b, c) = map(int, input().split())
	print(min(a * (p // a + (p % a != 0)) - p, b * (p // b + (p % b != 0)) - p, c * (p // c + (p % c != 0)) - p))


n = int(input())
for i in range(n):
	(p, a, b, c) = map(int, input().split())
	a1 = ((p - 1) // a + 1) * a - p
	b1 = ((p - 1) // b + 1) * b - p
	c1 = ((p - 1) // c + 1) * c - p
	print(min(a1, b1, c1))


from collections import Counter, defaultdict, OrderedDict, deque
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from typing import List
import itertools
import sys
import math
import heapq
import string
import random
(MIN, MAX, MOD) = (-1061109567, 1061109567, 1000000007)

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
for _ in range(N()):
	(p, a, b, c) = RL()
	(c1, c2, c3) = (p // a, p // b, p // c)
	c1 = c1 * a if c1 * a >= p else (c1 + 1) * a
	c2 = c2 * b if c2 * b >= p else (c2 + 1) * b
	c3 = c3 * c if c3 * c >= p else (c3 + 1) * c
	ans = float('inf')
	ans = min(ans, abs(p - c1))
	ans = min(ans, abs(p - c2))
	ans = min(ans, abs(p - c3))
	print(ans)


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	a1 = 0
	a2 = 0
	a3 = 0
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		a1 = a * (p // a) + a - p
		a2 = b * (p // b) + b - p
		a3 = c * (p // c) + c - p
		print(min(a1, a2, a3))


mod = int(1000000000.0) + 7

def ip(mode):
	if mode == 0:
		return int(input())
	if mode == 1:
		return list(map(int, input().split()))
	if mode == 2:
		return input()
	if mode == 3:
		return list(map(str, input().split()))
	if mode == 4:
		return map(int, input().split())
	if mode == 5:
		return map(str, input().split())
for _ in range(int(input())):
	(p, a, b, c) = ip(4)
	x = (p // a + (p % a != 0)) * a
	y = (p // b + (p % b != 0)) * b
	z = (p // c + (p % c != 0)) * c
	print(min([x, y, z]) - p)


a = int(input())
for x in range(a):
	(b, c, d, e) = map(int, input().split())
	if b % c > 0:
		c = (b // c + 1) * c
	else:
		c = b // c * c
	if b % d > 0:
		d = (b // d + 1) * d
	else:
		d = b // d * d
	if b % e > 0:
		e = (b // e + 1) * e
	else:
		e = b // e * e
	print(min(c - b, d - b, e - b))


t = int(input())
import math
while t > 0:
	t -= 1
	(p, a, b, c) = map(int, input().split())
	res = 0
	ra = ((p - 1) // a + 1) * a
	rb = ((p - 1) // b + 1) * b
	rc = ((p - 1) // c + 1) * c
	res = min(ra, rb, rc) - p
	print(abs(res))


def ceil(a, b):
	if a % b == 0:
		return a // b
	return a // b + 1

def solve():
	(p, a, b, c) = map(int, input().split(' '))
	(x, y, z) = (a * ceil(p, a), b * ceil(p, b), c * ceil(p, c))
	print(min(x, y, z) - p)
for _ in range(int(input())):
	solve()


import math
import collections

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a / gcd(a, b) * b

def nCr(n, r):
	return math.factorial(n) / (math.factorial(r) * math.factorial(n - r))

def isKthBitSet(n, k):
	if n & 1 << k - 1:
		return True
	else:
		return False

def solve():
	(p, a, b, c) = map(int, input().split())
	if a < p:
		a1 = p // a * a
		if p % a != 0:
			a1 += a
	else:
		a1 = a
	if b < p:
		b1 = p // b * b
		if p % b != 0:
			b1 += b
	else:
		b1 = b
	if c < p:
		c1 = p // c * c
		if p % c != 0:
			c1 += c
	else:
		c1 = c
	print(min(a1 - p, b1 - p, c1 - p))
t = int(input())
while t > 0:
	t -= 1
	solve()


t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	(p_a, p_b, p_c) = (p % a, p % b, p % c)
	if not (p_a and p_b and p_c):
		print(min([p_a, p_b, p_c]))
	else:
		print(min([a - p_a, b - p_b, c - p_c]))


import sys
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	(p, a, b, c) = map(int, stdin.readline().split())
	ans = min((a - p % a) % a, (b - p % b) % b, (c - p % c) % c)
	print(ans)


n = int(input())
for i in range(n):
	l = []
	lt = []
	(p, a, b, c) = list(map(int, input().split()))
	l.append(p % a)
	l.append(p % b)
	l.append(p % c)
	lt.append(a - p % a)
	lt.append(b - p % b)
	lt.append(c - p % c)
	minn = min(l)
	minnn = min(lt)
	if minn == 0:
		print(minn)
	else:
		print(minnn)


import math
res = []
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	a1 = p % a
	b1 = p % b
	c1 = p % c
	if a1 == 0:
		res.append(0)
	elif b1 == 0:
		res.append(0)
	elif c1 == 0:
		res.append(0)
	else:
		res.append(min(a - a1, b - b1, c - c1))
for i in res:
	print(i)


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	x = 0
	y = 0
	z = 0
	if p % a != 0:
		x = a - p % a
	if p % b != 0:
		y = b - p % b
	if p % c != 0:
		z = c - p % c
	print(min(x, y, z))


def minium(p, a, b, c):
	msf = 0
	if p % a == 0 or p % b == 0 or p % c == 0:
		return 0
	else:
		msf = min(a - p % a, b - p % b, c - p % c)
	return msf
t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	print(minium(p, a, b, c))


def func():
	n = int(input())
	for _ in range(n):
		(p, a, b, c) = map(int, input().strip().split())
		if p % a == 0 or p % b == 0 or p % c == 0:
			print(0)
		else:
			print(min(a - p % a, b - p % b, c - p % c))
func()


test_cases = int(input())
for i in range(test_cases):
	(time_come_to_pool, a_lap_of_first_swimmer, a_lap_of_second_swimmer, a_lap_of_third_swimmer) = map(int, input().split(' '))
	if time_come_to_pool % a_lap_of_first_swimmer == 0 or time_come_to_pool % a_lap_of_second_swimmer == 0 or time_come_to_pool % a_lap_of_third_swimmer == 0:
		print(0)
	else:
		time_wait_for_first_swimmer = a_lap_of_first_swimmer * (time_come_to_pool // a_lap_of_first_swimmer + 1)
		time_wait_for_second_swimmer = a_lap_of_second_swimmer * (time_come_to_pool // a_lap_of_second_swimmer + 1)
		time_wait_for_third_swimmer = a_lap_of_third_swimmer * (time_come_to_pool // a_lap_of_third_swimmer + 1)
		print(min(time_wait_for_first_swimmer, time_wait_for_second_swimmer, time_wait_for_third_swimmer) - time_come_to_pool)


tc = int(input())

def waittime(p, a, b, c):
	if p % a == 0 or p % b == 0 or p % c == 0:
		return 0
	return min(a - p % a, b - p % b, c - p % c)
for i in range(tc):
	arr = input()
	arr = arr.split()
	(p, a, b, c) = list(map(int, arr))
	print(waittime(p, a, b, c))


from typing import Match
import math
t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	ap = a - p % a if p % a != 0 else p % a
	bp = b - p % b if p % b != 0 else p % b
	cp = c - p % c if p % c != 0 else p % c
	l = [ap, bp, cp]
	print(min([x for x in l if x >= 0]))


def some_random_function():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)

def some_random_function5():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)
import os, sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		(p, a, b, c) = map(int, input().split())
		if not p % a or not p % b or (not p % c):
			print(0)
		else:
			print(min(a - p % a, b - p % b, c - p % c))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def some_random_function1():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)

def some_random_function2():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)

def some_random_function3():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)

def some_random_function4():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)

def some_random_function6():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)

def some_random_function7():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)

def some_random_function8():
	x = 10
	x *= 100
	i_dont_know = x
	why_am_i_writing_this = x * x
	print(i_dont_know)
	print(why_am_i_writing_this)
main()


t = int(input())
for i in range(0, t):
	(p, a, b, c) = [int(x) for x in input().split()]
	a = (p + a - 1) // a * a
	b = (p + b - 1) // b * b
	c = (p + c - 1) // c * c
	print(min(a, b, c) - p)


def mytest():
	for i in range(int(input())):
		(p, a, b, c) = list(map(int, input().split()))
		mini = 0
		ma = p % a
		if ma != 0:
			t = a - ma
			mini = t
		else:
			print(0)
			continue
		mb = p % b
		if mb != 0:
			t = b - mb
			mini = min(mini, t)
		else:
			print(0)
			continue
		mc = p % c
		if mc != 0:
			t = c - mc
			mini = min(mini, t)
		else:
			print(0)
			continue
		print(mini)
mytest()


t = int(input())
for i in range(t):
	(p, a, b, c) = [int(x) for x in input().split()]
	(a1, b1, c1) = (0, 0, 0)
	if p < a:
		a1 = a - p
	elif p % a == 0:
		a1 = 0
	else:
		a1 = a - p % a
	if p < b:
		b1 = b - p
	elif p % b == 0:
		b1 = 0
	else:
		b1 = b - p % b
	if p < c:
		c1 = c - p
	elif p % c == 0:
		c1 = 0
	else:
		c1 = c - p % c
	print(min(a1, b1, c1))


t = int(input())
for _ in range(t):
	(p, a, b, c) = [int(i) for i in input().strip().split()]
	temp_a = p % a
	temp_b = p % b
	temp_c = p % c
	if temp_a == 0 or temp_b == 0 or temp_c == 0:
		print(0)
	else:
		print(min(a - p % a, b - p % b, c - p % c))


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from itertools import permutations as per
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	(p, a, b, c) = R()
	print(min((a - p % a) % a, (b - p % b) % b, (c - p % c) % c))


n = int(input())
for _ in range(n):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		print(min(abs(p % a - a), abs(p % b - b), abs(p % c - c)))


from sys import stdin, stdout
t = int(stdin.readline())
for i in range(t):
	out = 1e+18
	(p, a, b, c) = [int(x) for x in stdin.readline().split()]
	for j in (a, b, c):
		mod = j - p % j if p % j > 0 else 0
		if mod < out:
			out = mod
	stdout.write(str(out) + '\n')


n = int(input())
for i in range(n):
	(p, a, b, c) = map(int, input().split())
	xa = a - p % a
	xb = b - p % b
	xc = c - p % c
	if xa == a or xb == b or xc == c:
		ans = 0
	else:
		ans = min(xa, xb, xc)
	print(ans)


for _ in range(int(input())):
	(p, *a) = map(int, input().split())
	ans = []
	for x in a:
		if p < x:
			ans.append(x - p)
		elif p % x == 0:
			ans.append(0)
		else:
			ans.append((p // x + 1) * x - p)
	print(min(ans))


n = int(input())
for i in range(n):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		z = a - p % a
		x = b - p % b
		y = c - p % c
		print(min(z, x, y))


inn = lambda : int(input())
inm = lambda : map(int, input().split())
ins = lambda : str(input())
ina = lambda : list(map(int, input().split()))

def solve():
	(p, a, b, c) = inm()
	aa = (p + a - 1) // a
	bb = (p + b - 1) // b
	cc = (p + c - 1) // c
	print(min(aa * a, bb * b, cc * c) - p)

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()
main()


import sys
input = sys.stdin.readline

def solve():
	(p, a, b, c) = map(int, input().split())
	return 0 if p % a == 0 or p % b == 0 or p % c == 0 else min(a * (p // a + 1), b * (p // b + 1), c * (p // c + 1)) - p
for _ in range(int(input())):
	print(solve())


def solve():
	(p, a, b, c) = map(int, input().split(' '))
	ans = min([(a - p % a) % a, (b - p % b) % b, (c - p % c) % c])
	print(ans)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	x = (p + a - 1) // a
	y = (p + b - 1) // b
	z = (p + c - 1) // c
	ans = min(x * a - p, y * b - p, z * c - p)
	print(ans, end='\n')


t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		lol1 = p // a
		lol2 = p // b
		lol3 = p // c
		lol1 += 1
		lol2 += 1
		lol3 += 1
		k1 = lol1 * a - p
		k2 = lol2 * b - p
		k3 = lol3 * c - p
		print(min(k1, k2, k3))


import math
import sys
import collections
from collections import Counter
import bisect
import string

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	ans = 10 ** 20
	factor = (p + a - 1) // a
	no = factor * a
	ans = min(ans, no - p)
	factor = (p + b - 1) // b
	no = factor * b
	ans = min(ans, no - p)
	factor = (p + c - 1) // c
	no = factor * c
	ans = min(ans, no - p)
	print(ans)


import sys
zz = 1
sys.setrecursionlimit(10 ** 5)
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def ii():
	return input().rstrip()

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def dadd(d, p, val):
	if p in d:
		d[p].append(val)
	else:
		d[p] = [val]

def gi():
	return [xx for xx in input().split()]

def gtc(tc, ans):
	print('Case #' + str(tc) + ':', ans)

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def prec(a, pre):
	for i in a:
		pre.append(pre[-1] + i)
	pre.pop(0)

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
uu = t
while t > 0:
	t -= 1
	(p, a, b, c) = mi()
	mini = 10 ** 19
	for i in [a, b, c]:
		mini = min(mini, (p + i - 1) // i * i - p)
	print(mini)


from sys import stdin, stdout
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import *
input = stdin.readline
from random import sample
t = int(input())
from collections import Counter, deque
from math import sqrt, ceil, log2, gcd

class DisjSet:

	def __init__(self, n):
		self.rank = [1] * n
		self.parent = [i for i in range(n)]

	def find(self, x):
		if self.parent[x] != x:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, x, y):
		xset = self.find(x)
		yset = self.find(y)
		if xset == yset:
			return
		if self.rank[xset] < self.rank[yset]:
			self.parent[xset] = yset
		elif self.rank[xset] > self.rank[yset]:
			self.parent[yset] = xset
		else:
			self.parent[yset] = xset
			self.rank[xset] = self.rank[xset] + 1

def f(arr, i, j, d, dist):
	if i == j:
		return
	nn = max(arr[i:j])
	for tl in range(i, j):
		if arr[tl] == nn:
			dist[tl] = d
			f(arr, i, tl, d + 1, dist)
			f(arr, tl + 1, j, d + 1, dist)

def ps(n):
	cp = 0
	while n % 2 == 0:
		n = n // 2
		cp += 1
	for ps in range(3, ceil(sqrt(n)) + 1, 2):
		while n % ps == 0:
			n = n // ps
			cp += 1
	if n != 1:
		return False
	return True

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def factorials(n, r):
	slr = n
	dpr = r
	qlr = 1
	qs = 1
	mod = 10 ** 9 + 7
	for ip in range(n - r + 1, n + 1):
		qlr = qlr * ip % mod
	for ij in range(1, r + 1):
		qs = qs * ij % mod
	ans = qlr * modInverse(qs) % mod
	return ans

def modInverse(b):
	qr = 10 ** 9 + 7
	return pow(b, qr - 2, qr)
tt = [xa ** 3 for xa in range(0, 10 ** 4 + 1)]
qq = set(tt)
for jj in range(t):
	(p, a, b, c) = [int(x) for x in input().strip().split()]
	s1 = a - p % a if p % a != 0 else 0
	s2 = b - p % b if p % b != 0 else 0
	s3 = c - p % c if p % c != 0 else 0
	print(min([s1, s2, s3]))


t = int(input())
for i in range(0, t):
	array = input()
	(px, ax, bx, cx) = array.split()
	p = int(px)
	a = int(ax)
	c = int(cx)
	b = int(bx)
	print(min((a - p % a) % a, (b - p % b) % b, (c - p % c) % c))


a = int(input())
for i in range(a):
	q = input().split()
	for j in range(4):
		q[j] = int(q[j])
	(p, a, b, c) = (q[0], q[1], q[2], q[3])
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		a1 = p // a
		a1 = a1 * a
		a1 = a1 + a
		a1 = a1 - p
		b1 = p // b
		b1 = b1 * b
		b1 = b1 + b
		b1 = b1 - p
		c1 = p // c
		c1 = c1 * c
		c1 = c1 + c
		c1 = c1 - p
		print(min(a1, b1, c1))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	if p == a or p == b or p == c:
		print(0)
	else:
		m = float('inf')
		if p < a:
			m = min(a - p, m)
		else:
			m = min(m, -p % a)
		if p < b:
			m = min(b - p, m)
		else:
			m = min(m, -p % b)
		if p < c:
			m = min(c - p, m)
		else:
			m = min(m, -p % c)
		print(m)


import math
import os
import random
import re
import sys
for _ in range(int(input())):
	(p, a, b, c) = list(map(int, input().split()))
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
		continue
	x = (p // a + 1) * a - p
	y = (p // b + 1) * b - p
	z = (p // c + 1) * c - p
	d = min(x, y, z)
	print(d)


try:
	t = int(input())
	while t != 0:
		(p, a, b, c) = map(int, input().split())
		if p % a == 0 or p % b == 0 or p % c == 0:
			print(0)
		else:
			print(min(min(a - p % a, b - p % b), c - p % c))
		t -= 1
except EOFError:
	print(' ')


import math
d = int(input())
while d:
	(p, a, b, c) = [int(x) for x in input().split()]
	if p % a == 0 or p % b == 0 or p % c == 0:
		print('0')
	else:
		print(min(a - p % a, b - p % b, c - p % c))
	d = d - 1


import math
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	ans = min({a - p % a, b - p % b, c - p % c})
	if p % a == 0 or p % b == 0 or p % c == 0:
		ans = 0
	print(ans)


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
from collections import defaultdict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj

def sieve():
	li = [True] * (10 ** 3 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	(prime, cur) = ([0] * 200, 0)
	for i in range(10 ** 3 + 5):
		if li[i] == True:
			prime[cur] = i
			cur += 1
	return prime
spf = [1000000000.0 for i in range(10 ** 6 + 1)]

def SPF():
	mx = 10 ** 6 + 1
	spf[1] = 1
	for i in range(2, mx):
		if spf[i] == 1000000000.0:
			spf[i] = i
			for j in range(i * i, mx, i):
				if i < spf[j]:
					spf[j] = i
	return

def prime(n):
	prm = deque()
	d = {}
	while n != 1:
		prm.append(spf[n])
		n = n // spf[n]
	for ele in prm:
		d[ele] = d.get(ele, 0) + 1
	return d
mod = 998244353
inf = 10000000000000000

def solve():
	(p, a, b, c) = L()
	(ta, tb, tc) = (p // a, p // b, p // c)
	if p % a:
		ta += 1
	if p % b:
		tb += 1
	if p % c:
		tc += 1
	print(min(a * ta - p, b * tb - p, c * tc - p))
	return
t = II()
for i in range(t):
	solve()


from sys import stdin, stdout
t = int(stdin.readline())
for i in range(t):
	(p, a, b, c) = [int(x) for x in stdin.readline().split()]
	moda = a - p % a if p % a > 0 else 0
	modb = b - p % b if p % b > 0 else 0
	modc = c - p % c if p % c > 0 else 0
	stdout.write(str(min((moda, modb, modc))) + '\n')


n = int(input())
for i in range(n):
	(p, a, b, c) = list(map(int, input().split()))
	k = min(p % a, p % b, p % c)
	if k == 0:
		print(k)
	else:
		print(min(a - p % a, b - p % b, c - p % c))


from math import *
n = int(input())
for i in range(n):
	a = list(map(int, input().split()))
	if a[0] % a[1] == 0 or a[0] % a[2] == 0 or a[0] % a[3] == 0:
		print(0)
	else:
		x = a[1] - a[0] % a[1]
		y = a[2] - a[0] % a[2]
		z = a[3] - a[0] % a[3]
		print(min(x, min(y, z)))


for _ in range(int(input())):
	(p, a, b, c) = list(map(int, input().split(' ')))
	print(min((p + a - 1) // a * a % p if p > a else a - p, (p + b - 1) // b * b % p if p > b else b - p, (p + c - 1) // c * c % p if p > c else c - p))


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(p, a, b, c) = map(int, stdin.readline().split())
	A = (p + a - 1) // a * a - p
	B = (p + b - 1) // b * b - p
	C = (p + c - 1) // c * c - p
	print(min(A, B, C))


for _ in range(int(input())):
	(p, a, b, c) = input().split()
	(p, a, b, c) = [int(p), int(a), int(b), int(c)]
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		x1 = (p // a + 1) * a
		x2 = (p // b + 1) * b
		x3 = (p // c + 1) * c
		print(min(x1 - p, x2 - p, x3 - p))


for i in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	if not p % a or not p % b or (not p % c):
		print(0)
	else:
		print(min(a - p % a, b - p % b, c - p % c))


t = int(input())
out = []
for i in range(t):
	(p, a, b, c) = [int(x) for x in input().split()]
	moda = a - p % a if p % a > 0 else 0
	modb = b - p % b if p % b > 0 else 0
	modc = c - p % c if p % c > 0 else 0
	out.append(str(min((moda, modb, modc))))
print('\n'.join(out))


def solve(p, a, b, c):
	if p % c == 0 or p % a == 0 or p % b == 0:
		print(0)
		return
	(div_a, rem_a) = divmod(p, a)
	(div_b, rem_b) = divmod(p, b)
	(div_c, rem_c) = divmod(p, c)
	if not rem_a or not rem_b or (not rem_c):
		print(0)
	else:
		print(min(a - rem_a, b - rem_b, c - rem_c))
	return
t = int(input())
while t:
	t -= 1
	(p, a, b, c) = [int(val) for val in input().split(' ')]
	solve(p, a, b, c)


t = int(input())
for jgdsljfgd in range(t):
	(p, a, b, c) = map(int, input().split())
	print(min((p + a - 1) // a * a - p, (p + b - 1) // b * b - p, (p + c - 1) // c * c - p))


import math
alf = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	s1 = (p + a - 1) // a * a - p
	s2 = (p + b - 1) // b * b - p
	s3 = (p + c - 1) // c * c - p
	print(min(s1, s2, s3))


import sys
n = int(sys.stdin.readline())
for _ in range(n):
	(k, a, b, c) = map(int, sys.stdin.readline().split())
	(ax, bx, cx) = (k // a, k // b, k // c)
	if k % a == 0:
		ax -= 1
	if k % b == 0:
		bx -= 1
	if k % c == 0:
		cx -= 1
	(at, bt, ct) = (a * (ax + 1) - k, b * (bx + 1) - k, c * (cx + 1) - k)
	print(min(at, bt, ct))


T = int(input())
for x in range(T):
	d = [int(y) for y in input().split()]
	p = d[0]
	a = d[1]
	b = d[2]
	c = d[3]
	a1 = 0
	b1 = 0
	c1 = 0
	if p % a == 0:
		a1 = p % a
	else:
		a1 = a - p % a
	if p % b == 0:
		b1 = p % b
	else:
		b1 = b - p % b
	if p % c == 0:
		c1 = p % c
	else:
		c1 = c - p % c
	l = [a1, b1, c1]
	print(min(l))


from collections import deque as de
import math
from collections import Counter as cnt
from functools import reduce
from typing import MutableMapping
from itertools import groupby as gb
from fractions import Fraction as fr

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

class My_stack:

	def __init__(self):
		self.data = []

	def my_push(self, x):
		return self.data.append(x)

	def my_pop(self):
		return self.data.pop()

	def my_peak(self):
		return self.data[-1]

	def my_contains(self, x):
		return self.data.count(x)

	def my_show_all(self):
		return self.data

	def isEmpty(self):
		return len(self.data) == 0
arrStack = My_stack()

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def get_prime_factors(number):
	prime_factors = []
	while number % 2 == 0:
		prime_factors.append(2)
		number = number / 2
	for i in range(3, int(math.sqrt(number)) + 1, 2):
		while number % i == 0:
			prime_factors.append(int(i))
			number = number / i
	if number > 2:
		prime_factors.append(int(number))
	return prime_factors

def get_frequency(list):
	dic = {}
	for ele in list:
		if ele in dic:
			dic[ele] += 1
		else:
			dic[ele] = 1
	return dic

def Log2(x):
	return math.log10(x) / math.log10(2)

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))

def ceildiv(x, y):
	return (x + y - 1) // y
t = int(input())
while t:
	t -= 1
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		ua = ceildiv(p, a)
		ub = ceildiv(p, b)
		uc = ceildiv(p, c)
		fora = ua * a - p
		forb = ub * b - p
		forc = uc * c - p
		print(min(fora, forb, forc))


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0:
		na = 0
	else:
		na = a - p % a
	if p % b == 0:
		nb = 0
	else:
		nb = b - p % b
	if p % c == 0:
		nc = 0
	else:
		nc = c - p % c
	m = min(na, nb)
	m = min(m, nc)
	print(m)


t = int(input())
for i in range(t):
	(r, a, b, c) = list(map(int, input().split()))
	x = a - r % a
	y = b - r % b
	z = c - r % c
	if r % a == 0 or r % b == 0 or r % c == 0:
		print(0)
	else:
		print(min(x, min(y, z)))


import sys
input = sys.stdin.readline
ceil = lambda x, y: (x + y - 1) // y
t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	print(min(ceil(p, a) * a - p, ceil(p, b) * b - p, ceil(p, c) * c - p))


t = int(input())

def f(p, a):
	rep = a - p % a
	if not p % a:
		return 0
	else:
		return rep
for case in range(t):
	(p, a, b, c) = map(int, input().split())
	print(min((f(p, x) for x in [a, b, c])))


from math import *
for y in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	print(min(-p % a, -p % b, -p % c))


import math

def threeSwimmers(p, a, b, c):
	n1 = math.ceil(p / a + 1)
	n2 = math.ceil(p / b + 1)
	n3 = math.ceil(p / c + 1)
	t1 = a * (n1 - 1) - p
	t2 = b * (n2 - 1) - p
	t3 = c * (n3 - 1) - p
	if t1 < 0:
		t1 = a * n1 - p
	if t2 < 0:
		t2 = b * n2 - p
	if t3 < 0:
		t3 = c * n3 - p
	return min(t1, t2, t3)
for i in range(int(input())):
	(p, a, b, c) = list(map(int, input().split()))
	print(threeSwimmers(p, a, b, c))


def solution():
	(p, a, b, c) = map(int, input().split())
	if p % a == 0:
		m1 = 0
	else:
		m1 = a - p % a
	if p % b == 0:
		m2 = 0
	else:
		m2 = b - p % b
	if p % c == 0:
		m3 = 0
	else:
		m3 = c - p % c
	print(min(m1, m2, m3))
for _ in range(int(input())):
	solution()


def threeSwimmers(p, z):
	z1 = p % z
	if z1 != 0:
		return abs(z - z1)
	return z1
for i in range(int(input())):
	num = list(map(int, input().strip().split()))
	p = num[0]
	a = num[1]
	b = num[2]
	c = num[3]
	print(min(threeSwimmers(p, a), threeSwimmers(p, b), threeSwimmers(p, c)))


import sys
inputlines = sys.stdin.readlines()
number_of_testcases = int(inputlines[0])
for i in range(number_of_testcases):
	(p, a, b, c) = list(map(int, inputlines[i + 1].split()))
	rem_for_a = p % a
	if rem_for_a == 0:
		min_time_for_a = 0
	else:
		min_time_for_a = a - rem_for_a
	rem_for_b = p % b
	if rem_for_b == 0:
		min_time_for_b = 0
	else:
		min_time_for_b = b - rem_for_b
	rem_for_c = p % c
	if rem_for_c == 0:
		min_time_for_c = 0
	else:
		min_time_for_c = c - rem_for_c
	print(min(min_time_for_a, min_time_for_b, min_time_for_c))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	ans = None
	x = p // a
	y = a - p + x * a
	if x * a == p:
		ans = 0
	else:
		ans = y
	x = p // b
	y = b - p + x * b
	if x * b == p:
		ans = 0
	elif y < ans:
		ans = y
	x = p // c
	y = c - p + x * c
	if x * c == p:
		ans = 0
	elif y < ans:
		ans = y
	print(ans)


from typing import List

def closest(start: int, values: List[int]) -> int:
	timings = [(item - start % item) % item for item in values]
	return min(timings)

def read_input(filename: str=None) -> None:
	from sys import stdin
	stdin = open(filename) if filename else stdin
	cases = int(stdin.readline())
	while cases:
		cases = cases - 1
		(start, *values) = list(map(int, stdin.readline().split()))
		print(closest(start, values))

def main():
	read_input()
main()


def ceil(a, b):
	return (a + b - 1) // b
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	if a >= p:
		t1 = a
	else:
		t1 = ceil(p, a) * a
	if b >= p:
		t2 = b
	else:
		t2 = ceil(p, b) * b
	if c >= p:
		t3 = c
	else:
		t3 = ceil(p, c) * c
	print(min(min(t1 - p, t2 - p), t3 - p))


t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		a1 = a - p % a
		b1 = b - p % b
		c1 = c - p % c
		print(min(a1, b1, c1))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	t1 = 0
	t2 = 0
	t3 = 0
	if p == a:
		t1 = a
	elif p % a == 0:
		t1 = 0
	else:
		t1 = abs(a - p % a)
	if p == b:
		t2 = b
	elif p % b == 0:
		t2 = 0
	else:
		t2 = abs(b - p % b)
	if p == c:
		t3 == c
	elif p % c == 0:
		t3 = 0
	else:
		t3 = abs(c - p % c)
	print(min(t1, t2, t3))


t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	if p <= a and p <= b and (p <= c):
		x = min(a, b, c)
		print(x - p)
	else:
		x1 = p % a
		x2 = p % b
		x3 = p % c
		if x1 == 0 or x2 == 0 or x3 == 0:
			print(0)
		else:
			y1 = p // a + 1
			y2 = p // b + 1
			y3 = p // c + 1
			d1 = a * y1
			d2 = b * y2
			d3 = c * y3
			f1 = d1 - p
			f2 = d2 - p
			f3 = d3 - p
			print(min(f1, f2, f3))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	ans = 0
	if p in [a, b, c]:
		ans = 0
	elif p < min(a, b, c):
		ans = min(a, b, c) - p
	else:
		x = p // a
		x = x + 1 if x * a < p else x
		y = p // b
		y = y + 1 if y * b < p else y
		z = p // c
		z = z + 1 if z * c < p else z
		(x, y, z) = (x * a, y * b, z * c)
		ans = min(x - p, y - p, z - p)
	print(ans)


try:

	def func(n, a):
		if n % a == 0:
			return a * (n // a)
		return a * (n // a + 1)
	t = int(input())
	while t != 0:
		(n, a, b, c) = map(int, input().split())
		if n > a:
			a = func(n, a)
		if n > b:
			b = func(n, b)
		if n > c:
			c = func(n, c)
		mini = a - n
		mini = min(mini, b - n)
		mini = min(mini, c - n)
		print(mini)
		t -= 1
except EOFError:
	print(' ')


t = int(input())

def timeleft(p, q):
	if p % q == 0:
		timeleft = 0
	else:
		timeleft = q * (p // q + 1) - p
	return timeleft
for i in range(t):
	(p, a, b, c) = input().split(' ')
	p = int(p)
	a = int(a)
	b = int(b)
	c = int(c)
	print(min(timeleft(p, a), timeleft(p, b), timeleft(p, c)))


t = int(input())
while t > 0:
	(p, a, b, c) = [int(x) for x in input().split()]
	l = [a, b, c]
	if p % a == 0 or p % b == 0 or p % c == 0:
		print('0')
	else:
		m = []
		for i in l:
			m.append((p // i + 1) * i - p)
		print(min(m))
	t -= 1


while True:
	try:
		t = eval(input())
		for i in range(t):
			(p, a, b, c) = map(int, input().split())
			ans1 = a - (p - 1) % a - 1
			ans2 = b - (p - 1) % b - 1
			ans3 = c - (p - 1) % c - 1
			print(min(ans1, ans2, ans3))
	except:
		break


def ceildiv(x, y):
	return (x + y - 1) // y
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	print(min(ceildiv(p, a) * a, ceildiv(p, b) * b, ceildiv(p, c) * c) - p)


t = int(input())

def solve():
	(p, a, b, c) = map(int, input().split())
	A = 0
	B = 0
	C = 0
	A = -(-p // a) * a
	B = -(-p // b) * b
	C = -(-p // c) * c
	print(min(A, B, C) - p)
	return 0
for _ in range(t):
	solve()


def printArray(arr):
	ret = ''
	for casi in arr:
		ret = ret + (str(casi) + ' ')
	print(ret)

def mySort(a):
	x = 0
	y = 0
	z = 0
	if a[0] > a[1]:
		k = a[0] % a[1]
		if k > 0:
			k = 1
			x = (int(a[0] / a[1]) + k) * a[1] - a[0]
			while x > a[1]:
				x = x - a[1]
			while x < 0:
				x = x + a[1]
		else:
			x = 0
	else:
		x = a[1] - a[0]
	if a[0] > a[2]:
		k = a[0] % a[2]
		if k > 0:
			k = 1
			y = (int(a[0] / a[2]) + k) * a[2] - a[0]
			while y > a[2]:
				y = y - a[2]
			while y < 0:
				y = y + a[2]
		else:
			y = 0
	else:
		y = a[2] - a[0]
	if a[0] > a[3]:
		k = a[0] % a[3]
		if k > 0:
			k = 1
			z = (int(a[0] / a[3]) + k) * a[3] - a[0]
			while z > a[3]:
				z = z - a[3]
			while z < 0:
				z = z + a[3]
		else:
			z = 0
	else:
		z = a[3] - a[0]
	return min(x, y, z)
cas = int(input())
inputB = []
inputS = []
for casi in range(cas):
	b = list(map(int, input().split()))
	inputB.append(b)
i = 0
for itemB in inputB:
	ret = mySort(itemB)
	print(ret)


T = int(input())
for _ in range(T):
	(p, a, b, c) = input().split(' ')
	p = int(p)
	a = int(a)
	b = int(b)
	c = int(c)
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		print(min(a - p % a, b - p % b, c - p % c))


t = int(input())

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1
while t:
	t = t - 1
	(p, a, b, c) = map(int, input().split())
	a = ceil(p, a) * a - p
	b = ceil(p, b) * b - p
	c = ceil(p, c) * c - p
	print(min(a, b, c))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	minn = a - p % a if p % a > 0 else 0
	minn = (b - p % b if p % b > 0 else 0) if minn > (b - p % b if p % b > 0 else 0) else minn
	minn = (c - p % c if p % c > 0 else 0) if minn > (c - p % c if p % c > 0 else 0) else minn
	print(minn)


import math

def func(a, b):
	return (a // b + 1) * b
t = int(input())
x = 0
for x in range(t):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or 0 == p % c:
		print(0)
	else:
		a1 = min((p // a + 1) * a, (p // b + 1) * b, (p // c + 1) * c)
		sum = a1 - p
		print(sum)


for _ in range(int(input())):
	(p, a, b, c) = list(map(int, input().split()))
	x = p // a
	y = p // b
	z = p // c
	if p % a != 0:
		x += 1
	if p % b != 0:
		y += 1
	if p % c != 0:
		z += 1
	ans = min(abs(p - x * a), abs(p - y * b), abs(p - z * c))
	print(ans)


t = int(input())
while t:
	t -= 1
	(p, *a) = map(int, input().split())
	m = min([(i - p) % i for i in a])
	print(m)


def ceildiv(a, b):
	return -(-a // b)
K = 3
T = int(input())
for _ in range(T):
	(p, *a) = map(int, input().split())
	m = 9e+99
	for n in a:
		m = min(m, n * ceildiv(p, n) - p)
	print(m)


def swimmers(p, a, b, c):
	pa = (p + a - 1) // a * a
	pb = (p + b - 1) // b * b
	pc = (p + c - 1) // c * c
	return min([pa, pb, pc])
t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	print(swimmers(p, a, b, c) - p)


def check(x, p):
	y = p % x
	if y == 0:
		return 0
	return x - y
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	time = [check(a, p), check(b, p), check(c, p)]
	print(min(time))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	if p % a and p % b and p % c:
		print(min(a - p % a, b - p % b, c - p % c))
	else:
		print(0)


def next(p, a):
	d = p // a
	if d * a == p:
		return p
	return (d + 1) * a
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	(a1, a2, a3) = (next(p, a), next(p, b), next(p, c))
	print(min(a1 - p, a2 - p, a3 - p))


import sys
input = sys.stdin.readline

def get_ans(p, n):
	if p % n == 0:
		return 0
	return n - p % n

def main():
	for _ in range(int(input())):
		(p, a, b, c) = map(int, input().split())
		print(min([get_ans(p, n) for n in (a, b, c)]))
main()


T = int(input())
for i in range(T):
	(p, a, b, c) = [int(s) for s in input().split()]
	pa = p % a
	pb = p % b
	pc = p % c
	if pa == 0 or pb == 0 or pc == 0:
		print(0)
	else:
		print(min(a - pa, b - pb, c - pc))


c = 0
t = int(input())
for c in range(t):
	(p, a, b, c) = (int(x) for x in input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		print(min(a * (p // a + 1), b * (p // b + 1), c * (p // c + 1)) - p)


import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')

def st():
	return list(sys.stdin.readline().strip())

def li():
	return list(map(int, sys.stdin.readline().split()))

def mp():
	return map(int, sys.stdin.readline().split())

def inp():
	return int(sys.stdin.readline())

def pr(n):
	return sys.stdout.write(str(n) + '\n')

def prl(n):
	return sys.stdout.write(str(n) + ' ')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	l = li()
	ans = INF
	for i in range(1, 4):
		ans = min(ans, (l[i] + l[0] - 1) // l[i] * l[i] - l[0])
	pr(ans)
for _ in range(inp()):
	solve()


t = int(input())
minimal = []
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		minimal.append(0)
	else:
		min_a = (p // a + 1) * a - p
		min_b = (p // b + 1) * b - p
		min_c = (p // c + 1) * c - p
		minimal.append(min(min_a, min_b, min_c))
for i in minimal:
	print(i)


import sys

def read_ints():
	return [int(i) for i in sys.stdin.readline().strip().split()]

def read_int():
	return int(sys.stdin.readline().strip())
t = read_int()
for i in range(t):
	(p, a, b, c) = read_ints()
	a_min = p // a * a
	if a_min < p:
		a_min += a
	a_min -= p
	b_min = p // b * b
	if b_min < p:
		b_min += b
	b_min -= p
	c_min = p // c * c
	if c_min < p:
		c_min += c
	c_min -= p
	print(min(a_min, b_min, c_min))


import bisect
import math
import itertools
import sys
alpha = 'abcdefghijklmnopqrstuvwxyz'

def solve(p, a, b, c):
	return min([(a + p - 1) // a * a - p, (b + p - 1) // b * b - p, (c + p - 1) // c * c - p])
	pass
t = int(input())
ans = []
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	ans.append(solve(p, a, b, c))
for test in ans:
	print(test)


import sys
n = int(input())
for _ in range(n):
	l = list(map(int, sys.stdin.readline().strip().split()))
	a = min((l[1] - l[0] % l[1]) % l[1], (l[2] - l[0] % l[2]) % l[2])
	a = min(a, (l[3] - l[0] % l[3]) % l[3])
	print(a)


t = int(input())
for t in range(t):
	(p, a, b, c) = map(int, input().split())
	x = -(-p // a) * a - p
	y = -(-p // b) * b - p
	z = -(-p // c) * c - p
	print(min(x, y, z))


t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	print(min([((p - 1) // x + 1) * x - p for x in [a, b, c]]))


t = int(input())
for _ in range(t):
	(p, *arr) = map(int, input().split())
	bestTime = max(arr)
	points = 0
	for c in arr:
		if p % c == 0:
			bestTime = 0
		else:
			points = (p + c - 1) // c * c - p
			if points < bestTime:
				bestTime = points
	print(bestTime)


import math
from decimal import Decimal
for _ in range(int(input())):
	(values, a, b, c) = map(Decimal, input().split())
	d = math.ceil(values / a) * a - values
	e = math.ceil(values / b) * b - values
	f = math.ceil(values / c) * c - values
	print(min(d, e, f))


def answer(p, a, b, c):
	pma = p % a
	pmb = p % b
	pmc = p % c
	if pma == 0 or pmb == 0 or pmc == 0:
		return 0
	else:
		return min(a - pma, b - pmb, c - pmc)

def main():
	t = int(input())
	p = [0] * t
	a = [0] * t
	b = [0] * t
	c = [0] * t
	for j in range(t):
		(p[j], a[j], b[j], c[j]) = [int(i) for i in input().split()]
	for j in range(t):
		print(answer(p[j], a[j], b[j], c[j]))
main()


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
		continue
	a = a - p % a
	b = b - p % b
	c = c - p % c
	print(min(a, b, c))


import math
t1 = int(input())
for _ in range(t1):
	(p, a, b, c) = map(int, input().split())
	a1 = p // a
	if p % a != 0:
		a1 += 1
	b1 = p // b
	if p % b != 0:
		b1 += 1
	c1 = p // c
	if p % c != 0:
		c1 += 1
	print(min(a1 * a, b1 * b, c1 * c) - p)


t = int(input())
for i in range(t):
	r = list(map(int, input().split()))
	p = r[0]
	a = r[1]
	b = r[2]
	c = r[3]
	aa = p % a
	bb = p % b
	cc = p % c
	if aa == 0 or bb == 0 or cc == 0:
		print(0)
	else:
		print(min(a - aa, b - bb, c - cc))


import sys
import math
from itertools import permutations
from bisect import bisect_left
import heapq

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()

def FACT(n, mod):
	s = 1
	facts = [1]
	for i in range(1, n + 1):
		s *= i
		s %= mod
		facts.append(s)
	return facts[n]

def C(n, k, mod):
	return FACT(n, mod) * pow(FACT(k, mod) * FACT(n - k, mod) % mod, mod - 2, mod) % mod
for _ in range(II()):
	(p, a, b, c) = MI()
	x = (a - p % a) % a
	y = (b - p % b) % b
	z = (c - p % c) % c
	print(min(x, y, z))


num = int(input())
nlist = []
mlist = []
t = True
for i in range(num):
	list1 = list(map(int, input().split()))
	k = list1[0]
	n = 0
	while t == True:
		n = n + 1
		if k % list1[n] == 0:
			o = 0
			mlist.append(o)
		elif k - list1[n] > 0:
			i = 1
			r = k // list1[n]
			i = list1[n] * (r + 1)
			h = k - i
			o = h
			o = int(o)
			o = abs(o)
			mlist.append(o)
		if k - list1[n] < 0:
			o = k - list1[n]
			o = abs(o)
			mlist.append(o)
		if n == 3:
			break
	m = min(mlist)
	nlist.append(m)
	mlist = []
print(*nlist, sep='\n')


t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	t1 = (p // a + 1) * a - p
	if p % a == 0:
		t1 = 0
	t2 = (p // b + 1) * b - p
	if p % b == 0:
		t2 = 0
	t3 = (p // c + 1) * c - p
	if p % c == 0:
		t3 = 0
	print(min(t1, t2, t3))


T = int(input())
for t in range(T):
	(p, a, b, c) = [int(k) for k in input().split()]
	m1 = p % a
	m2 = p % b
	m3 = p % c
	ans = 10 ** 19
	ans = min(ans, (a - m1) % a)
	ans = min(ans, (b - m2) % b)
	ans = min(ans, (c - m3) % c)
	print(ans)


for j in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	x = p // a
	y = p // b
	z = p // c
	if p % a == 0 or p % b == 0 or p % c == 0:
		print('0')
	else:
		print(min((x + 1) * a - p, (y + 1) * b - p, (z + 1) * c - p))


R = lambda : map(int, input().split())
for _ in range(int(input())):
	(p, a, b, c) = R()
	l = [a, b, c]
	l2 = []
	for i in l:
		if p % i == 0:
			l2.append(0)
			break
		else:
			l2.append(i - p % i)
	print(min(l2))


for _ in range(int(input())):
	(p, a, b, c) = list(map(int, input().split()))
	if p % a:
		h = a - p % a
	else:
		h = 0
	if p % b:
		i = b - p % b
	else:
		i = 0
	if p % c:
		j = c - p % c
	else:
		j = 0
	print(min(h, i, j))


for i in range(int(input())):
	arr = [int(_) for _ in input().split()]
	f = []
	if arr[0] in arr[1:]:
		print(0)
	else:
		for char in arr[1:]:
			if arr[0] % char == 0:
				char = char * (arr[0] // char)
			else:
				char = char * (arr[0] // char + 1)
			f.append(char)
		print(min(f) - arr[0])


import math

def getReminder(p, x):
	return (x - p % x) % x

def solution(p, a, b, c):
	r_A = getReminder(p, a)
	r_B = getReminder(p, b)
	r_C = getReminder(p, c)
	return min(r_A, r_B, r_C)

def main():
	test_cnt = int(input())
	for i in range(test_cnt):
		_testcase = [int(i) for i in input().split(' ')]
		[p, a, b, c] = _testcase
		result = solution(p, a, b, c)
		print(result)
main()


t = int(input())
answer = []
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0:
		a1 = 0
	else:
		a1 = (p // a + 1) * a - p
	if p % b == 0:
		b1 = 0
	else:
		b1 = (p // b + 1) * b - p
	if p % c == 0:
		c1 = 0
	else:
		c1 = (p // c + 1) * c - p
	answer.append(min(a1, b1, c1))
for ans in answer:
	print(ans)


for t in range(int(input())):
	(k, a, b, c) = [int(k) for k in input().split()]
	ka = 0 if k % a == 0 else a - k % a
	kb = 0 if k % b == 0 else b - k % b
	kc = 0 if k % c == 0 else c - k % c
	print(min(ka, kb, kc))


n = int(input())
for i in range(0, n):
	(p, a, b, c) = map(int, input().split())
	minm = min(a, b, c)
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	elif minm > p:
		print(minm - p)
	else:
		print(min(a - p % a, b - p % b, c - p % c))


for _ in range(int(input())):
	(p, a, b, c) = list(map(int, input().split(' ')))
	s = 1e+19
	for i in (a, b, c):
		d = p % i
		if not d:
			s = 0
			break
		else:
			s = min(s, i - d)
	print(s)


def wait():
	(p, a, b, c) = input().split()
	p = int(p)
	a = int(a)
	b = int(b)
	c = int(c)
	if p % a == 0 or p % b == 0 or p % c == 0:
		a1 = 0
		b1 = 0
		c1 = 0
	else:
		a1 = (p // a + 1) * a - p
		b1 = (p // b + 1) * b - p
		c1 = (p // c + 1) * c - p
	s = min(a1, b1, c1)
	print(s)
t = int(input())
for _ in range(t):
	wait()


t = int(input())
for i in range(t):
	sw = list(map(int, input().split()))
	p = sw[0]
	sw = sw[1:]
	for i in range(3):
		if p % sw[i] == 0:
			sw[i] = 0
		else:
			sw[i] = sw[i] - p % sw[i]
	print(min(sw))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	a1 = a - p % a if p % a != 0 else 0
	b1 = b - p % b if p % b != 0 else 0
	c1 = c - p % c if p % c != 0 else 0
	print(min(a1, b1, c1))


t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	print(min(-p % a, -p % b, -p % c))


import sys
input = sys.stdin.readline
from sys import stdin, stdout
from collections import defaultdict, Counter
M = 10 ** 9 + 7

def main():
	for _ in range(int(input())):
		(p, a, b, c) = [int(s) for s in input().split()]
		w = min([(p // a + 1) * a - p, (p // b + 1) * b - p, (p // c + 1) * c - p])
		if 0 in [p % c, p % a, p % b]:
			print(0)
		else:
			print(w)
main()


t = int(input())
for i in range(t):
	x = list(map(int, input().split()))
	p = x[0]
	a = x[1]
	b = x[2]
	c = x[3]
	if p % a != 0 and p % b != 0 and (p % c != 0):
		print(min((p // a + 1) * a - p, (p // b + 1) * b - p, (p // c + 1) * c - p))
	else:
		print(0)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(N, A, B, C) = map(int, input().split())
	if N == 0:
		print(0)
	else:
		(a, b, c) = (N % A, N % B, N % C)
		if a == 0 or b == 0 or c == 0:
			print(0)
		else:
			print(min([A - a, B - b, C - c]))


import sys
input = sys.stdin.readline
from math import gcd
for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	(ra, rb, rc) = (p % a, p % b, p % c)
	(aa, bb, cc) = ((a - ra) % a, (b - rb) % b, (c - rc) % c)
	print(min(aa, bb, cc))


import math
from collections import defaultdict
from sys import stdin
input = stdin.readline
T = int(input())
for _ in range(T):
	(p, a, b, c) = map(int, input().split())
	x = (p + a - 1) // a
	y = (p + b - 1) // b
	z = (p + c - 1) // c
	print(min(x * a, y * b, z * c) - p)


from sys import stdin, stdout
t = int(input())
out = []
for i in range(t):
	(p, a, b, c) = [int(x) for x in input().split()]
	moda = a - p % a if p % a > 0 else 0
	modb = b - p % b if p % b > 0 else 0
	modc = c - p % c if p % c > 0 else 0
	out.append(str(min((moda, modb, modc))))
stdout.write('\n'.join(out))


t = int(input())
for _ in range(t):
	(p, a, b, c) = map(int, input().split())
	w1 = (p + a - 1) // a * a - p
	w2 = (p + b - 1) // b * b - p
	w3 = (p + c - 1) // c * c - p
	print(min(w1, w2, w3))


for i in range(0, int(input())):
	(p, a, b, c) = map(int, input().split(' '))
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		set_1 = set()
		set_1.update([a - p % a, b - p % b, c - p % c])
		print(min(set_1))


def calc(p, gap):
	ans = gap - p % gap
	if ans == gap:
		return 0
	return ans

def main():
	cases = int(input())
	ans_list = []
	for _ in range(cases):
		(p, a, b, c) = map(int, input().split(' '))
		ans_list.append(str(min(calc(p, a), calc(p, b), calc(p, c))))
	print('\n'.join(ans_list))
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	(p, a, b, c) = (int(x) for x in input().split())
	mods = [p % x for x in (a, b, c)]
	if 0 in mods:
		print(0, flush=True)
	else:
		print(min((x - p % x for x in (a, b, c))), flush=True)


answers = []

def solve(p, a, b, c):
	if p % a == 0:
		answers.append(0)
		return
	if p % b == 0:
		answers.append(0)
		return
	if p % c == 0:
		answers.append(0)
		return
	req = p // a + 1
	ans1 = a * req - p
	req = p // b + 1
	ans2 = b * req - p
	req = p // c + 1
	ans3 = c * req - p
	answers.append(min(ans1, ans2, ans3))
T = int(input())
while T:
	(p, a, b, c) = [int(x) for x in input().split()]
	solve(p, a, b, c)
	T -= 1
for ans in answers:
	print(ans)


from functools import reduce
import os
import sys
from collections import *
from decimal import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
albhabet = 'abcdefghijklmnopqrstuvwxyz'
mo = 1000000007
inf = 1e+18
div = 998244353
for _ in range(inn()):
	(p, a, b, c) = value()
	r1 = ceil(p / a)
	r2 = ceil(p / b)
	r3 = ceil(p / c)
	print(min((r1 * a - p + a) % a, (r2 * b - p + b) % b, (r3 * c - p + c) % c))


for _ in range(int(input())):
	(p, a, b, c) = map(int, input().split())
	if p % a == 0:
		ma = p // a
	else:
		ma = p // a + 1
	if p % b == 0:
		mb = p // b
	else:
		mb = p // b + 1
	if p % c == 0:
		mc = p // c
	else:
		mc = p // c + 1
	print(min(ma * a, mb * b, mc * c) - p)


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from collections import *
from math import *
R = range
for _ in R(ii1()):
	(p, a, b, c) = iia()
	if p % a == 0 or p % b == 0 or p % c == 0:
		print(0)
	else:
		print(min(a - p % a, b - p % b, c - p % c))


def min_moment(a, p):
	if p % a != 0:
		rez1 = a * (p // a + 1)
	else:
		rez1 = p
	return rez1
t = int(input())
for i in range(t):
	(p, a, b, c) = map(int, input().split())
	rez1 = min_moment(a, p)
	rez2 = min_moment(b, p)
	rez3 = min_moment(c, p)
	if rez1 <= rez2 and rez1 <= rez3:
		print(rez1 - p)
	elif rez2 <= rez1 and rez2 <= rez3:
		print(rez2 - p)
	elif rez3 <= rez1 and rez3 <= rez1:
		print(rez3 - p)


set = int(input())
for x in range(set):
	lis = list(map(int, input().split()))
	p = lis.pop(0)
	for i in range(len(lis)):
		if p % lis[i] == 0:
			lis[i] = 0
		else:
			lis[i] = lis[i] - p % lis[i]
	lis.sort()
	print(lis.pop(0))


import math
n = int(input())
for i in range(n):
	(p, a, b, c) = list(map(int, input().split()))
	x = min((a - p % a) % a, (b - p % b) % b, (c - p % c) % c)
	print(x)


import math
T = int(input())

def solve():
	(p, a, b, c) = map(int, input().split())
	a *= p // a + (0 if p % a == 0 else 1)
	b *= p // b + (0 if p % b == 0 else 1)
	c *= p // c + (0 if p % c == 0 else 1)
	print(min(a - p, b - p, c - p))
for i in range(T):
	solve()