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

import heapq

num = int(input())
a = [int(input()) for i in range(num)]
for f in a:
  if max(a) == f:
    print(heapq.nlargest(2, a)[1])
  else:
    print(max(a))


### FIXED CODE ###

from heapq import nlargest

n = int(input())
a = [int(input()) for i in range(n)]
b = nlargest(2, a)
ma = max(a)
for j in a:
  if j == ma:
    print(b[1])
  else:
    print(ma)
### BUGGY CODE ###

from itertools import permutations
N, M, R = map(int, input().split())
r = list(map(int, input().split()))
A = []
B = []
C = []
D = [[] for _ in range(N)]
inf = float("inf")
for i in range(N):
    for j in range(N):
        if i == j:
            D[i].append(0)
        else:
            D[i].append(inf)


for _ in range(M):
    a, b, c = map(int, input().split())
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c


def WarshallFloyd(D):
    """
    >>> WarshallFloyd([[0, 1, 4], [1, 0, 1], [4, 1, 0]])
    [[0, 1, 2], [1, 0, 1], [2, 1, 0]]
    """
    for k in range(len(D)):
        for i in range(len(D)):
            for j in range(i, len(D)):
                D[i][j] = min(D[i][j], D[i][k] + D[k][j])
                D[j][i] = D[i][j]
    return D


D = WarshallFloyd(D)
Min = inf
for e in permutations(r):
    s = 0
    for i in range(R - 1):
        s += D[e[i] - 1][e[i + 1] - 1]
        if s > Min:
            break
    Min = min(s, Min)

print(Min)


### FIXED CODE ###

from itertools import permutations
import heapq
N, M, R = map(int, input().split())
r = list(map(int, input().split()))
D = [[] for _ in range(N)]
inf = float("inf")
for i in range(N):
    for j in range(N):
        if i == j:
            D[i].append(0)
        else:
            D[i].append(inf)

for _ in range(M):
    a, b, c = map(int, input().split())
    D[a - 1][b - 1] = c
    D[b - 1][a - 1] = c


def WarshallFloyd(D):
    """
    >>> WarshallFloyd([[0, 1, 4], [1, 0, 1], [4, 1, 0]])
    [[0, 1, 2], [1, 0, 1], [2, 1, 0]]
    """
    for k in range(len(D)):
        for i in range(len(D)):
            for j in range(len(D)):
                D[i][j] = min(D[i][j], D[i][k] + D[k][j])
    return D


def _dijkstra(D, s, F):
    q = []
    heapq.heappush(q, (0, s))
    while len(q) != 0:
        cost, index = heapq.heappop(q)
        if F[s][index] < cost:
            continue
        for i in range(len(D)):
            if cost + D[index][i] < F[s][i]:
                F[s][i] = cost + D[index][i]
                heapq.heappush(q, (F[s][i], i))
    return F


def Dijkstra(D):
    F = [[inf] * len(D) for _ in range(len(D))]
    for i in range(len(D)):
        F[i][i] = 0
        F = _dijkstra(D, i, F)
    return F


#W = WarshallFloyd(D)
D = Dijkstra(D)
# print(W)
# print(D)
Min = inf
for e in permutations(r):
    s = 0
    for i in range(R - 1):
        s += D[e[i] - 1][e[i + 1] - 1]
    Min = min(s, Min)

print(Min)
### BUGGY CODE ###

from scipy.misc import comb

S = input()
N = len(S)
mod = 10 ** 9 + 7


ca = [0] * N
cq1 = [0] * N
if S[0] == 'A':
    ca[0] = 1
if S[0] == '?':
    cq1[0] = 1
for i in range(1, N):
    if S[i] == 'A':
        ca[i] = ca[i - 1] + 1
    else:
        ca[i] = ca[i - 1]
    if S[i] == '?':
        cq1[i] = cq1[i - 1] + 1
    else:
        cq1[i] = cq1[i - 1]

cc = [0] * N
cq2 = [0] * N
if S[-1] == 'C':
    cc[0] = 1
if S[-1] == '?':
    cq2[0] = 1
for i in range(1, N):
    if S[N - 1 - i] == 'C':
        cc[i] = cc[i - 1] + 1
    else:
        cc[i] = cc[i - 1]
    if S[N - 1 - i] == '?':
        cq2[i] = cq2[i - 1] + 1
    else:
        cq2[i] = cq2[i - 1]
