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):
import sys
import math as mt
input=sys.stdin.buffer.readline  

t=1
for __ in range(t):
    
    n=int(input())
    l=list(map(int,input().split()))
    suma=sum(l[:])
    q=int(input())
    d=[0]*(100001)
    for i in l:
        d[i]+=1
    for ______ in range(q):
        b,c=map(int,input().split())
        suma-=(d[b]*b)
        suma+=(d[b]*c)
        d[c]+=d[b]
        d[b]=0
        print(suma)

Solution (Python):
from collections import defaultdict

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

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

B, C = [], []
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for i in range(Q):
    b = B[i]
    c = C[i]
    now_b = d[b]
    d[c] += now_b
    d[b] = 0
    s += (c - b) * now_b
    print(s)

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


res = {}
for a_i in a:
    if not a_i in res:
        res[a_i] = 0
    res[a_i] += 1

ans = 0
for key, val in res.items():
    ans += key * val

for b_i, c_i in zip(b, c):
    num = res[b_i] if b_i in res else 0
    res[b_i] = 0
    if not c_i in res:
        res[c_i] = 0
    res[c_i] += num

    ans += (c_i - b_i) * num

    print(ans)

Solution (Python):

"""
@author: H_Hoshigi
"""
def main():
    A_max = pow(10, 5)+1
    N = int(input())
    A_lsit = list(map(int, input().split()))
    Q = int(input())
    B_list = [0]*Q
    C_list = [0]*Q
    for q in range(Q):
        B, C = map(int, input().split())
        B_list[q] = B
        C_list[q] = C
    
    bucket_list = [0]*A_max
    for A in A_lsit:
        bucket_list[A] += 1

    S = 0
    for i in range(A_max):
        S += i*bucket_list[i]
    for q in range(Q):
        B = B_list[q]
        C = C_list[q]
        S += bucket_list[B] * (C-B)
        print(S)
        bucket_list[C] += bucket_list[B]
        bucket_list[B] = 0
    
if __name__ == "__main__":
    main()

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
cnt = collections.Counter(a)
sm = sum(a)
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    if cnt.get(b) != None:
        num = cnt[b]
        del cnt[b]
        sm += num * (c-b)
        if cnt.get(c) != None:
            cnt[c] += num
        else:
            cnt[c] = num
    print(sm)

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

from collections import*
C=Counter(A)
ans=sum(A)

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

Solution (Python):
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)
  
sum = 0
n_dic = {}
  
for i in range(N):
  sum += A[i]
  if A[i] not in n_dic.keys():
    n_dic[A[i]] = 1
  else:
  	n_dic[A[i]] += 1
  
for i in range(Q):
  
  
  if B[i] not in n_dic.keys():
    print(sum)
  else:
    
    sum = sum - (B[i] * n_dic[B[i]]) + (C[i] * n_dic[B[i]])
    print(sum)
    if C[i] not in n_dic.keys():
      n_dic[C[i]] = n_dic[B[i]]
      n_dic[B[i]] = 0
    else:
      n_dic[C[i]] += n_dic[B[i]]
      n_dic[B[i]] = 0

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
import collections

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

    ans = sum(a)
    a = collections.Counter(a)
    for b, c in bc:
        if b not in a:
            print(ans)
            continue
        b_num = a[b]
        ans -= b_num * b
        ans += b_num * c
        if c in a:
            a[c] += b_num
        else:
            a[c] = b_num
        a[b] = 0
        print(ans)

    return


if __name__ == "__main__":
    main()
    sys.exit(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
q=int(input())
s=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    if c in d and b in d:
        d[c]+=d[b]
    elif b in d:
        d[c]=d[b]
    if b in d:
        s=s+(c-b)*d[b]
        d.pop(b)
    print(s)

Solution (Python):
import collections
import numpy as np

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0 for _ in range(Q)]
C = [0 for _ in range(Q)]
for i in range(Q):
  B[i], C[i] = map(int, input().split())
  
