AtCoder Beginner Contest 119 - Lazy Faith

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.
Answer the following Q queries:
 - Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

-----Constraints-----
 - 1 \leq A, B \leq 10^5
 - 1 \leq Q \leq 10^5
 - 1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}
 - 1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}
 - 1 \leq x_i \leq 10^{10}
 - s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

-----Output-----
Print Q lines. The i-th line should contain the answer to the i-th query.

-----Sample Input-----
2 3 4
100
600
400
900
1000
150
2000
899
799

-----Sample Output-----
350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.
 - Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.
 - Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.
 - Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.
 - Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Difficulty: MEDIUM

Starter code:

Solutions:
from bisect import bisect, bisect_left
(A, B, Q) = map(int, input().split())
(S, T, X) = ([0] * (A + 2), [0] * (B + 2), [0] * Q)
INF = 10 ** 10
(S[0], T[0]) = (-INF, -INF)
(S[-1], T[-1]) = (2 * INF, 2 * INF)
for i in range(A):
	S[i + 1] = int(input())
for i in range(B):
	T[i + 1] = int(input())
for i in range(Q):
	X[i] = int(input())

def dis(x):
	RS = bisect_left(S, x)
	LS = RS - 1
	RT = bisect_left(T, x)
	LT = RT - 1
	ans = [0] * 4
	ans[0] = max(S[RS], T[RT]) - x
	ans[1] = x - min(S[LS], T[LT])
	ans[2] = S[RS] - T[LT] + min(S[RS] - x, x - T[LT])
	ans[3] = T[RT] - S[LS] + min(T[RT] - x, x - S[LS])
	return min(ans)
for x in X:
	print(dis(x))


import bisect
(a, b, q) = list(map(int, input().split()))
s = [-10 ** 19]
for i in range(a):
	s.append(int(input()))
s.append(10 ** 19)
t = [-10 ** 19]
for i in range(b):
	t.append(int(input()))
t.append(10 ** 19)
for i in range(q):
	x = int(input())
	ps = bisect.bisect_right(s, x)
	pt = bisect.bisect_right(t, x)
	ret = 10 ** 19
	for loc1 in (s[ps], s[ps - 1]):
		for loc2 in (t[pt], t[pt - 1]):
			val = min(abs(x - loc1) + abs(loc1 - loc2), abs(x - loc2) + abs(loc1 - loc2))
			ret = min(ret, val)
	print(ret)


import bisect
(A, B, Q) = map(int, input().split())
S = [-float('inf')] + list((int(input()) for i in range(A))) + [float('inf')]
T = [-float('inf')] + list((int(input()) for i in range(B))) + [float('inf')]
x = list((int(input()) for i in range(Q)))
for i in x:
	si = bisect.bisect_left(S, i)
	ti = bisect.bisect_left(T, i)
	ans = min(max(S[si], T[ti]) - i, i - min(S[si - 1], T[ti - 1]), 2 * S[si] - i - T[ti - 1], 2 * T[ti] - i - S[si - 1], T[ti] + i - 2 * S[si - 1], S[si] + i - 2 * T[ti - 1])
	print(ans)


from bisect import bisect_left
(a, b, q) = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()
s = [float('INF') * -1] + s + [float('INF')]
t = [float('INF') * -1] + t + [float('INF')]
for i in range(q):
	x = int(input())
	sp = bisect_left(s, x) - 1
	tp = bisect_left(t, x) - 1
	s1 = s[sp]
	t1 = t[tp]
	s2 = s[sp + 1]
	t2 = t[tp + 1]
	print(min(max(x - s1, x - t1), max(s2 - x, t2 - x), x - s1 + t2 - s1, x - t1 + s2 - t1, s2 - x + s2 - t1, t2 - x + t2 - s1))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
ss = [-INF] + [int(input()) for _ in range(A)] + [INF]
tt = [-INF] + [int(input()) for _ in range(B)] + [INF]
xx = [int(input()) for _ in range(Q)]
for x in xx:
	b = bisect.bisect_right(ss, x)
	d = bisect.bisect_right(tt, x)
	ans = INF
	for s in [ss[b - 1], ss[b]]:
		for t in [tt[d - 1], tt[d]]:
			d1 = abs(s - x) + abs(t - s)
			d2 = abs(t - x) + abs(s - t)
			ans = min(ans, d1, d2)
	print(ans)


from bisect import *
(a, b, q) = map(int, input().split())
a1 = [int(input()) for i in range(a)]
b1 = [int(input()) for i in range(b)]
q = [int(input()) for i in range(q)]
a1 = [-20 ** 10] + a1 + [20 ** 10]
b1 = [-20 ** 10] + b1 + [20 ** 10]
for i in q:
	x = bisect_left(a1, i)
	y = bisect_left(b1, i)
	low = i - min(a1[x - 1], b1[y - 1])
	high = max(a1[x], b1[y]) - i
	turn1 = min(a1[x] - i, i - b1[y - 1]) + abs(a1[x] - b1[y - 1])
	turn2 = min(b1[y] - i, i - a1[x - 1]) + abs(b1[y] - a1[x - 1])
	print(min(low, high, turn1, turn2))


import bisect
(a, b, q) = map(int, input().split())
s = []
t = []
for i in range(a):
	s.append(int(input()))
for i in range(b):
	t.append(int(input()))
s.append(10 ** 11)
t.append(10 ** 11)
for i in range(q):
	x = int(input())
	ss = bisect.bisect(s, x)
	tt = bisect.bisect(t, x)
	ans = []
	for i in [s[ss - 1], s[ss]]:
		for j in [t[tt - 1], t[tt]]:
			ans.append(abs(x - i) + abs(i - j))
			ans.append(abs(x - j) + abs(i - j))
	print(min(ans))


import bisect
(A, B, Q) = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for i in range(Q):
	x = int(input())
	b = bisect.bisect_right(s, x)
	d = bisect.bisect_right(t, x)
	ans = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			ans1 = abs(S - x) + abs(S - T)
			ans2 = abs(T - x) + abs(T - S)
			ans = min(ans, ans1, ans2)
	print(ans)


(a, b, q) = map(int, input().split())
import bisect
inf = 10 ** 18
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
x = [int(input()) for _ in range(q)]
s = [-inf] + s + [inf]
t = [-inf] + t + [inf]
for q in x:
	(c, d) = (bisect.bisect_left(s, q), bisect.bisect_left(t, q))
	ans = float('inf')
	for i in [c, c - 1]:
		for j in [d, d - 1]:
			ans = min(ans, abs(s[i] - t[j]) + abs(q - s[i]), abs(s[i] - t[j]) + abs(q - t[j]))
	print(ans)


from bisect import bisect_left
(A, B, Q) = map(int, input().split())
inf = float('inf')
S = [-inf] + [int(input()) for i in range(A)] + [inf]
T = [-inf] + [int(input()) for i in range(B)] + [inf]
X = [int(input()) for i in range(Q)]
S.sort()
T.sort()
for x in X:
	s_index = bisect_left(S, x)
	t_index = bisect_left(T, x)
	s_l = abs(S[s_index - 1] - x)
	s_r = abs(S[s_index] - x)
	t_l = abs(T[t_index - 1] - x)
	t_r = abs(T[t_index] - x)
	print(min(max(s_l, t_l), max(s_r, t_r), 2 * s_l + t_r, 2 * s_r + t_l, 2 * t_l + s_r, 2 * t_r + s_l))