cc = cc[::-1]
cq2 = cq2[::-1]

memo = {}
def f(q):
    if q in memo:
        return memo[q]
    x = [0] * (q + 1)
    for i in range(q + 1):
        x[i] = 2 ** (q - i) * comb(q, i, exact=True)
    memo[q] = x
    return x


def g(x, q):
    t = f(q)
    c = 0
    for i in range(q + 1):
        c += t[i] * (i + x)
    return c


ans = 0
for i in range(1, N - 1):
    if S[i] in ['B', '?']:
        ans += g(ca[i - 1], cq1[i - 1]) * g(cc[i + 1], cq2[i + 1])
print(ans % mod)


### FIXED CODE ###

S = input()
N = len(S)
mod = 10 ** 9 + 7

# dp[i][j]: i番目までの文字までで、状態jになる組み合わせ数
dp = [[0 for _ in range(4)] for _ in range(N + 1)]
dp[0][0] = 1

for i in range(N):
    if S[i] in ['A', '?']:
        for j in range(4):
            dp[i + 1][j] += dp[i][j]
        dp[i + 1][1] += dp[i][0]
    if S[i] in ['B', '?']:
        for j in range(4):
            dp[i + 1][j] += dp[i][j]
        dp[i + 1][2] += dp[i][1]
    if S[i] in ['C', '?']:
        for j in range(4):
            dp[i + 1][j] += dp[i][j]
        dp[i + 1][3] += dp[i][2]

    for j in range(4):
        dp[i + 1][j] %= mod

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

n,a=int(input()),list(map(int,input().split()));c=[a.count(i+1)for i in range(3)];x=[1.0/i for i in range(1,n+1)];dp=[[[-1]*(n+2) for i in range(n+2)]for i in range(n+2)]
for k in range(n+1):
  for j in range(n-k+1):
    for i in range(n-k-j+1):
      if i+j+k>0:dp[i][j][k]=(n+dp[i-1][j][k]*i+dp[i+1][j-1][k]*j+dp[i][j+1][k-1]*k)*x[i+j+k-1]
print(dp[c[0]][c[1]][c[2]]+1)

### FIXED CODE ###

n,a=int(input()),map(int,input().split());c=[0]*3
for i in a:c[i-1]+=1
x,y,z=c;dp=[[[None]*(z+2) for i in range(n-x+2)]for i in range(n+2)];dp[0][0][0]=0
for k in range(z+1):
  for j in range(n-x-k+1):
    for i in range(n-k-j+1):
      if i+j+k:dp[i][j][k]=((dp[i-1][j][k]*i if i else 0)+(dp[i+1][j-1][k]*j if j else 0)+(dp[i][j+1][k-1]*k if k else 0)+n)/(i+j+k)
print(dp[x][y][z])
### BUGGY CODE ###

#2
s = list(input())
flag = 1
cnt = 0
while flag :
    cant = 0
    for i in range(len(s)-1):
        if s[i] == 'B' and s[i+1] == 'W':
            s[i],s[i+1] = s[i+1],s[i]
            cnt += 1
        else:
            cant += 1
    if cant == len(s)-1:
        flag = 0
print(cnt)

### FIXED CODE ###

s = input()
t = 0
ans = 0

for i in range(len(s)):
    if s[i] == 'W':
        ans += (i-t)
        t += 1
print(ans)
### BUGGY CODE ###

n,m=map(int,input().split())
ass=[tuple(map(int,input().split())) for i in range(m)]
ass.sort(key=lambda x:x[1])

acum = []

for ab in ass:
    if [x for x in acum if ab[0]<x <=ab[1]] == []:
        acum =acum + [ab[1]]

print(len(acum))


### FIXED CODE ###

n,m=map(int,input().split())
ass=[tuple(map(int,input().split())) for i in range(m)]
ass.sort(key=lambda x:x[1])

acum = []

def ifsatisfied(ab):
    for x in acum:
        if ab[0]<x <=ab[1]:
            return(True)
    return(False)

for ab in ass:
    if not ifsatisfied(ab):
        acum.append(ab[1])

print(len(acum))
### BUGGY CODE ###

N=int(input())
lst = []
while N > 0:
    lst.append(N%10)
    N //= 10
