You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.
You will now successively do the following Q operations:
 - In the i-th operation, you replace every element whose value is B_{i} with C_{i}.
For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

-----Constraints-----
 - All values in input are integers.
 -  1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5} 
 -  B_{i} \neq C_{i} 

-----Input-----
Input is given from Standard Input in the following format:
N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

-----Output-----
Print Q integers S_{i} to Standard Output in the following format:
S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

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

-----Sample Output-----
11
12
16

Initially, the sequence A is 1,2,3,4.
After each operation, it becomes the following:
 - 2, 2, 3, 4
 - 2, 2, 4, 4
 - 4, 4, 4, 4

Difficulty: interview

Starter code:

Solutions:
from collections import Counter 

N = int(input())
A = list(map(int,input().split()))
Q = int(input())

tot = sum(A)
A = Counter(A)

for i in range(Q):
    B,C = map(int,input().split())
    tot +=(C - B) * A[B]
    A[C] += A[B]
    A[B] = 0
    print(tot)

import collections
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
Q=int(input())
B=[0]*Q;C=[0]*Q
for i in range(Q):
  B[i],C[i]=map(int,input().split())

D = collections.Counter(A)
s=sum(A)
for i in range(Q):
  s=s-B[i]*D[B[i]]
  D[C[i]]+=D[B[i]]
  s=s+C[i]*D[B[i]]
  D[B[i]]=0
#  A = [C[i] if B[i]==A[j] else A[j] for j in range(N)]
#  print(A)
  print(s)

n = int(input())
a = list(map(int, input().split()))
al = [0] * (10**5 + 1)
s = 0
for i in a:
    al[i] += 1
    s += i
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * al[b]
    al[c] += al[b]
    al[b] = 0
    print(s)

import sys,collections
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
Ac = collections.defaultdict(int)
ans = 0
for a in A:
    Ac[a]+=1
    ans+=a

Q = int(input())

for _ in range(Q):
    B,C = list(map(int,input().split()))
    numB = Ac[B]
    ans += C*numB - B*numB
    print(ans)
    Ac[C]+=numB
    Ac[B]=0



N = int(input())
As = list(map(int,input().split()))
Q = int(input())

count_array = [0]*(10**5+1)
for i in range(N):
    count_array[As[i]] += 1

sum = 0
for i in range(len(As)):
    sum += As[i]

for i in range(Q):
    x,y = list(map(int,input().split()))
    sum += count_array[x]*(y-x)
    print(sum)
    count_array[y] += count_array[x]
    count_array[x] = 0


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
res = sum(a)
cnt = Counter(a)
for _ in range(q):
    b, c = map(int, input().split())
    res = res + cnt[b]*(c - b)
    print(res)
    cnt[c] += cnt[b]
    cnt[b] = 0

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

num = [0] * (10 ** 5 + 1)
for ai in A:
    num[ai] += 1

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

return

