Problem p03112 (python)

Fixed submission:
def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))

    jinja_list = []

    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))

    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))

    from bisect import bisect

    for i in range(q):
        pos = int(input().rstrip("\n"))

        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]

        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]

        distances = []
        #distances.append((pos, jinja_left, jinja_right, temple_left, temple_right))
        if jinja_left and temple_left:
            distances.append(pos - min(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(max(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = abs(pos - jinja_left[0]) + abs(temple_right[0] - jinja_left[0])
            right_left = abs(pos - temple_right[0]) + abs(jinja_left[0] - temple_right[0])
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = abs(pos - temple_left[0]) + abs(jinja_right[0] - temple_left[0])
            right_left = abs(pos - jinja_right[0]) + abs(temple_left[0] - jinja_right[0])
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))
        
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    a, b, q = map(int, input().rstrip("\n").split(" "))
 
    jinja_list = []
 
    for i in range(a):
        jinja_list.append(int(input().rstrip("\n")))
 
    temple_list = []
    for i in range(b):
        temple_list.append(int(input().rstrip("\n")))
 
    from bisect import bisect
 
    for i in range(q):
        pos = int(input().rstrip("\n"))
 
        jinja_index = bisect(jinja_list, pos)
        jinja_left = jinja_list[jinja_index - 1:jinja_index]
        jinja_right = jinja_list[jinja_index:jinja_index + 1]
 
        temple_index = bisect(temple_list, pos)
        temple_left = temple_list[temple_index - 1:temple_index]
        temple_right = temple_list[temple_index:temple_index + 1]
 
        distances = []
        #distances.append((pos, jinja_left, jinja_right, temple_left, temple_right))
        if jinja_left and temple_left:
            distances.append(pos - min(jinja_left[0], temple_left[0]))
            #distances.append("jinja_left and temple_left")
        if jinja_right and temple_right:
            distances.append(max(jinja_right[0], temple_right[0]) - pos)
            #distances.append("jinja_right and temple_right")
        if jinja_left and temple_right:
            left_right = (pos - jinja_left[0]) * 2 + (temple_right[0] - pos)
            right_left = (pos - jinja_left[0]) + (temple_right[0] - pos) * 2
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_left and temple_right")
        if jinja_right and temple_left:
            left_right = ((pos - temple_left[0]) * 2) + (jinja_right[0] - pos)
            right_left = (pos - temple_left[0]) + ((jinja_right[0] - pos) * 2)
            distances.append(left_right)
            distances.append(right_left)
            #distances.append("jinja_right and temple_left")
        #print(distances)
        print(min(distances))

if __name __ == '__main__':
  main()