Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
S, T =[None] * (A+2), [None] * (B+2)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S[-1] = 10**20
T[-1] = 10**20

def Bsearch(x, L, lenL):
    low, high = 0, lenL
    while high-low > 1:
        mid = (low+high)//2
        if L[mid] > x:
            high = mid
        else: low = mid
    return [x-L[low], L[low+1]-x]

lenS, lenT = len(S), len(T)
for _ in range(Q):
    x = int(input())
    sLength0, sLength1 = Bsearch(x, S, lenS)
    tLength0, tLength1 = Bsearch(x, T, lenT)
    Ans = (max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0))
    print(min(Ans))

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

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

Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
S, T =[None] * (A+1), [None] * (B+1)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S.append(10**20)
T.append(10**20)

def Bsearch(x, L, lenL):
    low, high = 0, lenL
    while high-low > 1:
        mid = (low+high)//2
        if L[mid] > x:
            high = mid
        else: low = mid
    return [x-L[low], L[low+1]-x]

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

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

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

Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
S, T =[None] * (A+1), [None] * (B+1)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S.append(10**20)
T.append(10**20)

def Bsearch(x, L, lenL):
    low, high = 0, lenL
    while high-low > 1:
        mid = (low+high)//2
        if L[mid] > x:
            high = mid
        else: low = mid
    return [x-L[low], L[low+1]-x]

printAns = ""
lenS, lenT = len(S), len(T)
for _ in range(Q):
    x = int(input())
    sLength0, sLength1 = Bsearch(x, S, lenS)
    tLength0, tLength1 = Bsearch(x, T, lenT)
    Ans = [max(sLength0, tLength0), max(sLength1, tLength1), sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0)]
    printAns += str(min(Ans)) + "\n"
print(printAns)

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

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

Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
S, T =[None] * (A+1), [None] * (B+1)
S[0], T[0] = -10**20, -10**20
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S.append(10**20)
T.append(10**20)

def Bsearch(x, L):
    low, high = 0, len(L)
    while high-low > 1:
        mid = (low+high)//2
        if L[mid] > x:
            high = mid
        else: low = mid
    return low

def Length(x, i, L):
    return [x - L[i], L[i+1] - x]

printAns = ""

for _ in range(Q):
    x = int(input())
    sLength0, sLength1 = Length(x, Bsearch(x, S), S)
    tLength0, tLength1 = Length(x, Bsearch(x, T), T)
    Ans = min(max(sLength0, tLength0), max(sLength1, tLength1))
    Ans = min(Ans, min(sLength0 + tLength1 + min(sLength0, tLength1), sLength1 + tLength0 + min(sLength1, tLength0)))
    printAns += str(Ans) + "\n"
print(printAns)

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

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

Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
S, T =[None] * (A+1), [None] * (B+1)
S[0], T[0] = 0, 0
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S.append(10**20)
T.append(10**20)

def Bsearch(x, L):
    low, high = 0, len(L)
    while high-low > 1:
        mid = (low+high)//2
        if L[mid] > x:
            high = mid
        else: low = mid
    return low

def Length(x, i, L):
    if i == 0: return [10**30, L[1] - x]
    else: return [x - L[i], L[i+1] - x]

for _ in range(Q):
    x = int(input())
    sLength, tLength = Length(x, Bsearch(x, S), S), Length(x, Bsearch(x, T), T)
    Ans = min(max(sLength[0], tLength[0]), max(sLength[1], tLength[1]))
    Ans = min(Ans, min(sLength[0] + tLength[1] + min(sLength[0], tLength[1]), sLength[1] + tLength[0] + min(sLength[1], tLength[0])))
    print(Ans)

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

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

Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
S, T =[None] * (A+1), [None] * (B+1)
S[0], T[0] = 0, 0
for i in range(A):
    S[i+1] = int(input())
for i in range(B):
    T[i+1] = int(input())
S.append(10**20)
T.append(10**20)

def Bsearch(x, L):
    low, high = 0, len(L)
    while high-low > 1:
        mid = (low+high)//2
        if L[mid] > x:
            high = mid
        else: low = mid
    return low

def Length(x, i, L):
    if i == 0: return [10**30, L[1] - x]
    else: return [x - L[i], L[i+1] - x]

for _ in range(Q):
    x = int(input())
    sLength, tLength = Length(x, Bsearch(x, S), S), Length(x, Bsearch(x, T), T)
    Ans = min(max(sLength[0], tLength[0]), max(sLength[1], tLength[1]))
    Ans = min(Ans, min(sLength[0] + tLength[1] + min(sLength[0], tLength[1]), sLength[1] + tLength[0] + min(sLength[1], tLength[0])))
    print(Ans)

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

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