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

from itertools import product
N, M = map(int, input().split())
XYZ = [list(map(int, input().split())) for _ in range(N)]

INF = 2**60
ans = -INF 
for a, b, c in product([-1, 1], repeat=3):
    dp = [-INF]*(M+1)
    dp[0] = 0
    for i in range(N):
        for j in range(M-1, -1, -1):
            if dp[j] != -INF:
                s = a * XYZ[i][0] + b * XYZ[i][1] + c * XYZ[i][2]
                dp[j+1] = max(dp[j+1], dp[j]+s)
    ans = max(ans, dp[M])
print(ans)

### FIXED CODE ###

import sys

def main():
    INF = -2**60
    N, M = map(int, sys.stdin.buffer.readline().split())
    XYZ = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)]

    sgn = [[1,1,1], [1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]]
    ans = INF 
    for a, b, c in sgn:
        dp = [INF]*(M+1)
        dp[0] = 0
        for x, y, z in XYZ:
            for j in range(M-1, -1, -1):
                if dp[j] == INF: continue
                s = dp[j]+ a*x + b*y + c*z
                if s > dp[j+1]:
                    dp[j+1] = s
        if dp[M] > ans:
            ans = dp[M]
    print(ans)

main()
### BUGGY CODE ###

H,W,D=map(int,input().split());A=[list(map(int,input().split())) for _ in range(H)];Q=int(input());d=[0]*(H*W+1);g=[[0]*(H*W+1) for _ in range(H*W+1)];f=1
for i in range(H):
  for j in range(W):d[A[i][j]]=[i,j]
