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

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

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

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

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

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

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

Difficulty: interview

Starter code:

Solutions:
from bisect import bisect_left

A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]

x = []
for s in S:
    idx = bisect_left(T, s)
    if idx == 0:
        x.append(abs(s-T[0]))
    elif idx == B:
        x.append(abs(s-T[-1]))
    else:
        x.append(min(abs(s-T[idx]), abs(s-T[idx-1])))

y = []
for t in T:
    idx = bisect_left(S, t)
    if idx == 0:
        y.append(abs(t-S[0]))
    elif idx == A:
        y.append(abs(t-S[-1]))
    else:
        y.append(min(abs(t-S[idx]), abs(t-S[idx-1])))

for _ in range(Q):
    X = int(input())

    sa = 0
    si = bisect_left(S, X)

    if si == 0:
        sa = x[0]+abs(S[0]-X)
    elif si == A:
        sa = x[-1]+abs(S[-1]-X)
    else:
        sa = min(x[si]+abs(S[si]-X), x[si-1]+abs(S[si-1]-X))

    ti = bisect_left(T, X)

    if ti == 0:
        ta = y[0]+abs(T[0]-X)
    elif ti == B:
        ta = y[-1]+abs(T[-1]-X)
    else:
        ta = min(y[ti]+abs(T[ti]-X), y[ti-1]+abs(T[ti-1]-X))
    
    print(min(sa, ta))

import bisect
import math

a, b, q = map(int, input().split())
s = [-math.inf]
t = [-math.inf]
x = []
for _ in range(a):
    s.append(int(input()))
for _ in range(b):
    t.append(int(input()))
for _ in range(q):
    x.append(int(input()))
s.append(math.inf)
t.append(math.inf)

for i in x:
    ws = abs(s[bisect.bisect(s, i) - 1] - i)
    if ws != math.inf:
        ws += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])-1] - s[bisect.bisect(s, i) - 1]), \
                  abs(t[bisect.bisect(t, s[bisect.bisect(s, i) - 1])] - s[bisect.bisect(s, i) - 1]))
    else:
        ws = math.inf
    
    es = abs(s[bisect.bisect(s, i)] - i)
    if es != math.inf:
        es += min(abs(t[bisect.bisect(t, s[bisect.bisect(s, i)]) -1] - s[bisect.bisect(s, i)]), \
                  abs(t[bisect.bisect(t, s[bisect.bisect(s, i)])] - s[bisect.bisect(s, i)]))
    else:
        es = math.inf
    
    wt = abs(t[bisect.bisect(t, i) - 1] - i)
    if wt != math.inf:
        wt += min(abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])-1] - t[bisect.bisect(t, i) - 1]), \
                  abs(s[bisect.bisect(s, t[bisect.bisect(t, i) - 1])] - t[bisect.bisect(t, i) - 1]))
    else:
        wt = math.inf
    
    et = abs(t[bisect.bisect(t, i)] - i)
    if et != math.inf:
        et += min(abs(s[bisect.bisect(s, t[bisect.bisect(t, i)])-1] - t[bisect.bisect(t, i)]), \
                  abs(s[bisect.bisect(s, t[bisect.bisect(t, i)])] - t[bisect.bisect(t, i)]))
    else:
        et = math.inf
        
    print(min(ws, es, wt, et))

import sys
from bisect import bisect_left

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


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


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


def resolve():
    a, b, q = list(map(int, input().split()))
    S = list(int(input()) for _ in range(a))
    T = list(int(input()) for _ in range(b))
    query = list(int(input()) for _ in range(q))

    for x in query:
        dist1 = f_inf
        s1, s2 = neighborhood(S, x)
        for s in [s1, s2]:
            if s is not None:
                t1, t2 = neighborhood(T, S[s])
                for t in [t1, t2]:
                    if t is not None:
                        dist1 = min(dist1, dist_calc(x, S[s], T[t]))

        dist2 = f_inf
        t1, t2 = neighborhood(T, x)
        for t in [t1, t2]:
            if t is not None:
                s1, s2 = neighborhood(S, T[t])
                for s in [s1, s2]:
                    if s is not None:
                        dist2 = min(dist2, dist_calc(x, S[s], T[t]))

        res = min(dist1, dist2)
        print(res)


def __starting_point():
    resolve()

__starting_point()

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

from bisect import bisect

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

    s_i = bisect(s,x)
    d1 = d2 = 10**11
    if s_i<a:
        d1 = s[s_i]-x
        t_i = bisect(t,s[s_i])
        buf = 10**11
        if t_i<b:
            buf = t[t_i]-s[s_i]
        if t_i>0:
            buf = min(buf,s[s_i]-t[t_i-1])
        d1 += buf
    if s_i>0:
        d2 = x-s[s_i-1]
        t_i = bisect(t,s[s_i-1])
        buf = 10**11
        if t_i<b:
            buf = t[t_i]-s[s_i-1]
        if t_i>0:
            buf = min(buf,s[s_i-1]-t[t_i-1])
        d2 += buf
    
    t_i = bisect(t,x)
    d3 = d4 = 10**11
    if t_i<b:
        d3 = t[t_i]-x
        s_i = bisect(s,t[t_i])
        buf = 10**11
        if s_i<a:
            buf = s[s_i]-t[t_i]
        if s_i>0:
            buf = min(buf,t[t_i]-s[s_i-1])
        d3 += buf
    if t_i>0:
        d4 = x-t[t_i-1]
        s_i = bisect(s,t[t_i-1])
        buf = 10**11
        if s_i<a:
            buf = s[s_i]-t[t_i-1]
        if s_i>0:
            buf = min(buf,t[t_i-1]-s[s_i-1])
        d4 += buf

    print(min(d1,d2,d3,d4))

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

import bisect
A, B, Q = list(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]

# print("s", s)
# print("t", t)
for q in range(Q):
    x = int(input())
    b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    # print(f"b{b}, d{d}")

    res = INF

    for S in [s[b-1], s[b]]:
        for T in [t[d-1], t[d]]:
            # print(f"S{S}, T{T}")
            d1, d2 = abs(S-x)+abs(T-S), abs(T-x)+abs(T-S)
            res = min(res, d1, d2)
    print(res)



def main():
    import bisect

    a, b, q = map(int, input().split())
    INF = float('inf')
    s = [-INF] + [int(input()) for _ in range(a)] + [INF]
    t = [-INF] + [int(input()) for _ in range(b)] + [INF]
    x = [int(input()) for _ in range(q)]

    for i in x:
        si = bisect.bisect_left(s, i)
        ti = bisect.bisect_left(t, i)
        sl, sr = i-s[si-1], s[si]-i
        tl, tr = i-t[ti-1], t[ti]-i
        print(min(max(sl,tl), sl+tr+min(sl,tr), sr+tl+min(sr,tl), max(sr,tr)))

