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

import numpy as np
import itertools

X, Y, Z, K = tuple([int(x) for x in str(input()).split(' ')])

A = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))
B = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))
C = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))

out = sorted([A[a] + B[b] + C[c] for a, b, c in itertools.product(range(len(A)), range(len(B)), range(len(C))) if (a+1)*(b+1)*(c+1) <= K], reverse=True)

for x in out[:K]:
    print(x)

### FIXED CODE ###

import numpy as np
import itertools

X, Y, Z, K = tuple([int(x) for x in str(input()).split(' ')])

A = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))
B = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))
C = np.array(sorted([int(x) for x in str(input()).split(' ')], reverse=True))

tmp = {(0, 0, 0) : A[0] + B[0] + C[0]}
tmp_keys = [(0, 0 ,0)]
out = []

for i in range(K):
    (a, b, c),  p = max(tmp.items(), key=lambda x : x[1])
    out.append(tmp.pop((a, b, c)))

    keys = []
    if a < X - 1:
        keys.append((a+1, b, c))
    if b < Y - 1:
        keys.append((a, b+1, c))
    if c < Z - 1:
        keys.append((a, b, c+1))
    
    for key in keys:
        if key not in tmp_keys:
            tmp[key] = A[key[0]] + B[key[1]] + C[key[2]]
            tmp_keys.append(key)

for x in out:
    print(x)
### BUGGY CODE ###

n = int(input())
a_list = list(map(int, input().split()))
tr_list = [i+1 for i in range(n)]
ans_list =[]
for i in range(n):
    ans_list.append(a_list.index(tr_list[i])+1)
print(*ans_list)

### FIXED CODE ###

n = int(input())
a_list = list(map(int, input().split()))
ans_list =[0]*n
for i in range(n):
    ans_list[a_list[i]-1] = i+1
print(*ans_list)
### BUGGY CODE ###

a = int(input())
inputList = input().split()

sum = 0
for i in range(a - 1):
  for j in range(i + 1, a):
    sum += int(inputList[i]) ^ int(inputList[j])

print(sum % 1000000007)

### FIXED CODE ###

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

mod = 10**9 + 7
total = 0
for i in range(60):
  cnt = sum([1 for a in A if (a>>i & 1)])
  total += cnt * (n-cnt) * (1 << i)
  total %= mod
print(total)
### BUGGY CODE ###

# coding: utf-8
import sys
# from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
#from heapq import heappop, heappush
from collections import OrderedDict, defaultdict
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque
#import numba

def run():
    N = int(input())
    primes = [2]

    for i in range(3, int(N ** (0.5))+1):
        done = False
        for k in primes:
            if not i % k:
                done = True
                break
        if not done:
            primes.append(i)
    #print(primes)

    ret = 1# f(1) = 1

    for i in range(2, N+1):
        current = i
        dic = defaultdict(lambda:(0))
        eval_max = int(i ** (0.5))
        for p in primes:
            if p > eval_max:
                break
            while True:
                if not current % p:
                    current = current // p
                    dic[p] += 1
                else:break
        if current != 1:
            dic[current] += 1
        tmp = 1
        for key in dic.keys():
            tmp *= dic[key] + 1
        #print(dic, tmp)
        ret += tmp * i

    print(ret)


if __name__ == "__main__":
    run()

### FIXED CODE ###

# coding: utf-8
import sys
# from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
#from heapq import heappop, heappush
from collections import OrderedDict, defaultdict
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque
#import numba

def run():
    N = int(input())

    ret = 0
    for i in range(1,N+1):
        tmp = N // i
        left = i
        right = i * tmp
        ret += (left + right) * tmp // 2
    print(ret)
if __name__ == "__main__":
    run()
### BUGGY CODE ###

import fractions

N = int(input())
ints = list(map(int, input().split()))
ints.sort(reverse=True)

gcd = 0
result = 1
holed_ints = []
for i in range(N):
    holed_ints = ints[:i] + ints[i+1:]
    gcd = 0
    for j in range(N-1):
        gcd = fractions.gcd(gcd, holed_ints[j])

    if gcd > result:
        result = gcd

print(result)

### FIXED CODE ###

import fractions

integral_count = int(input())
blackboard_integers = list(map(int, input().split()))

gcd_integers = list(set(blackboard_integers))

