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

from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999999
R = 10**9 + 7
import bisect

def ddprint(x):
  if DBG:
    print(x)

a,b,q = inm()
s = []
t = []
for i in range(a):
    s.append(inn())
for i in range(b):
    t.append(inn())
for qq in range(q):
    x = inn()
    mn = BIG

    i = bisect.bisect_left(s,x)
    ss = [0] if i==0 else [a-1] if i==a else [i-1,i]
    for y in ss:
        d = abs(s[y]-x)
        j = bisect.bisect_left(t,s[y])
        tt = [0] if j==0 else [b-1] if j==b else [j-1,j]
        for z in tt:
            mn = min(mn, d+abs(s[y]-t[z]))

    i = bisect.bisect_left(t,x)
    tt = [0] if i==0 else [b-1] if i==b else [i-1,i]
    for y in tt:
        d = abs(t[y]-x)
        j = bisect.bisect_left(s,t[y])
        ss = [0] if j==0 else [a-1] if j==a else [j-1,j]
        for z in ss:
            mn = min(mn, d+abs(t[y]-s[z]))

    print(mn)


### FIXED CODE ###

from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999999
R = 10**9 + 7
import bisect

def ddprint(x):
  if DBG:
    print(x)

a,b,q = inm()
s = []
t = []
for i in range(a):
    s.append(inn())
for i in range(b):
    t.append(inn())
for qq in range(q):
    x = inn()
    mn = BIG

    i = bisect.bisect_left(s,x)
    j = bisect.bisect_left(t,x)
    #ddprint("i {} j {}".format(i,j))
    if i>0 and j>0:
        mn = min(mn, x-min(s[i-1],t[j-1]))
    if i>0 and j<b:
        mn = min(mn, min(x-s[i-1],t[j]-x)+t[j]-s[i-1])
    if i<a and j>0:
        mn = min(mn, min(x-t[j-1],s[i]-x)+s[i]-t[j-1])
    if i<a and j<b:
        mn = min(mn, max(s[i],t[j])-x)
    print(mn)
### BUGGY CODE ###

import sys

inf = sys.maxsize

# 入力:graphと始点s
# 出力:始点sから各頂点tへの距離dist
def shortestPath(graph, s):
    n = len(graph)

    dist    = [inf for _ in range(n)]
    visited = [False for _ in range(n)]
    dist[s] = 0

    while True:
        idx, u  = -1, inf
        for i in range(n):
            if not visited[i] and dist[i] < u:
                idx = i
                u = dist[i]
        
        if u == inf: break
        
        visited[idx] = True

        for (next, w) in graph[idx]:
            newdist = w + dist[idx]
            if newdist < dist[next]:
                dist[next] = newdist
    return dist

def main():
    while True:
        n, m = map(int, input().split())
        if n == 0 and m == 0: return

        # 隣接リスト
        c_graph = [[] for _ in range(m)]
        t_graph = [[] for _ in range(m)]
    
        for i in range(n):
            a, b, cost, time = map(int, input().split())
            a -= 1
            b -= 1
            c_graph[a].append((b, cost))
            c_graph[b].append((a, cost))
            t_graph[a].append((b, time))
            t_graph[b].append((a, time))
            
        c_dist = [shortestPath(c_graph, s) for s in range(m)]
        t_dist = [shortestPath(t_graph, s) for s in range(m)]
            
        k = int(input())
        for i in range(k):
            p, q, r = map(int, input().split())
            p -= 1
            q -= 1
            if r == 0:
                print(c_dist[p][q])
            else:
                print(t_dist[p][q])

main()

### FIXED CODE ###

inf = 0x10101010
def solve(A,strt):
    cost = [inf]*m
    visited = [False]*m
    cost[strt] = 0
    while 1:
        min = inf
        next = -1
        visited[strt] = True
        for i in range(m):
            if visited[i]: continue
            if A[strt][i]:
                d = cost[strt] + A[strt][i]
                if d < cost[i]:
                    cost[i] = d
            if min > cost[i]:
                min = cost[i]
                next = i
        strt = next
        if next == -1: break
    return cost
     