if f:
  for i in range(1,D+1):
    for j in range(1,H*W//D+1):
      if i+D*j<=H*W:g[i][i+j*D]=abs(d[i+j*D-D][0]-d[i+j*D][0])+abs(d[i+j*D-D][1]-d[i+j*D][1])+g[i][i+D*j-D]
  g[0]=g[D]
else:
  for i in range(1,H*W-D+1):
    g[i][i+D]=abs(d[i+D][0]-d[i][0])+abs(d[i+D][1]-d[i][1])
for _ in range(Q):
  l,r=map(int,input().split());t=l%D;a=0
  if f:print(g[t][r]-g[t][l])
  else:
    while l<r:
      a+=g[l][l+D];l+=D
    print(a)

### FIXED CODE ###

H,W,D=map(int,input().split());A=[list(map(int,input().split())) for _ in range(H)];Q=int(input());d=[0]*(H*W+1);g=[0]*(H*W+1)
for i in range(H):
  for j in range(W):d[A[i][j]]=[i,j]
for i in range(1,H*W-D+1):
  g[i+D]=abs(d[i+D][0]-d[i][0])+abs(d[i+D][1]-d[i][1])+g[i]
for q in range(Q):
  l,r=map(int,input().split());print(g[r]-g[l])
### BUGGY CODE ###


import numpy as np

def make_divisors(n):

    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def eratosthenes(limit):
    if limit ==1:
        return []

    A = [i for i in range(2, limit+1)]
    P = []
    
    while True:
        prime = min(A)
        
        if prime > np.sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

A,B = map(int,input().split())

A_s = set(make_divisors(A))
B_s = set(make_divisors(B))

divs = A_s&B_s

max_div = max(list(divs))

# prime_N=set(primes(max_div))

# ans = divs&prime_N
# ans = list(ans)

# print(len(ans)+1)

ans = set(eratosthenes(max_div))

print(len(ans&divs)+1)

### FIXED CODE ###

def prime_factor(n):
    ass = []
    for i in range(2,int(n**0.5)+1):
        while n % i==0:
            ass.append(i)
            n = n//i
    if n != 1:
        ass.append(n)
    return ass

A,B = map(int,input().split())

A_s = set(prime_factor(A))
B_s = set(prime_factor(B))

from fractions import gcd

print(len(A_s&B_s)+1)
### BUGGY CODE ###

import numpy as np
MOD = 10 ** 9 + 7

dp = np.zeros(13, dtype = np.int64)
dp[0] = 1

move = np.arange(0,52,4) % 13
for s in input():
  if s == '?':
    add = np.ones(10, dtype = np.int64)
  else:
    s = int(s)
    add = np.zeros(s+1, dtype=np.int64)
    add[-1] = 1
  dp = np.convolve(dp[move], add)
  dp[:len(dp) - 13] += dp[13:]
  dp %= MOD
  
print(dp[5])


### FIXED CODE ###

MOD = 10 ** 9 + 7
S = input()

dp = [0] * 13
dp[0] = 1

for s in S:
  prev = [x for x in dp]
  dp = [0] * 13
  if s == '?':
    add = range(10)
  else:
    add = [int(s)]
  for i in range(13):
    for x in add:
      dp[(10*i+x)%13] += prev[i]
  dp = [x % MOD for x in dp]
  
print(dp[5])
### BUGGY CODE ###

from collections import deque
N = int(input())
a = list(map(int, input().split()))
b = deque()
ans = 0

for i in range(1, N):
  t = N-1-i
  k = N-i
  s = 0
  while k-1 < N:
    if k in b:
      s += 1
    k += t+1
  if (a[t]+s) % 2 == 1:
    b.appendleft(t+1)
    ans += 1
    
if (a[0]+len(b)) % 2 == 1:
  b.appendleft(1)
  ans += 1
    
print(ans)
print(' '.join(map(str, b)))

### FIXED CODE ###

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

b = [0] * N
for i in range(N):
  tmp = 0
  n = N-i
  for j in range(n*2-1, N, n):
    if b[j] == 1:
      tmp += 1
  if a[n-1] != tmp%2:
    b[n-1] = 1

ans = []
for i in range(N):
  if b[i] == 1:
    ans.append(i+1)
if ans != []:
  print(len(ans))
  print(*ans)
else:
  print(0)
### BUGGY CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import deque
# from copy import deepcopy


def bfs(h, w, board, start_list, dx, dy):

    # キュー
    task_deque = deque()
    # キューにスタート地点リストを追加
    task_deque.append(start_list)
    next_task_list = deque()
    turn_count = 0

    while task_deque:
        next_task_list.clear()
        task_list = task_deque.popleft()


        # タスクリストからそれぞれの座標を取り出す
        for x, y in task_list:

            for i in range(len(dx)):
                nx = x + dx[i]
                ny = y + dy[i]

                # 条件に合う座標をnext_task_listに追加
                if 0 <= nx < w and 0 <= ny < h and board[ny][nx] == ".":
                    board[ny][nx] = "#"
                    next_task_list.append([nx, ny])


        # 一つもnext_task_listに入っていなければ終了
        if len(next_task_list) == 0:
            return turn_count

        # タスクリストが全部終わったらキューに追加
        turn_count += 1
        lst = []
        for task in next_task_list:
            lst.append(task)
        task_deque.append(lst)



h, w = map(int, input().split())
board = [list(input().rstrip()) for j in range(h)]

start_list = deque()

# すべてのスタート地点を記録
for i in range(w):
    for j in range(h):
        if board[j][i] == "#":
            start_list.append([i, j])

# dx,dy
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]


print(bfs(h, w, board, start_list, dx, dy))


### FIXED CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import deque
# from copy import deepcopy


def bfs(h, w, board, start_list, dx, dy):

    # キュー
    task_deque = deque()
    # キューにスタート地点リストを追加
    task_deque.append(start_list)
    next_task_list = deque()
    turn_count = 0

    while task_deque:
        next_task_list.clear()
        task_list = task_deque.popleft()


        # タスクリストからそれぞれの座標を取り出す
        for x, y in task_list:

            for i in range(len(dx)):
                nx = x + dx[i]
                ny = y + dy[i]

                # 条件に合う座標をnext_task_listに追加
                if 0 <= nx < w and 0 <= ny < h and board[ny][nx] == ".":
                    board[ny][nx] = "#"
                    next_task_list.append([nx, ny])


        # 一つもnext_task_listに入っていなければ終了
        if len(next_task_list) == 0:
            return turn_count

        # タスクリストが全部終わったらキューに追加
        turn_count += 1
        lst = []
        for task in next_task_list:
            lst.append(task)
        task_deque.append(lst)



h, w = map(int, input().split())
board = [list(input().rstrip()) for j in range(h)]

start_list = deque()

# すべてのスタート地点を記録
for i in range(w):
    for j in range(h):
        if board[j][i] == "#":
            start_list.append([i, j])

# dx,dy
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]


print(bfs(h, w, board, start_list, dx, dy))
### BUGGY CODE ###

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

answer = 0
for i in range(N):
  if P[i]==min(P[:i+1]):
    answer += 1
    