import bisect
(A, B, Q) = map(int, input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
x = [int(input()) for i in range(Q)]
for i in x:
	idx_i = bisect.bisect(s, i)
	z = float('inf')
	for j in s[max(0, idx_i - 1):idx_i + 1]:
		idx_j = bisect.bisect(t, j)
		for k in t[max(0, idx_j - 1):idx_j + 1]:
			_z = min(abs(i - j) + abs(j - k), abs(i - k) + abs(k - j))
			z = min(z, _z)
	print(z)


import sys
from bisect import bisect_left
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	(a, b, q) = list(map(int, input().split()))
	S = [-f_inf] + list((int(input()) for _ in range(a))) + [f_inf]
	T = [-f_inf] + list((int(input()) for _ in range(b))) + [f_inf]
	for _ in range(q):
		res = f_inf
		x = int(input())
		idx_s = bisect_left(S, x)
		idx_t = bisect_left(T, x)
		for s in [S[idx_s - 1], S[idx_s]]:
			for t in [T[idx_t - 1], T[idx_t]]:
				d1 = abs(s - x) + abs(s - t)
				d2 = abs(t - x) + abs(t - s)
				res = min(res, d1, d2)
		print(res)

def __starting_point():
	resolve()
__starting_point()


from bisect import bisect
(A, B, Q) = map(int, input().split())
s = [int(input()) for _ in range(A)] + [10 ** 11]
t = [int(input()) for _ in range(B)] + [10 ** 11]
for _ in range(Q):
	x = int(input())
	ans = []
	ps = bisect(s, x)
	pt = bisect(t, x)
	for vs in (s[ps], s[ps - 1]):
		for vt in (t[pt], t[pt - 1]):
			ans.append(abs(x - vs) + abs(vs - vt))
			ans.append(abs(x - vt) + abs(vt - vs))
	print(min(ans))


import bisect
(A, B, Q) = map(int, input().split())
inf = 10 ** 18
s = [-inf]
t = [-inf]
for i in range(A):
	s.append(int(input()))
for i in range(B):
	t.append(int(input()))
s.append(inf)
t.append(inf)
for i in range(Q):
	x = int(input())
	(a, b) = (bisect.bisect(s, x), bisect.bisect(t, x))
	res = inf
	for j in [s[a - 1], s[a]]:
		for k in [t[b - 1], t[b]]:
			res = min(res, abs(j - x) + abs(k - j), abs(k - x) + abs(k - j))
	print(res)


import sys
from bisect import bisect

def read():
	return sys.stdin.readline().rstrip()

def main():
	(a, b, q) = list(map(int, read().split()))
	s = [-10 ** 12] + [int(read()) for _ in range(a)] + [10 ** 12]
	t = [-10 ** 12] + [int(read()) for _ in range(b)] + [10 ** 12]
	for _ in range(q):
		x = int(read())
		si = bisect(s, x)
		ti = bisect(t, x)
		(sa, sb) = (s[si - 1], s[si])
		(ta, tb) = (t[ti - 1], t[ti])
		d = 10 ** 12
		for sx in (sa, sb):
			for tx in (ta, tb):
				d = min(abs(x - sx) + abs(sx - tx), abs(x - tx) + abs(tx - sx), d)
		print(d)

def __starting_point():
	main()
__starting_point()


from bisect import *
(a, b, q) = map(int, input().split())
s = [-10 ** 18] + [int(input()) for _ in range(a)] + [10 ** 18]
t = [-10 ** 18] + [int(input()) for _ in range(b)] + [10 ** 18]
for _ in range(q):
	i = int(input())
	si = bisect_left(s, i)
	ti = bisect_left(t, i)
	print(min(max(s[si], t[ti]) - i, i - min(t[ti - 1], s[si - 1]), 2 * t[ti] - s[si - 1] - i, 2 * s[si] - t[ti - 1] - i, i + t[ti] - 2 * s[si - 1], i + s[si] - 2 * t[ti - 1]))


(a, b, q) = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
import bisect
for i in range(q):
	s2 = 10 ** 12
	t2 = 10 ** 12
	qq = int(input())
	sp = bisect.bisect(s, qq)
	tp = bisect.bisect(t, qq)
	s1 = s[sp - 1]
	if sp < a:
		s2 = s[sp]
	t1 = t[tp - 1]
	if tp < b:
		t2 = t[tp]
	print(min(abs(qq - s1) + abs(s1 - t1), abs(qq - s1) + abs(s1 - t2), abs(qq - s2) + abs(s2 - t1), abs(qq - s2) + abs(s2 - t2), abs(qq - t1) + abs(t1 - s1), abs(qq - t1) + abs(t1 - s2), abs(qq - t2) + abs(t2 - s1), abs(qq - t2) + abs(t2 - s2)))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
x = [int(input()) for i in range(Q)]
for xi in x:
	(b, d) = (bisect.bisect_right(s, xi), bisect.bisect_right(t, xi))
	min_d = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			d1 = abs(S - xi) + abs(T - S)
			d2 = abs(T - xi) + abs(S - T)
			min_d = min(min_d, d1, d2)
	print(min_d)


from bisect import bisect_left
(A, B, Q) = list(map(int, input().split()))
s = [-10 ** 11] + [int(input()) for _ in range(A)] + [10 ** 11]
t = [-10 ** 11] + [int(input()) for _ in range(B)] + [10 ** 11]
x = [int(input()) for _ in range(Q)]
for xi in x:
	p1 = bisect_left(s, xi)
	p2 = bisect_left(t, xi)
	l = 10 ** 11
	for ss in s[p1 - 1:p1 + 1]:
		for tt in t[p2 - 1:p2 + 1]:
			l = min(l, min(abs(ss - xi), abs(tt - xi)) + abs(ss - tt))
	print(l)


(jin, ter, q) = map(int, input().split())
INF = 10 ** 19
(jil, tel) = ([-INF] + [int(input()) for i in range(jin)] + [INF], [-INF] + [int(input()) for i in range(ter)] + [INF])
from bisect import bisect_right as bl
for _ in range(q):
	x = int(input())
	ans = INF
	(b, d) = (bl(jil, x), bl(tel, x))
	for s in jil[b - 1:b + 1]:
		for t in tel[d - 1:d + 1]:
			ans = min(min(abs(x - s), abs(t - x)) + abs(t - s), ans)
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 11
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	(b, d) = (bisect.bisect_right(s, x), bisect.bisect_right(t, x))
	res = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(A, B) = (abs(S - x) + abs(T - S), abs(T - x) + abs(T - S))
			res = min(res, A, B)
	print(res)


import bisect
(A, B, Q) = list(map(int, input().split()))
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	(b, d) = (bisect.bisect_left(s, x), bisect.bisect_left(t, x))
	res = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


from bisect import bisect
(a, b, q) = map(int, input().split())
s = [-float('inf')] + [int(input()) for i in range(a)] + [float('inf')]
t = [-float('inf')] + [int(input()) for i in range(b)] + [float('inf')]
for i in range(q):
	x = int(input())
	l = bisect(s, x)
	r = bisect(t, x)
	ans = float('inf')
	for L in [s[l], s[l - 1]]:
		for R in [t[r], t[r - 1]]:
			ans = min(ans, abs(x - L) + abs(L - R), abs(x - R) + abs(R - L))
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
s = [-10 ** 10 - 1, -10 ** 10] + [int(input()) for _ in range(A)] + [2 * 10 ** 10]
t = [-10 ** 10 - 1, -10 ** 10] + [int(input()) for _ in range(B)] + [2 * 10 ** 10]
x = [int(input()) for _ in range(Q)]

def func(c, d, z):
	i = bisect.bisect_left(c, z)
	j1 = bisect.bisect_left(d, c[i - 1])
	j2 = bisect.bisect_left(d, c[i])
	return min(z - d[j1 - 1], z - c[i - 1] * 2 + d[j1], d[j2] - z, c[i] * 2 - z - d[j2 - 1])
for z in x:
	print(min(func(s, t, z), func(t, s, z)))


import bisect
(A, B, Q) = list(map(int, input().split()))
s = []
t = []
for i in range(A):
	s.append(int(input()))
for i in range(B):
	t.append(int(input()))
for i in range(Q):
	x = int(input())
	s_index_af = bisect.bisect(s, x)
	if s_index_af == A:
		s_index_af -= 1
		s_index_bf = s_index_af
	else:
		s_index_bf = s_index_af - 1
	t_index_af = bisect.bisect(t, x)
	if t_index_af == B:
		t_index_af -= 1
		t_index_bf = t_index_af
	else:
		t_index_bf = t_index_af - 1
	jl = [s[s_index_bf] - x, s[s_index_af] - x]
	tl = [t[t_index_bf] - x, t[t_index_af] - x]
	jt = []
	jt.append(abs(jl[0]) + abs(tl[0] - jl[0]))
	jt.append(abs(jl[0]) + abs(tl[1] - jl[0]))
	jt.append(abs(jl[1]) + abs(tl[0] - jl[1]))
	jt.append(abs(jl[1]) + abs(tl[1] - jl[1]))
	jt.append(abs(tl[0]) + abs(jl[0] - tl[0]))
	jt.append(abs(tl[0]) + abs(jl[1] - tl[0]))
	jt.append(abs(tl[1]) + abs(jl[0] - tl[1]))
	jt.append(abs(tl[1]) + abs(jl[1] - tl[1]))
	print(min(jt))


import bisect
(A, B, Q) = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	(b, d) = (bisect.bisect_right(s, x), bisect.bisect_right(t, x))
	res = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


from bisect import bisect
(a, b, q, *stx) = map(int, open(0).read().split())
s = [-10000000000.0] + stx[:a] + [2 * 10000000000.0]
t = [-10000000000.0] + stx[a:a + b] + [2 * 10000000000.0]
x = stx[a + b:a + b + q]
for i in x:
	pos_s = bisect(s, i)
	pos_t = bisect(t, i)
	sl = s[pos_s - 1]
	sr = s[pos_s]
	tl = t[pos_t - 1]
	tr = t[pos_t]
	ans = min(i - min(sl, tl), max(sr, tr) - i, sr - i + (sr - tl), tr - i + (tr - sl), i - sl + (tr - sl), i - tl + (sr - tl))
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
S = [-10 ** 18] + [int(input()) for _ in range(A)] + [10 ** 18]
T = [-10 ** 18] + [int(input()) for _ in range(B)] + [10 ** 18]
for i in range(Q):
	x = int(input())
	(b, d) = (bisect.bisect_right(S, x), bisect.bisect_right(T, x))
	ans = float('inf')
	for s in [S[b - 1], S[b]]:
		for t in [T[d - 1], T[d]]:
			(d1, d2) = (abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
			ans = min(ans, d1, d2)
	print(ans)


import sys
from bisect import bisect_left
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(A, B, Q) = list(map(int, readline().split()))
	S = [int(readline()) for _ in range(A)]
	T = [int(readline()) for _ in range(B)]
	X = [int(readline()) for _ in range(Q)]
	ans = [0] * Q
	vs = [0] * 2
	vt = [0] * 2
	for (i, x) in enumerate(X):
		idx_s = bisect_left(S, x)
		idx_t = bisect_left(T, x)
		vs[0] = S[idx_s - 1] if idx_s > 0 else -INF
		vt[0] = T[idx_t - 1] if idx_t > 0 else -INF
		vs[1] = S[idx_s] if idx_s < A else INF
		vt[1] = T[idx_t] if idx_t < B else INF
		ans[i] = min(abs(x - vs[0]) + abs(vs[0] - vt[0]), abs(x - vt[0]) + abs(vt[0] - vs[0]), abs(x - vs[0]) + abs(vs[0] - vt[1]), abs(x - vt[0]) + abs(vt[0] - vs[1]), abs(x - vs[1]) + abs(vs[1] - vt[1]), abs(x - vt[1]) + abs(vt[1] - vs[1]), abs(x - vs[1]) + abs(vs[1] - vt[0]), abs(x - vt[1]) + abs(vt[1] - vs[0]))
	print('\n'.join(map(str, ans)))
	return

def __starting_point():
	main()
__starting_point()


from bisect import bisect as bi
(a, b, q) = (int(i) for i in input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
inf = float('inf')
(s, t) = ([-inf] + s + [inf], [-inf] + t + [inf])

def f(x):
	(bs, bt) = (bi(s, x), bi(t, x))
	return min(x - min(s[bs - 1], t[bt - 1]), max(s[bs], t[bt]) - x, t[bt] - s[bs - 1] + min(t[bt] - x, x - s[bs - 1]), s[bs] - t[bt - 1] + min(s[bs] - x, x - t[bt - 1]))
for i in range(q):
	x = int(input())
	print(f(x))


from bisect import bisect
(a, b, q) = map(int, input().split())
INF = 10 ** 11 + 7
ls = [-INF] + [int(input()) for _ in range(a)] + [INF]
lt = [-INF] + [int(input()) for _ in range(b)] + [INF]
lq = [int(input()) for _ in range(q)]
for i in lq:
	inds = bisect(ls, i)
	indt = bisect(lt, i)
	(s1, s2) = (i - ls[inds - 1], ls[inds] - i)
	(t1, t2) = (i - lt[indt - 1], lt[indt] - i)
	print(min(max(s1, t1), max(s2, t2), min(2 * s1 + t2, s1 + 2 * t2), min(2 * t1 + s2, t1 + 2 * s2)))


from bisect import bisect_left as bl
(A, B, Q) = map(int, input().split())
S = []
T = []
X = []
INF = 10 ** 18
S.append(-INF)
T.append(-INF)
for i in range(A):
	S.append(int(input()))
for i in range(B):
	T.append(int(input()))
S.append(INF)
T.append(INF)
for i in range(Q):
	x = int(input())
	(b, d) = (bl(S, x), bl(T, x))
	res = INF
	for s in [S[b - 1], S[b]]:
		for t in [T[d - 1], T[d]]:
			(d1, d2) = (abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
			res = min(res, d1, d2)
	print(res)


import sys
from bisect import bisect_left
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(A, B, Q) = list(map(int, readline().split()))
	S = [-INF]
	S.extend((int(readline()) for _ in range(A)))
	S.append(INF)
	T = [-INF]
	T.extend((int(readline()) for _ in range(B)))
	T.append(INF)
	X = [int(readline()) for _ in range(Q)]
	ans = [INF] * Q
	for (i, x) in enumerate(X):
		idx_s = bisect_left(S, x)
		idx_t = bisect_left(T, x)
		for s in (S[idx_s - 1], S[idx_s]):
			for t in (T[idx_t - 1], T[idx_t]):
				ans[i] = min(ans[i], abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))
	print('\n'.join(map(str, ans)))
	return

def __starting_point():
	main()
__starting_point()


(A, B, Q) = map(int, input().split())
S = [-10 ** 18] + sorted([int(input()) for _ in range(A)]) + [10 ** 18]
T = [-10 ** 18] + sorted([int(input()) for _ in range(B)]) + [10 ** 18]
import bisect
for _ in range(Q):
	x = int(input())
	i = bisect.bisect_left(S, x)
	j = bisect.bisect_left(T, x)
	(p, q, r, s) = (S[i - 1], S[i], T[j - 1], T[j])
	res = 10 ** 18
	for a in (p, q):
		for b in (r, s):
			res = min(res, abs(x - a) + abs(a - b), abs(x - b) + abs(a - b))
	print(res)


import bisect
(a, b, q) = map(int, input().split())
inf = 10 ** 18
s = [-1 * inf] + [int(input()) for i in range(a)] + [inf]
t = [-1 * inf] + [int(input()) for i in range(b)] + [inf]
for i in range(q):
	q = int(input())
	x = bisect.bisect_right(s, q)
	y = bisect.bisect_right(t, q)
	res = inf
	for S in [s[x - 1], s[x]]:
		for T in [t[y - 1], t[y]]:
			(d1, d2) = (abs(S - q) + abs(T - S), abs(T - q) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


import bisect
(a, b, q) = map(int, input().split())
Inf = float('inf')
s = [-Inf] + [int(input()) for _ in range(a)] + [Inf]
t = [-Inf] + [int(input()) for _ in range(b)] + [Inf]
x = [int(input()) for _ in range(q)]
for i in x:
	si = bisect.bisect_left(s, i)
	ti = bisect.bisect_left(t, i)
	(sl, sr) = (i - s[si - 1], s[si] - i)
	(tl, tr) = (i - t[ti - 1], t[ti] - i)
	print(min(max(sl, tl), sl + tr + min(sl, tr), sr + tl + min(sr, tl), max(sr, tr)))


(A, B, Q) = map(int, input().split())
s = [-10 ** 11] + [int(input()) for _ in range(A)] + [10 ** 11]
t = [-10 ** 11] + [int(input()) for _ in range(B)] + [10 ** 11]
s.sort()
t.sort()
import bisect
import itertools
for _ in range(Q):
	pos = int(input())
	s_near_id = bisect.bisect_left(s, pos) - 1
	t_near_id = bisect.bisect_left(t, pos) - 1
	print(min((min(abs(pos - s_pos) + abs(s_pos - t_pos), abs(pos - t_pos) + abs(t_pos - s_pos)) for (s_pos, t_pos) in itertools.product(s[s_near_id:s_near_id + 2], t[t_near_id:t_near_id + 2]))))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 11
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	i = bisect.bisect_right(S, x)
	j = bisect.bisect_right(T, x)
	d = INF
	for s in [S[i - 1], S[i]]:
		for t in [T[j - 1], T[j]]:
			d1 = abs(s - x) + abs(s - t)
			d2 = abs(t - x) + abs(s - t)
			d = min(d, d1, d2)
	print(d)


from bisect import bisect_left
import sys
input = sys.stdin.readline
(A, B, Q) = (int(i) for i in input().split())
st = {'s': [], 't': []}
x = [0] * Q
for i in range(A):
	aa = int(input())
	st['s'].append(aa)
st['s'].append(10 ** 11)
for i in range(B):
	bb = int(input())
	st['t'].append(bb)
st['t'].append(10 ** 11)
for i in range(Q):
	x[i] = int(input())
dist = []
for xi in x:
	xs = bisect_left(st['s'], xi)
	s_l = st['s'][xs - 1]
	s_r = st['s'][xs]
	xs_l = abs(xi - s_l)
	xs_r = abs(xi - s_r)
	xs_l_t = bisect_left(st['t'], s_l)
	t_l = st['t'][xs_l_t - 1]
	t_r = st['t'][xs_l_t]
	xs_l_t_l = abs(s_l - t_l)
	xs_l_t_r = abs(s_l - t_r)
	xs_r_t = bisect_left(st['t'], s_r)
	t_l = st['t'][xs_r_t - 1]
	t_r = st['t'][xs_r_t]
	xs_r_t_l = abs(s_r - t_l)
	xs_r_t_r = abs(s_r - t_r)
	ds = min(xs_l + xs_l_t_l, xs_l + xs_l_t_r, xs_r + xs_r_t_l, xs_r + xs_r_t_r)
	xs = bisect_left(st['t'], xi)
	s_l = st['t'][xs - 1]
	s_r = st['t'][xs]
	xs_l = abs(xi - s_l)
	xs_r = abs(xi - s_r)
	xs_l_t = bisect_left(st['s'], s_l)
	t_l = st['s'][xs_l_t - 1]
	t_r = st['s'][xs_l_t]
	xs_l_t_l = abs(s_l - t_l)
	xs_l_t_r = abs(s_l - t_r)
	xs_r_t = bisect_left(st['s'], s_r)
	t_l = st['s'][xs_r_t - 1]
	t_r = st['s'][xs_r_t]
	xs_r_t_l = abs(s_r - t_l)
	xs_r_t_r = abs(s_r - t_r)
	dt = min(xs_l + xs_l_t_l, xs_l + xs_l_t_r, xs_r + xs_r_t_l, xs_r + xs_r_t_r)
	print(min(ds, dt))


from bisect import bisect_left
I = lambda x: [-1 << 50] + [int(input()) for i in range(x)] + [1 << 50]
(A, B, Q) = map(int, input().split())
(S, T) = (I(A), I(B))
for _ in range(Q):
	x = int(input())
	LS_ = bisect_left(S, x)
	LT_ = bisect_left(T, x)
	LS = S[LS_ - 1]
	RS = S[LS_]
	LT = T[LT_ - 1]
	RT = T[LT_]
	print(min(x - min(LS, LT), max(RS, RT) - x, (x - LS) * 2 + (RT - x), (x - LT) * 2 + (RS - x), (RT - x) * 2 + (x - LS), (RS - x) * 2 + (x - LT)))


import bisect
(A, B, Q) = map(int, input().split())
(*S,) = [int(input()) for _ in range(A)]
(*T,) = [int(input()) for _ in range(B)]
(*X,) = [int(input()) for _ in range(Q)]
for x in X:
	sx = bisect.bisect(S, x)
	try:
		se = S[sx]
	except:
		se = S[-1]
	try:
		sw = S[sx - 1]
	except:
		sw = S[0]
	tx = bisect.bisect(T, x)
	try:
		te = T[tx]
	except:
		te = T[-1]
	try:
		tw = T[tx - 1]
	except:
		tw = T[0]
	routes = []
	for s in [se, sw]:
		for t in [te, tw]:
			routes.append(abs(s - x) + abs(t - s))
			routes.append(abs(t - x) + abs(s - t))
	print(min(routes))


def near(x, X):
	i = bisect_left(X, x)
	if i == 0:
		return [X[i] - x]
	elif i == len(X):
		return [X[i - 1] - x]
	else:
		return [X[i] - x, X[i - 1] - x]
from bisect import *
(A, B, Q) = map(int, input().split())
(S, T) = ([], [])
for _ in range(A):
	S.append(int(input()))
for _ in range(B):
	T.append(int(input()))
for _ in range(Q):
	res = 1 << 40
	x = int(input())
	for a in near(x, S):
		for b in near(x, T):
			res = min(abs(a) + abs(a - b), abs(b) + abs(b - a), res)
	print(res)


(A, B, Q) = map(int, input().split())
S = [-10 ** 10] + [int(input()) for i in range(A)] + [2 * 10 ** 10]
T = [-10 ** 10] + [int(input()) for i in range(B)] + [2 * 10 ** 10]
X = [int(input()) for i in range(Q)]
from bisect import bisect
for x in X:
	s = bisect(S, x)
	sl = S[s - 1]
	sr = S[s]
	t = bisect(T, x)
	tl = T[t - 1]
	tr = T[t]
	print(min(x - sl + abs(tl - sl), x - sl + (tr - sl), sr - x + abs(tr - sr), sr - x + (sr - tl), x - tl + abs(tl - sl), x - tl + (sr - tl), tr - x + abs(tr - sr), tr - x + (tr - sl)))


import bisect
(A, B, Q) = map(int, input().split())
s = list((int(input()) for _ in range(A)))
t = list((int(input()) for _ in range(B)))
for i in range(Q):
	ans = 10 ** 19
	x = int(input())
	stmp1 = bisect.bisect_left(s, x)
	for j in range(2):
		if stmp1 == 0 and j == 1:
			continue
		if stmp1 == A and j == 0:
			continue
		ttmp1 = bisect.bisect_left(t, s[stmp1 - j])
		ktmp = 10 ** 11
		if ttmp1 != 0:
			ktmp = min(ktmp, abs(s[stmp1 - j] - t[ttmp1 - 1]))
		if ttmp1 != B:
			ktmp = min(ktmp, abs(s[stmp1 - j] - t[ttmp1]))
		total = abs(x - s[stmp1 - j]) + ktmp
		ans = min(ans, total)
	ttmp1 = bisect.bisect_left(t, x)
	for j in range(2):
		if ttmp1 == 0 and j == 1:
			continue
		if ttmp1 == B and j == 0:
			continue
		stmp1 = bisect.bisect_left(s, t[ttmp1 - j])
		ktmp = 10 ** 11
		if stmp1 != 0:
			ktmp = min(ktmp, abs(t[ttmp1 - j] - s[stmp1 - 1]))
		if stmp1 != A:
			ktmp = min(ktmp, abs(t[ttmp1 - j] - s[stmp1]))
		total = abs(x - t[ttmp1 - j]) + ktmp
		ans = min(ans, total)
	print(ans)


import sys
from bisect import bisect_left
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7

def neighborhood(L, now):
	tmp = bisect_left(L, now)
	if tmp == 0:
		return [tmp, None]
	elif tmp == len(L):
		return [tmp - 1, None]
	else:
		return [tmp - 1, tmp]

def dist_calc(now, p1, p2):
	if p1 is None or p2 is None:
		return f_inf
	res1 = abs(now - p1) + abs(p1 - p2)
	res2 = abs(now - p2) + abs(p1 - p2)
	return min(res1, res2)

def resolve():
	(a, b, q) = list(map(int, input().split()))
	S = list((int(input()) for _ in range(a)))
	T = list((int(input()) for _ in range(b)))
	query = list((int(input()) for _ in range(q)))
	for x in query:
		dist1 = f_inf
		(s1, s2) = neighborhood(S, x)
		for s in [s1, s2]:
			if s is not None:
				(t1, t2) = neighborhood(T, S[s])
				for t in [t1, t2]:
					if t is not None:
						dist1 = min(dist1, dist_calc(x, S[s], T[t]))
		dist2 = f_inf
		(t1, t2) = neighborhood(T, x)
		for t in [t1, t2]:
			if t is not None:
				(s1, s2) = neighborhood(S, T[t])
				for s in [s1, s2]:
					if s is not None:
						dist2 = min(dist2, dist_calc(x, S[s], T[t]))
		res = min(dist1, dist2)
		print(res)

def __starting_point():
	resolve()
__starting_point()


import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline())
ns = lambda : list(map(int, sys.stdin.readline().split()))
na = lambda : list(map(int, sys.stdin.readline().split()))
na1 = lambda : list([int(x) - 1 for x in sys.stdin.readline().split()])

def main():
	(a, b, q) = ns()
	s = [ni() for _ in range(a)]
	t = [ni() for _ in range(b)]

	def disLR(a, idx):
		small = a[max(0, idx - 1)]
		large = a[min(len(a) - 1, idx)]
		return [small, large]
	for _ in range(q):
		ans = INF
		x = ni()
		idxs = bisect.bisect_left(s, x)
		a1 = disLR(s, idxs)
		idxt = bisect.bisect_left(t, x)
		a2 = disLR(t, idxt)
		for ta1 in a1:
			for ta2 in a2:
				tmp1 = abs(x - ta1) + abs(ta2 - ta1)
				tmp2 = abs(x - ta2) + abs(ta1 - ta2)
				ans = min(ans, tmp1, tmp2)
		print(ans)

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
import bisect
(a, b, q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
for _ in range(q):
	x = int(input())
	i = bisect.bisect_right(s, x)
	j = bisect.bisect_right(t, x)
	d = INF
	for si in [s[i - 1], s[i]]:
		for ti in [t[j - 1], t[j]]:
			d1 = abs(x - si) + abs(ti - si)
			d2 = abs(x - ti) + abs(ti - si)
			d = min(d, d1, d2)
	print(d)


import bisect
from itertools import product
inf = float('inf')
(a, b, q) = map(int, input().split())
s = [-inf] + [int(input()) for i in range(a)] + [inf]
t = [-inf] + [int(input()) for i in range(b)] + [inf]
x = [int(input()) for i in range(q)]
for i in x:
	ids = bisect.bisect_right(s, i)
	idt = bisect.bisect_right(t, i)
	ans = inf
	for (ss, tt) in product(s[ids - 1:ids + 1], t[idt - 1:idt + 1]):
		check = min(abs(i - ss), abs(i - tt)) + abs(ss - tt)
		ans = min(ans, check)
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
A = [-1 * 10 ** 12]
B = [-1 * 10 ** 12]
for i in range(a):
	A.append(int(input()))
for i in range(b):
	B.append(int(input()))
A.append(10 ** 12)
B.append(10 ** 12)
for i in range(q):
	x = int(input())
	ia = bisect.bisect_right(A, x)
	ms = A[ia - 1]
	ps = A[ia]
	ib = bisect.bisect_right(B, x)
	mt = B[ib - 1]
	pt = B[ib]
	ans = min(x - min(ms, mt), max(ps, pt) - x, ps - mt + min(ps - x, x - mt), pt - ms + min(pt - x, x - ms))
	print(ans)


from bisect import bisect_left
from itertools import product
from sys import maxsize
(a, b, q) = list(map(int, input().split()))
s = [-maxsize] + [int(input()) for _ in range(a)] + [maxsize]
t = [-maxsize] + [int(input()) for _ in range(b)] + [maxsize]
for _ in range(q):
	x = int(input())
	i = bisect_left(s, x)
	j = bisect_left(t, x)
	res = maxsize
	for (ss, tt) in product((s[i - 1], s[i]), (t[j - 1], t[j])):
		res = min(res, abs(ss - x) + abs(tt - ss), abs(tt - x) + abs(ss - tt))
	print(res)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = sorted([-INF] + [int(input()) for i in range(A)] + [INF])
T = sorted([-INF] + [int(input()) for i in range(B)] + [INF])
for _ in range(Q):
	x = int(input())
	si = bisect.bisect_right(S, x)
	ti = bisect.bisect_right(T, x)
	d = INF
	for s in [S[si - 1], S[si]]:
		for t in [T[ti - 1], T[ti]]:
			d1 = abs(s - x) + abs(t - s)
			d2 = abs(t - x) + abs(s - t)
			d = min(d, d1, d2)
	print(d)


import bisect
(a, b, q) = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()
for i in range(q):
	ans = 10 ** 11
	x = int(input())
	c = bisect.bisect_left(s, x)
	if c == 0:
		(e1, e2) = (s[0], s[0])
	elif c == a:
		(e1, e2) = (s[-1], s[-1])
	else:
		(e1, e2) = (s[c - 1], s[c])
	f = bisect.bisect_left(t, e1)
	if f == 0:
		(f1, f2) = (t[0], t[0])
	elif f == b:
		(f1, f2) = (t[-1], t[-1])
	else:
		(f1, f2) = (t[f - 1], t[f])
	ans = min(ans, abs(x - e1) + abs(e1 - f1), abs(x - e1) + abs(e1 - f2))
	f = bisect.bisect_left(t, e2)
	if f == 0:
		(f1, f2) = (t[0], t[0])
	elif f == b:
		(f1, f2) = (t[-1], t[-1])
	else:
		(f1, f2) = (t[f - 1], t[f])
	ans = min(ans, abs(x - e2) + abs(e2 - f1), abs(x - e2) + abs(e2 - f2))
	d = bisect.bisect_left(t, x)
	if d == 0:
		(f1, f2) = (t[0], t[0])
	elif d == b:
		(f1, f2) = (t[-1], t[-1])
	else:
		(f1, f2) = (t[d - 1], t[d])
	e = bisect.bisect_left(s, f1)
	if e == 0:
		(e1, e2) = (s[0], s[0])
	elif e == a:
		(e1, e2) = (s[-1], s[-1])
	else:
		(e1, e2) = (s[e - 1], s[e])
	ans = min(ans, abs(x - f1) + abs(f1 - e1), abs(x - f1) + abs(f1 - e2))
	e = bisect.bisect_left(s, f2)
	if e == 0:
		(e1, e2) = (s[0], s[0])
	elif e == a:
		(e1, e2) = (s[-1], s[-1])
	else:
		(e1, e2) = (s[e - 1], s[e])
	ans = min(ans, abs(x - f2) + abs(f2 - e1), abs(x - f2) + abs(f2 - e2))
	print(ans)


import sys
sys.setrecursionlimit(10 ** 8)

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

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

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

def li2(N):
	return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]

def dp2(ini, i, j):
	return [[ini] * i for _ in range(j)]
import bisect
(A, B, Q) = mi()
S = [ii() for _ in range(A)]
T = [ii() for _ in range(B)]
S_ = [S[0]] + S + [S[A - 1]]
T_ = [T[0]] + T + [T[B - 1]]
for _ in range(Q):
	f = ii()
	to_s = bisect.bisect_left(S, f)
	to_t = bisect.bisect_left(T, f)
	ans = min(abs(S_[to_s + 1] - f) + abs(S_[to_s + 1] - T_[to_t + 1]), float('inf'))
	ans = min(abs(S_[to_s + 1] - T_[to_t + 1]) + abs(f - T_[to_t + 1]), ans)
	ans = min(abs(S_[to_s + 1] - f) + abs(S_[to_s + 1] - T_[to_t]), ans)
	ans = min(abs(S_[to_s + 1] - T_[to_t]) + abs(f - T_[to_t]), ans)
	ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t + 1]), ans)
	ans = min(abs(S_[to_s] - T_[to_t + 1]) + abs(f - T_[to_t + 1]), ans)
	ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t]), ans)
	ans = min(abs(S_[to_s] - T_[to_t]) + abs(f - T_[to_t]), ans)
	print(ans)


