Problem:
 Score : 400 points    Problem Statement 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 1 2 3 4
100
600
400
900
1000
150
2000
899
799      Sample Output 1 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.        Sample Input 2 1 1 3
1
10000000000
2
9999999999
5000000000      Sample Output 2 10000000000
10000000000
14999999998  The road is quite long, and we may need to travel a distance that does not fit into a 32 -bit integer.

Solution (Python):
import math
import bisect
A,B,Q = list(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.sort()
Ts.sort()
ss = len(Ss)
ts = len(Ts)
for x in Xs:
    si = bisect.bisect_right(Ss,x)
    ti = bisect.bisect_right(Ts, x)
    cands = [[],[]]
    if si > 0:
        cands[0].append(Ss[si-1])
    if si < ss:
        cands[0].append(Ss[si])
    if ti > 0:
        cands[1].append(Ts[ti-1])
    if ti < ts:
        cands[1].append(Ts[ti])
    anss = []
    temp = 0
    for i in [0,1]:
        for t1 in cands[i]: 
            for t2 in cands[1-i]: 
                temp = abs(x-t1) +  abs(t2-t1)
                anss.append(temp)
    print(min(anss))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

A, B, Q = LI()
s_list = []
t_list = []
for i in range(A):
  s_list.append(II())
for j in range(B):
  t_list.append(II())
x_list = []
for q in range(Q):
  x_list.append(II())
  
import bisect
for x in x_list:
  
  
  tidx = bisect.bisect_left(t_list, x)
  if tidx == 0:
    tl = 9*10**11
  else:
    tl = t_list[tidx-1]
  if tidx == len(t_list):
    tr = 9*10**11
  else:
    tr = t_list[tidx]

  sidx = bisect.bisect_left(s_list, x)
  if sidx == 0:
    sl = 9*10**11
  else:
    sl = s_list[sidx-1]
  if sidx == len(s_list):
    sr = 9*10**11
  else:
    sr = s_list[sidx]

  dsl = abs(x-sl)
  dsr = abs(x-sr)
  dtl = abs(x-tl)
  dtr = abs(x-tr)

  print(min(max(dsl, dtl), max(dsr, dtr), dsl+dtr+min(dsl, dtr), dsr+dtl+min(dsr,dtl)))

Solution (Python):
x=[]
for i,j in enumerate(map(int,input().split())):
    c=[]
    for l in range(j):
        t=int(input())
        x.append((i,l,t))
        c.append([t])
x.sort(key=lambda x:x[2])
l=[-10**20]*2
for y in x:
    if y[0] != 2: 
        l[y[0]]=y[2]
    else:
        c[y[1]]+=l[:]
l=[10**20]*2
for y in x[::-1]:
    if y[0] != 2: 
        l[y[0]]=y[2]
    else:
        c[y[1]]+=l[:]
print("\n".join(str(min(d[0]-min(d[1],d[2]),max(d[3],d[4])-d[0],
        d[4]-d[1]+min(d[0]-d[1],d[4]-d[0]),
        d[3]-d[2]+min(d[0]-d[2],d[3]-d[0])
        )) for d in c)
        )

Solution (Python):
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)

Solution (Python):
from bisect import bisect_left
from itertools import product

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 xx in x:
    ret = inf

    sr = bisect_left(s, xx)  
    sl = max(0, sr - 1)

    tr = bisect_left(t, xx)
    tl = max(0, tr - 1)

    for ss, tt in product(s[sl:sr + 1], t[tl:tr + 1]):
        ret = min(
            ret,
            abs(xx - ss) + abs(ss - tt),
            abs(xx - tt) + abs(tt - ss)
        )
    print(ret)

Solution (Python):
def main():
    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()), i] for i in range(q)]

    y = sorted(x, key=lambda a: a[0])
    d = [0] * q
    i, j = 0, 0
    for p in y:
        place = p[0]
        while s[i] < place:
            i += 1
        while t[j] < place:
            j += 1
        sl, sr = s[i-1], s[i]
        tl, tr = t[j-1], t[j]

        rr = abs(max(sr, tr) - place)
        ll = abs(min(sl, tl) - place)
        if sr < tr:
            rl = abs(sr - place) + abs(sr - tl)
        else:
            rl = abs(tr - place) + abs(tr - sl)
        if sl < tl:
            lr = abs(tl - place) + abs(sr - tl)
        else:
            lr = abs(sl - place) + abs(tr - sl)

        d[p[1]] = min(rr, rl, lr, ll)

    for dd in d:
        print(dd)