print(answer)
    

### FIXED CODE ###

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

dummy = 2*10**5
answer = 0

for p in P:
  if p <= dummy:
    answer += 1
    dummy = p

print(answer)
### BUGGY CODE ###

# Not using k-d tree.

import sys, math
from operator import itemgetter
from bisect import bisect_left, bisect_right

f_i = sys.stdin

n = int(f_i.readline())
P = [tuple(map(int, f_i.readline().split())) + (i,) for i in range(n)]
P.sort()

interval = int(math.sqrt(len(P)))

low = [x for x, y, i in P[::interval]]
high = [x for x, y, i in P[interval - 1::interval]] + [float('inf')]

P = [sorted(P[i:i + interval], key=itemgetter(1)) for i in range(0, len(P), interval)]
P = [([y for x, y, i in p_b], p_b) for p_b in P]

A = []

q = f_i.readline()

for l in f_i:
    sx, tx, sy, ty = map(int, l.split())
    ret = []
    for i in range(bisect_left(high, sx), bisect_right(low, tx)):
        k, v = P[i]
        for j in range(bisect_left(k, sy), bisect_right(k, ty)):
            if sx <= v[j][0] <=tx:
                ret.append(v[j][2])
    if ret:
        ret.sort()
        A.extend(ret)
    A.append('')

print('\n'.join(map(str, A)))

### FIXED CODE ###

import sys, math
from operator import itemgetter
from bisect import bisect_left, bisect_right

f_i = sys.stdin

def main():
    n = int(f_i.readline())
    
    lx = []
    ly = []
    li = []
    for i in range(n):
        x, y = map(int, f_i.readline().split())
        lx.append(x)
        ly.append(y)
        li.append(i)

    x_width = max(lx) - min(lx)
    y_width = max(ly) - min(ly)
    if x_width > y_width:
        P = list(zip(lx, ly, li))
    else:
        P = list(zip(ly, lx, li))
    P.sort()

    interval = int(math.sqrt(n))

    low = [v1 for v1, v2, i in P[::interval]]
    high = [v1 for v1, v2, i in P[interval - 1::interval]] + [float('inf')]

    S = []
    v2_sec_list = []
    for i in range(0, n, interval):
        si = P[i:i + interval]
        si.sort(key=itemgetter(1))
        v2_s = [p[1] for p in si]
        v2_sec_list.append(v2_s)
        S.append(si)

    A = []

    q = f_i.readline()

    for l in f_i:
        sx, tx, sy, ty = map(int, l.split())
        a = []
        if x_width > y_width:
            for i in range(bisect_left(high, sx), bisect_right(low, tx)):
                v = S[i]
                k = v2_sec_list[i]
                for j in range(bisect_left(k, sy), bisect_right(k, ty)):
                    if sx <= v[j][0] <= tx:
                        a.append(v[j][2])
        else:
            for i in range(bisect_left(high, sy), bisect_right(low, ty)):
                v = S[i]
                k = v2_sec_list[i]
                for j in range(bisect_left(k, sx), bisect_right(k, tx)):
                    if sy <= v[j][0] <= ty:
                        a.append(v[j][2])
        
        if a:
            a.sort()
            A.extend(a)
        A.append('')

    print('\n'.join(map(str, A)))

main()
### BUGGY CODE ###

s=input()
t=input()
ss=set(s)
tt=set(t)
tt <=ss
flag=0
if not tt<= ss:
    flag=1
if flag==1:
    print(-1)
else:
    ss=list(s)
    tt=list(t)
    l=len(ss)    
    count=0
    y=-1
    ind={}
    for x in tt:
        ind[x]=[i for i, xx in enumerate(ss) if xx == x]
    for x in tt:
        for z in ind[x]:
            if z>y:
                y=z
                flag=0
                break
            flag=1
        if flag==1:
            count+=1
            y=ind[x][0]
#         print(count,z,y)
    ans=count*l+z+1
    print(ans)

### FIXED CODE ###

s = input()
t = input()
r = 0
p = 0
x = 0
for i in t:
    if s.find(i) == -1:
        print(-1)
        exit()
    if p > s.find(i):
        if s[p:].find(i) == -1:
            x += 1
            p = s.find(i) + 1
        else:
            p = p + s[p:].find(i) + 1
    else:
        p = s.find(i) + 1
print(x * len(s) + p)
### BUGGY CODE ###

