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 bisect as bi
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) ])
x = [int(input()) for i in range(q) ]

for i in range(q):
    Q = x[i]
    ans_s = []
    ans_t = []
    ans_s.append(s[bi.bisect_left(s,Q)-1])
    ans_t.append(t[bi.bisect_left(t,Q)-1])
    ans = 10**11
    
    if bi.bisect_right(s,Q) == len(s):
        ans_s.append(s[bi.bisect_left(s,Q)-1])
    else:
        ans_s.append(s[bi.bisect_left(s,Q)])
        
    if bi.bisect_right(t,Q) == len(t):
        ans_t.append(t[bi.bisect_left(t,Q)-1])
    else:
        ans_t.append(t[bi.bisect_left(t,Q)])
    
    for i in range(2):
        for j in range(2):
            ans = min(ans,abs(ans_s[i]-Q)+abs(ans_s[i]-ans_t[j]),abs(ans_t[j]-Q)+abs(ans_s[i]-ans_t[j]))
    
    print(ans)

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)]
s.append(1<<64)
t.append(1<<64)
import bisect
for i in range(q):
    sl=s[bisect.bisect_left(s,x[i])-1]
    sr=s[bisect.bisect_left(s,x[i])]
    tl=t[bisect.bisect_left(t,x[i])-1]
    tr=t[bisect.bisect_left(t,x[i])]
    
    z=[max(abs(x[i]-sl),abs(x[i]-tl)),
       max(abs(sr-x[i]),abs(tr-x[i])),
       abs(x[i]-sl)*2+abs(tr-x[i]),
       abs(x[i]-tl)*2+abs(sr-x[i]),
       abs(tr-x[i])*2+abs(x[i]-sl),
       abs(sr-x[i])*2+abs(x[i]-tl),]
    
    print(min(z))

Solution (Python):
import sys,collections,math,itertools,bisect;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

A,B,Q = Is()
As,Bs = [],[]
for i in range(A):
    As.append(I())
for i in range(B):
    Bs.append(I())
for i in range(Q):
    x = I()
    a = bisect.bisect_left(As,x)
    b = bisect.bisect_left(Bs,x)
    ans = 10**11
    As.append(10**11) if a == A else 0
    Bs.append(10**11) if b == B else 0
    AR = As[a] - x
    AL = x - As[a-1] if 0 < a  else 10**11
    BR = Bs[b] - x 
    BL = x - Bs[b-1] if 0 < b  else 10**11
    print(min(max(AL,BL),max(AR,BR),AL+BR+min(AL,BR),BL+AR+min(BL,AR)))

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

Solution (Python):
import bisect

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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')]


def dist(x):
    pS = bisect.bisect_right(S, x)
    pT = bisect.bisect_right(T, x)

    d1 = max(S[pS], T[pT])-x  
    d2 = x - min(S[pS-1], T[pT-1])  
    d3 = min(x-T[pT-1], S[pS]-x) + S[pS]-T[pT-1]  
    d4 = min(x-S[pS-1], T[pT]-x)+T[pT]-S[pS-1]  
    return min(d1, d2, d3, d4)


ans = []
for _ in range(Q):
    x = int(input())
    ans.append(dist(x))