main()

#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print(('\n'.join(x)))
def printni(x): print(('\n'.join(list(map(str,x)))))
inf = 10**17
mod = 10**9 + 7

a,b,q=MI()
lis1=[-inf]
lis2=[-inf]
for i in range(a):
    lis1.append(I())
for i in range(b):
    lis2.append(I())
lis1.append(inf)
lis2.append(inf)
#print(lis1)
#print(lis2)
for i in range(q):
    u=I()
    x=bisect_left(lis1,u)
    y=bisect_left(lis2,u)
    #print(u,x,y)
    Lll=u-min(lis1[x-1],lis2[y-1])
    Lrr=max(lis1[x],lis2[y])-u
    Llr1=2*(u-lis1[x-1])+(lis2[y]-u)
    Llr2=2*(lis2[y]-u)+u-lis1[x-1]
    Lrl1=2*(u-lis2[y-1])+lis1[x]-u
    Lrl2=2*(lis1[x]-u)+u-lis2[y-1]
    #print(Lll,Lrr,Lrl1,Lrl2,Llr1,Llr2)
    print((min(Lll,Lrr,Lrl1,Lrl2,Llr1,Llr2)))
    
    
    
    



import bisect

A, B, Q = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]

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

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

Jinja = []
Tera = []
Ichi = []
for i in range (0, A):
	Jinja.append(int(input()))
for i in range (0, B):
	Tera.append(int(input()))
for i in range (0, Q):
	Ichi.append(int(input()))

import math
import bisect as bi

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

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

import sys
from bisect import bisect


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


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


def __starting_point():
    main()

__starting_point()

from bisect import *
A,B,Q = map(int,input().split())
inf = float("inf")
S = [-inf]+[int(input()) for a in range(A)]+[inf]
T = [-inf]+[int(input()) for b in range(B)]+[inf]
X = [int(input()) for q in range(Q)]

for x in X:
  si = bisect(S,x)
  ti = bisect(T,x)
  d1 = x-S[si-1]
  d2 = S[si]-x
  d3 = x-T[ti-1]
  d4 = T[ti]-x
  print(min(2*d1+d4,d1+d4*2,2*d2+d3,d2+d3*2,max(d1,d3),max(d2,d4)))

from bisect import bisect_right
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)]
for i in range(q):
    X = x[i]
    l, r = bisect_right(s, X), bisect_right(t, X)
    ans = float("inf")
    ans = min(ans, abs(s[l - 1] - X) + abs(t[r - 1] - s[l - 1]), abs(t[r - 1] - X) + abs(s[l - 1] - t[r - 1]))
    ans = min(ans, abs(s[l - 1] - X) + abs(t[r] - s[l - 1]), abs(t[r] - X) + abs(s[l - 1] - t[r]))
    ans = min(ans, abs(s[l] - X) + abs(t[r - 1] - s[l]), abs(t[r - 1] - X) + abs(s[l] - t[r - 1]))
    ans = min(ans, abs(s[l] - X) + abs(t[r] - s[l]), abs(t[r] - X) + abs(s[l] - t[r]))
    print(ans)

import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list([int(x) - 1 for x in sys.stdin.readline().split()])


# ===CODE===

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


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

    for _ in range(q):
        ans = INF

        x = ni()
        tmpans = [0] * 4

        idxs = bisect.bisect_left(s, x)
        sml, lrg = disLR(s, idxs)
        tmpans[0] += abs(x - sml)
        tmpans[1] += abs(x - lrg)
        idxsml = bisect.bisect_left(t, sml)
        tmpans[0] += min([abs(sml - i) for i in disLR(t, idxsml)])
        idxlrg = bisect.bisect_left(t, lrg)
        tmpans[1] += min([abs(lrg - i) for i in disLR(t, idxlrg)])

        idxt = bisect.bisect_left(t, x)
        sml, lrg = disLR(t, idxt)
        tmpans[2] += abs(x - sml)
        tmpans[3] += abs(x - lrg)
        idxsml = bisect.bisect_left(s, sml)
        tmpans[2] += min([abs(sml - i) for i in disLR(s, idxsml)])
        idxlrg = bisect.bisect_left(s, lrg)
        tmpans[3] += min([abs(lrg - i) for i in disLR(s, idxlrg)])
        ans = min(ans, min(tmpans))
        print(ans)


def __starting_point():
    main()

__starting_point()

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


from bisect import bisect

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

for i in range(q):
  x=int(input())
  s_inx=bisect(s,x)
  t_inx=bisect(t,x)

  s_l=s[s_inx-1]
  s_r=s[s_inx]

  t_l=t[t_inx-1]
  t_r=t[t_inx]

  ans=10**18
  for i in [s_l,s_r]:
    for j in [t_l,t_r]:
      ans=min(ans,abs(i-x)+abs(j-i))

  for i in [t_l,t_r]:
    for j in [s_l,s_r]:
      ans=min(ans,abs(i-x)+abs(j-i))

  print(ans)

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

from bisect import bisect_left
A, B, Q = list(map(int, input().split()))
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]

s.sort()
t.sort()

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


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


ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

mx = 3*10**10
a,b,q = ma()
S = [0 for i in range(a+2)]
T = [0 for i in range(b+2)]
S[0] = -mx;S[a+1] = mx
T[0] = -mx;T[b+1] = mx
for i in range(a):
    S[i+1]=ni()
for i in range(b):
    T[i+1]=ni()

def bs(x,ls):##ls == S or T
    ok =0
    ng = len(ls)-1
    while ng-ok > 1:
        idx = (ok+ng)//2
        if ls[idx] < x:
            ok = idx
        else:
            ng = idx
    return ok  #xよりも左にあるidxを返す
def solve(x):
    ids = bs(x,S)
    s1 = S[ids] #自分より左
    s2 = S[ids+1] #自分と同じか右
    idt = bs(x,T)
    t1 = T[idt]
    t2 = T[idt+1]

    s1t1 = max(x-s1,x-t1)
    s1t2 = 2*min(x-s1,t2-x) + max(x-s1,t2-x)
    s2t1 = 2*min(s2-x,x-t1) + max(s2-x,x-t1)
    s2t2 = max(s2-x,t2-x)
    print(min(s1t1,s1t2,s2t1,s2t2))

for i in range(q):
    x = ni()
    solve(x)


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

import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list([int(x) - 1 for x in sys.stdin.readline().split()])


