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

for i in range(Q):
    pos = int(input())
    plusA, plusB, minusA, minusB = 2E10, 2E10, -2E10, -2E10
    for i in range(A):
        a = pos - shrine[i]
        if(a>= 0):
            plusA = min(plusA, a)
        else:
            minusA = max(minusA, a)
    for i in range(B):
        b = pos - temple[i]
        if(b>= 0):
            plusB = min(plusB, b)
        else:
            minusB = max(minusB, b)
    lis = [max(plusA, plusB), abs(min(minusA, minusB)), abs(2*plusA-minusB), abs(2*minusA - plusB), abs(2*plusB - minusA), abs(2*minusB - plusA)]
    print(min(lis))

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
INF = 10**18
shrine = [-INF] + [int(input()) for i in range(A)] + [INF]
temple = [-INF] + [int(input()) for i in range(B)] + [INF]

for i in range(Q):
    pos = int(input())
    b,d = bisect.bisect_right(shrine, pos), bisect.bisect_right(temple, pos)
    res = INF
    for S in [shrine[b-1], shrine[b]]:
        for T in [temple[d-1], temple[d]]:
            d1, d2 = abs(S-pos)+abs(T-S), abs(T-pos)+abs(S-T)
            res = min(res, d1, d2)
    print(res)

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

for i in range(Q):
    pos = int(input())
    plusA, plusB, minusA, minusB = 2E10, 2E10, -2E10, -2E10
    for i in range(A):
        a = pos - shrine[i]
        if(a>= 0):
            plusA = min(plusA, a)
        else:
            minusA = max(minusA, a)
    for i in range(B):
        b = pos - temple[i]
        if(b>= 0):
            plusB = min(plusB, b)
        else:
            minusB = max(minusB, b)
    lis = [max(plusA, plusB), abs(min(minusA, minusB)), abs(2*plusA-minusB), abs(2*minusA - plusB), abs(2*plusB - minusA), abs(2*minusB - plusA)]
    print(min(lis))

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
INF = 10**18
shrine = [-INF] + [int(input()) for i in range(A)] + [INF]
temple = [-INF] + [int(input()) for i in range(B)] + [INF]

for i in range(Q):
    pos = int(input())
    b,d = bisect.bisect_right(shrine, pos), bisect.bisect_right(temple, pos)
    res = INF
    for S in [shrine[b-1], shrine[b]]:
        for T in [temple[d-1], temple[d]]:
            d1, d2 = abs(S-pos)+abs(T-S), abs(T-pos)+abs(S-T)
            res = min(res, d1, d2)
    print(res)

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

for i in range(Q):
    pos = int(input())
    nsh = [pos - shrine[i] for i in range(A)]
    ntem = [pos - temple[i] for i in range(B)]
    plusA, plusB, minusA, minusB = 2E10, 2E10, -2E10, -2E10
    for i in range(A):
        if(nsh[i]>= 0):
            plusA = min(plusA, nsh[i])
        else:
            minusA = max(minusA, nsh[i])
    for i in range(B):
        if(ntem[i]>= 0):
            plusB = min(plusB, ntem[i])
        else:
            minusB = max(minusB, ntem[i])
    lis = [max(plusA, plusB), abs(min(minusA, minusB)), abs(2*plusA-minusB), abs(2*minusA - plusB), abs(2*plusB - minusA), abs(2*minusB - plusA)]
    print(min(lis))

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
INF = 10**18
shrine = [-INF] + [int(input()) for i in range(A)] + [INF]
temple = [-INF] + [int(input()) for i in range(B)] + [INF]

for i in range(Q):
    pos = int(input())
    b,d = bisect.bisect_right(shrine, pos), bisect.bisect_right(temple, pos)
    res = INF
    for S in [shrine[b-1], shrine[b]]:
        for T in [temple[d-1], temple[d]]:
            d1, d2 = abs(S-pos)+abs(T-S), abs(T-pos)+abs(S-T)
            res = min(res, d1, d2)
    print(res)

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

for i in range(Q):
    pos = int(input())
    sh = [[shrine[i], abs(pos - shrine[i])] for i in range(A)]
    tem = [[temple[i], abs(pos - temple[i])] for i in range(B)]
    sh.sort(key = lambda x: x[1])
    tem.sort(key = lambda x: x[1])
    l1 = sh[0][1]
    npos = sh[0][0]
    ntem = [abs(npos - temple[i]) for i in range(B)]
    ntem.sort()
    l1 += ntem[0]

    l2 = tem[0][1]
    npos = tem[0][0]
    nsh = [abs(npos - shrine[i]) for i in range(A)]
    nsh.sort()
    l2 += nsh[0]
    print(min([l1, l2]))

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
INF = 10**18
shrine = [-INF] + [int(input()) for i in range(A)] + [INF]
temple = [-INF] + [int(input()) for i in range(B)] + [INF]

for i in range(Q):
    pos = int(input())
    b,d = bisect.bisect_right(shrine, pos), bisect.bisect_right(temple, pos)
    res = INF
    for S in [shrine[b-1], shrine[b]]:
        for T in [temple[d-1], temple[d]]:
            d1, d2 = abs(S-pos)+abs(T-S), abs(T-pos)+abs(S-T)
            res = min(res, d1, d2)
    print(res)

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

for i in range(Q):
    pos = int(input())
    sh = list(map(lambda x: abs(pos - shrine[x]), range(A)))
    tem = list(map(lambda x: abs(pos - temple[x]), range(B)))
    #print(sh, tem)
    l1 = min(sh)
    npos = shrine[sh.index(l1)]
    ntem =  list(map(lambda x: abs(npos - temple[x]), range(B)))
    l1 += min(ntem)

    l2 = min(tem)
    npos = temple[tem.index(l2)]
    nsh =  list(map(lambda x: abs(npos - shrine[x]), range(A)))
    l2 += min(nsh)
    print(min([l1, l2]))

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
INF = 10**18
shrine = [-INF] + [int(input()) for i in range(A)] + [INF]
temple = [-INF] + [int(input()) for i in range(B)] + [INF]

for i in range(Q):
    pos = int(input())
    b,d = bisect.bisect_right(shrine, pos), bisect.bisect_right(temple, pos)
    res = INF
    for S in [shrine[b-1], shrine[b]]:
        for T in [temple[d-1], temple[d]]:
            d1, d2 = abs(S-pos)+abs(T-S), abs(T-pos)+abs(S-T)
            res = min(res, d1, d2)
    print(res)