a = collections.Counter(A)
S = np.sum(A)
for i in range(Q):
  S += (C[i]-B[i]) * a[B[i]]
  a[C[i]] += a[B[i]]
  a[B[i]] = 0

  print(S)

Solution (Python):


def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    q = int(input())
    bc_lst = []
    for i in range(q):
        bc_lst.append(list(map(int, input().split())))
    cnt_lst = [0 for _ in range(100001)]
    s = 0

    for a in a_lst:
        cnt_lst[a] += 1
        s += a

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

if __name__ == '__main__':
    main()

Solution (Python):




import numpy as np
import collections

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return np.array(x)

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

c = collections.Counter(A)

ans = sum(A)

for i in range(0,Q):
    Bi = BC[i,0]
    Ci = BC[i,1]
    ans = ans-c[Bi]*Bi + c[Bi]*Ci
    print(ans)
    if Ci in c:
        c[Ci] = c[Ci] + c[Bi]
        del c[Bi]
    else:
        c[Ci] = c[Bi]
        del c[Bi]

Solution (Python):
import collections
n=int(input())
A=list(map(int,input().split()))
q=int(input())
A.sort()
B=collections.Counter(A)
S=sum(A)
C=[]
for _ in range(q):
  b,c=map(int,(input().split()))
  C.append(B[b]*(c-b)+S)
  S+=B[b]*(c-b)
  B[c]+=B[b]
  B[b]=0

for i in C:
  print(i)

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

import collections
ac = collections.Counter(A)

sumA = sum(A)

for b,c in BC:
    cha = 0
    cha += (c * ac[b])
    cha -= (b * ac[b])
    ac[c] += ac[b]
    ac[b] = 0
    sumA += cha
    print(sumA)

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

from collections import defaultdict

memo = defaultdict(int)

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

ans = []
now_sum = sum(A)


for ele in BC:

    now_sum += memo[ele[0]]*ele[1] - memo[ele[0]]*ele[0]
    memo[ele[1]] += memo[ele[0]]
    memo[ele[0]] = 0
    ans.append(now_sum)

for an in ans:
    print(an)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for _ in range(Q):
    B.append(list(map(int,input().split())))
s=sum(A)
from collections import Counter
a=Counter(A)
b=list(a.keys())
c=list(a.values())
for i in range(Q):
    s-=B[i][0]*a[B[i][0]]
    s+=B[i][1]*a[B[i][0]]
    a[B[i][1]]+=a[B[i][0]]
    a[B[i][0]]=0
    print(s)

Solution (Python):
import collections 
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dic = collections.Counter(A)
suma = sum(A)
output = []

for q in range(Q):
    b, c = map(int, input().split())
    if b in dic:
        suma += dic[b] * c
        suma -= dic[b] * b
        dic[c] += dic[b]
        dic[b] = 0

    output.append(suma)

print(*output, sep="\n")

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

Solution (Python):

 
import sys
import string
from collections import Counter

def get_int(): return int(sys.stdin.readline().strip())
def get_ints(): return map(int, sys.stdin.readline().strip().split()) 
def get_list(): return list(map(int, sys.stdin.readline().strip().split())) 
def get_string(): return sys.stdin.readline().strip()
def out(data): return sys.stdout.write(str(data))
def out_list(arr): return sys.stdout.write(' '.join(str(e) for e in arr))

MAX = 10**9
MOD = 10**9 + 7

n = get_int()
arr = get_list()
d = Counter(arr)
q = get_int()
sum_ = sum(arr)

for _ in range(q):
	a, b = get_ints()
	if a in d.keys() :
		sum_ += d[a] * (b - a)
		count = d[a]
		d.pop(a)
		if not d[b]:
			d[b] = count
		else:
			d[b] += count
	out(sum_)
	out('\n')

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

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

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

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

ans = [0]*10**6

sum = 0
for i in li_a:
    ans[i] += 1
    sum += i

    
for i in range(q):
    b = li_b[i][0]
    c = li_b[i][1]
    
    cnt = ans[b]
    ans[b] = 0
    ans[c] += cnt
    
    sum += (c-b)*cnt
    print(sum)

