Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from sys import stdin
input = stdin.readline
import numpy as np
from collections import deque
H,W = map(int, input().rstrip().split())
CH,CW = map(int, input().rstrip().split())
DH,DW = map(int, input().rstrip().split())
[CH,CW,DH,DW] = [CH-1,CW-1,DH-1,DW-1]
S = np.array([list(input().rstrip()) for i in range(H)])
INF = 10 ** 9
Distance = np.ones((H,W))*INF
Distance[CH,CW] = 0
[nowH,nowW] = [CH,CW]
q = deque([])
q.append([nowH,nowW])
while len(q) != 0:
  [nowH,nowW] = q.popleft()
  if [nowH,nowW] == [DH,DW]:
    break
  for i in range(-1,2):
    for j in range(-1,2):
      if nowH+i < 0 or nowW+j < 0 or nowH+i >= H or nowW+j >= W:
        continue
      if S[nowH+i,nowW+j] != '.':
        continue
      if abs(i) + abs(j) != 1:
        continue
      if Distance[nowH,nowW] < Distance[nowH+i,nowW+j]:
        Distance[nowH+i,nowW+j] = Distance[nowH,nowW]
        q.appendleft([nowH+i,nowW+j])
  for i in range(-2,3):
    for j in range(-2,3):
      if nowH+i < 0 or nowW+j < 0 or nowH+i >= H or nowW+j >= W:
        continue
      if S[nowH+i,nowW+j] != '.':
        continue
      if abs(i) + abs(j) < 2:
        continue
      if Distance[nowH,nowW] + 1 < Distance[nowH+i,nowW+j]:
        Distance[nowH+i,nowW+j] = Distance[nowH,nowW] + 1
        q.append([nowH+i,nowW+j])
if Distance[DH,DW] == INF:
  print(-1)
else:
  print(int(Distance[DH,DW]))

### FIXED CODE ###

from collections import deque
H,W = map(int, input().split())
CH,CW = map(lambda x: int(x) - 1, input().split())
DH,DW = map(lambda x: int(x) - 1, input().split())
S = [input() for i in range(H)]
INF = 10 ** 9
walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + walk]
h = CH
w = CW
Distance = [[INF] * W for _ in range(H)]
Distance[h][w] = 0
q = deque(())
q.append((h,w))
while q:
  h,w = q.popleft()
  if [h,w] == [DH,DW]:
    break
  for i,j in walk:
    hwalk = h + i
    wwalk = w + j
    if 0 <= hwalk < H and 0 <= wwalk < W and S[hwalk][wwalk] == '.' and Distance[h][w] < Distance[hwalk][wwalk]:
      Distance[hwalk][wwalk] = Distance[h][w]
      q.appendleft((hwalk,wwalk))
  for i,j in warp:
    hwalk = h + i
    wwalk = w + j
    if 0 <= hwalk < H and 0 <= wwalk < W and S[hwalk][wwalk] == '.' and Distance[h][w] + 1 < Distance[hwalk][wwalk]:
      Distance[hwalk][wwalk] = Distance[h][w] + 1
      q.append((hwalk,wwalk))
print(int(Distance[DH][DW]) if Distance[DH][DW] != INF else -1)
### BUGGY CODE ###

N = input()
A = map(int, input().split())
a = 1
for i in A:
  a *= i
  
if a > 10**18:
	print(-1)
else:
  print(a)

### FIXED CODE ###

N = input()
A = input().split()
if '0' in A:
  print(0)
else:
  a = 1
  for i in A:
    a *= int(i)
    if a > 10**18:
        print(-1)
        exit()
  else:
    print(a)
### BUGGY CODE ###

from copy import deepcopy

def count(A):
    cnt = 0
    for a in A:
        cnt += (a*(a-1))//2
    return cnt


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    max_A = max(A)

    count_A = [0]*(max_A+1)

    for a in A:
        count_A[a]+= 1

    for a in A:
        cal_A = deepcopy(count_A)
        cal_A[a] -= 1
        print(count(cal_A))

### FIXED CODE ###

from copy import deepcopy