lst.reverse()

if sum(lst) %9 ==0:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

N=int(input())


result = sum(list(map(int, str(N))))
if result  %9 ==0:
    print('Yes')
else:
    print('No')
### BUGGY CODE ###

import collections
N = (int(input()))
 
ans = 0
counter_list = []
 
for _ in range(N):
    s = input()
    c = collections.Counter(s)
#    print(c)
#    print(counter_list)
    ans += counter_list.count(c)
 
    counter_list.append(c)
 
print(ans)

### FIXED CODE ###

import collections
N = (int(input()))

ans = 0
counter_list = []

for _ in range(N):
    s = input()
    counter_list.append("".join(sorted(s)))

ans = 0
wordcount = {}
for word in counter_list:
    if word in wordcount:
        wordcount[word] += 1
        ans += (wordcount[word])
    else:
        wordcount[word] = 0

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

import math
from functools import reduce

K = int(input())
ans = 0

for i in range(K):
    i = i + 1
    for k in range(K):
        k = k + 1
        for j in range(K):
            j = j + 1
            def gcd(*numbers):
                return reduce(math.gcd, numbers)
            ans += gcd(i, k, j)
            j = j + 1
        k = k + 1
    i = i + 1
            
print(ans)

### FIXED CODE ###

K = int(input())
ans = 0
from math import gcd
for i in range(1,K+1):
    for k in range(1,K+1):
        for j in range(1,K+1):
            ans += gcd(gcd(i, k),j)
            
print(ans)
### BUGGY CODE ###

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

ans_set = set()

def is_triangle(a, b, c):
    if a < b + c and b < c + a and c < a + b:
        return True
    return False

for i in range(len(l)):
    for j in range(len(l)):
        for k in range(len(l)):
            if i == j or j == k or k == i:
                continue
            if is_triangle(l[i], l[j], l[k]):
                ans_set.add(frozenset([i, j, k]))

print(len(ans_set))

### FIXED CODE ###

import bisect

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

l.sort()
ans_set = set()
ans = 0

for i in range(n):
    if i + 1 == n:
        break
    for j in range(i+1, n):
        ans += max(bisect.bisect_left(l, l[i]+l[j]) - j-1, 0)

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

n=int(input())
l=list(map(int, input().split()))
# n=5
# l=[4,2,5,1,3]
count=0

import heapq


for i in range(1,len(l)+1):
    s=l[:i]
    heapq.heapify(s) 
    if heapq.heappop(s[:i])==l[i-1]:
        count+=1
    
    
    
print(count)

### FIXED CODE ###

n=int(input())
l=list(map(int, input().split()))
# n=5
# l=[4,2,5,1,3]
count=0

import heapq

tmp=l[0]
for i in range(1,len(l)+1):
    if tmp>=l[i-1]:
        count+=1
        tmp=l[i-1]
    
    
print(count)
### BUGGY CODE ###

from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import numpy as np
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

T = S()

def match(v):
    return int(v) % 2019 == 0

cnt = 0
A = []

for s in T:
    A.append(s)


M = {}
for i in range(0, len(A)):
    x = ""
    x += A[i]
    for j in range(i+1, len(A)):
        x += A[j]
        if x in M:
            cnt += 1
            continue

        if match(x):
            M[x] = True
            cnt += 1

print(cnt)

### FIXED CODE ###

from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import numpy as np
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

T = S()

revT = reversed(T)

n, d, mods = 0, 1, [1]+[0]*2019

for i in revT:
    n = (n + int(i)*d)%2019
    d = d*10%2019
    mods[n] += 1

sum = 0
for j in mods:
    sum += j*(j-1)//2

print(sum)
### BUGGY CODE ###

N,K,Q = map(int, input().split())
seikaisha = [0 for _ in range(N)]
for _ in range(Q):
  seikaisha[int(input())-1] += 1
for _ in seikaisha:
  shitten = sum(seikaisha) - _
  if K - shitten > 0:
    print('Yes')
  else:
    print('No')


### FIXED CODE ###

N,K,Q = map(int, input().split())
seikaisha = [0 for _ in range(N)]
for _ in range(Q):
  seikaisha[int(input())-1] += 1
sum_seikai = sum(seikaisha)
for seikai in seikaisha:
  shitten = sum_seikai - seikai
  if K - shitten > 0:
    print('Yes')
  else:
    print('No')
