AtCoder Beginner Contest 134 - Exception Handling

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:
 - Find the maximum value among the N-1 elements other than A_i in the sequence.

-----Constraints-----
 - 2 \leq N \leq 200000
 - 1 \leq A_i \leq 200000
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1
:
A_N

-----Output-----
Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

-----Sample Input-----
3
1
4
3

-----Sample Output-----
4
3
4

 - The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.
 - The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.
 - The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Source: atcoder | Difficulty: EASY


Solutions:
n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a, reverse=True)
[print(b[0]) if a[i] != b[0] else print(b[1]) for i in range(n)]


N = int(input())
A = []
for i in range(N):
	A += [int(input())]
B = sorted(A)
for i in range(N):
	if A[i] == B[-1]:
		print(B[-2])
	else:
		print(B[-1])


n = int(input())
A = [int(input()) for i in range(n)]
B = sorted(A, reverse=True)
for i in range(n):
	if A[i] != B[0]:
		print(B[0])
	else:
		print(B[1])


n = int(input())
a = [int(input()) for _ in range(n)]
l = sorted(a, reverse=True)
x = l[0]
y = l[1]
for i in a:
	if i != x:
		print(x)
	else:
		print(y)


n = int(input())
v = [int(input()) for i in range(n)]
t = sorted(v, reverse=True)
for i in v:
	print(t[1] if i == t[0] else t[0])


n = int(input())
a = [int(input()) for i in range(n)]
x = sorted(a)[-1]
y = sorted(a)[-2]
for i in range(n):
	if a[i] != x:
		print(x)
	else:
		print(y)


N = int(input())
a = [int(input()) for i in range(N)]
b = sorted(a)[::-1]
for i in range(N):
	if a[i] == b[0]:
		print(b[1])
	else:
		print(b[0])


import sys
input = sys.stdin.readline
n = int(input())
a = list((int(input()) for _ in range(n)))
b = sorted(a, reverse=True)
for i in range(n):
	if a[i] != b[0]:
		print(b[0])
	else:
		print(b[1])


N = int(input())
A = [int(input()) for _ in range(N)]
M = max(A)
M_index = A.index(M)
sub_M = 0
for i in range(N):
	if i != M_index:
		sub_M = max(sub_M, A[i])
for i in range(N):
	if i == M_index:
		print(sub_M)
	else:
		print(M)


n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
m = b[-1]
for i in a:
	print(m) if m != i else print(b[-2])


import heapq
n = int(input())
a = [int(input()) for _ in range(n)]
(m1, m2) = heapq.nlargest(2, a)
for ai in a:
	print(m1 if ai != m1 else m2)


n = int(input())
a_l = [int(input()) for _ in range(n)]
l = []
r = []
l_max = 0
r_max = 0
for i in range(n):
	l_max = max(l_max, a_l[i])
	r_max = max(r_max, a_l[-i - 1])
	l.append(l_max)
	r.append(r_max)
for i in range(n):
	if i == 0:
		t_l = 0
	else:
		t_l = l[i - 1]
	if i == n - 1:
		t_r = 0
	else:
		t_r = r[-i - 2]
	print(max([t_l, t_r]))


import copy
N = int(input())
ary = []
for _ in range(N):
	ary.append(int(input()))
ary_copy = copy.copy(ary)
ary_copy.sort()
max_1 = ary_copy[-1]
max_2 = ary_copy[-2]
for i in ary:
	if i == max_1:
		print(max_2)
	elif i != max_1:
		print(max_1)


n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
ans = [b[-1]] * n
ans[a.index(b[-1])] = b[-2]
print(*ans, sep='\n')


import copy
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
amax = max(A)
amaxindex = A.index(amax)
B = copy.deepcopy(A)
del B[amaxindex]
bmax = max(B)
for i in range(N):
	if i == amaxindex:
		print(bmax)
	else:
		print(amax)


from copy import copy
(n, *a) = list(map(int, open(0).read().split()))
b = copy(a)
c = copy(a)
for i in range(1, n):
	b[i] = max(b[i], b[i - 1])
for i in range(n - 2, -1, -1):
	c[i] = max(c[i], c[i + 1])
b.append(-1)
c.append(-1)
for i in range(n):
	print(max(b[i - 1], c[i + 1]))


n = int(input())
A = [int(input()) for i in range(n)]
B = sorted(A)
for a in A:
	if a == B[-1]:
		print(B[-2])
	else:
		print(B[-1])


n = int(input())
arr = [int(input()) for _ in range(n)]
ar = sorted(arr)
mx = ar[-1]
sc = ar[-2]
for ele in arr:
	if ele == mx:
		print(sc)
	else:
		print(mx)


n = int(input())
numbers = []
for i in range(n):
	a = int(input())
	numbers.append(a)
b = max(numbers)
max_index = numbers.index(max(numbers))
numbers.remove(b)
for i in range(n):
	if i == max_index:
		print(max(numbers))
	else:
		print(b)


n = int(input())
a = [int(input()) for i in range(n)]
q = sorted(a)
for i in range(n):
	if a[i] != q[-1]:
		print(q[-1])
	else:
		print(q[-2])


N = int(input())
arr = [int(input()) for _ in range(N)]
arr2 = sorted(arr)
for i in range(N):
	print(arr2[-2] if arr[i] == arr2[-1] else arr2[-1])


n = int(input())
L = list((int(input()) for _ in range(n)))
SL = sorted(L)
first_max = SL[-1]
second_max = SL[-2]
for l in L:
	if l != first_max:
		print(first_max)
	else:
		print(second_max)


n = int(input())
a = [int(input()) for _ in range(n)]
l = sorted(a)
for i in a:
	if l[-1] == i:
		print(l[-2])
	else:
		print(l[-1])


n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
for v in a:
	if v != b[-1]:
		print(b[-1])
	else:
		print(b[-2])


N = int(input())
lsA = [int(input()) for i in range(N)]
maxA = max(lsA)
Akazu = lsA.count(maxA)
for i in range(N):
	if lsA[i] == maxA and Akazu == 1:
		print(max(lsA[:i] + lsA[i + 1:]))
	else:
		print(maxA)


