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):
A,B,Q=map(int,input().split())
s=[int(input()) for _ in range(A)]
t=[int(input()) for _ in range(B)]
xx=[int(input()) for _ in range(Q)]


def bin_search(x,seq):
    a=0
    b=len(seq)-1
    for i in range(len(seq)):
        center=((a+b)//2)
        if x>=seq[center]:
            a=center
        else:
            b=center
        if b-a<=1:
            break
    return seq[a],seq[b]
 
for i in range(Q):
    x=xx[i]
    Nb_s0,Nb_s1=bin_search(x,s)
    Nb_t0,Nb_t1=bin_search(x,t)
    p1=abs(Nb_s0-x)
    p2=abs(Nb_s1-x)
    p3=abs(Nb_t0-x)
    p4=abs(Nb_t1-x)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Solution (Python):

import sys
A,B,Q = map(int,(input().split()))
si = [-(10**10)]+[int(input()) for _ in range(A)]+[2*(10**10)] 
ti = [-(10**10)]+[int(input()) for _ in range(B)]+[2*(10**10)]

*xi, = map(int,"".join(sys.stdin.readlines()).split())
A +=2
B +=2

for x in xi:
    l = 0
    r = A-1
    while l+1<r:
        i = (l+r)//2
        if si[i]<x:
            l = i
        else:
            r = i
    sl = si[l]
    sr = si[r]
    
    l = 0
    r = B-1
    while l+1<r:
        i = (l+r)//2
        if ti[i]<x:
            l = i
        else:
            r = i
    tl = ti[l]
    tr = ti[r]
    dsl = x-sl
    dsr = sr-x
    dtl = x-tl
    dtr = tr-x
    print(min(max(dsr,dtr),max(dsl,dtl),dsl+dtr+min(dsl,dtr),dsr+dtl+min(dsr,dtl)))

Solution (Python):

A,B,Q = map(int,(input().split()))
si = [-(10**10)]+[int(input()) for _ in range(A)]+[2*(10**10)] 
ti = [-(10**10)]+[int(input()) for _ in range(B)]+[2*(10**10)]
xi = [int(input()) for i in range(Q)]
A +=2
B +=2

for x in xi:
    l = 0
    r = A-1
    while l+1<r:
        i = (l+r)//2
        if si[i]<x:
            l = i
        else:
            r = i
    sl = si[l]
    sr = si[r]
    
    l = 0
    r = B-1
    while l+1<r:
        i = (l+r)//2
        if ti[i]<x:
            l = i
        else:
            r = i
    tl = ti[l]
    tr = ti[r]
    dsl = x-sl
    dsr = sr-x
    dtl = x-tl
    dtr = tr-x
    print(min(max(dsr,dtr),max(dsl,dtl),dsl+dtr+min(dsl,dtr),dsr+dtl+min(dsr,dtl)))

Solution (Python):
inf=10**11
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=[[i,int(input())] for i in range(Q)]

x.sort(key=lambda x:x[1])
i,s_i,t_i=0,0,0
ans=[]

for i in range(Q):
    while(1):
        if x[i][1]>=s[s_i]:
            s_i+=1
        else:
            break
    while(1):
        if x[i][1]>=t[t_i]:
            t_i+=1
        else:
            break
    ans.append([x[i][0],x[i][1],s[s_i-1],s[s_i],t[t_i-1],t[t_i]])
ans.sort(key=lambda x:x[0])
for i in range(Q):
    xx=ans[i][1]
    Nb_s0=ans[i][2]
    Nb_s1=ans[i][3]
    Nb_t0=ans[i][4]
    Nb_t1=ans[i][5]

    p1=abs(Nb_s0-xx)
    p2=abs(Nb_s1-xx)
    p3=abs(Nb_t0-xx)
    p4=abs(Nb_t1-xx)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Solution (Python):
inf=10**11
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=[[i,int(input())] for i in range(Q)]

x.sort(key=lambda x:x[1])
i,s_i,t_i=0,0,0
ans=[]

for i in range(Q):
    while(1):
        if x[i][1]>=s[s_i]:
            s_i+=1
        else:
            break
    while(1):
        if x[i][1]>=t[t_i]:
            t_i+=1
        else:
            break
    ans.append([x[i][0],x[i][1],s[s_i-1],s[s_i],t[t_i-1],t[t_i]])
ans.sort(key=lambda x:x[0])
for i in range(Q):
    xx=ans[i][1]
    Nb_s0=ans[i][2]
    Nb_s1=ans[i][3]
    Nb_t0=ans[i][4]
    Nb_t1=ans[i][5]

    p1=abs(Nb_s0-xx)
    p2=abs(Nb_s1-xx)
    p3=abs(Nb_t0-xx)
    p4=abs(Nb_t1-xx)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Solution (Python):
inf=10**11
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=[[i,int(input())] for i in range(Q)]

x.sort(key=lambda x:x[1])
i,s_i,t_i=0,0,0
ans=[]

for i in range(Q):
    while(1):
        if x[i][1]>=s[s_i]:
            s_i+=1
        else:
            break
    while(1):
        if x[i][1]>=t[t_i]:
            t_i+=1
        else:
            break
    ans.append([x[i][0],x[i][1],s[s_i-1],s[s_i],t[t_i-1],t[t_i]])
ans.sort(key=lambda x:x[0])
for i in range(Q):
    xx=ans[i][1]
    Nb_s0=ans[i][2]
    Nb_s1=ans[i][3]
    Nb_t0=ans[i][4]
    Nb_t1=ans[i][5]

    p1=abs(Nb_s0-xx)
    p2=abs(Nb_s1-xx)
    p3=abs(Nb_t0-xx)
    p4=abs(Nb_t1-xx)
    q1=abs(Nb_t0-Nb_s0)
    q2=abs(Nb_t0-Nb_s1)
    q3=abs(Nb_t1-Nb_s0)
    q4=abs(Nb_t1-Nb_s1)
    print(min(p1+q1,p2+q2,p1+q3,p2+q4,p3+q1,p4+q4,p3+q2,p4+q3))

Solution (Python):
import bisect
import sys
INF = float('inf')
input = sys.stdin.readline
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())
  idx = bisect.bisect_left(s, x)
  s1 = [s[idx - 1], s[idx]]
  idx = bisect.bisect_left(t, x)
  t1 = [t[idx - 1], t[idx]]
  ans = INF
  for shr in s1:
    for tem in t1:
      ans = min(ans, abs(tem - shr) + abs(shr - x))
      ans = min(ans, abs(shr - tem) + abs(tem - x))
  print(ans)

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