gcd = 0
result = 1
for i in range(len(gcd_integers)):
    gcd = 0
    if blackboard_integers.count(gcd_integers[i]) >= 2:
        for n in gcd_integers:
            gcd = fractions.gcd(gcd, n)
    else:
        for n in gcd_integers[:i]:
            gcd = fractions.gcd(gcd, n)
        for n in gcd_integers[i+1:]:
            gcd = fractions.gcd(gcd, n)
    if gcd > result:
        result = gcd

print(result)
### BUGGY CODE ###

A,B,C,D=map(int,input().split())
cnt=0
cnt1=0
cnt2=0
for j in range(A,B+1):
    if j%C!=0:
        cnt+=1
    if j%D!=0:
        cnt1+=1
    if j%C!=0 or j%D!=0:
        cnt2+=1    
print(cnt+cnt1-cnt2)

### FIXED CODE ###

import fractions

A,B,C,D=map(int,input().split())
c=B//C-(A-1)//C 
d=B//D-(A-1)//D 
l=C*D//fractions.gcd(C,D)
cd=B//l-(A-1)//l
print(int(B-(A-1)-c-d+cd))
### BUGGY CODE ###

import itertools

n = int(input())
arr = tuple(map(int, input().split()))

results = []
for dst in itertools.permutations(range(n)):
  results.append(sum(
    [a * abs(i - dst[i]) for i, a in enumerate(arr)]
  ))

print(max(results))


### FIXED CODE ###


def main():

  n = int(input())
  arr = map(int, input().split())
  arr = tuple(sorted(enumerate(arr), key=lambda tpl: -tpl[1]))

  dp = [[0] * (n + 1) for _ in range(n + 1)]
  for i, (p, a) in enumerate(arr):
    for l in range(i + 1):
      dp[i + 1][l] = max(dp[i + 1][l], dp[i][l] + a * abs(p - (n - (i - l) - 1)))
      dp[i + 1][l + 1] = max(dp[i + 1][l + 1], dp[i][l] + a * abs(p - l))

  print(max(dp[n]))

main()
### BUGGY CODE ###

N, K = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(N)]

ans = 10 ** 19
for a in range(N):
    for b in range(N):
        if a != b:
            for c in range(N):
                for d in range(N):
                    if c != d:
                        x0 = data[a][0]
                        x1 = data[b][0]
                        y0 = data[c][1]
                        y1 = data[d][1]

                        count = 0
                        for i, j in data:
                            if x0 <= i <= x1 and y0 <= j <= y1:
                                count += 1
                        if count >= K:
                            ans = min(ans, (x1 - x0)*(y1 - y0))

print(ans)

### FIXED CODE ###

N, K = map(int, input().split())
data = [list(map(int, input().split())) for _ in range(N)]

ans = 10 ** 19
for a in range(N):
    for b in range(a+1, N):
        for c in range(N):
            for d in range(c+1, N):
                x0 = data[a][0]
                x1 = data[b][0]
                y0 = data[c][1]
                y1 = data[d][1]

                if x0 > x1:
                    x0, x1 = x1, x0
                if y0 > y1:
                    y0, y1 = y1, y0

                count = 0
                for i, j in data:
                    if x0 <= i <= x1 and y0 <= j <= y1:
                        count += 1
                if count >= K:
                    ans = min(ans, (x1 - x0)*(y1 - y0))

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

N,X,M = map(int,input().split())

ans = X
for i in range(N-1):
    X = X**2 % M
    ans += X

print(ans)

### FIXED CODE ###

N,X,M = map(int,input().split())

List = [X]

for i in range(N-1):
    X = X**2 % M
    if X in List:
        #配列の何番目か
        num = i + 1
        #同じ数字
        key = List.index(X)
        break
    List.append(X)

ans = sum(List)
if len(List) == N:
    print(ans)

else:
    loop = (N - len(List)) // (num - key)
    add = (N - len(List)) % (num - key)

    ans += (N - len(List)) // (num - key) * sum(List[key:num])
    ans += sum(List[key:key+((N - len(List)) % (num - key))])

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

def main():
    import itertools,copy,sys
    input = sys.stdin.readline
    T = list(input())
    N = [i for i, x in enumerate(T) if x == '?']
    B = list(itertools.product(['P','D'], repeat=len(N)))
    max = 0
    maxstr = ''
    for b in B:
        TC = copy.deepcopy(T)
        for j,n in enumerate(N):
            TC[n] = b[j]
        TC = ''.join(TC)
        sum = TC.count('PD') + TC.count('D')
        if max < sum:
            max = sum
            maxstr = TC
    print(maxstr)
