Problem:
 Score : 400 points    Problem Statement 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 1 4
1 2 3 4
3
1 2
3 4
2 4      Sample Output 1 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        Sample Input 2 4
1 1 1 1
3
1 2
2 1
3 5      Sample Output 2 8
4
4  Note that the sequence A may not contain an element whose value is B_{i} .       Sample Input 3 2
1 2
3
1 100
2 100
100 1000      Sample Output 3 102
200
2000

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

from collections import Counter
dic = Counter(a)
ans = sum(a)

answer = []
for _ in range(q):
    b, c = map(int, input().split())
    if b in dic:
        if c in dic:
            dic[c] = dic[b] + dic[c]
        else:
            dic[c] = dic[b]
        ans += (c - b) * dic.pop(b)
    answer.append(ans)

for i in answer:
    print(i)

Solution (Python):
from collections import Counter
def main():
    N=int(input())
    A=list(map(int,input().split()))
    Q=int(input())
    count= Counter(A)
    ans=sum(A)
    for i in range(Q):
        b,c=map(int,input().split())
        if b in count.keys():
            if c in count.keys():
                count[c] = count[b]+count[c]
            else:
                count[c] = count[b]
            ch=count.pop(b)
            ans+=((c-b)*ch)
            print(ans)
        else:
            print(ans)
main()

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()
q = ni()
from collections import Counter

d = Counter(a)
s = sum(a)

for i in range(q):
    b,c = na()
    k = d[b]
    s += k*(c-b)
    d[c]+=d[b]
    d[b] = 0
    print(s)

Solution (Python):
from collections import defaultdict

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

    cnt[c] += cnt[b]
    total = total - b * cnt[b] + (cnt[b]) * c
    cnt[b] = 0
    ans[i] = total

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

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
q = int(input())
count_map = Counter(a)
res = sum(a)
ans = []
for _ in range(q):
    b, c = map(int, input().split())
    if b not in count_map or count_map[b] == 0:
        ans.append(res)
    else:
        diff = (c-b)*count_map[b]
        count_map[c] += count_map[b]
        count_map[b] = 0
        res += diff
        ans.append(res)
print(*ans, sep='\n')

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
C = Counter(a)
s = sum(a)
q = int(input())

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
num = [0] * 100010
ans = 0
for i in range(n):
    num[a[i]] += 1
    ans += a[i]

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = sum(A)

d = {}
for i in A:
  if i in d:
    d[i] += 1
  else:
    d[i] = 1

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

Solution (Python):
from collections import defaultdict

n = int(input())
alst = list(map(int, input().split()))
q = int(input())
dd = defaultdict(int)
for num in alst:
    dd[num] += 1
ans = sum(alst)
for _ in range(q):
    b, c = map(int, input().split())
    ans -= b * dd[b]
    ans += c * dd[b]
    dd[c] += dd[b]
    dd[b] = 0
    print(ans)

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

Solution (Python):
import sys
from collections import Counter


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
C = Counter(A)
total = sum(A)
for s in sys.stdin.readlines():
    b, c = map(int, s.split())
    k = C[b]
    total += (c - b) * k
    print(total)
    C[b] = 0
    C[c] += k

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bc = [list(map(int, input().split())) for _ in range(Q)]

num_list = [0 for _ in range(100001)]

all_val = 0
for i in range(N):
    all_val += A[i]
    num_list[A[i]] += 1

for i in range(Q):
    before = bc[i][0]
    after = bc[i][1]
    
    all_val += (after-before) * num_list[before]
    num_list[after] += num_list[before]
    num_list[before] = 0
    
    print(all_val)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
box = [0 for _ in range(int(1e5) + 1)]
sA = sum(A)
for a in A:
    box[a] += 1
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    sA = sA - box[B]*B + box[B]*C
    box[C] += box[B]
    box[B] = 0
    print(sA)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = [0] * q
c = [0] * q
for i in range(q):
    b[i], c[i] = map(int, input().split())

