Buggy python submission (verdict: Time Limit Exceeded):
A,B,Q = map(int,input().split())
INF = 10**18
s=[-INF]
t=[-INF]
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
# s.sort()
# t.sort()
s.append(INF)
t.append(INF)
def binSearch(value,upper,lower,array):
    ng = upper
    ok = lower
    while ng>ok+1:
        mid=(ok+ng)//2
        if(array[mid]<value):
            ok=mid
        else:
            ng=mid
    return(ok)

def solve(x):
    I = binSearch(x,A,-1,s)
    J = binSearch(x,B,-1,t)
    candidates=[]
    candidates.append(abs(x-s[I])+abs(s[I]-t[J]))
    candidates.append(abs(x-t[J])+abs(t[J]-s[I]))
    candidates.append(abs(x-s[I+1])+abs(s[I+1]-t[J]))
    candidates.append(abs(x-t[J])+abs(s[I+1]-t[J]))
    candidates.append(abs(x-s[I])+abs(s[I]-t[J+1]))
    candidates.append(abs(x-t[J+1])+abs(s[I]-t[J+1]))
    candidates.append(abs(x-s[I+1])+abs(s[I+1]-t[J+1]))
    candidates.append(abs(x-t[J+1])+abs(s[I+1]-t[J+1]))
    return(min(candidates))

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

Fixed submission:
import bisect
A,B,Q = map(int,input().split())
INF = 10**18
# s=[-INF]
# t=[-INF]
# for _ in range(A):
    # s.append(int(input()))
# for _ in range(B):
    # t.append(int(input()))
# s.sort()
# t.sort()
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
# s.append(INF)
# t.append(INF)

def solve(x):
    sr = bisect.bisect(s,x)
    tr = bisect.bisect(t,x)
    sRight = s[sr] if sr < A else s[sr-1]
    tRight = t[tr] if tr < B else t[tr-1]
    sLeft = s[sr-1] if sr>0 else s[sr] 
    tLeft = t[tr-1] if tr>0 else t[tr]
    # return(min(
    # print([[sr,sLeft,sRight],[tr,tLeft,tRight]])
    cand = [
        abs(x-sRight) + abs(sRight-tRight),
        abs(x-sRight) + abs(sRight-tLeft),
        abs(x-tRight) + abs(tRight-sRight),
        abs(x-tRight) + abs(tRight-sLeft),
        abs(x-sLeft) + abs(sLeft-tLeft),
        abs(x-sLeft) + abs(sLeft-tRight),
        abs(x-tLeft) + abs(tLeft-sRight),
        abs(x-tLeft) + abs(tLeft-sLeft),
    ]
    # print(cand)
    return(min(cand))
for _ in range(Q):
    print(solve(int(input())))

Buggy python submission (verdict: Time Limit Exceeded):
A,B,Q = map(int,input().split())
s=[]
t=[]
for _ in range(A):
    s.append(int(input()))
for _ in range(B):
    t.append(int(input()))
s.sort()
t.sort()
def binSearch(value,upper,lower,array):
    ng = upper
    ok = lower
    while ng>ok+1:
        mid=(ok+ng)//2
        if(array[mid]<=value):
            ok=mid
        else:
            ng=mid
    return(ok)

def solve(x):
    I = binSearch(x,A,-1,s)
    J = binSearch(x,B,-1,t)
    candidates=[]
    candidates.append(abs(x-s[I])+abs(s[I]-t[J]))
    candidates.append(abs(x-t[J])+abs(t[J]-s[I]))
    if I+1<A:
        candidates.append(abs(x-s[I+1])+abs(s[I+1]-t[J]))
        candidates.append(abs(x-t[J])+abs(s[I+1]-t[J]))
    if J+1<B:
        candidates.append(abs(x-s[I])+abs(s[I]-t[J+1]))
        candidates.append(abs(x-t[J+1])+abs(s[I]-t[J+1]))
    if J+1<B and I+1<A:
        candidates.append(abs(x-s[I+1])+abs(s[I+1]-t[J+1]))
        candidates.append(abs(x-t[J+1])+abs(s[I+1]-t[J+1]))
    return(min(candidates))

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

Fixed submission:
import bisect
A,B,Q = map(int,input().split())
INF = 10**18
# s=[-INF]
# t=[-INF]
# for _ in range(A):
    # s.append(int(input()))
# for _ in range(B):
    # t.append(int(input()))
# s.sort()
# t.sort()
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
# s.append(INF)
# t.append(INF)

def solve(x):
    sr = bisect.bisect(s,x)
    tr = bisect.bisect(t,x)
    sRight = s[sr] if sr < A else s[sr-1]
    tRight = t[tr] if tr < B else t[tr-1]
    sLeft = s[sr-1] if sr>0 else s[sr] 
    tLeft = t[tr-1] if tr>0 else t[tr]
    # return(min(
    # print([[sr,sLeft,sRight],[tr,tLeft,tRight]])
    cand = [
        abs(x-sRight) + abs(sRight-tRight),
        abs(x-sRight) + abs(sRight-tLeft),
        abs(x-tRight) + abs(tRight-sRight),
        abs(x-tRight) + abs(tRight-sLeft),
        abs(x-sLeft) + abs(sLeft-tLeft),
        abs(x-sLeft) + abs(sLeft-tRight),
        abs(x-tLeft) + abs(tLeft-sRight),
        abs(x-tLeft) + abs(tLeft-sLeft),
    ]
    # print(cand)
    return(min(cand))
for _ in range(Q):
    print(solve(int(input())))