n = int(input())
max_a = 0
next_a = 0
max_loc = 0
for i in range(n):
	ai = int(input())
	if ai > max_a:
		max_a = ai
		max_loc = i
	elif ai > next_a:
		next_a = ai
if next_a == 0:
	for i in range(n):
		print(max_a)
else:
	for i in range(max_loc):
		print(max_a)
	print(next_a)
	for i in range(n - max_loc - 1):
		print(max_a)


(n, *a) = map(int, open(0))
b = sorted(a)
for i in a:
	if b[-1] != i:
		print(b[-1])
	else:
		print(b[-2])


import bisect
N = int(input())
A = [int(input()) for _ in range(N)]
sort_A = sorted(A)
max_val = max(A)
second_val = sort_A[bisect.bisect_right(sort_A, max_val) - 2]
for i in range(N):
	if A[i] == max_val:
		print(second_val)
	else:
		print(max_val)


(n, *A) = map(int, open(0).read().split())
B = sorted(A)
for a in A:
	if a == B[-1]:
		print(B[-2])
	else:
		print(B[-1])


(n, *a) = map(int, open(0).read().split())
b = sorted(a)[::-1]
ma1 = b[0]
ma2 = b[1]
for i in a:
	if ma1 == i:
		print(ma2)
	else:
		print(ma1)


N = int(input())
A = [int(input()) for i in range(N)]
m = max(A)
m2 = sorted(A)[-2]
m_i = A.index(m)
ans = [m] * N
ans[m_i] = m2
print('\n'.join(list(map(str, ans))))


N = int(input())
A = [int(input()) for n in range(N)]
a1 = sorted(A)[-1]
a2 = sorted(A)[-2]
for a in A:
	if a == a1:
		print(a2)
	else:
		print(a1)


n = int(input())
A = []
for i in range(n):
	A.append(int(input()))
ma = max(A)
ma2 = sorted(A)[-2]
for a in A:
	if a == ma:
		print(ma2)
	else:
		print(ma)


n = int(input())
a = [int(input()) for i in range(n)]
a2 = sorted(a, reverse=True)
for i in a:
	print(a2[0] if i != a2[0] else a2[1])


n = int(input())
xlist = [int(input()) for i in range(n)]
ylist = sorted(xlist)
max1 = ylist[-1]
max2 = ylist[-2]
for i in range(n):
	if xlist[i] != max1:
		print(max1)
	else:
		print(max2)


n = int(input())
a = [int(input()) for _ in [0] * n]
M = max(a)
k = a.index(M)
m = max(a[:k] + a[k + 1:])
for i in range(n):
	if i != k:
		print(M)
	else:
		print(m)


N = int(input())
A = [int(input()) for _ in range(N)]
ll = sorted(A)
a = max(A)
b = ll[-2]
for n in A:
	if n == a:
		print(b)
	else:
		print(a)


n = int(input())
a = [int(input()) for _ in range(n)]
a1 = sorted(a)
st = a1[-1]
nd = a1[-2]
for i in a:
	if i == st:
		print(nd)
	else:
		print(st)


n = int(input())
a = [int(input()) for i in range(n)]
a_ = sorted(a)
m = a_[-1]
for i in range(n):
	if a[i] != m:
		print(m)
	else:
		print(a_[-2])


(N, *A) = map(int, open(0).read().split())
(a, b) = sorted(A, reverse=True)[:2]
for x in A:
	if x != a:
		print(a)
	else:
		print(b)


n = int(input())
a = [int(input()) for _ in [0] * n]
(b, c) = sorted(a, reverse=True)[0:2]
for A in a:
	if A == b:
		print(c)
	else:
		print(b)


import sys
input = sys.stdin.readline
n = int(input())
A = []
for _ in range(n):
	A.append(int(input()))
sort_A = sorted(A, reverse=True)
max_1 = sort_A[0]
max_2 = sort_A[1]
if max_1 == max_2:
	for _ in range(n):
		print(max_1)
else:
	for i in range(n):
		if A[i] == max_1:
			print(max_2)
		else:
			print(max_1)


(n, *a) = map(int, open(0).read().split())
b = sorted(a)
for i in range(n):
	print(b[-1] if a[i] != b[-1] else b[-2])


n = int(input())
a = [int(input()) for _ in range(n)]
b = list(reversed(sorted(a)))
for x in a:
	if x == b[0]:
		print(b[1])
	else:
		print(b[0])


import os
import sys
from collections import Counter

def main():
	n = int(input())
	A = []
	for _ in range(n):
		A.append(int(input()))
	A_cnt = Counter(A)
	flag = 0
	A_max = max(A)
	A_next_max = sorted(A, reverse=True)[1]
	if A_cnt[A_max] > 1:
		flag = 1
	for a in A:
		if a == A_max and flag == 1:
			print(A_max)
		elif a == A_max and flag == 0:
			print(A_next_max)
		else:
			print(A_max)

def __starting_point():
	main()
__starting_point()


N = int(input())
l = [int(input()) for i in range(N)]
l_ = sorted(l)
for Ai in l:
	if l_[-1] == Ai:
		print(l_[-2])
	else:
		print(l_[-1])


import copy
N = int(input())
A_list = []
for _ in range(N):
	A_list.append(int(input()))
sort_list = copy.deepcopy(A_list)
sort_list.sort()
a_max = sort_list[-1]
a_max_second = sort_list[-2]
if a_max == a_max_second:
	for _ in range(N):
		print(a_max)
else:
	for i in range(N):
		if A_list[i] == a_max:
			print(a_max_second)
		else:
			print(a_max)


N = int(input())
A = [0] * N
for i in range(N):
	A[i] = int(input())
sortedA = sorted(A)
if sortedA[N - 1] == sortedA[N - 2]:
	for _ in range(N):
		print(sortedA[N - 1])