# ===CODE===



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

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

    for _ in range(q):
        ans = INF

        x = ni()

        idxs = bisect.bisect_left(s, x)
        a1 = disLR(s, idxs)

        idxt = bisect.bisect_left(t, x)
        a2 = disLR(t, idxt)

        for ta1 in a1:
            for ta2 in a2:
                tmp1 = abs(x - ta1) + abs(ta2 - ta1)
                tmp2 = abs(x - ta2) + abs(ta1 - ta2)
                ans = min(ans, tmp1, tmp2)

        print(ans)


def __starting_point():
    main()

__starting_point()

import bisect
A, B, Q = list(map(int, input().split()))
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]

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


import bisect
INF=10**100

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

import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: list(map(int, sys.stdin.readline().split()))
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list([int(x) - 1 for x in sys.stdin.readline().split()])


# ===CODE===

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


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

    for _ in range(q):
        ans = INF

        x = ni()

        idxs = bisect.bisect_left(s, x)
        a1 = disLR(s, idxs)

        idxt = bisect.bisect_left(t, x)
        a2 = disLR(t, idxt)

        for ta1 in a1:
            for ta2 in a2:
                tmp1 = abs(x - ta1) + abs(ta2 - ta1)
                tmp2 = abs(x - ta2) + abs(ta1 - ta2)
                ans = min(ans, tmp1, tmp2)

        print(ans)


def __starting_point():
    main()

__starting_point()

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



import bisect
A, B, Q = map(int, input().split())
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)

#!/usr/bin/env python3
import bisect

INF = 10 ** 18


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



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

def __starting_point():
  main()

__starting_point()

from bisect import bisect

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

for i in range(q):
  x=int(input())
  s_inx=bisect(s,x)
  t_inx=bisect(t,x)

  s_l=s[s_inx-1]
  s_r=s[s_inx]

  t_l=t[t_inx-1]
  t_r=t[t_inx]

  ans=10**18
  for i in [s_l,s_r]:
    for j in [t_l,t_r]:
      d1=abs(i-x)+abs(j-i)
      d2=abs(j-x)+abs(i-j)
      ans=min(ans,d1,d2)

  print(ans)

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

s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]

s.append(-100000000000)
s.append(100000000000)
t.append(-100000000000)
t.append(100000000000)
s.sort()
t.sort()

for _ in range(q):
    x = int(input())

    idxs = bisect.bisect_right(s, x)
    idxt = bisect.bisect_right(t, x)
    left_s = s[idxs - 1]
    right_s = s[idxs]
    left_t = t[idxt - 1]
    right_t = t[idxt]

    kouho1 = max(x - left_s, x - left_t)
    kouho2 = max(right_s - x, right_t - x)
    kouho3 = min(x - left_s, right_t - x) * 2 + max(x - left_s, right_t - x)
    kouho4 = min(x - left_t, right_s - x) * 2 + max(x - left_t, right_s - x)
    print((min(kouho1, kouho2, kouho3, kouho4)))


import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

A, B, Q = mi()

S = [ii() for _ in range(A)]
T = [ii() for _ in range(B)]

S_ = [S[0]] + S + [S[A-1]]
T_ = [T[0]] + T + [T[B-1]]

for _ in range(Q):
    f = ii()
    to_s = bisect.bisect_left(S, f)
    to_t = bisect.bisect_left(T, f)
    #ans_1 = ans_2 = 0
    #print(to_s, to_t)

    #ans_1 += abs(S_[to_s+1] - f)
    #ans_2 += abs(f - S_[to_s])
    #print(ans_1, ans_2)

    #ans_1 = min(ans_1 + abs(S_[to_s+1] - T_[to_t+1]), ans_1 + abs(S_[to_s+1] - T_[to_t]))
    #ans_2 = min(ans_2 + abs(S_[to_s] - T_[to_t+1]), ans_2 + abs(S_[to_s] - T_[to_t]))

    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t+1]), float('inf'))
    ans = min(abs(S_[to_s+1] - T_[to_t+1]) + abs(f- T_[to_t+1]), ans)
    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t]), ans)
    ans = min(abs(S_[to_s+1] - T_[to_t]) + abs(f- T_[to_t]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - T_[to_t+1]) + abs(f- T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t]), ans)
    ans = min(abs(S_[to_s] - T_[to_t]) + abs(f- T_[to_t]), ans)
    print(ans)


import sys
from bisect import bisect_left

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


def main():
    A, B, Q = list(map(int, readline().split()))
    S = [-INF]
    S.extend(int(readline()) for _ in range(A))
    S.append(INF)
    T = [-INF]
    T.extend(int(readline()) for _ in range(B))
    T.append(INF)
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        for s in (S[idx_s - 1], S[idx_s]):
            for t in (T[idx_t - 1], T[idx_t]):
                ans[i] = min(ans[i], abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))

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

    return


def __starting_point():
    main()

__starting_point()

from bisect import bisect_left

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

from bisect import bisect_left

a, b, q = list(map(int, input().split()))
dummy = 10 ** 18 + 1
s = [-dummy] + [int(input()) for _ in range(a)] + [dummy]
t = [-dummy] + [int(input()) for _ in range(b)] + [dummy]
x = [int(input()) for _ in range(q)]

for i in x:
    # 神社から先に行くパターン
    idx_s, idx_t = bisect_left(s, i), bisect_left(t, i)
    ans = dummy
    for j in [s[idx_s-1], s[idx_s]]:
        for k in [t[idx_t-1], t[idx_t]]:
            res1, res2 = abs(j - i) + abs(k - j), abs(k - i) + abs(j - k)
            ans = min(ans, res1, res2)
    print(ans)


def main():
    from bisect import bisect_left
    A, B, Q = list(map(int, input().split()))
    INF = 2 * 10 ** 10
    S = [-INF] + [int(input()) for _ in range(A)] + [INF]
    T = [-INF] + [int(input()) for _ in range(B)] + [INF]
    S_rev = [-s for s in S][::-1]
    T_rev = [-t for t in T][::-1]

    for q in range(Q):
        x = int(input())
        ans = 5 * 10 ** 10

        # Case1: 右に直進
        cost1_s = S[bisect_left(S, x)] - x
        cost1_t = T[bisect_left(T, x)] - x
        ans = min(ans, max(cost1_s, cost1_t))

        # Case2: 左に直進
        cost2_s = S_rev[bisect_left(S_rev, -x)] + x
        cost2_t = T_rev[bisect_left(T_rev, -x)] + x
        ans = min(ans, max(cost2_s, cost2_t))

        # Case3: 右でS、左でT
        cost3_s = S[bisect_left(S, x)] - x
        cost3_t = T_rev[bisect_left(T_rev, -x)] + x
        ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t))

        # Case4: 右でT、左でS
        cost3_s = S_rev[bisect_left(S_rev, -x)] + x
        cost3_t = T[bisect_left(T, x)] - x
        ans = min(ans, 2 * min(cost3_s, cost3_t) + max(cost3_s, cost3_t))

        print(ans)


