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


"""
Created on 2019/2/24
Solved on 2019/2/24-25
@author: shinjisu
"""



def getInt(): return int(input())


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


def zeros(n): return [0]*n


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


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat



def zeros2(n, m):
    d2 = zeros(n)
    for i in range(n):
        d2[i] = zeros(m)
    return d2


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**12


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x





def lookup_v1(val, lst):
    
    for i in range(len(lst)):
        if val <= lst[i]:
            return i
    else:
        return len(lst)


def solve_v1(pos):
    global S, T
    dmp(pos,'pos')
    minD = INF
    
    i1 = lookup(pos, S)
    i2 = lookup(S[i1-1], T)
    d1 = abs(T[i2-1]-S[i1-1]) + abs(S[i1-1]-pos)
    d2 = abs(T[i2]-S[i1-1]) + abs(S[i1-1]-pos)
    minD = min(minD, d1, d2)
    dmp(minD,'minD')
    i2 = lookup(S[i1], T)
    d1 = abs(T[i2-1]-S[i1]) + abs(S[i1]-pos)
    d2 = abs(T[i2]-S[i1]) + abs(S[i1]-pos)
    minD = min(minD, d1, d2)
    dmp(minD,'minD')
    
    i1 = lookup(pos, T)
    i2 = lookup(T[i1-1], S)
    d1 = abs(S[i2-1]-T[i1-1]) + abs(T[i1-1]-pos)
    d2 = abs(S[i2]-T[i1-1]) + abs(T[i1-1]-pos)
    minD = min(minD, d1, d2)
    dmp(minD,'minD')
    i2 = lookup(T[i1], S)
    d1 = abs(S[i2-1]-T[i1]) + abs(T[i1]-pos)
    d2 = abs(S[i2]-T[i1]) + abs(T[i1]-pos) 
    minD = min(minD, d1, d2)
    dmp(minD,'minD')
    return minD



def getMinDist_v2(level, pos, ST, TS):
    dmp((level,pos),'level,pos')
    if level == 2:
        return INF
    minD = INF
    
    is1, is2 = lookup(pos, ST)
    it1, it2 = lookup(ST[is1], TS)
    d1 = abs(TS[it1]-ST[is1]) + abs(ST[is1]-pos)
    d2 = abs(TS[it2]-ST[is1]) + abs(ST[is1]-pos)
    minD = min(minD, d1, d2)
    dmp(minD,'minD')
    if is1 != is2:
        dmp(is2, 'is2')
        it1, it2 = lookup(ST[is2], TS)
        d1 = abs(TS[it1]-ST[is2]) + abs(ST[is2]-pos)
        d2 = abs(TS[it2]-ST[is2]) + abs(ST[is2]-pos)
        minD = min(minD, d1, d2)
        dmp(minD,'minD')
    return min(minD, getMinDist(level+1, pos, TS, ST))


def solve_v2(pos):
    global S, T
    return getMinDist(0, pos, S, T)



def lookup(val, lst):
    dmp((val, lst), 'lookup')
    left = 0
    right = len(lst) - 1
    if val < lst[0]:
        return 0, 0
    elif val > lst[-1]:
        return len(lst)-1, len(lst)-1
    while left+1 < right:
        mid = (left+right) // 2
        if val <= lst[mid]:
            right = mid
        else:
            left = mid
    dmp((left, right))
    return left, right


def getMinDist(level, pos, ST, TS):
    dmp((level,pos),'level,pos')
    if level == 2:
        return INF
    minD = INF
    
    d1 = abs(TS[it1]-ST[is1]) + abs(ST[is1]-pos)
    d2 = abs(TS[it2]-ST[is1]) + abs(ST[is1]-pos)
    minD = min(minD, d1, d2)
    dmp(minD,'minD')
    if is1 != is2:
        dmp(is2, 'is2')
        d1 = abs(TS[it1]-ST[is2]) + abs(ST[is2]-pos)
        d2 = abs(TS[it2]-ST[is2]) + abs(ST[is2]-pos)
        minD = min(minD, d1, d2)
        dmp(minD,'minD')
    return min(minD, getMinDist(level+1, pos, TS, ST))