print("\n".join(map(str, 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):
import sys
from bisect import bisect
F = sys.stdin
A, B, Q = map(int, F.readline().strip("\n").split(" "))
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(F.readline().strip("\n"))
for i in range(B):
    T[i+1] = int(F.readline().strip("\n"))
S[A+1] = 10**20
T[B+1] = 10**20

lenS, lenT = len(S), len(T)
Out = ""
for _ in range(Q):
    x = int(F.readline().strip("\n"))
    si, ti = bisect(S, x, 0, lenS), bisect(T, x, 0, lenT)
    sLength0, sLength1 = x - S[si-1], S[si] - x
    tLength0, tLength1 = x - T[ti-1], T[ti] - x
    Ans = min(max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    Out += str(Ans) + "\n"
print(Out)

Solution (Python):
import sys
from bisect import bisect
F = sys.stdin
A, B, Q = map(int, F.readline().strip("\n").split(" "))
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(F.readline().strip("\n"))
for i in range(B):
    T[i+1] = int(F.readline().strip("\n"))
S[A+1] = 10**20
T[B+1] = 10**20

lenS, lenT = len(S), len(T)
for _ in range(Q):
    x = int(F.readline().strip("\n"))
    si, ti = bisect(S, x, 0, lenS), bisect(T, x, 0, lenT)
    sLength0, sLength1 = x - S[si-1], S[si] - x
    tLength0, tLength1 = x - T[ti-1], T[ti] - x
    Ans = (max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    print(min(Ans))

Solution (Python):
from bisect import bisect
A, B, Q = map(int, input().split())
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S[A+1] = 10**20
T[B+1] = 10**20

lenS, lenT = len(S), len(T)
for _ in range(Q):
    x = int(input())
    si, ti = bisect(S, x, 0, lenS), bisect(T, x, 0, lenT)
    sLength0, sLength1 = x - S[si-1], S[si] - x
    tLength0, tLength1 = x - T[ti-1], T[ti] - x
    Ans = (max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    print(min(Ans))

Solution (Python):
A,B,Q=map(int,input().split())
s=[]
for _ in range(A):
    s.append(int(input()))
t=[]
for _ in range(B):
    t.append(int(input()))
from bisect import bisect
infty=1e50
for _ in range(Q):
    x=int(input())
    i=bisect(s,x)
    j=bisect(t,x)
    if 0<i<A:
        sL=s[i-1]
        sR=s[i]
    elif i==0:
        sL=-infty
        sR=s[i]
    else:
        sL=s[-1]
        sR=infty
    
    if 0<j<B:
        tL=t[j-1]
        tR=t[j]
    elif j==0:
        tL=-infty
        tR=t[j]
    else:
        tL=t[-1]
        tR=infty
    sL=abs(sL-x)
    sR=abs(sR-x)
    tL=abs(tL-x)
    tR=abs(tR-x)

    print(min(max(sR,tR),max(sL,tL),sR+tL+min(sR,tL),sL+tR+min(sL,tR)))

Solution (Python):
A,B,Q=map(int,input().split())
s=[]
for _ in range(A):
    s.append(int(input()))
t=[]
for _ in range(B):
    t.append(int(input()))
from bisect import bisect
infty=1e50
for _ in range(Q):
    x=int(input())
    i=bisect(s,x)
    j=bisect(t,x)
    if 0<i<A:
        sL=x-s[i-1]
        sR=s[i]-x
    elif i==0:
        sL=infty
        sR=s[i]-x
    else:
        sL=x-s[-1]
        sR=infty
    
    if 0<j<B:
        tL=x-t[j-1]
        tR=t[j]-x
    elif j==0:
        tL=infty
        tR=t[j]-x
    else:
        tL=x-t[-1]
        tR=infty
    print(min(max(sR,tR),max(sL,tL),sR+tL+min(sR,tL),sL+tR+min(sL,tR)))

Solution (Python):
A,B,Q=map(int,input().split())
inf=1e50
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)))

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

Solution (Python):
a,b,q=map(int,input().split())
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())
import bisect
for i in range(q):
  x=int(input())
  ids=bisect.bisect_left(s,x)
  idt=bisect.bisect_left(t,x)
  mi=14857363987569617
  if ids < a:
    if idt < b:
      mi=min(mi,min([abs(s[ids]-x)+abs(s[ids]-t[idt]),
                    abs(t[idt]-x)+abs(t[idt]-s[ids])]))
    if idt > 0:
      mi=min(mi,min([abs(s[ids]-x)+abs(s[ids]-t[idt-1]),
                    abs(t[idt-1]-x)+abs(t[idt-1]-s[ids])]))
  if ids > 0:
    if idt < b:
      mi=min(mi,min([abs(s[ids-1]-x)+abs(s[ids-1]-t[idt]),
                    abs(t[idt]-x)+abs(t[idt]-s[ids-1])]))
    if idt > 0:
      mi=min(mi,min([abs(s[ids-1]-x)+abs(s[ids-1]-t[idt-1]),
                    abs(t[idt-1]-x)+abs(t[idt-1]-s[ids-1])]))
  print(mi)

Solution (Python):
import bisect

a, b, q = map(int, input().split())
slist = list(sorted(int(input()) for _ in range(a)))
tlist = list(sorted(int(input()) for _ in range(b)))
xlist = [int(input()) for _ in range(q)]
dlist = []

for x in xlist:
    i = bisect.bisect_left(slist, x)
    j = bisect.bisect_left(tlist, x)
    d = float('inf')

    
    if i >= 1 and j >= 1:
        d = min(d, abs(min(slist[i - 1], tlist[j - 1]) - x))

    
    if i < a and j < b:
        d = min(d, abs(max(slist[i], tlist[j]) - x))

    
    k = min((v for v in (i - 1, i, i + 1) if 0 <= v < a), key=lambda v : abs(slist[v] - x))
    l = min((v for v in (j - 1, j, j + 1) if 0 <= v < b), key=lambda v : abs(tlist[v] - x))
    d = min(d, min(abs(slist[k] - x), abs(tlist[l] - x)) + abs(slist[k] - tlist[l]))

    dlist.append(d)


for d in dlist:
    print(d)

Solution (Python):

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


for _ in range(q):
    x = int(input())
    si = bisect_left(sl,x)
    ti = bisect_left(tl,x)
    
    res = INF
    for s in [sl[si-1], sl[si]]:
        for t in [tl[ti-1], tl[ti]]:
            res = min(
                res,
                abs(s-x)+abs(t-s),
                abs(t-x)+abs(s-t)
            )
    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(S-T),abs(T-x)+abs(S-T)
            res=min(res,d1,d2)
    print(res)

Solution (Python):
from bisect import *
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')]
Q = [int(input()) for _ in range(q)]
for x in Q:
    i = bisect_left(s, x)
    j = bisect_left(t, x)
    r = max(s[i]-x, t[j]-x)
    k = bisect_right(s, x)-1
    l = bisect_right(t, x)-1
    r = min(r, max(x-s[k], x-t[l]))
    r = min(r, min(x-s[k]+t[j]-s[k], t[j]-x+t[j]-s[k]))
    r = min(r, min(x-t[l]+s[i]-t[l], s[i]-x+s[i]-t[l]))
    print(r)

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

Solution (Python):
import bisect
import sys
input=lambda : sys.stdin.readline().rstrip()
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)])
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Solution (Python):
import bisect
import sys
input=lambda : sys.stdin.readline().rstrip()
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)])
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    sys.stdout.write("%d"%ans+'\n')