def __starting_point():
    main()

__starting_point()

#
a,b,q=list(map(int, input().split()))
*s,=[-10**11]+[int(input()) for _ in range(a)]+[10**11]
*t,=[-10**11]+[int(input()) for _ in range(b)]+[10**11]
*x,=[int(input()) for _ in range(q)]
s.sort()
t.sort()

from bisect import bisect_left
from itertools import product
for xi in x:
    ans=10**11
    i=bisect_left(s,xi)
    j=bisect_left(t,xi)
    for u,v in product(s[i-1:i+1],t[j-1:j+1]):
        d=min(abs(u-xi),abs(v-xi))+abs(u-v)####この式が簡明
        ans=min(d,ans)
    print(ans)
    



import bisect
A,B,Q = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
S = sorted(S)
T = sorted(T)

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

import bisect
A,B,Q =list(map(int,input().split()))
s = []
t = []
for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    x=(int(input()))    
    s_index_af = bisect.bisect(s,x)
    if s_index_af == A:
        s_index_af -= 1
        s_index_bf = s_index_af
    else:
        s_index_bf = s_index_af -1

    t_index_af = bisect.bisect(t,x)
    if t_index_af == B:
        t_index_af -= 1
        t_index_bf = t_index_af
    else:
        t_index_bf = t_index_af -1
   
    jl = [s[s_index_bf]-x,s[s_index_af]-x]
    tl = [t[t_index_bf]-x,t[t_index_af]-x]

    jt = []
    jt.append(abs(jl[0])+abs(tl[0]-jl[0]))
    jt.append(abs(jl[0])+abs(tl[1]-jl[0]))
    jt.append(abs(jl[1])+abs(tl[0]-jl[1]))
    jt.append(abs(jl[1])+abs(tl[1]-jl[1]))

    jt.append(abs(tl[0])+abs(jl[0]-tl[0]))
    jt.append(abs(tl[0])+abs(jl[1]-tl[0]))
    jt.append(abs(tl[1])+abs(jl[0]-tl[1]))
    jt.append(abs(tl[1])+abs(jl[1]-tl[1]))

            
    print((min(jt)))


import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

A, B, Q = mi()

S = [ii() for _ in range(A)]
T = [ii() for _ in range(B)]

S_ = [S[0]] + S + [S[A-1]]
T_ = [T[0]] + T + [T[B-1]]

for _ in range(Q):
    f = ii()
    to_s = bisect.bisect_left(S, f)
    to_t = bisect.bisect_left(T, f)

    ans = float('inf')

    for i in [S_[to_s], S_[to_s+1]]:
        for j in [T_[to_t], T_[to_t+1]]:
            tmp = min(abs(i - f), abs(j - f)) + abs(i - j)
            ans = min(ans, tmp)
    '''
    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t+1]), float('inf'))
    ans = min(abs(S_[to_s+1] - T_[to_t+1]) + abs(f - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s+1] - f) + abs(S_[to_s+1] - T_[to_t]), ans)
    ans = min(abs(S_[to_s+1] - T_[to_t]) + abs(f - T_[to_t]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - T_[to_t+1]) + abs(f - T_[to_t+1]), ans)
    ans = min(abs(S_[to_s] - f) + abs(S_[to_s] - T_[to_t]), ans)
    ans = min(abs(S_[to_s] - T_[to_t]) + abs(f - T_[to_t]), ans)
    '''
    print(ans)

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

for _ in range(q):
    x = int(input())

    ans = INF
    # 神社→寺
    Ri = bisect.bisect_left(shrines, x)
    Li = Ri-1
    if Ri != a+1:
        R = shrines[Ri]
        Rj = bisect.bisect_left(temples, R)
        Lj = Rj-1
        d = INF
        if Rj != b+1:
            d = min(d, temples[Rj]-x)
        if Lj != 0:
            d = min(d, R-temples[Lj]+R-x)
        if d < ans:
            ans = d
    if Li != 0:
        L = shrines[Li]
        Rj = bisect.bisect_left(temples, L)
        Lj = Rj-1
        d = INF
        if Rj != b+1:
            d = min(d, temples[Rj]-L+x-L)
        if Lj != 0:
            d = min(d, x-temples[Lj])
        if d < ans:
            ans = d

    # 寺→神社
    Ri = bisect.bisect_left(temples, x)
    Li = Ri-1
    if Ri != b+1:
        R = temples[Ri]
        Rj = bisect.bisect_left(shrines, R)
        Lj = Rj-1
        d = INF
        if Rj != a+1:
            d = min(d, shrines[Rj]-x)
        if Lj != 0:
            d = min(d, R-shrines[Lj]+R-x)
        if d < ans:
            ans = d
    if Li != 0:
        L = temples[Li]
        Rj = bisect.bisect_left(shrines, L)
        Lj = Rj-1
        d = INF
        if Rj != a+1:
            d = min(d, shrines[Rj]-L+x-L)
        if Lj != 0:
            d = min(d, x-shrines[Lj])
        if d < ans:
            ans = d

    print(ans)


import bisect

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

import sys
import math
from collections import deque
import bisect

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


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


def main():
	A, B, Q = NMI()
	S = [-float("inf")] + [NI() for _ in range(A)] + [float("inf")]
	T = [-float("inf")] + [NI() for _ in range(B)] + [float("inf")]
	Q = [NI() for _ in range(Q)]

	for q in Q:
		s_idx = bisect.bisect_left(S, q)
		t_idx = bisect.bisect_left(T, q)
		sl, sr, tl, tr = S[s_idx-1], S[s_idx], T[t_idx-1], T[t_idx]
		LL = max(abs(q - sl), abs(q - tl))
		RR = max(abs(q - sr), abs(q - tr))
		LR = abs(q - sl) * 2 + abs(q - tr)
		RL = abs(q - sr) * 2 + abs(q - tl)
		LR2 = abs(q - tl) * 2 + abs(q - sr)
		RL2 = abs(q - tr) * 2 + abs(q - sl)
		res = min(LL, RR, LR, LR2, RL, RL2)
		print(res)


def __starting_point():
	main()
__starting_point()

import bisect
A,B,Q = map(int,input().split())
INF = float('inf')
S = [-INF]+[int(input()) for _ in range(A)]+[INF]
T = [-INF]+[int(input()) for _ in range(B)]+[INF]

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

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