main()

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right





def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 



def main():
    A, B, Q = l_inpl()
    s = [-INF] + line_inpl(A) + [INF]
    t = [-INF] + line_inpl(B) + [INF]
    x = line_inpl(Q)

    for qi in range(Q):
        b, d = bisect_right(s, x[qi]), bisect_right(t, x[qi])
        res = INF
        for S in (s[b-1], s[b]):
            for T in (t[d-1], t[d]):
                d1, d2 = abs(S-x[qi]) + abs(T-S), abs(T-x[qi]) + abs(S-T)
                res = min(res, d1, d2)
        print(res)

if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect_right

A , B , Q = map(int,input().split())
INF = 10**20

s = [0] * A
t = [0] * B
x = [0] * Q

for i in range(A) :
    s[i] = int(input())
for i in range(B) :
    t[i] = int(input())
for i in range(Q) :
    x[i] = int(input())

s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for p in x :
    rightS = bisect_right(s,p)
    rightT = bisect_right(t,p)

    dist = INF

    for ss in [s[rightS - 1], s[rightS]] :
        for tt in [t[rightT - 1], t[rightT]] :
            dist1 = abs(ss - p) + abs(ss - tt)
            dist2 = abs(tt - p) + abs(tt - ss)
            dist = min(dist, dist1, dist2)

    print(dist)

Solution (Python):
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)

Solution (Python):
from bisect import bisect_left, bisect_right

def func(x, y, z):
    yi = bisect_right(y, x)

    if yi == 0 or yi == len(y):
        ans = 0
        if yi == len(y):
            yi -= 1
        ans += abs(y[yi] - x)
        x = y[yi]

        zi = bisect_right(z, x)
        ans += min(abs(z[zi % len(z)] - x), abs(z[(zi-1) % len(z)] - x))
        return ans
    else:
        ans1 = ans2 = 0
        ans1 += abs(y[yi-1] - x)
        x1 = y[yi-1]
        zi = bisect_right(z, x1)
        ans1 += min(abs(z[zi % len(z)] - x1), abs(z[(zi-1) % len(z)] - x1))

        ans2 += abs(y[yi] - x)
        x2 = y[yi]
        zi = bisect_right(z, x2)
        ans2 += min(abs(z[zi % len(z)] - x2), abs(z[(zi-1) % len(z)] - x2))
        return min(ans1, ans2)


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())

    print(min(func(x, s, t), func(x, t, s)))

Solution (Python):
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')]

res = []
for i in range(Q):
    x = int(input())
    a = bisect.bisect_left(s, x) - 1
    b = a + 1
    c = bisect.bisect_left(t, x) - 1
    d = c + 1
    ans = float('inf')
    for S in [s[a], s[b]]:
        for T in [t[c], t[d]]:
            ans = min(ans, abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
    res.append(ans)

print('\n'.join(map(str, res)))

Solution (Python):
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')]

res = []
for i in range(Q):
    x = int(input())
    b = bisect.bisect_right(s, x)
    a = b - 1
    d = bisect.bisect_right(t, x)
    c = d - 1
    ans = float('inf')
    for S in [s[a], s[b]]:
        for T in [t[c], t[d]]:
            ans = min(ans, abs(S - x) + abs(T - S), abs(T - x) + abs(S - T))
    res.append(ans)

print('\n'.join(map(str, res)))

Solution (Python):
a,b,q = map(int, input().split())
s = [-5*10**10] + [int(input()) for _ in range(a)] + [5*10**10]
t = [-5*10**10] + [int(input()) for _ in range(b)] + [5*10**10]
a += 2
b += 2

def bis(x,lis,num):
    ok = 0
    ng = num
    while ng - ok > 1:
        mid = (ok+ng)//2
        if lis[mid] > x:
            ng = mid
        else:
            ok = mid
    return ok
for _ in range(q):
    x = int(input())
    sl = bis(x,s,a)
    tl = bis(x,t,b)
    sr = sl + 1
    tr = tl + 1
    sl = x-s[sl]
    sr = s[sr]-x
    tl = x-t[tl]
    tr = t[tr]-x
    dis = min(max(sr,tr),max(sl,tl),sr+tl+min(sr,tl),sl+tr+min(sl,tr))
    print(dis)

Solution (Python):
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())
    s1 = bisect.bisect_left(S, x) -1 
    s2 = s1 + 1 
    t1 = bisect.bisect_left(T, x) -1 
    t2 = t1 + 1 
    res = 10 ** 18
    for s in [S[s1], S[s2]]:
        for t in [T[t1], T[t2]]:
            d1, d2 = abs(s-x) + abs(t-s), abs(t-x) + abs(s-t)
            res = min(res, d1, d2)
    print (res)