Solution (Python):
import bisect
import sys
input=lambda : sys.stdin.readline().rstrip()
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)])
out=[]
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    out.append(ans)
print(*out,sep='\n')

Solution (Python):
import bisect
import sys
input=lambda : sys.stdin.readline().rstrip()
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)])
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Solution (Python):
import bisect
import sys
def input_function():
    return sys.stdin.readline().rstrip()
input=input_function
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)])
for i in range(q):
    ans=999999999999
    x=int(input())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Solution (Python):
import bisect
from sys import stdin
readline=stdin.readline
a,b,q=map(int,input().rstrip().split())
s=sorted([int(readline().rstrip()) for i in range(a)])
t=sorted([int(readline().rstrip()) for i in range(b)])
for i in range(q):
    ans=999999999999
    x=int(readline().rstrip())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

Solution (Python):
import bisect
from sys import stdin
readline=stdin.readline
a,b,q=map(int,input().split())
s=sorted([int(readline()) for i in range(a)])
t=sorted([int(readline()) for i in range(b)])
for i in range(q):
    ans=999999999999
    x=int(readline())
    w=bisect.bisect_left(s,x)
    for j in range(2):
        if not 0<=w-j<len(s):
            continue
        z=bisect.bisect_left(t,s[w-j])
        for k in range(2):
            if not 0<=z-k<len(t):
                continue
            ans=min(ans,abs(x-s[w-j])+abs(s[w-j]-t[z-k]))
    w=bisect.bisect_left(t,x)
    for j in range(2):
        if not 0<=w-j<len(t):
            continue
        z=bisect.bisect_left(s,t[w-j])
        for k in range(2):
            if not 0<=z-k<len(s):
                continue
            ans=min(ans,abs(x-t[w-j])+abs(t[w-j]-s[z-k]))
    print(ans)

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

f_inf = float('inf')


def cal(syurui, d, index):
    if d == 'e':
        return syurui[index]
    elif d == 'w':
        return syurui[index - 1]


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()
ｓ.insert(0, -f_inf)
t.insert(0, -f_inf)
ｓ.append(f_inf)
t.append(f_inf)

for i in range(Q):
    x = int(input())
    s_index = bisect.bisect_left(s, x)
    t_index = bisect.bisect_left(t, x)
    print(min(max(abs(x-s[s_index]), abs(x-t[t_index])), max(abs(x-s[s_index-1]), abs(x-t[t_index-1])), 2*abs(x-s[s_index-1])+abs(x-t[t_index]), 2*abs(x-s[s_index])+abs(x-t[t_index-1]), abs(x-s[s_index-1])+2*abs(x-t[t_index]), abs(x-s[s_index])+2*abs(x-t[t_index-1])))