Solution (Python):
n = int(input())
a_series = list(map(int, input().split()))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int, input().split())))

summed = sum(a_series)
num = [0 for i in range(100005)]

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

for i in range(q):
    summed += ((bc[i][1] - bc[i][0]) * num[bc[i][0]])
    num[bc[i][1]] += num[bc[i][0]]
    num[bc[i][0]] = 0
    print(summed)

Solution (Python):
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
sum_ = sum(A)
q = int(input())
counter = Counter(A)
for _ in range(q):
    b, c = map(int, input().split())
    x = counter[b]
    counter[c] += x
    counter[b] = 0
    sum_ += (c-b) * x
    print(sum_)

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline
    from collections import Counter

    N = int(input())
    A = list(map(int, input().split()))
    CA = Counter(A)
    sumA = 0
    for key, value in CA.items():
        sumA += key*value

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

main()

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

Solution (Python):
def main():
    import collections
    import sys
    def input():
        return sys.stdin.readline()[:-1]
    N=int(input())
    A=list(map(int,input().split()))
    Q=int(input())
    BC=[list(map(int,input().split())) for _ in range(Q)]
    answer=sum(A)
    c=collections.Counter(A)
    for x in BC:
        B=x[0]
        C=x[1]
        if B in c.keys():
            n=c[B]
            answer=answer-(B-C)*n
            c[B]=0
            c[C]=c[C]+n
        print(answer)



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 i in range(q)]
Alist=[0]*(10**5+1)
ans=[]

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

sum=0
for i in range(1,len(Alist)):
    sum+=i*Alist[i]

for bc in BC:
    sum=sum-bc[0]*Alist[bc[0]]+bc[1]*Alist[bc[0]]
    Alist[bc[1]]+=Alist[bc[0]]
    Alist[bc[0]]=0
    ans.append(sum)
ans='\n'.join(map(str,ans))
print(ans)

Solution (Python):
N = int(input().strip())

A = {}
for a in [int(x) for x in input().strip().split(" ")]:
    A.setdefault(a, 0)
    A[a] += 1
total = sum([k * v for k, v in A.items()]) 

Q = int(input().strip())

for q in range(Q):
    B, C = [int(x) for x in input().strip().split(" ")]
    if B in A:
        A.setdefault(C, 0)
        dif = (C - B) * A[B]
        A[C] += A[B]
        del A[B]
        total += dif
    print(total)

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan,sqrt
import cmath as cma
import copy as cp
import sys
import re
import bisect as bs
sys.setrecursionlimit(10**7)
EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf
MOD = 10**9 + 7

def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
def adj(n): return [[] for _ in range(n)]

n = iinput()
a = iarr()
cnt = col.Counter(a)
sum = sum(a)

q = iinput()
anss = []
for _ in range(q):
    b,c = imap()
    tmp = cnt[b]
    cnt[b] -= tmp
    cnt[c] += tmp
    sum += (c - b) * tmp
    anss.append(sum)

for ans in anss: print(ans)

Solution (Python):
n = int(input())
l= list(map(int, input().split()))
table= {}
for num in l:
	if num in table:
		table[num]+=1
	else:
		table[num]=1

tot= 0
for num in l:
	tot+= num

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

Solution (Python):
N= int(input())
A= list(map(int, input().split()))
Q= int(input())
tmpDic = {}
res = sum(A)

for i in range(N):
    if A[i] in tmpDic:
        tmpDic[A[i]] += 1
    else:
        tmpDic[A[i]] = 1

for i in range(Q):
    B, C= list(map(int, input().split()))
    if B in tmpDic and tmpDic[B] > 0:
        tmp = tmpDic[B]
        tmpDic[B] = 0
        res += (C - B)* tmp
        
        if C in tmpDic:
            tmpDic[C] += tmp
        else:
            tmpDic[C] = tmp
    print(res)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
ans = sum(A)

rep = dict()
for i in range(N):
    if A[i] in rep:
        rep[A[i]] += 1
    else:
        rep[A[i]] = 1

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