def solve(pos):
    is1, is2 = lookup(pos, S)
    it1, it2 = lookup(pos, T)
    minD = INF
    d1 = abs(T[it1]-S[is1]) + abs(S[is1]-pos)
    d2 = abs(T[it2]-S[is1]) + abs(S[is1]-pos)
    d3 = abs(T[it1]-S[is2]) + abs(S[is2]-pos)
    d4 = abs(T[it2]-S[is2]) + abs(S[is2]-pos)
    d5 = abs(S[is1]-T[it1]) + abs(T[it1]-pos)
    d6 = abs(S[is2]-T[it1]) + abs(T[it1]-pos)
    d7 = abs(S[is1]-T[it2]) + abs(T[it2]-pos)
    d8 = abs(S[is2]-T[it2]) + abs(T[it2]-pos)
    minD = min(minD, d1, d2, d3, d4, d5, d6, d7, d8)

    dmp(minD)
    return minD


def probD():
    global S, T
    A, B, Q = getIntList()
    S = getIntLines(A)
    T = getIntLines(B)
    X = getIntLines(Q)
    dmp((A, B, Q), 'A, B, Q')
    dmp(S,'S')
    dmp(T,'T')
    dmp(X,'X')
    S = [-INF] + S + [INF]
    T = [-INF] + T + [INF]
    for x in X:
        print(solve(x))
    return ''


debug = False  
print(probD())


def probA():
    S = input()
    dmp(S)
    y = int(S[0:4])
    m = int(S[5:7])
    d = int(S[8:10])
    dmp((y,m,d))
    HEI = 'Heisei'
    if y <= 2018:
        return HEI
    if m <= 4:
        return HEI
    else:
        return 'TBD'


def probB():
    N = getInt()
    dmp(N)
    sm = 0.0
    for i in range(N):
        x, u = [a for a in input().split()]
        dmp((x,u))
        if u == 'JPY':
            x = int(x)
        else:
            x = float(x) * 380000.0
        sm += x
    return sm



def loop(level, a, b, c, mp):
    global N, L, A, B, C, minMP
    if level == N:
        if min(a, b, c) == 0:
            return INF
        mp += abs(A-a) + abs(B-b) + abs(C-c) - 30  
        dmp((minMP, mp), 'min, MP')
        minMP = min(minMP,mp)
        return minMP
    else:
        mp0 = loop(level+1, a, b, c, mp)
        mp1 = loop(level+1, a+L[level], b, c, mp+10)
        mp2 = loop(level+1, a, b+L[level], c, mp+10)
        mp3 = loop(level+1, a, b, c+L[level], mp+10)
        dmp((mp0, mp1, mp2, mp3))
        return min(mp0, mp1, mp2, mp3)



def probC():
    global N, L, A, B, C, minMP

    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C ))
    dmp(L)
    d = zeros(3)
    d = A, B, C
    dmp(d)
    minMP = INF
    return loop(0, 0, 0, 0, 0)


def gen2Bits(n):  
    bits = zeros(n)
    for i in range(4**n):
        for k in range(n):
            bits[k] = i % 4
            i //= 4
        yield bits



def probC_v2():
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C))
    dmp(L)
    d = zeros(3)
    d = A, B, C
    dmp(d)
    minMP = INF
    for f in gen2Bits(N):
        dmp(f)
        mp = 0
        lg = zeros(4)
        for i in range(N):
            if lg[f[i]] > 0 and f[i] < 3:
                mp += 10
            lg[f[i]] += L[i]
            
        
        if min(lg[:3]) == 0:  
            continue
        for i in range(3):
            mp += abs(lg[i]-d[i])
        dmp((minMP, mp), 'min, MP')
        minMP = min(minMP, mp)
    return minMP


def probC_v1():
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C))
    dmp(L)
    d = zeros(3)
    d[0] = A
    d[1] = B
    d[2] = C
    dmp(d)
    minMP = INF
    for f in gen2Bits(N):
        dmp(f)
        mp = 0
        lg = zeros(4)
        for i in range(N):
            if lg[f[i]] > 0 and f[i] < 3:
                mp += 10
            lg[f[i]] += L[i]
            
        
        allUsed = True
        for i in range(3):  
            if lg[i] == 0:
                allUsed = False
                break
            mp += abs(lg[i]-d[i])
        if not allUsed:
            continue
        dmp((minMP, mp))
        minMP = min(minMP, mp)
    return minMP

