Buggy python submission (verdict: Time Limit Exceeded):
def calc(locate, start_point):
    if start_point <= min(locate):
        if locate[0] >= locate[shrine_no]: 
            print(locate[0] - start_point)
        else:
            print(locate[shrine_no] - start_point)
            
    elif start_point >= max(locate):
        if locate[shrine_no -1] >= locate[-1]:
            print(start_point - locate[-1])
        else:
            print(start_point - locate[shrine_no - 1])
            
    else:
        s_diff_list = []
        t_diff_list = []
            
        s_diff = start_point - locate[0]
        t_diff = start_point - locate[shrine_no]
        for i in range(shrine_no):
            s_diff_list.append(start_point - locate[i])
            if abs(s_diff) > abs(start_point - locate[i]):
                s_diff = start_point - locate[i]
        sort_s_diff = sorted(s_diff_list)
                
        for i in range(shrine_no, shrine_no + temple_no):
            t_diff_list.append(start_point - locate[i])
            if abs(t_diff) > abs(start_point - locate[i]):
                t_diff = start_point - locate[i]
        sort_t_diff = sorted(t_diff_list)
      
        if s_diff * t_diff < 0:
            dist = []
            for i in range(len(s_diff_list)):
                for j in range(len(t_diff_list)):
                    dist.append(abs(s_diff_list[i]-t_diff_list[j]) + min(abs(s_diff_list[i]), abs(t_diff_list[j])))
            print(min(dist))
            
        elif s_diff * t_diff > 0:
            print(max(abs(s_diff), abs(t_diff)))
        else:
            print(abs(s_diff+t_diff))

shrine_no, temple_no, q = map(int, input().split())
locate = []
start = []

for i in range(shrine_no+temple_no+q):
    tmp = int(input())
    if i < shrine_no+temple_no:
        locate.append(tmp)
    else:
        start.append(tmp)

for i in range(len(start)):
    calc(locate, start[i])

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]
loc = [int(input()) for i in range(Q)]
for i in range(len(loc)):
    x = loc[i]
    res = INF
    s_w, t_w = bisect.bisect_right(s,x), bisect.bisect_right(t,x)
    d1 = abs(s[s_w]-x) + abs(t[t_w]-s[s_w])
    d2 = abs(t[t_w]-x) + abs(s[s_w]-t[t_w])
    d3 = abs(s[s_w]-x) + abs(t[t_w-1]-s[s_w])
    d4 = abs(t[t_w-1]-x) + abs(t[t_w-1]-s[s_w])
    d5 = abs(s[s_w-1]-x) + abs(t[t_w]-s[s_w-1])
    d6 = abs(t[t_w]-x) + abs(s[s_w-1]-t[t_w])
    d7 = abs(s[s_w-1]-x) + abs(t[t_w-1]-s[s_w-1])
    d8 = abs(t[t_w-1]-x) + abs(s[s_w-1]-t[t_w-1])
    print(min(d1,d2,d3,d4,d5,d6,d7,d8))

Buggy python submission (verdict: Time Limit Exceeded):
def calc(locate, start_point):
    if start_point <= min(locate):
        if locate[0] >= locate[shrine_no]: 
            print(locate[0] - start_point)
        else:
            print(locate[shrine_no] - start_point)
            
    elif start_point >= max(locate):
        if locate[shrine_no -1] >= locate[-1]:
            print(start_point - locate[-1])
        else:
            print(start_point - locate[shrine_no - 1])
            
    else:
        s_diff = start_point - locate[0]
        t_diff = start_point - locate[shrine_no]
        for i in range(1, shrine_no):
            if abs(s_diff) > abs(start_point - locate[i]):
                s_diff = start_point - locate[i]
                
        for i in range(shrine_no+1, shrine_no + temple_no):
            if abs(t_diff) > abs(start_point - locate[i]):
                t_diff = start_point - locate[i]
                
        if s_diff * t_diff < 0:
            s_diff_list = []
            t_diff_list = []
            
            for i in range(shrine_no):
                s_diff_list.append(start_point - locate[i])
            sort_s_diff = sorted(s_diff_list)
                
            for i in range(shrine_no, shrine_no + temple_no):
                t_diff_list.append(start_point - locate[i])
            sort_t_diff = sorted(t_diff_list)

            dist = []
            for i in range(len(s_diff_list)):
                for j in range(len(t_diff_list)):
                    dist.append(abs(s_diff_list[i]-t_diff_list[j]) + min(abs(s_diff_list[i]), abs(t_diff_list[j])))
            print(min(dist))
            
        elif s_diff * t_diff > 0:
            print(max(abs(s_diff), abs(t_diff)))
        else:
            print(abs(s_diff+t_diff))

shrine_no, temple_no, q = map(int, input().split())
locate = []
start = []

for i in range(shrine_no+temple_no+q):
    tmp = int(input())
    if i < shrine_no+temple_no:
        locate.append(tmp)
    else:
        start.append(tmp)

for i in range(len(start)):
    calc(locate, start[i])

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]
loc = [int(input()) for i in range(Q)]
for i in range(len(loc)):
    x = loc[i]
    res = INF
    s_w, t_w = bisect.bisect_right(s,x), bisect.bisect_right(t,x)
    d1 = abs(s[s_w]-x) + abs(t[t_w]-s[s_w])
    d2 = abs(t[t_w]-x) + abs(s[s_w]-t[t_w])
    d3 = abs(s[s_w]-x) + abs(t[t_w-1]-s[s_w])
    d4 = abs(t[t_w-1]-x) + abs(t[t_w-1]-s[s_w])
    d5 = abs(s[s_w-1]-x) + abs(t[t_w]-s[s_w-1])
    d6 = abs(t[t_w]-x) + abs(s[s_w-1]-t[t_w])
    d7 = abs(s[s_w-1]-x) + abs(t[t_w-1]-s[s_w-1])
    d8 = abs(t[t_w-1]-x) + abs(s[s_w-1]-t[t_w-1])
    print(min(d1,d2,d3,d4,d5,d6,d7,d8))