### BUGGY CODE ###

from statistics import median

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

for i in range(0, len(x)):
    x_copy = x[:]
    x_copy.pop(i)
    print(median(x_copy))

### FIXED CODE ###

n = int(input())
x = list(map(int, input().split()))
xsort = sorted(x)
x1, x2 = xsort[n//2-1], xsort[n//2]

for i in range(n):
    if x[i] <= x1:
        print(x2)
    else:
        print(x1)
### BUGGY CODE ###

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

temp = [i for i in range(a, b+1)]

count = 0

for i in temp:
    if (i % c != 0) & (i % d != 0):
        count += 1

print(count)

### FIXED CODE ###

import fractions

def calcA(a, b):
    ans = a / b
    if ans.is_integer():
        return a // b
    else:
        return a // b + 1 


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

a1 = calcA(a,c)

a2 = b // c
a3 = a2 - a1 + 1

b1 = calcA(a,d)

b2 = b // d
b3 = b2 - b1 + 1

x = c * d // fractions.gcd(c, d)

x1 = calcA(a,x)
x2 = b // x
x3 = x2 - x1 + 1

p = b - a + 1
q = a3 + b3 - x3

print(p - q)
### BUGGY CODE ###

N = int(input())
H = list(map(int, input().split()))
F = "Yes"

for i in range(0, N):
	for k in range(i+1, N):
		if H[i] > H[k] + 1:
			F = "No"
			break

print(F)


### FIXED CODE ###

N = int(input())
H = list(map(int, input().split()))
F = "Yes"
l = H[0]

for i in range(1, N):
	if l < H[i]:
		l = H[i]
	if l - 1 > H[i]:
		F = "No"
		break

print(F)
### BUGGY CODE ###

import heapq

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

heapq.heapify(A)

for i in range(M):
    k, v = map(int, input().split())
    while v > A[0] and k > 0:
        heapq.heappushpop(A, v)
        k -= 1

print(sum(A))

### FIXED CODE ###

import heapq

N, M = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()
A.append(0)
S = []
heapq.heapify(S)
c = A[0]
cnt = 1
for i in range(1, N + 1):
    if A[i] == c:
       cnt += 1
    else:
        heapq.heappush(S, (A[i - 1], cnt))
        cnt = 1
        c = A[i]


for i in range(M):
    B, v = map(int, input().split())
    flag = False #Sからの取り出し比較を1度以上終えているか
    b = B # 残りカード
    while len(S) > 0 and S[0][0] < v:
        x = heapq.heappop(S)
        if b <= x[1]:
            heapq.heappush(S, (v, B))
            if not b == x[1] : heapq.heappush(S, (x[0], x[1] - b))
            flag = False
            break
        else:
            flag = True
            b -= x[1]

    if flag: #交換しきらないで、途中で終わっているのでこの分は挿入する
        heapq.heappush(S, (v, B - b))

sumS = 0
while len(S) > 0:
    x = heapq.heappop(S)
    sumS += x[0] * x[1]
print(sumS)
### BUGGY CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left,bisect
def main():
    h, w, m = map(int,input().split()) 
    h_lis = [0] * (h+1)
    w_lis = [0] * (w+1)
    bombs = set([tuple(map(int,input().split())) for i in range(m)])
    for hh, ww in bombs:
        h_lis[hh] += 1
        w_lis[ww] += 1
    h_max_index = [i for i, h in enumerate(h_lis) if h == max(h_lis)]
    w_max_index = [i for i, w in enumerate(w_lis) if w == max(w_lis)]
    for h_i in h_max_index:
        for w_i in w_max_index:
            if not (h_i, w_i) in bombs:
                print(h_lis[h_i] + w_lis[w_i])
                sys.exit()
    else:
        print(h_lis[h_i] + w_lis[w_i] - 1)

if __name__=='__main__':
    main()

### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left,bisect
def main():
    h, w, m = map(int,input().split()) 
    h_lis = [0] * (h+1)
    w_lis = [0] * (w+1)
    bombs = set([tuple(map(int,input().split())) for i in range(m)])
    for hh, ww in bombs:
        h_lis[hh] += 1
        w_lis[ww] += 1
    max_h = max(h_lis)
    max_w = max(w_lis)
    h_max_index = [i for i, h in enumerate(h_lis) if h == max_h]
    w_max_index = [i for i, w in enumerate(w_lis) if w == max_w]
    for h_i in h_max_index:
        for w_i in w_max_index:
            if not (h_i, w_i) in bombs:
                print(h_lis[h_i] + w_lis[w_i])
                sys.exit()
    else:
        print(h_lis[h_i] + w_lis[w_i] - 1)

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

import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
n, k = map(int, input().split())
a = list(map(int, input().split()))
rui = [1]
t = 1
for i in range(n):
    t *= a[i]
    if i < k:
        rui += [t]
    else:
        t = t // a[i - k]
        rui += [t]
        if rui[i + 1] > rui[i]:
            print('Yes')
        else:
            print('No')
#print(rui)


### FIXED CODE ###

import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(k, n):
    if a[i] > a[i - k]:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

N, M = map(int, input().split())
AB_list = [tuple(map(int, input().split())) for i in range(N)]

sorted_AB_list = sorted(AB_list, key=lambda x:x[0], reverse=True)
sorted_AB_list = sorted(sorted_AB_list, key=lambda x:x[1], reverse=True)

money = 0

# for A, B in sorted_AB_list:
#  print(A, B)

# 最終日から逆算
for i in range(M): # 残り日数
  remain_days = i+1
  
  for A, B in sorted_AB_list:
    if A <= remain_days:
      money += B
      sorted_AB_list.remove((A, B))
      break
      
print(money)
    
  

### FIXED CODE ###

from heapq import *
n, m = map(int, input().split())
jobs = [tuple(map(int, input().split())) for _ in range(n)]
jobs.sort(reverse=True)
ans = 0
q = []
for i in range(1, m+1):
    while jobs and jobs[-1][0] <= i:
        heappush(q, -jobs.pop(-1)[1])
    if q:
        ans -= heappop(q)
print(ans)
### BUGGY CODE ###

n,q = map(int,input().split())
s = input()
for i in range(q):
    l,r = map(int,input().split())
    print(s[l-1:r].count("AC"))

### FIXED CODE ###

n,q = map(int,input().split())
s = input()
count = [0]*n
for i in range(1,n):
    count[i] += count[i-1]
    if s[i-1:i+1] == "AC":count[i] += 1
for i in range(q):
    l,r = map(int,input().split())
    print(count[r-1]-count[l-1])
### BUGGY CODE ###

n=int(input())
l=[int(input()) for i in range(n)]
for j in range(n):
    kari=l[j]
    l[j]=0
    print(max(l))
    l[j]=kari

### FIXED CODE ###

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])
### BUGGY CODE ###

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

    def Eratosthenes(x: int):
        import math
        sup = int(x)
        primes = {i for i in range(2, sup+1)}
        for i in range(2, int(math.sqrt(sup+1))+1):
            if i in primes:
                mul = 2
                while i*mul <= sup:
                    primes.discard(i*mul)
                    mul += 1
        return primes

    primes = Eratosthenes(55555)
    # print(len(primes))
    from itertools import combinations
    for ps in combinations(primes, N):
        # print(ps)
        for p in combinations(ps, 5):
            if sum(p) in primes:
                break
        else:
            return print(*ps)