main()
exit(0)

if __name__ == '__main__':
    main()

### FIXED CODE ###

#BBBBBBBBBBB

import sys
import math
import itertools
import collections
import heapq
import numpy as np

rl = sys.stdin.readline

t = rl()
li = []
for i in range(len(t)):
    if t[i] == '?':
        li.append('D')
    else:
        li.append(t[i])
print(''.join(li))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

n,q=map(int,input().split())
s=input()
lr=[input() for __ in range(q)]

ans=[]

for i in lr:
    l,r=map(int,i.split())
    #print(int((len(s[l-1:r])-len(s[l-1:r].replace('AC',"")))/2))
    #ans.append(str(int((len(s[l-1:r])-len(s[l-1:r].replace('AC',"")))/2)))
    st=s[l-1:r]
    ans.append(str(int((len(st)-len(st.replace('AC',"")))/2)))

print('\n'.join(ans))

### FIXED CODE ###

n,q=map(int,input().split())
s=input()
lr=[input() for __ in range(q)]
cs=[0]*(n+1)

#端がACで-1で止まる文字列指定は大丈夫？.
for i in range(n):
    cs[i+1]=cs[i] + (1 if s[i:i+2] == 'AC' else 0)

for num in lr:
    l,r=map(int,num.split())
    print(cs[r-1] - cs[l-1])
### BUGGY CODE ###

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
    N = int(input())

    x = prime_factorize(N)
    D = []
    for i in range(2 ** len(x)):
        a = format(i, '0{}b'.format(len(x)))
        D.append(a)

    # print(x)
    # print(D)

    mn = 1 + N
    for di in D:
        a = 1
        b = 1
        for i, dii in enumerate(di):
            if dii == '0':
                a = a * x[i]
            else:
                b = b * x[i]
        # print(a, b)
        mn = min(mn, a + b)
    print(mn - 2)

if __name__ == "__main__":
    main()

### FIXED CODE ###

from math import sqrt, floor

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
    N = int(input())

    # x = prime_factorize(N)
    # D = []
    # for i in range(2 ** len(x)):
    #     a = format(i, '0{}b'.format(len(x)))
    #     D.append(a)

    # mn = 1 + N
    # for di in D:
    #     a = 1
    #     b = 1
    #     for i, dii in enumerate(di):
    #         if dii == '0':
    #             a = a * x[i]
    #         else:
    #             b = b * x[i]
    #     mn = min(mn, a + b)
    # print(mn - 2)

    NN = floor(sqrt(N))
    mn = N + 100
    for i in range(1, NN + 1):
        if N % i == 0:
            a = i
            b = N // i
            # print(a, b)
            mn = min(mn, a + b)

    print(mn - 2)

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

N,M = map(int,input().split())
L = [0]*M
R = [0]*M
for i in range(M):
    L[i],R[i] = map(int,input().split())

a = [0]*M

for j in range(M):
    a[j] = set(range(L[j],R[j]+1))

origin = set(range(1,N+1))
for k in range(M):
    origin &= a[k]

print(len(origin))

### FIXED CODE ###

N,M = map(int,input().split())
L = [0]*M
R = [0]*M
for i in range(M):
    L[i],R[i] = map(int,input().split())

min = L[0]
max = R[0]

for j in range(1,M):
    if min < L[j]:
        min = L[j]
    if max > R[j]:
        max = R[j]
if max-min+1 >= 0:
	print(max-min+1)
else:
  print(0)
### BUGGY CODE ###

import itertools

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