table = {}
s = 0
for num in a:
    if num not in table:
        table[num] = 1
    else:
        table[num] += 1
    s += num

for i in range(q):
    frm, to = b[i], c[i]
    if frm in table:
        s += (to - frm) * table[frm]
        if to not in table:
            table[to] = table[frm]
        else:
            table[to] += table[frm]
        table.pop(frm)
    print(s)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ma = [0] * (100000 + 1)

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

Q = int(input())

s = sum(A)

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

Solution (Python):
import sys
from collections import defaultdict
read = sys.stdin.read
readline = sys.stdin.readline

_ = int(readline())
alst = list(map(int, readline().split()))
_ = int(readline())
m = map(int, read().split())
bc = list(zip(m, m))
s = sum(alst)
dic = defaultdict(int)
for i in alst:
  dic[i] += 1

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
mydict = {}
total = 0
for a in A:
    mydict[a] = mydict[a] + 1 if a in mydict else 1
    total += a

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

Solution (Python):
import bisect

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

Solution (Python):
from collections import Counter

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

S = sum(A)
cnt = Counter(A) 
ans = [0] * Q
for i in range(Q):
    B, C = [int(x) for x in input().split()]
    S += (C - B) * cnt[B]
    cnt[C] += cnt[B]
    cnt[B] = 0
    ans[i] = S

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

Solution (Python):
from collections import Counter
import sys
input = sys.stdin.readline

N = int(input())
*A, = map(int, input().split())
C = Counter(A)
S = sum(A)

Q = int(input())
for _ in [0]*Q:
    b, c = map(int, input().split())
    if b not in C:
        print(S)
        continue
    S += (c-b)*C[b]
    C[c] += C[b]
    C[b] = 0
    print(S)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  
from fractions import gcd
import sys  

n = int(input())
A = list(int(x) for x in input().split())
q = int(input())
BC = [tuple(int(x) for x in input().split()) for _ in range(q)]

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

ans = sum(key*value for key, value in d.items())
for b, c in BC:
    db = d[b]
    dc = d[c]
    d[b] = 0
    d[c] = dc + db
    ans += c*(dc+db) - (b*db + c*dc)
    print(ans)

Solution (Python):
import sys


def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    D = [0] * (1 + 10 ** 5)
    sA = sum(A)
    for a in A: D[a] += 1
    Q = int(input())
    Ans = [-1] * Q
    for i in range(Q):
        B, C = map(int, input().split())
        sA += (C - B) * D[B]
        D[C] += D[B]
        D[B] = 0
        Ans[i] = sA
    print(*Ans, sep="\n")

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
p = [0 for _ in range(10**5+1)]
for ai in a:
  p[ai] += 1
s = sum(a)

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

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    num = [0] * 100005
    A = list(map(int,input().split()))
    S = sum(A)
    for a in A:
        num[a] += 1
    
    Q = int(input())
    for _ in range(Q):
        c,d = map(int,input().split())
        x = num[c]
        num[c] = 0
        S -= x * c
        num[d] += x
        S += x * d
        
        print(S)
if __name__ == '__main__':
    main()

Solution (Python):
import collections

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

ans = sum(a)
cnter = collections.Counter(a)

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

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())

c = collections.Counter(a)
ans = sum(a)

for i in range(q):
    x,y = map(int, input().split())
    p = c[x]
    c[x] = 0
    c[y] += p
    ans -= x*p
    ans += y*p
    print(ans)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy



from collections import Counter,defaultdict,deque
from itertools import permutations, combinations, combinations_with_replacement
from heapq import heappop, heappush, heapify
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n = inp()
    a = inpl()
    dic = defaultdict(int)
    ans = sum(a)
    for i in range(n):
        dic[a[i]] += 1
    q = inp()
    for _ in range(q):
        b,c = inpm()
        ans += dic[b]*(c-b)
        dic[c] += dic[b]
        dic[b] = 0
        print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):

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

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

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