Solution (Python):
import sys
from bisect import bisect_left
a,b,q = map(int, sys.stdin.readline().split())
S = []
for _ in range(a):
  S.append(int(sys.stdin.readline()))
T = []
for _ in range(b):
  T.append(int(sys.stdin.readline()))
X = []
for _ in range(q):
  X.append(int(sys.stdin.readline()))

INF = 10 ** 11
def dfs(direction, tors, pos, count):
  if tors == 1:
    L = T
  elif tors == -1:
    L = S
  i = bisect_left(L, pos)
  if i == len(L):
    if direction == 1:
      return INF
    elif direction == -1:
      res = abs(L[i-1]-pos)
      next_pos = L[i-1]
  elif L[i] == pos:
    res = 0
    next_pos = pos
  else:
    if direction == 1:
      res = abs(L[i]-pos)
      next_pos = L[i]
    elif direction == -1:
      if i == 0:
        return INF

      res = abs(L[i-1]-pos)
      next_pos = L[i-1]

  if count == 1:
    return res

  res0 = res + dfs(direction, tors*(-1), next_pos, count-1)
  res1 = res + dfs(direction*(-1), tors*(-1), next_pos, count-1)

  return min(res0, res1)

for x in X:
  res0 = dfs(1, 1, x, 2)
  res1 = dfs(-1, 1, x, 2)
  res2 = dfs(1, -1, x, 2)
  res3 = dfs(-1, -1, x, 2)

  sys.stdout.write('{}\n'.format(min(res0, res1, res2, res3)))

Solution (Python):
import sys
from bisect import bisect_left
a,b,q = map(int, sys.stdin.readline().split())
S = []
for _ in range(a):
  S.append(int(sys.stdin.readline()))
T = []
for _ in range(b):
  T.append(int(sys.stdin.readline()))
X = []
for _ in range(q):
  X.append(int(sys.stdin.readline()))

INF = 10 ** 11
def dfs(direction, tors, pos, count):
  if tors == 1:
    L = T
  elif tors == -1:
    L = S
  i = bisect_left(L, pos)
  if i == len(L):
    if direction == 1:
      return INF
    elif direction == -1:
      res = abs(L[i-1]-pos)
      next_pos = L[i-1]
  elif L[i] == pos:
    res = 0
    next_pos = pos
  else:
    if direction == 1:
      res = abs(L[i]-pos)
      next_pos = L[i]
    elif direction == -1:
      if i == 0:
        return INF

      res = abs(L[i-1]-pos)
      next_pos = L[i-1]

  if count == 1:
    return res

  res0 = res + dfs(direction, tors*(-1), next_pos, count-1)
  res1 = res + dfs(direction*(-1), tors*(-1), next_pos, count-1)

  return min(res0, res1)

for x in X:
  res0 = dfs(1, 1, x, 2)
  res1 = dfs(-1, 1, x, 2)
  res2 = dfs(1, -1, x, 2)
  res3 = dfs(-1, -1, x, 2)

  print(min(res0, res1, res2, res3))

Solution (Python):
import bisect

INF = 2 ** 60

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

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

Solution (Python):
import bisect

INF = 2 ** 60

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

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

Solution (Python):
import bisect

A,B,Q = map(int,input().split())
INF = 10**12
S = [-INF]+[int(input()) for i in range(A)]+[INF]
T = [-INF]+[int(input()) for i in range(B)]+[INF]
for i in range(Q) :
    x = int(input())
    
    right_s = S[bisect.bisect_right(S,x)]
    right_t = T[bisect.bisect_right(T,x)]
    left_s = S[bisect.bisect_right(S,x) - 1]
    left_t = T[bisect.bisect_right(T,x) - 1]
    
    dist = []
    
    for i in [left_s,right_s] :
        for j in [left_t,right_t] :
            d = min(abs(x-i), abs(x-j)) + abs(i-j)
            dist.append(d)
    ans = min(dist)
    
    print(ans)