else:
	for i in range(N):
		if A[i] != sortedA[N - 1]:
			print(sortedA[N - 1])
		else:
			print(sortedA[N - 2])


n = int(input())
a = [int(input()) for _ in range(n)]
(s, f) = sorted(a)[-2:]
for ai in a:
	print(f if ai != f else s)


n = int(input())
a = [int(input()) for _ in range(n)]
m_1 = max(a)
m_2 = sorted(a)[n - 2]
for i in range(n):
	if a[i] == m_1:
		print(m_2)
	else:
		print(m_1)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
a_max = max(a)
a_max_cnt = a.count(a_max)
for i in range(n):
	if a[i] != a_max or a_max_cnt > 1:
		print(a_max)
	else:
		print(max(a[:i] + a[i + 1:]))


n = int(input())
l = [int(input()) for _ in range(n)]
(a, b) = sorted(l)[-2:]
print('\n'.join((str(a) if e == b else str(b) for e in l)))


N = int(input())
A = [int(input()) for _ in range(N)]
l = sorted(A)[::-1]
for i in A:
	print(l[1] if i == l[0] else l[0])


N = int(input())
A = [int(input()) for n in range(N)]
m = max(A)
n = sorted(A, reverse=True)[1]
print('\n'.join([str(m) if a < m else str(n) for a in A]))


N = int(input())
A = [int(input()) for i in range(N)]
sorted_a = sorted(A, reverse=True)
max_val = sorted_a[0]
next_max_val = sorted_a[1]
for a in A:
	if a == max_val:
		print(next_max_val)
	else:
		print(max_val)


N = int(input())
A = [int(input()) for _ in range(N)]
(m1, m2) = sorted(A, reverse=True)[0:2]
for a in A:
	print(m2 if a == m1 else m1)


N = int(input())
A = [int(input()) for _ in range(N)]
li = A.copy()
li.sort()
f = li[-1]
s = li[-2]
for i in range(N):
	if f == A[i]:
		print(s)
	else:
		print(f)


n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a)
m1 = b[-1]
m2 = b[-2]
for c in a:
	if c == m1:
		print(m2)
	else:
		print(m1)


from collections import Counter
N = int(input())
Alist = []
maxi = 0
n2 = 0
for _ in range(N):
	A = int(input())
	Alist.append(A)
	if A > maxi:
		n2 = maxi
		maxi = A
	elif A > n2:
		n2 = A
count = Counter(Alist)
for A in Alist:
	if A != maxi:
		print(maxi)
	elif A == maxi and count[A] > 1:
		print(maxi)
	else:
		print(n2)


N = int(input())
L = [int(input()) for i in range(N)]
NL = sorted(L)
for i in range(N):
	if L[i] == NL[N - 1]:
		print(NL[N - 2])
	else:
		print(NL[N - 1])


p = print
(n, *a) = map(int, open(0))
r = range(n)
m = max(a)
o = sorted(a)[-2]
[[p(m) for _ in r] if m == o else [p(o) if a[i] == m else p(m) for i in r]]


N = int(input())
A = [int(input()) for _ in range(N)]
mx = max(A)
mx2 = sorted(A)[-2]
for a in A:
	print(mx) if a != mx else print(mx2)


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
b = sorted(a)
(m, M) = b[-2:]
for i in range(n):
	print(m if a[i] == M else M)


import sys
import copy
import math
import itertools
import numpy as np
N = int(input())
A = [int(input()) for c in range(N)]
B = sorted(copy.deepcopy(A), reverse=True)
for i in range(N):
	if A[i] == B[0]:
		print(B[1])
	else:
		print(B[0])


n = int(input())
seq = [int(input()) for i in range(n)]
s_seq = sorted(seq, reverse=True)
for x in seq:
	print(s_seq[s_seq[0] == x])


N = int(input())
A = [int(input()) for _ in range(N)]
Asorted = sorted(A)
Amax1 = Asorted[-1]
Amax2 = Asorted[-2]
for a in A:
	if a != Amax1:
		print(Amax1)
	else:
		print(Amax2)


N = int(input())
A = [int(input()) for i in range(N)]
(x, y) = sorted(A)[-2:]
for a in A:
	print(y if a != y else x)


N = int(input())
A = []
for _ in range(N):
	A.append(int(input()))
AA = sorted(A)
most = AA[N - 1]
second = AA[N - 2]
for i in range(N):
	if A[i] == most:
		print(second)
	else:
		print(most)


N = int(input())
L = []
M = []
for i in range(N):
	a = int(input())
	L.append(a)
	M.append(a)
L.sort()
for j in range(N):
	if M[j] == L[len(L) - 1]:
		print(L[len(L) - 2])
	else:
		print(L[len(L) - 1])


n = int(input())
l = [int(input()) for i in range(n)]
L = sorted(l)
L.reverse()
x = L[0]
y = L[1]
for i in range(n):
	if l[i] != x:
		print(x)
	else:
		print(y)


N = int(input())
A = [int(input()) for _ in range(N)]
(m, s) = (0, 0)
for a in A:
	if a > m:
		m = a
	elif a > s:
		s = a
for a in A:
	if a == m:
		print(s)
	else:
		print(m)


n = int(input())
A = [int(input()) for _ in range(n)]
S = sorted(A)
for a in A:
	print(S[-1] if a != S[-1] else S[-2])


N = int(input())
A = [int(input()) for i in range(N)]
AS = sorted(A, reverse=True)
for i in range(N):
	print(AS[0] if AS[0] != A[i] else AS[1])


N = int(input())
A = [int(input()) for i in range(N)]
I1 = A.index(max(A))
M1 = A.pop(I1)
M2 = max(A)
for j in range(N):
	if j == I1:
		print(M2)
	else:
		print(M1)


n = int(input())
a = []
for _ in range(n):
	a.append(int(input()))
a_max = max(a)
flg = False
if a.count(a_max) > 1:
	flg = True
a_s = sorted(a, reverse=True)
for ai in a:
	if ai != a_max:
		print(a_max)
	elif flg:
		print(a_max)
	else:
		print(a_s[1])