ans = sum(a)
q = int(input())

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

Solution (Python):
import copy
n=int(input())
a=list(map(int,input().split()))
q=int(input())
l=[0]*10**5

for i in a:
    l[i-1]+=1

ans=sum(a)

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

Solution (Python):
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = defaultdict(int)

for i in a:
  d[i] += 1
s = sum(a)

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

Solution (Python):
import numpy as np

n = int(input())

a = list(map(int, input().split()))
l = [0 for i in range(100010)]

q = int(input())

s = np.sum(a)

for ae in a:
    l[ae] += 1

ans = []

for i in range(q):
    b, c = map(int, input().split())

    m_num = l[b]
    l[b] = 0
    m_s = m_num*b

    p_s = m_num*c
    l[c] += m_num

    s = s - m_s + p_s

    ans.append(s)

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

Solution (Python):
n = int(input())
temp = [int(_) for _ in input().split(' ')]
a = [0 for _ in range(100001)]
for k in temp:
    a[k] += 1
s = sum([i * a[i] for i in range(100001)])

q = int(input())

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

Solution (Python):
n = int(input())
aa = list(map(int, input().split()))
total = sum(aa)

q = int(input())
from collections import Counter
dic = Counter(aa)
for i in range(q):
    b, c = map(int, input().split())
    nb = dic.pop(b,0)
    total += nb * (c-b)
    print(total)
    dic[c] = dic.get(c,0) + nb

Solution (Python):
import collections

n = int(input())
l = list(map(int, input().split()))
c = collections.Counter(l)
s = sum(l)

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

Solution (Python):
import collections
N = int(input())
A = [int(i) for i in input().split()]

Cnt = collections.Counter(A)
S = sum(A)

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

Solution (Python):
N = int(input())
num = list(map(int, input().split()))

MAXX = 10**5 + 5
count = [0]*MAXX

sumz = sum(num)
for n in num:
    count[n] += 1

Q = int(input())
for _ in range(Q):
    b, c = map(int, input().split())
    sumz += count[b]*(c-b)
    
    count[c] += count[b]
    count[b] = 0

    print(sumz)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))

my_dict = dict()

current_sum = sum(a)

for item in a:
    if item in my_dict:
        my_dict[item] += 1
    else:
        my_dict[item] = 1

for item in bc:
    b, c = item
    if c in my_dict:
        if b in my_dict:
            cnt = my_dict.pop(b)
            my_dict[c] += cnt
            current_sum += (c - b) * cnt
    else:
        if b in my_dict:
            my_dict[c] = my_dict.pop(b)
            current_sum += (c - b) * my_dict[c]
    print(current_sum)

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int,input().split()))
Q = int(input())
dic = defaultdict(int)
s = 0
for n in A:
    dic[n] += 1
    s += n
for _ in range(Q):
    b,c = map(int,input().split())
    s += (c-b)*dic[b]
    dic[c] += dic[b]
    dic[b] = 0
    print(s)

Solution (Python):
from collections import defaultdict
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
q = int(input())
c = defaultdict(lambda: 0)
tmp = Counter(a)
for i, j in tmp.most_common():
	c[i] = j
s = sum(a)
for _ in range(q):
	mae, ato = map(int,input().split())
	kazu = c[mae]
	c[ato] += kazu
	c[mae] = 0
	s += (ato-mae)*kazu
	print(s)

