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: MEDIUM

Solutions:
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)


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


f = lambda : [*map(int, input().split())]
f()
(l, q) = (f(), f()[0])
from collections import *
(C, s) = (Counter(l), sum(l))
for _ in range(q):
	(b, c) = f()
	s += (c - b) * C[b]
	C[c] += C[b]
	C[b] = 0
	print(s)


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


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


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


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


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)


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


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


from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
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)


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


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 = 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()))
C = [0] * (10 ** 5 + 1)
for a in A:
	C[a] += 1
ans = sum(A)
q = int(input())
for i in range(q):
	(b, c) = map(int, input().split())
	d = C[b]
	C[c] += d
	C[b] = 0
	ans += d * (c - b)
	print(ans)


from collections import Counter
R = lambda : map(int, input().split())
R()
d = Counter(R())
s = sum(d.elements())
(q,) = R()
for _ in range(q):
	(b, c) = R()
	s += (c - b) * d[b]
	print(s)
	d[c] += d[b]
	del d[b]


from collections import Counter
N = int(input())
A = list(map(int, input().split()))
s = sum(A)
Q = int(input())
c = Counter(A)
for _ in range(Q):
	(B, C) = map(int, input().split())
	s -= B * c[B]
	s += C * c[B]
	c[C] += c[B]
	c[B] = 0
	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)


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


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


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


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


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()))
counter = [0] * (10 ** 5 + 1)
for i in a:
	counter[i] += 1
q = int(input())
res = sum(a)
for i in range(q):
	(b, c) = map(int, input().split())
	res += counter[b] * (c - b)
	counter[c] += counter[b]
	counter[b] = 0
	print(res)


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()))
		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()


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


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)


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


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


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


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)


n = int(input())
a_list = list(map(int, input().split()))
cnt = [0] * int(100000.0 + 10)
sum = 0
for i in a_list:
	cnt[i] += 1
	sum += i
q = int(input())
ans = 0
for j in range(q):
	(x, y) = map(int, input().split())
	sum += (y - x) * cnt[x]
	cnt[y] += cnt[x]
	cnt[x] = 0
	print(sum)


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


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


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


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()


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)


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)


from collections import Counter
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * (10 ** 5 + 10)
for a in A:
	cnt[a] += 1
s = sum(A)
Q = int(input())
for _ in [0] * Q:
	(b, c) = map(int, input().split())
	s += cnt[b] * (c - b)
	cnt[c] += cnt[b]
	cnt[b] = 0
	print(s)


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


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


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 = [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)


from collections import Counter
a = int(input())
l = list(map(int, input().split()))
b = int(input())
s = Counter(l)
initial_sum = sum(l)
for _ in range(b):
	(x, y) = map(int, input().split())
	k = s[x]
	s[x] = 0
	s[y] += k
	initial_sum += (y - x) * k
	print(initial_sum)


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


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


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())
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)


from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
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())
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)


N = int(input())
An = list(map(int, input().split()))
Q = int(input())
L = [0] * 100001
for item in An:
	L[item] += 1
sum = sum(An)
for i in range(Q):
	(b, c) = map(int, input().split())
	sum += (c - b) * L[b]
	L[c] += L[b]
	L[b] = 0
	print(sum)


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


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


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


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
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)


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())
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


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


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])


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
d = (10 ** 5 + 100) * [0]
res = sum(A)
for x in A:
	d[x] += 1
for i in range(Q):
	(b, c) = map(int, input().split())
	res += d[b] * (c - b)
	d[c] += d[b]
	d[b] = 0
	print(res)


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


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
counter = Counter(a)
sum_res = sum(a)
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)


f = lambda : map(int, input().split())
f()
(s, *l) = [0] * 100002
for i in f():
	s += i
	l[i] += 1
(q,) = f()
for _ in range(q):
	(b, c) = f()
	s += (c - b) * l[b]
	l[c] += l[b]
	l[b] = 0
	print(s)


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


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
q = int(input())
from collections import Counter
d = Counter(a)
for (b, c) in [list(map(int, input().split())) for _ in range(q)]:
	(bb, cc) = (d[b] * b, d[b] * c)
	s += cc - bb
	d[c] += d[b]
	d[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)


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()))
Q = int(input())
s = sum(A)
import collections
a = collections.Counter(A)
for i in range(Q):
	(B, C) = map(int, input().split())
	n = a[B]
	s += (C - B) * n
	print(s)
	a[B] = 0
	a[C] = a[C] + n


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


n = int(input())
a = list(map(int, input().split()))
f = [0] * (int(100000.0) + 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)