# https://atcoder.jp/contests/abc119/tasks/abc119_d
# 

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

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

"""
sとtのどちらを先にするか
左右どちらに走るか
の都合4パターンの最小値を選べばよい
"""

def minl(x):
    # sを選択
    idx = bisect.bisect_left(s, x)
    ret = 3*10**10
    if idx == 0:
        first = s[0] - x
        now = s[0]
        idx = bisect.bisect_left(t, now)
        if idx == 0:
            second = t[0] - now
        elif idx == b:
            second = now - t[b-1]
        else:
            second = min(now - t[idx-1], t[idx]-now)
        ret = min(ret, first+second)
    elif idx == a:
        first = x - s[a-1]
        now = s[a-1]
        idx = bisect.bisect_left(t, now)
        if idx == 0:
            second = t[0] - now
        elif idx == b:
            second = now - t[b-1]
        else:
            second = min(now - t[idx-1], t[idx]-now)
        ret = min(ret, first+second)
    else:
        for first_g in [s[idx-1], s[idx]]:
            first = abs(first_g - x)
            now = first_g
            idx = bisect.bisect_left(t, now)
            if idx == 0:
                second = t[0] - now
            elif idx == b:
                second = now - t[b-1]
            else:
                second = min(now - t[idx-1], t[idx]-now)
            ret = min(ret, first+second)
    
    # tを選択
    idx = bisect.bisect_left(t, x)
    if idx == 0:
        first = t[0] - x
        now = t[0]
        idx = bisect.bisect_left(s, now)
        if idx == 0:
            second = s[0] - now
        elif idx == a:
            second = now - s[a-1]
        else:
            second = min(now - s[idx-1], s[idx]-now)
        ret = min(ret, first+second)
    elif idx == b:
        first = x - t[b-1]
        now = t[b-1]
        idx = bisect.bisect_left(s, now)
        if idx == 0:
            second = s[0] - now
        elif idx == a:
            second = now - s[a-1]
        else:
            second = min(now - s[idx-1], s[idx]-now)
        ret = min(ret, first+second)
    else:
        for first_g in [t[idx-1], t[idx]]:
            first = abs(first_g - x)
            now = first_g
            idx = bisect.bisect_left(s, now)
            if idx == 0:
                second = s[0] - now
            elif idx == a:
                second = now - s[a-1]
            else:
                second = min(now - s[idx-1], s[idx]-now)
            ret = min(ret, first+second)
    return ret

for i in range(q):
    x = int(input())
    print(minl(x))

from bisect import bisect_left

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

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


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

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

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)

import sys
from bisect import bisect_left

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


def resolve():
    a, b, q = list(map(int, input().split()))
    S = [-f_inf] + list(int(input()) for _ in range(a)) + [f_inf]
    T = [-f_inf] + list(int(input()) for _ in range(b)) + [f_inf]

    for _ in range(q):
        res = f_inf
        x = int(input())

        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)
        for s in [S[idx_s - 1], S[idx_s]]:
            for t in [T[idx_t - 1], T[idx_t]]:
                d1 = abs(s - x) + abs(s - t)
                d2 = abs(t - x) + abs(t - s)
                res = min(res, d1, d2)
        print(res)


def __starting_point():
    resolve()

__starting_point()

from bisect import bisect, bisect_left 

A, B, Q = map(int, input().split())
S, T, X = [0]*(A+2), [0]*(B+2), [0]*Q

INF = 10**10
S[0], T[0] = -INF, -INF
S[-1], T[-1] = 2*INF, 2*INF

for i in range(A):
  S[i+1] = int(input())

for i in range(B):
  T[i+1] = int(input())

for i in range(Q):
  X[i] = int(input())

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

for x in X:
  print(dis(x))

from bisect import bisect

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

for i in range(q):
  x=int(input())
  s_inx=bisect(s,x)
  t_inx=bisect(t,x)

  s_l=s[s_inx-1]
  s_r=s[s_inx]

  t_l=t[t_inx-1]
  t_r=t[t_inx]

  ans=10**18
  for i in [s_l,s_r]:
    for j in [t_l,t_r]:
      d1=abs(i-x)+abs(j-i)
      d2=abs(j-x)+abs(i-j)
      ans=min(ans,d1,d2)

  print(ans)

from bisect import bisect_left
# 初期入力
import sys
input = sys.stdin.readline  #文字列では使わない
A,B,Q = (int(i) for i in input().split())
st ={"s":[],"t":[]}
x =[0] *Q

for i in range(A):
    aa = int(input())
    st["s"].append(aa)
st["s"].append(10**11)

for i in range(B):
    bb = int(input())
    st["t"].append(bb)
st["t"].append(10**11)


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

#2分探索で近い神社と寺を探す
dist =[]
for xi in x:
    #スタート⇒神社
    xs =bisect_left(st["s"],xi)
    s_l =st["s"][xs-1] #スタート地点から近い神社（左）⓵
    s_r =st["s"][xs  ] #スタート地点から近い神社（右）⓶
    xs_l =abs(xi -s_l)  
    xs_r =abs(xi -s_r)    

    xs_l_t =bisect_left(st["t"],s_l ) 
    t_l =st["t"][xs_l_t -1] #⓵から近い寺（左）
    t_r =st["t"][xs_l_t   ] #⓵から近い寺（右）
    xs_l_t_l =abs(s_l -t_l) 
    xs_l_t_r =abs(s_l -t_r) 
    
    xs_r_t =bisect_left(st["t"],s_r ) 
    t_l =st["t"][xs_r_t -1] #⓶から近い寺（左）
    t_r =st["t"][xs_r_t   ] #⓶から近い寺（右）
    xs_r_t_l =abs(s_r -t_l) 
    xs_r_t_r =abs(s_r -t_r)
    ds =min(xs_l +xs_l_t_l ,xs_l +xs_l_t_r ,xs_r +xs_r_t_l ,xs_r +xs_r_t_r)

     #スタート⇒寺
    xs =bisect_left(st["t"],xi)
    s_l =st["t"][xs-1] #スタート地点から近い寺（左）⓷
    s_r =st["t"][xs  ] #スタート地点から近い寺（右）⓸
    xs_l =abs(xi -s_l)  
    xs_r =abs(xi -s_r)    

    xs_l_t =bisect_left(st["s"],s_l ) 
    t_l =st["s"][xs_l_t -1] #⓷から近い神社（左）
    t_r =st["s"][xs_l_t   ] #⓷から近い神社（右）
    xs_l_t_l =abs(s_l -t_l) 
    xs_l_t_r =abs(s_l -t_r) 
    
    xs_r_t =bisect_left(st["s"],s_r ) 
    t_l =st["s"][xs_r_t -1] #⓸から近い神社（左）
    t_r =st["s"][xs_r_t   ] #⓸から近い神社（右）
    xs_r_t_l =abs(s_r -t_l) 
    xs_r_t_r =abs(s_r -t_r)
    dt =min(xs_l +xs_l_t_l ,xs_l +xs_l_t_r ,xs_r +xs_r_t_l ,xs_r +xs_r_t_r)
    print((min(ds,dt)))


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