Solution (Python):
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
loc = [int(input()) for i in range(Q)]
for i in range(len(loc)):
    x = loc[i]
    res = INF
    s_w, t_w = bisect.bisect_right(s,x), bisect.bisect_right(t,x)
    d1 = abs(s[s_w]-x) + abs(t[t_w]-s[s_w])
    d2 = abs(t[t_w]-x) + abs(s[s_w]-t[t_w])
    d3 = abs(s[s_w]-x) + abs(t[t_w-1]-s[s_w])
    d4 = abs(t[t_w-1]-x) + abs(t[t_w-1]-s[s_w])
    d5 = abs(s[s_w-1]-x) + abs(t[t_w]-s[s_w-1])
    d6 = abs(t[t_w]-x) + abs(s[s_w-1]-t[t_w])
    d7 = abs(s[s_w-1]-x) + abs(t[t_w-1]-s[s_w-1])
    d8 = abs(t[t_w-1]-x) + abs(s[s_w-1]-t[t_w-1])
    print(min(d1,d2,d3,d4,d5,d6,d7,d8))

Solution (Python):
def search(x, l, first, end):
    if end-first<=1:
        if x<l[first]:
            return -1
        elif x>l[end]:
            return end
        else:
            return first
    middle = (first+end)//2
    if x >= l[middle]:
        return search(x, l, middle, end)
    else:
        return search(x, l, first, middle)
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]
s.sort()
t.sort()
answer = []
for i in range(Q):
    x = int(input())
    sk = search(x,s,0,A)
    tk = search(x,t,0,B)
    ptn1 = abs(s[sk]-x) + min(abs(t[tk]-s[sk]),abs(t[tk+1]-s[sk]))
    ptn2 = abs(s[sk+1]-x) + min(abs(t[tk]-s[sk+1]),abs(t[tk+1]-s[sk+1]))
    ptn3 = abs(t[tk]-x) + min(abs(s[sk]-t[tk]),abs(s[sk+1]-t[tk]))
    ptn4 = abs(t[tk+1]-x) + min(abs(s[sk]-t[tk+1]),abs(s[sk+1]-t[tk+1]))
    answer.append(min(ptn1,ptn2,ptn3,ptn4))
for i in answer:
    print(i)

Solution (Python):
import bisect as bi
a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
u = [int(input()) for i in range(q)]

for x in u:
    c = []
    d = []
    tmp = bi.bisect_left(s, x)
    if tmp != a : c.append(s[tmp])
    if tmp != 0 : c.append(s[tmp-1])
    tmp = bi.bisect_left(t, x)
    if tmp != b : d.append(t[tmp])
    if tmp != 0 : d.append(t[tmp-1])
    ans = 2**64
    for e in c:
        for f in d:
            g = abs(x-e)
            h = abs(x-f)
            if (x-e)*(x-f) > 0:
                ans = min(ans, max(g, h))
            else:
                if g > h : g,h = h,g
                ans = min(ans, 2*g+h)
    print(ans)

Solution (Python):
import bisect as bi
a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
u = [int(input()) for i in range(q)]

for x in u:
    c = []
    d = []
    tmp = bi.bisect_left(s, x)
    if tmp != a : c.append(s[tmp])
    if tmp != 0 : c.append(s[tmp-1])
    tmp = bi.bisect_left(t, x)
    if tmp != b : d.append(t[tmp])
    if tmp != 0 : d.append(t[tmp-1])
    ans = 2**64
    for e in c:
        for f in d:
            g = abs(x-e)
            h = abs(x-f)
            if (x-e)*(x-f) > 0:
                ans = min(ans, max(g, h))
            else:
                if g > h : g,h = h,g
                ans = min(ans, 2*g+h)
    print(ans)