N = int(input())
A = list(map(int, input().strip().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q
from collections import Counter
K=Counter(A)
ans = 0

for i in range(Q):
    B[i],C[i] = list(map(int,input().split()))


for i in K:
    ans += i * K[i]

for i in range(Q):
    ans -= B[i] * K[B[i]]
    ans += C[i] * K[B[i]]
    K[C[i]] += K[B[i]]
    K[B[i]] = 0
    print(ans)

    
    


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
cnt = Counter(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    s += (c - b)*cnt[b]
    cnt[b], cnt[c] = 0, cnt[c] + cnt[b]
    print(s)

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

cnt_dict = {}

s = sum(A)
ans = []

for item in A:
    if item not in cnt_dict:
        cnt_dict[item] = 0
    cnt_dict[item] += 1

for _ in range(Q):
    b, c = map(int, input().split())

    if b not in cnt_dict:
        cnt_dict[b] = 0
    if c not in cnt_dict:
        cnt_dict[c] = 0

    s -= b * cnt_dict[b]
    s += c * cnt_dict[b]

    cnt_dict[c] += cnt_dict[b]
    cnt_dict[b] = 0

    ans.append(s)

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

import collections
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
Q=int(input())
B=[0]*Q;C=[0]*Q
for i in range(Q):
  B[i],C[i]=map(int,input().split())

D = collections.Counter(A)
s=sum(A)
for i in range(Q):
  s=s-B[i]*D[B[i]]
  D[C[i]]+=D[B[i]]
  s=s+C[i]*D[B[i]]
  D[B[i]]=0
  print(s)

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    S = sum(A)
    cunt_A = Counter(A)
    for q in range(Q):
        b, c = map(int,input().split())
        S += (c-b)*cunt_A[b]
        cunt_A[c] += cunt_A[b]
        cunt_A[b] = 0
        print(S)

def __starting_point():
    main()
__starting_point()

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


from collections import Counter

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

sum_a = sum(A)

d = Counter(A)

for _ in range(int(input())):
    b, c = map(int, input().split())
    temp = d[b]         
    d[b] -= temp
    d[c] = d.get(c, 0) + temp
    sum_a += (c - b) * temp
    print(sum_a)

N = int(input())
A = list(map(int, input().split()))
num = [0]*(10**5)
ans = 0
for i in A:
    num[i-1] += 1
    ans += i
Q = int(input())
for q in range(Q):
    B, C = map(int, input().split())
    ans += (C-B)*num[B-1]
    num[C-1] += num[B-1]
    num[B-1] = 0
    print(ans)

from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b in list(t.keys()):
            s += (c - b) * t[b]
            t[b], t[c] = 0, t[b] + t[c]
        ans.append(s)
    return "\n".join(map(str, ans))


def __starting_point():
    import sys
    print((solve(sys.stdin.read().strip())))

__starting_point()

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

tmp = 0
l = [0 for i in range(10**5 +1)]
for i in a:
    tmp += i
    l[i] += 1

for i in range(q):
    b,c = map(int,input().split())
    tmp += c*l[b] - b*l[b]
    print(tmp)
    l[c] += l[b]
    l[b] = 0

N=int(input())
*A,=map(int,input().split())
Q=int(input())
BC=[list(map(int,input().split())) for _ in range(Q)]

import collections
c = collections.Counter(A)
s=sum(A)

for i,j in BC:
    s=s-i*c[i]+j*c[i]
    print(s)
    c[j]+=c[i]
    c[i]=0

import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp

INF = inf
MOD = 1000000007

n = int(input())
A = [int(i) for i in input().split()]
q = int(input())
B = [[int(i) for i in input().split()]for j in range(q)]    # nは行数

tmp = 0
res = [sum(A)]

tmp = Counter(A)
for i in range(q):
    if B[i][0] in list(tmp.keys()):
        res.append((B[i][1] - B[i][0]) * tmp[B[i][0]])
        if B[i][1] in list(tmp.keys()):
            tmp[B[i][1]] += tmp[B[i][0]]
        else:
            tmp[B[i][1]] = tmp[B[i][0]]
        tmp[B[i][0]] = 0
    else:
        res.append(0)

res = "\n".join([str(r) for r in list(accumulate(res))[1:]])

print(res)


import collections

def main():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())

    a_dict = collections.Counter(a)
    a_dict = {key: key*value for key, value in list(a_dict.items())}
    answer = sum(a_dict.values())
    for _ in range(q):
        b, c = list(map(int, input().split()))
        diff = 0
        if b in a_dict:
            b_sum = a_dict.pop(b)
            c_add = b_sum//b * c
            diff = c_add - b_sum
            if c in a_dict:
                a_dict[c] += c_add
            else:
                a_dict[c] = c_add
        answer += diff
        print(answer)


def __starting_point():
    main()

__starting_point()

from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

d = defaultdict(int)
ans = 0
for a in A:
    ans += a
    d[a] += 1

for _ in range(Q):
    b, c = map(int, input().split())
    n = d[b]
    ans -= n * b
    ans += n * c
    print(ans)

    d[b] = 0
    d[c] += n

n =int(input())
a={i:0 for i in range(10**5+1)}
s=0
for i in map(int,input().split()):
    s+=i
    a[i]+=1
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    s-=b*a[b]-c*a[b]
    a[c]+=a[b]
    a[b]=0
    print(s)

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

cnt = [0 for i in range(10**5)]
a = sorted(a)
s = sum(a)

for i in range(n):
    cnt[a[i]-1] += 1

q = int(input().strip())

for i in range(q):
    b, c = list(map(int, input().split()))
    s += c * cnt[b - 1] - b * cnt[b - 1]
    cnt[c - 1] += cnt[b - 1]
    cnt[b - 1] = 0
    print(s)


f=lambda : map(int,input().split())
N=input()
*A,=f()
import collections
c = collections.Counter(A)
s=sum(A)
BC=[f() for _ in range(int(input()))]

for i,j in BC:
 v=c[i]
 s+=v*(j-i)
 print(s)
 c[j]+=v
 c[i]=0

N = int(input())
counter = {}
total = 0
for x in input().split():
    x = int(x)
    total += x
    if not x in counter:
        counter[x] = 1
    else:
        counter[x] += 1
Q = int(input())
for _ in range(Q):
    B, C = list(map(int, input().split()))
    if not B in counter:
        print(total)
        continue
    num = counter[B]
    if C in counter:
        counter[C] += num
    else:
        counter[C] = num
    total = total - num * B + num * C
    print(total)
    del counter[B]


n=int(input())
a=list(map(int,input().split()))
check=[0]*100100
for x in a:
    check[x]+=1
q=int(input())
ans=sum(a)
for i in range(q):
    b,c=list(map(int,input().split()))
    ans+=(c-b)*check[b]
    check[c]+=check[b]
    check[b]=0
    print(ans)


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

import collections

N = int(input())
A = list(map(int,input().split()))
Ac = collections.Counter(A)
ans = sum(A)

Q = int(input())

for _ in range(Q):
    B,C = map(int,input().split())
    numB = Ac[B]
    ans += (C-B)*numB
    print(ans)
    Ac[C]+=numB
    Ac[B]=0

N=int(input())
A=[int(x) for x in input().split()]
youso=[0]*(10**5+1)
for j in range(N):
    youso[A[j]]+=1
ans=sum(A)
Q=int(input())
for i in range(Q):
    B,C=list(map(int,input().split()))
    numofb=youso[B]
    youso[B]=0
    ans+=(C-B)*numofb
    print(ans)
    youso[C]+=numofb


n=int(input())

a_raw=list(map(int,input().split()))
a=[0]*100001
for a_i in a_raw:
    a[a_i]+=1
q=int(input())
sum_tmp=sum(a_raw)

for _ in range(q):
    b,c=list(map(int,input().split()))
    sum_tmp = sum_tmp + c*a[b]-b*a[b]
    a[c]+=a[b]
    a[b]=0
    print(sum_tmp)


import collections

N = int(input())
A = list(map(int,input().split()))
Ac = collections.defaultdict(int)
ans = 0
for a in A:
    Ac[a]+=1
    ans+=a

Q = int(input())

for _ in range(Q):
    B,C = map(int,input().split())
    numB = Ac[B]
    ans += C*numB - B*numB
    print(ans)
    Ac[C]+=numB
    Ac[B]=0

from collections import Counter
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
counter = Counter(A)
res = sum(A)
for i in range(Q):
    b, c = list(map(int,input().split()))
    res -= counter[b] * b
    res += counter[b] * c
    counter[c] += counter[b]
    counter[b] = 0
    print(res)


import collections

N = int(input())
A = list(map(int,input().split()))
Ac = collections.defaultdict(int)
ans = 0
for a in A:
    Ac[a]+=1
    ans+=a

Q = int(input())

for _ in range(Q):
    B,C = map(int,input().split())
    numB = Ac[B]
    ans += (C-B)*numB
    print(ans)
    Ac[C]+=numB
    Ac[B]=0

from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b not in list(t.keys()):
            ans.append(s)
            continue
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


def __starting_point():
    import sys
    print((solve(sys.stdin.read().strip())))

__starting_point()

from collections import Counter

n = int(input())
A = list(map(int, input().split()))
C =  Counter(A)
q = int(input())
BC = [list(map(int, input().split())) for _ in range(q)]

A_sum = sum(A)
for b, c in BC:
  tmp = C[b]
  C[c] += tmp
  C[b] = 0
  A_sum += tmp * (c-b)
  print(A_sum)


from collections import Counter
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
ans = sum(A)
num = Counter(A)
for i in range(Q):
    b,c = map(int,input().split())

    ans += (c-b)*num[b]
    num[c] += num[b] 
    num[b] = 0
    
    print(ans)

n = int(input())
al = list(map(int, input().split()))
q = int(input())

sum_n = sum(al)
num = [0]*(10**5+1)
for a in al:
    num[a] += 1

for _ in range(q):
    bi, ci = list(map(int, input().split()))
    sum_n += (ci-bi)*num[bi]
    print(sum_n)
    num[ci] += num[bi]
    num[bi] = 0


def __starting_point():

    n = int(input())
    B = [0]*(10**5+1)
    A = list(map(int,input().split()))
    for a in A:
        B[a] += 1
    #合計
    sm = sum(A)
    m = int(input())

    for j in range(m):
        b,c = list(map(int,input().split()))
        #bの個数の増減をチェック
        cnt_b = B[b]
        cnt_c = B[c]
        if cnt_b == 0:
            #変換できないので何もしない
            print(sm)
        else:
            B[b] = 0
            B[c] += cnt_b
            sm = sm + c*cnt_b - b*cnt_b
            print(sm)

__starting_point()

from collections import Counter
n = int(input())
A = list(map(int, input().split()))
C = Counter(A)
q = int(input())
ans = sum(A)
for _ in range(q):
    b, c = list(map(int, input().split()))
    if C.get(b):
        x = C.pop(b)
        ans += - b * x + c * x
        if C.get(c):
            C[c] += x
        else:
            C[c] = x
    print(ans)


import collections
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = [[int(i) for i in input().split()] for _ in range(q)]

total = sum(a)
s = collections.Counter(a)


for x in range(q):
    if bc[x][0] in s and bc[x][1] in s:
        s[bc[x][1]] += s[bc[x][0]]
        total += (bc[x][1]-bc[x][0]) * s[bc[x][0]]
        del s[bc[x][0]]
    elif bc[x][0] in s and bc[x][1] not in s:
        s[bc[x][1]] = s[bc[x][0]]
        total += (bc[x][1]-bc[x][0]) * s[bc[x][0]]
        del s[bc[x][0]]
    print(total)

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = list(list(map(int,input().split())) for _ in range(Q))

total = sum(A)

li = [0] * 100000
for i in range(N):
  li[A[i] - 1] += 1

for i in range(Q):
  j = BC[i][0]
  k = BC[i][1]
  if li[j - 1] > 0:
    li[k - 1] += li[j - 1]
    total += li[j - 1] * (k - j)
    li[j - 1] = 0
  print(total)

from collections import Counter

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

counter = Counter(a)
sum_res = sum(a)  # はじめの合計、名前がsumだとsum関数とかぶってよくないので

# q回操作します。ループ変数は使わないので_とします（Pythonの慣習）
for _ in range(q):
    before, after = map(int, input().split())

    # 合計を変更します
    sum_res -= before * counter[before]
    sum_res += after * counter[before]

    # 個数を変更します
    counter[after] += counter[before]
    counter[before] = 0

    print(sum_res)

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

d = {}
v = 0
ans = []

for a in A:
    d[a] = d.get(a, 0) + 1
    v += a

for i in range(Q):
    B, C = list(map(int, input().split()))
    v += d.get(B, 0) * (C - B)
    d[C] = d.get(C, 0) + d.get(B, 0)
    d[B] = 0
    ans.append(v)

for a in ans:
    print(a)




from collections import Counter
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
cnt = Counter(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    cnt_b = cnt[b]
    cnt[b], cnt[c] = 0, cnt[c] + cnt_b
    s += (c - b)*cnt_b
    print(s)

N = int(input())
A = input().split()
num_list = [0] * 10**5
ans = 0

for i in range(N):
    num_list[int(A[i])-1] += 1

for i in range(10**5):
    ans += (i+1) * num_list[i]

Q = int(input())

for i in range(Q):
    B, C = map(int, input().split())

    ans = ans + C * num_list[B-1] - B * num_list[B-1]
    num_list[C-1] += num_list[B-1]
    num_list[B-1] = 0

    print(ans)

N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())

d = [0]*(10**5)

for i in A:
    d[i-1] += 1

ans = sum(A)

for i in range(Q):
    b, c = map(int,input().split())
    ans += (c-b)*d[b-1]
    d[c-1] += d[b-1]
    d[b-1] = 0
    print(ans)

I=lambda:list(map(int,input().split()))
I()
dic={}
s=0
for a in I():
  s+=a
  dic[a]=dic.get(a,0)+1
for _ in [0]*I()[0]:
  b,c=I()
  s+=(c-b)*dic.get(b,0)
  dic[c]=dic.get(c,0)+dic.get(b,0)
  dic[b]=0
  print(s)

def Replacing():
    N = int(input())
    A = list(map(int,input().split()))
    lis = [0 for i in range(10 ** 5)]
    res = 0
    for i in A:
        lis[i-1] += 1
        res += i
    
    Q = int(input())
    for i in range(Q):
        b, c = list(map(int,input().split()))
        num = lis[b-1]
        res += (c - b) * num
        lis[b-1] = 0
        lis[c-1] += num
        print(res)
        
Replacing()


n=int(input())
a=list(map(int,input().split()))
f=[0]*(int(1e5)+1)
sum=0
for i in range(n):
    f[a[i]]+=1
    sum+=a[i]
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    sum+=(c-b)*f[b]
    f[c]+=f[b]
    f[b]=0
    print(sum)

from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())

ans = sum(a)
count = Counter(a)
for _ in range(q):
  b,c = map(int, input().split())
  count[c] += count[b]
  ans -= b*count[b]
  ans += c*count[b]
  count[b] = 0
  print(ans)

f=lambda : map(int,input().split())
N=int(input())
*A,=f()
Q=int(input())
BC=[f() for _ in range(Q)]

import collections
c = collections.Counter(A)
s=sum(A)

for i,j in BC:
 v=c[i]
 s+=v*(j-i)
 print(s)
 c[j]+=v
 c[i]=0

import numpy as np
n = int(input())
li_a = list(map(int, input().split()))
A = [0]*(10**5)
s = sum(li_a)

li_a = list(map(lambda x: x-1, li_a))
for a in li_a:
    A[a] += 1
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    if A[b-1]>0:
        A[c-1] += A[b-1]
        s -= b * A[b-1]
        s += c * A[b-1]
        A[b-1] = 0
        print(s)
    else:
        print(s)

N = int(input())
natural = list(map(int,input().split()))
Q = int(input())
number = [0]*(10**5+1)
for i in range(N):
    number[natural[i]] += 1
Nsum = sum(natural)
ans = []
for i in range(Q):
    B,C = map(int,input().split())
    count = number[B]
    diff = C-B
    number[C] += count
    number[B] = 0
    Nsum += diff * count
    ans.append(Nsum)

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

from collections import Counter
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = [list(map(int,input().split())) for _ in range(q)]
count = dict(Counter(a))
ans = sum(a)
for i in bc:
    try:
        ans += count[i[0]] * (i[1] - i[0])
        try:
            count[i[1]] += count[i[0]]
        except:
            count[i[1]] = count[i[0]]
        count[i[0]] = 0
        print(ans)
    except KeyError:
        print(ans)
        continue

from collections import Counter

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int, input().split())) for _ in range(q)]