import bisect

A, B, Q=map(int, input().split())
s=[int(input()) for _ in range(A)]
t=[int(input()) for _ in range(B)]
x=[int(input()) for _ in range(Q)]

st=[0]*A
ts=[0]*B

for i in range(A):
  tmp=bisect.bisect_left(t, s[i])
  if tmp==0:
    st[i]=t[0]-s[i]
  elif tmp==B:
    st[i]=s[i]-t[-1]
  else:
    st[i]=min(s[i]-t[tmp-1], t[tmp]-s[i])
    
for i in range(B):
  tmp=bisect.bisect_left(s, t[i])
  if tmp==0:
    ts[i]=s[0]-t[i]
  elif tmp==A:
    ts[i]=t[i]-s[-1]
  else:
    ts[i]=min(t[i]-s[tmp-1], s[tmp]-t[i])

for i in range(Q):
  cand=set()
  tmp=bisect.bisect_left(s, x[i])
  if tmp>0:
    cand.add(x[i]-s[tmp-1]+st[tmp-1])
  if tmp<A:
    cand.add(s[tmp]-x[i]+st[tmp])
    
  tmp=bisect.bisect_left(t, x[i])
  if tmp>0:
    cand.add(x[i]-t[tmp-1]+ts[tmp-1])
  if tmp<B:
    cand.add(t[tmp]-x[i]+ts[tmp])
    
  print(min(cand))

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

for i in range(q):
    x = int(input())
    s_ind = bisect_left(s, x)
    t_ind = bisect_left(t, x)
    
    res = inf
    for cand_s in [s[s_ind - 1], s[s_ind]]:
        for cand_t in [t[t_ind - 1], t[t_ind]]:
            st = abs(cand_s - cand_t)
            dx = min(abs(cand_s - x), abs(cand_t - x))
            
            res = min(res, st + dx)
    
    print(res)

import sys
from bisect import bisect_left

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


def main():
    A, B, Q = list(map(int, readline().split()))
    S = [-INF] + [int(readline()) for _ in range(A)] + [INF]
    T = [-INF] + [int(readline()) for _ in range(B)] + [INF]
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        for s in (S[idx_s - 1], S[idx_s]):
            for t in (T[idx_t - 1], T[idx_t]):
                ans[i] = min(ans[i], abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))

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

    return


def __starting_point():
    main()

__starting_point()

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

s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]

import bisect

for i in range(q):
    s2 = 10**12
    t2 = 10**12
    qq = int(input())
    sp = bisect.bisect(s,qq)
    tp = bisect.bisect(t,qq)
    s1 = s[sp-1]
    if sp <a:
        s2 = s[sp]

    t1 = t[tp-1]
    if tp <b:
        t2 = t[tp]
    print(min(abs(qq-s1)+abs(s1-t1),abs(qq-s1)+abs(s1-t2),
              abs(qq-s2)+abs(s2-t1),abs(qq-s2)+abs(s2-t2),
              abs(qq-t1)+abs(t1-s1),abs(qq-t1)+abs(t1-s2),
              abs(qq-t2)+abs(t2-s1),abs(qq-t2)+abs(t2-s2)))

from bisect import bisect_left
from itertools import product
from sys import maxsize

a, b, q = list(map(int, input().split()))
s = [-maxsize] + [int(input()) for _ in range(a)] + [maxsize]
t = [-maxsize] + [int(input()) for _ in range(b)] + [maxsize]

for _ in range(q):
    x = int(input())
    i = bisect_left(s, x)
    j = bisect_left(t, x)
    res = maxsize
    for ss, tt in product((s[i-1], s[i]), (t[j-1], t[j])):
        res = min(res, abs(ss-x)+abs(tt-ss), abs(tt-x)+abs(ss-tt))
    print(res)


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


# https://img.atcoder.jp/abc119/editorial.pdf
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)

import sys
from bisect import bisect_left

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


def main():
    A, B, Q = list(map(int, readline().split()))
    S = [int(readline()) for _ in range(A)]
    T = [int(readline()) for _ in range(B)]
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    vs = [0] * 2
    vt = [0] * 2
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        vs[0] = S[idx_s - 1] if idx_s > 0 else -INF
        vt[0] = T[idx_t - 1] if idx_t > 0 else -INF
        vs[1] = S[idx_s] if idx_s < A else INF
        vt[1] = T[idx_t] if idx_t < B else INF

        for s in vs:
            for t in vt:
                ans[i] = min(ans[i], abs(x - s) + abs(s - t), abs(x - t) + abs(t - s))

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

    return


def __starting_point():
    main()

__starting_point()

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

def is_not_less_than(ls, ind, key):
    if ls[ind] >= key:
        return True
    else:
        return False
def return_min_ind_whose_value_not_less_than(ls,key):
    r = len(ls)
    l = -1
    while True:
        next_ind = (r+l) // 2
        if is_not_less_than(ls, next_ind, key):
            r = next_ind
        else:
            l = next_ind
        if r - l == 1:
            return r
n_shrine,n_temple,Q = list(map(int,input().split()))
shrine_ls = [0] * (n_shrine+1)
temple_ls = [0] * (n_temple+1)
Q_ls = [0] * Q
for i in range(n_shrine):
    shrine_ls[i] = int(input())
shrine_ls[-1] = float('inf')
for i in range(n_temple):
    temple_ls[i] = int(input())
temple_ls[-1] = float('inf')
for i in range(Q):
    Q_ls[i] = int(input())

for q in Q_ls:
    i_Rshrine = return_min_ind_whose_value_not_less_than(shrine_ls,q)
    Rshrine = shrine_ls[i_Rshrine]
    i_Lshrine = i_Rshrine-1
    Lshrine = shrine_ls[i_Lshrine]

    i_Rtemple = return_min_ind_whose_value_not_less_than(temple_ls,q)
    Rtemple = temple_ls[i_Rtemple]
    i_Ltemple = i_Rtemple-1
    Ltemple = temple_ls[i_Ltemple]
    # Temple,Shrine共に右
    RR = max(Rtemple,Rshrine) - q
    # Temple,Shrine共に左
    if i_Lshrine >= 0 and i_Ltemple >= 0:
        LL = abs(min(Ltemple,Lshrine)-q)
    else:
        LL = float('inf')
    #右templeと左shrine
    if i_Lshrine >= 0:
        RtLs = 2*min(Rtemple-q,q-Lshrine) + max(Rtemple-q,q-Lshrine)
    else:
        RtLs = float('inf')
    # 左templeと右shrine
    if i_Ltemple >= 0:
        LtRs = 2*min(Rshrine-q,q-Ltemple) + max(Rshrine-q,q-Ltemple)
    else:
        LtRs = float('inf')

    print((min(LL,RR,RtLs,LtRs)))



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

