Buggy python submission (verdict: Time Limit Exceeded):
A,B,Q = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
X = [int(input()) for i in range(Q)]


def bs(x, L, ub, lb):
    if x < L[0]:
        return -1
    if x > L[-1]:
        return len(L)-1
    c = (ub+lb)//2
    if ub-lb <= 1:
        return c
    if L[c] >= x:
        return bs(x, L, c, lb)
    else:
        return bs(x, L, ub, c)
    
for i in range(Q):
    s_l = bs(X[i], S, A-1, 0)
    t_l = bs(X[i], T, B-1, 0)
    
    if s_l < 0:
        sl = 10**11
        su = S[0]-X[i]
    elif s_l == A-1:
        sl = X[i]-S[s_l]
        su = 10**11
    else:
        sl = X[i]-S[s_l]
        su = S[s_l+1]-X[i]
        
    if t_l < 0:
        tl = 10**11
        tu = T[0]-X[i]
    elif t_l == B-1:
        tl = X[i]-T[t_l]
        tu = 10**11
    else:
        tl = X[i]-T[t_l]
        tu = T[t_l+1]-X[i]
    ans = min(max(su, tu), max(sl, tl), su*2+tl, tl*2+su, sl*2+tu, tu*2+sl)
    print(ans)

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

Buggy python submission (verdict: Time Limit Exceeded):
A,B,Q = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
X = [int(input()) for i in range(Q)]