cou = Counter(a)
s = sum(a)

for b, c in bc:
    s = s + cou[b]*(c-b)
    cou[c] += cou[b]
    cou[b] = 0
    print(s)

n = int(input())
al = list(map(int, input().split()))
al_sum = sum(al)
q = int(input())

num = [0]*100001
for a in al:
    num[a] += 1


for _ in range(q):
    bi, ci = map(int, input().split())

    al_sum += num[bi]*(ci - bi)
    num[ci] += num[bi]
    num[bi] = 0

    print(al_sum)

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

ans = sum(A)
num = [0] * (100001)

for a in A:
    num[a] += 1

Q = int(input())

for _ in range(Q):
    B, C = map(int, input().split())
    ans += C * num[B]
    ans -= B * num[B]
    num[C] += num[B]
    num[B] = 0
    print(ans)

# import numpy as np
# import math

# from scipy.special import perm : perm(n, r, exact=True)
# from scipy.special import comb : comb(n, r, exact=True)

# import itertools
# for v in itertools.combinations(L, n):M.append(list(v))

# from numba import njit
# @njit('f8(i8,i8,i8,i8,f8[:,:,:])', cache=True)


""" Definitions  """

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

MOD = 10**9+7

# ============================================================

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

L = [0]*(10**5+1)

