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

from collections import deque
S=input()
d=deque(S)
rev=False
Q=int(input())
for i in range(Q):
    Que=input().split()
    if(Que[0]=='1'):
        rev=not rev
    else:
        if(Que[1]=='1'):
            if(not rev):
                d.appendleft(Que[2])
            else:
                d.append(Que[2])
        else:
            if(not rev):
                d.append(Que[2])
            else:
                d.appendleft(Que[2])
if(not rev):
    for i in range(len(d)):
        print(d[i],end='')
else:
    for i in reversed(range(len(d))):
        print(d[i],end='')

### FIXED CODE ###

from collections import deque
S=input()
d=deque(S)
rev=False
Q=int(input())
for i in range(Q):
    Que=input()
    if(Que=='1'):
        rev=True if not rev else False
    else:
        T,F,C=map(str,Que.split())
        if(F=='1'):
            if(not rev):
                d.appendleft(C)
            else:
                d.append(C)
        elif(F=='2'):
            if(not rev):
                d.append(C)
            else:
                d.appendleft(C)
if(not rev):
    for i in range(len(d)):
        print(d[i],end='')
else:
    for i in reversed(range(len(d))):
        print(d[i],end='')
### BUGGY CODE ###

n = int(input())
A = list(map(int,input().split()))
ans = 1
for i in A:
    ans *= i
if ans >= 10**18 + 1:
    ans = -1
print(ans)


### FIXED CODE ###

n = int(input())
A = list(map(int,input().split()))
ans = 1
for i in A:
    ans *= i
    ans = min(ans,10**18 + 1)
if ans >= 10**18 + 1:
    ans = -1
print(ans)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
s=[0]
ans=0
for i in range(n):
    S=s[i]+a[i]
    s.append(S)
tmp=list(set(s))
for i in range(len(tmp)):
    C=s.count(tmp[i])
    ans+=C*(C-1)//2
print(ans)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
s=[0]
ans=0
for i in range(n):
    S=s[i]+a[i]
    s.append(S)
s.sort()
cnt=1
for i in range(n):
    if s[i]==s[i+1]:
        cnt+=1
    else:
        ans+=cnt*(cnt-1)//2
        cnt=1
ans+=cnt*(cnt-1)//2
print(ans)
### BUGGY CODE ###

import re

def xor(x,y):
  x = int(bin(x)[2:]) if type(x) == int else int(x)
  y = int(bin(y)[2:]) if type(x) == int else int(y)
  ret = re.sub("2", "0", str(x+y))
  return ret
  
input()
s = list(map(int, input().split()))

ans = 0
r = 0
x = 0
y = 0
for i in range(len(s)):
  ans += r - i
  for j in range(r, len(s)):
    x = x + s[j]
    y = xor(y, s[j])
    if bin(x) == "0b"+y:
      ans += 1
      r += 1
    else:
      break
  x = x - s[i] - s[j]
  y = xor(y, s[i])
  y = xor(y, s[j])

print(ans)

### FIXED CODE ###

def xor(x,y):
  return str(int(x)+y).replace("2", "0")
  
input()
s = list(map(int, input().split()))
t = list(map(lambda x: int(bin(x)[2:]), s))

ans = 0
r = 0
x = 0
y = "0"
for i in range(len(s)):
  ans += r - i
  for j in range(r, len(s)):
    nx = x + s[j]
    ny = xor(y, t[j])
    if bin(nx)[2:] == ny:
      ans += 1
      r += 1
      x = nx
      y = ny
    else:
      break
  x = x - s[i]
  y = xor(y, t[i])

print(ans)
### BUGGY CODE ###

from collections import defaultdict
N,Q = map(int,input().split())
d = defaultdict(list)

for _ in range(N-1):
    a,b = map(int,input().split())
    d[a].append(b)
    d[b].append(a)

c = defaultdict(int)

for _ in range(Q):
    p,x = map(int,input().split())
    c[p] += x