import sys
input = sys.stdin.readline
import bisect
INF = 100000000000.0
(A, B, Q) = map(int, input().split())
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
x = [int(input()) for _ in range(Q)]
for i in range(Q):
	(a, b) = (bisect.bisect_right(s, x[i]), bisect.bisect_right(t, x[i]))
	ans = INF
	for j in [s[a - 1], s[a]]:
		for k in [t[b - 1], t[b]]:
			d1 = abs(j - x[i]) + abs(j - k)
			d2 = abs(k - x[i]) + abs(j - k)
			ans = min(ans, d1, d2)
	print(ans)


import bisect
import sys
(A, B, Q) = map(int, input().split())
INF = 10 ** 13
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(sys.stdin.readline())
	(b, d) = (bisect.bisect_right(s, x), bisect.bisect_right(t, x))
	res = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


from bisect import *
(a, b, q) = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
inf = 10 ** 18
s = [-inf] + s + [inf]
t = [-inf] + t + [inf]
for i in range(q):
	x = int(input())
	s_ind = bisect_left(s, x)
	t_ind = bisect_left(t, x)
	res = inf
	for cand_s in [s[s_ind - 1], s[s_ind]]:
		for cand_t in [t[t_ind - 1], t[t_ind]]:
			st = abs(cand_s - cand_t)
			dx = min(abs(cand_s - x), abs(cand_t - x))
			res = min(res, st + dx)
	print(res)


import sys
from bisect import bisect_right
input = sys.stdin.readline
INF = 10 ** 18
(a, b, q) = map(int, input().split())
S = [-INF] + [int(input()) for _ in range(a)] + [INF]
T = [-INF] + [int(input()) for _ in range(b)] + [INF]
for i in range(q):
	x = int(input())
	(c, d) = (bisect_right(S, x), bisect_right(T, x))
	ans = INF
	for s in S[c - 1:c + 1]:
		for t in T[d - 1:d + 1]:
			ans = min(ans, abs(t - x) + abs(s - t), abs(s - x) + abs(t - s))
	print(ans)


import bisect
INF = 1 << 64
(A, B, Q) = map(int, input().split())
S = [int(input()) for a in range(A)]
T = [int(input()) for b in range(B)]

def cost(S, A, x):
	index_s = bisect.bisect_left(S, x)
	ws = INF if index_s == 0 else x - S[index_s - 1]
	es = INF if index_s == A else S[index_s] - x
	return (ws, es)
for q in range(Q):
	x = int(input())
	(ws, es) = cost(S, A, x)
	(wt, et) = cost(T, B, x)
	print(min(max(ws, wt), max(es, et), min(ws, et) + ws + et, min(es, wt) + es + wt))


from bisect import bisect_left, bisect
(a, b, q) = map(int, input().split())
jin = [-10 ** 16]
for i in range(a):
	jin.append(int(input()))
jin.append(10 ** 16)
tera = [-10 ** 20]
for i in range(b):
	tera.append(int(input()))
tera.append(10 ** 20)
for i in range(q):
	x = int(input())
	j = bisect(jin, x)
	t = bisect(tera, x)
	j1 = jin[j]
	j2 = jin[j - 1]
	t1 = tera[t]
	t2 = tera[t - 1]
	ans = min(abs(x - j1) + abs(j1 - t1), abs(x - j1) + abs(j1 - t2), abs(x - j2) + abs(j2 - t1), abs(x - j2) + abs(j2 - t2))
	ans = min(ans, abs(x - t1) + abs(t1 - j1), abs(x - t1) + abs(t1 - j2), abs(x - t2) + abs(t2 - j1), abs(x - t2) + abs(t2 - j2))
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
for k in range(q):
	x = int(input())
	i = bisect.bisect_right(s, x)
	j = bisect.bisect_right(t, x)
	d = INF
	for S in [s[i - 1], s[i]]:
		for T in [t[j - 1], t[j]]:
			d1 = abs(S - x) + abs(S - T)
			d2 = abs(T - x) + abs(S - T)
			d = min(d, d1, d2)
	print(d)


def main():
	from bisect import bisect_left
	(A, B, Q) = list(map(int, input().split()))
	INF = 2 * 10 ** 10
	S = [-INF] + [int(input()) for _ in range(A)] + [INF]
	T = [-INF] + [int(input()) for _ in range(B)] + [INF]
	S_rev = [-s for s in S][::-1]
	T_rev = [-t for t in T][::-1]
	for q in range(Q):
		x = int(input())
		ans = 5 * 10 ** 10
		cost1_s = S[bisect_left(S, x)] - x
		cost1_t = T[bisect_left(T, x)] - x
		ans = min(ans, max(cost1_s, cost1_t))
		cost2_s = S_rev[bisect_left(S_rev, -x)] + x
		cost2_t = T_rev[bisect_left(T_rev, -x)] + x
		ans = min(ans, max(cost2_s, cost2_t))
		cost3_s = S[bisect_left(S, x)] - x
		cost3_t = T_rev[bisect_left(T_rev, -x)] + x
		ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t))
		cost3_s = S_rev[bisect_left(S_rev, -x)] + x
		cost3_t = T[bisect_left(T, x)] - x
		ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t))
		print(ans)

def __starting_point():
	main()
__starting_point()


INF = 1e+20
(A, B, Q) = [int(i) for i in input().split()]
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
from bisect import bisect_left
mins = []
for _ in range(Q):
	x = int(input())
	s = bisect_left(S, x)
	t = bisect_left(T, x)
	mi = INF
	for ss in [S[s], S[s - 1]]:
		for tt in [T[t], T[t - 1]]:
			mi = min(mi, abs(ss - x) + abs(tt - ss), abs(tt - x) + abs(ss - tt))
	mins.append(mi)
print(*mins, sep='\n')


from bisect import bisect_left
(a, b, q) = map(int, input().split())
INF = 10 ** 12
S = [int(input()) for i in range(a)]
S.append(INF)
T = [int(input()) for i in range(b)]
T.append(INF)
for _ in range(q):
	x = int(input())
	i = bisect_left(S, x)
	j = bisect_left(T, x)
	ans = []
	for y in [S[i - 1], S[i]]:
		for z in [T[j - 1], T[j]]:
			ans.append(abs(x - y) + abs(y - z))
			ans.append(abs(x - z) + abs(z - y))
	print(min(ans))


import bisect
inf = 10 ** 18
(a, b, q) = map(int, input().split())
shrine = [-inf] + list((int(input()) for _ in range(a))) + [inf]
temple = [-inf] + list((int(input()) for _ in range(b))) + [inf]
for q in range(q):
	x = int(input())
	(m, n) = (bisect.bisect_right(shrine, x), bisect.bisect_right(temple, x))
	res = inf
	for s in [shrine[m - 1], shrine[m]]:
		for t in [temple[n - 1], temple[n]]:
			(d1, d2) = (abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
			res = min(res, d1, d2)
	print(res)


import sys
import math
from collections import deque
import bisect
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().strip()
NI = lambda : int(input())
NMI = lambda : map(int, input().split())
NLI = lambda : list(NMI())
SI = lambda : input()

def make_grid(h, w, num):
	return [[int(num)] * w for _ in range(h)]

def main():
	(A, B, Q) = NMI()
	S = [-float('inf')] + [NI() for _ in range(A)] + [float('inf')]
	T = [-float('inf')] + [NI() for _ in range(B)] + [float('inf')]
	Q = [NI() for _ in range(Q)]
	for q in Q:
		s_idx = bisect.bisect_left(S, q)
		t_idx = bisect.bisect_left(T, q)
		(sl, sr, tl, tr) = (S[s_idx - 1], S[s_idx], T[t_idx - 1], T[t_idx])
		LL = max(abs(q - sl), abs(q - tl))
		RR = max(abs(q - sr), abs(q - tr))
		LR = abs(q - sl) * 2 + abs(q - tr)
		RL = abs(q - sr) * 2 + abs(q - tl)
		LR2 = abs(q - tl) * 2 + abs(q - sr)
		RL2 = abs(q - tr) * 2 + abs(q - sl)
		res = min(LL, RR, LR, LR2, RL, RL2)
		print(res)

def __starting_point():
	main()
__starting_point()


from bisect import bisect_right
(A, B, Q) = map(int, input().split())
INF = float('inf')
S = [-INF] + list((int(input()) for _ in range(A))) + [INF]
T = [-INF] + list((int(input()) for _ in range(B))) + [INF]
for _ in range(Q):
	x = int(input())
	i = bisect_right(S, x)
	j = bisect_right(T, x)
	d = INF
	for s in [S[i - 1], S[i]]:
		for t in [T[j - 1], T[j]]:
			d1 = abs(s - x) + abs(s - t)
			d2 = abs(t - x) + abs(s - t)
			d = min(d, d1, d2)
	print(d)


import bisect
(a, b, q) = map(int, input().split())
s = [int(input()) for i in range(a)]
s.extend([float('-inf'), float('inf')])
s.sort()
t = [int(input()) for i in range(b)]
t.extend([float('-inf'), float('inf')])
t.sort()
for i in range(q):
	x = int(input())
	s2 = bisect.bisect_right(s, x)
	s1 = s2 - 1
	t2 = bisect.bisect_right(t, x)
	t1 = t2 - 1
	print(min(x - min(s[s1], t[t1]), max(s[s2], t[t2]) - x, x - s[s1] * 2 + t[t2], t[t2] * 2 - x - s[s1], x - t[t1] * 2 + s[s2], s[s2] * 2 - x - t[t1]))


from bisect import bisect_right
INF = 10 ** 18
(A, B, Q) = map(int, input().split())
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	(se, te) = (bisect_right(S, x), bisect_right(T, x))
	ans = INF
	for s in [S[se - 1], S[se]]:
		for t in [T[te - 1], T[te]]:
			(ds, dt) = (abs(s - x) + abs(s - t), abs(t - x) + abs(t - s))
			ans = min(ans, ds, dt)
	print(ans)


from bisect import bisect_left
(A, B, Q) = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
for i in range(Q):
	x = int(input())
	si = bisect_left(S, x)
	ti = bisect_left(T, x)
	if 0 < si < A and 0 < ti < B:
		(sl, sr) = (abs(x - S[si - 1]), abs(x - S[si]))
		(tl, tr) = (abs(x - T[ti - 1]), abs(x - T[ti]))
		print(min(max(sr, tr), max(sl, tl), sl + tr + min(sl, tr), sr + tl + min(sr, tl)))
	elif si == 0 and ti == 0:
		sr = abs(x - S[si])
		tr = abs(x - T[ti])
		print(max(sr, tr))
	elif si == A and ti == B:
		sl = abs(x - S[si - 1])
		tl = abs(x - T[ti - 1])
		print(max(sl, tl))
	elif si == 0 and ti == B:
		sr = abs(x - S[si])
		tl = abs(x - T[ti - 1])
		print(sr + tl + min(sr, tl))
	elif si == A and ti == 0:
		sl = abs(x - S[si - 1])
		tr = abs(x - T[ti])
		print(sl + tr + min(sl, tr))
	elif si == 0:
		sr = abs(x - S[si])
		(tl, tr) = (abs(x - T[ti - 1]), abs(x - T[ti]))
		print(min(max(sr, tr), sr + tl + min(sr, tl)))
	elif si == A:
		sl = abs(x - S[si - 1])
		(tl, tr) = (abs(x - T[ti - 1]), abs(x - T[ti]))
		print(min(max(sl, tl), sl + tr + min(sl, tr)))
	elif ti == 0:
		(sl, sr) = (abs(x - S[si - 1]), abs(x - S[si]))
		tr = abs(x - T[ti])
		print(min(max(sr, tr), sl + tr + min(sl, tr)))
	elif ti == B:
		(sl, sr) = (abs(x - S[si - 1]), abs(x - S[si]))
		tl = abs(x - T[ti - 1])
		print(min(max(sl, tl), sr + tl + min(sr, tl)))


import bisect
import itertools
(A, B, Q) = map(int, input().split())
INF = 10 ** 15
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
for _ in range(Q):
	answer = INF
	x = int(input())
	si = bisect.bisect_left(S, x)
	ti = bisect.bisect_left(T, x)
	for (s, t) in itertools.product(S[si - 1:si + 1], T[ti - 1:ti + 1]):
		d = min(abs(s - x), abs(t - x)) + abs(t - s)
		answer = min(answer, d)
	print(answer)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	s = bisect.bisect_right(S, x)
	t = bisect.bisect_right(T, x)
	d = INF
	for i in [S[s - 1], S[s]]:
		for j in [T[t - 1], T[t]]:
			ds = abs(i - x) + abs(i - j)
			dt = abs(j - x) + abs(i - j)
			d = min(d, ds, dt)
	print(d)


import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal
from functools import reduce

def s():
	return input()

def k():
	return int(input())

def S():
	return input().split()

def I():
	return list(map(int, input().split()))

def X():
	return list(input())

def L():
	return list(input().split())

def l():
	return list(map(int, input().split()))

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

def gcd(*numbers):
	reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10 ** 9 + 7
count = 0
ans = 0
(A, B, Q) = I()
inf = float('inf')
s = [-inf] + sorted([k() for _ in range(A)]) + [inf]
t = [-inf] + sorted([k() for _ in range(B)]) + [inf]
aa = []
for i in range(Q):
	q = k()
	S = bisect.bisect(s, q)
	T = bisect.bisect(t, q)
	ans = inf
	for j in [s[S - 1], s[S]]:
		for z in [t[T - 1], t[T]]:
			x = abs(j - q) + abs(j - z)
			y = abs(z - q) + abs(j - z)
			ans = min(ans, x, y)
	aa.append(ans)
for i in aa:
	print(i)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left, bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil, pi, factorial
from operator import itemgetter

def I():
	return int(input())

def MI():
	return list(map(int, input().split()))

def LI():
	return list(map(int, input().split()))

def LI2():
	return [int(input()) for i in range(n)]

def MXI():
	return [[LI()] for i in range(n)]

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

def printns(x):
	print('\n'.join(x))

def printni(x):
	print('\n'.join(list(map(str, x))))
inf = 10 ** 17
mod = 10 ** 9 + 7
(a, b, q) = MI()
lis1 = [I() for i in range(a)]
lis2 = [I() for i in range(b)]
lis1.append(inf)
lis2.append(inf)
lis1 = [-inf] + lis1
lis2 = [-inf] + lis2
for i in range(q):
	u = I()
	x = bisect_left(lis1, u)
	y = bisect_left(lis2, u)
	Lll = u - min(lis1[x - 1], lis2[y - 1])
	Lrr = max(lis1[x], lis2[y]) - u
	Llr1 = 2 * (u - lis1[x - 1]) + (lis2[y] - u)
	Llr2 = 2 * (lis2[y] - u) + u - lis1[x - 1]
	Lrl1 = 2 * (u - lis2[y - 1]) + lis1[x] - u
	Lrl2 = 2 * (lis1[x] - u) + u - lis2[y - 1]
	print(min(Lll, Lrr, Lrl1, Lrl2, Llr1, Llr2))


import bisect
(A, B, Q) = map(int, input().split())
S = list((int(input()) for _ in range(A)))
T = list((int(input()) for _ in range(B)))
for _ in range(Q):
	x = int(input())
	s = bisect.bisect_right(S, x)
	t = bisect.bisect_right(T, x)
	s %= A
	t %= B
	ans = 2 * 10 ** 10
	for i in range(2):
		for j in range(2):
			ans = min(ans, min(abs(x - S[s - i]) + abs(S[s - i] - T[t - j]), abs(x - T[t - j]) + abs(T[t - j] - S[s - i])))
	print(ans)


import sys
from bisect import bisect_left
input = sys.stdin.readline

def solve(Q, S, T, X):
	ans = [0] * Q
	INF = 10 ** 11
	for (q, x) in enumerate(X):
		min_dist = INF
		i_s = bisect_left(S, x)
		i_t = bisect_left(T, x)
		for s in (S[i_s - 1], S[i_s]):
			for t in (T[i_t - 1], T[i_t]):
				dist_s2t = abs(x - s) + abs(s - t)
				dist_t2s = abs(x - t) + abs(t - s)
				min_dist = min(min_dist, dist_s2t, dist_t2s)
		ans[q] = min_dist
	return ans

def main():
	(A, B, Q) = list(map(int, input().split()))
	S = [0] * (A + 2)
	T = [0] * (B + 2)
	X = [0] * Q
	for i in range(1, A + 1):
		S[i] = int(input())
	for i in range(1, B + 1):
		T[i] = int(input())
	for i in range(Q):
		X[i] = int(input())
	S[0] = T[0] = -10 ** 11
	S[-1] = T[-1] = 10 ** 11
	ans = solve(Q, S, T, X)
	print('\n'.join(map(str, ans)))

def __starting_point():
	main()
__starting_point()


import bisect
(a, b, q) = map(int, input().split())
INF = 10 ** 12
S = [-INF] + [int(input()) for i in range(a)] + [INF]
T = [-INF] + [int(input()) for i in range(b)] + [INF]
for i in range(q):
	x = int(input())
	sid = bisect.bisect_left(S, x)
	tid = bisect.bisect_left(T, x)
	ans = INF
	for s in (S[sid - 1], S[sid]):
		for t in (T[tid - 1], T[tid]):
			ans = min(ans, abs(x - s) + abs(s - t))
			ans = min(ans, abs(x - t) + abs(t - s))
	print(ans)


ma = lambda : map(int, input().split())
lma = lambda : list(map(int, input().split()))
tma = lambda : tuple(map(int, input().split()))
ni = lambda : int(input())
yn = lambda fl: print('Yes') if fl else print('No')
import collections
import math
import itertools
import heapq as hq
mx = 3 * 10 ** 10
(a, b, q) = ma()
S = [0 for i in range(a + 2)]
T = [0 for i in range(b + 2)]
S[0] = -mx
S[a + 1] = mx
T[0] = -mx
T[b + 1] = mx
for i in range(a):
	S[i + 1] = ni()
for i in range(b):
	T[i + 1] = ni()

def bs(x, ls):
	ok = 0
	ng = len(ls) - 1
	while ng - ok > 1:
		idx = (ok + ng) // 2
		if ls[idx] < x:
			ok = idx
		else:
			ng = idx
	return ok

def solve(x):
	ids = bs(x, S)
	s1 = S[ids]
	s2 = S[ids + 1]
	idt = bs(x, T)
	t1 = T[idt]
	t2 = T[idt + 1]
	s1t1 = max(x - s1, x - t1)
	s1t2 = 2 * min(x - s1, t2 - x) + max(x - s1, t2 - x)
	s2t1 = 2 * min(s2 - x, x - t1) + max(s2 - x, x - t1)
	s2t2 = max(s2 - x, t2 - x)
	print(min(s1t1, s1t2, s2t1, s2t2))
for i in range(q):
	x = ni()
	solve(x)


import bisect
(a, b, q) = map(int, input().split())
s = []
t = []
for i in range(a):
	s.append(int(input()))
for i in range(b):
	t.append(int(input()))
for _ in range(q):
	x = int(input())
	ans = [0, 0, 0]
	i = bisect.bisect_left(s, x)
	if i == 0:
		sl = 100000000000
	else:
		sl = abs(s[i - 1] - x)
	if i == a:
		sr = 100000000000
	else:
		sr = abs(s[i] - x)
	i = bisect.bisect_left(t, x)
	if i == 0:
		tl = 100000000000
	else:
		tl = abs(t[i - 1] - x)
	if i == b:
		tr = 100000000000
	else:
		tr = abs(t[i] - x)
	ans[0] = max(tl, sl)
	ans[1] = max(tr, sr)
	ans[2] = min(2 * tl + sr, 2 * sl + tr, 2 * sr + tl, 2 * tr + sl)
	print(min(ans))


import bisect
(A, B, Q) = map(int, input().split())
s = [-10 ** 15]
t = [-10 ** 15]
ans = []
for _ in range(A):
	s.append(int(input()))
for _ in range(B):
	t.append(int(input()))
s.append(10 ** 15)
t.append(10 ** 15)

def search(a, b, q):
	dis = 10 ** 15
	S = [s[a - 1], s[a]]
	T = [t[b - 1], t[b]]
	for i in range(2):
		for j in range(2):
			A = min(abs(S[i] - q), abs(T[j] - q)) + abs(S[i] - T[j])
			dis = min(dis, A)
	return dis
for _ in range(Q):
	q = int(input())
	r = bisect.bisect_left(s, q)
	l = bisect.bisect_left(t, q)
	ans.append(search(r, l, q))
for i in ans:
	print(i)


from bisect import bisect_right
(a, b, q) = map(int, input().split())
INF = 10 ** 18
J = [-INF] + [int(input()) for _ in range(a)] + [INF]
T = [-INF] + [int(input()) for _ in range(b)] + [INF]
for _ in range(q):
	s = int(input())
	(b, d) = (bisect_right(J, s), bisect_right(T, s))
	res = INF
	for j in [J[b - 1], J[b]]:
		for t in [T[d - 1], T[d]]:
			(d1, d2) = (abs(j - s) + abs(t - j), abs(t - s) + abs(j - t))
			res = min(res, d1, d2)
	print(res)


import bisect
INF = 10 ** 11
(A, B, q) = map(int, input().split())
a = [-INF] + [int(input()) for _ in range(A)] + [INF]
b = [-INF] + [int(input()) for _ in range(B)] + [INF]
for _ in range(q):
	x = int(input())
	i = bisect.bisect_left(a, x)
	j = bisect.bisect_left(b, x)
	ans = min(max(a[i], b[j]) - x, x - min(a[i - 1], b[j - 1]), a[i] - b[j - 1] + min(a[i] - x, x - b[j - 1]), b[j] - a[i - 1] + min(b[j] - x, x - a[i - 1]))
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]