N = int(input())
A = [int(input()) for i in range(N)]
B = A.copy()
B.sort()
for i in range(N):
	if A[i] == B[-1]:
		print(B[-2])
	else:
		print(B[-1])


n = int(input())
a = []
for i in range(n):
	A = int(input())
	a.append(A)
a_1 = list(set(a))[-1]
if a.count(a_1) >= 2:
	for i in range(n):
		print(a_1)
else:
	a_2 = list(set(a))[-2]
	for i in range(n):
		if a[i] == a_1:
			print(a_2)
		else:
			print(a_1)


import copy
n = int(input())
a = []
for i in range(n):
	s = int(input())
	a.append(s)
b = copy.copy(a)
a.sort(reverse=True)
c = b.index(a[0])
for j in b:
	if j == b[c]:
		print(a[1])
	else:
		print(a[0])


n = int(input())
A = [int(input()) for _ in range(n)]
sort_A = sorted(A, reverse=True)
for a in A:
	if a == sort_A[0]:
		print(sort_A[1])
	else:
		print(sort_A[0])


n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
for a in a:
	print(b[(a < b[-1]) - 2])


N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
C = max(A)
for i in A:
	if C == i:
		print(B[-2])
	else:
		print(C)


N = int(input())
a = [int(input()) for i in range(N)]
sw = True
b = sorted(a, reverse=True)
if b[0] > b[1]:
	max2 = b[1]
	max1 = b[0]
	sw = False
if sw == True:
	max1 = b[0]
	for k in range(N):
		print(max1)
else:
	for k in range(N):
		if a[k] == max1:
			print(max2)
		else:
			print(max1)


n = int(input())
t = [int(input()) for i in range(n)]
m = max(t)
m2 = sorted(t)[-2]
for i in t:
	if i != m:
		print(m)
	else:
		print(m2)


N = int(input())
A = tuple((int(input()) for _ in range(N)))
B = tuple(sorted(A, reverse=True))
print(*[B[0] if a != B[0] else B[1] for a in A], sep='\n')


import copy
N = int(input())
A = [int(input()) for i in range(N)]
B = A.copy()
B.sort()
(M, m) = (B[-1], B[-2])
for a in A:
	print(M if a != M else m)


n = int(input())
l = [int(input()) for _ in range(n)]
s = sorted(l)
for x in l:
	if x == s[-1]:
		print(s[-2])
	else:
		print(s[-1])


n = int(input())
a = list((int(input()) for _ in range(n)))
(mx_1, mx) = sorted(a)[-2:]
for i in a:
	if i == mx:
		print(mx_1)
	else:
		print(mx)


n = int(input())
lst = []
for i in range(n):
	x = int(input())
	lst.append(x)
a = max(lst)
b = []
for i in range(n):
	if lst[i] == a:
		b.append(i)
if len(b) > 1:
	for i in range(n):
		print(a)
else:
	lst[b[0]] = 0
	c = max(lst)
	for i in range(n):
		if i == b[0]:
			print(c)
		else:
			print(a)


N = int(input())
A = [int(input()) for _ in range(N)]
max1 = max(A)
max2 = sorted(A)[-2]
for a in A:
	print(max1 if max1 != a else max2)


n = int(input())
l = [int(input()) for _ in range(n)]
k = sorted(l)
for i in l:
	if i < k[-1]:
		print(k[-1])
	if i == k[-1]:
		print(k[-2])


(N, *d) = map(int, open(0).read().split())
m = d.index(max(d))
c = [max(d)] * N
d[m] = 0
y = max(d)
c[m] = y
for i in range(len(c)):
	print(c[i])


import copy
n = int(input())
A = []
for i in range(n):
	A.append(int(input()))
B = copy.copy(A)
B.sort()
A_max = B[-1]
A_max2 = B[-2]
for i in range(n):
	if A[i] < A_max:
		print(A_max)
	elif A[i] == A_max:
		print(A_max2)


import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
for a in a:
	print(b[(a < b[-1]) - 2])


N = int(input())
A = [int(input()) for _ in range(N)]
B = A[:]
B.sort()
for a in A:
	if a == B[N - 1]:
		print(B[N - 2])
	else:
		print(B[N - 1])


n = int(input())
a = [[int(input()), i] for i in range(n)]
a.sort()
c = a[-1][0]
d = a[-2][0]
e = a[-1][1]
for i in range(n):
	if i == e:
		print(d)
	else:
		print(c)


N = int(input())
A = [int(input()) for i in range(N)]
(max_v, next_max_v) = sorted(A, reverse=True)[:2]
for a in A:
	print(max_v if a != max_v else next_max_v)


import sys
input = sys.stdin.readline
import heapq

def main():
	numbers = []
	n = int(input())
	[numbers.append(int(input().rstrip())) for _ in range(n)]
	max_n = 0
	second_n = 0
	for i in numbers:
		if i > max_n:
			max_n = i
		elif i > second_n:
			second_n = i
	for i in numbers:
		if i == max_n:
			print(second_n)
		else:
			print(max_n)

def __starting_point():
	main()
__starting_point()


n = int(input())
l = [int(input()) for i in range(n)]
a = sorted(l)
b = a[-1]
c = a[-2]
for i in l:
	if i != b:
		print(b)
	else:
		print(c)


n = int(input())
a = []
for _ in range(n):
	a.append(int(input()))
b = a.copy()
import heapq

def _heappush_max(heap, item):
	heap.append(item)
	heapq._siftdown_max(heap, 0, len(heap) - 1)

def _heappop_max(heap):
	lastelt = heap.pop()
	if heap:
		returnitem = heap[0]
		heap[0] = lastelt
		heapq._siftup_max(heap, 0)
		return returnitem
	return lastelt
b = sorted(b, reverse=True)
b1 = _heappop_max(b)
b2 = _heappop_max(b)
for i in range(n):
	if a[i] == b1:
		print(b2)
	else:
		print(b1)