for i in range(n):
    L[A[i]] += 1
s = sum(A)

for i in range(q):
    a, b = list(map(int, input().split()))
    s += (b-a)*L[a]
    L[b] += L[a]
    L[a] = 0
    print(s)
    


n = int(input())
sum = 0
a_list = list(map(int, input().split()))
num = [0] * (10 ** 5 + 1)
for i in range(n):
    num[a_list[i]] += 1
    sum += a_list[i]

q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    sum += (c - b) * num[b]
    num[c] += num[b]
    num[b] = 0
    print(sum)

from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


def __starting_point():
    import sys
    print((solve(sys.stdin.read().strip())))

__starting_point()

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

s = sum(a)
nums = {}
for i in range(n):
    if a[i] in nums:
        nums[a[i]] += 1
    else:
        nums[a[i]] = 1

for i in range(int(input())):
    b, c = map(int, input().split())
    if b in nums:
        s -= nums[b]*b
        s += nums[b]*c
        if c in nums:
            nums[c] += nums[b]
        else:
            nums[c] = nums[b]
        nums[b] = 0
    print(s)

from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
Q = int(input())

d = defaultdict(int)
v = 0
ans = []

for a in A:
    d[a] += 1
    v += a

for i in range(Q):
    B, C = list(map(int, input().split()))
    v += d[B] * (C - B)
    d[C] += d[B]
    d[B] = 0
    ans.append(v)

