Buggy python submission (verdict: Runtime Error):

from bisect import bisect_right

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:
    # Shrine -> Temple
    idx_s = bisect_right(S, x)
    if abs(S[min(A - 1, idx_s)] - x) > abs(S[max(0, idx_s - 1)] - x):
        x_s = S[max(0, idx_s - 1)]
    else:
        x_s = S[min(A - 1, idx_s)]

    idx_t = bisect_right(T, x_s)
    if abs(T[min(A - 1, idx_t)] - x_s) > abs(T[max(0, idx_t - 1)] - x_s):
        x_t = T[max(0, idx_t - 1)]
    else:
        x_t = T[min(B - 1, idx_t)]

    cand_0 = abs(x_s - x) + abs(x_t - x_s)

    # Temple -> Shrine
    idx_t = bisect_right(T, x)
    if abs(T[min(A - 1, idx_t)] - x) > abs(T[max(0, idx_t - 1)] - x):
        x_t = T[max(0, idx_t - 1)]
    else:
        x_t = T[min(B - 1, idx_t)]

    idx_s = bisect_right(S, x_t)
    if abs(S[min(A - 1, idx_s)] - x_t) > abs(S[max(0, idx_s - 1)] - x_t):
        x_s = S[max(0, idx_s - 1)]
    else:
        x_s = S[min(A - 1, idx_s)]

    cand_1 = abs(x_t - x) + abs(x_s - x_t)

    print(min(cand_0, cand_1))


Fixed submission:
from bisect import bisect_right

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 = 10 ** 18
s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i in range(Q):
    b = bisect_right(s, x[i])
    d = bisect_right(t, x[i])
    res = INF
    for n in [s[b - 1], s[b]]:
        for m in [t[d - 1], t[d]]:
            d1 = abs(n - x[i]) + abs(m - n)
            d2 = abs(m - x[i]) + abs(n - m)
            res = min(res, d1, d2)
    print(res)

Buggy python submission (verdict: Runtime Error):

from bisect import bisect_right

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:
    # Shrine -> Temple
    idx_s = bisect_right(S, x)
    if abs(S[min(A - 1, idx_s)] - x) > abs(S[max(0, idx_s - 1)] - x):
        x_s = S[max(0, idx_s - 1)]
    else:
        x_s = S[min(A - 1, idx_s)]

    idx_t = bisect_right(T, x_s)
    if abs(T[min(A - 1, idx_t)] - x_s) > abs(T[max(0, idx_t - 1)] - x_s):
        x_t = T[max(0, idx_t - 1)]
    else:
        x_t = T[min(A - 1, idx_t)]

    cand_0 = abs(x_s - x) + abs(x_t - x_s)

    # Temple -> Shrine
    idx_t = bisect_right(T, x)
    if abs(T[min(A - 1, idx_t)] - x) > abs(T[max(0, idx_t - 1)] - x):
        x_t = T[max(0, idx_t - 1)]
    else:
        x_t = T[min(A - 1, idx_t)]

    idx_s = bisect_right(S, x_t)
    if abs(S[min(A - 1, idx_s)] - x_t) > abs(S[max(0, idx_s - 1)] - x_t):
        x_s = S[max(0, idx_s - 1)]
    else:
        x_s = S[min(A - 1, idx_s)]

    cand_1 = abs(x_t - x) + abs(x_s - x_t)

    print(min(cand_0, cand_1))


Fixed submission:
from bisect import bisect_right

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 = 10 ** 18
s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i in range(Q):
    b = bisect_right(s, x[i])
    d = bisect_right(t, x[i])
    res = INF
    for n in [s[b - 1], s[b]]:
        for m in [t[d - 1], t[d]]:
            d1 = abs(n - x[i]) + abs(m - n)
            d2 = abs(m - x[i]) + abs(n - m)
            res = min(res, d1, d2)
    print(res)