Solution (Python):


def main():
    from collections import Counter

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

    ans = [None] * Q
    Asum = sum(A)
    A_Counter = Counter(A)
    for q in range(Q):
        B, C = map(int, input().split())
        Asum = Asum + (C-B)*A_Counter[B]
        A_Counter[C] += A_Counter[B]
        A_Counter[B] = 0

        ans[q] = Asum

    print('\n'.join(map(str, ans)))


if __name__ == "__main__":
    main()

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
num_list = [0 for i in range(100001)]
for i in a:
    num_list[i] += 1
ans = sum(a)

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

Solution (Python):
from collections import defaultdict

n=int(input())
a= list(map(int, input().split()))
q=int(input())
b= [list(map(int, input().split())) for i in range(q)]
d=defaultdict(int)
ans=sum(a)

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

for i,j in b:
    ans+=d[i]*(j-i)
    print(ans)
    d[j]+=d[i]
    d[i]=0

Solution (Python):
n=int(input())
a_list = list(map(int,input().split()))
q=int(input())
b_c_list=[list(map(int,input().split())) for _ in range(q)]

sum_ans = sum(a_list)

count_list = [0] * 100001
for i in range(n):
    count_list[a_list[i]] += 1

for j in range(len(b_c_list)):
    from_j = b_c_list[j][0]
    to_j = b_c_list[j][1]
    
    sum_diff = (to_j - from_j) * count_list[from_j]
    sum_ans += sum_diff
    print(sum_ans)
    
    count_list[to_j] += count_list[from_j]
    count_list[from_j] = 0

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    q = int(input())
    cnt = [0]*(10**5+7)
    for i in range(n):
        cnt[a[i]]+=1
    ruiseki = sum(a)
    for i in range(q):
        b,c = map(int,input().split())
        d = cnt[b]
        e = d*(c-b)
        cnt[c] += d
        cnt[b] -= d
        ruiseki += e
        print(ruiseki)

Solution (Python):


import sys
import numpy as np
from collections import defaultdict
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

Q = int(readline())
m = map(int, read().split())
B, C = zip(*zip(m, m))

dicts = defaultdict(int)
s = 0
for a in A:
    dicts[a] += 1
    s += a

for b, c in zip(B, C):
    cnt = dicts[b]
    dicts[b] = 0
    dicts[c] += cnt
    s += cnt * (c - b)
    print(s)

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

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

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

Solution (Python):
def resolve():
    from collections import defaultdict
    N = int(input())
    A = [int(i) for i in input().split()]
    d = defaultdict(int)
    sumA = 0
    for a in A:
        d[a] += 1
        sumA += a
    Q = int(input())
    BBCC = [list(map(int, input().split())) for _ in range(Q)]
    for b, c in BBCC:
        sumA = sumA + (d[b] * c - d[b] * b)
        tmp = d[b]
        d[c] += tmp
        d[b] = 0
        print(sumA)


resolve()

Solution (Python):
import numpy as np

N=int(input())
A=list(map(int,input().split()))
Adic={}
for i in A:
  if Adic.get(i)==None:
    Adic[i]=1
  else:
    Adic[i]+=1
Q=int(input())
ope=[]

for i in range(Q):
  ope.append(list(map(int,input().split())))

A=np.array(A)

sum=0
for key,value in Adic.items():
  sum+=key*value
for b,c in ope:
  if Adic.get(b)!=None:
    if Adic.get(c)==None:Adic[c]=0
    sum=sum-b*Adic[b]+c*Adic[b]
    Adic[c]+=Adic.pop(b)
    print(sum)
  else:
    print(sum)

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

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

ans = 0
for k in m:
    ans += m[k] * k

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

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

from collections import defaultdict
dic = defaultdict(int)
for a in A:
  dic[a] += 1 
  
ans = sum(A)
for i in range(q):  
  b,c = map(int,input().split())
  
  diff = dic[b]*c-dic[b]*b
  dic[c] += dic[b]
  ans += diff
  print(ans)  
  dic[b] = 0

