Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
  q = int(input())
  temp = inf
  i = bl(Sh, q)
  que = []
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Te, Sh[j])
    temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q))
    if k != 0:
      temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q))
  i = bl(Te, q)
  if abs(Te[i] - q) < temp:
    que.append(i)
  if abs(Te[i - 1] - q) < temp:
    que.append(i)
    if i != 0:
      que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Sh, Te[j])
    temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q))
    if k != 0:
      temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q))
  print(temp)

Fixed submission:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  b, d = bl(Sh, x), bl(Te, x)
  res = inf
  for S in [Sh[b - 1], Sh[b]]:
    for T in [Te[d - 1], Te[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 as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [int(input()) for _ in range(A)] + [inf, inf]
Te = [int(input()) for _ in range(B)] + [inf, inf]

for _ in range(Q):
  q = int(input())
  temp = inf
  i = bl(Sh, q)
  #print(q, Sh[i - 1])
  que = []
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Te, Sh[j])
    #print(Sh[j], Te[k])
    temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q))
    if k != 0:
      temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q))
    
  i = bl(Te, q)
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.pop()
    k = bl(Sh, Te[j])
    temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q))
    if k != 0:
      temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q))
  print(temp)
    

Fixed submission:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  b, d = bl(Sh, x), bl(Te, x)
  res = inf
  for S in [Sh[b - 1], Sh[b]]:
    for T in [Te[d - 1], Te[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 as bl
from collections import deque
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [int(input()) for _ in range(A)] + [inf, inf]
Te = [int(input()) for _ in range(B)] + [inf, inf]

for _ in range(Q):
  q = int(input())
  temp = inf
  i = bl(Sh, q)
  #print(q, Sh[i - 1])
  que = deque([])
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.popleft()
    k = bl(Te, Sh[j])
    #print(Sh[j], Te[k])
    temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q))
    if k != 0:
      temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q))
    
  i = bl(Te, q)
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.popleft()
    k = bl(Sh, Te[j])
    temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q))
    if k != 0:
      temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q))
  print(temp)
    

Fixed submission:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  b, d = bl(Sh, x), bl(Te, x)
  res = inf
  for S in [Sh[b - 1], Sh[b]]:
    for T in [Te[d - 1], Te[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 as bl
from collections import deque
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [int(input()) for _ in range(A)] + [inf, inf]
Te = [int(input()) for _ in range(B)] + [inf, inf]

for _ in range(Q):
  q = int(input())
  temp = inf
  i = bl(Sh, q)
  #print(q, Sh[i - 1])
  que = deque([])
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.popleft()
    k = bl(Te, Sh[j])
    #print(Sh[j], Te[k])
    temp = min(temp, abs(Sh[j] - Te[k]) + abs(Sh[j] - q))
    if k != 0:
      temp = min(temp, abs(Sh[j] - Te[k - 1]) + abs(Sh[j] - q))
    
  i = bl(Te, q)
  que.append(i)
  if i != 0:
    que.append(i - 1)
  while len(que):
    j = que.popleft()
    k = bl(Sh, Te[j])
    temp = min(temp, abs(Te[j] - Sh[k]) + abs(Te[j] - q))
    if k != 0:
      temp = min(temp, abs(Te[j] - Sh[k - 1]) + abs(Te[j] - q))
  print(temp)
    

Fixed submission:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  b, d = bl(Sh, x), bl(Te, x)
  res = inf
  for S in [Sh[b - 1], Sh[b]]:
    for T in [Te[d - 1], Te[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):
A, B, Q = map(int, input().split())
Sh = [int(input()) for _ in range(A)]
Te = [int(input()) for _ in range(B)]
inf = 10 ** 11 + 9
def move(x, s, t, d):
  ress = inf
  rest = inf
  if s >= 1:
    if t >= 1:
      #print(d, "dです")
      return d
  mins = inf
  tempi = 0
  temp = inf
  if s == 0:
    ress = 0
    for i in range(A):
      temp = min(mins, abs(x - Sh[i]))
      if mins != temp: tempi = i
      mins = temp
    #print(Sh[tempi], "ress")
    ress += move(Sh[tempi], s + 1, t + 0, d + abs(x - Sh[tempi]))
  
  tempi = 0
  temp = inf
  mint = inf
  if t == 0:
    rest = 0
    for i in range(B):
      temp = min(mint, abs(x - Te[i]))
      if mint != temp: tempi = i
      mint = temp
    #print(Te[tempi], "rest")
    rest += move(Te[tempi], s + 0, t + 1, d + abs(x - Te[tempi]))
  #print(ress, rest)
  return min(ress, rest)
for _ in range(Q):
  print(move(int(input()), 0, 0, 0))

Fixed submission:
from bisect import bisect_left as bl
A, B, Q = map(int, input().split())
inf = 10 ** 11
Sh = [-1 * inf] + [int(input()) for _ in range(A)] + [inf]
Te = [-1 * inf] + [int(input()) for _ in range(B)] + [inf]
for _ in range(Q):
  x = int(input())
  b, d = bl(Sh, x), bl(Te, x)
  res = inf
  for S in [Sh[b - 1], Sh[b]]:
    for T in [Te[d - 1], Te[d]]:
      d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
      res = min(res, d1, d2)
  print(res)