n = int(input())
A = [int(input()) for _ in range(n)]
maxm = sorted(A)
for i in range(n):
	if A[i] == maxm[-1]:
		print(maxm[-2])
	else:
		print(maxm[-1])


N = int(input())
A = [int(input()) for _ in range(N)]
b = sorted(A)
for a in A:
	if a == b[-1]:
		print(b[-2])
	else:
		print(b[-1])


(_, *a) = map(int, open(0))
b = sorted(a)
for i in a:
	print(b[(i < b[-1]) - 2])


N = int(input())
A = [int(input()) for i in range(N)]
a = max(A)
b = sorted(A)[-2]
for i in A:
	print(b if i == a else a)


(N, A) = (int(input()), [])
for _ in range(N):
	A.append(int(input()))
B = sorted(A)
for x in A:
	print(B[N - 2] if x == B[N - 1] else B[N - 1])


n = int(input())
a = [int(input()) for i in range(n)]
(sbmx, mx) = sorted(a)[n - 2:]
for m in a:
	if m == mx:
		print(sbmx)
	else:
		print(mx)


a = [int(input()) for _ in [0] * int(input())]
(m1, m2) = sorted(a, reverse=True)[:2]
b = [m2 if x == m1 else m1 for x in a]
print('\n'.join(map(str, b)))


n = int(input())
a = [int(input()) for _ in range(n)]
sa = sorted(a)
amx = sa[-1]
amx2 = sa[-2]
for ai in a:
	print(amx if ai != amx else amx2)


N = int(input())
b = [int(input()) for _ in range(N)]
a = sorted(b)
ma = a[-1]
sa = a[-2]
for c in b:
	if c == ma:
		print(sa)
	else:
		print(ma)


n = int(input())
a = [int(input()) for _ in range(n)]
s = sorted(a)
(one, two) = (s[-1], s[-2])
for i in a:
	print(one if i != one else two)


n = int(input())
l = [int(input()) for _ in range(n)]
s = sorted(l)
(a, b) = (s[-1], s[-2])
for i in l:
	print([b, a][a > i])


import copy
n = int(input())
an = [int(input()) for i in range(n)]
bn = copy.copy(an)
top = max(an)
an.remove(top)
top2 = max(an)
for x in bn:
	if x == top:
		print(top2)
	else:
		print(top)


N = int(input())
A = [int(input()) for _ in range(N)]
(m2, m) = sorted(A)[-2:]
for i in range(N):
	if A[i] == m:
		print(m2)
	else:
		print(m)


N = int(input())
A = [int(input()) for i in [0] * N]
M = max(A)
m = sorted(A)[-2]
for i in A:
	if i == M:
		print(m)
	else:
		print(M)


n = int(input())
z = [int(input()) for i in range(n)]
l = sorted(z)
a = l[-1]
s = l[-2]
for d in z:
	print(a if d != a else s)


n = int(input())
a = [int(input()) for _ in range(n)]
(b, c) = sorted(a)[-2:]
j = a.index(c)
for i in range(n):
	print((c, b)[i == j])


import collections
n = int(input())
a = [int(input()) for i in range(n)]
m = sorted(list(set(a)))
c = collections.Counter(a)
t = [max(a)] * n
if c[max(a)] == 1:
	t[a.index(max(a))] = m[-2]
[print(i) for i in t]


n = [int(input()) for _ in range(int(input()))]
(N1, N2) = (max(n), sorted(n)[-2])
for i in n:
	if i < N1:
		print(N1)
	elif i == N1:
		print(N2)


(n, *a) = map(int, open(0))
b = sorted(a)
for i in range(n):
	print([b[-2], b[-1]][a[i] < b[-1]])


n = int(input())
l = [int(input()) for i in range(n)]
ls = l[:]
ls.sort()
for i in l:
	print(ls[-1] if i != ls[-1] else ls[-2])


N = int(input())
nums = []
for i in range(N):
	A = int(input())
	nums.append(A)
nums2 = sorted(nums, reverse=True)
for i in nums:
	if i == nums2[0]:
		print(nums2[1])
	else:
		print(nums2[0])


N = int(input())
A = []
for i in range(N):
	a = int(input())
	A.append(a)
B = sorted(A)
if B[-1] != B[-2]:
	t = A.index(B[-1])
	for i in range(N):
		if i == t:
			print(B[-2])
		else:
			print(B[-1])
else:
	for i in range(N):
		print(B[-1])


N = int(input())
A = [int(input()) for _ in [0] * N]
B = sorted(A)[::-1]
for a in A:
	if a != B[0]:
		print(B[0])
	else:
		print(B[1])


import sys
import numpy as np
n = int(input())
a = [int(input()) for _ in range(n)]
mx = str(max(a))
mx_id = a.index(int(mx))
a.remove(int(mx))
mx_sub = str(max(a))
ans = '\n'.join((mx if i != mx_id else mx_sub for i in range(n)))
print(ans)


(n, *a) = map(int, open(0).read().split())
am = sorted(a)[::-1][:2]
for i in a:
	print(am[i == am[0]])


(_, *a) = map(int, open(0).read().split())
s = sorted(a)
for i in a:
	print(s[(i < s[-1]) - 2])


N = int(input())
A = [int(input()) for i in range(N)]
M = A[::-1]
M.sort()
for i in A:
	if i == M[-1]:
		print(M[-2])
	else:
		print(M[-1])


n = int(input())
a = list((int(input()) for i in range(n)))
b = sorted(a)
for i in range(n):
	if a[i] == b[-1]:
		print(b[-2])
	else:
		print(b[-1])


n = int(input())
l = [int(input()) for i in range(n)]
a = sorted(l, reverse=True)
for v in l:
	if v == a[0]:
		print(a[1])
	else:
		print(a[0])


N = int(input())
A = [int(input()) for _ in range(N)]
sort_A = sorted(A, reverse=True)
ans = []
for i in range(N):
	if sort_A[0] == A[i]:
		ans.append(sort_A[1])
	else:
		ans.append(sort_A[0])
for a in ans:
	print(a)