if __name__ == '__main__':
    main()


### FIXED CODE ###

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

    def Eratosthenes(x: int) -> set:
        """x以下の素数の集合を返す"""
        import math
        sup = int(x)
        primes = {i for i in range(2, sup+1)}
        for i in range(2, int(math.sqrt(sup+1))+1):
            if i in primes:
                mul = 2
                while i*mul <= sup:
                    primes.discard(i*mul)
                    mul += 1
        return primes

    p = Eratosthenes(55555)
    for v in p.copy():
        if v % 5 != 1:
            p.discard(v)

    from itertools import combinations
    for i, v in enumerate(combinations(p, N)):
        if i == 0:
            print(*v)
            break


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

n = int(input())
str_list = input().split()

result = 1

for i in range(n):
  result = result * int(str_list[i])
  
t = 1
for j in range(18):
  t = t * 10

if(result > t):
  print('-1')
else:
  print(result)
   


### FIXED CODE ###

n = int(input())
str_list = input().split()

list = [int(k) for k in str_list]

list.sort(reverse=True)

result = 1

if(0 in list):
  print('0')
  exit()

for i in range(n):
  result = result * list[i]
  if(result > 1000000000000000000):
    print('-1')
    exit()

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

H, W = map(int, input().split())
C = [input() for i in range(H)]