for a in ans:
    print(a)


from collections import defaultdict

def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(int)
    for i in a:
        d[i]+=1
    s = sum(a)
    q = int(input())
    for i in range(q):
        b, c = map(int, input().split())
        s-=b*d[b]
        s+=c*d[b]
        print(s)
        d[c]+=d[b]
        d[b] = 0
main()

n=int(input())
a=[int(x) for x in input().split()]
q=int(input())
s=[0]*(10**5+5)

sum=0
for ai in a:
  s[ai]+=1; sum+=ai;

for i in range(q):
  b,c=map(int,input().split())
  cnt=s[b]; s[b]=0; s[c]+=cnt;
  sum+=(c-b)*cnt
  print(sum)

def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    q = int(input())
    bc_lst = [list(map(int, input().split())) for _ in range(q)]

    a_max = max(a_lst)
    bc_max = 0
    for i in range(q):
        b = bc_lst[i][0]
        c = bc_lst[i][1]
        bc_max = max(b, bc_max)
        bc_max = max(c, bc_max)
    maximum = max(a_max, bc_max)

    count_lst = [0] * maximum
    for i in range(n):
        a = a_lst[i]
        count_lst[a - 1] += 1
    tmp = sum(a_lst)

    for i in range(q):
        b = bc_lst[i][0]
        c = bc_lst[i][1]

        count = count_lst[b - 1]
        tmp += count * (c - b)
        count_lst[b - 1] = 0
        count_lst[c - 1] += count

        print(tmp)