def count(A):
    cnt = 0
    for a in A:
        cnt += (a*(a-1))//2
    return cnt


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    max_A = max(A)

    count_A = [0]*(max_A+1)

    for a in A:
        count_A[a]+= 1

    combi_A = 0

    for c in count_A:
        combi_A += (c*(c-1))//2
    for a in A:
        print(combi_A-(count_A[a]*(count_A[a]-1)-(count_A[a]-1)*(count_A[a]-2))//2)
### BUGGY CODE ###

n,m=map(int,input().split())
l=[[0,0]]*m
t=[0]*m
tc=0
a=False
f=0
for k in range(m):
  l[k]=list(map(int,input().split()))
l.sort()
for k in l:
  if (k[0]!=1 and f==0):
    del t[tc:]
    f=1
  if k[0]==1:
    t[tc]=k[1]
    tc+=1
  elif k[1]==n:
    if k[0] in t:
      a=True
      break
if a:
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")

### FIXED CODE ###

n,m=map(int,input().split())
l=[[0,0]]*m
at=[0]*m
bt=[0]*m
atc=0
btc=0
a=False

f=0
for k in range(m):
  ln=list(map(int,input().split()))
  if ln[0]==1:
    at[atc]=ln[1]
    atc+=1
  elif ln[1]==n:
    bt[btc]=ln[0]
    btc+=1
del at[atc:]
del bt[btc:]
at.sort()
bt.sort()
c=list(set(at)^set(bt))
if len(c)!=(atc+btc):
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")
### BUGGY CODE ###

# 法Pの下での組み合わせ数 nCk を求める
# MAX: nの最大値

MAX = (10**6)*2//3 +1  # (X+Y)/3より大きい整数 1 <= X, Y <= 10**6
P = (10**9)+7

fac=[1 for _ in range(MAX)]
inv=[1 for _ in range(MAX)]
finv=[1 for _ in range(MAX)]

# 拡張ユークリッドアルゴリズム
# (d, x, y): d=ax+by を満たすd, x, yを求める
# aとbが互いに素な整数であればgcd(a,b)=d=1, ax=1 (mod b)
# xは法bの元でaの乗法逆元a^-1になる
def exEuclid(a, b):
    if (b==0):
        return (a, 1, 0)
    else:
        (dd, xx, yy) = exEuclid(b, a % b)
        return (dd, yy, xx - (a//b)*yy)

def COMinit():
    global fac, inv, finv
    #print('len(fac): {}'.format(len(fac)))
    #print(MAX)
    for i in range(2,MAX):
        #print(i)
        fac[i] = fac[i-1]*i % P
        (d, x, y) = exEuclid(i, P)
        inv[i] = x
        finv[i] = finv[i-1]*inv[i] % P

def COM(n, k):
    if(n<k):
        return 0
    elif ( (n<0)or(k<0)):
        return 0
    else:
        return fac[n]*(finv[n-k]*finv[k] % P) % P

####

COMinit()
#print('x, y:')
x, y = list(map(int,input().split()))

if ((x+y)%3 != 0):
    print(0)
    exit()
else:
    n1 = (-x+2*y)//3
    n2 = (2*x -y)//3
    if (n1<0):
        print(0)
        exit()
    elif(n2<0):
        print(0)
        exit()
    else:
        npath = COM(n1+n2, n1)
        print(npath)



### FIXED CODE ###

# 法Pの下での組み合わせ数 nCk を求める
# MAX: nの最大値

P = (10**9)+7

fac=[]
inv=[]
finv=[]

# 拡張ユークリッドアルゴリズム
# (d, x, y): d=ax+by を満たすd, x, yを求める
# aとbが互いに素な整数であればgcd(a,b)=d=1, ax=1 (mod b)
# xは法bの元でaの乗法逆元a^-1になる
def exEuclid(a, b):
    if (b==0):
        return (a, 1, 0)
    else:
        (dd, xx, yy) = exEuclid(b, a % b)
        return (dd, yy, xx - (a//b)*yy)

def COMinit(MAX):
    global fac, inv, finv
    fac=[1 for _ in range(MAX)]
    inv=[1 for _ in range(MAX)]
    finv=[1 for _ in range(MAX)]
    #print('len(fac): {}'.format(len(fac)))
    #print(MAX)
    for i in range(2,MAX):
        #print(i)
        fac[i] = fac[i-1]*i % P
        (d, x, y) = exEuclid(i, P)
        inv[i] = x
        finv[i] = finv[i-1]*inv[i] % P

def COM(n, k):
    if(n<k):
        return 0
    elif ( (n<0)or(k<0)):
        return 0
    else:
        return fac[n]*(finv[n-k]*finv[k] % P) % P

####

#print('x, y:')
x, y = list(map(int,input().split()))
MAX = (x+y)//3 +1  # (X+Y)/3より大きい整数 1 <= X, Y <= 10**6
COMinit(MAX)

if ((x+y)%3 != 0):
    print(0)
    exit()
else:
    n1 = (-x+2*y)//3
    n2 = (2*x -y)//3
    if (n1<0):
        print(0)
        exit()
    elif(n2<0):
        print(0)
        exit()
    else:
        npath = COM(n1+n2, n1)
        print(npath)
### BUGGY CODE ###

import sys

from functools import lru_cache

sys.setrecursionlimit(10 ** 5)

MOD = 998244353


def solve(A, B, C, D):
    @lru_cache(maxsize=None)
    def numWays(rows, cols):
        if rows == A and cols == B:
            return 1
        ways = 0
        if rows > A:
            ways += cols * numWays(rows - 1, cols)
        if cols > B:
            ways += rows * numWays(rows, cols - 1)

        if rows > A and cols > B:
            ways -= (cols - 1) * (rows - 1) * numWays(rows - 1, cols - 1)
        return ways % MOD

    return numWays(C, D) % MOD


A, B, C, D = [int(x) for x in input().split()]
print(solve(A, B, C, D))


### FIXED CODE ###

MOD = 998244353


def solve(A, B, C, D):
    numWays = [[None for c in range(D + 1)] for r in range(C + 1)]

    numWays[A][B] = 1

    for rows in range(A, C + 1):
        for cols in range(B, D + 1):
            if rows == A and cols == B:
                continue
            ways = 0
            if rows > A:
                ways += cols * numWays[rows - 1][cols] % MOD
            if cols > B:
                ways += rows * numWays[rows][cols - 1] % MOD
            if rows > A and cols > B:
                ways -= (cols - 1) * (rows - 1) * numWays[rows - 1][cols - 1] % MOD
            numWays[rows][cols] = ways % MOD

    return numWays[C][D] % MOD


A, B, C, D = [int(x) for x in input().split()]
print(solve(A, B, C, D))
### BUGGY CODE ###

N = int(input())
S = input()

left = []
right = []
ans = 3*10**5

for i in range(N):
    left_count = 0
    right_count = 0
    left = S[0:i]
    right = S[i+1:]

    for l in left:
        if l == "W":
            left_count += 1

    for r in right:
        if r == "E":
            right_count += 1
            
    count = left_count + right_count
    ans = min(count, ans)
print(ans)

### FIXED CODE ###

N = int(input())
S = list(input())

# 一番左がリーダーの時
l = 0
r = S[1:].count('W')
ans = l + r
for i in range(1, N):
    if S[i-1] == 'E':  # 元リーダーの人が東向きの時
        l += 1
    if S[i] == 'W':  # 元リーダーが西向きの時
        r -= 1
    cnt = l+r
    ans = max(cnt, ans)  # 向きを変える必要のない人数
print(N - ans -1)  # 向きを変える必要のある人数
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))

result = []

for i in range(N//2):
    if i < N -2:
        s = A[2*i+1]
        for j in range(2*i+2, N):
            s = s ^ A[j]
        result.append(str(s))
        s = A[2*i]
        for j in range(2*i+2, N):
            s = s ^ A[j]
        result.append(str(s))
        
    else:
        result.append(str(A[N-1]))
        result.append(str(A[N-2]))
        break
    
    for j in range(2*i+1, N):
        A[j] = int(result[-1])^int(result[-2])^A[j]

print(' '.join(result)) 

### FIXED CODE ###

import copy
N = int(input())
A = list(map(int,input().split()))

result = ['' for _ in range(N)]
s = A[0]
for i in range(1,N):
    s = s ^ A[i]

for i in range(N):
    result[i] = str(s ^ A[i])

print(' '.join(result))
### BUGGY CODE ###

import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
def myAnswer(N:int,M:int,S:str,T:str) -> int:
   L = lcm(N,M)
   setS = [1]
   for n in range(1,N):
      setS.append(n * L//N + 1)
   setT = [1]
   for m in range(1,M):
      setT.append(m * L//M + 1)
   # print(setS,setT,L)
   for n,i in enumerate(setS):
      for m,j in enumerate(setT):
         if(i == j and S[n] != T[m]):
            # print(i,j,S[n],T[m],n,m)
            return -1
   else:
      return L

def modelAnswer():
   return
def main():
   N,M = map(int,input().split())
   S = input()
   T = input()
   print(myAnswer(N,M,S,T))
if __name__ == '__main__':
   main()

### FIXED CODE ###

import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
def myAnswer(N:int,M:int,S:str,T:str) -> int:
   a = N//fractions.gcd(N,M)
   b =M // fractions.gcd(N,M)
   # print(a,b)
   for i in range(fractions.gcd(N,M)):
      # print(S[a * i],T[b * i])
      if(S [a * i] != T[b * i]):
         return -1
   else:
      return lcm(N,M)

def modelAnswer():
   return
def main():
   N,M = map(int,input().split())
   S = input()
   T = input()
   print(myAnswer(N,M,S,T))
if __name__ == '__main__':
   main()
### BUGGY CODE ###

import itertools

N = int(input())
A_list = list(map(int, input().split()))

divided = []
for i in range(N):
    divided.append(A_list.count(i + 1))

ans = 0
for j in divided:
    ans += len(list(itertools.combinations(range(j), 2)))

# print(divided)

for k in range(1, N + 1):
    n = divided[A_list[k - 1] - 1]
    # print(n)
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))

### FIXED CODE ###

import collections
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N = int(input())
A_list = list(map(int, input().split()))

counts = collections.Counter(A_list)
ans = 0
for _, v in counts.items():
    # ans += len(list(itertools.combinations(range(v), 2)))
    if v > 1:
        ans += combinations_count(v, 2)

for k in range(1, N + 1):
    n = counts[A_list[k - 1]]
    ans_k = ans
    if n > 1:
        ans_k -= n - 1

    print(int(ans_k))
### BUGGY CODE ###

n = int(input())
a_list =input().split()
total = 1
for i in range(n):
    total *= int(a_list[i])
if total > 10**18:
    print(-1)
else:
    print(total)

### FIXED CODE ###

a = int(input())
li = list(map(int,input().split()))
total = 1
i = 1
for i in range(a):
    total = total*li[i]
    if total > 1000000000000000000:
        total = -1
        break
for i in range(a):
    if li[i] == 0:
        total = 0
 
print(total)
### BUGGY CODE ###

N, M, R = map(int, input().split())
Rs = list(map(int, input().split()))

INF = 100000*200*200
dp = [[INF for _ in range(N)] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c

for i in range(N):
    for j in range(N):
        for k in range(N):
            dp[j][k] = min(dp[j][k], dp[j][i]+dp[i][k])


def ind(i, R, rem, l, ans):
    if i == R:
        ans.append(l)
        return ans
    for r in rem:
        nl = l[:]
        nl.append(r)
        nr = rem[:]
        nr.remove(r)
        ans = ind(i+1, R, nr, nl, ans)
    return ans

inds = ind(0, R, [i for i in range(R)], [], [])

for k, ind in enumerate(inds):
    D = 0
    for j in range(R-1):
        D += dp[Rs[ind[j]]-1][Rs[ind[j+1]]-1]
    if k == 0:
        ans = D
    ans = min(ans, D)
print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():
    N, M, R = map(int, input().split())
    Rs = list(map(int, input().split()))

    INF = 100000*200*200
    dp = [[INF for _ in range(N)] for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        dp[a-1][b-1] = c
        dp[b-1][a-1] = c

    for i in range(N):
        for j in range(N):
            for k in range(N):
                dp[j][k] = min(dp[j][k], dp[j][i]+dp[i][k])


    def ind(i, R, rem, l, ans):
        if i == R:
            ans.append(l)
            return ans
        for r in rem:
            nl = l[:]
            nl.append(r)
            nr = rem[:]
            nr.remove(r)
            ans = ind(i+1, R, nr, nl, ans)
        return ans

    inds = ind(0, R, [i for i in range(R)], [], [])

    for k, ind in enumerate(inds):
        D = 0
        for j in range(R-1):
            D += dp[Rs[ind[j]]-1][Rs[ind[j+1]]-1]
        if k == 0:
            ans = D
        ans = min(ans, D)
    print(ans)

if __name__ == "__main__":
    main()
### BUGGY CODE ###

x, y, z, k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
D = []
E = []
for i in range(x):
  for j in range(y):
    D.append(A[i]+B[j])
for i in range(z):
  for j in range(len(D)):
    E.append(C[i]+D[j])
print("\n".join(map(str,sorted(E)[::-1][:k])))


### FIXED CODE ###

x, y, z, k = [int(n) for n in input().split()]
va = sorted([int(n) for n in input().split()], reverse=True)
vb = sorted([int(n) for n in input().split()], reverse=True)
xy = min(x*y, k)
vab = (sorted([a + b for a in va for b in vb], reverse=True))[:xy]
vc = sorted([int(n) for n in input().split()], reverse=True)
xyz = min(x*y*z, k)
print(*((sorted([ab + c for ab in vab for c in vc], reverse=True))[:xyz]), sep="\n")
### BUGGY CODE ###

from collections import deque
import itertools
import copy

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]


def bfs(maze, s, g):
    q = deque()
    q.append((s, 0))

    maze[s[0]][s[1]] = "#"

    while len(q) > 0:
        p, depth = q.popleft()

        if p == g:
            return depth

        # ikerutoko
        next_pos = []
        if 0 <= p[0] - 1 < H and maze[p[0] - 1][p[1]] != "#":
            next_pos.append((p[0] - 1, p[1]))
        if 0 <= p[0] + 1 < H and maze[p[0] + 1][p[1]] != "#":
            next_pos.append((p[0] + 1, p[1]))
        if 0 <= p[1] - 1 < W and maze[p[0]][p[1] - 1] != "#":
            next_pos.append((p[0], p[1] - 1))
        if 0 <= p[1] + 1 < W and maze[p[0]][p[1] + 1] != "#":
            next_pos.append((p[0], p[1] + 1))

        # print(next_pos, q)

        for pos in next_pos:
            maze[pos[0]][pos[1]] = "#"
            q.append((pos, depth + 1))

    return -1


kaisuu = 0
for s in itertools.product(range(H), range(W)):
    for g in itertools.product(range(H), range(W)):
        if s == g or S[s[0]][s[1]] == "#" or S[g[0]][g[1]] == "#":
            continue

        maze = copy.deepcopy(S)
        k = bfs(maze, s, g)
        kaisuu = max(kaisuu, k)

print(kaisuu)


### FIXED CODE ###

from collections import deque
import copy


H, W = map(int, input().split())
S = [list(input()) for i in range(H)]


def bfs(maze, start):
    q = deque()
    q.append((start, 0))

    max_depth = 0

    while len(q) > 0:
        p, depth = q.popleft()

        if p[0] < 0 or H <= p[0]:
            continue
        if p[1] < 0 or W <= p[1]:
            continue
        if maze[p[0]][p[1]] == "#":
            continue

        maze[p[0]][p[1]] = "#"
        max_depth = depth

        next_pos = [
            (p[0]-1, p[1]),
            (p[0]+1, p[1]),
            (p[0], p[1]-1),
            (p[0], p[1]+1),
        ]

        for pos in next_pos:
            q.append((pos, depth+1))

    return max_depth


depth = 0
for h in range(H):
    for w in range(W):
        maze = copy.deepcopy(S)
        depth = max(depth, bfs(maze, (h, w)))

print(depth)
### BUGGY CODE ###

N = int(input())
L = [input() for i in range(N)]

list_val = list(set(L))
list_size = [0]*N
for i,v in enumerate(list_val):
    list_size[i] = L.count(v)

max_size = max(list_size)
#sorted_index = sorted(range(len(list_size)), key=lambda k: list_size[k], reverse=True)

ans = []
for i,s in enumerate(list_size):
    if s == max_size:
      ans.append(list_val[i])
print("\n".join(sorted(ans)))


### FIXED CODE ###

import collections

N = int(input())

s = [input() for i in range(N)]

c = collections.Counter(s)
max_n = max(c.values())

ans = []
for key, val in c.items():
  if val == max_n:
    ans.append(key)
print("\n".join(sorted(list(set(ans)))))
### BUGGY CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort(reverse=True)
f.sort()
m = [i * j for i, j in zip(a, f)]
for i in range(k):
    t = max(m)
    if t <= 0:
        print(0)
        break
    l = m.index(t)
    a[l] -= 1
    m[l] = a[l] * f[l]
else:
    print(max(m))



### FIXED CODE ###

import math
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    f = list(map(int, input().split()))
    a.sort(reverse=True)
    f.sort()
    hi = a[0] * f[-1]
    lo = 0
    def caneat(m):
        t = 0
        for i, j in zip(a, f):
            if i * j > m:
                t += math.ceil((i*j - m)/j)
            if t > k:
                return False
        return True
    while hi != lo:
        m = (hi + lo) // 2
        if caneat(m):
            hi = m
        else:
            lo = m + 1
    return hi
print(main())
### BUGGY CODE ###

N = int(input())
x = []
for i in range(N):
    a1,b1,c1,d1=[int(i) for i in input().split()]
    x.append([a1,b1,c1,d1])
for i in range(N):
  y = x[i]
  if y[0] < y[1]:
    print("No")
  elif y[1] > y[3]:
    print("No")
  elif y[1] > y[2]:
    ama = y[0] % y[1]
    mita = set()
    while True:
      if ama in mita:
        print("Yes")
        break
      mita.add(ama)
      if ama <= y[2] and ama>= 0:
        ama += y[3]
      if ama < y[1]:
        print("No")
        break
      ama %= y[1]
  else:
    print("Yes")

### FIXED CODE ###

import fractions
N = int(input())
x = []
for i in range(N):
    a1,b1,c1,d1=[int(i) for i in input().split()]
    x.append([a1,b1,c1,d1])
for i in range(N):
  y = x[i]
  if y[0] < y[1]:
    print("No")
  elif y[1] > y[3]:
    print("No")
  elif y[1] > y[2]:
    ama = y[0] % y[1]
    mo = y[3] %  y[1]
    if mo == 0:
      if ama<= y[2]:
        print("Yes")
      else:
          print("No")
    elif mo <= y[1]-y[2]-1:
      print("No")
    else:
        to = y[1] % mo
        if to == 0:
          if ama<= y[2]:
            print("Yes")
          else:
              print("No")
        else:
          are = fractions.gcd(to, y[1])
          if are <= y[1]-y[2]-1:
            print("No")
          else:
            print("Yes")
          
  else:
    print("Yes")
### BUGGY CODE ###

import bisect

A, B, Q = map(int, input().split())
s = [0]*(A+4)
t = [0]*(B+4)

for i in range(2, A+2):
    s[i] = int(input())

for i in range(2, B+2):
    t[i] = int(input())

s[0], s[1], s[-2], s[-1] = -10**11-1, -10**11, 10**11, 10**11+1
t[0], t[1], t[-2], t[-1] = -10**11-1, -10**11, 10**11, 10**11+1


for i in range(Q):
    ans = 10**11
    x = int(input())
    sh_left = bisect.bisect(s, x) - 1
    sh_right = bisect.bisect(s, x)
    for i in range(2):
        loc_sh = s[[sh_left, sh_right][i]]
        te_left = bisect.bisect(t, loc_sh) - 1
        te_right = bisect.bisect(t, loc_sh)
        loc_te_l = t[te_left]
        loc_te_r = t[te_right]
        ans = min(ans, min(abs(x-loc_sh)+abs(loc_sh-loc_te_l), abs(x-loc_sh)+abs(loc_sh-loc_te_r)))
    te_left = bisect.bisect(t, x) - 1
    te_right = bisect.bisect(t, x)
    for i in range(2):
        loc_te = t[[te_left, te_right][i]]
        sh_left = bisect.bisect(s, loc_te) - 1
        sh_right = bisect.bisect(s, loc_te)
        loc_sh_l = s[sh_left]
        loc_sh_r = s[sh_right]
        ans = min(ans, min(abs(x-loc_te)+abs(loc_te-loc_sh_l), abs(x-loc_te)+abs(loc_te-loc_sh_r)))
    print(ans)


### FIXED CODE ###

from bisect import bisect_right

A, B, Q = map(int, input().split())
INF = 10**18
s = [-INF] + [int(input()) for _ in range(A)] + [INF]
t = [-INF] + [int(input()) for _ in range(B)] + [INF]

for _ in range(Q):
    x = int(input())
    sh_r, te_r = bisect_right(s, x), bisect_right(t, x)
    sh_l, te_l = sh_r - 1, te_r - 1
    SR, SL, TR, TL = s[sh_r], s[sh_l], t[te_r], t[te_l]
    ans = INF
    for i in [SR, SL]:
        for j in [TR, TL]:
            dis = abs(i-x) + abs(i-j)
            dis = min(dis, abs(j-x) + abs(j-i))
            ans = min(ans, dis)
    print(ans)
### BUGGY CODE ###

a=int(input())
y=int(a ** (1/2))
z=y
while y*z != a:
 if y*z > a:
  z=z-1
 if y*z < a:
  y=y+1
print(y+z-2)

### FIXED CODE ###

import math

a=int(input())
x=(math.ceil(a**(1/2)))
for i in range(x+1):
 if a%(x-i+1)==0:
  print(int(x-i-1+(a/(x-i+1))))
  break
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))

a.sort()
tn = 1
cnt = 0
for i in range(n):
    
    for j in a:
        if j == tn: cnt += 1
    print(cnt)
    cnt = 0
    tn += 1
        

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))

a.sort()
tn = 1
cnt = 0
suffix = 0
for i in range(n):
    for j in range(suffix, n-1):
        if a[j] == tn:
            cnt += 1
            suffix = j+1
        else:
            break
    print(cnt)
    cnt = 0
    tn += 1
### BUGGY CODE ###

n, a, b = map(int, input().split())
l = []
while(True):
    for i in range(a):
        l.append(1)
    for i in range(b):
        l.append(0)
    if len(l) >= n:
        break

print(sum(l[:n]))


### FIXED CODE ###

n, a, b = map(int, input().split())
k = n // (a + b)
m = n % (a + b)
print(k * a + min(a, m))
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))

result = 0
for b in range(60):
  a = [(e >> b) & 1 for e in A]
  bs = [0] * N
  bs[N - 1] = a[N - 1]
  for i in range(N - 2, -1, -1):
    bs[i] = bs[i + 1] + a[i]

  for i in range(N - 1):
    if a[i] == 0:
      t = bs[i + 1]
    else:
      t = (N - (i + 1)) - bs[i + 1]
    result = (result + t * (1 << b)) % 1000000007
print(result)


### FIXED CODE ###

def main():
    N = int(input())
    A = list(map(int, input().split()))

    r = list(range(N - 2, -1, -1))
    result = 0
    for b in range(60):
        t = 0
        bs = (A[N - 1] >> b) & 1
        for i in r:
            if (A[i] >> b) & 1 == 0:
                t += bs
            else:
                t += (N - (i + 1)) - bs
                bs += 1
        result = (result + t * (1 << b)) % 1000000007
    print(result)
main()