def kai(x):
	res = INF
	s1 = bisect.bisect_right(s, x)
	ls = [s1, s1 - 1]
	t1 = bisect.bisect_right(t, x)
	lt = [t1, t1 - 1]
	for ss in ls:
		for st in lt:
			res = min(res, abs(x - s[ss]) + abs(s[ss] - t[st]), abs(x - t[st]) + abs(s[ss] - t[st]))
	return res
lx = [int(input()) for _ in range(q)]
for x in lx:
	print(kai(x))


def is_not_less_than(ls, ind, key):
	if ls[ind] >= key:
		return True
	else:
		return False

def return_min_ind_whose_value_not_less_than(ls, key):
	r = len(ls)
	l = -1
	while True:
		next_ind = (r + l) // 2
		if is_not_less_than(ls, next_ind, key):
			r = next_ind
		else:
			l = next_ind
		if r - l == 1:
			return r
(n_shrine, n_temple, Q) = list(map(int, input().split()))
shrine_ls = [0] * (n_shrine + 1)
temple_ls = [0] * (n_temple + 1)
Q_ls = [0] * Q
for i in range(n_shrine):
	shrine_ls[i] = int(input())
shrine_ls[-1] = float('inf')
for i in range(n_temple):
	temple_ls[i] = int(input())
temple_ls[-1] = float('inf')
for i in range(Q):
	Q_ls[i] = int(input())
for q in Q_ls:
	i_Rshrine = return_min_ind_whose_value_not_less_than(shrine_ls, q)
	Rshrine = shrine_ls[i_Rshrine]
	i_Lshrine = i_Rshrine - 1
	Lshrine = shrine_ls[i_Lshrine]
	i_Rtemple = return_min_ind_whose_value_not_less_than(temple_ls, q)
	Rtemple = temple_ls[i_Rtemple]
	i_Ltemple = i_Rtemple - 1
	Ltemple = temple_ls[i_Ltemple]
	RR = max(Rtemple, Rshrine) - q
	if i_Lshrine >= 0 and i_Ltemple >= 0:
		LL = abs(min(Ltemple, Lshrine) - q)
	else:
		LL = float('inf')
	if i_Lshrine >= 0:
		RtLs = 2 * min(Rtemple - q, q - Lshrine) + max(Rtemple - q, q - Lshrine)
	else:
		RtLs = float('inf')
	if i_Ltemple >= 0:
		LtRs = 2 * min(Rshrine - q, q - Ltemple) + max(Rshrine - q, q - Ltemple)
	else:
		LtRs = float('inf')
	print(min(LL, RR, RtLs, LtRs))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
x = [int(input()) for i in range(Q)]
for e in x:
	(b, d) = (bisect.bisect_right(s, e), bisect.bisect_right(t, e))
	(a, c) = (b - 1, d - 1)
	ans = INF
	for S in [s[a], s[b]]:
		for T in [t[c], t[d]]:
			(d1, d2) = (abs(S - e) + abs(T - S), abs(T - e) + abs(S - T))
			ans = min(ans, d1, d2)
	print(ans)


import sys
input = sys.stdin.readline
A = list(map(int, input().split()))
(S, T, X) = [[int(input()) for i in range(A[j])] for j in range(3)]

def binary(a, l):
	low = 0
	high = len(l) - 1
	while high - low > 1:
		mid = (low + high) // 2
		if l[mid] < a:
			low = mid
		else:
			high = mid
	return [l[high], l[low]]
for x in X:
	s = set(binary(x, S))
	t = set(binary(x, T))
	Min = 20000000000
	for i in s:
		for j in t:
			Min = min(Min, min(abs(x - i), abs(x - j)) + abs(i - j))
	print(Min)


import bisect
INF = 10 ** 12
(A, B, Q) = list(map(int, input().split()))
(S, T, X) = ([-INF], [-INF], [])
for _ in range(A):
	S.append(int(input()))
for _ in range(B):
	T.append(int(input()))
for _ in range(Q):
	X.append(int(input()))
S.append(INF)
T.append(INF)
for x in X:
	ans = INF
	sr = bisect.bisect_right(S, x)
	tr = bisect.bisect_right(T, x)
	for s in S[sr - 1:sr + 1]:
		for t in T[tr - 1:tr + 1]:
			ans = min(ans, abs(s - x) + abs(s - t), abs(t - x) + abs(s - t))
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	i = bisect.bisect_left(S, x)
	j = bisect.bisect_left(T, x)
	ans = INF
	for s in [S[i - 1], S[i]]:
		for t in [T[j - 1], T[j]]:
			d1 = abs(s - x) + abs(s - t)
			d2 = abs(t - x) + abs(s - t)
			ans = min(ans, d1, d2)
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
x = [int(input()) for _ in range(q)]
for r in x:
	k = bisect.bisect(s, r)
	l = bisect.bisect(t, r)
	ans = 10 ** 11
	if k < a and l < b:
		ans = min(ans, max(s[k], t[l]) - r)
	if k > 0 and l > 0:
		ans = min(ans, r - min(s[k - 1], t[l - 1]))
	if k < a and l > 0:
		ans = min(ans, s[k] + r - t[l - 1] * 2, s[k] * 2 - r - t[l - 1])
	if k > 0 and l < b:
		ans = min(ans, t[l] + r - s[k - 1] * 2, t[l] * 2 - r - s[k - 1])
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
s = [-2 * 10 ** 10] + [int(input()) for _ in range(a)] + [2 * 10 ** 10]
t = [-2 * 10 ** 10] + [int(input()) for _ in range(b)] + [2 * 10 ** 10]
x = [int(input()) for _ in range(q)]
for i in range(q):
	j = bisect.bisect_left(s, x[i])
	al = x[i] - s[j - 1]
	ar = s[j] - x[i]
	j = bisect.bisect_left(t, x[i])
	bl = x[i] - t[j - 1]
	br = t[j] - x[i]
	l = min(max(al, bl), max(ar, br), 2 * al + br, 2 * ar + bl, 2 * bl + ar, 2 * br + al)
	print(l)


from bisect import bisect_right
(a, b, q) = map(int, input().split())
INF = 10 ** 11
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
for _ in range(q):
	x = int(input())
	si = bisect_right(s, x)
	ti = bisect_right(t, x)
	ans = INF * 10
	for ss in [s[si - 1], s[si]]:
		for tt in [t[ti - 1], t[ti]]:
			ans = min(ans, abs(x - ss) + abs(ss - tt), abs(x - tt) + abs(tt - ss))
	print(ans)


ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rr = lambda N: [ri() for _ in range(N)]
INF = 10 ** 18
(A, B, Q) = rl()
s = [-INF] + rr(A) + [INF]
t = [-INF] + rr(B) + [INF]
X = rr(Q)
import bisect
for x in X:
	s_i = bisect.bisect_left(s, x)
	t_i = bisect.bisect_left(t, x)
	s_l = x - s[s_i - 1]
	s_r = s[s_i] - x
	t_l = x - t[t_i - 1]
	t_r = t[t_i] - x
	ans = min(max(s_l, t_l), s_l + t_r + min(s_l, t_r), s_r + t_l + min(s_r, t_l), max(s_r, t_r))
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
s = [-999999999999] + [int(input()) for i in range(a)] + [999999999999]
t = [-999999999999] + [int(input()) for i in range(b)] + [999999999999]
x = [int(input()) for i in range(q)]
for i in range(q):
	a1 = s[bisect.bisect_left(s, x[i]) - 1]
	a2 = s[bisect.bisect_left(s, x[i])]
	b1 = t[bisect.bisect_left(t, x[i]) - 1]
	b2 = t[bisect.bisect_left(t, x[i])]
	l = []
	for j in [a1, a2]:
		for k in [b1, b2]:
			l.append(abs(j - k) + min(abs(x[i] - j), abs(x[i] - k)))
	print(min(l))


from bisect import bisect
(a, b, q) = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
xx = [int(input()) for _ in range(q)]
for x in xx:
	si = bisect(s, x)
	ti = bisect(t, x)
	(sf, sb) = (x - s[si - 1], s[si] - x)
	(tf, tb) = (x - t[ti - 1], t[ti] - x)
	print(min(2 * sf + tb, sf + tb * 2, 2 * sb + tf, sb + tf * 2, max(sf, tf), max(sb, tb)))


import sys
input = sys.stdin.readline
import bisect
(A, B, Q) = map(int, input().split())
S = [-10 ** 11] + [int(input()) for i in range(A)] + [10 ** 11]
T = [-10 ** 11] + [int(input()) for i in range(B)] + [10 ** 11]
for _ in range(Q):
	x = int(input())
	a = bisect.bisect_left(S, x)
	b = bisect.bisect_left(T, x)
	print(min(x - min(S[a - 1], T[b - 1]), max(S[a], T[b]) - x, x + T[b] - 2 * S[a - 1], x + S[a] - 2 * T[b - 1], 2 * T[b] - x - S[a - 1], 2 * S[a] - x - T[b - 1]))


(a, b, q) = map(int, input().split())
tl = []
qlist = []
for i in range(a):
	tl.append((int(input()), 0, i))
for i in range(b):
	tl.append((int(input()), 1, i))
for i in range(q):
	q1 = int(input())
	tl.append((q1, 2, i))
	qlist.append(q1)
tl.sort()
prea = [-1] * q
preb = [-1] * q
ta = tb = -1
for (i, c) in enumerate(tl):
	if c[1] == 0:
		ta = c[0]
	elif c[1] == 1:
		tb = c[0]
	elif c[1] == 2:
		(prea[c[2]], preb[c[2]]) = (ta, tb)
posta = [-1] * q
postb = [-1] * q
ra = rb = -1
for (i, c) in enumerate(tl[::-1]):
	if c[1] == 0:
		ra = c[0]
	elif c[1] == 1:
		rb = c[0]
	elif c[1] == 2:
		(posta[c[2]], postb[c[2]]) = (ra, rb)

def cal(pra, poa, prb, pob, q):
	gmin = 999999999999
	if pra != -1 and prb != -1:
		if pra < prb:
			gmin = min(gmin, q - pra)
		else:
			gmin = min(gmin, q - prb)
	if pra != -1 and pob != -1:
		gmin = min(gmin, pob - pra + min(q - pra, pob - q))
	if prb != -1 and poa != -1:
		gmin = min(gmin, poa - prb + min(poa - q, q - prb))
	if poa != -1 and pob != -1:
		if poa < pob:
			gmin = min(gmin, pob - q)
		else:
			gmin = min(gmin, poa - q)
	return gmin
for (a1, a2, b1, b2, q) in zip(prea, posta, preb, postb, qlist):
	print(cal(a1, a2, b1, b2, q))


from bisect import bisect_right

def main():
	(A, B, Q) = list(map(int, input().split()))
	s = [int(input()) for _ in range(A)]
	t = [int(input()) for _ in range(B)]
	s = [-10 ** 12] + s + [10 ** 12]
	t = [-10 ** 12] + t + [10 ** 12]
	for _ in range(Q):
		x = int(input())
		i = bisect_right(s, x)
		j = bisect_right(t, x)
		ans = []
		ans.append(max(abs(s[i - 1] - x), abs(t[j - 1] - x)))
		ans.append(max(abs(s[i] - x), abs(t[j] - x)))
		ans.append(abs(s[i - 1] - x) + abs(t[j] - x) + min(abs(s[i - 1] - x), abs(t[j] - x)))
		ans.append(abs(s[i] - x) + abs(t[j - 1] - x) + min(abs(s[i] - x), abs(t[j - 1] - x)))
		print(min(ans))

def __starting_point():
	main()
__starting_point()


import bisect
(a, b, q) = map(int, input().split())
shrines = [-10 ** 20] + [int(input()) for i in range(a)] + [10 ** 20]
temples = [-10 ** 20] + [int(input()) for i in range(b)] + [10 ** 20]
for i in range(q):
	x = int(input())
	s = bisect.bisect_left(shrines, x)
	t = bisect.bisect_left(temples, x)
	Dsleft = x - shrines[s - 1]
	Dtleft = x - temples[t - 1]
	Dsright = shrines[s] - x
	Dtright = temples[t] - x
	print(min(max(Dsleft, Dtleft), max(Dsright, Dtright), Dsleft + Dtright + min(Dsleft, Dtright), Dsright + Dtleft + min(Dsright, Dtleft)))


import bisect
inf = 10 ** 18
(a, b, q) = map(int, input().split())
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(b)] + [inf]
x = [int(input()) for _ in range(q)]
for i in x:
	a1 = s[bisect.bisect_right(s, i)]
	b1 = t[bisect.bisect_right(t, i)]
	a2 = s[bisect.bisect_right(s, i) - 1]
	b2 = t[bisect.bisect_right(t, i) - 1]
	ans = inf
	for j in [a1, a2]:
		for k in [b1, b2]:
			ans = min(ans, abs(i - j) + abs(j - k), abs(i - k) + abs(k - j))
	print(ans)


