Harryxun/stf_run huggingface dataset:
#encoding=utf-8
import sys

def inp():
    for line in sys.stdin:
        word = line.split()
        je(word[0])

def je(word):
    ans, i = 0, 0
    while True:
        print word[i], ans
        if word[i] == "M":
            ans += 1000
        elif word[i] == "D":
            ans += 500
        elif word[i] == "C":
            ans += 100
            try:
                if word[i + 1] == "D":
                    ans += 300
                    i += 1
                elif word[i + 1] == "M":
                    ans += 800
                    i += 1
            except:pass
        elif word[i] == "L":
            ans += 50
            try:
                if word[i - 1] == "X":
                    ans -= 10
                    i += 1
            except: pass
        elif word[i] == "X":
            ans += 10
            try:
                if word[i + 1] == "I" and word[i + 2] == "X":
                    ans += 9
                    i += 2
                elif word[i + 1] == "C":
                    ans += 80
                    i += 1

            except: pass
        elif word[i] == "V":
            ans += 5
        elif word[i] == "I":
            ans += 1
            try:
                if word[i + 1] == "V":
                    ans += 4
                    i += 1
                if word[i + 1] == "X":
                    ans += 8
                    i += 1
            except: pass
        i += 1
        if i == len(word):
            print ans
            break

if __name__ == "__main__":
    inp()

### FIXED CODE ###

#encoding=utf-8
import sys

def inp():
    for line in sys.stdin:
        word = line.split()
        je(word[0])

def je(word):
    ans, i = 0, 0
    while True:
        if word[i] == "M":
            ans += 1000
        elif word[i] == "D":
            ans += 500
        elif word[i] == "C":
            ans += 100
            try:
                if word[i + 1] == "M":
                    ans += 800
                    i += 1
                elif word[i + 1] == "D":
                    ans += 300
                    i += 1
            except: pass

        elif word[i] == "L":
            ans += 50
        elif word[i] == "X":
            ans += 10
            try:
                if word[i + 1] == "C":
                    ans += 80
                    i += 1
                elif word[i + 1] == "L":
                    ans += 30
                    i += 1
            except: pass

        elif word[i] == "V":
            ans += 5
        elif word[i] == "I":
            ans += 1
            try:
                if word[i + 1] == "X":
                    ans += 8
                    i += 1
                elif word[i + 1] == "V":
                    ans += 3
                    i += 1
            except: pass

        i += 1
        if i == len(word):
            print ans
            break

if __name__ == "__main__":
    inp()
S = int(raw_input())

h = S / 3600
m = S - 3600*h
s = m % 60
m = m - s

S = str(h) + ???:??? + str(m) + ???:??? + str(s)
print S

### FIXED CODE ###

S = int(raw_input())

h = S / 3600
m = S - 3600*h
s = m % 60
m = (m - s)/60

print str(h) + ":" + str(m) + ":" + str(s)
n,x = map(int, input().split())

a = list(map(int, input().split())
before = sum(a)

for i in range(n-1):
	while a[i] + a[i+1] > x:
		a[i+1] -= 1
		
after = sum(a)

print(after - before)

### FIXED CODE ###

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

cnt, a[0] = sum(a), min(a[0], x)
for i in range(1, n):
    if a[i - 1] + a[i] > x:
        a[i] = x - a[i - 1]
print(cnt - sum(a))
N, X, Y = list(map(int, input()))

import numpy as np
# N = 5
# X = 2
# Y = 4

dist = np.full(N*N, float("inf")).reshape((N, N))
np.fill_diagonal(dist, 0)
# np.fill_diagonal(dist[1:], 1)
np.fill_diagonal(dist[:, 1:], 1)
dist[X, Y] = 1
# dist[Y, X] = 1
for i in range(N):
    for j in range(i+1, N):
        for k in range(0, N):
            print(i, k, j, dist[i, k]+dist[k, j])
            dist[i, j] = min(dist[i, j], dist[i, k]+dist[k, j])
for i in range(1, N):
    count = np.count_nonzero(dist == i)
    print(count)


### FIXED CODE ###


N, X, Y = list(map(int, input().split()))

# N = 2000
# X = 100
# Y = 1900


X -= 1
Y -= 1

# @profile


def main(N, X, Y):
    ans = [0]*N
    for i in range(N):
        for j in range(i+1, N):
            d = min(j-i, abs(X-i)+abs(Y-j)+1)
            ans[d] += 1

    for a in ans[1:]:
        print(a)


main(N, X, Y)
a=[int(i) for i in input().split()]
a.sort()
if a=[5, 5, 7]:
 print("YES")
else print("NO");
 
  

### FIXED CODE ###

a=input().split()
a=[int(i) for i in a]
a.sort()
if a[0]==5 and a[1]==5 and a[2]==7:
	print("YES")
else:
 	print("NO")
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    # print(nizi)
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] += nizi[B]
    nizi[B] = 0
    print(sumall)