Solution (Python):
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 i in range(Q):
  x=int(input())
  j=bisect.bisect_left(s,x)
  if j==0:
    kouhoA=[s[j]]
  elif j==A:
    kouhoA=[s[j-1]]
  else:
    kouhoA=[s[j],s[j-1]]
  k=bisect.bisect_left(t,x)
  if k==0:
    kouhoB=[t[k]]
  elif k==B:
    kouhoB=[t[k-1]]
  else:
    kouhoB=[t[k],t[k-1]]
  if len(kouhoA)==1:
    
    if len(kouhoB)==1:
      a=abs(kouhoA[0]-kouhoB[0])
      ans=min([abs(x-kouhoA[0])+a,abs(x-kouhoB[0])+a])
    else:
      u=abs(x-kouhoA[0])
      a=abs(kouhoA[0]-kouhoB[0])
      b=abs(kouhoA[0]-kouhoB[1])
      ans=min([u+a,abs(x-kouhoB[0])+a,u+b,abs(x-kouhoB[1])+b])
  else:
    if len(kouhoB)==1:
      a=abs(kouhoA[0]-kouhoB[0])
      c=abs(kouhoB[0]-kouhoA[1])
      v=abs(x-kouhoB[0])
      ans=min([abs(x-kouhoA[0])+a,v+a,abs(x-kouhoA[1])+c,v+c])
    else:
      u=abs(x-kouhoA[0])
      a=abs(kouhoA[0]-kouhoB[0])
      b=abs(kouhoA[0]-kouhoB[1])
      c=abs(kouhoB[0]-kouhoA[1])
      d=abs(kouhoA[1]-kouhoB[1])
      v=abs(x-kouhoB[0])
      w=abs(x-kouhoA[1])
      y=abs(x-kouhoB[1])
      ans=min([u+a,v+a,w+d,y+d,w+c,v+c,u+b,y+b])
  print(ans)

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

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(T - S)
            res = min(res, d1, d2)
    print(res)

Solution (Python):
import bisect

A, B, Q = map(int, input().split())  
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.insert(0, -float("inf"))
S.append(float("inf"))
T.insert(0, -float("inf"))
T.append(float("inf"))

for i in range(Q):
    b = bisect.bisect_right(S, X[i])
    d = bisect.bisect_right(T, X[i])
    res = float("inf")
    for s in [S[b - 1], S[b]]:
        for t in [T[d - 1], T[d]]:
            d1 = abs(s - X[i]) + abs(t - s)  
            d2 = abs(t - X[i]) + abs(t - s)  
            res = min(res, d1, d2)

    print(res)

Solution (Python):
import bisect
a,b,q = input().split()
a = int(a)
b = int(b)
q = int(q)
s = []
for i in range(a):
    s.append(int(input()))
t = []
for i in range(b):
    t.append(int(input()))
x = []
for i in range(q):
    x.append(int(input()))
s = [-10**18] + s + [10**18]
t = [-10**18] + t + [10**18]

for xi in x:
    ans = 10 ** 18
    id1_rs = bisect.bisect_right(s,xi)
    id1_rt = bisect.bisect_right(t,xi)
    id1_ls = id1_rs - 1
    id1_lt = id1_rt - 1
    for i1 in [id1_ls,id1_rs]:
        id2_rt = bisect.bisect_right(t,s[i1])
        id2_lt = id2_rt - 1
        if id2_rt==len(t):
            id2_rt -= 1
        for i2 in [id2_rt,id2_lt]:
            ans_tmp = abs(xi - s[i1])
            ans_tmp += abs(s[i1] - t[i2])
            if ans > ans_tmp:
                ans = ans_tmp
    for i1 in [id1_lt,id1_rt]:
        id2_rs = bisect.bisect_right(s,t[i1])
        if id2_rs==len(s):
            id2_rs -= 1
        id2_ls = id2_rs - 1
        for i2 in [id2_rs,id2_ls]:
            ans_tmp = abs(xi - t[i1]) + abs(t[i1] - s[i2])
            if ans > ans_tmp:
                ans = ans_tmp
    print(ans)

Solution (Python):
import bisect