comb = []
if K % 2 != 0:
    for a, b, c in itertools.product(range(1, N//K+1), range(1, N//K+1), range(1, N//K+1)):
        comb.append((a, b, c))
else:
    for a, b, c in itertools.product(range(1, N//K+1), range(1, N//K+1), range(1, N//K+1)):
        comb.append(((a, K), (b, K), (c, K)))
    for a, b, c in itertools.product(range(1, (2*N+K)//(2*K)+1), range(1, (2*N+K)//(2*K)+1), range(1, (2*N+K)//(2*K)+1)):
        comb.append(((a, K, 1), (b, K, 1), (c, K, 1)))


print(len(set(comb)))


### FIXED CODE ###

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

if K % 2 == 0:
    ans = (N//K)**3 + ((N+K//2)//K)**3
else:
    ans = (N//K)**3
print(ans)
### BUGGY CODE ###

N = int(input())
A = [int(x) for x in input().split()]
total_medians = N*(N+1)//2
# n件のデータ
# (上側中央値 >= x) iff (a > x となる a が(n-1)//2以下)

def test(x):
  # 「答はx以下である」
  # 「xより真に大きな区間中央値が(total_medians-1)//2個以下」
  B = [0]
  for a in A:
    B.append(B[-1] + (1 if a > x else -1))
  m = min(B)-1
  L = max(B)-m+1
  BIT = [0]*L
  cnt = 0
  for b in B:
    x = b-m
    while x:
      cnt += BIT[x]
      x -= (x & (-x))
    x = b-m
    while x < L:
      BIT[x] += 1
      x += (x & (-x))
  return cnt <= (total_medians-1)//2

AA = sorted(A)
left = -1 # 答はAA[left]より大きい
right = len(AA)-1 # 答はAA[right]以下である
while right - left > 1:
  mid = (left+right)//2
  if test(AA[mid]):
    right = mid
  else:
    left = mid
    
answer = AA[right]
print(answer)


### FIXED CODE ###

N = int(input())
A = [int(x) for x in input().split()]
total_medians = N*(N+1)//2
# n件のデータ
# (上側中央値 >= x) iff (a > x となる a が(n-1)//2以下)

def BIT_update(BIT,x,value):
  # 番号xにvalueを加える
  L = len(BIT)
  while x < L:
    BIT[x] += value
    bottom_bit = x & (-x)
    x += bottom_bit
  return

def BIT_sum(BIT,x):
  # 番号1～xの和を求める
  result = 0
  while x:
    result += BIT[x]
    bottom_bit = x & (-x)
    x -= bottom_bit
  return result

def test(x):
  # 「答はx以下である」
  # 「xより真に大きな区間中央値が(total_medians-1)//2個以下」
  B = [0]
  for a in A:
    B.append(B[-1] + (1 if a>x else -1))
  m = min(B)-1
  B = [x-m for x in B]

  BIT = [0]*(N+1)
  
  cnt = 0
  for R in range(1,N+1):
    BIT_update(BIT,B[R-1],1)
    cnt += BIT_sum(BIT,B[R]) # 右端がRとなる(L,R)対
  return cnt <= (total_medians-1)//2

AA = sorted(A)
left = -1 # 答はAA[left]より大きい
right = len(AA)-1 # 答はAA[right]以下である
while right - left > 1:
  mid = (left+right)//2
  if test(AA[mid]):
    right = mid
  else:
    left = mid
answer = AA[right]

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

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

lp = []
for i in range(n-k+1):
  p = 0.0
  for j in range(k):
    a = dices[i+j]
    p += a*(a+1)/(2*a)
  lp.append(p)
  
print(max(lp))

### FIXED CODE ###

n, k = map(int, input().split(" "))
dices_e = [float((e+1)/2) for e in list(map(int, input().split(" ")))]
e = max_e = sum(dices_e[:k])
for i in range(1, n-k+1):
  e = e - dices_e[i-1] + dices_e[i+k-1]
  if e>max_e:
    max_e = e

print(max_e)
### BUGGY CODE ###

H, W, M = map(int, input().split())

freq_w = [ 0 for _ in range(W)]
freq_h = [ 0 for _ in range(H)]
pos = dict()

for _ in range(M):
    h, w = map(int, input().split())
    freq_h[h-1] += 1
    freq_w[w-1] += 1
    pos[(h-1, w-1)] = True


m_w = max(freq_w)
pos_w = [i for i, j in enumerate(freq_w) if j == m_w]

m_h = max(freq_h)
pos_h = [i for i, j in enumerate(freq_h) if j == m_h]

max_ = m_w + m_h - 1
found = False
for i in pos_h:
    for j in pos_w:
        if not((i, j) in pos):
            found = True

print(max_ + int(found))


### FIXED CODE ###

H, W, M = map(int, input().split())

freq_w = [ 0 for _ in range(W)]
freq_h = [ 0 for _ in range(H)]
pos = dict()

for _ in range(M):
    h, w = map(int, input().split())
    freq_h[h-1] += 1
    freq_w[w-1] += 1
    pos[(h-1, w-1)] = True


m_w = max(freq_w)
pos_w = [i for i, j in enumerate(freq_w) if j == m_w]

m_h = max(freq_h)
pos_h = [i for i, j in enumerate(freq_h) if j == m_h]

max_ = m_w + m_h - 1
found = False
#
# for el in pos:
#     i, j = el
#     max_

for i in pos_h:
    for j in pos_w:
        if not((i, j) in pos):
            found = True
            break
    if found == True:
        break

print(max_ + int(found))
### BUGGY CODE ###


s = input()
n = int(input())

for i in range(n):
    Q = input()
    if int(Q[0]) == 2:
        F = int(Q[2])
        C = Q[4]
        #print(F, C)
        if F == 1:
            s = C + s
        else:
            s = s + C
    else:
        s = s[::-1]
    
print(s)


### FIXED CODE ###


s = list(input())
n = int(input())

flag = 0
s1 = []
s2 = s
for i in range(n):
    Q = input()
    if Q[0] == "2":
        if Q[2] == "1":
            if flag == 1:
                s2.append(Q[4])
            else:
                s1.append(Q[4])
        else:
            if flag == 0:
                s2.append(Q[4])
            else:
                s1.append(Q[4])
    else:
        flag ^= 1
s2 = s1[::-1] + s2
print("".join(s2) if flag == 0 else "".join(s2[::-1]))
### BUGGY CODE ###

n = int(input())
data_input = []
for i in range(n):
    data_input.append(input().split())
dlList = []
for x in data_input:
    if x[0] == 'insert':
        dlList.insert(0,x[1])
    elif x[0] == 'delete':
        if x[1] in dlList:
            dlList.remove(x[1])
        else :
            pass
    elif x[0] == 'deleteFirst':
        dlList.pop(0)
    elif x[0] == 'deleteLast':
        dlList.pop()
    else:
        pass
print(' '.join(dlList))

### FIXED CODE ###

import sys
from collections import deque
input_lines = sys.stdin.readlines()

doublyLinkedList = deque()


for x in input_lines[1:]:
    oneLine = x.split()
    if 'insert' in oneLine[0]:
        doublyLinkedList.appendleft((int)(oneLine[1]))
    elif 'delete' == oneLine[0]:
        remove_value = (int)(oneLine[1])
        if remove_value in doublyLinkedList:
            doublyLinkedList.remove(remove_value)
    elif 'deleteFirst'in oneLine[0]:
        doublyLinkedList.popleft()
    elif 'deleteLast' in oneLine[0]:
        doublyLinkedList.pop()

print(*doublyLinkedList)
### BUGGY CODE ###

s = input()[::-1] # 入力文字列を逆順でsに格納

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    #d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans) # 答えの出力
"""
def main(S):
    a = len(S)
    b = list(0 for j in range(4, a+1)
        for i in range(a-j+1)
            if int(S[i:i+j])%2019==0)
    print(len(b))
if __name__ == '__main__':
    main(S)
"""
"""
def solution(S):
    a = len(S)
    b = list(0 for j in range(4, a+1)
        for i in range(a-j+1)
            if int(S[i:i+j])%3==0 and int(S[i:i+j])%673==0)
    print(len(b))
solution(S)
"""
"""
def main(S):
    count = 0
    a = len(S)
    
    
    if int(S)%2019 == 0:
        count += 1
    for j in range(a):
        for i in range(j,a):
            if (int(S[j:]) - int(S[i:]))%2019 == 0:
                count += 1
    print(count)
if __name__ == '__main__':
    main(S)

"""

### FIXED CODE ###

s = input()[::-1] # 入力文字列を逆順でsに格納

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans) # 答えの出力
### BUGGY CODE ###

n = int(input())

from fractions import Fraction

for i in range(1,3501):
    for j in range(i,3501):
        k = Fraction(4, n) - Fraction(1, i) - Fraction(1, j)
        if k.numerator == 1:
            print(i, j, k.denominator)
            import sys
            sys.exit()


### FIXED CODE ###

n = int(input())

from fractions import Fraction

for i in range(1,3501):
    for j in range(1,3501):
        if 4*i*j - n*i - n*j > 0:
            if (i * j * n) % (4*i*j - n*i - n*j) == 0 and (i * j * n) // (4*i*j - n*i - n*j) > 0:
                print(i,j, (i * j * n) // (4*i*j - n*i - n*j))
                import sys
                sys.exit()
### BUGGY CODE ###

# B - Frog 2
N, K = map(int,input().split())
h = list(map(int, input().split()))
dp = [float('inf')]*N

dp[0] = 0
dp[1] = abs(h[1] - h[0])
for i in range(2,N):
    for k in range(1,K+1):
        dp1 = dp[i - k]+ abs(h[i] - h[i - k])
        if dp[i] >= dp1:
            dp[i] = dp1
        else:
            pass
print(dp[N-1])

### FIXED CODE ###

#B - Frog 2
N, K = map(int, input().split())
h = list(map(int, input().split()))
dp = [10 ** 9] * N
dp[0] = 0
dp[1] = abs(h[1] - h[0])

for i in range(2,N):
    for j in range(K,0,-1):
        dp[i] = min(dp[i],dp[i-j] + abs(h[i] - h[i-j]))

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

n = int(input())
l = [list(map(int,input().split())) for _ in range(n-1)]
#i番目駅からのスタート
for i in range(n):
  #時間
  t = 0
  for j in range(i,n-1):
    c,s,f = l[j]
    while not (t>=s and t%f==0):
      t += 1
    t += c
  print(t)

### FIXED CODE ###

n = int(input())
l = [list(map(int,input().split())) for _ in range(n-1)]
#i番目駅からのスタート
for i in range(n):
  #時間
  t = 0
  for j in range(i,n-1):
    c,s,f = l[j]
    if t>=s:
      if t%f!=0:
        t += -(t%f)+f
      else:
        t += 0
    else:
      t += (s-t)
      if t%f!=0:
        t += -(t%f)+f
      else:
        t += 0
    t += c
  print(t)
### BUGGY CODE ###

n,W=map(int,input().split())
wv=[[int(j) for j in input().split()] for i in range(n)]
dp=[[0]*110000 for i in range(110)]
INF=10**10
for j in range(1,110000):
  dp[0][j]=INF

for i in range(1,n+1):
  for j in range(110000):
    dp[i][j]=dp[i-1][j]
    if j-wv[i-1][1]>=0:
      dp[i][j]=min(dp[i][j],
                   dp[i-1][j-wv[i-1][1]]+wv[i-1][0])
ans=0
for v in range(110000):
  if dp[n][v]<=W:
    ans=max(ans,v)
      
print(ans)

### FIXED CODE ###

n,W=map(int,input().split())
wv=[[int(j) for j in input().split()] for i in range(n)]
dp=[[0]*110000 for i in range(110)]
INF=10**10
for j in range(1,110000):
  dp[0][j]=INF

for i in range(1,n+1):
  for j in range(110000):
    dp[i][j]=dp[i-1][j]
    if j-wv[i-1][1]>=0:
      dp[i][j]=min(dp[i][j],
                   dp[i-1][j-wv[i-1][1]]+wv[i-1][0])
ans=0
for v in range(110000):
  if dp[n][v]<=W:
    ans=max(ans,v)
      
print(ans)
### BUGGY CODE ###

from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
s = Counter(A)
t=[0]*(10**5+1)
for i in range(10**5+1):
    t[i] = s[i]*i

sumnum=sum(t)
for i in range(Q):
    B, C = map(int, input().split())
    num = t[B]//(B)
    t[B] = 0
    t[C] += num*(C)
    sumnum=sumnum-num*B+num*C
    print(sum(t))


### FIXED CODE ###

from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
s = Counter(A)
t=[0]*(10**5+1)
for i in range(10**5+1):
    t[i] = s[i]*i

sumnum=sum(t)
for i in range(Q):
    B, C = map(int, input().split())
    num = t[B]//(B)
    t[B] = 0
    t[C] += num*(C)
    sumnum=sumnum-num*B+num*C
    print(sumnum)
### BUGGY CODE ###

n = int(input())
s = list(input())
ans = 0
for i in range(1000):
    pin = list(str(i).zfill(3))
    idx = 0
    for c in s:
        if c == pin[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break
print(ans)

### FIXED CODE ###

# 000~999で決め打ちして数える
# TLEになるんだけどこれそんなに重いか？？？？？

n = int(input())
s = input()
ans = 0

for i in range(0, 1000):
    t = format(i, "0>3")
    idx = 0
    for c in s:
        if c == t[idx]:
            idx += 1
        if idx == 3:
            ans += 1
            break

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

import sys
n = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for i in range(n)]
for j in range(n):
    B = A.copy()
    B.remove(B[j])
    print(max(B))

### FIXED CODE ###

import sys
n = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for i in range(n)]
B = sorted(A)
m1 = B[-1]
m2 = B[-2]

for a in A:
    if a == m1:
        print(m2)
    else:
        print(m1)