A = [int(input()) for i in range(int(input()))]
one = sorted(A)[-1]
two = sorted(A)[-2]
for i in A:
	print(one if i != one else two)


N = int(input())
l = [int(input()) for _ in range(N)]
largest = max(l)
largest_index = l.index(max(l))
second_largest = sorted(l)[-2]
second_largest_index = l.index(second_largest)
for (idx, n) in enumerate(l):
	if idx == largest_index:
		print(second_largest)
	else:
		print(largest)


N = int(input())
A = [int(input()) for _ in range(N)]
S = sorted(A)
m1 = S[-1]
m2 = S[-2]
for a in A:
	if a == m1:
		print(m2)
	else:
		print(m1)


import heapq
n = int(input())
a = [int(input()) for _ in range(n)]
(a1, a2) = heapq.nlargest(2, a)
[print(a1) if _ != a1 else print(a2) for _ in a]


n = int(input())
a = list((int(input()) for _ in range(n)))
aa = sorted(a.copy())
(mx, sd) = (aa[n - 1], aa[n - 2])
for i in a:
	print(mx if i < mx else sd)


i = input
p = print
n = int(i())
r = range(n)
a = [int(i()) for _ in r]
m = max(a)
o = sorted(a)[-2]
[[p(m) for _ in r] if m == o else [p(o) if a[i] == m else p(m) for i in r]]


N = int(input())
A = [int(input()) for i in range(N)]
M = sorted(A)
for i in A:
	print([M[-1], M[-2]][i == M[-1]])


n = int(input())
aa = [int(input()) for _ in range(n)]
aho = sorted(aa)
for a in aa:
	print(aho[-1] if a < aho[-1] else aho[-2])


(N, *A) = map(int, open(0).read().split())
B = A[:]
B.sort()
b0 = B[-1]
b1 = B[-2]
for i in range(N):
	print(b1 if b0 == A[i] else b0)


n = int(input())
a = [int(input()) for i in range(n)]
inx = a.index(max(a))
a_s = sorted(a)
for i in range(n):
	print(a_s[-1] if inx != i else a_s[-2])


(n, *aa) = map(int, open(0).read().split())
bb = sorted(aa)
(f, s) = (bb[-1], bb[-2])
for a in aa:
	if a == f:
		print(s)
	else:
		print(f)


N = int(input())
A = [int(input()) for _ in range(N)]
M = max(A)
i = A.index(M)
M2 = max(A[:i] + A[i + 1:])
for a in A:
	print(M if a != M else M2)


N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A)
for n in range(N):
	if A[n] != B[-1]:
		print(B[-1])
	else:
		print(B[-2])


(n, *l) = map(int, open(0))
s = sorted(l)
for i in l:
	print(s[(i < s[-1]) - 2])


import math
from math import gcd, pi, sqrt
INF = float('inf')
MOD = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 6)
import itertools
import bisect
from collections import Counter, deque

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 main():
	N = i_input()
	A = [i_input() for i in range(N)]
	L = list(set(A))
	L.sort()
	m1 = L[-1]
	cnt = A.count(m1)
	if len(L) == 1:
		m2 = m1
	else:
		m2 = L[-2]
	for i in A:
		if i == m1 and cnt == 1:
			print(m2)
		else:
			print(m1)

def __starting_point():
	main()
__starting_point()


n = int(input())
a = []
for i in range(n):
	a.append(int(input()))
a_max = max(a)
a_second = sorted(a)[-2]
for i in range(n):
	print(a_max if a[i] != a_max else a_second)


N = int(input())
A = [int(input()) for _ in range(N)]
(m2, m1) = sorted(A)[N - 2:]
ans = [m1 if a != m1 else m2 for a in A]
for a in ans:
	print(a)


(n, *a) = map(int, open(0).read().split())
p = max(a)
k = a.index(p)
s = max(a[:k] + a[k + 1:])
print('\n'.join((str(p) if i != k else str(s) for i in range(n))))


n = int(input())
a = [int(input()) for _ in range(n)]
a_sorted = sorted(a)[::-1][:2]
for an in a:
	print(a_sorted[an == a_sorted[0]])


n = int(input())
a = [int(input()) for _ in range(n)]
p = a[:]
m = max(a)
a.remove(m)
m2 = max(a)
for i in p:
	if i == m:
		print(m2)
	else:
		print(m)


import sys
input = sys.stdin.readline
N = int(input())
A = []
for _ in range(N):
	A.append(int(input()))
for (i, a) in enumerate(sorted(A, reverse=True)):
	if i == 0:
		first = a
	elif i == 1:
		second = a
	else:
		break
for a in A:
	if a == first:
		print(second)
	else:
		print(first)


N = int(input())
A = [int(input()) for i in range(N)]
sorted_A = sorted(A, reverse=True)
(max_value, next_value) = (sorted_A[0], sorted_A[1])
for i in A:
	if i == max_value:
		print(next_value)
	else:
		print(max_value)


n = int(input())
t = [int(input()) for i in range(n)]
ma = max(t)
ma2 = sorted(t)[n - 2]
for j in t:
	if j != ma:
		print(ma)
	else:
		print(ma2)


n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a)
m = b[-1]
c = b[-2]
for i in range(0, n):
	if m == a[i]:
		print(c)
	else:
		print(m)


n = int(input())
a = [int(input()) for _ in range(n)]
t = sorted(a, reverse=True)
p = t[0]
q = t[1]
for ai in a:
	print(q if ai == p else p)


n = int(input())
a = [int(input()) for i in range(n)]
l1 = sorted(a)[-1]
l2 = sorted(a)[-2]
for i in a:
	if i == l1:
		print(l2)
	if i != l1:
		print(l1)


n = int(input())
a = [int(input()) for i in range(n)]
if len(set(a)) == 1:
	for i in range(n):
		print(a[0])
	return
x = sorted(a)[-1]
y = sorted(a)[-2]
for i in range(n):
	if a[i] != x:
		print(x)
	else:
		print(y)


n = int(input())
a = [0] * n
for i in range(n):
	a[i] = int(input())