def __starting_point():
    main()
__starting_point()

N = int(input())
A = list(map(int,input().split()))
Q = int(input())

cnt_dict = {}


s = sum(A)

ans = 0

for item in A:
    if item not in cnt_dict:
        cnt_dict[item] = 0
    cnt_dict[item] += 1

for _ in range(Q):
    X,Y = list(map(int,input().split()))

    if X not in cnt_dict:
        cnt_dict[X] = 0
    if Y not in cnt_dict:
        cnt_dict[Y] = 0

    s -= cnt_dict[X] * X
    s += cnt_dict[X] * Y

    cnt_dict[Y] += cnt_dict[X]
    cnt_dict[X] = 0


    print(s)





'''
#初期案　遅すぎ

N = int(input())
A = list(map(int,input().split()))
Q = int(input())



for i in range(Q):
    X,Y = map(int,input().split())

    ans = 0

    for j in range(N):
        if A[j] == X:
            ans += Y
            A[j] = Y
        else:
            ans += A[j]

    print(ans)

    '''


import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time, copy,bisect
#from operator import itemgetter
#from heapq import heappush, heappop
#import numpy as np
#import sympy
#from scipy.sparse.csgraph import breadth_first_order, depth_first_order, shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
import sys

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ni()
A = na()
Q = ni()
ans = sum(A)
d = collections.Counter(A)
for i in range(Q):
    B, C = na()
    ct_b = d[B]
    if ct_b == 0:
        print(ans)
    else:
        ans += ct_b * (C - B)
        d[C] += ct_b
        d[B] = 0
        print(ans)

N=int(input())
*A,=map(int,input().split())
Q=int(input())
BC=[list(map(int,input().split())) for _ in range(Q)]

import collections
c = collections.Counter(A)
s=sum(A)

for i,j in BC:
 v=c[i]
 s+=v*(j-i)
 print(s)
 c[j]+=v
 c[i]=0

import collections

n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
for i in range(q):
    bi, ci = list(map(int, input().split()))
    b.append(bi)
    c.append(ci)

count = collections.Counter(a)
sums = sum(a)

for i in range(q):
    sums += count[b[i]] * (c[i] - b[i])
    print(sums)
    count[c[i]] += count[b[i]]
    count[b[i]] = 0


n=int(input())
l=[0]*(10**5+1)
k=list(map(int,input().split()))
q=int(input())
for i in range(n):
    l[k[i]]+=1
wa=sum(k)
for i in range(q):
    a,b=map(int,input().split())
    wa+=(b-a)*l[a]
    print(wa)
    l[b]+=l[a]
    l[a]=0

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

    sum_a = sum(A)

    d = dict()

    for a in A:
        d[a] = d.get(a, 0) + 1

    for _ in range(int(input())):
        b, c = list(map(int, input().split()))
        if d.get(b, 0) != 0:

            temp = d[b]         
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

            sum_a += (c - b) * temp
        print(sum_a)
        
main()


from collections import Counter


def solve(string):
    n, *aqbc = list(map(int, string.split()))
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if c in list(t.keys()):
            t[c] += t[b]
        else:
            t[c] = t[b]
        s += (c - b) * t[b]
        del t[b]
        ans.append(s)
    return "\n".join(map(str, ans))


def __starting_point():
    import sys
    print((solve(sys.stdin.read().strip())))

__starting_point()

N = int(input())
A = list(map(int,input().split()))
a = {}
for i in A:
    if i in a:
        a[i] += 1
    else:
        a[i] = 1
Q = int(input())
ans = sum(A)
for i in range(Q):
    B,C = map(int,input().split())
    if B in a:
        ans += (C-B) * a[B]
        if C in a:
            a[C] += a[B]
        else:
            a[C] = a[B]
        a[B] = 0
    print(ans) 

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
nums = [0]*100000

for i in range(N):
  nums[A[i]-1] += 1

S = 0

for j in range(100000):
  S += (j+1)*nums[j]

for j in range(Q):
  B,C = map(int,input().split())
  S += nums[B-1]*(C-B)
  nums[C-1] += nums[B-1]
  nums[B-1] = 0
  print(S)