Solution (Python):
import sys
from bisect import bisect_left
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)]
X=[int(input()) for _ in range(q)]
INF=float('inf')
S=[-INF]+S+[INF]
T=[-INF]+T+[INF]
for x in X:
    s_e_idx=bisect_left(S,x)
    s_e,s_w=S[s_e_idx],S[s_e_idx-1]
    t_e_idx=bisect_left(T,x)
    t_e,t_w=T[t_e_idx],T[t_e_idx - 1]
    d=INF
    for s in [s_w,s_e]:
        for t in [t_w,t_e]:
            d_st=abs(s-x)+abs(t-s)
            d_ts=abs(t-x)+abs(s-t)
            d=min(d,d_st,d_ts)
    print(d)

Solution (Python):
import bisect
import itertools
 
a, b, q = map(int, input().split())
ss, tt = [], []
for _ in range(a):
  ss.append(int(input()))
for _ in range(b):
  tt.append(int(input()))
  
for _ in range(q):
  x = int(input())
  md = pow(10, 15)
  xs, xt = bisect.bisect_left(ss, x), bisect.bisect_left(tt, x)
  ls, rs, lt, rt = ss[max(0, xs-1)], ss[min(a-1, xs)], tt[max(0, xt-1)], tt[min(b-1, xt)]
  for s, t in itertools.product([ls, rs], [lt, rt]):
    if s > t:
      if t > x:
        md = s - x if s - x < md else md
      elif s > x:
        if md > 2 * (s-x) + x-t:
          md = 2 * (s-x) + x-t
        if md > 2 * (x-t) + s-x:
          md = 2 * (x-t) + s-x
      else:
        md = x - t if x - t < md else md
    else:  
      if s > x:
        md = t - x if t - x < md else md
      elif t > x:
        if md > 2 * (x-s) + t-x:
          md = 2 * (x-s) + t-x
        if md > 2 * (t-x) + x-s:
          md = 2 * (t-x) + x-s
      else:
        md = x - s if x - s < md else md
  print(md)

Solution (Python):
inf = 1 << 60
A,B,Q = map(int,input().split())
s = []
t = []
x = []
s.append(-inf*2)
s.append(-inf)
t.append(-inf*2)
t.append(-inf)
for i in range(A):
    s.append(int(input()))
for i in range(B):
    t.append(int(input()))
for i in range(Q):
    x.append(int(input()))
s.append(inf)
s.append(inf*2)
t.append(inf)
t.append(inf*2)

def search(lst, target):
    min1 = 0
    max1 = len(lst)
    while max1 - min1 > 1:
        mid1 = (min1 + max1) // 2
        if lst[mid1] <= target:
            min1 = mid1
        else:
            max1 = mid1
    return min1

def solve(x):
    chk = [0] * 8
    s1 = search(s, x)

    chk[0] = abs(s[s1] - x)
    chk[1] = abs(s[s1] - x)
    chk[2] = abs(s[s1+1] - x)
    chk[3] = abs(s[s1+1] - x)

    t1 = search(t, s[s1])
    t2 = search(t, s[s1+1])
    chk[0] += abs(t[t1] - s[s1])
    chk[1] += abs(t[t1+1] - s[s1])
    chk[2] += abs(t[t2] - s[s1+1])
    chk[3] += abs(t[t2+1] - s[s1+1])

    t1 = search(t, x)

    chk[4] = abs(t[t1] - x)
    chk[5] = abs(t[t1] - x)
    chk[6] = abs(t[t1+1] - x)
    chk[7] = abs(t[t1+1] - x)

    s1 = search(s, t[t1])
    s2 = search(s, t[t1+1])
    chk[4] += abs(s[s1] - t[t1])
    chk[5] += abs(s[s1+1] - t[t1])
    chk[6] += abs(s[s2] - t[t1+1])
    chk[7] += abs(s[s2+1] - t[t1+1])

    return min(chk)

for xi in x:
    ans = solve(xi)
    print(ans)

Solution (Python):
A, B, Q = map(long, raw_input().split())
S = [long(raw_input()) for _ in range(A)]
T = [long(raw_input()) for _ in range(B)]
X = [long(raw_input()) for _ in range(Q)]
INF = 10**11

def meguru_bs(array, x):
  left = -1
  right = len(array)
  while right - left>1:
    mid = (left+right) / 2
    if array[mid] >= x:
      right = mid
    else:
      left = mid
  return right