Solution (Python):
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())
  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)

Solution (Python):
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())
  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)

Solution (Python):
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())
    s1 = bisect.bisect_left(S, x) -1 
    s2 = s1 + 1 
    t1 = bisect.bisect_left(T, x) -1 
    t2 = t1 + 1 
    res = 10 ** 18
    for s in [S[s1], S[s2]]:
        for t in [T[t1], T[t2]]:
            d1, d2 = abs(s-x) + abs(t-s), abs(t-x) + abs(s-t)
            res = min(res, d1, d2)
    print (res)

Solution (Python):
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())
    b, d = bisect.bisect_left(s, x), bisect.bisect_left(t, x)
    ans = 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)

Solution (Python):
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())
    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)

Solution (Python):
from bisect import bisect_left,bisect_right

a,b,q=map(int,input().split())
s=[-10**15]
s+=[int(input()) for _ in range(a)]
s+=[10**15]
t=[-10**15]
t+=[int(input()) for _ in range(b)]
t+=[10**15]
x=[int(input()) for _ in range(q)]
for i in range(q):
  sl=s[bisect_left(s,x[i])-1]
  sr=s[bisect_right(s,x[i])]
  tl=t[bisect_left(t,x[i])-1]
  tr=t[bisect_right(t,x[i])]
  ans1=max(x[i]-sl,x[i]-tl)
  ans2=max(sr-x[i],tr-x[i])
  ans3=(x[i]-sl)*2+(tr-x[i])
  ans4=(x[i]-tl)*2+(sr-x[i])
  ans5=(sr-x[i])*2+(x[i]-tl)
  ans6=(tr-x[i])*2+(x[i]-sl)
  print(min(ans1,ans2,ans3,ans4,ans5,ans6))

Solution (Python):
from sys import stdin
import bisect

A, B, Q = [int(x) for x in stdin.readline().rstrip().split()]
S = [int(stdin.readline().rstrip()) for _ in range(A)]
T = [int(stdin.readline().rstrip()) for _ in range(B)]
X = [int(stdin.readline().rstrip()) for _ in range(Q)]
S.append(10 ** 18)
T.append(10 ** 18)
S.insert(0, -10 ** 18)
T.insert(0, -10 ** 18)

for x in X:
    index_s = bisect.bisect_left(S, x)
    index_t = bisect.bisect_left(T, x)
    print(min(
        abs(S[index_s - 1] - T[index_t - 1]) + abs(x - S[index_s - 1]),
        abs(S[index_s - 1] - T[index_t - 1]) + abs(x - T[index_t - 1]),
        abs(S[index_s] - T[index_t - 1]) + abs(x - S[index_s]),
        abs(S[index_s] - T[index_t - 1]) + abs(x - T[index_t - 1]),
        abs(S[index_s] - T[index_t]) + abs(x - S[index_s]),
        abs(S[index_s] - T[index_t]) + abs(x - T[index_t]),
        abs(S[index_s - 1] - T[index_t]) + abs(x - S[index_s - 1]),
        abs(S[index_s - 1] - T[index_t]) + abs(x - T[index_t]),
    ))

Solution (Python):
from bisect import bisect_left

a,b,q = map(int,input().split())

inf = 10**11
s = [-inf]+[int(input()) for i in range(a)]+[inf,inf]
t = [-inf]+[int(input()) for i in range(b)]+[inf,inf]
X = [int(input()) for i in range(q)]

for x in X:
	s_idx = bisect_left(s,x)
	t_idx = bisect_left(t,x)
	sr = s[s_idx]
	sl = s[s_idx-1]
	tr = t[t_idx]
	tl = t[t_idx-1]
	
	ans1 = max(sr-x,tr-x)
	ans2 = max(x-sl,x-tl)
	ans3 = (x-sl)*2+(tr-x)
	ans4 = (tr-x)*2+(x-sl)
	ans5 = (x-tl)*2+(sr-x)
	ans6 = (sr-x)*2+(x-tl)
	
	print(min(ans1,ans2,ans3,ans4,ans5,ans6))

Solution (Python):
from bisect import bisect_left

a,b,q = map(int,input().split())