from collections import Counter
N = int(input())
# 正数列A
A = list(map(int, input().split()))
Q = int(input())
# B_iである要素をC_iに置き換える
B_C = [list(map(int, input().split())) for _ in range(Q)]

all_count = Counter(A)
S = sum(A)

for B, C in B_C:

    count_B = all_count[B]
    all_count[B] = 0
    all_count[C] += count_B
    S = S + (C-B)*count_B

    print(S)

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

sum_a = sum(A)

d = dict()

for a in A:
    d[a] = d.get(a, 0) + 1

for _ in range(int(input())):
    b, c = list(map(int, input().split()))
    if d.get(b, 0) != 0:

        temp = d[b]         
        d[b] -= temp
        d[c] = d.get(c, 0) + temp

        sum_a += (c - b) * temp
    print(sum_a)
        



import collections

N = int(input())
A = list(map(int,input().split()))
Ac = collections.Counter(A)
ans = sum(A)

Q = int(input())
Ackey = set(Ac.keys())

for _ in range(Q):
    B,C = list(map(int,input().split()))
    if B in Ackey:
        numB = Ac[B]
    else:
        numB = 0
    ans += (C-B)*numB
    print(ans)
    Ac[C]+=numB
    Ackey.add(C)
    Ac[B]=0



# -*- coding: utf-8 -*-
"""
Created on Mon Sep  7 01:13:08 2020

@author: liang
"""
"""
ABC  171 D 差分更新　パケット変換
【無駄な探索は行わない（メモリ化） ⇒　パケット変換】
パケット
　値vが出現する回数を記録しておく

dict()方式　⇒　探索にO(logn) or O(n)がかかる
配列方式　⇒　探索がO(1)となる!

【差分更新】
値Bが全て値Cになる
　⇒　(C - B) * 値Bの個数だけ増加する
 ⇒　値Cの個数　+= 値Bの個数
 ⇒　値Bの個数　= 0
"""

N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())

d = [0]*(10**5)

for i in A:
    d[i-1] += 1

ans = sum(A)

for i in range(Q):
    b, c = map(int,input().split())
    ans += (c-b)*d[b-1]
    d[c-1] += d[b-1]
    d[b-1] = 0
    print(ans)

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
C = (10**5+1)*[0]
T = sum(A)

for a in A:
  C[a]+=1

for q in range(Q):
  b,c = map(int,input().split())
  T+=C[b]*(c-b)
  C[c]+=C[b]
  C[b]=0
  print(T)

import numpy as np
from collections import Counter
N=int(input())
Alist=list(map(int,input().split()))
Q=int(input())
bc=[]
for _ in range(Q):
    bc.append(list(map(int,input().split())))
count=Counter(Alist)
result=0
for key,value in count.items():
    result+=key*value
for i in range(Q):
    if bc[i][0] in count.keys():
        result+=(bc[i][1]-bc[i][0])*count[bc[i][0]]
        count[bc[i][1]]+=count[bc[i][0]]
        count[bc[i][0]]=0
    print(result)

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

d={}
cnt=sum(a)

for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1

for i in range(q):
    b,c=map(int,input().split())
    if b in d:
        if c not in d:
            d[c]=0
        e=d.pop(b)
        d[c]+=e
        cnt+=(c-b)*e
    print(cnt)

N = int(input())
Alist = list(map(int,input().split()))
sum1 = sum(Alist)
dic = {}
for a in Alist:
    if not a in dic:
        dic[a] = 0
    dic[a]+=1

Q = int(input())
Qlist = []
for _ in range(Q):
    Qlist.append(list(map(int,input().split())))
for q in Qlist:
    b,c = q
    n = 0
    if b in dic:
        n = dic[b]
        dic[b] = 0
    else:
        pass

    if c in dic:
        dic[c] += n
    else:
        dic[c] = n
    sum1 += n*(c-b)

    print(sum1)

import collections

n = int(input())
a = list(map(int, input().split()))
q = int(input())
s = sum(a)

ad = collections.Counter(a)

for i in range(q):
    b, c = map(int, input().split())
    s += ad.get(b, 0) * (c - b) 
    print(s)
    ad[c] += ad.get(b, 0)
    ad[b] = 0

_,A,Q,*X=open(0);S=0;L=[0]*7**6
for a in map(int,A.split()):S+=a;L[a]+=1
for x in X:B,C=map(int,x.split());S+=(C-B)*L[B];L[C]+=L[B];L[B]=0;print(S)

from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
su = sum(A)
counter = Counter(A)
lis = {}
for i in range(Q):
  B, C = map(int, input().split())
  su -= B * counter[B]
  su += C * counter[B]
  counter[C] += counter[B]
  counter[B] = 0
  print(su)

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

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

s = sum(a)
count = {}
for x in a:
    count[x] = count.get(x, 0) + 1