N = int(input())
A = list(map(int, input().split()))
TMP = sum(A)
Q = int(input())
import collections
List = collections.Counter(A)
for i in range(Q):
	(S, T) = map(int, input().split())
	TMP = TMP + (T - S) * List[S]
	print(TMP)
	List[T] += List[S]
	List[S] = 0


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())
A = list(map(int, input().split()))
Q = int(input())
cnt = [0] * (10 ** 5 + 1)
for i in range(N):
	cnt[A[i]] += 1
s = sum(A)
for i in range(Q):
	(B, C) = map(int, input().split())
	m = cnt[B]
	s -= B * cnt[B]
	cnt[B] = 0
	s += C * m
	cnt[C] += m
	print(s)


import sys
input = sys.stdin.buffer.readline
from collections import Counter
n = int(input())
(*a,) = map(int, input().split())
C = Counter(a)
ans = sum(a)
for _ in range(int(input())):
	(b, c) = map(int, input().split())
	ans += C[b] * (c - b)
	C[c] += C[b]
	C[b] = 0
	print(ans)


num = [0] * 100100
n = int(input())
sum = 0
for A in map(int, input().split()):
	num[A] += 1
	sum += A
for _ in range(int(input())):
	(b, c) = map(int, input().split())
	sum += (c - b) * num[b]
	num[c] += num[b]
	num[b] = 0
	print(sum)


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


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


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


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


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


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)


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()


R = lambda : map(int, input().split())
R()
a = [0] * 100001
s = 0
for x in R():
	s += x
	a[x] += 1
(q,) = R()
for _ in range(q):
	(b, c) = R()
	s += (c - b) * a[b]
	print(s)
	a[c] += a[b]
	a[b] = 0


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


import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
numl = collections.Counter(a)
s = sum(a)
for qq in range(q):
	(x, y) = map(int, input().split())
	s += numl[x] * (y - x)
	print(s)
	numl[y] += numl[x]
	numl[x] = 0


(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)


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())
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


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


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


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


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()))
s = sum(A)
X = [0] * 100001
for a in A:
	X[a] += 1
Q = int(input())
for _ in range(Q):
	(B, C) = map(int, input().split())
	s -= B * X[B]
	s += C * X[B]
	X[C] += X[B]
	X[B] = 0
	print(s)


from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
S = sum(A)
C = Counter(A)
Q = int(input())
for i in range(Q):
	(Bi, Ci) = map(int, input().split())
	S += (Ci - Bi) * C[Bi]
	C[Ci] += C[Bi]
	C[Bi] = 0
	print(S)


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


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()


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 = list(map(int, input().split()))
t = [0] * (10 ** 5 + 1)
s = 0
for i in range(n):
	t[a[i]] += 1
	s += a[i]
q = int(input())
for j in range(q):
	(b, c) = map(int, input().split())
	s += (c - b) * t[b]
	t[c] += t[b]
	t[b] *= 0
	print(s)


import sys
from collections import Counter
readline = sys.stdin.readline
n = readline()
A = list(map(int, readline().split()))
q = int(readline())
D = Counter(A)
s = sum(A)
for _ in range(q):
	(b, c) = map(int, readline().split())
	s += (c - b) * D[b]
	D[c] += D[b]
	del D[b]
	print(s)


from collections import Counter
N = int(input())
A = list(map(int, input().split()))
ans = sum(A)
cnt = Counter(A)
Q = int(input())
for _ in range(Q):
	(a, b) = map(int, input().split())
	ans += (b - a) * cnt[a]
	cnt[b] += cnt[a]
	cnt[a] = 0
	print(ans)


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


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


import bisect
import collections
n = int(input())
a = sorted(list(map(int, input().split())))
q = int(input())
A = collections.Counter(a)
ans = sum(a)
for _ in range(q):
	(b, c) = map(int, input().split())
	k = A[b]
	ans = (c - b) * k + ans
	print(ans)
	del A[b]
	A[c] += k


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


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


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


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()))
b = [0]
b *= 10 ** 5 + 1
for i in a:
	b[i] += 1
ans = sum(a)
q = int(input())
for i in range(q):
	(bi, ci) = map(int, input().split())
	d = b[bi]
	b[ci] += d
	b[bi] = 0
	ans = ans + d * (ci - bi)
	print(ans)


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


from collections import Counter
N = int(input())
As = list(map(int, input().split()))
ct = Counter(As)
S = sum(As)
Q = int(input())
for _ in range(Q):
	(b, c) = map(int, input().split())
	n = ct[b]
	ct[b] = 0
	ct[c] += n
	S += (c - b) * n
	print(S)


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)


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


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)


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


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


from collections import Counter
R = lambda : map(int, input().split())
R()
d = Counter(R())
s = sum((k * v for (k, v) in d.items()))
(q,) = R()
for _ in range(q):
	(b, c) = R()
	s += (c - b) * d[b]
	print(s)
	d[c] += d[b]
	d[b] = 0


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


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)


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


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


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