Solution (Python):
from collections import Counter

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

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

Solution (Python):
from collections import Counter

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

cnt = Counter(a)

ans = []
sum_ = sum(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    bv, cv = cnt[b], cnt[c]
    sum_ += c*(cv+bv) - b*bv - c*cv
    cnt[c] += bv
    cnt[b] = 0
    ans.append(sum_)

print("\n".join(map(str, ans)))

Solution (Python):
from collections import Counter
n = int(input())
A = list(input().split())
q = int(input())
ansList = [0]*q
d = Counter(A)
Sum = sum(map(int,A))

for i in range(q):
    b,c = input().split()
    Sum = Sum - int(b)*d[b] + int(c)*d[b]
    if b in d:
        if c in d:
            d[c] += d.pop(b)
        else:
            d[c] = d.pop(b)
    ansList[i]  +=  Sum

[print(ans) for ans in ansList]

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

q = int(input())
b,c = [],[]

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

Solution (Python):
N=int(input())

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

Amap=dict()

for i in range(N):
    if A[i] in Amap:
        Amap[A[i]]+=1
    else:
        Amap[A[i]]=1



s = 0
for k in Amap.keys():
    s += (int(k)*Amap[k])


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

    if B in Amap:
        if C in Amap:
            Amap[C] += Amap[B]
            s += (C-B)*Amap[B] 
        else:
            Amap[C] = Amap[B]
            s += (C-B)*Amap[B] 

    else:
        if C in Amap:
            Amap[C] += 0
        else:
            Amap[C] = 0

    Amap[B] = 0
    


    ans.append(s)
    

for s in ans:
    print(s)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a_dict = {}
sum = 0
for i in range(n):
    if a[i] not in a_dict:
        a_dict[a[i]] = 0
    a_dict[a[i]] += 1
    sum += a[i]
q = int(input())
for i in range(q):
    b,c = map(int, input().split())
    
    
    if b in a_dict:
        tmp = a_dict[b]
        a_dict[b] = 0
        if c not in a_dict:
            a_dict[c] = 0
        a_dict[c] += tmp
        sum = sum + tmp * c - tmp * b

    
    
    
    print(sum)

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



def main():
    N = int(input().strip())
    A_list = [int(i) for i in input().strip().split()]
    Q = int(input().strip())
    _sum = sum(A_list)
    num_count = Counter(A_list)
    for _ in range(Q):
        B, C = [int(i) for i in input().strip().split()]
        if B in num_count:
            if C in num_count:
                num_count[C] = num_count[C] + num_count[B]
            else:
                num_count[C] = num_count[B]

            _sum = _sum - (num_count[B] * B) + num_count[B] * C
            del num_count[B]
            print(_sum)
        else:
            print(_sum)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = {}
total = 0
for i in a:
  total += i
  d[i] = d.get(i, 0) + 1




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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
l=[0]*q
ans=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    l[i]=(b,c)
book={}
for i in a:
    if i in book:
        book[i]+=1
    else:
        book[i]=1

for i in range(q):
    if l[i][0] in book:
        if l[i][1] in book:
            ans+=(l[i][1]-l[i][0])*book[l[i][0]]
            book[l[i][1]]+=book[l[i][0]]
            book[l[i][0]]=0
        else:
            ans+=(l[i][1]-l[i][0])*book[l[i][0]]
            book[l[i][1]]=book[l[i][0]]
            book[l[i][0]]=0
    print(ans)

Solution (Python):
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []
result = 0
for num in list(numset):
    result += numcount[num]*num
    
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    result += (c-b)*numcount[b]
    anslist.append(result)

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)

for ans in anslist:
    print(ans)

Solution (Python):
import numpy as np
import math
import collections
import bisect
import itertools
from collections import Counter


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

    counter = Counter(A)
    sumA = sum(A)
    
    for i in range(Q):
        b, c = map(int, input().split())
        if b in counter:
            if c in counter:
                counter[c] = counter[c] + counter[b]
            else:
                counter[c] = counter[b]
            sumA += (c - b) * (counter[b])
            del counter[b]
        print(sumA)