inf = 10**11
s = [-inf]+[int(input()) for i in range(a)]+[inf,inf]
t = [-inf]+[int(input()) for i in range(b)]+[inf,inf]
X = [int(input()) for i in range(q)]

for x in X:
	s_idx = bisect_left(s,x)
	t_idx = bisect_left(t,x)
	sr = s[s_idx]
	sl = s[s_idx-1]
	tr = t[t_idx]
	tl = t[t_idx-1]
	
	ans1 = max(sr-x,tr-x)
	ans2 = max(x-sl,x-tl)
	ans3 = (x-sl)*2+(tr-x)
	ans4 = (tr-x)*2+(x-sl)
	ans5 = (x-tl)*2+(sr-x)
	ans6 = (sr-x)*2+(x-tl)
	
	print(min(ans1,ans2,ans3,ans4,ans5,ans6))

Solution (Python):
import bisect


A, B, Q = map(int, input().split())
s = [-10 ** 12] + [int(input()) for _ in range(A)] + [10 ** 12]
t = [-10 ** 12] + [int(input()) for _ in range(B)] + [10 ** 12]
x = [int(input()) for _ in range(Q)]
for i in x:
    si = bisect.bisect_left(s, i)
    ti = bisect.bisect_left(t, i)
    s1, s2 = s[si - 1], s[si]
    t1, t2 = t[ti - 1], t[ti]
    ans = 10 ** 12
    for ss in [s1, s2]:
        for tt in [t1, t2]:
            ans = min(ans, abs(i - ss) + abs(ss - tt),
                      abs(i - tt) + abs(ss - tt))
    print(ans)

Solution (Python):
from bisect import bisect
INF = float('inf')
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:
    ans = INF
    i = bisect(S,x)
    for j in {min(i,A-1),max(i-1,0)}:
        s = S[j]
        k = bisect(T,s)
        for l in {min(k,B-1),max(k-1,0)}:
            t = T[l]
            ans = min(ans,abs(x-s)+abs(s-t))
    i = bisect(T,x)
    for j in {min(i,B-1),max(i-1,0)}:
        t = T[j]
        k = bisect(S,t)
        for l in {min(k,A-1),max(k-1,0)}:
            s = S[l]
            ans = min(ans,abs(x-t)+abs(s-t))
    print(ans)

Solution (Python):
from bisect import bisect
INF = float('inf')
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:
    ans = INF
    i = bisect(S,x)
    for j in {min(i,A-1),max(i-1,0)}:
        s = S[j]
        k = bisect(T,s)
        for l in {min(k,B-1),max(k-1,0)}:
            t = T[l]
            ans = min(ans,abs(x-s)+abs(s-t))
    i = bisect(T,x)
    for j in {min(i,B-1),max(i-1,0)}:
        t = T[j]
        k = bisect(S,t)
        for l in {min(k,A-1),max(k-1,0)}:
            s = S[l]
            ans = min(ans,abs(x-t)+abs(s-t))
    print(ans)

Solution (Python):
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)]
"""
def bsr(a, v, lo=0, hi=None):
	if None == hi:
		hi = len(a) - 1
	while lo <= hi:
		mi = (lo + hi) // 2
		if a[mi] <= v:
			lo = mi + 1
		else:
			hi = mi - 1
	return lo
"""
bsr = bisect.bisect_right

INF = 10 ** 18
s = [-INF] + S + [INF]
t = [-INF] + T + [INF]

for x in X:
	sp = bsr(s, x)
	tp = bsr(t, x)
	cost = []
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	print(min(cost))

Solution (Python):
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)]
"""
def bsr(a, v, lo=0, hi=None):
	if None == hi:
		hi = len(a) - 1
	while lo <= hi:
		mi = (lo + hi) // 2
		if a[mi] <= v:
			lo = mi + 1
		else:
			hi = mi - 1
	return lo
"""
bsr = bisect.bisect_right

INF = 10 ** 18
s = [-INF] + S + [INF]
t = [-INF] + T + [INF]

for x in X:
	sp = bsr(s, x)
	tp = bsr(t, x)
	cost = []
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	print(min(cost))

Solution (Python):
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)]

def bsr(a, v, lo=0, hi=None):
	if None == hi:
		hi = len(a) - 1
	while lo <= hi:
		mi = (lo + hi) // 2
		if a[mi] <= v:
			lo = mi + 1
		else:
			hi = mi - 1
	return lo