Solution (Python):
import bisect
import sys

def input():
    return sys.stdin.readline()

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

shrines = []
for i in range(A):
    shrines.append(int(input()))

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

shortest_s_to_t = {}
for shrine in shrines:
    index = bisect.bisect_left(temples, shrine)
    if index == 0:
        distance = abs(shrine - temples[index])
        shortest_s_to_t[shrine] = distance
    elif index == B:
        distance = abs(shrine - temples[index - 1])
        shortest_s_to_t[shrine] = distance
    else:
        distance_left = abs(shrine - temples[index - 1])
        distance_right = abs(shrine - temples[index])
        shortest_s_to_t[shrine] = min(distance_left, distance_right)

shortest_t_to_s = {}
for temple in temples:
    index = bisect.bisect_left(shrines, temple)
    if index == 0:
        distance = abs(temple - shrines[index])
        shortest_t_to_s[temple] = distance
    elif index == A:
        distance = abs(temple - shrines[index - 1])
        shortest_t_to_s[temple] = distance
    else:
        distance_left = abs(temple - shrines[index - 1])
        distance_right = abs(temple - shrines[index])
        shortest_t_to_s[temple] = min(distance_left, distance_right)

for i in range(Q):
    x = int(input())

    
    index = bisect.bisect_left(shrines, x)
    ans = 10**11
    if index > 0:
        left_shrine = shrines[index - 1]
        candidate = abs(x - left_shrine) + shortest_s_to_t[left_shrine]
        ans = min(ans, candidate)
    if index < A:
        right_shrine = shrines[index]
        candidate = abs(x - right_shrine) + shortest_s_to_t[right_shrine]
        ans = min(ans, candidate)

    
    index = bisect.bisect_left(temples, x)
    if index > 0:
        left_temple = temples[index - 1]
        candidate = abs(x - left_temple) + shortest_t_to_s[left_temple]
        ans = min(ans, candidate)
    if index < B:
        right_temple = temples[index]
        candidate = abs(x - right_temple) + shortest_t_to_s[right_temple]
        ans = min(ans, candidate)

    print(ans)

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