if __name__ == "__main__":
    main()

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
COL = collections.Counter(A)
ret = 0
ans = list()
for c in COL.keys():
    ret += COL[c] * c
for i in range(Q):
    B,C = map(int,input().split())
    if B in COL.keys():
        if C in COL.keys():
            COL[C] += COL[B]
            
            ret += (C-B) * COL[B]           
        else:
            COL[C] = COL[B]
            ret += (C-B) * COL[B]
        COL.pop(B)
    ans.append(ret)

for a in ans:
    print(a)

Solution (Python):


import itertools

n = int(input())
a_l = [int(x) for x in input().split()]
q = int(input())

a_l.sort()
gr = itertools.groupby(a_l)
a_d = dict()
a_sum = sum(a_l)
for key, group in gr:
    a_d[key] = len(list(group))
for i in range(q):
    _b, _c = map(int, input().split())
    try:
        _b_num = a_d.pop(_b)
        a_sum = a_sum + (_c - _b) * _b_num
        try:
            _c_num = a_d[_c]
            a_d[_c] = _b_num + _c_num
        except:
            a_d[_c] = _b_num
    except:
        pass
    print(a_sum)

Solution (Python):
from collections import Counter

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

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

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

import collections

result = sum(a)
counter = collections.Counter(a)

for j in range(q):
  b,c = list(map(int,input().strip().split()))
  if c in counter:
    counter[c] += counter[b]
  else:
    counter[c] = counter[b]
  result += (c-b) * counter[b]
  counter[b] = 0
  print(result)

Solution (Python):
def readinput():
    n=int(input())
    a=list(map(int,input().split()))
    q=int(input())
    bcList=[]
    for _ in range(q):
        bcList.append(list(map(int,input().split())))
    return n,a,q,bcList

def main(n,a,q,bcList):
    hist=[0]*(10**5+1)
    for v in a:
        hist[v]+=1

    sum=0
    for i in range(1,10**5+1):
        sum+=i*hist[i]

    for b,c in bcList:
        sum=sum+hist[b]*(c-b)
        hist[c]+=hist[b]
        hist[b]=0
        print(sum)


if __name__=='__main__':
    n,a,q,bcList=readinput()
    main(n,a,q,bcList)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
l=[0]*q
ans=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    l[i]=(b,c)
book={}
for i in a:
    if i in book:
        book[i]+=1
    else:
        book[i]=1

for i in range(q):
    if l[i][0] in book:
        if l[i][1] in book:
            ans+=(l[i][1]-l[i][0])*book[l[i][0]]
            book[l[i][1]]+=book[l[i][0]]
            book[l[i][0]]=0
        else:
            ans+=(l[i][1]-l[i][0])*book[l[i][0]]
            book[l[i][1]]=book[l[i][0]]
            book[l[i][0]]=0
    print(ans)

Solution (Python):
from collections import Counter
N = int(input())
val2ind = Counter(list(map(int, input().split())))
ans = sum([key*val2ind[key] for key in val2ind])
Q = int(input())

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in val2ind:
        cand = val2ind[B]
        del val2ind[B]
        if C not in val2ind:
            val2ind[C] = cand
        else:
            val2ind[C] += cand
        ans = ans+(C-B)*cand
    print(ans)

Solution (Python):
def main():
    nmax = 100000
    n = int(input())
    values = [0] * nmax
    a = [int(v) for v in input().split()]
    for v in a:
        values[v-1] += 1
    asum = sum(a)

    q = int(input())
    for _ in range(q):
        b, c = [int(v) for v in input().split()]
        asum += values[b-1] * (c - b)
        values[c-1] += values[b-1]
        values[b-1] = 0
        print(asum)

main()

Solution (Python):
import collections

num = int(input())
original = list(map(int, input().split()))
trial_num = int(input())

total = sum(original)

count_dict = collections.Counter(original)

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