Buggy python submission (verdict: Time Limit Exceeded):
def main():
    a,b,q = map(int,input().split())
    A = []
    B = []
    
    for _ in range(a):
        A.append(int(input()))
    
    for _ in range(b):
        B.append(int(input()))
    
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(input())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        print(min(min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr)),max(sl,tl),max(sr,tr)))

if __name__ == '__main__':
    main()

Fixed submission:
import sys
readline = sys.stdin.readline


def main():
    a,b,q = map(int,readline().split())
    A = [int(readline()) for i in range(a)]
    B = [int(readline()) for i in range(b)]
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(readline())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        print(min(min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr)),max(sl,tl),max(sr,tr)))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    a,b,q = map(int,input().split())
    A = []
    B = []
    
    for _ in range(a):
        A.append(int(input()))
    
    for _ in range(b):
        B.append(int(input()))
    
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(input())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        p1 = min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr))
        p2 = max(sl,tl)
        p3 = max(sr,tr)
        print(min(p1,p2,p3))

if __name__ == '__main__':
    main()
    
    
    



Fixed submission:
import sys
readline = sys.stdin.readline


def main():
    a,b,q = map(int,readline().split())
    A = [int(readline()) for i in range(a)]
    B = [int(readline()) for i in range(b)]
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(readline())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        print(min(min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr)),max(sl,tl),max(sr,tr)))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    a,b,q = map(int,input().split())
    A = []
    B = []
    
    for _ in range(a):
        A.append(int(input()))
    
    for _ in range(b):
        B.append(int(input()))
    
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),0)
        if x >A[-1]:
            return (right,float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (left,right)
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(input())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        sl = dist(A,x,sl)
        sr = dist(A,x,sr)
        tl = dist(B,x,tl)
        tr = dist(B,x,tr)
        p1 = min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr))
        p2 = max(sl,tl)
        p3 = max(sr,tr)
        print(min(p1,p2,p3))

if __name__ == '__main__':
    main()
    
    
    



Fixed submission:
import sys
readline = sys.stdin.readline


def main():
    a,b,q = map(int,readline().split())
    A = [int(readline()) for i in range(a)]
    B = [int(readline()) for i in range(b)]
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(readline())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        print(min(min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr)),max(sl,tl),max(sr,tr)))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    a,b,q = map(int,input().split())
    A = []
    B = []
    
    for _ in range(a):
        A.append(int(input()))
    
    for _ in range(b):
        B.append(int(input()))
    
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),0)
        if x >A[-1]:
            return (right,float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (left,right)
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(input())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        sl = dist(A,x,sl)
        sr = dist(A,x,sr)
        tl = dist(B,x,tl)
        tr = dist(B,x,tr)
        p1 = min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr))
        p2 = max(sl,tl)
        p3 = max(sr,tr)
        print(min(p1,p2,p3))

if __name__ == '__main__':
    main()
    
    
    



Fixed submission:
import sys
readline = sys.stdin.readline


def main():
    a,b,q = map(int,readline().split())
    A = [int(readline()) for i in range(a)]
    B = [int(readline()) for i in range(b)]
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(readline())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        print(min(min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr)),max(sl,tl),max(sr,tr)))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
a,b,q = map(int,input().split())
A = []
B = []

for _ in range(a):
    A.append(int(input()))

for _ in range(b):
    B.append(int(input()))

def search(A,x):
    left =0
    right = len(A)-1
    
    if x<A[0]:
        return (float('inf'),0)
    if x >A[-1]:
        return (right,float('inf'))
    
    while left != right-1:
        center = (left+right)//2
        
        if A[center] > x:
            right = center
        else:
            left = center
    
    return (left,right)


def dist(A,x,lr):
    if lr == float('inf'):
        return float('inf')
    return abs(x - A[lr])

for _ in range(q):
    x = int(input())
    sl,sr = search(A,x)
    tl,tr = search(B,x)
    sl = dist(A,x,sl)
    sr = dist(A,x,sr)
    tl = dist(B,x,tl)
    tr = dist(B,x,tr)
    p1 = min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr))
    p2 = max(sl,tl)
    p3 = max(sr,tr)
    print(min(p1,p2,p3))

Fixed submission:
import sys
readline = sys.stdin.readline


def main():
    a,b,q = map(int,readline().split())
    A = [int(readline()) for i in range(a)]
    B = [int(readline()) for i in range(b)]
    def search(A,x):
        left =0
        right = len(A)-1
        
        if x<A[0]:
            return (float('inf'),abs(x - A[left]))
        if x >A[-1]:
            return (abs(x - A[right]),float('inf'))
        
        while left != right-1:
            center = (left+right)//2
            
            if A[center] > x:
                right = center
            else:
                left = center
        
        return (abs(x - A[left]),abs(x - A[right]))
    
    
    def dist(A,x,lr):
        if lr == float('inf'):
            return float('inf')
        return abs(x - A[lr])
    
    for _ in range(q):
        x = int(readline())
        sl,sr = search(A,x)
        tl,tr = search(B,x)
        print(min(min(sl,sr)+min(tl,tr)+min(min(sl,sr),min(tl,tr)),max(sl,tl),max(sr,tr)))

if __name__ == '__main__':
    main()