while 1:
    n,m = map(int,raw_input().split())
    if n == 0: break
    T = [[0]*m for i in range(m)]
    C = [[0]*m for i in range(m)]
    for i in range(n):
        a,b,c,t = map(int,raw_input().split())
        T[a-1][b-1] = T[b-1][a-1] = t
        C[a-1][b-1] = C[b-1][a-1] = c
    TS = [solve(T,i) for i in range(m)]
    CS = [solve(C,i) for i in range(m)]
    for i in range(input()):
        a,b,q = map(int,raw_input().split())
        if q == 0:
            print CS[a-1][b-1]
        else:
            print TS[a-1][b-1]
### BUGGY CODE ###

S = str(input())
ans = 0
N = len(S)
while True:
  S = S.replace('10', '').replace('01', '')
  if S.count('10') == 0 and S.count('01') == 0:
    break
ans = N - len(S)
print(ans)

### FIXED CODE ###

#!/usr/bin/env python3
import sys


def solve(S: str):
    stack = []
    N = len(S)
    for i, s in enumerate(S):
        if i == 0:
            stack.append(s)
        else:
            if len(stack) == 0:
                stack.append(s)
            else:
                if s != stack[-1]:
                    tmp = stack.pop()
                else:
                    stack.append(s)
    print(N - len(stack))

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    S = str(input())
    solve(S)

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

import math
import itertools

def is_prime(n):
    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return 1

Q = int(input())
L = []
R = []

for _ in range(Q):
    l,r = [int(i) for i in input().split()]
    L.append(l)
    R.append(r)

li = []
for i in range(3,max(R)+1,1):
    if is_prime(i) * is_prime((i+1)/2):
        li.append(1)
    else:
        li.append(0)
ans = [0, 0, 0]
ans += list(itertools.accumulate(li))
for i,j in zip(L,R):
    print(ans[j]-ans[i-1])


### FIXED CODE ###

import math
from itertools import accumulate


def is_prime(n):
    if n == 1:
        return 0

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return 0

    return 1


Q = int(input())
L = [0]*Q
R = [0]*Q

for i in range(Q):
    L[i], R[i] = list(map(int, input().split()))

min_L = min(L)
max_R = max(R)

check = []
for i in range(min_L, max_R+1):
    if (i % 2 == 1):
        flg = (is_prime(i) and is_prime((i+1)/2))
    else:
        flg = 0
    check.append(flg)

# 累積和
check = [0] + check
check = list(accumulate(check))

for i in range(Q):
    print(check[R[i] - min_L + 1] - check[L[i] - min_L])
### BUGGY CODE ###

N, M = map(int, input().split())
A = [int(input()) for _ in range(M)]

MOD = 10 ** 9 + 7

dp = [1] + [0] * (N + 10)

for n in range(1, N + 1):
	if n in A:
		continue
	dp[n] = (dp[n - 1] + (dp[n - 2] if 2 <= n else 0)) % MOD

#print(dp)
print(dp[N])


### FIXED CODE ###

N, M = map(int, input().split())
A = [int(input()) for _ in range(M)]

MOD = 10 ** 9 + 7

am = {}
for a in A:
	am[a] = True

dp = [1] + [0] * (N + 10)

for n in range(1, N + 1):
	if n in am:
		continue
	dp[n] = (dp[n - 1] + (dp[n - 2] if 2 <= n else 0)) % MOD

#print(dp)
print(dp[N])
### BUGGY CODE ###


S = input()

S = [s for s in S]

ans = 0
while len(S) > 0:
    counter = 0
    for ix in range(len(S)):
        if ix+1 < len(S):
            if S[ix] != S[ix+1]:
                del S[ix]
                del S[ix]
                ans += 1
                counter += 1
                break
    if counter == 0:
        break

print(ans*2)

### FIXED CODE ###


S = input()

S = [s for s in S]
S = [int(s, 0) for s in S]
S.sort()
if 1 in S and  0 in S:
    print(min(S.count(0)*2,S.count(1)*2))
else:
    print(0)
### BUGGY CODE ###

from fractions import gcd #直して
n = int(input())
a = list(map(int, input().split()))
def primelist(N):
    prime = [2]
    for i in range(3, N+1):
        flag = 1
        for j in prime:
            if i % j == 0:
                flag = 0
                break
        if flag == 1:
            prime.append(i)
    return prime