### FIXED CODE ###



N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10**5+1)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    # print(nizi)
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] += nizi[B]
    nizi[B] = 0
    print(sumall)
from itertools import permutations
from functools import lru_cache
from sys import setrecursionlimit
setrecursionlimit(1000000)

#@lru_cache(None)
def doit(a, b, c, idx):
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    return min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    for p in permutations(abc):
        ret = min(ret, doit(*p, 0))
print(ret)

### FIXED CODE ###

from sys import setrecursionlimit
setrecursionlimit(1000000)

def permute(a, idx):
    if idx == len(a):
        permutations.append(a[::])
    for i in range(idx, len(a)):
        a[i], a[idx] = a[idx], a[i]
        permute(a, idx + 1)
        a[i], a[idx] = a[idx], a[i]

def doit(a, b, c, idx):
    if (a, b, c, idx) in memo:
        return memo[a, b, c, idx]
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    memo[a, b, c, idx] = min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )
    return memo[a, b, c, idx]

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    permutations = []
    permute(abc, 0)
    for p in permutations:
        memo = {}
        ret = min(ret, doit(p[0], p[1], p[2], 0))
print(ret)
import sys
read = sys.stdin.readline
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
MOD = 10 ** 9 + 7

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

k = np.arange(K, N + 2, dtype=np.int64)
low = (k - 1) * k // 2
high = N * k - low
cnt = high - low + 1
print(cnt.sum() % MOD)


### FIXED CODE ###

N, K = map(int, input().split())
S1 = 0
S2 = 0
for k in range(1, N+1):
    S1 += k*N - k*(k-1) +1
    S1 %= 10**9 + 7
for k in range(1, K):
    S2 += k*N - k*(k-1) +1
    S2 %= 10**9 + 7
    
S = S1 - S2 + 1
S %= 10**9 +7

print(S)
# 2019/09/28
from fractions import gcd

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


# 約数列挙