from bisect import bisect
(A, B, Q) = list(map(int, input().split()))
INF = 10 ** 18
S = [-INF]
T = [-INF]
for i in range(A):
	S.append(int(input()))
for i in range(B):
	T.append(int(input()))
S.append(INF)
T.append(INF)
for i in range(Q):
	x = int(input())
	sr = bisect(S, x)
	sl = sr - 1
	tr = bisect(T, x)
	tl = tr - 1

	def d(y):
		return abs(x - y)
	ans = min(d(max(S[sr], T[tr])), d(min(S[sl], T[tl])))
	ans = min(ans, 2 * d(S[sr]) + d(T[tl]))
	ans = min(ans, d(S[sr]) + 2 * d(T[tl]))
	ans = min(ans, 2 * d(S[sl]) + d(T[tr]))
	ans = min(ans, d(S[sl]) + 2 * d(T[tr]))
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
INF = 3 * 10 ** 10
S = [int(input()) for _ in range(a)]
S = [-INF] + S[:] + [INF]
T = [int(input()) for _ in range(b)]
T = [-INF] + T[:] + [INF]
for _ in range(q):
	x = int(input())
	i = bisect.bisect_left(S, x)
	j = bisect.bisect_left(T, x)
	A = [max(S[i] - x, T[j] - x), max(x - S[i - 1], x - T[j - 1]), S[i] - T[j - 1] + min(S[i] - x, x - T[j - 1]), T[j] - S[i - 1] + min(x - S[i - 1], T[j] - x)]
	print(min(A))


import bisect
inf = 10 ** 18
(a, b, q) = map(int, input().split())
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(b)] + [inf]
for _ in range(q):
	x = int(input())
	i = bisect.bisect_right(s, x)
	j = bisect.bisect_right(t, x)
	ans = inf
	for s_x in [s[i - 1], s[i]]:
		for t_x in [t[j - 1], t[j]]:
			ans0 = abs(s_x - x) + abs(t_x - s_x)
			ans1 = abs(t_x - x) + abs(s_x - t_x)
			ans = min(ans, ans0, ans1)
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
S = [-float('inf')] + [int(input()) for _ in range(A)] + [float('inf')]
T = [-float('inf')] + [int(input()) for _ in range(B)] + [float('inf')]
for _ in range(Q):
	answer = float('inf')
	x = int(input())
	si = bisect.bisect_left(S, x)
	ti = bisect.bisect_left(T, x)
	for s in [S[si - 1], S[si]]:
		for t in [T[ti - 1], T[ti]]:
			d = min(abs(s - x), abs(t - x)) + abs(t - s)
			answer = min(answer, d)
	print(answer)


import bisect
(A, B, Q) = [int(x) for x in input().split()]
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
x = [int(input()) for _ in range(Q)]
for q in range(Q):
	xx = x[q]
	(b, d) = (bisect.bisect_right(s, xx), bisect.bisect_right(t, xx))
	res = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(d1, d2) = (abs(S - xx) + abs(T - S), abs(T - xx) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


from bisect import bisect_left, bisect_right
(a, b, q) = map(int, input().split())
s = [-10 ** 21] + [int(input()) for _ in range(a)] + [10 ** 21]
t = [-10 ** 21] + [int(input()) for _ in range(b)] + [10 ** 21]
for _ in range(q):
	x = int(input())
	(s1, s2) = (s[bisect_right(s, x) - 1], s[bisect_right(s, x)])
	(t1, t2) = (t[bisect_right(t, x) - 1], t[bisect_right(t, x)])
	ans = 10 ** 11
	for i in [s1, s2]:
		for j in [t1, t2]:
			ans = min(ans, abs(i - j) + min(abs(x - i), abs(x - j)))
	print(ans)


from bisect import bisect_right
INF = 10 ** 18
(A, B, Q) = [int(x) for x in input().split()]
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
X = [int(input()) for _ in range(Q)]
for x in X:
	ans = INF
	b = bisect_right(S, x)
	d = bisect_right(T, x)
	dist = []
	for s in [S[b - 1], S[b]]:
		for t in [T[d - 1], T[d]]:
			ans = min(ans, abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))
	print(ans)


import bisect
(A, B, Q) = list(map(int, input().split()))
S = [-10 ** 20] + [int(input()) for i in range(A)] + [10 ** 20]
T = [-10 ** 20] + [int(input()) for i in range(B)] + [10 ** 20]
for i in range(Q):
	x = int(input())
	s = bisect.bisect_left(S, x)
	t = bisect.bisect_left(T, x)
	num = [x - min(S[s - 1], T[t - 1]), S[s] - T[t - 1] + min(S[s] - x, x - T[t - 1]), T[t] - S[s - 1] + min(x - S[s - 1], T[t] - x), max(S[s], T[t]) - x]
	print(min(num))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
shrine = [-INF] + [int(input()) for i in range(A)] + [INF]
temple = [-INF] + [int(input()) for i in range(B)] + [INF]
for i in range(Q):
	pos = int(input())
	(b, d) = (bisect.bisect_right(shrine, pos), bisect.bisect_right(temple, pos))
	res = INF
	for S in [shrine[b - 1], shrine[b]]:
		for T in [temple[d - 1], temple[d]]:
			(d1, d2) = (abs(S - pos) + abs(T - S), abs(T - pos) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]
X = [int(input()) for i in range(Q)]
for x in X:
	d = float('inf')
	i = bisect.bisect_right(S, x)
	j = bisect.bisect_right(T, x)
	for s in [S[i - 1], S[i]]:
		for t in [T[j - 1], T[j]]:
			d1 = abs(s - x) + abs(t - s)
			d2 = abs(t - x) + abs(t - s)
			d = min(d, d1, d2)
	print(d)


(A, B, Q) = list(map(int, input().split()))
S = [-int(100000000000.0)] + [int(input()) for _ in range(A)] + [int(100000000000.0)]
T = [-int(100000000000.0)] + [int(input()) for _ in range(B)] + [int(100000000000.0)]
X = [int(input()) for _ in range(Q)]
import bisect
for x in X:
	SL = S[bisect.bisect_left(S, x) - 1]
	SR = S[bisect.bisect_right(S, x)]
	TL = T[bisect.bisect_left(T, x) - 1]
	TR = T[bisect.bisect_right(T, x)]
	print(min(x - min(SL, TL), max(SR, TR) - x, TR + x - 2 * SL, SR + x - 2 * TL, 2 * SR - x - TL, 2 * TR - x - SL))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for y in range(A)] + [INF]
t = [-INF] + [int(input()) for z in range(B)] + [INF]
for i in range(Q):
	x = int(input())
	a = bisect.bisect_right(s, x)
	b = bisect.bisect_right(t, x)
	cost = INF
	for j in [s[a - 1], s[a]]:
		for k in [t[b - 1], t[b]]:
			(d1, d2) = (abs(j - x) + abs(j - k), abs(k - x) + abs(j - k))
			cost = min(cost, d1, d2)
	print(cost)


from bisect import bisect_left
(A, B, Q) = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
x = []
for s in S:
	idx = bisect_left(T, s)
	if idx == 0:
		x.append(abs(s - T[0]))
	elif idx == B:
		x.append(abs(s - T[-1]))
	else:
		x.append(min(abs(s - T[idx]), abs(s - T[idx - 1])))
y = []
for t in T:
	idx = bisect_left(S, t)
	if idx == 0:
		y.append(abs(t - S[0]))
	elif idx == A:
		y.append(abs(t - S[-1]))
	else:
		y.append(min(abs(t - S[idx]), abs(t - S[idx - 1])))
for _ in range(Q):
	X = int(input())
	sa = 0
	si = bisect_left(S, X)
	if si == 0:
		sa = x[0] + abs(S[0] - X)
	elif si == A:
		sa = x[-1] + abs(S[-1] - X)
	else:
		sa = min(x[si] + abs(S[si] - X), x[si - 1] + abs(S[si - 1] - X))
	ti = bisect_left(T, X)
	if ti == 0:
		ta = y[0] + abs(T[0] - X)
	elif ti == B:
		ta = y[-1] + abs(T[-1] - X)
	else:
		ta = min(y[ti] + abs(T[ti] - X), y[ti - 1] + abs(T[ti - 1] - X))
	print(min(sa, ta))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