prime = primelist(1001)

def factorization(n):
    arr = []
    temp = n
    for i in prime:
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


mod = 10**9+7
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

fact = [0] * (1000001)
pri = []
for i in range(n):
    f = factorization(a[i])
    for fac, c in f:
        fact[fac] = max(fact[fac], c)

fact_list = []
for i in range(1000001):
    if fact[i] > 0:
        fact_list.append((i, fact[i]))

def power_func(a,n,p):
  bi=str(format(n,"b"))
  res=1
  for i in range(len(bi)):
    res=(res*res) %p
    if bi[i]=="1":
      res=(res*a) %p
  return res


ans = 0
for i in range(n):
    m = modinv(a[i])
    for fac, c in fact_list:
        m *= power_func(fac, c, mod)
        m %= mod

    ans += m
    ans %= mod

print(ans)


### FIXED CODE ###

from fractions import gcd #直して
n = int(input())
a = list(map(int, input().split()))

mod = 10**9+7
l = 1
for i in range(n):
    l = a[i] // gcd(l, a[i]) * l
    

def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

d = 0
for i in range(n):
    d += modinv(a[i])

print((l*d) % mod)
### BUGGY CODE ###

from collections import deque
def main():
  d=deque(list(input()))
  Q=int(input())
  for i in range(Q):
    q=list(map(str, input().split()))
    if q[0]=='1':
      d.reverse()
    else:
      if q[1]=='1':
        d.appendleft(q[2])
      else:
        d.append(q[2])
  print(''.join(list(d)))
main()

### FIXED CODE ###

from collections import deque
def main():
  d=deque(list(input()))
  Q=int(input())
  rev=0
  for i in range(Q):
    q=list(map(str, input().split()))
    if q[0]=='1':
      rev+=1
    else:
      if (int(q[1])+rev)%2==1:
        d.appendleft(q[2])
      else:
        d.append(q[2])
  if rev%2==0:
    print(''.join(list(d)))
  else:
    d.reverse()
    print(''.join(list(d)))
main()
### BUGGY CODE ###

N=int(input())
button=[]

for i in range(N):
  button.append(int(input()))

num=1
cnt=0
i=0

while True:
  num=button[num-1]
  cnt+=1
  if num==2:
    print(cnt)
    break
  if cnt>N:
    print(-1)

### FIXED CODE ###

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

btn=1
cnt=0

while True:
  btn=a[btn-1]
  cnt+=1
  if btn==2:
    print(cnt)
    break
  if cnt>N:
    print(-1)
    break
### BUGGY CODE ###

a,b,c=input().split()
d=0
for i in range(int(a),int(b)+1):
  if i%int(c)==0:
    d+=1
print(d)

### FIXED CODE ###

