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

N = int(input())

strList = list([])

for i in range(N):
  strList.append(str(input()))

strDict = {}

for item in strList:
  if item not in strDict.keys():
    strDict[item] = 1
  else:
    strDict[item] += 1

maxList = list([])

for strKeys in strDict.keys():
  if strDict[strKeys] == max(strDict.values()):
    maxList.append(strKeys)

for srtedItem in sorted(maxList):
  print(srtedItem)

### FIXED CODE ###

N = int(input())

strList = list([])

for i in range(N):
  strList.append(str(input()))

strDict = {}

for item in strList:
  if item not in strDict.keys():
    strDict[item] = 1
  else:
    strDict[item] += 1

maxList = list([])
maxNum = max(strDict.values())

for strKeys in strDict.keys():
  if strDict[strKeys] == maxNum:
    maxList.append(strKeys)

for srtedItem in sorted(maxList):
  print(srtedItem)
### BUGGY CODE ###

import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7

S = s()
N = len(S)
dp = [[0]*13 for _ in range(N+1)]
dp[0][0] = 1
for i in range(N):
    for j in range(13):
        if S[i] == '?':
            for k in range(10):
                dp[i+1][(10*j+k)%13] += dp[i][j]
        else:
            x = int(S[i])
            dp[i+1][(10*j+x)%13] += dp[i][j]
print(dp[N][5]%mod)

### FIXED CODE ###

import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7

S = s()
N = len(S)
dp = [[0]*13 for _ in range(N+1)]
dp[0][0] = 1
for i in range(N):
    for j in range(13):
        if S[i] == '?':
            for k in range(10):
                dp[i+1][(10*j+k)%13] = (dp[i+1][(10*j+k)%13]+dp[i][j])%mod
        else:
            x = int(S[i])
            dp[i+1][(10*j+x)%13] = (dp[i+1][(10*j+x)%13]+dp[i][j])%mod
print(dp[N][5])
### BUGGY CODE ###

N, W = map(int, input().split())
w_v = [list(map(int, input().split())) for i in range(N)]

w = [w_v[i][0] for i in range(N)]
v = [w_v[i][1] for i in range(N)]