Solution (Python):
"""
    こんにちは。これはクシャルです
    
    Author : thekushalghosh (クシャル)
    Team   : CodeDiggers
 
    I prefer Python language over the C++ language :p :D
        
    Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time
start_time = time.time()



def solve():
    n = inp()
    a = inlt()
    q = inp()
    w = sum(a)
    qw = counter(a)
    for i in range(q):
        b,c = invr()
        w = w + ((c - b) * qw[b - 1])
        qw[b - 1],qw[c - 1] = 0,qw[c - 1] + qw[b - 1]
        print(w)
            


 
def main():
    global tt
    if not ONLINE_JUDGE:
        sys.stdin = open("input.txt","r")
        sys.stdout = open("output.txt","w")
    t = 1
    for tt in range(t):
        solve()
    if not ONLINE_JUDGE:
        print("Time Elapsed :",time.time() - start_time,"seconds")
        sys.stdout.close()
    

def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    return(input().strip())
def invr():
    return(map(int,input().split()))
 

def counter(a):
    q = [0] * 100000
    for i in range(len(a)):
        q[a[i] - 1] = q[a[i] - 1] + 1
    return(q)
 
def string_counter(a):
    q = [0] * 26
    for i in range(len(a)):
        q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
    return(q)


ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
    input = sys.stdin.readline
main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B_C = [[0,0] for _ in range(Q)]
for i in range(Q):
    B_C[i] = list(map(int,input().split()))

df = {}
original_sum = 0 
for a in A:
    if not a in df.keys():
        df[a] = 1
    else:
        df[a] += 1
    original_sum += a

result = []
now_sum = original_sum

for i in range(Q):
    if (B_C[i][0] in df.keys()):
        if (df[B_C[i][0]] > 0):
            now_sum += (B_C[i][1] -B_C[i][0]) * df[B_C[i][0]]
            if B_C[i][1] in df.keys():
                df[B_C[i][1]] += df[B_C[i][0]]
            else:
                df[B_C[i][1]] = df[B_C[i][0]]
            df[B_C[i][0]] = 0
    result.append(now_sum)

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

Solution (Python):
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
q=int(input())
d=defaultdict(int)
s=0
for i in range(n):
    d[a[i]]+=1
    s+=a[i]
for i in range(q):
    b,c=map(int,input().split())
    k=0
    if(d[b]>0):
        k=d[b]*b
        d[c]+=d[b]
        s-=k
        s+=(d[b]*c)
        d[b]=0
    print(s)

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

from collections import Counter
sum_ = sum(A)

Counter_A = Counter(A)

for q in range(Q):
    B,C = list(map(int,input().split()))
    B_num = Counter_A[B]
       
    sum_ = sum_+(C-B)*B_num
    print(sum_)
    
    
    Counter_A[B]=0
    Counter_A[C]=Counter_A[C]+B_num

Solution (Python):
import sys
input = sys.stdin.buffer.readline
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

counter = Counter(A)
A_sum = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    num_b = counter.get(b, 0)
    A_sum = A_sum + (c - b) * num_b
    del counter[b]
    counter[c] = counter.get(c, 0) + num_b
    print(A_sum)

Solution (Python):
N = int(input())
l = [0]*(10**5+1)
A = list(map(int,input().split()))
Q = int(input())
for a in A:
  l[a] += a
suml = sum(l)
for _ in range(Q):
  b,c = map(int,input().split())
  suml += ((l[b]//b)*c - l[b])
  l[c] += (l[b]//b)*c
  l[b] = 0
  print(suml)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
	return list(map(int,input().split()))
def m(): 
	return map(int,input().split())
def onem(): 
	return int(input())
def s(x): 
	a = []
	if len(x) == 0:
		return []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa != x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): 
	return " ".join(map(str,x))
def max2(x): 
	return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
	ll = 0
	rr = r
	while True:
		mid = (ll+rr)//2

	if rr == mid:
		return ll
	if (ここに評価入れる):
		rr = mid
	else:
		ll = mid+1
"""


n = onem()

a = l()
ans = sum(a)

po = [0 for i in range(10**5+1)]

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

q = onem()

for i in range(q):
	b,c = m()

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

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
q = int(input())
now = [0 for i in range(10 ** 5)]
for num in lis:
    now[num-1] += 1
ans = 0
for i in range(10 ** 5):
    ans += now[i] * (i+1)
  
for turn in range(q):
    b,c = map(int,input().split())
    num = now[b-1]
    now[b-1] = 0
    now[c-1] += num
    print(ans-b*num+c*num)
    ans -= b*num
    ans += c*num

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())