import sys
 
input = sys.stdin.readline
 
ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]
 
import sys
import numpy as np
import fractions
 
from functools import reduce
 
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)
 
def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)
 
def lcm_list(numbers):
    ans = 1
    for num in numbers:
        ans = ans * num // fractions.gcd(ans,num)
    return ans
 
def LI():
  return [int(s) for s in input().split()]
 
input = sys.stdin.readline
ri = lambda: int(input())
ril = lambda: list(map(int, input().split()))
 
N = ri()
A_s = ril()
mod = 10**9 + 7
 
#l = lcm_list(A_s)
l = 1
for num in A_s:
    l = l * num // fractions.gcd(l,num)

print(sum(map(lambda x : l//x, A_s)) % mod)


### FIXED CODE ###

import fractions
 
N = int(input())

def LI():
  return [int(s) for s in input().split()]

A_s = LI()
mod = 10**9 + 7

#l = lcm_list(A_s)
l = 1
for num in A_s:
    l = l * num // fractions.gcd(l,num)

count = 0
for a in A_s:
  count+=l//a
  
print(count % mod)
### BUGGY CODE ###

import bisect
import sys

mod = 10**9+7

def modInverse(a,p):
    return pow(a,p-2,p)

def nCr(n,r,p=mod):
    num = 1
    den = 1
    for i in range(r):
        num = (num*(n-i)) % p
        den = (den*(i+1)) % p
    return num*modInverse(den,p)% p

def main(args):
    
    n = int(input())
    A = list(map(int,input().split()))
    
    A.sort()
    
    ncr = 0
    for a in A[::-1][:n-1]:
        check = a//2
        right = bisect.bisect_right(A, check)
        left = right - 1
        r1 = min(A[right],a-A[right])
        r2 = min(A[left],a-A[left])
        if r1 > r2:
            r = A[right]
        else:
            r = A[left]
        
        if nCr(a, r) > ncr:
            ncr = nCr(a, r)
            ans = [a, r]
            
    print(*ans)
    
if __name__ == '__main__':
    main(sys.argv[1:])

### FIXED CODE ###

import bisect
import sys

def main(args):
    
    n = int(input())
    A = list(map(int,input().split()))
    
    A.sort()
    
    ans = [max(A), 0]
    ind = bisect.bisect_left(A ,ans[0]//2)
    if A[ind] == ans[0]//2:
        ans[1] = ans[0]//2
    else:
        r1 = min(A[ind],ans[0]-A[ind])
        r2 = min(A[ind-1],ans[0]-A[ind-1])
        if r1 > r2:
            ans[1] = A[ind]
        else:
            ans[1] = A[ind-1]
    
    print(*ans)
    
if __name__ == '__main__':
    main(sys.argv[1:])
### BUGGY CODE ###

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

lists = [0]*N

for i in range(N):
  lists[i] = A.index(i+1)+1
  
for i in lists:
  print(i, end = ' ')

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
lists = [0] * n

for i in range(n):
    lists[a[i] - 1] = i + 1
    
for i in lists:
  print(i , end =' ')
### BUGGY CODE ###

N = int(input())

def count_n(n):
    ans = 0
    for x in range(1,int(N**0.5)):
        if 6*x**2 == n:
            ans = 1
            return ans
        else:
            for y in range(1,int(N**0.5)):
                    if (3*x**2 + y**2 + 2*x*y)  == n:
                        ans = 3
                        return ans
    return ans

for i in range(1,N+1):
    print(count_n(i))

### FIXED CODE ###

n = int(input())
a = [0]*(n+1)
for i in range(1, 102):
    for j in range(1, 102):
        for k in range(1, 102):
            if (i*i + j*j + k*k + i*j + j*k + i*k) <= n:
                a[i*i + j*j + k*k + i*j + j*k + i*k] +=1
                
for x in a[1:]:
    print(x)
### BUGGY CODE ###

import sys
from heapq import heapify, heapreplace


def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    As = list(map(int, input().split()))
    heapify(As)
    BCs = [None] * M
    for i in range(M):
        BCs[i] = list(map(int, input().split()))
    BCs = reversed(sorted(BCs, key=lambda x: x[1]))
    for B, C in BCs:
        for _ in range(B):
            if As[0] < C:
                heapreplace(As, C)
    print(sum(As))


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from heapq import heapify, heapreplace


def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    As = list(map(int, input().split()))
    heapify(As)
    BCs = [None] * M
    for i in range(M):
        BCs[i] = list(map(int, input().split()))
    BCs = reversed(sorted(BCs, key=lambda x: x[1]))
    for B, C in BCs:
        for _ in range(B):
            if As[0] >= C:
                print(sum(As))
                sys.exit()
            heapreplace(As, C)
    print(sum(As))


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

import collections
from collections import deque
import numpy as np

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

ca = collections.Counter(A)
cakeys = list(ca.keys())
cavalues = np.array(list(ca.values()))

#print(cakeys)
#print(np.array(list(ca.values())))

cavaluesmax = cavalues*(cavalues-1)//2
valuesum = np.sum(cavaluesmax)
#cavaluesmin = (cavalues-1)*(cavalues-2)//2
diff = cavalues-1

ans = valuesum - diff

#print(cavaluesmax)
#print(cavaluesmin)

for i in A:
    #print(cakeys[index])
    index = cakeys.index(i)
    #ans = cavaluesmin[index].sum() + cavaluesmax[:index].sum() + cavaluesmax[index+1:].sum()
    print(ans[index])


### FIXED CODE ###

import collections
from collections import deque
import numpy as np

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

ca = collections.Counter(A)
cakeys = list(ca.keys())
cavalues = np.array(list(ca.values()))

#print(cakeys)
#print(np.array(list(ca.values())))

cavaluesmax = cavalues*(cavalues-1)//2
valuesum = np.sum(cavaluesmax)
#cavaluesmin = (cavalues-1)*(cavalues-2)//2


for a in A:
    #print(cakeys[index])
    v = ca[a]
    ans = valuesum - v + 1
    #ans = cavaluesmin[index].sum() + cavaluesmax[:index].sum() + cavaluesmax[index+1:].sum()
    print(ans)
### BUGGY CODE ###

from collections import Counter

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

for i in range(n):
    x[i]-=a
    
left=x[:n//2]
l=len(left)
right=x[n//2:]
r=len(right)

data_left=Counter()
data_right=Counter()

for i in range(1,2**l):
    num=0
    for j in range(l):
        if (i>>j)&1==1:
            num+=left[j]
    data_left[-num]+=1

for i in range(1,2**r):
    num=0
    for j in range(r):
        if (i>>j)&1==1:
            num+=right[j]
    data_right[num]+=1

ans=data_left[0]+data_right[0]
for u,v in data_left.items():
    ans+=data_right[u]*v
    
print(ans)

### FIXED CODE ###

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

for i in range(n):
    x[i]-=a
    
dp=[0]*(2*n*50+2)
dp[0]=1
for i in range(n):
    h=dp[:]
    for j in range(-(n-1)*50,(n-1)*50+1):
        h[j]+=dp[j-x[i]]
    dp=h
print(dp[0]-1)
### BUGGY CODE ###

a,b=map(int,input().split())

def cf(x1,x2):
    cf=[]
    for i in range(1,min(x1,x2)+1):
        if x1 % i == 0 and x2 % i == 0:
            if i%2==0 and i!=2:
                    continue
            cf.append(i)
    return cf

L=cf(a,b)
for i in range(1,len(L)-1):
      for j in range(i+1,len(L)):
            if L[j]%L[i]==0:
                  L.pop(j)
print(len(L))

### FIXED CODE ###

import fractions

a,b=map(int,input().split())
g=fractions.gcd(a,b)
#print(g)
ans=1
i=2
while i*i<=g:
    if(g%i==0):
        ans+=1
        while g%i==0:
            g/=i
    i=i+1
if(g!=1):ans+=1
print(ans)
### BUGGY CODE ###

import sys

n, m = map(int, input().split())
maxdiv = None
for div in reversed(range(1, 1+m//n)):
	if m % div == 0:
		print(div)
		sys.exit()

### FIXED CODE ###

import sys

n, m = map(int, input().split())
for div in reversed(range(1, 1+m//n)):
	if m % div == 0:
		print(div)
		sys.exit()
### BUGGY CODE ###

n = int(input())
s = int(input())
rtn = -1* int( -1 * (n**0.5))
def f(b,n):
    if n<b:
        return n
    return f(b,n//b) + n % b
if n < s or (n // 2)+1 < s < n :
    print("-1")
elif s == n:
    print(n+1)
elif s == n//2 +1:
    if n % 2 == 1:
        print(n//2 + 1)
    else:
        print("-1")
elif (n//3) + 2 < s <= n//2:
    print(n-s+1)
elif s == 1:
    def check(n,b):
        if n == 1:
            return b
        else:
            if n % b !=0 :
                return 0
            else:
                return check(n // b,b)
    for b in range(2,rtn+1):
        r = check(n,b)
        if r:
            print(r)
            exit()
    print(n)
else:
    x = n // s
    if s - (n //(x+1))  <= x:
        x = x + 1
    if 5 < n/(x -1) - n/x :
        for b in range(n//x, n//(x-1)):
            if f(b,n) == s:
                print(b)
                exit()
        for i in range(x-1,2,-1):
            for b in range(n//i + (n//x)//(n//i), n//i + (n//x)//(n//i)  + (2*n//x - n//(x+1))//(x-i)):
                if f(b,n) == s:
                    print(b)
                    exit()
    else:
        for b in range(2,rtn+1):
            if f(b,n) == s:
                print(b)
                exit()
        for p in range(rtn,0,-1):
            if (n-s) % p == 0:
                b = (n-s)//p + 1
                if f(b,n) == s:
                    print(b)
                    exit()
    print(n-s+1)


### FIXED CODE ###

n = int(input())
s = int(input())
rtn = -1* int( -1 * (n**0.5))
def f(b,n):
    if n<b:
        return n
    return f(b,n//b) + n % b
if n < s or (n // 2)+1 < s < n :
    print("-1")
elif s == n:
    print(n+1)
elif s == n//2 +1:
    if n % 2 == 1:
        print(n//2 + 1)
    else:
        print("-1")
elif (n//3) + 2 < s <= n//2:
    print(n-s+1)
else:
    for b in range(2,rtn+1):
        if f(b,n) == s:
            print(b)
            exit()
    for p in range(rtn,0,-1):
        if (n-s) % p == 0:
            b = (n-s)//p + 1
            if f(b,n) == s:
                print(b)
                exit()
    print(n-s+1)
### BUGGY CODE ###

import sys
import bisect
input = sys.stdin.readline
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
s = sorted(s)
t = sorted(t)

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_right(s, x)
    idx2 = bisect.bisect_right(t, x)
    ans = float('inf')
    if idx1 > 0:
        left_s = s[idx1-1]
    else:
        left_s = float('inf')
    if idx2 > 0:
        left_t = t[idx2-1]
    else:
        left_t = float('inf')
    if idx1 < A:
        right_s = s[idx1]
    else:
        right_s = float('inf')
    if idx2 < B:
        right_t = t[idx2]
    else:
        right_t = float('inf')

    ls = x-left_s
    lt = x-left_t
    rs = right_s-x
    rt = right_t-x 
    if left_s <= x <= right_t:
        tmp = right_t-left_s + min(ls, rt)
        ans = min(ans, tmp)
    if left_t <= x <= right_s:
        tmp = right_s-left_t + min(lt, rs)
        ans = min(ans, tmp)
    ans = min(ans, max(abs(ls), abs(lt)))
    ans = min(ans, max(abs(rs), abs(rt)))
    print(ans)


### FIXED CODE ###

import sys
import bisect
input = sys.stdin.readline
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
s = sorted(s)
t = sorted(t)

for _ in range(Q):
    x = int(input())
    idx1 = bisect.bisect_right(s, x)
    idx2 = bisect.bisect_right(t, x)
    ans = float('inf')
    if idx1 > 0:
        left_s = s[idx1-1]
    else:
        left_s = float('inf')
    if idx2 > 0:
        left_t = t[idx2-1]
    else:
        left_t = float('inf')
    if idx1 < A:
        right_s = s[idx1]
    else:
        right_s = float('inf')
    if idx2 < B:
        right_t = t[idx2]
    else:
        right_t = float('inf')

    ls = x-left_s
    lt = x-left_t
    rs = right_s-x
    rt = right_t-x 
    if left_s <= x <= right_t:
        tmp = right_t-left_s + min(ls, rt)
        ans = min(ans, tmp)
    if left_t <= x <= right_s:
        tmp = right_s-left_t + min(lt, rs)
        ans = min(ans, tmp)
    ans = min(ans, max(abs(ls), abs(lt)))
    ans = min(ans, max(abs(rs), abs(rt)))
    print(ans)