INF = 10 ** 18
s = [-INF] + S + [INF]
t = [-INF] + T + [INF]

for x in X:
	sp = bsr(s, x)
	tp = bsr(t, x)
	cost = []
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	print(min(cost))

Solution (Python):
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)]

def bsr(a, v, lo=0, hi=None):
	if None == hi:
		hi = len(a) - 1
	while lo <= hi:
		mi = (lo + hi) // 2
		if a[mi] <= v:
			lo = mi + 1
		else:
			hi = mi - 1
	return lo

INF = 10 ** 18
s = [-INF] + S + [INF]
t = [-INF] + T + [INF]

for x in X:
	sp = bsr(s, x)
	tp = bsr(t, x)
	cost = []
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(s[sp - 0] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(s[sp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 0] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 0] - t[tp - 1])]
	cost += [abs(t[tp - 0] - x) + abs(s[sp - 1] - t[tp - 0])]
	cost += [abs(t[tp - 1] - x) + abs(s[sp - 1] - t[tp - 1])]
	print(min(cost))

Solution (Python):
import sys
import bisect
input = sys.stdin.readline

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())
    sh = bisect.bisect_left(s, x)
    te = bisect.bisect_left(t, x)
    dist_sh_e = float("inf")
    dist_sh_w = float("inf")
    dist_te_e = float("inf")
    dist_te_w = float("inf")
    if sh != 0:
        dist_sh_w = x - s[sh - 1]
    if sh != a:
        dist_sh_e = s[sh] - x
    if te != 0:
        dist_te_w = x - t[te - 1]
    if te != b:
        dist_te_e = t[te] - x
    print(min(max(dist_sh_w, dist_te_w), max(dist_sh_e, dist_te_e), dist_sh_w * 2 + dist_te_e, dist_te_w * 2 + dist_sh_e, dist_sh_e * 2 + dist_te_w, dist_te_e * 2 + dist_sh_w))

Solution (Python):
import bisect

A,B,Q=map(int,input().split())
slist = []
for i in range(A):
  slist.append(int(input()))


tlist = []
for i in range(B):
  tlist.append(int(input()))


for i in range(Q):
  x = int(input())
  min_dist = 10**11
  
  
  if x < slist[0]:
    si = [0]
  elif slist[-1] < x:
    si = [len(slist)-1]
  else:
    sl = bisect.bisect(slist,x)
    si = [sl-1,sl]
    
  for index in si:
    s = slist[index]
    dist = abs(x-s)

    if s < tlist[0]:
      dist += tlist[0]-s
    elif tlist[-1] < s:
      dist += s-tlist[-1]
    else:
      tl = bisect.bisect(tlist,s)  
      dist += min(s-tlist[tl-1],tlist[tl]-s)
    
    if dist < min_dist:
      min_dist = dist

  
  if x < tlist[0]:
    ti = [0]
  elif tlist[-1] < x:
    ti = [len(tlist)-1]
  else:
    tl = bisect.bisect(tlist,x)
    ti = [tl-1,tl]
    
  for index in ti:
    t = tlist[index]
    dist = abs(x-t)

    if t < slist[0]:
      dist += slist[0]-t
    elif slist[-1] < t:
      dist += t-slist[-1]
    else:
      sl = bisect.bisect(slist,t)
      dist += min(t-slist[sl-1],slist[sl]-t)
    
    if dist < min_dist:
      min_dist = dist

  print(min_dist)

Solution (Python):
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)

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 i in range(Q):
	min_s = 0
	max_s = A
	ind_s = (min_s + max_s) // 2
	
	while min_s < max_s:
		ind_s = (min_s + max_s) // 2
		if s[ind_s] > x[i]:
			max_s = ind_s
		else:
			min_s = ind_s + 1
	
	ind_s = (min_s + max_s) // 2
	
	min_t = 0
	max_t = B
	ind_t = (min_t + max_t) // 2
	
	while min_t < max_t:
		ind_t = (min_t + max_t) // 2
		if t[ind_t] > x[i]:
			max_t = ind_t
		else:
			min_t = ind_t + 1
	
	ind_t = (min_t + max_t) // 2
		
	if ind_s == A or ind_t == B:
		ptn1 = 3 * 10 ** 10
	else:
		ptn1 = max((s[ind_s] - x[i]), (t[ind_t] - x[i]))
		
	if ind_s == 0 or ind_t == 0:
		ptn2 = 3 * 10 ** 10
	else:
		ptn2 = max((x[i] - s[ind_s - 1]), (x[i] - t[ind_t - 1]))
		
	if ind_s == A or ind_t == 0:
		ptn3 = 3 * 10 ** 10
	else:
		ptn3 = min(2 * (s[ind_s] - x[i]) + (x[i] - t[ind_t - 1]), (s[ind_s] - x[i]) + 2 * (x[i] - t[ind_t - 1]))
		
	if ind_s == 0 or ind_t == B:
		ptn4 = 3 * 10 ** 10
	else:
		ptn4 = min(2 * (x[i] - s[ind_s - 1]) + (t[ind_t] - x[i]), (x[i] - s[ind_s - 1]) + 2 * (t[ind_t] - x[i]))
		
	print(min(ptn1, ptn2, ptn3, ptn4))