def bl(A,x):
    return bisect.bisect_left(A,x)

A,B,Q = map(int,input().split())

INF = 10 ** 18
S,T = [-INF],[-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)

O = []
for i in range(Q):
    x = int(input())    
    xs = bl(S,x)
    xt = bl(T,x)
    res = INF
    for s in [S[xs-1],S[xs]]:
        for t in [T[xt-1],T[xt]]:
            d1,d2 = abs(x-s)+abs(s-t),abs(x-t)+abs(t-s)
            res = min(res,d1,d2)
    O.append(res)
    
for o in O:
    print(o)

Solution (Python):
from bisect import bisect_left
A , B, Q = map(int,input().split(" "))
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())
    shrine1 = bisect_left(s,x[i])
    shrine2 = shrine1 - 1
    temple1 = bisect_left(t,x[i])
    temple2 = temple1 - 1
    a = float('inf')
    if shrine1 == len(s):
        s1,s2 = (x[i] - s[-1]),float('inf')
    elif shrine1 == 0:
        s1,s2 = float('inf'),(s[0] - x[i])
    else:
        s1,s2 = x[i] - s[shrine2],s[shrine1] - x[i]

    if temple1 == len(t):
        t1,t2 = (x[i] - t[-1]),float('inf')

    elif temple1 == 0:
        t1,t2 = float('inf'),(t[0] - x[i])
    else:
        t1,t2 = x[i] - t[temple2],t[temple1] - x[i]
    print(min([ max(s2,t2) , max(t1,s1) , min(s1,t2)*2+max(s1,t2), min(s2,t1)*2+max(s2,t1) ]))

Solution (Python):
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)
    j = bisect.bisect_left(d, z)
    if c[i - 1] < d[j - 1] and c[i] < d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    elif c[i - 1] < d[j - 1] and c[i] >= d[j]:
        return min(z - c[i - 1], z + c[i] - 2 * d[j - 1], 2 * d[j] - z - c[i - 1], c[i] - z)
    elif c[i - 1] >= d[j - 1] and c[i] < d[j]:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * c[i] - z - d[j - 1], d[j] - z)
    else:
        return min(z - d[j - 1], z + d[j] - 2 * c[i - 1], 2 * d[j] - z - c[i - 1], c[i] - z)


for z in x:
    print(func(s, t, z))

Solution (Python):
import bisect
A,B,Q = map(int,input().split())
a = []
b = []
x = []
for i in range(A):
  a.append(int(input()))
for j in range(B):
  b.append(int(input()))
for k in range(Q):
  x.append(int(input()))
a.insert(0,-(10**15))
a.append(10**15)
b.insert(0,-(10**15))
b.append(10**15)
for k in range(Q):  
  p = bisect.bisect_left(a,x[k])
  q = bisect.bisect_left(b,x[k])
  teraleft_right = [a[p-1],a[p]]
  jinjyaleft_right = [b[q-1],b[q]]
  minlist = []
  for i in range(2):
    for j in range(2):
      minlist.append(abs(x[k]-teraleft_right[i])+abs(teraleft_right[i]-jinjyaleft_right[j]))
      minlist.append(abs(x[k]-jinjyaleft_right[i])+abs(jinjyaleft_right[i]-teraleft_right[j]))
  minlist.sort() 
  print(minlist[0])

Solution (Python):
import bisect

a,b,q=[int(it) for it in input().split()]
dummy=10**20
s=[-dummy] + [int(input()) for it in range(a)] + [dummy]
t=[-dummy] + [int(input()) for it in range(b)] + [dummy]
x=[int(input()) for it in range(q)]

for xi in x:
    s_w = s[bisect.bisect_left(s, xi)-1]
    s_e = s[bisect.bisect_left(s, xi)]
    t_w = t[bisect.bisect_left(t, xi)-1]
    t_e = t[bisect.bisect_left(t, xi)]

    d = min(
        abs(xi - s_w) + abs(s_w - t_w),
        abs(xi - s_e) + abs(s_e - t_w),
        abs(xi - s_w) + abs(s_w - t_e),
        abs(xi - s_e) + abs(s_e - t_e),
        abs(xi - t_w) + abs(t_w - s_w),
        abs(xi - t_e) + abs(t_e - s_w),
        abs(xi - t_w) + abs(t_w - s_e),
        abs(xi - t_e) + abs(t_e - s_e)
    )
    print(d)