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

n, m = map(int, input().split())
s = input()
t = input()
if max(n, m) % min(n, m) == 0:
    print(-1)
else:
    for L in range(n, n * m + 1, n):
        if L % m == 0:
            x = ["" for i in range(L)]
            no = 0
            for i in range(n):
                x[i * L // n] = s[i]
            for i in range(m):
                if x[i * L // m] and x[i * L // m] != t[i]:
                    no = 1
                    break
                else:
                    x[i * L // m] = t[i]
            if no:
                continue
            print(L)
            exit()
    print(-1)

### FIXED CODE ###

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

def lcm(a, b):
    return (a * b) // gcd(a, b)

n, m = map(int, input().split())
s = input()
t = input()
L = lcm(n, m)
l = lcm(L // n, L // m)
for i in range(0, L, l):
    if s[i // (L // n)] != t[i // (L // m)]:
        print(-1)
        exit()
print(L)
### BUGGY CODE ###

L,R = (int(i) for i in input().split())

minimum = 2019
checked = set()

start = L
end   = R+1

for i in range(start, end):
    a = i % 2019
    if a not in checked:
        checked.add(a)
        endd = min(end, i+1+2019)
        for j in range(i+1, endd):
            v = (i * j) % 2019
            minimum = min([v, minimum])

print(minimum)

### FIXED CODE ###

L,R = (int(i) for i in input().split())

minimum = 2019
checked = set()

start = L
end   = R+1

end2 = min(end, start+1+2019)
for i in range(start, end2):
    a = i % 2019
    if a not in checked:
        checked.add(a)
        endd = min(end, i+1+2019)
        for j in range(i+1, endd):
            v = (i * j) % 2019
            minimum = min([v, minimum])

print(minimum)
### BUGGY CODE ###

x, k, d = map(int, input().split())

for i in range(k):
    if x < 0:
        x += d
    else:
        x -= d

print(abs(x))

### FIXED CODE ###

x, k, d = map(int, input().split())

x = abs(x)
kk = x // d

if kk >= k:
    print(str(x - (d * k)))
    exit()

k -= kk

if k % 2 == 0:
    print(str(x - (d * kk)))
else:
    print(str(abs(x - (d * (kk + 1)))))
### BUGGY CODE ###

from itertools import permutations

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

ans = float('inf')
for permutation in permutations(X,K):
  tmp_ans = 0
  prv_pos = 0
  for pos in permutation:
    tmp_ans += abs(pos-prv_pos)
    prv_pos = pos
  if ans > tmp_ans:ans = tmp_ans
print(ans)

### FIXED CODE ###

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

print(min([min(abs(X[i]) + abs(X[i+K-1]-X[i]), abs(X[i+K-1]) + abs(X[i]-X[i+K-1])) for i in range(N-K+1)]))
### BUGGY CODE ###

import sys

input_nums = raw_input().split(' ')
A = int(input_nums[0])
B = int(input_nums[1])
C = int(input_nums[2])
K = int(input_nums[3])

if A == B and B == C:
    print 0
    sys.exit()

for _ in xrange(K):
    A, B, C = B + C, A + C, A + B
    # if A > 10**18 or B > 10**18 or C > 10**18:
    #     print 'Unfair'
    #     sys.exit()

print A - B


### FIXED CODE ###

input_nums = raw_input().split(' ')
A = int(input_nums[0])
B = int(input_nums[1])
C = int(input_nums[2])
K = int(input_nums[3])

if K % 2 == 0:
    print A - B
else:
    print B - A
### BUGGY CODE ###

#!/usr/bin/env python3

P_MOD = 1000000007


def solve(S):
    # print(S)
    # 1, 10, 100, 1000, ... を 13 で割った値は 1, 10, 9, 12, 3, 4 で巡回する
    MODS = [1, 10, 9, 12, 3, 4]

    # 現在の桁の余り N の状態を cur[N]
    # 直前の桁の余り N の状態を priv[N] とする
    priv = [1] + [0] * 12
    # print(priv)
    s = S[::-1]
    for i in range(0, len(s)):
        cur = [0] * 13
        d = s[i]
        # print('i=', i, 'd=', d)
        if d == '?':
            for j in range(13):
                for di in range(10):
                    m = di * MODS[i % 6]
                    cur[(j+m) % 13] += priv[j]
        else:
            m = int(d) * MODS[i % 6]
            for j in range(13):
                cur[(m+j) % 13] = priv[j]
        # print(cur)
        priv = cur
    return cur[5]


if __name__ == '__main__':
    S = input()

    ans = solve(S)

    print(ans % P_MOD)


### FIXED CODE ###


P_MOD = 1000000007


def solve(S):
    # print(S)
    # 1, 10, 100, 1000, ... を 13 で割った値は 1, 10, 9, 12, 3, 4 で巡回する
    MODS = [1, 10, 9, 12, 3, 4]

    # 現在の桁の余り N の状態を cur[N]
    # 直前の桁の余り N の状態を priv[N] とする
    priv = [1] + [0] * 12
    # print(priv)
    s = S[::-1]
    for i in range(0, len(s)):
        cur = [0] * 13
        d = s[i]
        # print('i=', i, 'd=', d)
        if d == '?':
            for j in range(13):
                for di in range(10):
                    m = di * MODS[i % 6]
                    cur[(j+m) % 13] += priv[j]
        else:
            m = int(d) * MODS[i % 6]
            for j in range(13):
                cur[(m+j) % 13] = priv[j]
        # print(cur)
        for j in range(13):
            priv[j] = cur[j] % P_MOD
    return cur[5]


if __name__ == '__main__':
    S = input()

    ans = solve(S)

    print(ans % P_MOD)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from itertools import product
from heapq import heappop, heappush
import numpy as np


def small_dp(N,k,X):
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        c = 0
        for i in range(N//X[0][0]+1):
            j = (N-i*X[0][0])//X[1][0]
            c = max(X[0][1]*i + X[1][1]*j + (N-i*X[0][0])%X[1][0], c)
        return c
    if k == 3:
        dp = np.zeros((3, N+1), dtype=int)
        dp[0] = X[0][1]*(np.arange(N+1)//X[0][0]) + np.arange(N+1)%X[0][0]
        for i in range(1,3):
            d = X[i][1]*(np.arange(N+1)//X[i][0]) + np.arange(N+1)%X[i][0]
            for w in range(1, N+1):
                dp[i,w] = np.max(dp[i-1,w::-1] + d[:w+1])
        N = dp[2][N]
        return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, len(X), X)
    res = N

    return str(res)

if __name__ == '__main__':
    print(solve())

### FIXED CODE ###

# -*- coding: utf-8 -*-
from itertools import product
from heapq import heappop, heappush

def small_dp(N,A,B):
    *X, = filter(lambda c: int.__sub__(*c)<0, zip(A,B))
    k = len(X)
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        c = 0
        for i in range(N//X[0][0]+1):
            j = (N-i*X[0][0])//X[1][0]
            c = max(X[0][1]*i + X[1][1]*j + (N-i*X[0][0])%X[1][0], c)
        return c
    if k == 3:
        if (N//X[1][0]+1)*(N//X[0][0]+1) <= 10**6 :
            return max(X[0][1]*i + max(X[1][1]*j + X[2][1]*((N-i*X[0][0]-j*X[1][0])//X[2][0]) + ((N-i*X[0][0]-j*X[1][0])%X[2][0]) for j in range((N-i*X[0][0])//X[1][0]+1)) for i in range(N//X[0][0]+1))
        else:
            import numpy as np
            dp = np.zeros(N+1, dtype=int)
            for w in range(1,N+1):
                dp[w] = max(dp[w-X[i][0]]+X[i][1] if w>=X[i][0] else w for i in range(k))
            N = dp[-1]
            return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    res = small_dp(small_dp(N, A, B), B, A)

    return str(res)

if __name__ == '__main__':
    print(solve())
### BUGGY CODE ###

N = int(input())
ans = 0
for i in range(1, N+1):
    j = 1
    while i * j <= N:
        ans += i * j
        j += 1
print(ans)

### FIXED CODE ###

N = int(input())
ans = 0
for i in range(1, N+1):
    temp = N // i
    ans += i * temp * (temp + 1) // 2
print(ans)
### BUGGY CODE ###

N=int(input())
a=[int(x) for x in input().split()]
b=a
t=0
s=sum(b)
while s%2==0:
  for i in range(N):
    b[i]=b[i]/2
    t=t+1
print(t)

### FIXED CODE ###

N=int(input())
a=[int(x) for x in input().split()]
b=a
t=-1
s=0
while s==0:
  for i in range(N):
    if b[i]%2!=0:
      s=1
    b[i]=b[i]/2
  t=t+1
print(t)
### BUGGY CODE ###

def main():
    #0. input
#    N,C = 2, 3
#    D = [[0, 1, 1], [1, 0, 1], [1, 4, 0]] 
#    c_init = [[1, 2],[3, 3]]
    
#    N, C = 4, 3
#    D = [[0, 12, 71],
#             [81, 0, 53],
#             [14, 92, 0]]
#    c_init = [[1, 1, 2, 1],
#                   [2, 1, 1, 2],
#                   [2, 2, 1, 3],
#                   [1, 1, 2, 2]]
    
    N, C = map(int, input().split())
    
    D = []
    for i in range(C):
        D.append(list(map(int,input().split())))
        
    c_init = []
    for i in range(N):
        c_init.append(list(map(int, input().split())))
    
    iwakan = []
    
    for color0 in range(C):
        
        for color1 in range(C):
            if color1 == color0:
                continue
            
            for color2 in range(C):
                if color2 == color0 or color2 == color1:
                    continue
                
                # calculate diff
                iwakan_temp = 0
                for i in range(N):
                    for j in range(N):
                        if (i+j)%3 == 0:
                            iwakan_temp += D[c_init[i][j] - 1][color0 - 1]
                        elif (i+j)%3 == 1:
                            iwakan_temp += D[c_init[i][j] - 1][color1 - 1]
                        else:
                            iwakan_temp += D[c_init[i][j] - 1][color2 - 1]
                            
                iwakan.append(iwakan_temp)
                
    ans = min(iwakan)
    print(ans)
                


if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    N, C = map(int, input().split())
    D = []
    for i in range(C):
        D.append(list(map(int, input().split())))
        
    c_init = []
    for i in range(N):
        c_init.append(list(map(int,input().split())))
    
    group0 = [0]*C
    group1 = [0]*C
    group2 = [0]*C
    
    
    #1. 各グループに何色がどれだけあるかチェック
    for i in range(N):
        for j in range(N):
            if (i+j+2)%3==0:
                group0[c_init[i][j] - 1] += 1
            elif (i+j+2)%3 == 1:
                group1[c_init[i][j] - 1] += 1
            else:
                group2[c_init[i][j] - 1] += 1
    
    #2. 違和感の大きさをチェック
    iwakan =[]
    for color0 in range(C):
        for color1 in range(C):
            if color1 == color0:
                continue
            
            for color2 in range(C):
                if color2==color0 or color2==color1:
                    continue
                
                iwakan_temp = 0
                for colorN in range(C):
                    iwakan_temp += group0[colorN]*D[colorN][color0]
                    iwakan_temp += group1[colorN]*D[colorN][color1]
                    iwakan_temp += group2[colorN]*D[colorN][color2]
                
                iwakan.append(iwakan_temp)
                
    print(min(iwakan))
    
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import numpy as np
N, M = map(int, input().split())
As = np.array( list( map(float, input().split()) ) )
As /= 2.0
As.sort()
N1 = int(N/10)
for _ in range(M):
    As[-1] /= 2.0
    if As.size == 1:
      continue
    elif (As[-1] < As[-2]):
        As.sort()

ans = np.sum(np.floor(As*2.0))
print(int(ans))

### FIXED CODE ###

import heapq
N, M = map(int, input().split())
a_list = [-float(i) for i in input().split()]
heapq.heapify(a_list)
for _ in range(M):
    _ = heapq.heapreplace(a_list, a_list[0]/2.0)
ans = sum([-int(i) for i in a_list])
print(ans)
### BUGGY CODE ###

s=list(str(input()))
b=0
while len(s)>=5:
  if s[-1]=='r' and len(s)>=7:
    sd=[s[-7],s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    se=[s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dreamer':
      del s[-7:]
    elif ''.join(se)=='eraser':
      del s[-6:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='r':
    if ''.join(s)=='eraser':
      print('YES')
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='m':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dream':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='e':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='erase':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break  
  else:
    print('NO')
    b+=1
    break
if len(s)==0:
  print('YES')
elif b!=1:
  print('NO')

### FIXED CODE ###

s=list(str(input()))
b=0
while len(s)>=7:
  if s[-1]=='r':
    sd=[s[-7],s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    se=[s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dreamer':
      del s[-7:]
    elif ''.join(se)=='eraser':
      del s[-6:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='m':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dream':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='e':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='erase':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break  
  else:
    print('NO')
    b+=1
    break
if len(s)==0 or (len(s)==6 and ''.join(s)=='eraser') or (len(s)==5 and ''.join(s)=='dream') or (len(s)==5 and ''.join(s)=='erase'):
  print('YES') 
elif b!=1:
  print('NO')
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
def quick_sort(a):
    if len(a)<= 1:
        return a
    else:
        b=[]
        c=[]
        for i in range(1, len(a)):
            if a[i]<a[0]:
                b.append(a[i])
            else:
                c.append(a[i])
        return quick_sort(b)+[a[0]]+quick_sort(c)
def abs(x):
    if x>=0:
        return x
    else:
        return x*(-1)
B=[0]*N
for i in range(N):
  B[i]=A[i]-i-1
C=quick_sort(B)
if N%2==0:
  b=(C[N//2]+C[N//2-1])//2
else:
  b=C[N//2-1]
sad=0
for j in range(N):
  sad=sad+abs(C[j]-b)
print(sad)



### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
B=[]
for i in range(N):
  B.append(A[i]-i-1)
B.sort()
if N%2==0:
  b=(B[N//2]+B[N//2-1])//2
else:
  b=B[N//2]
sad=0
for j in range(N):
  sad=sad+abs(B[j]-b)
print(sad)
### BUGGY CODE ###

import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
# MOD = 10 ** 9 + 7
MOD = 998244353
num_list = []
str_list = []

def main():
    n, k = i_map()
    num_list = i_row_list(k)

    dp = [0] * (n+1)
    dp_sum = [0] * (n+1)
    dp[1] = 1
    dp_sum[1] = 1

    for i in range(2, n+1):
        for j in range(k):
            li = i - num_list[j][1]
            ri = i - num_list[j][0]
            if ri < 0:
                continue
            li = max(li, 0)
            dp[i] += dp_sum[ri] - dp_sum[li-1]
        dp_sum[i] = dp_sum[i-1] + dp[i]
    
    print(dp[n]%MOD)

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
# MOD = 10 ** 9 + 7
MOD = 998244353
num_list = []
str_list = []

def main():
    n, k = i_map()
    num_list = i_row_list(k)

    dp = [0] * (n+1)
    dp_sum = [0] * (n+1)
    dp[1] = 1
    dp_sum[1] = 1

    for i in range(2, n+1):
        for j in range(k):
            li = i - num_list[j][1]
            ri = i - num_list[j][0]
            if ri < 0:
                continue
            li = max(li, 0)
            dp[i] += dp_sum[ri] - dp_sum[li-1]
            dp[i] %= MOD
        dp_sum[i] = dp_sum[i-1] + dp[i]
    
    print(dp[n]%MOD)

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

N = int(input())
A = list(map(int, input().split()))
S = 0
MOD = 1000000007

import itertools
L = list(itertools.combinations(A, 2))
for i in range(len(L)):
    S += L[i][0] ^ L[i][1]
      
print(S % MOD)

### FIXED CODE ###

import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
MOD = 10 ** 9 + 7
ans = 0

for i in range(61):
    cnt = np.count_nonzero(A & 1)
    ans += (2 ** i) * cnt * (N - cnt)
    A >>= 1

print(ans % MOD)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)

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

    ans = 1
    for a in A:
        ans *= a

    print(ans if ans <= 10**18 else -1)



if __name__ == "__main__":
  main()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)

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

    A.sort()
    ans = 1
    for a in A:
        ans *= a
        if ans > 10**18:
          print(-1)
          exit()

    print(ans)



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

def main():
    n=int(input())
    s=input()
    l1=[0]*(n)
    l2=[0]*(n)
    l3=[0]*(n)
    for i in range(1,n):
        if s[i]=="R":
            l1[i-1]+=1
        elif s[i]=="G":
            l2[i-1]+=1
        else:
            l3[i-1]+=1
    for i in range(n-1,0,-1):
        l1[i-1]+=l1[i]
        l2[i-1]+=l2[i]
        l3[i-1]+=l3[i]
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            if set([s[i],s[j]])=={"G","B"}:
                ans+=l1[j]
                if 2*j-i<n and s[2*j-i]=="R":
                    ans-=1
            elif set([s[i],s[j]])=={"R","B"}:
                ans+=l2[j]
                if 2*j-i<n and s[2*j-i]=="G":
                    ans-=1
            elif set([s[i],s[j]])=={"R","G"}:
                ans+=l3[j]
                if 2*j-i<n and s[2*j-i]=="B":
                    ans-=1
    print(ans)
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    n=int(input())
    s=input()
    l1=[0]*(n)
    l2=[0]*(n)
    l3=[0]*(n)
    for i in range(1,n):
        if s[i]=="R":
            l1[i-1]+=1
        elif s[i]=="G":
            l2[i-1]+=1
        else:
            l3[i-1]+=1
    for i in range(n-1,0,-1):
        l1[i-1]+=l1[i]
        l2[i-1]+=l2[i]
        l3[i-1]+=l3[i]
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            if set([s[i],s[j]])=={"G","B"}:
                ans+=l1[j]
                if 2*j-i<n and s[2*j-i]=="R":
                    ans-=1
            elif set([s[i],s[j]])=={"R","B"}:
                ans+=l2[j]
                if 2*j-i<n and s[2*j-i]=="G":
                    ans-=1
            elif set([s[i],s[j]])=={"R","G"}:
                ans+=l3[j]
                if 2*j-i<n and s[2*j-i]=="B":
                    ans-=1
    print(ans)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
input = sys.stdin.readline

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

for i in range(min(n, m)):
    b, c = map(int, input().split())
    a.extend([c]*b)

a.sort(reverse=True)

print(sum(a[:n]))


### FIXED CODE ###

import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(reverse=True, key=lambda x: x[1])

bcs = []
for b, c in bc:
    if len(bcs) > n: break
    bcs.extend([c]*b)

a.extend(bcs)
a.sort(reverse=True)
print(sum(a[:n]))
### BUGGY CODE ###

N = int(input())

v = 0
for k in range(1, N+1):
    v += int(k/2 * (N//k) * int(1 + N/k))
print(v)


### FIXED CODE ###

N = int(input())

v = 0
for k in range(1, N+1):
    v += int(k/2 * (N//k) * int(1 + N/k))
print(v)
### BUGGY CODE ###

from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(stdin.readline().rstrip()) for _ in range(n)]

for i in range(n):
    if i == n - 1:
        ai = a[:i]
    else:
        ai = a[:i] + a[i+1:]
    print(max(ai))

### FIXED CODE ###

from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(stdin.readline().rstrip()) for _ in range(n)]

a_s = sorted(a)
m1 = a_s[-1]
m2 = a_s[-2]

for i in a:
    if i == m1:
        print(m2)
    else:
        print(m1)
### BUGGY CODE ###

a=int(input())
d=0
for i in range(33):
  b=i
  while b<a:
    b=b*i
  c=b/i
  if d>c:
    d=c
print(d)

### FIXED CODE ###

a=int(input())
d=0
e=0
for i in range(40):
  b=i+2
  while b<=a:
    b=b*(i+2)
    e=e+1
  c=b/(i+2)
  if e>1:
    if d<=c:
      d=c
      e=0
  else:
    e=0
if d==0:
  print(1)
else:
  print(int(d))
### BUGGY CODE ###

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

K = int(input())
if K % 2 == 0:
    print(-1)
    exit()

ans = 1
seven = 7
mod = 7 % K
while True:
    if seven % K == 0:
        break
    ans += 1
    seven = (seven * 10) % K + mod
print(ans)

### FIXED CODE ###

K = int(input())

x = 7
ans = 1
for i in range(K + 1):
    if x % K == 0:
        print(ans)
        exit()
    x = (x * 10 + 7) % K
    ans += 1
print(-1)
### BUGGY CODE ###

N=int(input())
A=[int(input()) for i in range(N)]
A0=max(A)
for i in range(N):
  if A[i]!=A0:
    print(A0)
  else:
    A.remove(A0)
    A1=max(A)
    print(max(A))
    A.append(A1)

### FIXED CODE ###

N=int(input())
A=[int(input()) for i in range(N)]
B=sorted(A)
A0=B[N-1]
A1=B[N-2]
for i in range(N):
  if A[i]!=A0:
    print(A0)
  else:
    print(A1)
### BUGGY CODE ###

# Problem D - String Formation

# input process
S = input()
Q = int(input()) # query number

# initialization
sento = 1

# process
for i in range(Q):
    query_num = input()
    query_1 = int(query_num[0])
    # クエリが１の時
    if query_1==1 and sento==1:
        sento = 10
    elif query_1==1:
        sento = 1
    else: # query num 2
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                S = c + S
            else:
                S = S + c
        else: # 末尾追加処理
            if sento==1:
                S = S + c
            else:
                S = c + S

# output process
if sento==1:
    print(S)
else:
    print(S[::-1])


### FIXED CODE ###

# Problem D - String Formation

from collections import deque

# input process
S = input().replace("\n", "")
S = deque(list(S))
Q = int(input()) # query number

# initialization
sento = 1

# process
for i in range(Q):
    query_num = input()
    # クエリが１の時
    if query_num[0]=="1":
        # 反転処理
        if sento==1:
            sento = 10
        elif sento==10:
            sento = 1
    elif query_num[0]=="2":
        n, f, c = query_num.split()
        f = int(f)
        if f==1: # 先頭追加処理
            # 先頭の位置によって変わる
            if sento==1:
                S.appendleft(c)
            elif sento==10:
                S.append(c)
        elif f==2: # 末尾追加処理
            if sento==1:
                S.append(c)
            elif sento==10:
                S.appendleft(c)

# output process
if sento==1:
    print("".join(S))
else:
    print("".join(list(S)[::-1]))