a, b, c = map(int, input().split())
if a == 0:
  print(b // c+1)
  exit()
print(b // c - (a-1) // c)
### BUGGY CODE ###

K, S = map(int, input().split())
ans = 0

for x in range(K+1):
    for y in range(K+1):
        for z in range(K+1):
            if x + y + z == S:
                ans += 1

print(ans)


### FIXED CODE ###

K, S = map(int, input().split())
ans = 0

for x in range(K+1):
    for y in range(K+1):
        z = S - y - x
        if 0 <= z <= K:
            ans += 1
print(ans)
### BUGGY CODE ###

import collections
N = int(input())
S = []
for i in range(N):
    S.append(input())
S_counter = collections.Counter(sorted(S))
S_max = [Sv[0] for Sv in S_counter.items() if Sv[1] == max(S_counter.values())]
S_max_sorted = sorted(S_max)
for ans in S_max_sorted:
    print(ans)


### FIXED CODE ###

import collections
N = int(input())
S = {}
for i in range(N):
    value = input()
    if value not in S:
        S[value] = 1
    else:
        S[value] += 1
S_largest = max(S.values())
S_sorted = sorted(S.items(), key=lambda x:x[1], reverse=True)
ans = []
for i in range(len(S_sorted)):
    if S_sorted[i][1] == S_largest:
        ans.append(S_sorted[i][0])
    else:
        break
ans_sorted = sorted(ans)
for a in ans_sorted:
    print(a)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

#単入力
n=int(input())

l=[i for i in range(1,n+1)]
ll=l
ans=0
for i in range(len(l)):
    if i==n-1:
        break
    ans+=ll[i]%l[i+1]
    #if i==0:
        #del ans[0]

print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

#単入力
n=int(input())
print(n*(n-1)//2)
### BUGGY CODE ###

import numpy as np
N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)

a_unique = np.unique(a_s)
a_counts = []
for i,a in enumerate(a_unique):
    a_counts.append(np.sum(a_s==a))
a_counts = np.array(a_counts)

ans = 0
for i in range(len(a_unique)):
    val = a_counts[i]
    if i>0:
        if a_unique[i-1]+1==a_unique[i]:
            val += a_counts[i-1]
            
    if i<len(a_unique)-1:
        if a_unique[i+1]-1==a_unique[i]:
            val += a_counts[i+1]
            
    if val > ans:
        ans = val
        
    if ans > sum(a_counts[i:]):
        break
        
print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
a_s = input().split()
a_s = [int(a_s[i]) for i in range(N)]

c = Counter(a_s)
c = sorted(c.items(), key=lambda x:x[0])
a_unique, a_counts = zip(*c)

ans = 0
for i,a in enumerate(a_unique):
    val = a_counts[i]
    if (i>0):
        if (a_unique[i-1]+1==a):
            val += a_counts[i-1]
            
    if (i<len(a_unique)-1):
        if (a_unique[i+1]-1==a):
            val += a_counts[i+1]
    
    if val > ans:
        ans = val
        
print(ans)
### BUGGY CODE ###

n = int(input())


power = 1

for i in range(1,n+1):
    power *= i

print(power%(10**9+7))


### FIXED CODE ###

n = int(input())


power = 1

for i in range(1,n+1):
    power *= i
    if power > (10**9+7):
        power %= (10**9+7)

print(power)
### BUGGY CODE ###

n=int(input())
L=list(map(int,input().split()))
L.sort()
cnt=0
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if L[k]>=L[i]+L[j]:
        break
      else:
        cnt+=1
print(cnt)

### FIXED CODE ###

import sys
input = sys.stdin.readline
from bisect import bisect_left

def main():
    n=int(input())
    l=sorted(list(map(int,input().split())))
    ans=0
    for i in range(n-2):
        a=l[i]
        for j in range(i+1,n-1):
            ans+=bisect_left(l,a+l[j])-j-1
    print(ans)

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

n,k=map(int,input().split())
s=list(map(int,input().split()))
e=[0]*n
e_table=[0]*1000
tmp=0
for i in range(1000):
    tmp+=(i+1)
    e_table[i] = tmp/(i+1)

for i,p in enumerate(s):
    e[i] = e_table[p-1]

ans=0

for i in range(n-k+1):
    ans =max(ans,sum(e[i:i+k]))
print(ans)
    

### FIXED CODE ###

n,k=map(int,input().split())
s=list(map(int,input().split()))

ans=0
tmp=0
tmp2=sum(s[0:k])
kukan=[0,n]
for i in range(n-k):
    tmp2 -= s[i]
    tmp2 += s[k+i]
    if tmp < tmp2:
        tmp = tmp2
        kukan = [i+1,i+k+1]

for i in range(kukan[0],kukan[1]):
    ans += (s[i]+1)/2
    
print(ans)
### BUGGY CODE ###

h, n = map(int, input().split())

ab_list = []

for i in range(n):
    ab_list.append(list(map(int, input().split())))

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])


### FIXED CODE ###

h, n = map(int, input().split())

ab_list = [0]*n

for i in range(n):
    ab_list[i] = list(map(int, input().split()))

ma_list = [0]*h

for i in range(h):
    ma_list[i] = 10000000000
    for item in ab_list:
        ma = 0
        if item[0] >= (i + 1):
            ma = item[1]
        else:
            ma = item[1] + ma_list[i - item[0]]
        if ma_list[i] > ma:
            ma_list[i] = ma

print(ma_list[-1])
### BUGGY CODE ###

a, b, x = map(int, input().split())
cnt = 0
for i in range(a, b+1):
    if i%x == 0:
        cnt+=1