for x in X:
  s_right = meguru_bs(S, x)
  s_nearests = [s_right-1, s_right]
  t_right = meguru_bs(T, x)
  t_nearests = [t_right-1, t_right]
  ans = INF
  for s in s_nearests:
    if s < 0 or s >= A:
      continue
    for t in t_nearests:
      if t < 0 or t >= B:
        continue
      ans = min(ans, abs(S[s]-x)+abs(T[t]-S[s]),abs(T[t]-x)+abs(S[s]-T[t]))
  print ans

Solution (Python):
A, B, Q = map(long, raw_input().split())
S = [long(raw_input()) for _ in range(A)]
T = [long(raw_input()) for _ in range(B)]
X = [long(raw_input()) for _ in range(Q)]
INF = 10**11
S = [-INF]+S+[INF]
T = [-INF]+T+[INF]

def meguru_bs(array, x):
  left = 0 
  right = len(array)-1 
  while right - left>1:
    mid = (left+right) / 2
    if array[mid] >= x:
      right = mid
    else:
      left = mid
  return right

for x in X:
  s_right = meguru_bs(S, x)
  s_nearests = [s_right-1, s_right]
  t_right = meguru_bs(T, x)
  t_nearests = [t_right-1, t_right]
  ans = INF
  for s in s_nearests:
    for t in t_nearests:
      ans = min(ans, abs(S[s]-x)+abs(T[t]-S[s]),abs(T[t]-x)+abs(S[s]-T[t]))
  print ans

Solution (Python):
import bisect
import itertools

a, b, q = map(int, input().split())
ss, tt = [], []
for _ in range(a):
  ss.append(int(input()))
for _ in range(b):
  tt.append(int(input()))
  
for _ in range(q):
  x = int(input())
  md = pow(10, 15)
  xs, xt = bisect.bisect_left(ss, x), bisect.bisect_left(tt, x)
  ls, rs, lt, rt = ss[max(0, xs-1)], ss[min(a-1, xs)], tt[max(0, xt-1)], tt[min(b-1, xt)]
  for s, t in itertools.product([ls, rs], [lt, rt]):
    if s > t:
      if t > x:
        md = s - x if s - x < md else md
      elif s > x:
        if md > 2 * (s-x) + x-t:
          md = 2 * (s-x) + x-t
        if md > 2 * (x-t) + s-x:
          md = 2 * (x-t) + s-x
      else:
        md = x - t if x - t < md else md
    else:  
      if s > x:
        md = t - x if t - x < md else md
      elif t > x:
        if md > 2 * (x-s) + t-x:
          md = 2 * (x-s) + t-x
        if md > 2 * (t-x) + x-s:
          md = 2 * (t-x) + x-s
      else:
        md = x - s if x - s < md else md
  print(md)

Solution (Python):
import bisect
A, B, Q = map(int, input().split())
ss = [int(input()) for i in range(A)]
ts = [int(input()) for i in range(B)]
xs = [int(input()) for i in range(Q)]
INF = 10**11

ss = [-INF] + ss + [INF]
ts = [-INF] + ts + [INF]
 

for x in xs:
  s_e_ix = bisect.bisect_left(ss, x)
  s_e = ss[s_e_ix]
  s_w = ss[s_e_ix - 1]
  
  t_e_ix = bisect.bisect_left(ts, x)
  t_e = ts[t_e_ix]
  t_w = ts[t_e_ix - 1]
  
  d = INF
  for s in [s_w, s_e]:
    for t in [t_w, t_e]:
      d0 = abs(s-x) + abs(t-s)
      d1 = abs(t-x) + abs(s-t)
      d = min(d0, d1, d)
  print(d)

Solution (Python):
import bisect
A, B, Q = map(int, input().split())
s = [-10**11] + [int(input()) for i in range(A)] + [10**11]
t = [-10**11] + [int(input()) for i in range(B)] + [10**11]