x = [0] * 100001
value = 0
n = int(input())
for a in map(int, input().split()):
	x[a] += 1
	value += a
q = int(input())
for _ in range(q):
	(b, c) = map(int, input().split())
	value += (c - b) * x[b]
	x[c] += x[b]
	x[b] = 0
	print(value)


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


import collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
M = collections.Counter(A)
ans = 0
for j in M.items():
	ans += j[0] * j[1]
for i in range(Q):
	(B, C) = map(int, input().split())
	ans += (C - B) * M[B]
	M[C] += M[B]
	M[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


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()))
B = {}
s = 0
for a in A:
	B[a] = B.get(a, 0) + 1
	s += a
q = int(input())
for _ in range(q):
	(b, c) = map(int, input().split())
	s = s - b * B.get(b, 0) + c * B.get(b, 0)
	B[c] = B.get(c, 0) + B.get(b, 0)
	B[b] = 0
	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])


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)


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())
SUM = 0
map = [0] * 100001
for i in [int(i) for i in input().split()]:
	SUM += i
	map[i] += 1
Q = int(input())
for i in range(Q):
	[b, c] = [int(i) for i in input().split()]
	SUM += (c - b) * map[b]
	print(SUM)
	map[c] += map[b]
	map[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 math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, bisect
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()
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 = list(map(int, input().split()))
q = int(input())
temp = sum(a)
import collections
ca = collections.Counter(a)
for i in range(q):
	(b, c) = map(int, input().split())
	ba = ca[b]
	temp = temp - b * ba + c * ba
	print(temp)
	ca[b] = ca[b] - ba
	ca[c] = ca[c] + ba


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


N = int(input())
A = [int(a) for a in input().split()]
X = [0] * 100001
S = sum(A)
for a in A:
	X[a] += 1
for _ in range(int(input())):
	(b, c) = map(int, input().split())
	S += (c - b) * X[b]
	X[c] += X[b]
	X[b] = 0
	print(S)


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


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


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


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


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


import sys
input = sys.stdin.readline
N = int(input())
L = list(map(int, input().split()))
Q = int(input())
R = [0] * 10 ** 5
for i in L:
	R[i - 1] += 1
ans = sum(L)
for i in range(Q):
	(a, b) = map(int, input().split())
	R[b - 1] += R[a - 1]
	ans += (b - a) * R[a - 1]
	R[a - 1] = 0
	print(ans)


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
arg = [0] * 100005
for a in A:
	arg[a] += 1
ans = sum(A)
for _ in range(Q):
	(a, b) = map(int, input().split())
	ans += arg[a] * (b - a)
	print(ans)
	arg[b] += arg[a]
	arg[a] = 0


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


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()))
s = sum(A)
Q = int(input())
d = collections.Counter(A)
for i in range(Q):
	(B, C) = map(int, input().split())
	s += d[B] * C - d[B] * B
	print(s)
	if C in d:
		d[C] += d[B]
	else:
		d[C] = d[B]
	d[B] = 0


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


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


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


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()))
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)


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())
cnt = 0
l = [0] * (10 ** 5 + 1)
for v in A:
	l[v] += 1
	cnt += v
for i in range(Q):
	(b, c) = map(int, input().split())
	cnt += (c - b) * l[b]
	print(cnt)
	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


from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
S = sum(A)
Q = int(input())
for _ in range(Q):
	(x, y) = map(int, input().split())
	S += c[x] * (y - x)
	print(S)
	c[y] += c[x]
	c[x] = 0


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)


N = int(input())
A = [int(x) for x in input().split()]
S = sum(A)
D = [0] * (10 ** 5 + 1)
for a in A:
	D[a] += 1
Q = int(input())
for i in range(Q):
	(b, c) = map(int, input().split())
	S -= D[b] * b
	S += D[b] * c
	D[c] += D[b]
	D[b] = 0
	print(S)


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


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)


N = int(input())
A = list(map(int, input().split()))
dp = [0] * (10 ** 5 + 1)
cnt = 0
Q = int(input())
for a in A:
	cnt += a
	dp[a] += 1
for i in range(Q):
	(b, c) = map(int, input().split())
	a = dp[b]
	cnt += (c - b) * a
	dp[c] += a
	dp[b] = 0
	print(cnt)


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())
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())
Ax = map(int, input().split())
Q = int(input())
t = [0] * (10 ** 5 + 1)
s = 0
for a in Ax:
	t[a] += 1
	s += a
for _ in range(Q):
	(b, c) = map(int, input().split())
	s += t[b] * c
	s -= t[b] * b
	t[c] += t[b]
	t[b] = 0
	print(s)


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


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