print(cnt)

### FIXED CODE ###

a, b, x = map(int, input().split())
print(b//x-(a-1)//x)
### BUGGY CODE ###

# D
import numpy as np
N, X, Y = map(int, input().split())

X = X - 1
Y = Y - 1

x = np.zeros((N,N),dtype=np.int)
for i in range(N):
    x[i,i:] = np.array(range(N-i))
y = np.zeros((N,N),dtype=np.int)
for i in range(0, N):
    for j in range(i+1, N):
        y[i, j] = abs(i-X) + abs(j-Y) + 1

ans = np.minimum(x, y)

count = [0]*(N)
for i in ans.reshape(-1):
    count[i]+=1

for i in count[1:]:
    print(i)

### FIXED CODE ###

# D
import numpy as np
N, X, Y = map(int, input().split())

D = [0]*N
for i in range(1, N+1):
    for j in range(i+1, N+1):
        d = min(j-i, abs(j-Y) + abs(i-X) + 1) 
        D[d] += 1
for i in range(1, N):
    print(D[i])
### BUGGY CODE ###

def gcd(m,n):
    while (m % n != 0):
        temp = n
        n = m % n
        m = temp
    return(n)


k = int(input())
s = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        t = gcd(a,b)
        for c in range(1,k+1):
            s += gcd(t,c)
print(s)


### FIXED CODE ###

def gcd(m,n):
    while (m % n != 0):
        n,m = m%n,n
    return(n)


k = int(input())
s = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        t = gcd(a,b)
        for c in range(b,k+1):
            v = gcd(t,c)
            l = len(set([a,b,c]))
            if(l == 1):s += v
            elif(l == 2):s += v * 3
            else:s += v * 6
print(s)
### BUGGY CODE ###

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

min = 1000000000000000000000
for i in range(1,N):
  tmp = abs(sum(a[:i]) - sum(a[i:])) 
  if min > tmp:
    min = tmp

print(min)

### FIXED CODE ###

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

S = sum(a)
_min  = 10000000000000000000
x = 0
for i in range(0,N-1):
  x += a[i]
  tmp = abs(S - 2*x)
  if tmp < _min:
    _min = tmp

print(_min)
### BUGGY CODE ###

N, M, Q = map(int, raw_input().split())

train = []
for i in range(0, N):
	train.append([])
for i in range(0, M):
	L, R = map(int, raw_input().split())
	train[L-1].append(R)
for i in range(0, N):
	train[i-1].sort()

counter = []
for i in range(0, N):
	lst = []
	for j in range(0, N):
		lst.append(0)
	for j in range(0, len(train[i])):
		lst[train[i][j]-1] = lst[train[i][j]-1] + 1
	for j in range(1, len(lst)):
		lst[j] = lst[j-1] + lst[j]
	counter.append(lst)

for i in range(0, Q):
	p, q = map(int, raw_input().split())
	cnt = 0
	for j in range(p-1, q):
		cnt = cnt + counter[j][q-1]
	print cnt


### FIXED CODE ###

N, M, Q = map(int, raw_input().split())

train = []
for i in range(0, N):
	train.append([])
for i in range(0, M):
	L, R = map(int, raw_input().split())
	train[L-1].append(R)
for i in range(0, N):
	train[i-1].sort()

counter = []
for i in range(0, N):
	lst = []
	for j in range(0, N):
		lst.append(0)
	for j in range(0, len(train[i])):
		lst[train[i][j]-1] = lst[train[i][j]-1] + 1
	for j in range(1, len(lst)):
		lst[j] = lst[j-1] + lst[j]
	counter.append(lst)
for i in range(1, N):
	for j in range(0, N):
		counter[N-1-i][j] = counter[N-1-i][j] + counter[N-i][j]

for i in range(0, Q):
	p, q = map(int, raw_input().split())
	print counter[p-1][q-1]
### BUGGY CODE ###

#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))


n = I()
edge = [[] for _ in range(n)]
for v in range(1,n):
    u = I()-1
    edge[u].append(v)

dp = [10**10]*n
def dfs(s):
    if edge[s] == []:
        dp[s] = 0
        return 0
    tmp = []
    for v in edge[s]:
        tmp.append(dfs(v))
    tmp.sort(reverse=True)
    dp[s] = max(list((i+1+j) for i,j in enumerate(tmp)))
    return dp[s]

print(dfs(0))

### FIXED CODE ###

#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))


n = I()
edge = [[] for _ in range(n)]
for v in range(1,n):
    u = I()-1
    edge[u].append(v)

dp = [None]*n
def dfs(s):
    if dp[s] != None:
        return dp[s]
    if edge[s] == []:
        dp[s] = 0
        return 0
    tmp = []
    for v in edge[s]:
        tmp.append(dfs(v))
    tmp.sort(reverse=True)
    m = 0
    for i,j in enumerate(tmp):
        m = max(m,(i+1)+j)
    dp[s] = m
    return m

dfs(0)
print(dp[0])
### BUGGY CODE ###

s = int(input())
 
ans=0
 
def f(n):
  if (n % 2 == 0):
    return n / 2
  else:
    return 3*n + 1
  
while (True):
  a_n = [s]
  for i in range(len(a_n)):
    a_i  = f(a_n.pop(-1))
    a_n.append(a_i)
    
print(ans)

### FIXED CODE ###

s = int(input())
 
ans = 0
 
if (s == 1 or s == 2 or s == 4):
  print(4)
else:
  while (s != 4):
    if (s % 2 == 0):
      s  =  int(s/2)
    else:
      s = 3*s + 1
    ans += 1
  
 
  print(ans+4)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

from collections import deque

n = int(input())
a = deque()
for _ in range(n):
    a.append(int(input()))

for _ in range(n):
    tmp = a.popleft()
    print(max(a))
    a.append(tmp)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
a = [int(input()) for _ in range(n)]
a_max_1 = max(a)
b = sorted(a, reverse=True)
a_max_2 = b[1]

for i in range(n):
    if a[i] == a_max_1:
        print(a_max_2)
    else:
        print(a_max_1)
### BUGGY CODE ###

n,k = map(int,input().split())
lis = list(map(int,input().split()))
dp = [10 ** 10 for i in range(n+k)]
dp[0] = 0
for i in range(n):
  for j in range(1,k+1):
    if i + j < n:
      dp[i+j] = min(dp[i+j],dp[i] + abs(lis[i+j]-lis[i]))
print(dp[n-1])

### FIXED CODE ###

n,k = map(int,input().split())
lis = list(map(int,input().split()))

dp = [10 ** 10 for i in range(n+k+1)]

dp[0] = 0
for i in range(n):
    for j in range(1,min(n-i,k+1)):
        dp[i+j] = min(dp[i+j],dp[i]+abs(lis[i+j]-lis[i]))
print(dp[n-1])
### BUGGY CODE ###

import sys

#計算量O(n/2)
N = int(input())
A = list(map(int,input().split()))
amari = 10**9 + 7
A.sort()

if N % 2 == 0:
    for i in range(1,N,2):
        if A.count(i) != 2:
            print("0")
            sys.exit()
else:
    if A.count(0) != 1:
        print("0")
        sys.exit()
    for i in range(2,N,2):
        if A.count(i) != 2:
            print("0")
            sys.exit()

print(2**(N//2) % amari)

### FIXED CODE ###

N = int(input())
A = sorted(list(map(int,input().split())))
amari = 10**9 + 7
ans = []
han = 0

if N % 2 == 0:
    han = 1
else:
    han = 0
    A.insert(0,0)


ans = list(int(i) for i in range(han,N,2))
ans += ans
ans.sort()

if ans == A:
    print(2**(N//2) % amari)
else:
    print("0")
### BUGGY CODE ###

import collections
N = int(input())
L = []
C = 0
for i in range(N):
  L.append(input())
for j in range(N):
  for k in range(N):
    if k > j:
      if collections.Counter(L[j]) == collections.Counter(L[k]):
        C += 1
print(C)

### FIXED CODE ###

from collections import Counter as CC
N = int(input())
L = [str(sorted(list(input()))) for i in range(N)]
C = CC(L)
print(sum([C[i] * (C[i] - 1) // 2 for i in C if C[i] > 1]))