import sys
sys.setrecursionlimit(500000)

def search(x, y, z):
    if y >= H or x >= W or x < 0 or y <  0:
        return
    elif C[y][x] == z:
        return
    elif itta[y][x] != 0:
        return
    else:
        itta[y][x] = 0.0000000000000000001 # appendすると遅くなる
        prez = C[y][x]
        if C[y][x] == '.':
            itta[y][x] = 1
    
    search(x+1, y, prez)
    search(x-1, y, prez)
    search(x, y+1, prez)
    search(x, y-1, prez) 

ans = 0
# #を探しに行く
for y in range(H):
    for x in range(W):
        if C[y][x] != '#':
            pass
        else:
            itta = [[0] * W for i in range(H)]
            search(x, y, '.')
            ans += int(sum(map(sum, itta))) 
            # 入れ子リスト（２次元配列）の合計値
            # https://maku77.github.io/python/numstr/sum.html

print(ans)

### FIXED CODE ###

H, W = map(int, input().split())
C = [input() for i in range(H)]

import sys
sys.setrecursionlimit(500000)

itta = [[0] * W for i in range(H)]

def search(x, y, z):
    if y >= H or x >= W or x < 0 or y <  0:
        return
    elif C[y][x] == z:
        return
    elif itta[y][x] == 1:
        return
    elif C[y][x] == z:
        return
    else:
        itta[y][x] = 1
        
        if C[y][x] == '.':
            bw_count[0] += 1
        else:
            bw_count[1] += 1
        
        prez = C[y][x]
        
    search(x+1, y, prez)
    search(x-1, y, prez)
    search(x, y+1, prez)
    search(x, y-1, prez) 

ans = 0

for y in range(H):
    for x in range(W):
        if itta[y][x] ==1:
            pass
        else:
            bw_count = [0, 0]
            search(x, y, '-')
            
            ans += bw_count[0] * bw_count[1]  

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

n = int(input())
aa = list(map(int, input().split()))
bb = set()
cc = set()

for a in aa:
  if aa.count(a) > 1:
    bb.add(a)
  if aa.count(a) >3:
    cc.add(a)
    
if len(cc) > 0:
  y = max(cc)
else:
  y = 0

if len(bb) >= 2:
  m1 = max(bb)
  bb.remove(m1)
  m2 = max(bb)
  ans = max(m1*m2, y*y)
  
else:
  ans = max(0, y*y)
print(ans)

### FIXED CODE ###

import collections
n = int(input())
aa = list(map(int, input().split()))
ca = collections.Counter(aa)
la = sorted([a for a in ca.items()], key = lambda x:x[0], reverse=True)

list1 = []
list2 = []

for a in la:
  if a[1] > 1:
    list1.append(a)
  if a[1] >3:
    list2.append(a)
    
if len(list1) > 1:    
    xr = list1[0][0] * list1[1][0]
else:
  xr = 0
    
if len(list2) > 0:
  xs = list2[0][0] * list2[0][0]
else:
  xs = 0
  
ans = max(xr, xs)

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

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

d = {}
for i in a:
    if not i in d.keys():
        d[i] = 1
    else:
        d[i] += 1

q = int(input())

for _ in range(q):
    b, c = map(str, input().split())
    if b in d.keys():
        if c in d.keys():
            d[c] += d[b]
        else:
            d[c] = d[b]
        d.pop(b)
    ans = 0
    for k, v in d.items():
        ans += int(k) * v
    print(ans)


### FIXED CODE ###

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

d = {}
t = 0
for i in a:
    if not i in d.keys():
        d[i] = 1
    else:
        d[i] += 1
    t += int(i)

q = int(input())

for _ in range(q):
    b, c = map(str, input().split())
    diff = 0
    if b in d.keys() and c in d.keys():
        diff = (int(c) - int(b)) * d[b]
        d[c] += d[b]
        d.pop(b)
    elif b in d.keys():
        diff = int(c) * d[b] - int(b) * d[b]
        d[c] = d[b]
        d.pop(b)
    t += diff
    print(t)