ans = [0 for i in range(N)]
que = [(1,0,-1)]
while len(que)>0:
    tmp = []
    for v,x,par in que:
        x += c[v]
        ans[v-1] = str(x)
        for w in d[v]:
            if w != par:
                tmp.append((w,x,v))
    que = tmp

print(' '.join(ans))


### FIXED CODE ###

from collections import defaultdict
N,Q = map(int,input().split())
d = defaultdict(list)

for _ in range(N-1):
    a,b = map(int,input().split())
    d[a].append(b)
    d[b].append(a)

c = defaultdict(int)

for _ in range(Q):
    p,x = map(int,input().split())
    c[p] += x

ans = [0 for i in range(N)]
que = [(1,0,-1)]
while len(que)>0:
    tmp = []
    for v,x,par in que:
        x += c[v]
        ans[v-1] = str(x)
        for w in d[v]:
            if w != par:
                tmp.append((w,x,v))
    que = tmp

print(*ans)
### BUGGY CODE ###

N,K = map(int,input().split())

X = []
Y = []
Z = []

for i in range(N):
  x,y = map(int,input().split())
  X.append(x)
  Y.append(y)
  Z.append([x,y])
  
T = max(Y)
U = min(Y)
R = max(X)
L = min(X)
Y.sort()
X.sort()
ans = 10**100

for t in range(N):
  for u in range(N):
    for r in range(N):
      for l in range(N):
        T = Y[N-t-1]
        U = Y[u]
        R = X[N-r-1]
        L = X[l]
        cnt = 0
        for i in range(N):
          xi = Z[i][0]
          yi = Z[i][1]
          if xi >= L and xi <= R and yi >= U and yi <= T:
            cnt += 1
        if cnt >= K:
          M = int( (T-U) * (R-L) )
          ans = min(ans,M)
          
print(ans)

### FIXED CODE ###

N,K = map(int,input().split())

X = []
Y = []
Z = []

for i in range(N):
  x,y = map(int,input().split())
  X.append(x)
  Y.append(y)
  Z.append([x,y])
  
Y.sort()
X.sort()
ans = 10**100

for t in range(N):
  for u in range(N):
    for r in range(N):
      for l in range(N):
        T = Y[N-t-1]
        U = Y[u]
        R = X[N-r-1]
        L = X[l]
        
        if T <= U or R <= L:
          continue
        
        cnt = 0
        for i in range(N):
          xi = Z[i][0]
          yi = Z[i][1]
          if xi >= L and xi <= R and yi >= U and yi <= T:
            cnt += 1
        if cnt >= K:
          M = int( (T-U) * (R-L) )
          ans = min(ans,M)
          
print(ans)
### BUGGY CODE ###

import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	#print(name)
	sys.stdin = open(name)


#nCrの計算。nこの中から、r個を取り出す。
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

import copy


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

b = [0]*(n+1)
b[0] = 0
for i in range(1,n+1):
	b[i] = b[i-1] + a[i-1]

#print(b)

c = [0]*(n+1)
c = copy.copy(b)
c = list(set(c))
#print(c)

ans = 0

for i in c:
	#print(i)
	chk = b.count(i)
	#print(chk)
	if chk >= 2:
		ans = ans + chk*(chk-1)//2

print(ans)




### FIXED CODE ###

import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	#print(name)
	sys.stdin = open(name)


#nCrの計算。nこの中から、r個を取り出す。
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

import copy


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

b = [0]*(n+1)
b[0] = 0
for i in range(1,n+1):
	b[i] = b[i-1] + a[i-1]

#print(b)

#c = [0]*(n+1)
#c = copy.copy(b)
#c = list(set(c))
#print(c)

import collections
c = collections.Counter(b)
#print(c)
#print(c.values())

ans = 0

for i in c.values():
	#print(i)
	#chk = b.count(i)

	#print(chk)
	if i >= 2:
		ans = ans + i*(i-1)//2

print(ans)
### BUGGY CODE ###