M = 10**5+3
s = sum(A)
L = [0]*(M+1)
for a in A:
    L[a] += 1

for _ in range(Q):
    b,c = map(int, input().split())
    if L[b] == 0:
        print(s)
        continue
    s = s-L[b]*b+L[b]*c
    L[c] += L[b]
    L[b] = 0
    print(s)

Solution (Python):
from collections import Counter
import copy
N = int(input())
A = list(map(int,input().split()))
c = Counter(A)
Q = int(input())
total = sum(A)
for i in range(Q):
    x,y = map(int,input().split())
    num = copy.deepcopy(c[x])
    total = total + ((y-x)*num)
    c[x] = 0
    c[y] += num
    print(total)

Solution (Python):
from collections import Counter

def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    acc = sum(a)
    counter = Counter(a)
    q = int(input())
    for _ in range(q):
        b, c = map(int, input().split())
        if b in counter:
            if c not in counter:
                counter[c] = 0
            acc += counter[b] * c - counter[b] * b
            counter[c] += counter[b]
            counter[b] = 0
        print(acc)

if __name__ == "__main__":
    resolve()

Solution (Python):
import collections
from collections import defaultdict

def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    BC = []
    for q in range(Q):
        b, c = map(int, input().split())
        BC.append((b, c))
    
    C = dict(collections.Counter(A))
    s = sum(A)
    ans = []
    for b, c in BC:
        cb = C.get(b, 0)
        delta = cb * (c - b)
        s += delta
        ans.append(s)
        C[c] = C.get(c, 0) + cb
        C[b] = 0
    
    print(*ans, sep='\n')

if __name__ == '__main__':
    main()

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  k=I()
  d={}
  for x in l:
    if x in d:
      d[x]+=1
    else:
      d[x]=1

  sm=sum(l)

  ans=[]
  for _ in range(k):
    a,b=LI()
    if a in d:
      x=d[a]
      d[a]=0

      aa=a*x
      bb=b*x

      sm+=(bb-aa)
      ans.append(sm)

      if b in d:
        d[b]+=x
      else:
        d[b]=x

    else:
      ans.append(sm)

  for x in ans:
    print(x)

main()

Solution (Python):
import math as mt
import collections as cc
I=lambda:list(map(int,input().split()))
n,=I()
l=I()
f=cc.defaultdict(int)
for i in l:
    f[i]+=1
s=sum(l)
for i in range(int(input())):
    x,y=I()
    if f[x]==0:
        print(s)
    else:
        s-=f[x]*x
        s+=f[x]*y
        f[y]+=f[x]
        f[x]=0
        print(s)

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

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

Solution (Python):
n=int(input())
arr=[int(i) for i in input().split()]
q=int(input())
q_arr=[]
for i in range(q):
    q_arr.append([int(j) for j in input().split()])
from collections import defaultdict
cnt=defaultdict(lambda :0)
sum_=sum(arr)
for i in arr:
    cnt[i]+=1

for j in q_arr:
    if cnt[j[0]]:
        sum_+=(j[1]-j[0])*(cnt[j[0]])
        cnt[j[1]]+=cnt[j[0]]
        cnt[j[0]]=0
    print(sum_)

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

lis = [0]*(10**5 + 1)
total = 0
for a in A:
    lis[a] += 1
    total += a

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

Solution (Python):
def main():
    n = int(input())
    a = list(map(int,input().split()))
    q = int(input())
    b = [0] * q
    c = [0] * q
    for i in range(q):
        bb,cc = map(int,input().split())
        b[i] = bb
        c[i] = cc
    import collections
    cnt = collections.Counter(a)
    S = sum(a)

    for i in range(q):
        now_b = b[i]
        now_c = c[i]
        now_cnt = cnt[now_b]
        sub = (now_c - now_b) * now_cnt
        S += sub
        cnt[now_b] = 0
        cnt[now_c] += now_cnt
        
        
        print(S)