a, b, q =  [ int(v) for v in input().split() ]

temple_list = [ int(input()) for i in range(a) ]
shrine_list = [ int(input()) for i in range(b) ]


def excursion(x):
	global temple_list, shine_list
	temple_west = bisect.bisect_left(temple_list,x)
	shrine_west = bisect.bisect_left(shrine_list,x)

	tw, te, sw, se = temple_list[(temple_west-1)%a],temple_list[temple_west%a],shrine_list[(shrine_west-1)%b],shrine_list[(shrine_west)%b]
	print(min( abs(x-tw) + min(abs(sw-tw),abs(tw-se)) , abs(te-x) + min(abs(se-te),abs(te-sw)), abs(x-sw) + min(abs(tw-sw),abs(sw-te)) , abs(se-x) + min(abs(te-se),abs(se-tw))))
	return 


for i in range(q):
	excursion(int(input()))

Solution (Python):
import bisect

a, b, q =  [ int(v) for v in input().split() ]

temple_list = [ int(input()) for i in range(a) ]
shrine_list = [ int(input()) for i in range(b) ]


def excursion(x):
	global temple_list, shine_list
	temple_west = bisect.bisect_left(temple_list,x)
	shrine_west = bisect.bisect_left(shrine_list,x)

	tw, te, sw, se = temple_list[(temple_west-1)%a],temple_list[temple_west%a],shrine_list[(shrine_west-1)%b],shrine_list[(shrine_west)%b]
	print(min( abs(x-tw) + min(abs(sw-tw),abs(tw-se)) , abs(te-x) + min(abs(se-te),abs(te-sw)), abs(x-sw) + min(abs(tw-sw),abs(sw-te)) , abs(se-x) + min(abs(te-se),abs(se-tw))))
	return 


for i in range(q):
	excursion(int(input()))

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

Solution (Python):
from bisect import bisect_right, bisect_left
A, B, Q = map(int, input().split())
INF = float('inf')
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())
    b = bisect_right(S, x)
    d = bisect_right(T, x)
    ans = INF
    ans = min(ans, abs(x - S[b - 1]) + abs(S[b - 1] - T[d - 1]))
    ans = min(ans, abs(x - S[b]) + abs(S[b] - T[d - 1]))
    ans = min(ans, abs(x - S[b - 1]) + abs(S[b - 1] - T[d]))
    ans = min(ans, abs(x - S[b]) + abs(S[b] - T[d]))
    ans = min(ans, abs(x - T[d - 1]) + abs(T[d - 1] - S[b - 1]))
    ans = min(ans, abs(x - T[d]) + abs(T[d] - S[b - 1]))
    ans = min(ans, abs(x - T[d - 1]) + abs(T[d - 1] - S[b]))
    ans = min(ans, abs(x - T[d]) + abs(T[d] - S[b]))
    print(ans)

Solution (Python):
import bisect
import 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 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):
INF = 10**11
from bisect import bisect
import sys
readline = sys.stdin.readline