Sa = [-INF] + [int(input()) for s in range(A)] + [INF]
Tb = [-INF] + [int(input()) for s in range(B)] + [INF]
Xq = [int(input()) for s in range(Q)]
for x in Xq:
	(b, d) = (bisect.bisect_right(Sa, x), bisect.bisect_right(Tb, x))
	res = INF
	for S in [Sa[b - 1], Sa[b]]:
		for T in [Tb[d - 1], Tb[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


from bisect import bisect_left, bisect_right
(a, b, q) = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
for _ in range(q):
	x = int(input())
	(b, c) = (bisect_right(s, x), bisect_right(t, x))
	a = INF
	for i in [s[b - 1], s[b]]:
		for j in [t[c - 1], t[c]]:
			a = min(a, abs(x - i) + abs(i - j), abs(x - j) + abs(j - i))
	print(a)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
q = [int(input()) for _ in range(Q)]

def solve(start):
	ans = []
	(c, d) = (bisect.bisect_left(s, start), bisect.bisect_left(t, start))
	for S in [s[c], s[c - 1]]:
		for T in [t[d], t[d - 1]]:
			ans.append(min(abs(start - S) + abs(S - T), abs(start - T) + abs(S - T)))
	print(min(ans))
for e in q:
	solve(e)


import bisect, sys
input = sys.stdin.readline
(a, b, Q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
for q in range(Q):
	x = int(input())
	(b, d) = (bisect.bisect_right(s, x), bisect.bisect_right(t, x))
	res = INF
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(T - S))
			res = min(res, d1, d2)
	print(res)


(a, b, q) = map(int, input().split())
x = [-10 ** 11] + [int(input()) for i in range(a)] + [10 ** 11]
y = [-10 ** 11] + [int(input()) for i in range(b)] + [10 ** 11]
p = [int(input()) for i in range(q)]
import bisect
ans = []
for i in p:
	xi = bisect.bisect_left(x, i)
	yi = bisect.bisect_left(y, i)
	a1 = max(x[xi], y[yi]) - i
	a2 = x[xi] - i + x[xi] - y[yi - 1]
	a3 = y[yi] - i + y[yi] - x[xi - 1]
	a4 = i - x[xi - 1] + y[yi] - x[xi - 1]
	a5 = i - y[yi - 1] + x[xi] - y[yi - 1]
	a6 = abs(min(x[xi - 1], y[yi - 1]) - i)
	ans.append(min(a1, a2, a3, a4, a5, a6))
for i in ans:
	print(i)


import bisect
(a, b, q) = map(int, input().split())
s = [int(input()) for _i in range(a)]
t = [int(input()) for _i in range(b)]
s.append(float('inf'))
s.insert(0, -float('inf'))
t.append(float('inf'))
t.insert(0, -float('inf'))
x = [int(input()) for _i in range(q)]
for i in x:
	n = bisect.bisect_right(s, i)
	m = bisect.bisect_right(t, i)
	print(min([abs(max([s[n], t[m]]) - i), min([abs(i - s[n - 1]), abs(i - t[m])]) + abs(s[n - 1] - t[m]), min([abs(i - s[n]), abs(i - t[m - 1])]) + abs(s[n] - t[m - 1]), abs(i - min(s[n - 1], t[m - 1]))]))


import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline())
ns = lambda : list(map(int, sys.stdin.readline().split()))
na = lambda : list(map(int, sys.stdin.readline().split()))
na1 = lambda : list([int(x) - 1 for x in sys.stdin.readline().split()])

def disLR(a, idx):
	small = a[max(0, idx - 1)]
	large = a[min(len(a) - 1, idx)]
	return (small, large)

def main():
	(a, b, q) = ns()
	s = [ni() for _ in range(a)]
	t = [ni() for _ in range(b)]
	for _ in range(q):
		ans = INF
		x = ni()
		tmpans = [0] * 4
		idxs = bisect.bisect_left(s, x)
		(sml, lrg) = disLR(s, idxs)
		tmpans[0] += abs(x - sml)
		tmpans[1] += abs(x - lrg)
		idxsml = bisect.bisect_left(t, sml)
		tmpans[0] += min([abs(sml - i) for i in disLR(t, idxsml)])
		idxlrg = bisect.bisect_left(t, lrg)
		tmpans[1] += min([abs(lrg - i) for i in disLR(t, idxlrg)])
		idxt = bisect.bisect_left(t, x)
		(sml, lrg) = disLR(t, idxt)
		tmpans[2] += abs(x - sml)
		tmpans[3] += abs(x - lrg)
		idxsml = bisect.bisect_left(s, sml)
		tmpans[2] += min([abs(sml - i) for i in disLR(s, idxsml)])
		idxlrg = bisect.bisect_left(s, lrg)
		tmpans[3] += min([abs(lrg - i) for i in disLR(s, idxlrg)])
		ans = min(ans, min(tmpans))
		print(ans)

def __starting_point():
	main()
__starting_point()


from bisect import bisect_left
(a, b, q) = list(map(int, input().split()))
dummy = 10 ** 18 + 1
s = [-dummy] + [int(input()) for _ in range(a)] + [dummy]
t = [-dummy] + [int(input()) for _ in range(b)] + [dummy]
x = [int(input()) for _ in range(q)]
for i in x:
	(idx_s, idx_t) = (bisect_left(s, i), bisect_left(t, i))
	ans = dummy
	for j in [s[idx_s - 1], s[idx_s]]:
		for k in [t[idx_t - 1], t[idx_t]]:
			(res1, res2) = (abs(j - i) + abs(k - j), abs(k - i) + abs(j - k))
			ans = min(ans, res1, res2)
	print(ans)


import bisect
(A, B, Q) = list(map(int, input().split()))
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
X = [int(input()) for i in range(Q)]
for x in X:
	s_min = bisect.bisect_left(s, x)
	t_min = bisect.bisect_left(t, x)
	cost = INF
	for S in [s[s_min - 1], s[s_min]]:
		for T in [t[t_min - 1], t[t_min]]:
			cost = min(cost, abs(T - S) + abs(T - x), abs(T - S) + abs(S - x))
	print(cost)


(A, B, Q) = map(int, input().split())
S = [-float('inf')] + [int(input()) for _ in range(A)] + [float('inf')]
T = [-float('inf')] + [int(input()) for _ in range(B)] + [float('inf')]
X = [int(input()) for _ in range(Q)]
from bisect import bisect_left
for x in X:
	(Si, Ti) = (bisect_left(S, x), bisect_left(T, x))
	(Sw, Se) = (abs(x - S[Si - 1]), abs(x - S[Si]))
	(Tw, Te) = (abs(x - T[Ti - 1]), abs(x - T[Ti]))
	print(min(max(Sw, Tw), max(Se, Te), 2 * min(Sw, Te) + max(Sw, Te), 2 * min(Tw, Se) + max(Tw, Se)))


import bisect
(A, B, Q) = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
S = sorted(S)
T = sorted(T)
for j in range(Q):
	x = int(input())
	a = S[bisect.bisect_left(S, x) - 1]
	b = T[bisect.bisect_left(T, x) - 1]
	if x <= S[-1]:
		a_ = S[bisect.bisect_right(S, x)]
	else:
		a_ = S[bisect.bisect_right(S, x) - 1]
	if x <= T[-1]:
		b_ = T[bisect.bisect_right(T, x)]
	else:
		b_ = T[bisect.bisect_right(T, x) - 1]
	y = min([abs(a - x) + abs(b - a), abs(a - x) + abs(b_ - a), abs(a_ - x) + abs(b - a_), abs(a_ - x) + abs(b_ - a_), abs(b - x) + abs(a - b), abs(b - x) + abs(a_ - b), abs(b_ - x) + abs(a - b_), abs(b_ - x) + abs(b_ - a_)])
	print(y)


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

def I():
	return int(input())

def F():
	return float(input())

def SS():
	return input()

def LI():
	return [int(x) for x in input().split()]

def LI_():
	return [int(x) - 1 for x in input().split()]

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

def LSS():
	return input().split()

def resolve():
	(A, B, Q) = LI()
	s = [I() for _ in range(A)]
	t = [I() for _ in range(B)]
	len_of_nearest_t = [INF] * A
	for i in range(A):
		idx = bisect.bisect_left(t, s[i])
		len_r = t[idx] - s[i] if 0 <= idx < B else INF
		len_l = s[i] - t[idx - 1] if 0 <= idx - 1 < B else INF
		len_of_nearest_t[i] = min(len_l, len_r)
	len_of_nearest_s = [INF] * B
	for i in range(B):
		idx = bisect.bisect_left(s, t[i])
		len_r = s[idx] - t[i] if 0 <= idx < A else INF
		len_l = t[i] - s[idx - 1] if 0 <= idx - 1 < A else INF
		len_of_nearest_s[i] = min(len_l, len_r)
	for _ in range(Q):
		x = I()
		idx = bisect.bisect_left(s, x)
		len_r = s[idx] - x + len_of_nearest_t[idx] if 0 <= idx < A else INF
		len_l = x - s[idx - 1] + len_of_nearest_t[idx - 1] if 0 <= idx - 1 < A else INF
		len_x_s_t = min(len_r, len_l)
		idx = bisect.bisect_left(t, x)
		len_r = t[idx] - x + len_of_nearest_s[idx] if 0 <= idx < B else INF
		len_l = x - t[idx - 1] + len_of_nearest_s[idx - 1] if 0 <= idx - 1 < B else INF
		len_x_t_s = min(len_r, len_l)
		print(min(len_x_s_t, len_x_t_s))

def __starting_point():
	resolve()
__starting_point()


import bisect as bs
(a, b, q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + sorted([int(input()) for _ in range(a)]) + [INF]
t = [-INF] + sorted([int(input()) for _ in range(b)]) + [INF]
for _ in range(q):
	z = int(input())
	(x, y) = (bs.bisect_right(s, z), bs.bisect_right(t, z))
	(s1, s2, t1, t2) = (s[x - 1], s[x], t[y - 1], t[y])
	print(min(max(z - s1, z - t1), max(s2 - z, t2 - z), min(2 * t2 - z - s1, 2 * s2 - z - t1), min(z + t2 - 2 * s1, z + s2 - 2 * t1)))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = [-INF]
for i in range(A):
	S.append(int(input()))
S.append(INF)
T = [-INF]
for i in range(B):
	T.append(int(input()))
T.append(INF)
X = []
for i in range(Q):
	X.append(int(input()))
for x in X:
	(a, b) = (bisect.bisect_right(S, x), bisect.bisect_right(T, x))
	print(min((abs(s - t) + min(abs(s - x), abs(t - x)) for s in S[a - 1:a + 1] for t in T[b - 1:b + 1])))


import bisect as bi
(a, b, q) = map(int, input().split())
INF = 10 ** 15
S = [-INF] + [int(input()) for i in range(a)] + [INF]
T = [-INF] + [int(input()) for i in range(b)] + [INF]
X = [int(input()) for i in range(q)]
for i in range(q):
	d = bi.bisect_right(S, X[i])
	e = bi.bisect_right(T, X[i])
	as0 = abs(X[i] - S[d - 1])
	as1 = abs(X[i] - S[d])
	at0 = abs(X[i] - T[e - 1])
	at1 = abs(X[i] - T[e])
	ans = min(max(as0, at0), max(as1, at1), 2 * as0 + at1, as0 + 2 * at1, 2 * at0 + as1, at0 + as1 * 2)
	print(ans)


from bisect import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 20
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
x = [int(input()) for _ in range(Q)]
for i in x:
	ans = INF
	idx_s = bisect(s, i)
	idx_t = bisect(t, i)
	for S in [s[idx_s - 1], s[idx_s]]:
		for T in [t[idx_t - 1], t[idx_t]]:
			d1 = abs(i - S) + abs(S - T)
			d2 = abs(i - T) + abs(S - T)
			ans = min(ans, d1, d2)
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
s = [-10 ** 11] + [int(input()) for i in range(a)] + [10 ** 11]
t = [-10 ** 11] + [int(input()) for i in range(b)] + [10 ** 11]
for i in range(q):
	x = int(input())
	xs = bisect.bisect_right(s, x)
	xt = bisect.bisect_right(t, x)
	ss = [x - s[xs - 1], s[xs] - x]
	tt = [x - t[xt - 1], t[xt] - x]
	print(min(max(ss[0], tt[0]), max(ss[1], tt[1]), ss[0] * 2 + tt[1], ss[1] * 2 + tt[0], tt[0] * 2 + ss[1], tt[1] * 2 + ss[0]))


import bisect
INF = 10 ** 18
(a, b, Q) = map(int, input().split())
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
for q in range(Q):
	x = int(input())
	s_idx = bisect.bisect_left(s, x)
	t_idx = bisect.bisect_left(t, x)
	ans = INF
	for i in [s_idx, s_idx - 1]:
		for j in [t_idx, t_idx - 1]:
			temp1 = abs(s[i] - x) + abs(s[i] - t[j])
			temp2 = abs(t[j] - x) + abs(s[i] - t[j])
			ans = min(ans, temp1, temp2)
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 12
S = [-INF]
for i in range(A):
	S.append(int(input()))
S.append(INF)
T = [-INF]
for i in range(B):
	T.append(int(input()))
T.append(INF)
for i in range(Q):
	x = int(input())
	sl = [S[bisect.bisect_left(S, x)], S[bisect.bisect_left(S, x) - 1]]
	tl = [T[bisect.bisect_left(T, x)], T[bisect.bisect_left(T, x) - 1]]
	ans = []
	for s in sl:
		for t in tl:
			ans.append(abs(x - s) + abs(s - t))
			ans.append(abs(x - t) + abs(t - s))
	print(min(ans))


import bisect
INF = 10 ** 18
(A, B, Q) = map(int, input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
s = [-INF] + s + [INF]
t = [-INF] + t + [INF]
for q in range(Q):
	X = int(input())
	t1 = bisect.bisect_right(t, X)
	s1 = bisect.bisect_right(s, X)
	res = INF
	for T in [t[t1], t[t1 - 1]]:
		for S in [s[s1], s[s1 - 1]]:
			ans1 = abs(X - T) + abs(S - T)
			ans2 = abs(X - S) + abs(S - T)
			res = min(res, ans1, ans2)
	print(res)


(a, b, q) = map(int, input().split())
s = sorted([int(input()) for i in range(a)])
t = sorted([int(input()) for i in range(b)])
from bisect import bisect
for i in range(q):
	x = int(input())
	s_i = bisect(s, x)
	d1 = d2 = 10 ** 11
	if s_i < a:
		d1 = s[s_i] - x
		t_i = bisect(t, s[s_i])
		buf = 10 ** 11
		if t_i < b:
			buf = t[t_i] - s[s_i]
		if t_i > 0:
			buf = min(buf, s[s_i] - t[t_i - 1])
		d1 += buf
	if s_i > 0:
		d2 = x - s[s_i - 1]
		t_i = bisect(t, s[s_i - 1])
		buf = 10 ** 11
		if t_i < b:
			buf = t[t_i] - s[s_i - 1]
		if t_i > 0:
			buf = min(buf, s[s_i - 1] - t[t_i - 1])
		d2 += buf
	t_i = bisect(t, x)
	d3 = d4 = 10 ** 11
	if t_i < b:
		d3 = t[t_i] - x
		s_i = bisect(s, t[t_i])
		buf = 10 ** 11
		if s_i < a:
			buf = s[s_i] - t[t_i]
		if s_i > 0:
			buf = min(buf, t[t_i] - s[s_i - 1])
		d3 += buf
	if t_i > 0:
		d4 = x - t[t_i - 1]
		s_i = bisect(s, t[t_i - 1])
		buf = 10 ** 11
		if s_i < a:
			buf = s[s_i] - t[t_i - 1]
		if s_i > 0:
			buf = min(buf, t[t_i - 1] - s[s_i - 1])
		d4 += buf
	print(min(d1, d2, d3, d4))


from bisect import bisect_left as bl
(A, B, Q) = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
	x = int(input())
	(b, d) = (bl(Sh, x), bl(Te, x))
	res = inf
	for S in [Sh[b - 1], Sh[b]]:
		for T in [Te[d - 1], Te[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


(A, B, Q) = map(int, input().split())
Jinja = []
Tera = []
Ichi = []
for i in range(0, A):
	Jinja.append(int(input()))
for i in range(0, B):
	Tera.append(int(input()))
for i in range(0, Q):
	Ichi.append(int(input()))
import math
import bisect as bi
for i in range(0, Q):
	temp = Ichi[i]
	J = math.inf
	T = math.inf
	JL = bi.bisect_left(Jinja, temp)
	JR = bi.bisect_right(Jinja, temp)
	TL = bi.bisect_left(Tera, temp)
	TR = bi.bisect_right(Tera, temp)
	if JL != JR:
		J = 0
	if TL != TR:
		T = 0
	if JR == 0:
		JW = math.inf
		JE = abs(temp - Jinja[0])
	elif JL == A:
		JW = abs(temp - Jinja[A - 1])
		JE = math.inf
	else:
		JW = abs(temp - Jinja[JL - 1])
		JE = abs(temp - Jinja[JL])
	if TR == 0:
		TW = math.inf
		TE = abs(temp - Tera[0])
	elif TL == B:
		TW = abs(temp - Tera[B - 1])
		TE = math.inf
	else:
		TW = abs(temp - Tera[TL - 1])
		TE = abs(temp - Tera[TL])
	print(min(J + T, J + TW, J + TE, T + JW, T + JE, max(JW, TW), max(JE, TE), 2 * JW + TE, 2 * TE + JW, 2 * JE + TW, 2 * TW + JE))


(A, B, Q) = map(int, input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
X = [int(input()) for i in range(Q)]
s.append(2 * 10 ** 10 + 1)
t.append(2 * 10 ** 10 + 1)
import bisect
for i in range(Q):
	a = bisect.bisect_left(s, X[i])
	b = bisect.bisect_left(t, X[i])
	(s1, s2) = (s[a - 1], s[a])
	(t1, t2) = (t[b - 1], t[b])
	ans = 10 ** 10 * 2
	for k in [s[a], s[a - 1]]:
		for j in [t[b], t[b - 1]]:
			d = abs(k - j) + min(abs(k - X[i]), abs(j - X[i]))
			ans = min(ans, d)
	print(ans)


import bisect
from itertools import product
I = lambda n: [int(input()) for _ in range(n)]

def f(S, x):
	p = bisect.bisect_left(S, x)
	return [S[p - 1], S[p]]
g = lambda x, s, t: abs(x - s) + abs(s - t)
INF = 10 ** 11
(a, b, q) = map(int, input().split())
(S, T, X) = ([-INF] + I(a) + [INF], [-INF] + I(b) + [INF], I(q))
for x in X:
	min_ = INF
	for (s, t) in product(f(S, x), f(T, x)):
		min_ = min(min_, g(x, s, t), g(x, t, s))
	print(min_)


import bisect
(a, b, q) = map(int, input().split())
ss = [int(input()) for _ in range(a)]
ts = [int(input()) for _ in range(b)]
xs = [int(input()) for _ in range(q)]
ss = [-float('inf')] + ss + [float('inf')]
ts = [-float('inf')] + ts + [float('inf')]
for x in xs:
	s_r = bisect.bisect_left(ss, x)
	t_r = bisect.bisect_left(ts, x)
	s_l = s_r - 1
	t_l = t_r - 1
	res = float('inf')
	if ss[s_l] > ts[t_l]:
		res = min(res, x - ts[t_l], (x - ss[s_l]) * 2 + (ts[t_r] - x))
	elif ss[s_l] < ts[t_l]:
		res = min(res, x - ss[s_l], (x - ts[t_l]) * 2 + (ss[s_r] - x))
	else:
		res = min(res, (x - ss[s_l]) * 2 + (ss[s_r] - x), x - ss[s_l] * 2 + (ts[t_r] - x))
	if ss[s_r] > ts[t_r]:
		res = min(res, ss[s_r] - x, (ts[t_r] - x) * 2 + (x - ss[s_l]))
	elif ss[s_r] < ts[t_r]:
		res = min(res, ts[t_r] - x, (ss[s_r] - x) * 2 + (x - ts[t_l]))
	else:
		res = min(res, (ss[s_r] - x) * 2 + (x - ss[s_l]), (ss[s_r] - x) * 2 + (x - ts[t_l]))
	print(res)


import bisect
(A, B, Q) = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]
S = [-10 ** 12] + S + [10 ** 12]
T = [-10 ** 12] + T + [10 ** 12]
for x in X:
	si = bisect.bisect_right(S, x)
	ti = bisect.bisect_right(T, x)
	(Ls, Rs) = (x - S[si - 1], -x + S[si])
	(Lt, Rt) = (x - T[ti - 1], -x + T[ti])
	ans = min(Ls * 2 + Rt, Ls + Rt * 2, Lt * 2 + Rs, Lt + Rs * 2, max(Ls, Lt), max(Rs, Rt))
	print(ans)


from bisect import bisect_left
(a, b, q) = list(map(int, input().split()))
INF = 10 ** 15
s = [-INF] + sorted([int(input()) for _ in range(a)]) + [INF]
t = [-INF] + sorted([int(input()) for _ in range(b)]) + [INF]
for i in range(q):
	pro = int(input())
	s_index = bisect_left(s, pro)
	t_index = bisect_left(t, pro)
	(s1, s2) = (s[s_index - 1], s[s_index])
	(t1, t2) = (t[t_index - 1], t[t_index])
	minimum = []
	kouho1 = abs(pro - s1) + abs(s1 - t1)
	minimum.append(kouho1)
	kouho2 = abs(pro - s1) + abs(s1 - t2)
	minimum.append(kouho2)
	kouho3 = abs(pro - t1) + abs(t1 - s1)
	minimum.append(kouho3)
	kouho4 = abs(pro - t2) + abs(t2 - s1)
	minimum.append(kouho4)
	kouho5 = abs(pro - s2) + abs(s2 - t1)
	minimum.append(kouho5)
	kouho6 = abs(pro - s2) + abs(s2 - t2)
	minimum.append(kouho6)
	kouho7 = abs(pro - t1) + abs(s2 - t1)
	minimum.append(kouho7)
	kouho8 = abs(pro - t2) + abs(s2 - t2)
	minimum.append(kouho8)
	print(min(minimum))


from bisect import bisect
(a, b, q) = map(int, input().split())
s = [-10 ** 11] + [int(input()) for _ in range(a)] + [10 ** 11]
t = [-10 ** 11] + [int(input()) for _ in range(b)] + [10 ** 11]
x = [int(input()) for _ in range(q)]
for xi in x:
	(p, q) = (bisect(s, xi), bisect(t, xi))
	(sl, sr) = (abs(s[p - 1] - xi), abs(s[p] - xi))
	(tl, tr) = (abs(t[q - 1] - xi), abs(t[q] - xi))
	ans = min(max(sl, tl), sl + tr + min(sl, tr), max(sr, tr), sr + tl + min(sr, tl))
	print(ans)


import bisect
INF = 10 ** 18

def solve(s, t, x):
	ans = [0] * len(x)
	for i in range(len(x)):
		b = bisect.bisect_right(s, x[i])
		d = bisect.bisect_right(t, x[i])
		res = INF
		for S in [s[b - 1], s[b]]:
			for T in [t[d - 1], t[d]]:
				res = min(res, abs(S - x[i]) + abs(T - S), abs(T - x[i]) + abs(T - S))
		ans[i] = res
	return ans

def main():
	(A, B, Q) = list(map(int, input().split()))
	s = [-10 ** 18] + [int(input()) for _ in range(A)] + [10 ** 18]
	t = [-10 ** 18] + [int(input()) for _ in range(B)] + [10 ** 18]
	x = [int(input()) for _ in range(Q)]
	ans = solve(s, t, x)
	for i in range(Q):
		print(ans[i])
	return

def __starting_point():
	main()
__starting_point()


from bisect import bisect_left as bl

def I(a):
	return [-1 << 50] + [int(input()) for i in range(a)] + [1 << 50]
(A, B, Q) = map(int, input().split())
(S, T) = (I(A), I(B))
for _ in range(Q):
	x = int(input())
	(k, l) = (bl(S, x), bl(T, x))
	(a, b, c, d) = (x - S[k - 1], x - T[l - 1], S[k] - x, T[l] - x)
	print(min(max(a, b), max(c, d), 2 * a + d, 2 * d + a, 2 * b + c, 2 * c + b))


from bisect import bisect_left
(a, b, q) = map(int, input().split())
INF = 10 ** 12
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
for _ in range(q):
	x = int(input())
	k = bisect_left(s, x)
	l = bisect_left(t, x)
	sm = x - s[k - 1]
	sM = s[k] - x
	tm = x - t[l - 1]
	tM = t[l] - x
	ans = min(2 * sm + tM, 2 * sM + tm, 2 * tm + sM, 2 * tM + sm, max(sm, tm), max(sM, tM))
	print(ans)


(a, b, q) = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
import bisect
for _ in range(q):
	x = int(input())
	sb = bisect.bisect_left(s, x)
	tb = bisect.bisect_left(t, x)
	ss = [x - s[max(sb - 1, 0)], x - s[min(sb, a - 1)]]
	tt = [x - t[max(tb - 1, 0)], x - t[min(tb, b - 1)]]
	l = []
	for i in ss:
		for j in tt:
			if i * j >= 0:
				l.append(max(abs(i), abs(j)))
			else:
				l.append(max(abs(i), abs(j)) + 2 * min(abs(i), abs(j)))
	print(min(l))


from bisect import bisect_left, bisect_right
(A, B, Q) = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
for _ in range(Q):
	x = int(input())
	a = bisect_right(s, x)
	b = bisect_right(t, x)
	la = x - s[a - 1] if a > 0 else None
	ra = s[a] - x if a < A else None
	lb = x - t[b - 1] if b > 0 else None
	rb = t[b] - x if b < B else None
	ans = float('inf')
	if la and lb:
		ans = min(ans, max(la, lb))
	if la and rb:
		ans = min(ans, la + 2 * rb, 2 * la + rb)
	if ra and lb:
		ans = min(ans, ra + 2 * lb, 2 * ra + lb)
	if ra and rb:
		ans = min(ans, max(ra, rb))
	print(ans)


(A, B, Q) = map(int, input().split())
INF = 10 ** 15
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
from bisect import bisect_left
for _ in range(Q):
	x = int(input())
	s = bisect_left(S, x)
	t = bisect_left(T, x)
	sr = abs(S[s] - x)
	sl = abs(S[s - 1] - x)
	tr = abs(T[t] - x)
	tl = abs(T[t - 1] - x)
	Ans = min(max(sr, tr), max(sl, tl), 2 * sr + tl, sr + 2 * tl, 2 * tr + sl, tr + 2 * sl)
	print(Ans)


import bisect
(A, B, Q) = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	ans = INF
	a_i = bisect.bisect_right(s, x)
	b_i = bisect.bisect_right(t, x)
	for a in [s[a_i], s[a_i - 1]]:
		for b in [t[b_i], t[b_i - 1]]:
			ans = min(ans, abs(x - a) + abs(b - a), abs(x - b) + abs(b - a))
	print(ans)


from bisect import *
(A, B, Q) = map(int, input().split())
inf = float('inf')
S = [-inf] + [int(input()) for a in range(A)] + [inf]
T = [-inf] + [int(input()) for b in range(B)] + [inf]
X = [int(input()) for q in range(Q)]
for x in X:
	si = bisect(S, x)
	ti = bisect(T, x)
	d1 = x - S[si - 1]
	d2 = S[si] - x
	d3 = x - T[ti - 1]
	d4 = T[ti] - x
	print(min(2 * d1 + d4, d1 + d4 * 2, 2 * d2 + d3, d2 + d3 * 2, max(d1, d3), max(d2, d4)))


(A, B, Q) = map(int, input().split())
import bisect
s = []
t = []
for _ in range(A):
	s.append(int(input()))
for _ in range(B):
	t.append(int(input()))
s = [-10 ** 20] + s + [10 ** 20]
t = [-10 ** 20] + t + [10 ** 20]
for q in range(Q):
	x = int(input())
	(b, d) = (bisect.bisect_right(s, x), bisect.bisect_right(t, x))
	res = 10 ** 20
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
X = [int(input()) for _ in range(Q)]
from bisect import bisect_left
for _ in range(Q):
	ans = INF
	x = X[_]
	idxs = bisect_left(S, x)
	idxt = bisect_left(T, x)
	for s in [S[idxs - 1], S[idxs]]:
		for t in [T[idxt - 1], T[idxt]]:
			(d1, d2) = (abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
			ans = min(ans, d1, d2)
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
INF = 10 ** 18
s = [int(input()) for i in range(a)]
s = [-INF] + s + [INF]
t = [int(input()) for i in range(b)]
t = [-INF] + t + [INF]
s.sort()
t.sort()
for i in range(q):
	ans = 10 ** 11
	x = int(input())
	c = bisect.bisect_left(s, x)
	(e1, e2) = (s[c - 1], s[c])
	f = bisect.bisect_left(t, e1)
	(f1, f2) = (t[f - 1], t[f])
	ans = min(ans, abs(x - e1) + abs(e1 - f1), abs(x - e1) + abs(e1 - f2))
	f = bisect.bisect_left(t, e2)
	(f1, f2) = (t[f - 1], t[f])
	ans = min(ans, abs(x - e2) + abs(e2 - f1), abs(x - e2) + abs(e2 - f2))
	d = bisect.bisect_left(t, x)
	(f1, f2) = (t[d - 1], t[d])
	e = bisect.bisect_left(s, f1)
	(e1, e2) = (s[e - 1], s[e])
	ans = min(ans, abs(x - f1) + abs(f1 - e1), abs(x - f1) + abs(f1 - e2))
	e = bisect.bisect_left(s, f2)
	(e1, e2) = (s[e - 1], s[e])
	ans = min(ans, abs(x - f2) + abs(f2 - e1), abs(x - f2) + abs(f2 - e2))
	print(ans)


from bisect import bisect_left
(A, B, Q) = map(int, input().split())
INF = 10 ** 12
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for _ in range(Q):
	x = int(input())
	si = bisect_left(s, x)
	ti = bisect_left(t, x)
	sl = [s[si - 1], s[si]]
	tl = [t[ti - 1], t[ti]]
	ans = INF
	for i in range(2):
		for j in range(2):
			ans = min(ans, abs(x - sl[i]) + abs(sl[i] - tl[j]), abs(x - tl[j]) + abs(tl[j] - sl[i]))
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
inf = 10 ** 18
S = [-inf] + [int(input()) for _ in range(A)] + [inf]
T = [-inf] + [int(input()) for _ in range(B)] + [inf]
for q in range(Q):
	x = int(input())
	s_h = bisect.bisect(S, x)
	s_l = s_h - 1
	t_h = bisect.bisect(T, x)
	t_l = t_h - 1
	ans = inf
	for s in [S[s_l], S[s_h]]:
		for t in [T[t_l], T[t_h]]:
			d1 = abs(s - x) + abs(t - s)
			d2 = abs(t - x) + abs(s - t)
			ans = min(ans, d1, d2)
	print(ans)


from bisect import bisect
(a, b, q) = map(int, input().split())
s = [-10 ** 18] + [int(input()) for i in range(a)] + [10 ** 18]
t = [-10 ** 18] + [int(input()) for i in range(b)] + [10 ** 18]
for i in range(q):
	x = int(input())
	s_inx = bisect(s, x)
	t_inx = bisect(t, x)
	s_l = s[s_inx - 1]
	s_r = s[s_inx]
	t_l = t[t_inx - 1]
	t_r = t[t_inx]
	ans = 10 ** 18
	for i in [s_l, s_r]:
		for j in [t_l, t_r]:
			d1 = abs(i - x) + abs(j - i)
			d2 = abs(j - x) + abs(i - j)
			ans = min(ans, d1, d2)
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
inf = 10 ** 18
lisS = [-inf] + [int(input()) for x in range(A)] + [inf]
lisT = [-inf] + [int(input()) for x in range(B)] + [inf]
for _ in range(Q):
	x = int(input())
	i = bisect.bisect_right(lisS, x)
	j = bisect.bisect_right(lisT, x)
	res = inf
	for S in [lisS[i - 1], lisS[i]]:
		for T in [lisT[j - 1], lisT[j]]:
			r1 = abs(S - x) + abs(S - T)
			r2 = abs(T - x) + abs(S - T)
			res = min(res, r1, r2)
	print(res)


import bisect
(A, B, Q) = map(int, input().split())
inf = 10 ** 18
s = [-inf] + [int(input()) for i in range(A)] + [inf]
t = [-inf] + [int(input()) for i in range(B)] + [inf]
for i in range(Q):
	ans = inf
	x = int(input())
	b = bisect.bisect_left(s, x)
	d = bisect.bisect_left(t, x)
	for S in (s[b - 1], s[b]):
		for T in (t[d - 1], t[d]):
			ans = min(ans, abs(S - x) + abs(S - T), abs(T - x) + abs(S - T))
	print(ans)


import bisect
INF = 10 ** 100
(A, B, Q, *stx) = open(0).read().split()
(A, B, Q) = map(int, [A, B, Q])
slist = [-INF] + [int(stx[i]) for i in range(A)] + [INF]
tlist = [-INF] + [int(stx[i]) for i in range(A, A + B)] + [INF]
xlist = [int(stx[i]) for i in range(A + B, A + B + Q)]
oslist = [0] * 2
otlist = [0] * 2
sr_index = 0
tr_index = 0
for i in xlist:
	ans = INF
	sr_index = bisect.bisect_left(slist, i)
	oslist = [slist[sr_index - 1], slist[sr_index]]
	tr_index = bisect.bisect_left(tlist, i)
	otlist = [tlist[tr_index - 1], tlist[tr_index]]
	for j in oslist:
		for k in otlist:
			ans = min(ans, abs(j - i) + abs(k - j), abs(k - i) + abs(j - k))
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for i in range(Q):
	x = int(input())
	b = bisect.bisect_right(s, x)
	d = bisect.bisect_right(t, x)
	dist = INF
	for sb in [s[b - 1], s[b]]:
		for td in [t[d - 1], t[d]]:
			(d1, d2) = (abs(x - sb) + abs(sb - td), abs(x - td) + abs(td - sb))
			dist = min(dist, d1, d2)
	print(dist)


import bisect
(A, B, Q) = map(int, input().split())
s = [-10 ** 20] + [int(input()) for k in range(A)] + [10 ** 20, 10 ** 20]
t = [-10 ** 20] + [int(input()) for k in range(B)] + [10 ** 20, 10 ** 20]
for _ in range(Q):
	x = int(input())
	S = bisect.bisect_left(s, x)
	T = bisect.bisect_left(t, x)
	print(min(max(s[S], t[T]) - x, x - min(s[S - 1], t[T - 1]), s[S] - x + s[S] - t[T - 1], t[T] - x + t[T] - s[S - 1], x - s[S - 1] + t[T] - s[S - 1], x - t[T - 1] + s[S] - t[T - 1]))


(a, b, q) = list(map(int, input().split()))
(*s,) = [-10 ** 11] + [int(input()) for _ in range(a)] + [10 ** 11]
(*t,) = [-10 ** 11] + [int(input()) for _ in range(b)] + [10 ** 11]
(*x,) = [int(input()) for _ in range(q)]
s.sort()
t.sort()
from bisect import bisect_left
from itertools import product
for xi in x:
	ans = 10 ** 11
	i = bisect_left(s, xi)
	j = bisect_left(t, xi)
	for (u, v) in product(s[i - 1:i + 1], t[j - 1:j + 1]):
		d = min(abs(u - xi), abs(v - xi)) + abs(u - v)
		ans = min(d, ans)
	print(ans)


from bisect import bisect_left
(A, B, Q) = list(map(int, input().split()))
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
s.sort()
t.sort()
for k in range(Q):
	x = int(input())
	ans = []
	ms = bisect_left(s, x)
	for i in [-1, 0]:
		if 0 <= ms + i <= A - 1:
			mt = bisect_left(t, s[ms + i])
			for j in [-1, 0]:
				if 0 <= mt + j <= B - 1:
					ans.append(abs(s[ms + i] - x) + abs(t[mt + j] - s[ms + i]))
	mt = bisect_left(t, x)
	for i in [-1, 0]:
		if 0 <= mt + i <= B - 1:
			ms = bisect_left(s, t[mt + i])
			for j in [-1, 0]:
				if 0 <= ms + j <= A - 1:
					ans.append(abs(t[mt + i] - x) + abs(s[ms + j] - t[mt + i]))
	print(min(ans))


import bisect
(a, b, q) = list(map(int, input().split()))
INF = 10 ** 18
shrines = [-INF] + [int(input()) for _ in range(a)]
temples = [-INF] + [int(input()) for _ in range(b)]
for _ in range(q):
	x = int(input())
	ans = INF
	Ri = bisect.bisect_left(shrines, x)
	Li = Ri - 1
	if Ri != a + 1:
		R = shrines[Ri]
		Rj = bisect.bisect_left(temples, R)
		Lj = Rj - 1
		d = INF
		if Rj != b + 1:
			d = min(d, temples[Rj] - x)
		if Lj != 0:
			d = min(d, R - temples[Lj] + R - x)
		if d < ans:
			ans = d
	if Li != 0:
		L = shrines[Li]
		Rj = bisect.bisect_left(temples, L)
		Lj = Rj - 1
		d = INF
		if Rj != b + 1:
			d = min(d, temples[Rj] - L + x - L)
		if Lj != 0:
			d = min(d, x - temples[Lj])
		if d < ans:
			ans = d
	Ri = bisect.bisect_left(temples, x)
	Li = Ri - 1
	if Ri != b + 1:
		R = temples[Ri]
		Rj = bisect.bisect_left(shrines, R)
		Lj = Rj - 1
		d = INF
		if Rj != a + 1:
			d = min(d, shrines[Rj] - x)
		if Lj != 0:
			d = min(d, R - shrines[Lj] + R - x)
		if d < ans:
			ans = d
	if Li != 0:
		L = temples[Li]
		Rj = bisect.bisect_left(shrines, L)
		Lj = Rj - 1
		d = INF
		if Rj != a + 1:
			d = min(d, shrines[Rj] - L + x - L)
		if Lj != 0:
			d = min(d, x - shrines[Lj])
		if d < ans:
			ans = d
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
inf = 10 ** 18
S = [-inf] + [int(input()) for _ in range(a)] + [inf]
T = [-inf] + [int(input()) for _ in range(b)] + [inf]
ans = inf
for _ in range(q):
	x = int(input())
	(b, d) = (bisect.bisect_right(S, x), bisect.bisect_right(T, x))
	ans = inf
	for s in [S[b - 1], S[b]]:
		for t in [T[d - 1], T[d]]:
			(tmp1, tmp2) = (abs(s - x) + abs(s - t), abs(t - x) + abs(t - s))
			ans = min(ans, tmp1, tmp2)
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
ff = float('inf')
s = [-ff]
t = [-ff]
for i in range(a):
	v = int(input())
	s.append(v)
for i in range(b):
	w = int(input())
	t.append(w)
s.append(ff)
t.append(ff)
s.sort()
t.sort()
for i in range(q):
	x = int(input())
	a = bisect.bisect_right(s, x)
	b = bisect.bisect_right(t, x)
	res = ff
	for y in [s[a], s[a - 1]]:
		for w in [t[b], t[b - 1]]:
			(d1, d2) = (abs(y - x) + abs(w - y), abs(w - x) + abs(w - y))
			res = min(res, d1, d2)
	print(res)


import sys
input = sys.stdin.readline
from bisect import bisect
inf = float('inf')
(A, B, Q) = map(int, input().split())
S = [-inf] + [int(input()) for _ in range(A)] + [inf]
T = [-inf] + [int(input()) for _ in range(B)] + [inf]
X = [int(input()) for _ in range(Q)]
for x in X:
	ans = inf
	si = bisect(S, x)
	ti = bisect(T, x)
	for s in [S[si - 1], S[si]]:
		for t in [T[ti - 1], T[ti]]:
			res = abs(s - t) + min(abs(x - s), abs(x - t))
			ans = min(ans, res)
	print(ans)


import bisect
INF = 2 ** 60
(A, B, Q) = map(int, input().split())
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for q in range(Q):
	x = int(input())
	(b, d) = (bisect.bisect_right(s, x), bisect.bisect_right(t, x))
	(a, c) = (b - 1, d - 1)
	res = INF
	for S in s[a:b + 1]:
		for T in t[c:d + 1]:
			d1 = abs(S - x) + abs(T - S)
			d2 = abs(T - x) + abs(T - S)
			res = min(res, d1, d2)
	print(res)


import bisect, sys
input = sys.stdin.readline
(a, b, q) = map(int, input().split())
inf = 10 ** 18
s = [-inf] + [int(input()) for i in range(a)] + [inf]
t = [-inf] + [int(input()) for i in range(b)] + [inf]
for i in range(q):
	x = int(input())
	i = bisect.bisect_right(s, x)
	j = bisect.bisect_right(t, x)
	print(min(max(s[i], t[j]) - x, x - min(s[i - 1], t[j - 1]), min(s[i] - x, x - t[j - 1]) + s[i] - t[j - 1], min(t[j] - x, x - s[i - 1]) + t[j] - s[i - 1]))


import bisect
(a, b, q) = list(map(int, input().split()))
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
s.append(-100000000000)
s.append(100000000000)
t.append(-100000000000)
t.append(100000000000)
s.sort()
t.sort()
for _ in range(q):
	x = int(input())
	idxs = bisect.bisect_right(s, x)
	idxt = bisect.bisect_right(t, x)
	left_s = s[idxs - 1]
	right_s = s[idxs]
	left_t = t[idxt - 1]
	right_t = t[idxt]
	kouho1 = max(x - left_s, x - left_t)
	kouho2 = max(right_s - x, right_t - x)
	kouho3 = min(x - left_s, right_t - x) * 2 + max(x - left_s, right_t - x)
	kouho4 = min(x - left_t, right_s - x) * 2 + max(x - left_t, right_s - x)
	print(min(kouho1, kouho2, kouho3, kouho4))


import bisect
(A, B, Q) = map(int, input().split())
(S, T) = ([-10 ** 18], [-10 ** 18])
for i in range(A):
	S.append(int(input()))
S.append(10 ** 18)
for i in range(B):
	T.append(int(input()))
T.append(10 ** 18)
for q in range(Q):
	x = int(input())
	res = 10 ** 18
	s_idx = bisect.bisect_right(S, x)
	t_idx = bisect.bisect_right(T, x)
	for s in [S[s_idx - 1], S[s_idx]]:
		for t in [T[t_idx - 1], T[t_idx]]:
			(d1, d2) = (abs(s - x) + abs(t - s), abs(t - x) + abs(s - t))
			res = min(res, d1, d2)
	print(res)


import bisect
(A, B, Q) = list(map(int, input().split()))
S = list((int(input()) for _ in range(A)))
T = list((int(input()) for _ in range(B)))
for _ in range(Q):
	x = int(input())
	s = bisect.bisect(S, x)
	t = bisect.bisect(T, x)
	ans = [10 ** 10 * 2] * 4
	if s < A:
		ans[0] = abs(S[s] - x) + min(abs(S[s] - T[max(bisect.bisect(T, S[s]) - 1, 0)]), abs(S[s] - T[min(bisect.bisect(T, S[s]), B - 1)]))
	if s > 0:
		s -= 1
		ans[1] = abs(S[s] - x) + min(abs(S[s] - T[max(bisect.bisect(T, S[s]) - 1, 0)]), abs(S[s] - T[min(bisect.bisect(T, S[s]), B - 1)]))
	if t < B:
		ans[2] = abs(T[t] - x) + min(abs(T[t] - S[max(bisect.bisect(S, T[t]) - 1, 0)]), abs(T[t] - S[min(bisect.bisect(S, T[t]), A - 1)]))
	if t > 0:
		t -= 1
		ans[3] = abs(T[t] - x) + min(abs(T[t] - S[max(bisect.bisect(S, T[t]) - 1, 0)]), abs(T[t] - S[min(bisect.bisect(S, T[t]), A - 1)]))
	print(min(ans))


import bisect
INF = 1e+20
(A, B, Q) = map(int, input().split())
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
X = [int(input()) for _ in range(Q)]
for x in X:
	ans = INF
	(iS, iT) = (bisect.bisect_left(S, x), bisect.bisect_left(T, x))
	for s in [S[iS - 1], S[iS]]:
		for t in [T[iT - 1], T[iT]]:
			(a1, a2) = (abs(s - x) + abs(s - t), abs(t - x) + abs(t - s))
			ans = min(ans, a1, a2)
	print(ans)


(A, B, Q) = map(int, input().split())
inf = 1e+50
s = [-inf]
for _ in range(A):
	s.append(int(input()))
s.append(inf)
t = [-inf]
for _ in range(B):
	t.append(int(input()))
t.append(inf)
from bisect import bisect
for _ in range(Q):
	x = int(input())
	i = bisect(s, x)
	j = bisect(t, x)
	sL = x - s[i - 1]
	sR = s[i] - x
	tL = x - t[j - 1]
	tR = t[j] - x
	print(min(max(sR, tR), max(sL, tL), sR + tL + min(sR, tL), sL + tR + min(sL, tR)))


import bisect
(a, b, q) = map(int, input().split())
s = [int(input()) for i in range(a)]
s.sort()
t = [int(input()) for i in range(b)]
t.sort()

def minl(x):
	idx = bisect.bisect_left(s, x)
	ret = 3 * 10 ** 10
	if idx == 0:
		first = s[0] - x
		now = s[0]
		idx = bisect.bisect_left(t, now)
		if idx == 0:
			second = t[0] - now
		elif idx == b:
			second = now - t[b - 1]
		else:
			second = min(now - t[idx - 1], t[idx] - now)
		ret = min(ret, first + second)
	elif idx == a:
		first = x - s[a - 1]
		now = s[a - 1]
		idx = bisect.bisect_left(t, now)
		if idx == 0:
			second = t[0] - now
		elif idx == b:
			second = now - t[b - 1]
		else:
			second = min(now - t[idx - 1], t[idx] - now)
		ret = min(ret, first + second)
	else:
		for first_g in [s[idx - 1], s[idx]]:
			first = abs(first_g - x)
			now = first_g
			idx = bisect.bisect_left(t, now)
			if idx == 0:
				second = t[0] - now
			elif idx == b:
				second = now - t[b - 1]
			else:
				second = min(now - t[idx - 1], t[idx] - now)
			ret = min(ret, first + second)
	idx = bisect.bisect_left(t, x)
	if idx == 0:
		first = t[0] - x
		now = t[0]
		idx = bisect.bisect_left(s, now)
		if idx == 0:
			second = s[0] - now
		elif idx == a:
			second = now - s[a - 1]
		else:
			second = min(now - s[idx - 1], s[idx] - now)
		ret = min(ret, first + second)
	elif idx == b:
		first = x - t[b - 1]
		now = t[b - 1]
		idx = bisect.bisect_left(s, now)
		if idx == 0:
			second = s[0] - now
		elif idx == a:
			second = now - s[a - 1]
		else:
			second = min(now - s[idx - 1], s[idx] - now)
		ret = min(ret, first + second)
	else:
		for first_g in [t[idx - 1], t[idx]]:
			first = abs(first_g - x)
			now = first_g
			idx = bisect.bisect_left(s, now)
			if idx == 0:
				second = s[0] - now
			elif idx == a:
				second = now - s[a - 1]
			else:
				second = min(now - s[idx - 1], s[idx] - now)
			ret = min(ret, first + second)
	return ret
for i in range(q):
	x = int(input())
	print(minl(x))


from bisect import *
(A, B) = (abs, bisect)
(a, b, q, *l) = map(int, open(0).read().split())
f = 2 ** 60
(s, t) = ([-f] + l[:a] + [f], [-f] + l[a:a + b] + [f])
for x in l[a + b:]:
	m = f
	(i, j) = (B(s, x), B(t, x))
	for v in s[i - 1:i + 1]:
		for w in t[j - 1:j + 1]:
			m = min(m, A(x - v) + A(v - w), A(x - w) + A(w - v))
	print(m)


from bisect import bisect_left
(A, B, Q) = map(int, input().split())
INF = 2 * 10 ** 10
S = [-INF]
T = [-INF]
for _ in range(A):
	S.append(int(input()))
S.append(INF)
for _ in range(B):
	T.append(int(input()))
T.append(INF)
for _ in range(Q):
	x = int(input())
	s = bisect_left(S, x)
	t = bisect_left(T, x)
	ans = min(max(S[s], T[t]) - x, x - min(S[s - 1], T[t - 1]), 2 * S[s] - T[t - 1] - x, 2 * T[t] - S[s - 1] - x, T[t] + x - 2 * S[s - 1], S[s] + x - 2 * T[t - 1])
	print(ans)


import sys
input = sys.stdin.readline
(a, b, q) = map(int, input().split())
s = [-10 ** 11]
t = [-10 ** 11]
for i in range(a):
	s.append(int(input()))
s.append(10 ** 11)
for i in range(b):
	t.append(int(input()))
t.append(10 ** 11)
import bisect
for i in range(q):
	x = int(input())
	s_l = bisect.bisect_left(s, x)
	t_l = bisect.bisect_left(t, x)
	ans1 = x - min(s[s_l - 1], t[t_l - 1])
	ans2 = max(s[s_l], t[t_l]) - x
	ans3 = min(s[s_l] - x, x - t[t_l - 1]) + s[s_l] - t[t_l - 1]
	ans4 = min(t[t_l] - x, x - s[s_l - 1]) + t[t_l] - s[s_l - 1]
	print(min(ans1, ans2, ans3, ans4))


(a, b, q) = map(int, input().split())
s = [-float('inf')] + [int(input()) for i in range(a)] + [float('inf')]
t = [-float('inf')] + [int(input()) for i in range(b)] + [float('inf')]
x = [int(input()) for i in range(q)]
import bisect
for x_ in x:
	s_r = bisect.bisect_right(s, x_)
	t_r = bisect.bisect_right(t, x_)
	res = float('inf')
	for S in [s[s_r - 1], s[s_r]]:
		for T in [t[t_r - 1], t[t_r]]:
			d1 = abs(S - x_) + abs(T - S)
			d2 = abs(T - x_) + abs(S - T)
			res = min(res, d1, d2)
	print(res)


import math
import bisect
inf = 10 ** 12
(a, b, q) = map(int, input().split())
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
s.sort()
t = [-inf] + [int(input()) for _ in range(b)] + [inf]
t.sort()
for i in range(q):
	x = int(input())
	p = bisect.bisect_left(s, x)
	q = bisect.bisect_left(t, x)
	(sl, sr, tl, tr) = (s[p - 1], s[p], t[q - 1], t[q])
	sltl = x - min(sl, tl)
	srtr = max(sr, tr) - x
	sltr = min(x - sl * 2 + tr, -x - sl + tr * 2)
	srtl = min(x - tl * 2 + sr, -x - tl + sr * 2)
	print(min(sltl, srtr, sltr, srtl))


from bisect import bisect
import sys
input = sys.stdin.readline
(A, B, Q) = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for _ in range(Q):
	x = int(input())
	i = bisect(s, x)
	j = bisect(t, x)
	ans = INF
	for a in [s[i - 1], s[i]]:
		for b in [t[j - 1], t[j]]:
			ans = min(ans, abs(a - x) + abs(b - a), abs(b - x) + abs(a - b))
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
s = sorted([int(input()) for i in range(a)])
t = sorted([int(input()) for i in range(b)])
s = [-9e+20] + s + [9e+20]
t = [-9e+20] + t + [9e+20]
for i in range(q):
	x = int(input())
	p = bisect.bisect_left(s, x)
	m = bisect.bisect_left(t, x)
	ll = max(s[p], t[m]) - x
	lr = min(s[p] * 2 - t[m - 1] - x, t[m] * 2 - s[p - 1] - x)
	rl = min(s[p] - t[m - 1] * 2 + x, t[m] - s[p - 1] * 2 + x)
	rr = x - min(s[p - 1], t[m - 1])
	print(min(ll, lr, rl, rr))


import bisect
(a, b, q) = map(int, input().split())
s = [-10 ** 21] + [int(input()) for i in range(a)] + [10 ** 21]
t = [-10 ** 21] + [int(input()) for i in range(b)] + [10 ** 21]
x = [int(input()) for i in range(q)]
for i in x:
	bs = bisect.bisect_left(s, i)
	bt = bisect.bisect_left(t, i)
	(p_s, p_t) = (i - s[bs - 1], i - t[bt - 1])
	(n_s, n_t) = (s[bs] - i, t[bt] - i)
	a = max(p_s, p_t)
	b = max(n_s, n_t)
	c = n_t + p_s + min(p_s, n_t)
	d = n_s + p_t + min(p_t, n_s)
	print(min(a, b, c, d))


import bisect
(a, b, q) = map(int, input().split(' '))
INF = 10 ** 19
s = [-INF] + [int(input()) for i in range(a)] + [INF]
t = [-INF] + [int(input()) for i in range(b)] + [INF]
s.sort()
t.sort()
for i in range(q):
	x = int(input())
	(b, d) = (bisect.bisect(s, x), bisect.bisect(t, x))
	mi = INF
	for j in [s[b - 1], s[b]]:
		for k in [t[d - 1], t[d]]:
			mi = min(mi, abs(j - x) + abs(j - k), abs(k - x) + abs(j - k))
	print(mi)


from bisect import bisect_left
(A, B, Q) = list(map(int, input().split()))
S = [-10 ** 11] + [int(input()) for _ in range(A)] + [10 ** 11]
T = [-10 ** 11] + [int(input()) for _ in range(B)] + [10 ** 11]
for q in range(Q):
	x = int(input())
	i = bisect_left(S, x)
	j = bisect_left(T, x)
	(sl, sr) = (x - S[i - 1], S[i] - x)
	(tl, tr) = (x - T[j - 1], T[j] - x)
	print(min(max(sl, tl), max(sr, tr), sl * 2 + tr, tl * 2 + sr, sr * 2 + tl, tr * 2 + sl))


from bisect import bisect as B
I = lambda z: [int(input()) for _ in range(z)]
(a, b, q) = map(int, input().split())
F = float('inf')
s = [-F] + I(a) + [F]
t = [-F] + I(b) + [F]
X = I(q)
for x in X:
	i = B(s, x)
	j = B(t, x)
	(o, p) = (x - s[i - 1], s[i] - x)
	(q, r) = (x - t[j - 1], t[j] - x)
	print(min(2 * o + r, o + r * 2, 2 * p + q, p + q * 2, max(o, q), max(p, r)))


import bisect
(a, b, q) = map(int, input().split())
s = []
for i in range(a):
	s.append(int(input()))
s.sort()
t = []
for i in range(b):
	t.append(int(input()))
t.sort()
for i in range(q):
	x = int(input())
	temp_s = bisect.bisect(s, x)
	temp_t = bisect.bisect(t, x)
	temp = float('inf')
	if temp_s == 0:
		num = (s[0],)
	elif temp_s == a:
		num = (s[a - 1],)
	else:
		num = (s[temp_s - 1], s[temp_s])
	for j in num:
		temp2 = bisect.bisect(t, j)
		if temp2 == 0:
			num2 = (t[0],)
		elif temp2 == b:
			num2 = (t[b - 1],)
		else:
			num2 = (t[temp2 - 1], t[temp2])
		for k in num2:
			temp = min(temp, abs(x - j) + abs(j - k))
	if temp_t == 0:
		num = (t[0],)
	elif temp_t == b:
		num = (t[b - 1],)
	else:
		num = (t[temp_t - 1], t[temp_t])
	for j in num:
		temp2 = bisect.bisect(s, j)
		if temp2 == 0:
			num2 = (s[0],)
		elif temp2 == a:
			num2 = (s[a - 1],)
		else:
			num2 = (s[temp2 - 1], s[temp2])
		for k in num2:
			temp = min(temp, abs(x - j) + abs(j - k))
	print(temp)


from bisect import bisect_right as br
(a, b, q) = map(int, input().split())
I = 2 << 60
(s, t) = ([-I] + [int(input()) for _ in range(a)] + [I], [-I] + [int(input()) for _ in range(b)] + [I])
for question in range(q):
	x = int(input())
	(b, d, r) = (br(s, x), br(t, x), I)
	for S in [s[b - 1], s[b]]:
		for T in [t[d - 1], t[d]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			r = min(r, d1, d2)
	print(r)


from bisect import bisect_left
(A, B, Q) = map(int, input().split())
inf = float('inf')
S = [-inf] + [int(input()) for i in range(A)] + [inf]
T = [-inf] + [int(input()) for i in range(B)] + [inf]
X = [int(input()) for i in range(Q)]
for x in X:
	si = bisect_left(S, x)
	ti = bisect_left(T, x)
	ans = inf
	for s in [S[si - 1], S[si]]:
		for t in [T[ti - 1], T[ti]]:
			d1 = abs(s - x) + abs(t - s)
			d2 = abs(t - x) + abs(s - t)
			ans = min(ans, d1, d2)
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
s = [-float('inf')] + [int(input()) for _ in range(A)] + [float('inf')]
t = [-float('inf')] + [int(input()) for _ in range(B)] + [float('inf')]
X = [int(input()) for _ in range(Q)]
for x in X:
	sp = bisect.bisect_left(s, x)
	tp = bisect.bisect_left(t, x)
	res = float('inf')
	for S in [s[sp - 1], s[sp]]:
		for T in [t[tp - 1], t[tp]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


import bisect
import math
(a, b, q) = map(int, input().split())
s = [-math.inf]
t = [-math.inf]
x = []
for _ in range(a):
	s.append(int(input()))
for _ in range(b):
	t.append(int(input()))
for _ in range(q):
	x.append(int(input()))
s.append(math.inf)
t.append(math.inf)
for i in x:
	ws = abs(s[bisect.bisect(s, i) - 1] - i)
	if ws != math.inf:
		ws += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1]) - 1] - s[bisect.bisect(s, i) - 1]), abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])] - s[bisect.bisect(s, i) - 1]))
	else:
		ws = math.inf
	es = abs(s[bisect.bisect(s, i)] - i)
	if es != math.inf:
		es += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i)]) - 1] - s[bisect.bisect(s, i)]), abs(t[bisect.bisect(t, s[bisect.bisect(s, i)])] - s[bisect.bisect(s, i)]))
	else:
		es = math.inf
	wt = abs(t[bisect.bisect(t, i) - 1] - i)
	if wt != math.inf:
		wt += min(abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1]) - 1] - t[bisect.bisect(t, i) - 1]), abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])] - t[bisect.bisect(t, i) - 1]))
	else:
		wt = math.inf
	et = abs(t[bisect.bisect(t, i)] - i)
	if et != math.inf:
		et += min(abs(s[bisect.bisect(s, t[bisect.bisect(t, i)]) - 1] - t[bisect.bisect(t, i)]), abs(s[bisect.bisect(s, t[bisect.bisect(t, i)])] - t[bisect.bisect(t, i)]))
	else:
		et = math.inf
	print(min(ws, es, wt, et))


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]
for i in range(Q):
	x = int(input())
	s = bisect.bisect_left(S, x)
	t = bisect.bisect_left(T, x)
	saisyo = float('inf')
	for j in [S[s], S[s - 1]]:
		for k in [T[t], T[t - 1]]:
			saisyo = min(saisyo, abs(j - x) + abs(k - j), abs(k - x) + abs(j - k))
	print(saisyo)