if __name__=='__main__':
    main()

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

count = [0] * (10**5 + 10)

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

ans = sum(A)

for b,c in BC:
  x = count[b]
  y = c - b
  ans += x * y
  print(ans)
  count[b] = 0
  count[c] += x

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
cur = [0]*(10**5+1)

for i in range(n):
    cur[a[i]] += 1
wa = 0
for i in range(10**5+1):
    wa += cur[i] * i

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

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
Q = int(input())
BC = [[int(_) for _ in input().split()] for _ in range(Q)]


class SegmentTree():
    def __init__(self, array, f, ti):
        """
        Parameters
        ----------
        array : list
            to construct segment tree from
        f : func
            binary operation of the monoid
        ti : 
            identity element of the monoid
        """
        self.f = f
        self.ti = ti
        self.n = n = 2**(len(array).bit_length())
        self.dat = dat = [ti] * n + array + [ti] * (n - len(array))
        for i in range(n - 1, 0, -1):  
            dat[i] = f(dat[i << 1], dat[i << 1 | 1])

    def update(self, p, v):  
        f, n, dat = self.f, self.n, self.dat
        p += n
        dat[p] = v
        while p > 1:
            p >>= 1
            dat[p] = f(dat[p << 1], dat[p << 1 | 1])

    def query(self, l, r):  
        f, ti, n, dat = self.f, self.ti, self.n, self.dat
        res = ti
        l += n
        r += n
        while l < r:
            if l & 1:
                res = f(res, dat[l])
                l += 1
            if r & 1:
                r -= 1
                res = f(dat[r], res)
            l >>= 1
            r >>= 1
        return res


array = [0] * (10**5 + 1)
for a in A:
    array[a] += a
st = SegmentTree(array, f=lambda x, y: x + y, ti=0)
for b, c in BC:
    st.update(c, st.query(c, c + 1) + st.query(b, b + 1) * c // b)
    st.update(b, 0)
    print(st.query(0, 10**5 + 1))

Solution (Python):
N = int(input())
l = [int(x) for x in input().split()]
Q = int(input())
ans = sum(l)
from collections import defaultdict
d = defaultdict(int)
for i in range(N):
    d[l[i]] += 1
for i in range(Q):
    b,c = [int(x) for x in input().split()]
    ans = ans + (c-b)*d[b]
    d[c] += d[b]
    d[b] = 0
    print(ans)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    A = [0] *(10**5+1)
    total = 0
    for a in map(int, readline().split()):
        total += a
        A[a] += 1


    Q = int(readline())
    for _ in range(Q):
        b, c = map(int, readline().split())
        total += A[b]*(c-b)
        print(total)
        A[c] += A[b]
        A[b] = 0


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
n = int(input())
A = [int(i) for i in input().split()]
my_dict = defaultdict(int)
for i in A:
    my_dict[i] += 1
ans = sum(A) 
Q = int(input())
for q in range(Q):
    b, c = [int(i) for i in input().split()]
    if b in my_dict:
        my_dict[c] += my_dict[b]
        ans += my_dict[b] * (c-b)
        my_dict[b] = 0
    print(ans)

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

base=sum(A)

from collections import Counter
ad=Counter(A)
for _ in range(Q):
    b, c=map(int, input().split())
    if ad[b]!=0:
        base+=(c-b)*ad[b]
        ad[c]+=ad[b]
        ad[b]=0
    print(base)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for _ in range (Q)]

dic = dict()
ans = sum(A)
for a in A:
    if a in dic:
        dic[a] += 1
    else:
        dic[a] = 1

for b,c in BC:
    if not (b in dic) or dic[b]==0:
        print(ans)
        continue
    
    sa = (c-b)*dic[b]
    ans += sa
    print(ans)

    if c in dic:
        dic[c] += dic[b]
    else:
        dic[c] = dic[b]
    dic[b] = 0