Problem p03112 (python)

Fixed submission:
import bisect
a,b,q = map(int,input().split())
s = []
t = []
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
X  =[int(input()) for i in range(q)]
#入力


for x in X:
    s_index= bisect.bisect_left(s,x)
    #print(s_index)
    t_index= bisect.bisect_left(t,x)
    #print(t_index)
    if s_index == 0 and t_index == 0:
        print(max(t[0]-x,s[0]-x))
    elif s_index == 0:
        if t_index != b:
            if t[t_index] < s[0]:
                print(s[0]-x)
            else:
                ans  =min(2*s[0]-x - t[t_index-1],t[t_index]-x,x+s[0]-2*t[t_index-1])
                print(ans)
        else:
            print(min(2*s[s_index]-x-t[t_index-1],s[s_index]+x-2*t[t_index-1]))
    elif t_index == 0:
        if s_index != a:
            if s[s_index] < t[0]:
                print(t[0]-x)
            else:
                ans  =min(2*t[0]- x- s[s_index-1],s[s_index]-x,x+t[0]-2*s[s_index-1])
                print(ans)
        else:
            print(min(2*t[t_index]-x-s[s_index-1],t[t_index]+x-2*s[s_index-1]))
    elif s_index == a and t_index == b:
        print(max(x-s[a-1],x-t[b-1]))
    elif s_index == a:
        if s[a-1] < t[t_index-1]:
            print(x-s[a-1])
        else:
            ans = min(x-t[t_index-1],t[t_index]+x-2*s[a-1],2*t[t_index]-x-s[a-1])
            print(ans)
    elif t_index == b:
        if t[b-1] < s[s_index-1]:
            print(x-t[b-1])
        else:
            ans = min(x - s[s_index - 1], s[s_index] + x - 2 * t[b - 1],2*s[s_index]-x-t[b-1])
            print(ans)

    else:
        if s[s_index-1] < t[t_index-1] and s[s_index] < t[t_index]:
            ans = min(x-s[s_index-1],x+s[s_index]-2*t[t_index-1],2*s[s_index]-x-t[t_index-1],t[t_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and t[t_index] < s[s_index]:
            ans = min(x - t[t_index - 1], x + t[t_index] - 2 * s[s_index - 1], 2 * t[t_index] - x - s[s_index - 1],
                      s[s_index] - x)
            print(ans)
        if s[s_index-1] < t[t_index-1] and t[t_index] < s[s_index]:
            ans = min(x-s[s_index-1],s[s_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and s[s_index] < t[t_index]:
            ans = min(x - t[t_index - 1], t[t_index] - x)
            print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
a,b,q = map(int,input().split())
s = []
t = []
for i in range(a):
    s.append(int(input()))
for i in range(b):
    t.append(int(input()))
X  =[int(input()) for i in range(q)]
#入力


for x in X:
    s_index= bisect.bisect_left(s,x)
    #print(s_index)
    t_index= bisect.bisect_left(t,x)
    #print(t_index)
    if s_index == 0 and t_index == 0:
        print(max(t[0]-x,s[0]-x))
    elif s_index == 0:
        if t_index != b:
            if t[t_index] < s[0]:
                print(s[0]-x)
            else:
                ans  =min(2*s[0]-x - t[t_index-1],t[t_index]-x,x+s[0]-2*t[t_index-1])
                print(ans)
        else:
            print(min(2*s[s_index]-x-t[t_index-1],s[s_index]+x-2*t[t_index-1]))
    elif t_index == 0:
        if s_index !=a:
            if s[s_index] < t[0]:
                print(t[0]-x)
            else:
                ans  =min(2*t[0]- x- s[s_index-1],s[s_index]-x,x+t[0]-2*s[s_index-1])
                print(ans)
        else:
            print(min(2*t[t_index]-x-s[s_index-1],t[t_index]+x-2*s[s_index-1]))
    elif s_index == a and t_index == b:
        print(max(x-s[a-1],x-t[b-1]))
    elif s_index == a:
        if s[a-1] < t[t_index-1]:
            print(x-s[a-1])
        else:
            ans = min(x-t[t_index-1],t[t_index]+x-2*s[a-1],2*t[t_index]-x-s[a-1])
            print(ans)
    elif t_index == b:
        if t[a-1] < s[s_index-1]:
            print(x-t[b-1])
        else:
            ans = min(x - s[s_index - 1], s[s_index] + x - 2 * t[b - 1],2*s[s_index]-x-t[b-1])
            print(ans)

    else:
        if s[s_index-1] < t[t_index-1] and s[s_index] < t[t_index]:
            ans = min(x-s[s_index-1],x+s[s_index]-2*t[t_index-1],2*s[s_index]-x-t[t_index-1],t[t_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and t[t_index] < s[s_index]:
            ans = min(x - t[t_index - 1], x + t[t_index] - 2 * s[s_index - 1], 2 * t[t_index] - x - s[s_index - 1],
                      s[s_index] - x)
            print(ans)
        if s[s_index-1] < t[t_index-1] and t[t_index] < s[s_index]:
            ans = min(x-s[s_index-1],s[s_index]-x)
            print(ans)
        if t[t_index - 1] < s[s_index - 1] and s[s_index] < t[t_index]:
            ans = min(x - t[t_index - 1], t[t_index] - x)
            print(ans)