'''
def nibutans(x, l, r):
	if r - l <= 1:
		return max(r, l)
	nibu = (r + l) // 2
	return nibutans(x, nibu, r) if x > s[nibu] else nibutans(x, l, nibu)

def nibutant(x, l, r):
	if r - l <= 1:
		return max(r, l)
	nibu = (r + l) // 2
	return nibutant(x, nibu, r) if x > t[nibu] else nibutant(x, l, nibu)

for i in range(Q):
	x = int(input())
	sr, tr = nibutans(x, 0, A + 1), nibutant(x, 0, B + 1)
	ans = 10**17
	for S in [s[sr - 1], s[sr]]:
		for T in [t[tr - 1], t[tr]]:
			d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)

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

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

a, b, q = map(int,input().split())
a_lst,b_lst = [],[]
for _ in range(a):
    a_lst.append(int(input()))
for _ in range(b):
    b_lst.append(int(input()))

for _ in range(q):
    n = int(input())
    a_index = bisect.bisect_left(a_lst, n)
    b_index = bisect.bisect_left(b_lst, n)
    a_next = 10 ** 100 if a_index == a else a_lst[a_index] - n
    b_next = 10 ** 100 if b_index == b else b_lst[b_index] - n
    a_bef = 10 ** 100 if a_index == 0 else n - a_lst[a_index-1]
    b_bef = 10** 100 if b_index == 0 else n - b_lst[b_index-1]
    
    if a_next >= b_next:
        ans = min(a_next, 2*b_next + a_bef)
    else:
        ans = min(b_next, 2*a_next + b_bef)
    if a_bef >= b_bef:
        ans = min(ans, a_bef, 2*b_bef + a_next)
    else:
        ans = min(ans, b_bef, 2*a_bef + b_next)
    print(ans)

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

Solution (Python):

import itertools
import collections
import bisect

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

    for x in X:
        pos_s = bisect.bisect(S, x)  
        pos_t = bisect.bisect(T, x)  
        s1, s2 = S[pos_s - 1], S[pos_s]
        t1, t2 = T[pos_t - 1], T[pos_t]

        ans = INF
        for st, tt in itertools.product([s1, s2], [t1, t2]):
            path1 = abs(x - st) + abs(st - tt)
            path2 = abs(x - tt) + abs(st - tt)
            ans = min(ans, path1, path2)
        print(ans)

if __name__ == '__main__':
    main()

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

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

Solution (Python):

import bisect,sys
A,B,Q=map(int,raw_input().split())
s=[0]*A
t=[0]*B
for i in range(A):    
    s[i]=int(raw_input())
for i in range(B):
    t[i]=int(raw_input())
buf=[]
def f(x):
    sl=bisect.bisect_left(s,x)-1
    sr=sl+1
    tl=bisect.bisect_left(t,x)-1
    tr=tl+1
    ans=float("inf")
    if sl!=-1 and tl!=-1:
        A=max(s[sl],t[tl])
        B=min(s[sl],t[tl])
        L=abs(x-A)+abs(A-B)
        ans=min(ans,L)
    if sl!=-1 and tr!=len(t):
        L=abs(x-s[sl])*2 + abs(t[tr]-x)        
        ans=min(ans,L)
        ans=min(ans,abs(x-s[sl])+abs(t[tr]-x)*2)
    if tl!=-1 and sr!=len(s):
        ans=min(ans,abs(x-t[tl])*2+abs(s[sr]-x))
        ans=min(ans,abs(x-t[tl])+abs(s[sr]-x)*2)
    if tr!=len(t) and sr!=len(s):
        A=min(s[sr],t[tr])
        B=max(s[sr],t[tr])
        L=abs(A-x)+abs(B-A)
        ans=min(ans,L)
    return str(ans)


for i in xrange(Q):
    x=int(raw_input())
    ans=f(x)
    print(ans)

Solution (Python):
import bisect

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

for x_i in x:
    ss = bisect.bisect_left(s, x_i)
    tt = bisect.bisect_left(t, x_i)

    sc = [ss - 1, ss]
    tc = [tt - 1, tt]
    optimum = 10 ** 19
    for s_i in sc:
        for t_i in tc:
            dist = abs(s[s_i] - x_i) + abs(s[s_i] - t[t_i])
            if dist < optimum:
                optimum = dist
            dist = abs(t[t_i] - x_i) + abs(t[t_i] - s[s_i])
            if dist < optimum:
                optimum = dist
    print(optimum)