# coding: utf-8

import bisect

A,B,Q = map(int, input().split())
*S, = [int(input()) for _ in range(A)]
*T, = [int(input()) for _ in range(B)]
*X, = [int(input()) for _ in range(Q)]

for x in X:
    # 2分探索で、「現在地」と神社群の位置関係を探る
    sx = bisect.bisect(S, x)
    # 現在地から見て東側で最も近い神社
    try:
        se = S[sx]
    except:
        # 東側に存在しない場合は、神社群の中での最東
        se = S[-1]
    # 現在地から見て西側で最も近い神社
    try:
        sw = S[sx-1]
    except:
        # 西側に存在しない場合は、神社群の中での最西
        sw = S[0]

    tx = bisect.bisect(T, x)
    # 現在地から見て東側で最も近いお寺
    try:
        te = T[tx]
    except:
        # 東側に存在しない場合は、お寺群の中での最東
        te = T[-1]
    # 現在地から見て西側で最も近いお寺
    try:
        tw = T[tx-1]
    except:
        # 西側に存在しない場合は、お寺群の中での最西
        tw = T[0]

    # 候補 (8通り) をシミュレーション
    # 東神社 -> 東寺
    # 東寺 -> 東神社
    # 東神社 -> 西寺
    # 西寺 -> 東神社
    # 西神社 -> 東寺
    # 東寺 -> 西神社
    # 西神社 -> 西寺
    # 西寺 -> 西神社
    routes = []
    for s in [se, sw]:
        for t in [te, tw]:
            routes.append(abs(s - x)+abs(t - s))
            routes.append(abs(t - x)+abs(s - t))

    # 最小距離を選ぶ
    print(min(routes))

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

import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    A, B, Q = LI()
    s = [I() for _ in range(A)]
    t = [I() for _ in range(B)]

    # xから一番近い神社に行く→その神社から一番近い寺に行く、またはその逆の2パターン

    # 各神社に対する一番近い寺の距離を求めておく
    len_of_nearest_t = [INF] * A
    for i in range(A):
        idx = bisect.bisect_left(t, s[i])
        len_r = t[idx] - s[i] if 0 <= idx < B else INF
        len_l = s[i] - t[idx-1] if 0 <= idx - 1 < B else INF
        len_of_nearest_t[i] = min(len_l, len_r)
    # print(len_of_nearest_t)

    # 各寺に対する一番近い神社の距離を求めておく
    len_of_nearest_s = [INF] * B
    for i in range(B):
        idx = bisect.bisect_left(s, t[i])
        len_r = s[idx] - t[i] if 0 <= idx < A else INF
        len_l = t[i] - s[idx-1] if 0 <= idx - 1 < A else INF
        len_of_nearest_s[i] = min(len_l, len_r)
    # print(len_of_nearest_s)

    for _ in range(Q):
        x = I()
        # 神社→寺
        idx = bisect.bisect_left(s, x)
        len_r = s[idx] - x + len_of_nearest_t[idx] if 0 <= idx < A else INF
        len_l = x - s[idx-1] + len_of_nearest_t[idx-1] if 0 <= idx - 1 < A else INF
        len_x_s_t = min(len_r, len_l)
        # 寺→神社
        idx = bisect.bisect_left(t, x)
        len_r = t[idx] - x + len_of_nearest_s[idx] if 0 <= idx < B else INF
        len_l = x - t[idx-1] + len_of_nearest_s[idx-1] if 0 <= idx - 1 < B else INF
        len_x_t_s = min(len_r, len_l)
        print((min(len_x_s_t, len_x_t_s)))

def __starting_point():
    resolve()

__starting_point()

#!/usr/bin/env python3
from bisect import bisect_right

def main():
    A, B, Q = list(map(int, input().split()))
    s = [int(input()) for _ in range(A)]
    t = [int(input()) for _ in range(B)]
    # print(s,t)
    s = [-10**12] + s + [10**12]
    t = [-10**12] + t + [10**12]

    for _ in range(Q):
        x = int(input())
        i = bisect_right(s,x)
        j = bisect_right(t,x)
        # print(i,j,x)
        ans = []

        ans.append(max(abs(s[i-1] - x), abs(t[j-1] - x)))
        ans.append(max(abs(s[i] - x), abs(t[j] - x)))
        ans.append(abs(s[i-1] - x) + abs(t[j] - x) + min(abs(s[i-1]-x), abs(t[j]-x)))
        ans.append(abs(s[i] - x) + abs(t[j-1] - x) + min(abs(s[i]-x), abs(t[j-1]-x)))

        print((min(ans)))

def __starting_point():
    main()

__starting_point()

import sys
from bisect import bisect_left

input = sys.stdin.readline


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


def main():
    A, B, Q = list(map(int, input().split()))
    S = [0] * (A + 2)
    T = [0] * (B + 2)
    X = [0] * Q
    for i in range(1, A + 1):
        S[i] = int(input())
    for i in range(1, B + 1):
        T[i] = int(input())
    for i in range(Q):
        X[i] = int(input())

    S[0] = T[0] = -10 ** 11
    S[-1] = T[-1] = 10 ** 11

    ans = solve(Q, S, T, X)
    print(("\n".join(map(str, ans))))


def __starting_point():
    main()

__starting_point()

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

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

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

import bisect

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

s = [- 10 ** 19]
for i in range(a):
    s.append(int(input()))
s.append(10 ** 19)

t = [- 10 ** 19]
for i in range(b):
    t.append(int(input()))
t.append(10 ** 19)

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


from bisect import bisect

A, B, Q = list(map(int, input().split()))
INF = 10 ** 18
S = [-INF]
T = [-INF]
for i in range(A):
    S.append(int(input()))
for i in range(B):
    T.append(int(input()))
S.append(INF)
T.append(INF)
for i in range(Q):
    x = int(input())
    sr = bisect(S, x)
    sl = sr - 1
    tr = bisect(T, x)
    tl = tr - 1
    def d(y): return abs(x - y)
    ans = min(d(max(S[sr], T[tr])), d(min(S[sl], T[tl])))
    ans = min(ans, 2 * d(S[sr]) + d(T[tl]))
    ans = min(ans, d(S[sr]) + 2 * d(T[tl]))
    ans = min(ans, 2 * d(S[sl]) + d(T[tr]))
    ans = min(ans, d(S[sl]) + 2 * d(T[tr]))
    print(ans)


