Buggy python submission (verdict: Runtime Error):
import math
a, b, q = map(int, input().split())
s = [-float("inf")] + [int(input()) for _ in range(a)] + [float("inf")]
t = [-float("inf")] + [int(input()) for _ in range(b)] + [float("inf")]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


for _ in range(q):
    near(x, s)
    near(x, t)
#     x = int(input())
# 
#     si = near(x, s)
#     spos = [si, si + 1] if s[si] < x else [si, si - 1]
#     ti = near(x, t)
#     tpos = [ti, ti + 1] if t[ti] < x else [ti, ti - 1]
# 
#     result = float("inf")
#     for sindex in spos:
#         for tindex in tpos:
#             v1 = abs(x - s[sindex]) + abs(s[sindex] - t[tindex])
#             v2 = abs(x - t[tindex]) + abs(t[tindex] - s[sindex])
#             result = min(
#                 float("inf") if math.isnan(v1) else v1,
#                 float("inf") if math.isnan(v2) else v2,
#                 result
#             )
# 
#     print(result)


Fixed submission:
import bisect
import math
a, b, q = map(int, input().split())
inf = float("inf")
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(b)] + [inf]

results = ""
for _ in range(q):
    x = int(input())
    si = bisect.bisect_right(s, x)
    spos = [s[si], s[si - 1]]

    ti = bisect.bisect_right(t, x)
    tpos = [t[ti], t[ti - 1]]

    result = float("inf")
    for svalue in spos:
        for tvalue in tpos:
            if svalue == tvalue == inf or svalue == tvalue == -inf:
                continue
            result = min(
                abs(x - svalue) + abs(svalue - tvalue),
                abs(x - tvalue) + abs(tvalue - svalue),
                result
            )

    results += str(result) + "\n"
print(results)

Buggy python submission (verdict: Time Limit Exceeded):
import math
a, b, q = map(int, input().split())
s = [-float("inf")] + [int(input()) for _ in range(a)] + [float("inf")]
t = [-float("inf")] + [int(input()) for _ in range(b)] + [float("inf")]

s.sort()
t.sort()

def near(x, buildings):
    left, right = 0, len(buildings) - 1

    while right - left > 1:
        center = (left + right) // 2
        value = buildings[center]
        if x < value:
            right = center
        elif x > value:
            left = center
        else:
            return center
    return left if abs(x - buildings[left]) < abs(x - buildings[right]) else right


for _ in range(q):
    x = int(input())

    si = near(x, s)
    spos = [si, si + 1] if s[si] < x else [si, si - 1]
    ti = near(x, t)
    tpos = [ti, ti + 1] if t[ti] < x else [ti, ti - 1]

    result = float("inf")
    for sindex in spos:
        for tindex in tpos:
            v1 = abs(x - s[sindex]) + abs(s[sindex] - t[tindex])
            v2 = abs(x - t[tindex]) + abs(t[tindex] - s[sindex])
            result = min(
                float("inf") if math.isnan(v1) else v1,
                float("inf") if math.isnan(v2) else v2,
                result
            )

    print(result)


Fixed submission:
import bisect
import math
a, b, q = map(int, input().split())
inf = float("inf")
s = [-inf] + [int(input()) for _ in range(a)] + [inf]
t = [-inf] + [int(input()) for _ in range(b)] + [inf]

results = ""
for _ in range(q):
    x = int(input())
    si = bisect.bisect_right(s, x)
    spos = [s[si], s[si - 1]]

    ti = bisect.bisect_right(t, x)
    tpos = [t[ti], t[ti - 1]]

    result = float("inf")
    for svalue in spos:
        for tvalue in tpos:
            if svalue == tvalue == inf or svalue == tvalue == -inf:
                continue
            result = min(
                abs(x - svalue) + abs(svalue - tvalue),
                abs(x - tvalue) + abs(tvalue - svalue),
                result
            )

    results += str(result) + "\n"
print(results)