def bs_up(x, L, ub, lb):
    if x > L[-1]:
        return 10**11+1
    if x < L[0]:
        return L[0]
    if ub-lb <= 1:
        return L[(ub+lb)//2+1]
    c = (ub+lb)//2
    if L[c] > x:
        return bs_up(x, L, c, lb)
    else:
        return bs_up(x, L, ub, c)
    
def bs_low(x, L, ub, lb):
    if x < L[0]:
        return -10**11-1
    if x > L[-1]:
        return L[-1]
    if ub-lb <= 1:
        return L[(ub+lb)//2]
    c = (ub+lb)//2
    if L[c] >= x:
        return bs_low(x, L, c, lb)
    else:
        return bs_low(x, L, ub, c)
    
for i in range(Q):
    s_u = bs_up(X[i], S, A-1, 0)-X[i]
    t_u = bs_up(X[i], T, B-1, 0)-X[i]
    s_l = bs_low(X[i], S, A-1, 0)-X[i]
    t_l = bs_low(X[i], T, B-1, 0)-X[i]
    print(min(max(s_u, t_u), max(-s_l, -t_l), s_u*2-t_l, s_u-2*t_l, t_u*2-s_l, t_u-2*s_l))

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

Buggy python submission (verdict: Time Limit Exceeded):
A,B,Q = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
X = [int(input()) for i in range(Q)]

def bs_up(x, L, ub, lb):
    if x > L[-1]:
        return 10**11+1
    if x < L[0]:
        return L[0]
    if ub-lb <= 1:
        return L[(ub+lb)//2+1]
    c = (ub+lb)//2
    if L[c] > x:
        return bs_up(x, L, c, lb)
    else:
        return bs_up(x, L, ub, c)
    
def bs_low(x, L, ub, lb):
    if x < L[0]:
        return -10**11-1
    if x > L[-1]:
        return L[-1]
    if ub-lb <= 1:
        return L[(ub+lb)//2]
    c = (ub+lb)//2
    if L[c] >= x:
        return bs_low(x, L, c, lb)
    else:
        return bs_low(x, L, ub, c)
    
for i in range(Q):
    s_u = bs_up(X[i], S, A-1, 0)-X[i]
    t_u = bs_up(X[i], T, B-1, 0)-X[i]
    s_l = bs_low(X[i], S, A-1, 0)-X[i]
    t_l = bs_low(X[i], T, B-1, 0)-X[i]
    print(min(max(s_u, t_u), max(-s_l, -t_l), s_u*2-t_l, s_u-2*t_l, t_u*2-s_l, t_u-2*s_l))

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

Buggy python submission (verdict: Time Limit Exceeded):
A,B,Q = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
X = [int(input()) for x in range(Q)]

L = []
for i in range(A):
    L.append([S[i], "s"])
for i in range(B):
    L.append([T[i], "t"])
for i in range(Q):
    L.append([X[i], "x", i])
    
L.sort()
ans = []
for i in range(len(L)):
    if L[i][1] == "x":
        c = i+1
        jud = []
        temp_s = []
        temp_t = []
        cnt = 0
        while c < len(L) and len(jud) < 2:
            if L[c][1] == "s" and "s" not in jud:
                jud.append("s")
                temp_s.append(L[c][0]-L[i][0])
            if L[c][1] == "t" and "t" not in jud:
                jud.append("t")
                temp_t.append(L[c][0]-L[i][0])
            c += 1
        if len(temp_s) == 0:
            temp_s.append(10**11+1)
        if len(temp_t) == 0:
            temp_t.append(10**11+1)
        c = i-1
        jud = []
        while c >= 0 and len(jud) < 2:
            if L[c][1] == "s" and "s" not in jud:
                jud.append("s")
                temp_s.append(L[c][0]-L[i][0])
            if L[c][1] == "t" and "t" not in jud:
                jud.append("t")
                temp_t.append(L[c][0]-L[i][0])
            c -= 1
        if len(temp_s) == 1:
            temp_s.append(-10**11-1)
        if len(temp_t) == 1:
            temp_t.append(-10**11-1)
        temp = []
        temp.append(max(temp_s[0], temp_t[0]))
        temp.append(max(-temp_s[1], -temp_t[1]))
        temp.append(min(2*temp_s[0]-temp_t[1], temp_s[0]-2*temp_t[1]))
        temp.append(min(2*temp_t[0]-temp_s[1], temp_t[0]-2*temp_s[1]))
        ans.append([L[i][2], min(temp)])
        
        
ans.sort()
for i in range(Q):
    print(ans[i][1])

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

Buggy python submission (verdict: Time Limit Exceeded):
A,B,Q = map(int, input().split())
S = [int(input()) for i in range(A)]
T = [int(input()) for i in range(B)]
X = [int(input()) for x in range(Q)]

L = []
for i in range(A):
    L.append([S[i], "s"])
for i in range(B):
    L.append([T[i], "t"])
for i in range(Q):
    L.append([X[i], "x", i])
    
L.sort()
ans = []
for i in range(len(L)):
    if L[i][1] == "x":
        c = i+1
        jud = []
        temp_s = []
        temp_t = []
        cnt = 0
        while c < len(L) and len(jud) < 2:
            if L[c][1] == "s" and "s" not in jud:
                jud.append("s")
                temp_s.append(L[c][0]-L[i][0])
            if L[c][1] == "t" and "t" not in jud:
                jud.append("t")
                temp_t.append(L[c][0]-L[i][0])
            c += 1
        if len(temp_s) == 0:
            temp_s.append(10**11+1)
        if len(temp_t) == 0:
            temp_t.append(10**11+1)
        c = i-1
        jud = []
        while c >= 0 and len(jud) < 2:
            if L[c][1] == "s" and "s" not in jud:
                jud.append("s")
                temp_s.append(L[c][0]-L[i][0])
            if L[c][1] == "t" and "t" not in jud:
                jud.append("t")
                temp_t.append(L[c][0]-L[i][0])
            c -= 1
        if len(temp_s) == 1:
            temp_s.append(-10**11-1)
        if len(temp_t) == 1:
            temp_t.append(-10**11-1)
        temp = []
        temp.append(max(temp_s[0], temp_t[0]))
        temp.append(max(-temp_s[1], -temp_t[1]))
        temp.append(min(2*temp_s[0]-temp_t[1], temp_s[0]-2*temp_t[1]))
        temp.append(min(2*temp_t[0]-temp_s[1], temp_t[0]-2*temp_s[1]))
        ans.append([L[i][2], min(temp)])
        
        
ans.sort()
for i in range(Q):
    print(ans[i][1])

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