Solution (Python):
import sys
import bisect


input = sys.stdin.readline
a, b, q = map(int, input().split())
inf = 10**12
s_list = [-inf] + [int(input()) for _ in range(a)] + [inf]
s_list.sort()
t_list = [-inf] + [int(input()) for _ in range(b)] + [inf]
t_list.sort()
ans_list =[]
for _ in range(q):
    x = int(input())
    s_idx = bisect.bisect_right(s_list, x)
    t_idx = bisect.bisect_right(t_list, x)
    ans = inf
    for s in [s_list[s_idx-1], s_list[s_idx]]:
        for t in [t_list[t_idx-1], t_list[t_idx]]:
            ans = min(ans, min(abs(x-s), abs(x-t)) + abs(s-t))
    print(ans)

Solution (Python):

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)

Solution (Python):
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)))

Solution (Python):
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())
    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)

Solution (Python):
A,B,Q=map(int,input().split())
s=[]
t=[]
answer=[]
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
s=[-10**21]+s+[10**21]
t=[-10**21]+t+[10**21]

def binary_search(list,target):
    left=0
    right=len(list)-1
    while left<=right:
        center=(left+right)//2
        if list[center]<target and list[center+1]>target:
            return center
            break
        elif list[center]<target:
            left=center+1
        else:
            right=center-1
    
for q in range(Q):
    x=int(input())
    i=binary_search(s,x)
    j=binary_search(t,x)
    ans=min(max(x-t[j],x-s[i]),max(s[i+1]-x,t[j+1]-x),t[j+1]-s[i]+min(x-s[i],t[j+1]-x),s[i+1]-t[j]+min(x-t[j],s[i+1]-x))
    answer.append(ans)

for k in answer:
    print(k)

Solution (Python):
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]))

Solution (Python):
import bisect

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())
    s_pl = bisect.bisect_left(s, x) - 1
    s_pr = s_pl + 1
    t_pl = bisect.bisect_left(t, x) - 1
    t_pr = t_pl + 1
    
    dist = 10 ** 11
    if 0 <= s_pl < A and 0 <= t_pr < B:
        dist = x + t[t_pr] - 2 * s[s_pl]
        dist = min(dist, 2 * t[t_pr] - s[s_pl] - x)
    if 0 <= s_pr < A and 0 <= t_pr < B:
        dist = min(dist, max(s[s_pr], t[t_pr]) - x)
    if 0 <= s_pl < A and 0 <= t_pl < B:
        dist = min(dist, x - min(s[s_pl], t[t_pl]))
    if 0 <= s_pr < A and 0 <= t_pl < B:
        dist = min(dist, x + s[s_pr] - 2 * t[t_pl])
        dist = min(dist, 2 * s[s_pr] - t[t_pl] - x)

    print(dist)

Solution (Python):
A,B,Q = list(map(int,input().split()))
s = []
for i in range(A):
    s.append(int(input()))
t = []
for i in range(B):
    t.append(int(input()))

s.sort()
t.sort()

from bisect import bisect

for i in range(Q):
    q=int(input())
    a=bisect(s,q)
    b=bisect(t,q)
    if a==0:
        sl = -10**27
    else:
        sl = s[a-1]
    if b==0:
        tl = -10**27
    else:
        tl = t[b-1]
    if a==A:
        sr = 10**27
    else:
        sr = s[a]
    if b==B:
        tr = 10**27
    else:
        tr = t[b]
    c1 = max(sr,tr)-q
    c2 = q-min(sl,tl)
    c3 = min(2*(sr-q)+(q-tl),2*(tr-q)+(q-sl),(tr-q)+2*(q-sl),(sr-q)+2*(q-tl))
    print(min(c1,c2,c3))