from bisect import bisect_left
INF = 10 ** 18
(A, B, Q) = map(int, input().split())
als = [int(input()) for i in range(A)]
bls = [int(input()) for i in range(B)]
als = [-INF] + als + [INF]
bls = [-INF] + bls + [INF]
for i in range(Q):
	x = int(input())
	ai = bisect_left(als, x)
	bi = bisect_left(bls, x)
	res = INF
	for S in [als[ai], als[ai - 1]]:
		for T in [bls[bi], bls[bi - 1]]:
			(d1, d2) = (abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


import bisect
INF = 10 ** 15
(A, B, Q) = map(int, input().split())
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]
for i in range(Q):
	x = int(input())
	sw = bisect.bisect_right(s, x) - 1
	se = sw + 1
	tw = bisect.bisect_right(t, x) - 1
	te = tw + 1
	res = INF
	for S in [s[sw], s[se]]:
		for T in [t[tw], t[te]]:
			(d1, d2) = (abs(x - S) + abs(T - S), abs(x - T) + abs(S - T))
			res = min(res, d1, d2)
	print(res)


(a, b, q) = map(int, input().split())
inf = 10 ** 18
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(b)] + [inf]
x = [int(input()) for _ in range(q)]
from bisect import bisect_left, bisect_right
for i in range(q):
	idx_s = bisect_right(s, x[i])
	idx_t = bisect_right(t, x[i])
	ans = inf
	for ss in [s[idx_s - 1], s[idx_s]]:
		for tt in [t[idx_t - 1], t[idx_t]]:
			(d1, d2) = (abs(ss - x[i]) + abs(ss - tt), abs(tt - x[i]) + abs(tt - ss))
			ans = min(ans, d1, d2)
	print(ans)


