Buggy python submission (verdict: Time Limit Exceeded):
def calc(a_list, b_list, x):
    inf = 10**11
    aa = [a - x for a in a_list]
    bb = [b - x for b in b_list]
    if len([b for b in bb if b>=0])*len([a for a in aa if a>=0]) >0:
        p_1 = max([b for b in bb if b>=0][0], [a for a in aa if a>=0][0])
    else:
        p_1 = inf
    if len([b for b in bb if b<=0])*len([a for a in aa if a<=0]) >0:
        p_2 = max(abs(max([b for b in bb if b<=0])), abs(max([a for a in aa if a<=0])))
    else:
        p_2 = inf
    if len([b for b in bb if b<=0])*len([a for a in aa if a>=0]) >0:
        b_tmp = max([b for b in bb if b<=0])
        a_tmp = min([a for a in aa if a>=0])
        p_3 = a_tmp - b_tmp + min(abs(a_tmp), abs(b_tmp))
    else:
        p_3 = inf
    if len([b for b in bb if b>=0])*len([a for a in aa if a<=0]) >0:
        b_tmp = min([b for b in bb if b>=0])
        a_tmp = max([a for a in aa if a<=0])
        p_4 = b_tmp - a_tmp + min(abs(a_tmp), abs(b_tmp))
    else:
        p_4 = inf
    return min(p_1, p_2, p_3, p_4)
a,b,q = map(int, input().split())
a_list = []
b_list = []
q_list = []
for i in range(a):
    a_list.append(int(input()))
for j in range(b):
    b_list.append(int(input()))
for k in range(q):
    q = int(input())
    ans = calc(a_list, b_list, q)
    print(ans)

Fixed submission:
import bisect
a, b, q = map(int, input().split())
s = []
for i in range(a):
    s.append(int(input()))
t = []
for j in range(b):
    t.append(int(input()))
inf = 10 ** 18
s = [-inf] + s + [inf]
t = [-inf] + t + [inf]
for k in range(q):
    x = int(input())
    s_r, t_r = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = inf
    for s_ in [s[s_r], s[s_r - 1]]:
        for t_ in [t[t_r], t[t_r - 1]]:
            d = min(abs(x-s_), abs(x-t_)) + abs(s_ - t_)
            res = min(d, res)
    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
def calc(a_list, b_list, x):
    inf = 10**11
    aa = [a - x for a in a_list]
    bb = [b - x for b in b_list]
    if len([b for b in bb if b>=0])*len([a for a in aa if a>=0]) >0:
        p_1 = max([b for b in bb if b>=0][0], [a for a in aa if a>=0][0])
    else:
        p_1 = inf
    if len([b for b in bb if b<=0])*len([a for a in aa if a<=0]) >0:
        p_2 = max(abs(max([b for b in bb if b<=0])), abs(max([a for a in aa if a<=0])))
    else:
        p_2 = inf
    if len([b for b in bb if b<=0])*len([a for a in aa if a>=0]) >0:
        b_tmp = max([b for b in bb if b<=0])
        a_tmp = min([a for a in aa if a>=0])
        p_3 = a_tmp - b_tmp + min(abs(a_tmp), abs(b_tmp))
    else:
        p_3 = inf
    if len([b for b in bb if b>=0])*len([a for a in aa if a<=0]) >0:
        b_tmp = min([b for b in bb if b>=0])
        a_tmp = max([a for a in aa if a<=0])
        p_4 = b_tmp - a_tmp + min(abs(a_tmp), abs(b_tmp))
    else:
        p_4 = inf
    return min(p_1, p_2, p_3, p_4)

a,b,q = map(int, input().split())
a_list = []
b_list = []
q_list = []
for i in range(a):
    a_list.append(int(input()))
for j in range(b):
    b_list.append(int(input()))
a_list = sorted(a_list)
b_list = sorted(b_list)
for k in range(q):
    q = int(input())
    ans = calc(a_list, b_list, q)
    print(ans)

Fixed submission:
import bisect
a, b, q = map(int, input().split())
s = []
for i in range(a):
    s.append(int(input()))
t = []
for j in range(b):
    t.append(int(input()))
inf = 10 ** 18
s = [-inf] + s + [inf]
t = [-inf] + t + [inf]
for k in range(q):
    x = int(input())
    s_r, t_r = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
    res = inf
    for s_ in [s[s_r], s[s_r - 1]]:
        for t_ in [t[t_r], t[t_r - 1]]:
            d = min(abs(x-s_), abs(x-t_)) + abs(s_ - t_)
            res = min(d, res)
    print(res)