b = sorted(a)
for x in a:
	print(b[-2] if x == b[-1] else b[-1])


n = int(input())
a = [int(input()) for _ in range(n)]
(m1, m2) = (-1, -1)
for i in range(n):
	if m1 < a[i]:
		m2 = m1
		m1 = a[i]
	elif m2 < a[i]:
		m2 = a[i]
for i in range(n):
	if a[i] == m1:
		print(m2)
	else:
		print(m1)


n = int(input())
l = [int(input()) for i in range(n)]
ma = sorted(l)[-2:]
for j in l:
	if j == ma[1]:
		print(ma[0])
	else:
		print(ma[1])


n = int(input())
a = [int(input()) for i in range(n)]
sa = sorted(a)
(am, asl) = (sa[-1], sa[-2])
print(*[asl if ai == am else am for ai in a], sep='\n')


(N, *A) = map(int, open(0).read().split())
B = sorted(A)
biggest = B[-1]
for i in A:
	if i == biggest:
		print(B[-2])
	else:
		print(biggest)


n = int(input())
l = [int(input()) for i in range(n)]
a = max(l)
b = sorted(l)[-2]
for i in l:
	print(b if i == a else a)


n = int(input())
d = [int(input()) for i in range(n)]
s = sorted(d)
for i in range(n):
	if s[-1] == d[i]:
		print(s[-2])
	else:
		print(s[-1])


N = int(input())
A_ls = []
for i in range(N):
	A_ls.append(int(input()))
sorted_A_ls = sorted(A_ls, reverse=True)
first_val = sorted_A_ls[0]
second_val = sorted_A_ls[1]
for val in A_ls:
	if val == first_val:
		print(second_val)
	else:
		print(first_val)


N = int(input())
S = [int(input()) for _ in range(N)]
A = sorted(S)
for i in range(N):
	if S[i] == A[-1]:
		if len(A) > 1:
			print(A[-2])
		else:
			print(A[-1])
	else:
		print(A[-1])


n = int(input())
a = [[int(input()), _] for _ in range(n)]
a.sort()
m1 = a[-1][0]
m2 = a[-2][0]
for i in range(n):
	if i == a[-1][1]:
		print(m2)
	else:
		print(m1)


n = int(input())
a = [int(input()) for i in range(n)]
b = a[:]
p = max(a)
b.remove(p)
q = max(b)
for i in a:
	if i != p:
		print(p)
	else:
		print(q)


N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A, reverse=True)
for i in range(N):
	if A[i] == B[0]:
		print(B[1])
	else:
		print(B[0])


(N, *D) = map(int, open(0).read().split())
S = sorted(D, reverse=True)
if S[1] == S[0]:
	[print(S[0]) for i in range(N)]
else:
	import numpy as np
	max_i = np.arange(N)[np.array(D) == S[0]]
	for i in range(N):
		if i == max_i:
			print(S[1])
		else:
			print(S[0])


n = int(input())
a = [int(input()) for _ in range(n)]
b = sorted(a)
mx = b[-1]
mx2 = b[-2]
for i in a:
	if i == mx:
		print(mx2)
	else:
		print(mx)


arr = []
for _ in range(int(input())):
	arr.append(int(input()))
arr2 = arr.copy()
arr2.sort()
z = arr2[-1]
zz = arr2[-2]
for k in arr:
	if k != z:
		print(z)
	else:
		print(zz)


N = int(input())
A = [int(input()) for _ in range(N)]
M1 = max(A[0], A[1])
M2 = min(A[0], A[1])
for i in range(2, N):
	if A[i] >= M1:
		M2 = M1
		M1 = A[i]
	else:
		M2 = max(M2, A[i])
for i in range(N):
	if A[i] == M1:
		print(M2)
	else:
		print(M1)


d = [int(input()) for i in range(int(input()))]
a = list(reversed(sorted(d)))[0:2]
[print(a[0] if i != a[0] else a[1]) for i in d]


n = int(input())
a = []
for _ in range(n):
	a.append(int(input()))
b = a.copy()
b.sort()
a_ = max(a)
for i in range(n):
	if a[i] == a_:
		print(b[-2])
	else:
		print(a_)


n = int(input())
a = [0] * n
(fm, fi) = (0, 0)
(sm, si) = (0, 0)
for i in range(n):
	a[i] = int(input())
	if a[i] > fm:
		(sm, si) = (fm, fi)
		(fm, fi) = (a[i], i)
	elif a[i] > sm:
		(sm, si) = (a[i], i)
for i in range(n):
	if i != fi:
		print(fm)
	else:
		print(sm)


n = int(input())
a = list((int(input()) for i in range(n)))
aa = sorted(a)
amax = aa[n - 1]
asecond = aa[n - 2]
for i in a:
	if i != amax:
		print(amax)
	else:
		print(asecond)


N = int(input())
A = [0] * N
for i in range(N):
	A[i] = int(input())
mx = max(A)
mxidx = A.index(mx)
for i in range(N):
	if i == mxidx:
		B = A[0:i] + A[i + 1:N]
		print(max(B))
	else:
		print(mx)


n = int(input())
a = [int(input()) for _ in range(n)]
aa = sorted(a)
b = aa[-1]
c = aa[-2]
for aa in a:
	if aa < b:
		print(b)
	else:
		print(c)


n = int(input())
l = [int(input()) for i in range(n)]
s = sorted(l)
p = s[-1]
q = s[-2]
for i in range(n):
	if l[i] == p:
		print(q)
	else:
		print(p)


N = int(input())
A = [int(input()) for i in range(N)]
m = max(A)
mindex = A.index(m)
for i in range(N):
	if i != mindex:
		print(m)
	else:
		pop = A.pop(i)
		print(max(A))
		A.insert(i, pop)


n = int(input())
a = [int(input()) for i in range(n)]
(m2, m1) = sorted(a)[n - 2:n]
for i in range(n):
	if a[i] == m1:
		print(m2)
	else:
		print(m1)


N = int(input())
A = []
for i in range(N):
	a = int(input())
	A.append(a)