q = int(input())
ans = []
for _ in range(q):
    b, c = map(int, input().split())
    s += (c - b) * count.get(b, 0)
    count[c] = count.get(c, 0) + count.get(b, 0)
    count[b] = 0
    ans.append(s)

for x in ans:
    print(x)

from collections import Counter

N=int(input())
A=list(map(int,input().split()))
Q=int(input())
#BC=[]
#for i in range(Q):
#    BC.append(list(map(int,input().split())))
A_sum=sum(A)
A_count=Counter(A)

for i in range(Q):
    B,C=(map(int,input().split()))
    A_sum+=(C-B)*A_count[B]
    A_count[C]+=A_count[B]
    A_count[B]=0
    print(A_sum)

import collections
n = int(input())
arr = list(map(int,input().split()))
cnt = collections.defaultdict(int)
for val in arr:
    cnt[val] += 1
sums = sum(arr)
q = int(input())
for _ in range(q):
    b, c = list(map(int,input().split()))
    diff = (c-b)*(cnt[b])
    sums += diff
    print(sums)
    cnt[c] += cnt[b]
    cnt[b] = 0


n = int(input())
a = [int(x) for x in input().split()]
ans = sum(a)
ans_li = []
q = int(input())

d = {}
for i in range(len(a)):
    if a[i] not in d:
        d[a[i]] = 0
    d[a[i]] += 1

for i in range(q):
    B,C = list(map(int,input().split()))
    if B not in d:
        ans_li.append(ans)
    else:
        num = d[B]
        dis = abs(B - C)
        if B >= C:
            ans_li.append(ans - (num * dis))
            ans = ans_li[-1]
        else:
            ans_li.append(ans + (num * dis))
            ans = ans_li[-1]

        if C not in d:
            d[C] = num
            d[B] = 0
        else:
            d[C] += num
            d[B] = 0

for aa in ans_li:
    print(aa)


n = int(input())
a = list(map(int, input().split()))
q = int(input())
dic = [0 for _ in range(10 ** 5 + 1)]
count = 0
for i in a:
    dic[i] += 1
    count += i

for i in range(q):
    b, c = map(int,input().split())
    count -= b * dic[b]
    dic[c] += dic[b]
    count += c * dic[b]
    dic[b] = 0
    print(count)

from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    q = int(input())
    bc = []
    sum1 = sum(a)
    for _ in range(q):
        bc.append(list(map(int, input().split(" "))))
    d = defaultdict(lambda:0)
    for i in range(n):
        d[a[i]] += 1

    for i in range(q):
        if bc[i][0] in d:
            sum1 += (bc[i][1] - bc[i][0]) * d[bc[i][0]]
            d[bc[i][1]] += d[bc[i][0]]
            del d[bc[i][0]] 
        print(sum1)
        

def __starting_point():
    main()
__starting_point()

n = int(input())
A = list(map(int, input().split()))
q = int(input())
query = [tuple(map(int, input().split())) for _ in range(q)]

s = sum(A)

B = [0 for _ in range(10**5 + 1)]
for a in A:
    B[a] += 1

for b, c in query:
    s -= b * B[b]
    s += c * B[b]
    print(s)
    B[c] += B[b]
    B[b] = 0

N=int(input())
List = list(map(int, input().split()))
Row = int(input())
QList = []
for i in range (Row):
  QList.append(list(map(int, input().split())))
dictA ={}
res = 0
for i in range (N):
  dictA.setdefault(List[i],0) 
  dictA[List[i]] += 1
  res += List[i]
num = 0
for i in range(Row):
  if QList[i][0] not in dictA:
    pass
  else:
    num = dictA[QList[i][0]] 
    res = res - dictA[QList[i][0]] * QList[i][0]
    dictA[QList[i][0]] = 0
    dictA.setdefault(QList[i][1],0) 
    dictA[QList[i][1]] += num
    res += QList[i][1] * num
  print(res)

n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = dict()

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

s = sum([k * v for k, v in d.items()])

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in d.keys():
        s += (c - b) * d[b]
        if c in d.keys():
            d[c] += d[b]
            d[b] = 0
        else:
            d[c] = d[b]
            d[b] = 0
    print(s)

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
  B_temp,C_temp = map(int,input().split())
  B.append(B_temp)
  C.append(C_temp)
Counts = [0]*100010
for i in range(N):
  Counts[A[i]]=Counts[A[i]]+1

sum_A = 0
for i in range(100010):
  sum_A = sum_A + i*Counts[i]
  
for i in range(Q):
  sum_A = sum_A + Counts[B[i]]*(C[i]-B[i])
  Counts[C[i]] = Counts[C[i]]+Counts[B[i]]
  Counts[B[i]] = 0
  print(sum_A)