def divisors(n):
    div=[]
    if n%2==0:div.append(2)
    for i in range(1,int(n**0.5)+1):
        if n%i==0:
            if i%2:
                div.append(i)
                if i!=n//i and n//i%2:
                    div.append(n//i)
    # div.sort()
    return div

diva=divisors(a)
divb=divisors(b)
div=set()
for aa,bb in zip(diva,divb):
    if aa in divb:
        div.add(aa)
    if bb in diva:
        div.add(bb)



def sieve(n):
    end=int(pow(n,0.5))+1
    is_prime=[True]*(n+1) 
    is_prime[0]=False
    is_prime[1]=False

    for i in range(2,end):
        if not is_prime[i]:continue
        for j in range(i*2,n+1,i):
        #for j in range(i*(i|0),n+1,i):
            is_prime[j]=False
    return [i for i in range(n+1) if is_prime[i]]


cnt=1

prime=sieve(max(div))

for e in div:
    if e in prime:
        cnt+=1
print(cnt)

### FIXED CODE ###

# 2019/09/30

from fractions import gcd

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

g=gcd(a,b)


def trial_div(n):
    from collections import Counter
    
    prime=Counter()

    for i in range(2,int(pow(n,0.5)+2)):
        while n%i==0:
            n//=i
            prime[i]+=1
    if n>1:prime[n]+=1
    
    return prime


res=trial_div(g)
print(1+len(res))
while True:
    a,b=map(int,raw_input().split("."))
    if a<0:break
    s,s2="",""
    while a!=0:
        s+=str(a%2)
        a/=2
    cnt=0

    s2="0"*4
    if len(s2)<=4:
        print "%08d"%(int(s[::-1]))
    else:
        print "NA"

### FIXED CODE ###

def convert(x,p):
    if p==12:
	return
    convert(x/2,p+1)
    if p==3:
        ans.append(".")
    ans.append(str(x%2))

while True:
    n=input()
    if n<0:break
    ans=[]
    shift=int(n*16)
    if n==shift/16.0 and shift<4096:
        convert(shift,0)
	print "".join(ans)
    else:
        print "NA"
data = list(lambda x:int(x),input().strip().split())
print(sum(data))

### FIXED CODE ###

print(sum(list(map(lambda x:int(x),list(input().strip())))))
A=map(int,input())
B=map(int,input())
if (A=1 and B=2)or(A=2 and B=1):
  print(3)
if (A=2 and B=3)or(A=3 and B=2):
  print(1)
if (A=3 and B=1)or(A=1 and B=3):
  print(2)

### FIXED CODE ###

A=int(input())
B=int(input())  
C=6-A-B
print(C)
X = int(input())

is_prime_num_list = [True] * 100000
is_prime_num_list[0] = False
is_prime_num_list[1] = False

for i in range(2, X + 1):
    if is_prime_num_list[i] == True:

        # iの倍数番目をFalseにする
        for j in range(2 * i, 100000, i):
            is_prime_num_list[j] = False


prime_num_list = [i for i, j in enumerate(is_prime_num_list) if i >= X and j == True]

print(min(prime_num_list))


'''
if is_prime_num_list[X] == True:
    print(X)
    exit()
'''

'''
while True:
    X += 1
    is_target = True
    for i in prime_num_list:
        if X % i == 0:
            is_target = False
            break

    if is_target:
        print(X)
        exit()
'''
    
    




        







### FIXED CODE ###

X = int(input())

is_prime_num_list = [True] * 100005
is_prime_num_list[0] = False
is_prime_num_list[1] = False

for i in range(2, X + 1):
    if is_prime_num_list[i] == True:

        # iの倍数番目をFalseにする
        for j in range(2 * i, 100005, i):
            is_prime_num_list[j] = False

prime_num_list = [i for i, j in enumerate(is_prime_num_list) if i >= X and j == True]

print(min(prime_num_list))


'''
if is_prime_num_list[X] == True:
    print(X)
    exit()
'''

'''
while True:
    X += 1
    is_target = True
    for i in prime_num_list:
        if X % i == 0:
            is_target = False
            break

    if is_target:
        print(X)
        exit()
'''
n, m, k = map(int, input().split())
ab = []
for _ in range(m):
    ab.append([int(x)-1 for x in input().split()])
cd = []
for _ in range(k):
    cd.append([int(x)-1 for x in input().split()])
group = [-1 for _ in range(n)]
lis = [[] for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    lis[a].append(b)
    lis[b].append(a)
g_n = 0
def dfs(now):
    global g_n
    group[now] = g_n
    global group_len_n
    group_len_n += 1
    for i in lis[now]:
        if group[i] == -1:
            dfs(i)
group_len = []
for i in range(n):
    if group[i] == -1:
        group_len_n = 0
        dfs(i)
        group_len.append(group_len_n)
        g_n += 1
friend = [0 for _ in range(n)]
block = [0 for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    if group[a] == group[b]:
        friend[a] += 1
        friend[b] += 1
for x in cd:
    c = x[0]
    d = x[1]
    if group[c] == group[d]:
        block[c] += 1
        block[d] += 1
ans = []
for i in range(n):
    ans.append(str(group_len[group[i]] - friend[i] - block[i] -1))
print(" ".join(ans))

### FIXED CODE ###

import sys
sys.setrecursionlimit(2**20)
n, m, k = map(int, input().split())
ab = []
for _ in range(m):
    ab.append([int(x)-1 for x in input().split()])
cd = []
for _ in range(k):
    cd.append([int(x)-1 for x in input().split()])
group = [-1 for _ in range(n)]
lis = [[] for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    lis[a].append(b)
    lis[b].append(a)
g_n = 0
def dfs(now):
    global g_n
    group[now] = g_n
    global group_len_n
    group_len_n += 1
    for i in lis[now]:
        if group[i] == -1:
            dfs(i)
group_len = []
for i in range(n):
    if group[i] == -1:
        group_len_n = 0
        dfs(i)
        group_len.append(group_len_n)
        g_n += 1
friend = [0 for _ in range(n)]
block = [0 for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    if group[a] == group[b]:
        friend[a] += 1
        friend[b] += 1
for x in cd:
    c = x[0]
    d = x[1]
    if group[c] == group[d]:
        block[c] += 1
        block[d] += 1
ans = []
for i in range(n):
    ans.append(str(group_len[group[i]] - friend[i] - block[i] -1))
print(" ".join(ans))
L , N = map(int, input().split())
S = [input() for i in range(N)]
S = sorted(S)
print(*S,sep="")

### FIXED CODE ###

N , L = map(int, input().split())
S = [input() for i in range(N)]
S = sorted(S)
print(*S,sep='')
N,b=map(int,input().split())
S=input()
S[b-1]=S[b-1].lower()
print(S)

### FIXED CODE ###

N,b=map(int,input().split())
S=input()
S=S[:b-1]+S[b-1].lower()+S[b:]
print(S)
A = int(input())
B = int(input())
x=1
for i range(3):
  if x!=A and x!=B:
    print(str(x))
  x+=1

### FIXED CODE ###

A = int(input())
B = int(input())
x=1
for i in range(3):
  if x!=A and x!=B:
    print(str(x))
  x+=1
a,b,c = map(input().split(","))
print(a,b,c)

### FIXED CODE ###

a,b,c = input().split(",")
print(a,b,c)
X, Y = map(int, input().split()) ans = 'No'
for a in range(X + 1):
	b=X-a
	if 2 * a + 4 * b == Y:
		ans = 'Yes' 
print(ans)



### FIXED CODE ###

X, Y = map(int, input().split()) 
ans = 'No'
for a in range(X + 1):
	b=X-a
	if 2 * a + 4 * b == Y:
		ans = 'Yes' 
print(ans)
def acinput():
    return list(map(int, input().split(" ")))

import numpy as np

import heapq

N,M=acinput()

ab=[]
for i in range(N):
    ab.append(acinput())

#ab=sorted(ab,key=lambda x:x[1],reverse=True)
ab=sorted(ab,key=lambda x:x[0])


res=0
do=[0]*(M+1)
nowdate=M-1

koho=[]
k=0
while True:
    #print(k)
    i=k
    while True:
        if i>=len(ab):
            k=10**8
            break
        if ab[i][0]+nowdate>M:
            k=i
            break
        #koho=np.append(koho,ab[i][1])
        heapq.heappush(koho,-ab[i][1])
        i+=1
    #print(koho)

    tmp=heapq.heappop(koho)
    #print(tmp)
    res+=(-tmp)
    nowdate-=1
    if nowdate<0:
        break
    

print(int(res))

#print(do)
'''
3 6
4 5
4 3
6 4

'''

### FIXED CODE ###

def acinput():
    return list(map(int, input().split(" ")))


import heapq

N,M=acinput()

ab=[]
for i in range(N):
    ab.append(acinput())

#ab=sorted(ab,key=lambda x:x[1],reverse=True)
ab=sorted(ab,key=lambda x:x[0])
#print(ab)

res=0
do=[0]*(M+1)
nowdate=M-1

koho=[]
k=0
while True:
    #print(k)
    #print(nowdate)
    i=k
    while True:
        if i>=len(ab):
            k=10**10
            break
        if ab[i][0]+nowdate>M:
            k=i
            break
        #koho=np.append(koho,ab[i][1])
        heapq.heappush(koho,-ab[i][1])
        i+=1
    #print(koho)
    try:
        tmp=heapq.heappop(koho)
        res+=(-tmp)
    except:
        pass

    
    nowdate-=1
    if nowdate<0:
        break
    

print(int(res))
N = int(input())
table = [[0]*10 for _ in range(10)]

for i in range(1, N+1):
    s = str(i)
    e1 = int(s[0])
    e2 = int(s[1])
    table[e1][e2] += 1

res = 0
for i in range(1, 10):
    for j in range(1, 10):
        res += table[i][j]*table[j][i]
print(res)

### FIXED CODE ###

N = int(input())
table = [[0]*10 for _ in range(10)]

for i in range(1, N+1):
    s = str(i)
    e1 = int(s[0])
    e2 = int(s[-1])
    table[e1][e2] += 1

res = 0
for i in range(1, 10):
    for j in range(1, 10):
        res += table[i][j]*table[j][i]
print(res)
N = int(input())
S = [input() for i in range(N)]

# 'AB'の数を数える
cnt_ab = sum([s.count('AB') for s in S])

# L: 先頭が'B'、終わりが'A'の数を数える
cnt_b_a = sum([(s[0] == 'B') and (s[-1] == 'A') for s in S])

# M: 先頭が'B'、終わりが'A'じゃないの数を数える
cnt_b_na = sum([(s[0] == 'B') and (s[-1] != 'A') for s in S])

# N : 先頭が'B'じゃなく、終わりが'A'の数を数える
cnt_nb_a = sum([(s[0] != 'B') and (s[-1] == 'A') for s in S])

count = cnt_ab
if (cnt_b_a == 0): # MとNの数の小さい方の分だけ追加できる
    ans += min(cnt_b_na, cnt_nb_a)
elif (cnt_b_na + cnt_nb_a == 0): # Lの個数 - 1個だけ追加できる
    ans += (cnt_b_a - 1)
else:
    ans += (cnt_b_a + min(cnt_b_na, cnt_nb_a))
print(ans)

### FIXED CODE ###

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

# 'AB'の数を数える
cnt_ab = sum([s.count('AB') for s in S])

# L: 先頭が'B'、終わりが'A'の数を数える
cnt_b_a = sum([(s[0] == 'B') and (s[-1] == 'A') for s in S])

# M: 先頭が'B'、終わりが'A'じゃないの数を数える
cnt_b_na = sum([(s[0] == 'B') and (s[-1] != 'A') for s in S])

# N : 先頭が'B'じゃなく、終わりが'A'の数を数える
cnt_nb_a = sum([(s[0] != 'B') and (s[-1] == 'A') for s in S])

count = cnt_ab
if (cnt_b_a == 0): # MとNの数の小さい方の分だけ追加できる
    count += min(cnt_b_na, cnt_nb_a)
elif (cnt_b_na + cnt_nb_a == 0): # Lの個数 - 1個だけ追加できる
    count += (cnt_b_a - 1)
else:
    count += (cnt_b_a + min(cnt_b_na, cnt_nb_a))
print(count)
n = int(input())
a = list(map(int, input().split()))
b = [0] * n

for i in reversed(range(n)):
    for j in range(i + 1, n, i + 1):
        x = 1
        k = j
        while k < n:
            x ^= b[k - 1]
            k += j
        if x == a[i]:
            b[i] = 1

print(sum(b))
for i in range(n):
    if b[i] == 1:
        print(i + 1)

### FIXED CODE ###

n = int(input())
a = [int(input()) for _ in range(n)]

b = sorted(a, reverse=True)
max1 = b[0]
max2 = b[1]

for ai in a:
    if ai != max1:
        print(max1)
    else:
        print(max2)
S,T=input().split()
A,B=map(int,input().split())
U=input()
if S==U:
  print(A-1 B)
else:
  print(A B-1)

### FIXED CODE ###

S,T=input().split()
A,B=map(int,input().split())
U=input()
if S==U:
  print(str(A-1)+" "+str(B))
else:
  print(str(A)+" "+str(B-1))
a = int(input())
b = int(input())

s1 = ''
s2 = ''

for i in range(a):
  s1 += str(a)

for i in range(b):
  s2 += str(b)
  
if a < b:
  print(s1)
else:
  print(s2)

### FIXED CODE ###

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

s1 = ''
s2 = ''

for i in range(b):
  s1 += str(a)

for i in range(a):
  s2 += str(b)
  
if a < b:
  print(s1)
else:
  print(s2)
def inN():
    return int(input())
def inL():
    return list(map(int,input().split()))

n = inN()
a = inL()

kab = 0
mon = 1000
f = []
i=0
state = 0
while(i < n-1):
    if i == 0:
        if a[i+1] > a[i]:
            f.append(1)
            state = 1
        else:
            f.append(0)
    elif state == 1:
        while(i<n-1 and a[i+1]>=a[i]):
            f.append(0)
            i+=1
        f.append(2)
        state = 0
    elif state == 0:
        while(i<n-1):
            if a[i+1]<=a[i]:
                f.append(0)
                i +=1
            else:
                f.append(1)
                i += 1
                break
    else:
        f.append(0)
    i += 1
f.append(2)
    
print(f)   
for i in range(n):

    if f[i] == 1 :
        kab += mon//a[i]
        mon = mon % a[i]
    elif f[i] == 2:
        mon += a[i]*kab
        kab = 0
    
print(mon)



### FIXED CODE ###

def inN():
    return int(input())
def inL():
    return list(map(int,input().split()))

n = inN()
a = inL()

kab = 0
mon = 1000
f = []
i=0
state = 0
while(i < n-1):
    if state == 1:
        while(i<n-1):
            #print(a[i],a[i+1])
            if a[i+1]>=a[i]:
                f.append(0)
                i+=1
            else:
                f.append(2)
                #i+=1
                break
        state = 0
    elif state == 0:
        while(i<n-1):
            if a[i+1]<=a[i]:
                f.append(0)
                i +=1
            else:
                f.append(1)
                #i += 1
                state = 1
                break
    else:
        f.append(0)
    i += 1
f.append(2)
    
#print(f)   
for i in range(n):

    if f[i] == 1 :
        kab += mon//a[i]
        mon = mon % a[i]
    elif f[i] == 2:
        mon += a[i]*kab
        kab = 0
    
print(mon)
def solve():
    n=int(input())
    max1=0
    count=0
    num=[]
    def append(num1):
        num.append(num1)
    for i in range(n):
        a=int(input())
        if max1<a:
            count=1
            max1=a
        elif max1==a:
            count+=1
        append(a)
    if count!=1:
        for i in range(n):
            print(num[i])
    else:
        for i in range(n):
            if num[i]==max1:
                print(max(max(num[:i]),max(num[i+1:])))
            else:
                print(max1)
solve()


### FIXED CODE ###

def solve():
    n=int(input())
    num=[]
    def append(num1):
        num.append(num1)
    def count1(num2):
        return num.count(num2)
    for i in range(n):
        a=int(input())
        append(a)
    max1=max(num)
    count=count1(max1)
    if count>1:
        for i in range(n):
            print(max1)
    else:
        for i in range(n):
            if num[i]==max1:
                if i==0:
                    print(max(num[1:]))
                elif i==n-1:
                    print(max(num[:n-1]))
                else:
                    print(max(max(num[:i]),max(num[i+1:])))
            else:
                print(max1)
solve()
A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
x = [int(input()) for _ in range(Q)]
INF = 10 ** 9


def bsearch(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    return l


s = [-INF] + s + [INF]
t = [-INF] + t + [INF]

for i, x_i in enumerate(x):
    sa = bsearch(x_i, s)
    ta = bsearch(x_i, t)
    lmin = INF
    for si in range(sa, sa + 2):
        for ti in range(ta, ta + 2):
            l = min(abs(x_i - s[si]) + abs(s[si] - t[ti]), abs(x_i - t[ti]) + abs(t[ti] - s[si]))
            lmin = min(l, lmin)
    print(lmin)

### FIXED CODE ###

A, B, Q = map(int, input().split())
s = [int(input()) for _ in range(A)]
t = [int(input()) for _ in range(B)]
x = [int(input()) for _ in range(Q)]


def bsearch(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    if r < len(arr) and abs(arr[l] - n) > abs(arr[r] - n):
        return r
    else:
        return l


def bsearch_l(n, arr):
    l = 0
    r = len(arr)
    m = (l + r) // 2
    while l != m:
        if n <= arr[m]:
            r = m
        else:
            l = m
        m = (l + r) // 2
    return l


def calc_distance(x, l, c, near_idx):
    ret = abs(x - l)
    if c == 's':
        next_l = t[near_idx]
    else:
        next_l = s[near_idx]
    ret += abs(next_l - l)
    return ret


st = []
for i, s_i in enumerate(s):
    t_i = bsearch(s_i, t)
    st.append((s_i, 's', t_i))

for i, t_i in enumerate(t):
    s_i = bsearch(t_i, s)
    st.append((t_i, 't', s_i))

st = sorted(st)
starr = sorted(s + t)

ans_list = []
for i, x_i in enumerate(x):
    st_i = bsearch_l(x_i, starr)
    l, c, near_idx = st[st_i]
    ans = calc_distance(x_i, l, c, near_idx)
    if st_i < len(st) - 1:
        l, c, near_idx = st[st_i + 1]
        ans = min(ans, calc_distance(x_i, l, c, near_idx))
    ans_list.append(ans)

for i, ans in enumerate(ans_list):
    print(ans)