AA = sorted(A, reverse=True)
for i in range(N):
	if A[i] != AA[0]:
		print(AA[0])
	else:
		print(AA[1])


N = int(input())
ls = []
for i in range(N):
	ls.append(int(input()))
first_val = sorted(ls, reverse=True)[0]
second_val = sorted(ls, reverse=True)[1]
for i in ls:
	if i == first_val:
		print(second_val)
	else:
		print(first_val)


def main():
	N = int(input())
	A = [int(input()) for _ in range(N)]
	sortA = sorted(A)
	ans = sortA[N - 1]
	ans2 = sortA[N - 2]
	if ans == ans2:
		flag = 1
	else:
		flag = 0
	for i in range(N):
		if flag == 1:
			print(ans)
		elif A[i] == ans:
			print(ans2)
		else:
			print(ans)
main()


n = int(input())
a = [int(input()) for i in range(n)]
a_1 = max(a)
a_2 = sorted(a)[-2]
for i in a:
	if i != a_1:
		print(a_1)
	else:
		print(a_2)


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

def MI():
	return map(int, input().split())

def MF():
	return map(float, input().split())

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

def TI():
	return tuple(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	N = I()
	L = [I() for i in range(N)]
	L2 = sorted(L)
	max_n = L2[N - 1]
	max_n2 = L2[N - 2]
	if max_n == max_n2:
		for i in range(N):
			print(max_n)
	else:
		for i in L:
			if i != max_n:
				print(max_n)
			else:
				print(max_n2)

def __starting_point():
	main()
__starting_point()


n = int(input())
alist = [int(input()) for _ in range(n)]
import copy
alist_copy = copy.copy(alist)
alist_copy.sort()
max_value = alist_copy[-1]
next_value = alist_copy[-2]
for a in alist:
	if a == max_value:
		print(next_value)
	else:
		print(max_value)


INT = lambda : int(input())
INTM = lambda : map(int, input().split())
STRM = lambda : map(str, input().split())
STR = lambda : str(input())
LIST = lambda : list(map(int, input().split()))
LISTS = lambda : list(map(str, input().split()))

def do():
	n = INT()
	A = []
	for i in range(n):
		A.append(INT())
	B = sorted(A, reverse=True)
	m1 = A.count(B[0])
	m2 = A.count(B[1])
	if m1 >= 2:
		flg = True
	else:
		index = A.index(B[0])
		flg = False
	if flg:
		for i in range(n):
			print(B[0])
	else:
		for i in range(n):
			if i != index:
				print(B[0])
			else:
				print(B[1])

def __starting_point():
	do()
__starting_point()


N = int(input())
A = [int(input()) for i in range(N)]
firstVal = max(A)
firstInd = A.index(firstVal)
secondVal = max(A[0:firstInd] + A[firstInd + 1:])
for i in range(firstInd):
	print(firstVal)
print(secondVal)
for i in range(N - firstInd - 1):
	print(firstVal)


N = int(input())
A = [int(input()) for _ in range(N)]
A2 = sorted(A)[::-1]
for a in A:
	if a == A2[0]:
		print(A2[1])
	else:
		print(A2[0])


n = int(input())
a = [int(input()) for i in range(n)]
b = sorted(a)
f = b[-1]
s = b[-2]
for ans in a:
	if ans < f:
		print(f)
	else:
		print(s)


n = int(input())
A = [int(input()) for _ in range(n)]
m = max(A)
ind = A.index(m)
A[ind] = 0
m2 = max(A)
for i in range(n):
	print(m2 if i == ind else m)


N = int(input())
b = []
max_num = 0
for i in range(N):
	a = int(input())
	b.append(a)
	max_num = max(a, max_num)
max_num2 = 0
num = b.count(max_num)
if num > 1:
	for i in range(N):
		print(max_num)
elif num == 1:
	for i in b:
		if i == max_num:
			pass
		else:
			max_num2 = max(max_num2, i)
	for i in b:
		if max_num == i:
			print(max_num2)
		else:
			print(max_num)


n = int(input())
A = [int(input()) for i in range(n)]
S = sorted(A)[::-1]
m = S[0]
sm = S[1]
for a in A:
	print(m if a != m else sm)


n = int(input())
list_a = []
for i in range(n):
	list_a.append(int(input()))
from collections import Counter
counter = dict(Counter(list_a))
sorted_counter = sorted(counter, reverse=True)
for item in list_a:
	counter[item] -= 1
	for submax in sorted_counter:
		if counter[submax] == 0:
			continue
		print(submax)
		break
	counter[item] += 1


n = int(input())
a = [0]
for i in range(n):
	a.append(int(input()))
a.append(0)
l = [0] * (n + 2)
l[0] = 0
for i in range(1, n + 1):
	l[i] = max(l[i - 1], a[i])
r = [0] * (n + 2)
r[n] = 0
for i in range(n, 0, -1):
	r[i] = max(r[i + 1], a[i])
for i in range(1, n + 1):
	print(max(l[i - 1], r[i + 1]))


n = int(input())
A = [int(input()) for _ in range(n)]
AM = sorted(A)[-2:]
for a in A:
	print(AM[a != AM[1]])


n = int(input())
a = [int(input()) for _ in range(n)]
maxa = max(a)
maxnum = a.count(maxa)
if maxnum > 1:
	for i in range(n):
		print(maxa)
else:
	maxidx = a.index(maxa)
	for i in range(n):
		if i != maxidx:
			print(maxa)
		else:
			a[maxidx] = 0
			print(max(a))


def main():
	N = int(input())
	(fir, fir_i, sec, sec_i) = (0, 0, 0, 0)
	for i in range(N):
		tmp = int(input())
		if tmp > fir:
			sec = fir
			sec_i = fir_i
			fir = tmp
			fir_i = i
		elif tmp > sec:
			sec = tmp
			sec_i = i
	for i in range(N):
		if i != fir_i:
			print(fir)
		else:
			print(sec)

def __starting_point():
	main()
__starting_point()