Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
a,b,q = map(int, input().split())
S = []
for _ in range(a):
  S.append(int(input()))
T = []
for _ in range(b):
  T.append(int(input()))
X = []
for _ in range(q):
  X.append(int(input()))

INF = 10 ** 11
def dfs(direction, tors, pos, count):
  if tors == 1:
    L = T
  elif tors == -1:
    L = S
  i = bisect_left(L, pos)
  if i == len(L):
    if direction == 1:
      return INF
    elif direction == -1:
      res = abs(L[i-1]-pos)
      next_pos = L[i-1]
  elif L[i] == pos:
    res = 0
    next_pos = pos
  else:
    if direction == 1:
      res = abs(L[i]-pos)
      next_pos = L[i]
    elif direction == -1:
      if i == 0:
        return INF

      res = abs(L[i-1]-pos)
      next_pos = L[i-1]

  if count == 1:
    return res

  res0 = res + dfs(direction, tors*(-1), next_pos, count-1)
  res1 = res + dfs(direction*(-1), tors*(-1), next_pos, count-1)

  return min(res0, res1)

for x in X:
  res0 = dfs(1, 1, x, 2)
  res1 = dfs(-1, 1, x, 2)
  res2 = dfs(1, -1, x, 2)
  res3 = dfs(-1, -1, x, 2)

  print(min(res0, res1, res2, res3))


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]
for q in range(Q):
  x = int(input())
  b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
  res = INF
  for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
      d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
      res = min(res, d1, d2)
  print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left
a,b,q = map(int, input().split())
S = []
for _ in range(a):
  S.append(int(input()))
T = []
for _ in range(b):
  T.append(int(input()))
X = []
for _ in range(q):
  X.append(int(input()))

INF = 10 ** 11
def dfs(direction, tors, pos, count):
  if tors == 1:
    L = T
  elif tors == -1:
    L = S
  i = bisect_left(L, pos)
  if i == len(L):
    if direction == 1:
      return INF
    elif direction == -1:
      res = abs(L[i-1]-pos)
      next_pos = L[i-1]
  elif L[i] == pos:
    res = 0
    next_pos = pos
  else:
    if direction == 1:
      res = abs(L[i]-pos)
      next_pos = L[i]
    elif direction == -1:
      if i == 0:
        return INF

      res = abs(L[i-1]-pos)
      next_pos = L[i-1]

  if count == 1:
    return res

  res0 = res + dfs(direction, tors*(-1), next_pos, count-1)
  res1 = res + dfs(direction*(-1), tors*(-1), next_pos, count-1)

  return min(res0, res1)

for x in X:
  res0 = dfs(1, 1, x, 2)
  res1 = dfs(-1, 1, x, 2)
  res2 = dfs(1, -1, x, 2)
  res3 = dfs(-1, -1, x, 2)

  print(min(res0, res1, res2, res3))


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]
for q in range(Q):
  x = int(input())
  b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
  res = INF
  for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
      d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
      res = min(res, d1, d2)
  print(res)