#dp[i+1][j]:i番目までの品物で価値がj以上となるときの重さの総和の最小値
INF = float('inf')
dp = [[INF for j in range(max(v)*N+1)] for i in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for j in range(max(v)*N+1):
        if j >= v[i]:
            dp[i+1][j] = min(dp[i][j],dp[i][j-v[i]]+w[i])
        else:
            dp[i+1][j] = dp[i][j]

res = 0
for j in range(max(v)*N+1):
    if dp[N][j] <= W:
        res = j
print(res)

### FIXED CODE ###

N, W = map(int, input().split())
w_v = [list(map(int, input().split())) for i in range(N)]

w = [w_v[i][0] for i in range(N)]
v = [w_v[i][1] for i in range(N)]

#dp[i+1][j]:i番目までの品物で価値がj以上となるときの重さの総和の最小値
INF = float('inf')
dp = [[INF for j in range(max(v)*N+1)] for i in range(N+1)]
dp[0][0] = 0

for i in range(N):
    for j in range(max(v)*N+1):
        if j >= v[i]:
            dp[i+1][j] = min(dp[i][j],dp[i][j-v[i]]+w[i])
        else:
            dp[i+1][j] = dp[i][j]

res = 0
for j in range(max(v)*N+1):
    if dp[N][j] <= W:
        res = j
print(res)
### BUGGY CODE ###

k,s=list(map(int,input().split()))
count=0
for x in range(0,k+1):
    for y in range(0,k+1):
        for z in range(0,k+1):
            if (x+y+z)==s:
                count+=1
print(count)

### FIXED CODE ###

k,s=list(map(int,input().split()))
count=0
for x in range(0,k+1):
    for y in range(0,k+1):
        if s-(x+y)>=0 and s-(x+y)<=k:
            count+=1
print(count)
### BUGGY CODE ###

n = int(input())
l = sorted(list(map(int,input().split())))
cou =0
for i in range(0,n-2):
  for j in range(i+1,n-1):
    #iがaの長さ、jがbの長さ
    m = l[i]
    u = l[j]
    l1 = l[j+1:]
    c =[t for t in l1 if max(m-u,u-m)<t and t<m+u]
    cou += len(c)
print(cou)
    

### FIXED CODE ###

import bisect
n = int(input())
l = sorted(list(map(int,input().split())))
cou =0
for i in range(0,n-2):
  for j in range(i+1,n-1):
    #iがaの長さ、jがbの長さ
    m = l[i]
    u = l[j]
    c =bisect.bisect_left(l,m+u)
    if c>j+1:
      cou += c-j-1
print(cou)
### BUGGY CODE ###

def countAC(s, stt, end):
    count = 0
    for i in range(stt, end-1):
        if (s[i] == 'A' and s[i+1] == 'C'):
            count += 1
    return count

N, Q = map(int, input().split())
S = input()
ans = []
for q in range(Q):
    l, r = map(int, input().split())
    ans.append(countAC(S, l-1, r))

for q in range(Q):
    print(ans[q])

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()

count_list = [0] * (N+1)
count = 0
for n in range(N-1):
    if (S[n] == 'A' and S[n+1] == 'C'):
        count += 1
    count_list[n+2] = count

ans = []
for q in range(Q):
    l, r = map(int, input().split())
    ans.append(count_list[r] - count_list[l])

for q in range(Q):
    print(ans[q])
### BUGGY CODE ###

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

ans = 0
for i in range(a, b + 1):
    ans ^= i

print(ans)


### FIXED CODE ###

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


def f(x):
    return (x, 1, x + 1, 0)[x % 4]


print(f(b) ^ f(a - 1))
### BUGGY CODE ###

def main():
    from collections import Counter

    s = list(str(input()))
    num = ""
    li = []
    f = 0
    mi = 0
    li_append = li.append

    cm = {}
    for i in range(1,2*(10 ** 5)):
        cm[i] = i * (i - 1) // 2
    cm[0] = 0



    for i in range(1, len(s) + 1):
        num = s[-i] + num
        li_append(int(num) % 2019)
        if num[0] == "0":
            f += 1
        else:
            mi += cm[f+1]
            f = 0

    co = Counter(li)
    val = [i for i in co.values() if i > 1]
    ans_1 = 0

    for x in val:
        ans_1 += cm[x]
    ans2 = 0
    if 0 in co:
        ans2 = co[0]
    print(ans_1 + ans2 - mi)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    import sys
    from collections import defaultdict
    dic = defaultdict(int)

    input = sys.stdin.readline

    s = str(input().rstrip())
    m = 0
    before_m = 0.1

    for i in range(1, len(s) + 1):
        before_m = int(before_m*10)%2019
        m_ad = (int(s[-i]) * before_m) % 2019
        m = (m_ad + m) % 2019
        dic[m] += 1

    ans1 = 0

    for x in dic.values():
        if x > 1:
            ans1 += x * (x - 1) // 2
    ans2 = dic[0]

    print(ans1 + ans2)

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

n, q = map(int, input().split())
c = list(map(lambda x: int(x) - 1, input().split()))
query = []
for i in range(q):
    l, r = map(lambda x: int(x) - 1, input().split())
    query.append((l, r, i))

query.sort()
ball = [list() for i in range(n)]
for i, x in enumerate(c):
    ball[x].append(i)

# binary indexed tree
bit = [0] * (n + 1)
def add(i, x):
    i += 1
    while i <= n:
        bit[i] += x
        i += i & -i

def get(i):
    i += 1
    s = 0
    while i:
        s += bit[i]
        i -= i & -i
    return s

for i in ball:
    if i:
        add(i[0], 1)
        i.reverse()

at = 0
ans = [0] * q
for l, r, i in query:
    while at < l:
        b = ball[c[at]]
        add(b[-1], -1)
        b.pop()
        if b:
            add(b[-1], 1)
        at += 1
    ans[i] = get(r) - get(l - 1)

for i in ans:
    print(i)

### FIXED CODE ###

import sys
input = sys.stdin.readline
n, q = map(int, input().split())
c = list(map(lambda x: int(x) - 1, input().split()))
query = []
for i in range(q):
    l, r = map(lambda x: int(x) - 1, input().split())
    query.append(l << 40 | r << 20 | i)

query.sort()
ball = [list() for i in range(n)]
for i, x in enumerate(c):
    ball[x].append(i)

# binary indexed tree
bit = [0] * (n + 1)
def add(i, x):
    i += 1
    while i <= n:
        bit[i] += x
        i += i & -i

def get(i):
    i += 1
    s = 0
    while i:
        s += bit[i]
        i -= i & -i
    return s

for i in ball:
    if i:
        add(i[0], 1)
        i.reverse()

at = 0
ans = [0] * q
for x in query:
    l = x >> 40
    r = x >> 20 & 0xfffff
    i = x & 0xfffff
    while at < l:
        b = ball[c[at]]
        add(b[-1], -1)
        b.pop()
        if b:
            add(b[-1], 1)
        at += 1
    ans[i] = get(r) - get(l - 1)

for i in ans:
    print(i)
### BUGGY CODE ###

N, K = map(int, input().split())
P = list(map(int, input().split()))
P = [x - 1 for x in P]
C = list(map(int, input().split()))
ans = -(10 ** 18)
for i in range(N):
    j = i
    S = 0
    L = 0
    for _ in range(K):
        j = P[j]
        S += C[j]
        L += 1
        ans = max(ans, S)
        if j == i:
            break
    if j == i:
        M = max(0, K // L - 1)
        T = S * M
        for _ in range(M * L, K):
            j = P[j]
            T += C[j]
            ans = max(ans, T)
print(ans)

### FIXED CODE ###

def main():
    N, K = map(int, input().split())
    P = list(map(int, input().split()))
    P = [x - 1 for x in P]
    C = list(map(int, input().split()))
    ans = -(10 ** 18)
    for i in range(N):
        j = i
        S = 0
        L = 0
        for _ in range(K):
            j = P[j]
            S += C[j]
            L += 1
            ans = max(ans, S)
            if j == i:
                break
        if j == i:
            M = max(0, K // L - 1)
            T = S * M
            for _ in range(M * L, K):
                j = P[j]
                T += C[j]
                ans = max(ans, T)
    print(ans)


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

import sys
input = sys.stdin.readline

from itertools import combinations_with_replacement
from collections import Counter

n = int(input())
a = list(map(int, input().split())) #1-index

ans_l = []
for k in range(n):
    ans = 0
    temp = a[k]
    a.remove(a[k]) #k番目を取り除いた
    c_v = list(Counter(a).values())
    for i in c_v:
        if i > 1:
            ans += len((list(combinations_with_replacement(range(1,i), 2))))
    a.insert(k,temp)
    ans_l.append(ans)

for i in ans_l:
    print(i)


### FIXED CODE ###

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
s=Counter(a)
sums=0
for k in s.keys():
    sums+=(s[k]-1)*s[k]//2

for i in a:
    print(sums-s[i]+1)
### BUGGY CODE ###

import bisect


def memoize(f):
    cache = {}

    def func(x):
        if x not in cache:
            cache[x] = f(x)
        return cache[x]

    return func


@memoize
def get_bisect(y):
    return bisect.bisect(vy, (y, float('-inf')))


n = int(input())
vx, vy = [], []
for _ in range(n):
    x1, y1, x2, y2 = map(int, input().split())
    if x1 == x2:
        if y1 > y2:
            y1, y2 = y2, y1
        vx.append((x1, y1, y2))
    else:
        if x1 > x2:
            x1, x2 = x2, x1
        vy.append((y1, x1, x2))
vy.sort()

ans = 0

for x, y1, y2 in vx:
    it1, it2 = get_bisect(y1), get_bisect(y2 + 1)
    for y, x1, x2 in vy[it1:it2]:
        if x1 <= x <= x2:
            ans += 1

print(ans)

### FIXED CODE ###

import bisect


class BIT:
    def __init__(self, n):
        self.tree = [0] * (n + 1)
        self.n = n

    def add(self, i, v):
        while i <= self.n:
            self.tree[i] += v
            i += i & -i

    def _sum(self, i):
        ret = 0
        while i:
            ret += self.tree[i]
            i -= i & -i
        return ret

    def sum(self, l, h):
        return self._sum(h) - self._sum(l - 1)


n = int(input())
vx = []
xs = set()
for _ in range(n):
    x1, y1, x2, y2 = map(int, input().split())
    if x1 == x2:
        if y1 > y2:
            y1, y2 = y2, y1
        vx.append((y1, float('-inf'), x1))
        vx.append((y2, float('inf'), x1))
        xs.add(x1)
    else:
        if x1 > x2:
            x1, x2 = x2, x1
        vx.append((y1, x1, x2))
vx.sort()

bit = BIT(len(xs))
xs = [float('-inf')] + sorted(xs)
ix = {v: i for i, v in enumerate(xs)}
ans = 0

for y, j, x2 in vx:
    if j == float('-inf'):
        bit.add(ix[x2], 1)
    elif j == float('inf'):
        bit.add(ix[x2], -1)
    else:
        i1 = bisect.bisect_left(xs, j)
        i2 = bisect.bisect(xs, x2) - 1
        ans += bit.sum(i1, i2)

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

while 1:
  h,w = map(int, raw_input().split())
  if h==w==0: break
  H = range(h)
  A = [0]*h
  for i in H:
    x = raw_input().replace(".", "1").replace("*", "0")
    A[i] = map(int, list(x))
  B = A[:]
  C = [0]*h
  for j in H[1:]:
    M = ["1","11","111"]
    for i in H[j:]:
      x = [e1 * e2 for e1, e2 in zip(B[i-j], A[i])]
      B[i-j] = x[:]
      x=sorted("".join(map(str, x)).split("0"))
#      M.append(x[-1])
    C[j] = len(sorted(M)[-1])*(j+1)
  print max(C)

### FIXED CODE ###

while 1:
  h,w = map(int, raw_input().split())
  if h==w==0: break
  M = [0]*(h+1)
  a=[0]*w
  for i in range(h):
    x = raw_input()
    b=[0]*w
    sp=-1
    C = [-1]*(h+1)
    for j in range(w):
      if x[j]=="*": c=0
      else: c=a[j]+1
      b[j]=c
      dw=c-sp
      if dw>0: C[sp+1:c+1]=[j]*dw
      elif dw<0:
        for k in range(c+1,sp+1): M[k]=max(M[k],j-C[k])
        C[c+1:sp+1]=[-1]*(-dw)
      sp=c
    for k in range(1,sp+1):
      if C[k]>=0: M[k]=max(M[k],w-C[k])
    a=b
  s=max([a*b for a,b in zip(M, range(h+1))])
  print s
### BUGGY CODE ###

import sys
from collections import deque

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    p = nl()
    diag = [-1, 1, n, -n]
    st = [1]*(n+2)**2
    gr = [0]*(n+2)**2
    ans = 0
    for i in range(n):
        for j in range(n):
            gr[i*n + j + n + 1] = min(min(i, j), n-1-max(i, j))
    for v in p:
        v += n
        ans += gr[v]
        st[v] = 0
        c = gr[v]
        for dv in diag:
            nv = v + dv
            c = min(c, gr[nv] + st[nv])
        gr[v]= c
        q = deque()
        q.append(v)
        while q:
            v = q.popleft()
            for dv in diag:
                nv = v + dv
                if gr[nv] > gr[v] + st[v]:
                    gr[nv] = gr[v] + st[v]
                    if st[v]:
                        q.append(nv)
                    else:
                        q.appendleft(nv)
    print(ans)
    return

if __name__ == '__main__':
    solve()


### FIXED CODE ###

import sys
from collections import deque

readline = sys.stdin.readline
ni = lambda: int(readline().rstrip())
nl = lambda: list(map(int, readline().split()))

def solve():
    n = ni()
    p = nl()
    diag = [-1, 1, n, -n]
    st = [1]*(n+2)**2
    gr = [0]*(n+2)**2
    ans = 0
    for i in range(n):
        for j in range(n):
            gr[i*n + j + n + 1] = min(min(i, j), n-1-max(i, j))
    for v in p:
        v += n
        ans += gr[v]
        st[v] = 0
        c = gr[v]
        for dv in diag:
            nv = v + dv
            c = min(c, gr[nv] + st[nv])
        gr[v]= c
        q = deque()
        q.append(v)
        while q:
            v = q.popleft()
            for dv in diag:
                nv = v + dv
                if gr[nv] > gr[v] + st[v]:
                    gr[nv] = gr[v] + st[v]
                    if st[v]:
                        q.append(nv)
                    else:
                        q.appendleft(nv)
    print(ans)
    return

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

X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
P = []
for i in range(X):
    for j in range(Y):
        for k in range(Z):
            P.append(A[i]+B[j]+C[k])

P.sort(reverse = True)
for i in range(K):
    print(P[i])  

### FIXED CODE ###

from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    X,Y,Z,K = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort(reverse = True)
    A = A[:min(K,X)]
    B = list(map(int,input().split()))
    B.sort(reverse = True)
    B = B[:min(K,X)]
    C = list(map(int,input().split()))
    C.sort(reverse = True)
    C = C[:min(K,X)]
    Pre = []
    for i in range(min(X,K)):
        for j in range(min(Y,K)):
            Pre.append(A[i]+B[j])
    Pre.sort(reverse = True)
    Pre = Pre[:K]
    ans = []
    for i in range(min(Z,K)):
        for j in Pre:
            ans.append(j+C[i])
    ans.sort(reverse = True)
    for i in range(K):
        print(ans[i])



if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)
### BUGGY CODE ###

a, b, k = [int(x) for x in input().split()]
while k > 0:
  if a > 0:
    a -= 1
  elif b > 0:
    b -= 1
  k -= 1
print(a, b)

### FIXED CODE ###

a, b, k = [int(x) for x in input().split()]
if a > k:
  a-=k
else:
  k-=a
  a = 0
  if b > k:
    b-=k
  else:
    b=0
print(a, b)
### BUGGY CODE ###

N = int(input())
a = [int(s) for s in input().split()]
b = a[0]
c = str("")
for i in range(1,N):
  b = b ^ a[i]
c = str(b ^ a[0])
for i in range(1, N):
  c = c + " " + str(b ^ a[i])
print(c)

### FIXED CODE ###

N = int(input())
a = [int(s) for s in input().split()]
b = a[0]
c = []
for i in range(1,N):
  b = b ^ a[i]

for i in range(N):
  c.append(str(b ^ a[i]))
 
d = ' '.join(c)

print(d)
### BUGGY CODE ###

n = int(input())
out=0
for i in range(n):
    out=out+i

print(out)

### FIXED CODE ###

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

import sys
sys.setrecursionlimit(10**6)
n,A,B,C=map(int,input().split())
s=[input() for i in range(n)]
def dfs(d,a,b,c,now):
    global n,s
    if d==n:
        print("Yes")
        for i in range(n):
            print(now[i])
        sys.exit()
    else:
        if s[d][0]=="A" and s[d][1]=="B":
            if a>b:
                dfs(d+1,a-1,b+1,c,now+"B")
            elif a<b:
                dfs(d+1,a+1,b-1,c,now+"A")
            else:
                if a>0:
                    dfs(d+1,a-1,b+1,c,now+"B")
                if b>0:
                    dfs(d+1,a+1,b-1,c,now+"A")
        elif s[d][0]=="A" and s[d][1]=="C":
            if a>c:
                dfs(d+1,a-1,b,c+1,now+"C")
            elif a<c:
                dfs(d+1,a+1,b,c-1,now+"A")
            else:
                if a>0:
                    dfs(d+1,a-1,b,c+1,now+"C")
                if c>0:
                    dfs(d+1,a+1,b,c-1,now+"A")
        else:
            if b>c:
                dfs(d+1,a,b-1,c+1,now+"C")
            elif b<c:
                dfs(d+1,a,b+1,c-1,now+"B")
            else:
                if b>0:
                    dfs(d+1,a,b-1,c+1,now+"C")
                if c>0:
                    dfs(d+1,a,b+1,c-1,now+"B")

dfs(0,A,B,C,"")
print("No")

### FIXED CODE ###

n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if a==1 and b==1 and i!=n-1:
            if s_[i+1][0]=="A":
                a,b,ans=(a+1,b-1,ans+"A")
            else:
                a,b,ans=(a-1,b+1,ans+"B")
            continue
        a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if a==1 and c==1 and i!=n-1:
            if s_[i+1][1]=="C":
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a+1,c-1,ans+"A")
            continue
        a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if c==1 and b==1 and i!=n-1:
            if s_[i+1][1]=="C":
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b+1,c-1,ans+"B")
            continue
        b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])
### BUGGY CODE ###

N=int(input())
A=list(map(int, input().split()))
Q=int(input())
for i in range(Q):
    B,C=map(int,input().split())
    for j in range(N):
        if A[j]==B:
            A[j]=C
    print(sum(A))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
S = sum(A)
L = [0] * 100001
for i in A:
    L[i] += 1
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    S += (C - B) * L[B]
    L[C] += L[B]
    L[B] = 0
    print(S)