A, B, Q = map(int, readline().split())
s_array = [int(readline()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(readline()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(readline()) for i in range(Q)]



ans_array = []
for q in q_array:
    s_index = bisect(s_array, q) - 1
    t_index = bisect(t_array, q) - 1
    
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(str(min(method_a, method_b, method_c, method_d)) + "\n")

sys.stdout.writelines(ans_array)

Solution (Python):
INF = 10**11
from bisect import bisect

A, B, Q = map(int, input().split())
s_array = [int(input()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(input()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(input()) for i in range(Q)]



ans_array = []
for q in q_array:
    s_index = bisect(s_array, q) - 1
    t_index = bisect(t_array, q) - 1
    
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(min(method_a, method_b, method_c, method_d))

for ans in ans_array:
    print(ans)

Solution (Python):
INF = 10**11


def binary_search(array, value):
    start = 0
    end = len(array) - 1
    while end - start >= 2:
        tmp = (start + end) // 2
        
        if array[tmp] <= value:
            start = tmp
        else:
            end = tmp
        
    return start


A, B, Q = map(int, input().split())
s_array = [int(input()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(input()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(input()) for i in range(Q)]



ans_array = []
for q in q_array:
    s_index = binary_search(s_array, q)
    t_index = binary_search(t_array, q)
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(min(method_a, method_b, method_c, method_d))

for ans in ans_array:
    print(ans)

Solution (Python):
INF = 10**11


def binary_search(array, value):
    start = 0
    end = len(array) - 1
    while end - start >= 2:
        tmp = (start + end) // 2
        
        if array[tmp] <= value:
            start = tmp
        else:
            end = tmp
        
    return start


A, B, Q = map(int, input().split())
s_array = [int(input()) if i != 0 else -INF for i in range(A + 1)]
s_array.append(INF)
t_array = [int(input()) if i != 0 else -INF for i in range(B + 1)]
t_array.append(INF)
q_array = [int(input()) for i in range(Q)]



ans_array = []
for q in q_array:
    s_index = binary_search(s_array, q)
    t_index = binary_search(t_array, q)
    method_a = q - min(s_array[s_index], t_array[t_index])
    method_b = max(s_array[s_index + 1], t_array[t_index + 1]) - q
    method_c = (q - s_array[s_index]) + (t_array[t_index + 1] - q) + min(
        (q - s_array[s_index]), (t_array[t_index + 1] - q))
    method_d = (q - t_array[t_index]) + (s_array[s_index + 1] - q) + min(
        (q - t_array[t_index]), (s_array[s_index + 1] - q))
    ans_array.append(min(method_a, method_b, method_c, method_d))

for ans in ans_array:
    print(ans)

Solution (Python):

class AvlTree:  
    def __init__(self, values=None, sorted_=False):
        if values is None:
            self.left = [-1]
            self.right = [-1]
            self.values = [-float("inf")]
            self.diff = [0]  
            self.size_l = [0]
        else:
            if not sorted_:
                values.sort()
            n = len(values)
            self_left = self.left = [-1] * (n+1)
            self_right = self.right = [-1] * (n+1)
            self_values = self.values = [-float("inf")] + values
            self_diff = self.diff = [0] * (n+1)  
            self_size_l = self.size_l = [0] * (n+1)

            st = [[1, n+1, 0]]
            while len(st) > 0:
                l, r, idx_par = st.pop()  
                c = (l+r) // 2  
                if self_values[c] < self_values[idx_par]:
                    self_left[idx_par] = c
                else:
                    self_right[idx_par] = c
                siz = r - l
                if siz & -siz == siz != 1:  
                    self_diff[c] = 1
                siz_l = c - l
                self_size_l[c] = siz_l
                if siz_l > 0:
                    st.append([l, c, c])
                    c1 = c + 1
                    if c1 < r:  
                        st.append([c1, r, c])

    
    
    
    
    
    

    def rotate_right(self, idx_par, lr):  
        idx = self.left[idx_par] if lr==0 else self.right[idx_par]
        idx_l = self.left[idx]
        assert idx_l != -1  

        
        self.size_l[idx] -= self.size_l[idx_l] + 1

        
        a = self.diff[idx]
        b = self.diff[idx_l]  
        if b >= 0:
            na = a - 1 - b
            if na >= 0:
                nb = b - 1
            else:
                nb = a - 2
        else:
            na = a - 1
            if na >= 0:
                nb = b - 1
            else:
                nb = a + b - 2
        self.diff[idx] = na
        self.diff[idx_l] = nb

        
        self.left[idx] = self.right[idx_l]
        self.right[idx_l] = idx
        if lr==0:
            self.left[idx_par] = idx_l
        else:
            self.right[idx_par] = idx_l

    def rotate_left(self, idx_par, lr):  
        idx = self.left[idx_par] if lr==0 else self.right[idx_par]
        idx_r = self.right[idx]
        assert idx_r != -1  

        
        self.size_l[idx_r] += self.size_l[idx] + 1

        
        a = self.diff[idx]
        b = self.diff[idx_r]  
        if b <= 0:
            na = a + 1 - b
            if na <= 0:
                nb = b + 1
            else:
                nb = a + 2
        else:
            na = a + 1
            if na <= 0:
                nb = b + 1
            else:
                nb = a + b + 2
        self.diff[idx] = na
        self.diff[idx_r] = nb

        
        self.right[idx] = self.left[idx_r]
        self.left[idx_r] = idx
        if lr == 0:
            self.left[idx_par] = idx_r
        else:
            self.right[idx_par] = idx_r

    def add(self, x):  
        idx = 0
        path = []
        path_left = []
        while idx != -1:
            path.append(idx)
            value = self.values[idx]
            if x == value:
                return  
            elif x < value:
                path_left.append(idx)
                idx = self.left[idx]
            else:
                idx = self.right[idx]

        if x < value:
            self.left[path[-1]] = len(self.values)
        else:
            self.right[path[-1]] = len(self.values)
        self.left.append(-1)
        self.right.append(-1)
        self.values.append(x)
        self.diff.append(0)
        self.size_l.append(0)

        for idx_ in path_left:
            self.size_l[idx_] += 1

        self.diff[path[-1]] += 1 if x < value else -1
        for idx, idx_par in zip(path[:0:-1], path[-2::-1]):
            diff = self.diff[idx]
            if diff == 0:
                return
            elif diff == 2:  
                idx_l = self.left[idx]  
                if self.diff[idx_l] == -1:  
                    self.rotate_left(idx, 0)
                self.rotate_right(idx_par, self.right[idx_par]==idx)
                return
            elif diff == -2:  
                idx_r = self.right[idx]  
                if self.diff[idx_r] == 1:  
                    self.rotate_right(idx, 1)
                self.rotate_left(idx_par, self.right[idx_par]==idx)
                return
            else:
                self.diff[idx_par] += 1 if self.left[idx_par]==idx else -1

    def remove(self):  
        raise NotImplementedError

    def __contains__(self, x):  
        raise NotImplementedError

    def bisect_left(self, x):  
        self_left = self.left
        self_right = self.right
        self_values = self.values
        self_size_l = self.size_l

        idx = self_right[0]
        res = 0
        while idx != -1:
            value = self_values[idx]
            if value == x:
                return res + self_size_l[idx]
            elif value < x:
                res += self_size_l[idx] + 1
                idx = self_right[idx]
            else:
                idx = self_left[idx]
        return res

    def bisect_right(self):  
        raise NotImplementedError


from bisect import bisect_left
import sys
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)]
avl_S = AvlTree(S, True)
avl_T = AvlTree(T, True)

for _ in range(Q):
    x = int(input())
    p = avl_S.bisect_left(x)
    m = avl_T.bisect_left(x)

    mi = 1<<60
    if p < A:
        if m < B:
            mi = min(mi, max(T[m], S[p]) - x)
        if 0 < m:
            mi = min(mi, min(S[p] - x, x - T[m - 1]) + S[p] - T[m - 1])
    if 0 < p:
        if m < B:
            mi = min(mi, T[m] - S[p - 1] + min(T[m] - x, x - S[p - 1]))
        if 0 < m:
            mi = min(mi, x - min(T[m - 1], S[p - 1]))
    print(mi)

Solution (Python):

class AvlTree:  
    def __init__(self, values=None, sorted_=False):
        if values is None:
            self.left = [-1]
            self.right = [-1]
            self.values = [-float("inf")]
            self.diff = [0]  
            self.size_l = [0]
        else:
            if not sorted_:
                values.sort()
            n = len(values)
            self_left = self.left = [-1] * (n+1)
            self_right = self.right = [-1] * (n+1)
            self_values = self.values = [-float("inf")] + values
            self_diff = self.diff = [0] * (n+1)  
            self_size_l = self.size_l = [0] * (n+1)

            st = [[1, n+1, 0]]
            while len(st) > 0:
                l, r, idx_par = st.pop()  
                c = (l+r) // 2  
                if self_values[c] < self_values[idx_par]:
                    self_left[idx_par] = c
                else:
                    self_right[idx_par] = c
                siz = r - l
                if siz & -siz == siz != 1:  
                    self_diff[c] = 1
                siz_l = c - l
                self_size_l[c] = siz_l
                if siz_l > 0:
                    st.append([l, c, c])
                    c1 = c + 1
                    if c1 < r:  
                        st.append([c1, r, c])

    
    
    
    
    
    

    def rotate_right(self, idx_par, lr):  
        idx = self.left[idx_par] if lr==0 else self.right[idx_par]
        idx_l = self.left[idx]
        assert idx_l != -1  

        
        self.size_l[idx] -= self.size_l[idx_l] + 1

        
        a = self.diff[idx]
        b = self.diff[idx_l]  
        if b >= 0:
            na = a - 1 - b
            if na >= 0:
                nb = b - 1
            else:
                nb = a - 2
        else:
            na = a - 1
            if na >= 0:
                nb = b - 1
            else:
                nb = a + b - 2
        self.diff[idx] = na
        self.diff[idx_l] = nb

        
        self.left[idx] = self.right[idx_l]
        self.right[idx_l] = idx
        if lr==0:
            self.left[idx_par] = idx_l
        else:
            self.right[idx_par] = idx_l

    def rotate_left(self, idx_par, lr):  
        idx = self.left[idx_par] if lr==0 else self.right[idx_par]
        idx_r = self.right[idx]
        assert idx_r != -1  

        
        self.size_l[idx_r] += self.size_l[idx] + 1

        
        a = self.diff[idx]
        b = self.diff[idx_r]  
        if b <= 0:
            na = a + 1 - b
            if na <= 0:
                nb = b + 1
            else:
                nb = a + 2
        else:
            na = a + 1
            if na <= 0:
                nb = b + 1
            else:
                nb = a + b + 2
        self.diff[idx] = na
        self.diff[idx_r] = nb

        
        self.right[idx] = self.left[idx_r]
        self.left[idx_r] = idx
        if lr == 0:
            self.left[idx_par] = idx_r
        else:
            self.right[idx_par] = idx_r

    def add(self, x):  
        idx = 0
        path = []
        path_left = []
        while idx != -1:
            path.append(idx)
            value = self.values[idx]
            if x == value:
                return  
            elif x < value:
                path_left.append(idx)
                idx = self.left[idx]
            else:
                idx = self.right[idx]

        if x < value:
            self.left[path[-1]] = len(self.values)
        else:
            self.right[path[-1]] = len(self.values)
        self.left.append(-1)
        self.right.append(-1)
        self.values.append(x)
        self.diff.append(0)
        self.size_l.append(0)

        for idx_ in path_left:
            self.size_l[idx_] += 1

        self.diff[path[-1]] += 1 if x < value else -1
        for idx, idx_par in zip(path[:0:-1], path[-2::-1]):
            diff = self.diff[idx]
            if diff == 0:
                return
            elif diff == 2:  
                idx_l = self.left[idx]  
                if self.diff[idx_l] == -1:  
                    self.rotate_left(idx, 0)
                self.rotate_right(idx_par, self.right[idx_par]==idx)
                return
            elif diff == -2:  
                idx_r = self.right[idx]  
                if self.diff[idx_r] == 1:  
                    self.rotate_right(idx, 1)
                self.rotate_left(idx_par, self.right[idx_par]==idx)
                return
            else:
                self.diff[idx_par] += 1 if self.left[idx_par]==idx else -1

    def remove(self):  
        raise NotImplementedError

    def __contains__(self, x):  
        raise NotImplementedError

    def bisect_left(self, x):  
        self_left = self.left
        self_right = self.right
        self_values = self.values
        self_size_l = self.size_l

        idx = self_right[0]
        res = 0
        while idx != -1:
            value = self_values[idx]
            if value == x:
                return res + self_size_l[idx]
            elif value < x:
                res += self_size_l[idx] + 1
                idx = self_right[idx]
            else:
                idx = self_left[idx]
        return res

    def bisect_right(self):  
        raise NotImplementedError


from bisect import bisect_left
import sys
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)]
avl_S = AvlTree(S, True)
avl_T = AvlTree(T, True)

for _ in range(Q):
    x = int(input())
    p = avl_S.bisect_left(x)
    m = avl_T.bisect_left(x)

    mi = 1<<60
    if p < A:
        if m < B:
            mi = min(mi, max(T[m], S[p]) - x)
        if 0 <= m - 1:
            mi = min(mi, min(S[p] - x, x - T[m - 1]) + S[p] - T[m - 1])
    if 0 <= p - 1:
        if m < B:
            mi = min(mi, T[m] - S[p - 1] + min(T[m] - x, x - S[p - 1]))
        if 0 <= m - 1:
            mi = min(mi, x - min(T[m - 1], S[p - 1]))
    print(mi)