n,m=map(int, input().split())
s=set(range(1, n+1)) #1~nのset
for i in range(m):
  l,r=map(int, input().split())
  s= s & set(range(l, r+1))
print(len(s))

### FIXED CODE ###

n,m=map(int, input().split())
lmax=0
rmin=1000000
for i in range(m):
  l,r=map(int, input().split())
  lmax=max(l, lmax)
  rmin=min(r, rmin)

if rmin-lmax>=0:
  print(rmin-lmax+1)
else:
  print(0)
### BUGGY CODE ###

import sys, math
from collections import deque

readline = sys.stdin.readline

S = int(readline())

M = 10 ** 9
rS = math.ceil(math.sqrt(S))

while True:
    rem = rS ** 2 - S


    if rem <= M:
        b = rem
        break
    else:
        b = 0
        a = rem // M + 1
        c = math.floor(rem ** 0.5)
        while a < c and a < M:
            if rem % a == 0:
                b = a
                break
            a += 1
        if b != 0:
            break
    
    if rS >= M:
        rS = M - 1000
    else:
        rS += 1

if b==0:
    while True:
        a = 1

print('0 0 {} {} {} {}'.format(rS, b, rem // b, rS))

### FIXED CODE ###

import sys, math
from collections import deque

readline = sys.stdin.readline

S = int(readline())

M = 10 ** 9
rS = math.ceil(math.sqrt(S))

while True:
    rem = rS ** 2 - S

    if rem == 0:
        print('0 0 {} {} {} {}'.format(rS, 0, 0, rS))
        exit(0)

    if rem <= M:
        b = rem
        break
    else:
        b = 0
        a = rem // M + 1
        c = math.floor(rem ** 0.5)
        while a < c and a < M:
            if rem % a == 0:
                b = a
                break
            a += 1
        if b != 0:
            break
    
    if rS >= M:
        rS = M - 1000
    else:
        rS += 1


print('0 0 {} {} {} {}'.format(rS, b, rem // b, rS))
### BUGGY CODE ###

from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
base = ["#" *(w+4)]
base.append("#" * (w+4))
for n in range(h):
    base.append("##" + input() + "##")
base.append("#" * (w+4))
base.append("#" * (w+4))
ans = [[-1] * (w+4) for _ in range(h+4)]
for j in range(w+4):
    for i in range(h+4):
        if base[i][j] == "#":
            ans[i][j] = -2
ch+=1; cw+=1
dh+=1; dw+=1
ans[ch][cw] = 0
move = [(1, 0), (-1, 0), (0, 1), (0, -1)]
move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2), (-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2), (0, -2), (0, -1), (0, 0), (0, 1), (0, 2), (1, -2), (1, -1), (1, 0), (1, 1), (1, 2), (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]
q = deque()
one_time = deque()
q.append([ch, cw])
while q:
    x, y = q.popleft()
    one_time.append((x, y))
    for (i, j) in move:
        v1, v2 = x+i, y+j
        if ans[v1][v2] == -1:
            ans[v1][v2] = ans[x][y]
            q.append((v1, v2))
    if len(q) == 0:
        while one_time:
            x2, y2 = one_time.popleft()
            for (i, j) in move2:
                v1, v2 = x2+i, y2+j
                if ans[v1][v2] == -1:
                    ans[v1][v2] = ans[x2][y2] + 1
                    q.append((v1, v2))
print(ans[dh][dw])

### FIXED CODE ###

from collections import deque
 
 
# In[30]:
 
 
def main():
    H,W = map(int, input().split())
    ch,cw = map(int, input().split())
    dh,dw = map(int, input().split())
    s = ["#"*(W+4)]
    s.append("#"*(W+4))
    for i in range(H):
        s.append("##"+input()+"##")
    s.append("#"*(W+4))
    s.append("#"*(W+4))
    
    ans = [[-1]*(W+4) for _ in range(H+4)]
    for i in range(H+4):
        for j in range(W+4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ch += 1; cw += 1
    dh += 1; dw += 1
    ans[ch][cw] = 0
    
    move = [[-1,0],[1,0],[0,-1],[0,1]]
    magic = [
        [-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],\
        [-1,-2],[-1,-1],[-1,0],[-1,1],[-1,2],\
        [0,-2],[0,-1],[0,0],[0,1],[0,2],\
        [1,-2],[1,-1],[1,0],[1,1],[1,2],\
        [2,-2],[2,-1],[2,0],[2,1],[2,2]
        ]
    
    mylist = deque([[ch,cw]])
    mylist2 = deque([])
    while len(mylist) > 0:
        x,y = mylist.popleft()
        mylist2.append([x,y])
        for p,q in move:
            v1,v2 = x+p,y+q
            if ans[v1][v2] == -1:
                mylist.append([v1,v2])
                ans[v1][v2] = ans[x][y]
        if len(mylist) == 0:
            while len(mylist2) > 0:
                x2,y2 = mylist2.popleft()
                for v1,v2 in magic:
                    i,j = x2+v1,y2+v2
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2]+1
                        mylist.append([i,j])
    print(ans[dh][dw])
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from fractions import Fraction
import sys

N = int(input())

dict_F = {}
count_0 = 0

def insert(A,B,s): # A>=0, B>0, s=0or1
  if Fraction(A,B) not in dict_F.keys():
    dict_F[Fraction(A,B)] = [0,0]
  dict_F[Fraction(A,B)][s] += 1

def power2(A):
  result = 1
  for _ in range(A):
    result = (2*result)%mod
  return result

for _ in range(N):
  A,B = map(int, sys.stdin.readline().split())
  if A==0 and B==0:
    count_0 += 1
  elif A>=0 and B>0:
    insert(A,B,0)
  elif A>0 and B<=0:
    insert(-B,A,1)
  elif A<=0 and B<0:
    insert(-A,-B,0)
  elif A<0 and B>=0:
    insert(B,-A,1)

mod = 1000000007
answer = 1
for A,B in dict_F.values():
  answer = (answer*(power2(A)+power2(B)-1))%mod
answer = (answer+count_0-1)%mod
print(answer)

### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
from math import gcd

N, *AB = map(int, sys.stdin.buffer.read().split())

dict_F = {}
count_0 = 0

def insert(A,B,s): # A>=0, B>0, s=0or1
  if (A,B) not in dict_F.keys():
    dict_F[(A,B)] = [0,0]
  dict_F[(A,B)][s] += 1

def make_power2(N):
  result = 1
  list_result = [1]
  for _ in range(N):
    result = (2*result)%mod
    list_result.append(result)
  return list_result

for i in range(N):
  A,B = AB[2*i], AB[2*i+1]
  if A==0 and B==0:
    count_0 += 1
    continue
  g = gcd(A,B)
  A //= g
  B //= g
  if A>=0 and B>0:
    insert(A,B,0)
  elif A>0 and B<=0:
    insert(-B,A,1)
  elif A<=0 and B<0:
    insert(-A,-B,0)
  elif A<0 and B>=0:
    insert(B,-A,1)

mod = 1000000007
power2 = make_power2(N)
answer = 1

for A,B in dict_F.values():
  answer = (answer*(power2[A]+power2[B]-1))%mod
answer = (answer+count_0-1)%mod

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


s = input()

a = ''
for i in range(len(s)):
    if s[i]!='x':
        a+=s[i]

b = ''
for i in range(len(a)):
    b+=a[-1-i]

if a!=b:
    print(-1)
else:
    ss = ''
    for i in range(len(s)):
        ss += s[-1-i]
    i = 0
    j=0
    ans = 0
    while True:
        if i>=len(s):
            ans += len(ss)-j
            break
        elif j>=len(ss):
            ans += len(s)-i
            break
        if s[i]!=ss[j]:
            if s[i]=='x':
                i+=1
            else:
                j+=1
            ans += 1
        else:
            i+=1
            j+=1
    print(ans//2)



### FIXED CODE ###

s = input()

a = ''
for i in range(len(s)):
    if s[i]!='x':
        a+=s[i]

b = ''
for i in range(len(a)):
    b+=a[-1-i]

if a!=b:
    print(-1)
else:
    ss = ''
    for i in range(len(s)):
        ss += s[-1-i]
    i = 0
    j=0
    ans = 0
    while True:
        if i>=len(s):
            ans += len(ss)-j
            break
        elif j>=len(ss):
            ans += len(s)-i
            break
        if s[i]!=ss[j]:
            if s[i]=='x':
                i+=1
            else:
                j+=1
            ans += 1
        else:
            i+=1
            j+=1
    print(ans//2)
### BUGGY CODE ###

S = list(map(int,input().split()))
N = S[0]
M = S[1]
ans = 1
for x in range(N,M//2+1):
    if M % x == 0:
        ans = M // x
        break

print(ans) 

### FIXED CODE ###

import math

S = list(map(int,input().split()))
N = S[0]
M = S[1]
div = list()
z = math.sqrt(M)
z = math.ceil(z)

for x in range(1,z):
    if M % x == 0:
        div.append(x)
        div.append(M//x)
div.append(M)
div.append(N)

sorted_div = sorted(div)
n = sorted_div.index(N)

y =sorted_div[n+1]

ans = M // y

print(ans)
### BUGGY CODE ###

n = int(input())
li = list(map(int, input().split()))
mid = (len(li)//2)-1

for i in range(n):
    temp = li[:i] + li[i+1:]
    temp.sort()
    print(temp[mid])

### FIXED CODE ###

n = int(input())
li = list(map(int, input().split()))
mid_id = (len(li)//2)
temp = li.copy()
temp.sort()
mid1 = temp[mid_id-1]
mid2 = temp[mid_id]
for i in li:
    if i <= mid1:
        print(mid2)
    else:
        print(mid1)
### BUGGY CODE ###

def gcd(a,b):
	if b == 0:
		return a
	return gcd(b,a%b)

def lcm(a,b):
	p = a*b
	g = gcd(a,b);
	return p//g
MOD = int(10**9+7)
n = int(input())
a = list(map(int,input().split()))
lc = a[0];
for i in range(n):
	lc = lcm(a[i],lc)
ans = 0
for i in range(n):
	ans += (lc//a[i])
	ans %= MOD
print(ans)

### FIXED CODE ###

def gcd(a,b):
	if b == 0:
		return a
	return gcd(b,a%b)

def lcm(a,b):
	p = a*b
	g = gcd(a,b);
	return p//g
MOD = int(10**9+7)
n = int(input())
a = list(map(int,input().split()))
lc = a[0];
for i in range(1,n):
	lc = lcm(a[i],lc)
ans = 0
for i in range(n):
	ans += (lc//a[i])
print(ans%MOD)
### BUGGY CODE ###

a = list(map(int,input().split()))
 
for i in range(a[2]):
  if a[0] != 0:
    a[0] = a[0] - 1
  elif a[1] != 0:
    a[1] = a[1] - 1
 
print(a[0],a[1])

### FIXED CODE ###

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

if A >= K:
  ans_A = A - K
  ans_B = B
elif A + B >= K:
  ans_A = 0
  ans_B = B - (K - A)
else:
  ans_A = 0
  ans_B = 0

print(ans_A,ans_B)
### BUGGY CODE ###

N = int(input())
box = []
S = input()
box.append(S)
count = N
for i in range(N-1):
    S = input()
    for j in range(len(box)):
        if S == box[j]:
            count = count - 1
            break
        box.append(S)
print(count)

### FIXED CODE ###

N = int(input())

clip = set()
for i in range(N):
    S = input()
    clip.add(S)

    
print(len(clip))
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
n = len(A)
ans = 0

for i in range(n):
    for j in range(n):
        if i != j:
            ans += A[i] * A[j]

ans = ans // 2
ans = ans % (10 ** 9 + 7)
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
n = len(A)
z = 10 ** 9 + 7
ans = 0

x = sum(A)
x = x ** 2
for i in A:
    y = i * i
    x -= y

x = x // 2
x = x % z
print(x)
### BUGGY CODE ###

# from pprint import pprint
# import math
# import collections
from numba import jit

N = int(input())

loops = 0


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

@jit('i8(i8,i8,i8)', cache=True)
def calc(x, y, z):
    return x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x


# 全数字が等しい場合
p_eq = [0] * (100 + 1)
for xyz in range(1, 100 + 1):
    p_eq[xyz] = calc(xyz, xyz, xyz)

p_ne = []
for x in range(1, 100):
    for y in range(1, 100):
        if x + y > 100:
            break
        for z in range(1, 100):
            if x + y + z > 100:
                break
            res = calc(x, y, z)
            p_ne.append(res)

# p_set = p

# p_equal = []
# p_nonequal = []
# for _p in p_set:
#     loops += 1
#     x, y, z = _p[0], _p[1], _p[2]
#
#     if x == y == z:
#         p_equal.append(calc(x, y, z))
#     else:
#         p_nonequal.append(calc(x, y, z))

# print(p_equal)
# print(p_nonequal)

res = [None] * (N+1)
for i in range(1, N+1):
    if i in p_eq:
        res[i] = 1
    elif i in p_ne:
        res[i] = 3
    else:
        res[i] = 0

for s in res[1:]:
    print(s)
# print('lp', loops)


### FIXED CODE ###

# from pprint import pprint
# import math
# import collections
from numba import jit

N = int(input())

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

@jit(cache=True)
def calc(x, y, z):
    return x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x


res = [0] * 1000009

for x in range(1, 100):
    for y in range(1, 100):
        for z in range(1, 100):
            n = calc(x, y, z)
            res[n - 1] += 1

for i in range(N):
    print(res[i])
### BUGGY CODE ###

def main():
    import numpy as np
    import sys
    inf = 100100100
    ans = inf
    H, W, K = map(int, input().split())
    S = [0]*H
    for i in range(H):
        S[i] = [int(x) for x in sys.stdin.readline().strip()]
    S = np.array(S)
    for i in range(2**(H-1)):
        group = [0]
        g = 0
        for j in range(H-1):
            if (i >> j) & 1:
                g += 1
            group.append(g)
        group = np.array(group)
        g += 1
        c = np.array([[0]*W]*g)
        for j in range(H):
            for k in range(W):
                c[group[j],k] += S[j,k]
        num = g-1
        now = np.zeros(g)
        def large(now):
            for n in now:
                if n > K:
                    return True
            else:
                return False
        for k in range(W):
            now += c[:,k]
            if large(now):
                now = c[:,k]
                num += 1
                if large(now):
                    num = inf
                    break
        ans = min(ans, num)
    print(ans)

if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    import sys
    inf = 100100100
    ans = inf
    H, W, K = map(int, input().split())
    S = [0]*H
    for i in range(H):
        S[i] = [int(x) for x in sys.stdin.readline().strip()]
    for i in range(2**(H-1)):
        group = [0]
        g = 0
        for j in range(H-1):
            if (i >> j) & 1:
                g += 1
            group.append(g)
        g += 1
        c = [[0 for _ in range(W)] for __ in range(g)]
        for j in range(H):
            for k in range(W):
                c[group[j]][k] += S[j][k]
        num = g-1
        now = [0]*g
        def large(now):
            for n in now:
                if n > K:
                    return True
            else:
                return False
        def v_vec(twodlis, k):
            ans = []
            for lis in twodlis:
                ans.append(lis[k])
            return ans
        for k in range(W):
            for n in range(len(now)):
                now[n] = now[n] + v_vec(c,k)[n]
            if large(now):
                now = v_vec(c,k)
                num += 1
                if large(now):
                    num = inf
                    break

        ans = min(ans, num)
    print(ans)

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

import copy
N = int(input())
A = []
for i in range(N):
  tmp = int(input())
  A.append(tmp)

B = copy.copy(A)
B.sort()

for i in range(N):
  if A[i] == max(B):
    print(B[N-2])
  else:
    print(B[N-1])

### FIXED CODE ###

import copy
N = int(input())
A = []
for i in range(N):
  tmp = int(input())
  A.append(tmp)

B = copy.copy(A)
B.sort()

max_val = max(B)
for i in range(N):
  if A[i] == max_val:
    print(B[N-2])
  else:
    print(B[N-1])
### BUGGY CODE ###

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

def main():
  N_a, N_b, N_q = map(int, input().strip().split(' '))
  As = list(map(int, [input().strip() for _ in range(N_a)]))
  Bs = list(map(int, [input().strip() for _ in range(N_b)]))
  Qs = list(map(int, [input().strip() for _ in range(N_q)]))

  for q in Qs:
    min_a_l = -(2*(10**10)+1)
    min_b_l = -(2*(10**10)+1)
    min_a_r = 2*(10**10)+1
    min_b_r = 2*(10**10)+1
    i = 0
    for a in As:
      da = a - q
      if da < 0:
        i += 1
      elif da > 0:
        min_a_r = da
        if i-1>=0:
          min_a_l = As[i-1] - q
        break
    if i == len(As):
      min_a_l = As[-1] - q


    i = 0
    for b in Bs:
      db = b - q
      if db < 0:
        i += 1
      elif db > 0:
        min_b_r = db
        if i-1>=0:
          min_b_l = Bs[i-1] - q
        break
    if i == len(Bs):
      min_b_l = Bs[-1] - q

    score = min([
      min_a_r if min_a_r > min_b_r else min_b_r,
      abs(min_a_l) if min_a_l < min_b_l else abs(min_b_l),
      2*min_a_r + abs(min_b_l) if min_a_r < abs(min_b_l) else min_a_r + 2*abs(min_b_l),
      2*abs(min_a_l) + abs(min_b_r) if abs(min_a_l) < abs(min_b_r) else abs(min_a_l) + 2*abs(min_b_r)])
    print(score)

  pass

if __name__=='__main__':
  main()

### FIXED CODE ###

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

from bisect import bisect_left

def main():
  N_a, N_b, N_q = map(int, input().strip().split(' '))
  As = list(map(int, [input().strip() for _ in range(N_a)]))
  Bs = list(map(int, [input().strip() for _ in range(N_b)]))
  Qs = list(map(int, [input().strip() for _ in range(N_q)]))

  for q in Qs:
    min_a_l = -(2*(10**10)+1)
    min_b_l = -(2*(10**10)+1)
    min_a_r = 2*(10**10)+1
    min_b_r = 2*(10**10)+1
    a_index = bisect_left(As, q)
    if len(As) > a_index-1 >= 0:
      min_a_l = As[a_index-1] - q
    if a_index < len(As):
      min_a_r = As[a_index] - q

    b_index = bisect_left(Bs, q)
    if len(Bs) > b_index-1 >= 0:
      min_b_l = Bs[b_index-1] - q
    if b_index < len(Bs):
      min_b_r = Bs[b_index] - q

    score = min([
      min_a_r if min_a_r > min_b_r else min_b_r,
      abs(min_a_l) if min_a_l < min_b_l else abs(min_b_l),
      2*min_a_r + abs(min_b_l) if min_a_r < abs(min_b_l) else min_a_r + 2*abs(min_b_l),
      2*abs(min_a_l) + abs(min_b_r) if abs(min_a_l) < abs(min_b_r) else abs(min_a_l) + 2*abs(min_b_r)])
    print(score)

  pass

if __name__=='__main__':
  main()