import bisect
(a, b, q) = map(int, input().split())
s = []
for i in range(a):
	s.append(int(input()))
t = []
for j in range(b):
	t.append(int(input()))
inf = 10 ** 18
s = [-inf] + s + [inf]
t = [-inf] + t + [inf]
for k in range(q):
	x = int(input())
	(s_r, t_r) = (bisect.bisect_right(s, x), bisect.bisect_right(t, x))
	res = inf
	for s_ in [s[s_r], s[s_r - 1]]:
		for t_ in [t[t_r], t[t_r - 1]]:
			d = min(abs(x - s_), abs(x - t_)) + abs(s_ - t_)
			res = min(d, res)
	print(res)


from bisect import bisect_left, bisect_right
(A, B, Q) = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
for i in range(Q):
	x = int(input())
	sl = float('-inf')
	sr = float('inf')
	tl = float('-inf')
	tr = float('inf')
	a = bisect_left(s, x)
	if a > 0:
		sl = s[a - 1]
	if a <= A - 1:
		sr = s[a]
	b = bisect_left(t, x)
	if b > 0:
		tl = t[b - 1]
	if b <= B - 1:
		tr = t[b]
	pat_LL = x - min(sl, tl)
	pat_LR = sr - tl + min(sr - x, x - tl)
	pat_RL = tr - sl + min(tr - x, x - sl)
	pat_RR = max(sr, tr) - x
	print(min(pat_LL, pat_LR, pat_RL, pat_RR))


import bisect
(a, b, q) = map(int, input().split())
inf = 10 ** 18
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(b)] + [inf]
for _ in range(q):
	start = int(input())
	x = bisect.bisect_right(s, start)
	y = bisect.bisect_right(t, start)
	ans = inf
	for i in [s[x], s[x - 1]]:
		for j in [t[y], t[y - 1]]:
			res = min(abs(i - start) + abs(j - i), abs(j - start) + abs(i - j))
			ans = min(ans, res)
	print(ans)


import bisect
(A, B, Q) = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
x = [int(input()) for _ in range(Q)]
st = [0] * A
ts = [0] * B
for i in range(A):
	tmp = bisect.bisect_left(t, s[i])
	if tmp == 0:
		st[i] = t[0] - s[i]
	elif tmp == B:
		st[i] = s[i] - t[-1]
	else:
		st[i] = min(s[i] - t[tmp - 1], t[tmp] - s[i])
for i in range(B):
	tmp = bisect.bisect_left(s, t[i])
	if tmp == 0:
		ts[i] = s[0] - t[i]
	elif tmp == A:
		ts[i] = t[i] - s[-1]
	else:
		ts[i] = min(t[i] - s[tmp - 1], s[tmp] - t[i])
for i in range(Q):
	cand = set()
	tmp = bisect.bisect_left(s, x[i])
	if tmp > 0:
		cand.add(x[i] - s[tmp - 1] + st[tmp - 1])
	if tmp < A:
		cand.add(s[tmp] - x[i] + st[tmp])
	tmp = bisect.bisect_left(t, x[i])
	if tmp > 0:
		cand.add(x[i] - t[tmp - 1] + ts[tmp - 1])
	if tmp < B:
		cand.add(t[tmp] - x[i] + ts[tmp])
	print(min(cand))


import bisect
(A, B, Q) = map(int, input().split())
INF = float('inf')
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
ans = []
for q in range(Q):
	x = int(input())
	i = bisect.bisect_right(S, x)
	j = bisect.bisect_right(T, x)
	d = INF
	for s in [S[i - 1], S[i]]:
		for t in [T[j - 1], T[j]]:
			d1 = abs(x - s) + abs(s - t)
			d2 = abs(x - t) + abs(s - t)
			d = min(d, d1, d2)
	ans.append(d)
print(*ans, sep='\n')


import bisect
(A, B, Q) = map(int, input().split())
INF = 10 ** 18
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
x = [int(input()) for _ in range(Q)]
for a in x:
	ans1 = 0
	ans2 = 0
	b = bisect.bisect_left(S, a)
	c = bisect.bisect_left(T, a)
	res = INF
	for s in [S[b - 1], S[b]]:
		for t in [T[c - 1], T[c]]:
			(d1, d2) = (abs(s - a) + abs(s - t), abs(t - a) + abs(s - t))
			res = min(res, d1, d2)
	print(res)


from bisect import bisect_left
(A, B, Q, *L) = map(int, open(0).read().split())
s = L[:A]
t = L[A:A + B]
x = L[A + B:]
s = [-float('inf')] + s + [float('inf')]
t = [-float('inf')] + t + [float('inf')]
for c in x:
	br = bisect_left(t, c)
	ar = bisect_left(s, c)
	bl = br - 1
	al = ar - 1
	print(min(s[ar] - t[bl] + min(s[ar] - c, c - t[bl]), t[br] - s[al] + min(t[br] - c, c - s[al]), c - min(t[bl], s[al]), max(s[ar], t[br]) - c))