import bisect

A, B, Q = list(map(int, input().split()))
INF = 10**18
S = [-INF] + [int(input()) for i in range(A)] + [INF]
T = [-INF] + [int(input()) for i in range(B)] + [INF]
X = [int(input()) for i in range(Q)]


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


import sys
from bisect import bisect_left

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


def main():
    A, B, Q = list(map(int, readline().split()))
    S = [int(readline()) for _ in range(A)]
    T = [int(readline()) for _ in range(B)]
    X = [int(readline()) for _ in range(Q)]

    ans = [0] * Q
    vs = [0] * 2
    vt = [0] * 2
    for i, x in enumerate(X):
        idx_s = bisect_left(S, x)
        idx_t = bisect_left(T, x)

        vs[0] = S[idx_s - 1] if idx_s > 0 else -INF
        vt[0] = T[idx_t - 1] if idx_t > 0 else -INF
        vs[1] = S[idx_s] if idx_s < A else INF
        vt[1] = T[idx_t] if idx_t < B else INF

        ans[i] = min(
            abs(x - vs[0]) + abs(vs[0] - vt[0]),
            abs(x - vt[0]) + abs(vt[0] - vs[0]),
            abs(x - vs[0]) + abs(vs[0] - vt[1]),
            abs(x - vt[0]) + abs(vt[0] - vs[1]),
            abs(x - vs[1]) + abs(vs[1] - vt[1]),
            abs(x - vt[1]) + abs(vt[1] - vs[1]),
            abs(x - vs[1]) + abs(vs[1] - vt[0]),
            abs(x - vt[1]) + abs(vt[1] - vs[0]),
        )

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

    return


def __starting_point():
    main()

__starting_point()

import bisect

a, b, q = map(int, input().split())
INF = float('inf')
s = [-INF] + [int(input()) for _ in range(a)] + [INF]
t = [-INF] + [int(input()) for _ in range(b)] + [INF]
x = [int(input()) for _ in range(q)]

for i in x:
    si = bisect.bisect_left(s, i)
    ti = bisect.bisect_left(t, i)
    sl, sr = i-s[si-1], s[si]-i
    tl, tr = i-t[ti-1], t[ti]-i
    print(min(max(sl,tl), sl+tr+min(sl,tr), sr+tl+min(sr,tl), max(sr,tr)))

#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return list(map(int, input().split()))
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print(('\n'.join(x)))
def printni(x): print(('\n'.join(list(map(str,x)))))
inf = 10**17
mod = 10**9 + 7

a,b,q=MI()
lis1=[I() for i in range(a)]
lis2=[I() for i in range(b)]
lis1.append(inf)
lis2.append(inf)
lis1=[-inf]+lis1
lis2=[-inf]+lis2
#print(lis1)
#print(lis2)
for i in range(q):
    u=I()
    x=bisect_left(lis1,u)
    y=bisect_left(lis2,u)
    #print(u,x,y)
    Lll=u-min(lis1[x-1],lis2[y-1])
    Lrr=max(lis1[x],lis2[y])-u
    Llr1=2*(u-lis1[x-1])+(lis2[y]-u)
    Llr2=2*(lis2[y]-u)+u-lis1[x-1]
    Lrl1=2*(u-lis2[y-1])+lis1[x]-u
    Lrl2=2*(lis1[x]-u)+u-lis2[y-1]
    #print(Lll,Lrr,Lrl1,Lrl2,Llr1,Llr2)
    print((min(Lll,Lrr,Lrl1,Lrl2,Llr1,Llr2)))
    
    
    
    



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

import bisect

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

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

import sys
from bisect import bisect_left

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


def main():
    A, B, Q = list(map(int, readline().split()))
    S = [-INF]
    S.extend(int(readline()) for _ in range(A))
    S.append(INF)
    T = [-INF]
    T.extend(int(readline()) for _ in range(B))
    T.append(INF)
    X = [int(readline()) for _ in range(Q)]

    ans = [INF] * Q
    for i, x in enumerate(X):
        i1 = bisect_left(S, x)
        i2 = bisect_left(T, x)

        ans[i] = min(
            abs(x - S[i1 - 1]) + abs(S[i1 - 1] - T[i2 - 1]),
            abs(x - T[i2 - 1]) + abs(T[i2 - 1] - S[i1 - 1]),
            abs(x - S[i1 - 1]) + abs(S[i1 - 1] - T[i2]),
            abs(x - T[i2 - 1]) + abs(T[i2 - 1] - S[i1]),
            abs(x - S[i1]) + abs(S[i1] - T[i2]),
            abs(x - T[i2]) + abs(T[i2] - S[i1]),
            abs(x - S[i1]) + abs(S[i1] - T[i2 - 1]),
            abs(x - T[i2]) + abs(T[i2] - S[i1 - 1]),
        )

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

    return


def __starting_point():
    main()

__starting_point()

import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
from functools import reduce
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return list(map(int,input().split()))
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

A, B, Q = I()
inf = float("inf")
s = [-inf]+sorted([k() for _ in range(A)])+[inf]
t = [-inf]+sorted([k() for _ in range(B)])+[inf]
aa = []

for i in range(Q):
    q = k()
    S = bisect.bisect(s,q)
    T = bisect.bisect(t,q)
    ans = inf
    for j in [s[S-1],s[S]]:
        for z in [t[T-1],t[T]]:
            x = abs(j-q)+abs(j-z)
            y = abs(z-q)+abs(j-z)
            ans = min(ans,x,y)
    aa.append(ans)

for i in aa:
    print(i)


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

import bisect
A, B, Q = map(int, input().split())
s = list(int(input()) for _ in range(A))
t = list(int(input()) for _ in range(B))

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

    ttmp1 = bisect.bisect_left(t, x)
    for j in range(2):
        if ttmp1 == 0 and j == 1:
            continue
        if ttmp1 == B and j == 0:
            continue
        stmp1 = bisect.bisect_left(s, t[ttmp1 - j])
        ktmp = 10 ** 11
        if stmp1 != 0:
            ktmp = min(ktmp, abs(t[ttmp1 - j] - s[stmp1 - 1]))
        if stmp1 != A:
            ktmp = min(ktmp, abs(t[ttmp1 - j] - s[stmp1]))
        total = abs(x - t[ttmp1 - j]) + ktmp
        ans = min(ans, total)
    print(ans)