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):
from collections import Counter

n = int(input())
A = [int(x) for x in input().split()]
BC = [(int(x) for x in input().split()) for _ in range(int(input()))]
ans = sum(A)
cnts = Counter(A)
for b, c in BC:
    b_num = cnts[b]
    c_num = cnts[c]
    ans += (c - b) * cnts[b]
    print(ans)
    cnts[c] = cnts[c] + cnts[b]
    cnts[b] = 0

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    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 = map(int, input().split())
        x = C[b]
        ans += (c-b) * x
        C[b] = 0
        C[c] += x
        print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
L = [int(i) for i in input().split()]
A = [0]*(1+(10**5))
num = 0
for l in L:
    A[l] += 1
    num += l
Q = int(input())
for _ in range(Q):
    B,C =[int(i) for i in input().split()]
    num += (C-B)*A[B]
    A[C] += A[B]
    A[B] = 0
    print(num)

Solution (Python):
from collections import Counter

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

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

sum_a = sum(A)
c = Counter(A)
for i in range(q):
    b_num = c[B[i]]
    diff = (C[i] - B[i]) * b_num
    c[C[i]] = c[C[i]] + c[B[i]]
    c[B[i]] = 0
    sum_a = sum_a + diff
    print(sum_a)

Solution (Python):
from collections import Counter


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

Solution (Python):
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

Solution (Python):
from collections import Counter
N = int(input())
List = list(map(str, input().split()))
Q = int(input())
x = [0] * Q
y = [0] * Q
for i in range(Q):
    x[i], y[i] = map(str, input().split())
    y[i]=int(y[i])

wa=0
for i in List:
    wa+=int(i)
c =Counter(List)
for i in range(Q):
    num=c['{}'.format(x[i])]
    wa+=num*(y[i]-int(x[i]))
    c['{}'.format(x[i])]=0
    c['{}'.format(str(y[i]))]+=num
    print(wa)

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

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

main()

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
query = [list(map(int, input().split())) for i in range(Q)]

Asum = sum(A)
Adict = Counter(A)

for q in query:
  before = q[0]
  after = q[1]
  if before in Adict:
    Asum += (after-before) * Adict[before]
    if after in Adict:
      Adict[after] += Adict.pop(before)
    else:
      Adict[after] = Adict.pop(before)
  print(Asum)

Solution (Python):
n=int(input())
a=[int(x) for x in input().split()]
s=sum(a)
q=int(input())
b=[]
c=[]
for i in range(q):
    xb,xc=map(int,input().split())
    b.append(xb)
    c.append(xc)
r=[0]*1000001
for i in a:
    r[i]+=1
for i in range(q):
    y=r[b[i]]
    r[b[i]]=0
    r[c[i]]+=y
    s+=y*(c[i]-b[i])
    print(s)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

from collections import Counter
cnt = Counter(a)

ans = [sum(a)]

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
d = dict()
total = sum(A)
for a in A:
    d[a] = d.get(a, 0) + 1
Q = int(input())
res = [-1] * Q
for i in range(Q):
    b, c = map(int, input().split())
    cnt = d.get(b, 0)
    total -= (b - c) * cnt
    d[c] = d.get(c, 0) + cnt
    d[b] = 0
    res[i] = total
print(*res, sep='\n')

Solution (Python):
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)

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

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

for i in range(N):
  a = List[i]
  arr[a] += 1
  S += int(a)
  
for i in range(Q):
  a,b = map(int,input().split())
  aa = arr[a]
  arr[b] += aa
  arr[a] = 0
  S += int ( (b-a) * aa )
  print(int(S))

Solution (Python):
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    cA = defaultdict(int)
    sv = sum(A)
    for a in A:
        cA[a] += 1
    Q = int(input())
    for _ in range(Q):
        bi, ci = map(int, input().split())
        sv -= cA[bi] * bi
        sv += cA[bi] * ci
        cA[ci] += cA[bi]
        del cA[bi]
        
        print(sv)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
s = 0
A2 = {}
for i in range(N):
  s += A[i]
  k = str(A[i])
  if k not in A2:
    A2[k] = 1
  else:
    A2[k] += 1

for i in range(Q):
  b,c = map(str,input().split())
  if b in A2:
    l = A2[b]
    s += l*(int(c)-int(b))
    print(s)
    A2.pop(b)
    if c in A2:
      A2[c] += l
    else:
      A2[c] = l
  else:
    print(s)

Solution (Python):
import sys
from collections import Counter
def input():
    return sys.stdin.readline()[:-1]
def main():
    N = int(input())
    A = list(map(int,input().split()))
    D = [0]*100001
    for e in A:
        D[e] += 1
    Q = int(input())
    ans = sum(A)
    for _ in range(Q):
        B, C = map(int,input().split())
        if D[B] > 0:
            t = D[B]
            D[B] = 0
            D[C] += t
            ans += (C-B)*t
        print(ans)
if __name__ == '__main__':
    main()

Solution (Python):

def main():
    import sys
    imput = sys.stdin.readline
    
    
    
    from collections import Counter
    N = int(input())
    A = list(map(int, input().split()))
    D = Counter(A)
    S = sum(A)
    Q = int(input())
    B = [0] * Q
    C = [0] * Q
    for i in range(Q):
        b, c = map(int, input().split())
        x = D[b]
        y = D[c]
        temp = -x * b + x * c
        print(S+temp)
        S += temp
        D[b] = 0
        D[c] += x  

    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
dic = {}
list = [int(i) for i in input().split()]
for i in list:
    if i not in dic:
        dic[i] = 0
    dic[i] += 1
q = int(input())
ans = sum(list)
for j in range(q):
    b,c = (int(x) for x in input().split())
    if b not in dic:
        print(ans)
        continue
    else:
        if c not in dic:
            dic[c] = dic[b]
        else:
            dic[c] += dic[b]
    ans = ans + (c-b)*dic[b]
    dic[b] = 0
    print(ans)

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

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

accum = 0
for value in acollection:
    dp[value] = dp[value] + 1
    accum += value

q = int(input())

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

    changingNum = dp[b]
    diff = c * changingNum - b * changingNum
    accum += diff
    print(accum)

    dp[c] = dp[c] + changingNum
    dp[b] = 0

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

asum = sum(a)
acount = []
for i in range(10**5):
    acount.append(0)
for i in a:
    acount[i-1] += 1

q = int(input())

ans = []
for i in range(q):
    b,c = map(int, input().split())
    if acount[b-1] > 0:
        asum = asum - b * acount[b-1]
        acount[c-1] += acount[b-1]
        asum = asum + c * acount[b-1]
        acount[b-1] = 0
    ans.append(asum)
for i in ans:
    print(i)

Solution (Python):
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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc = [list(map(int,input().split())) for _ in range(q)]
N=[0]*(10**5+1)
for i in a:
    N[i]+=1
ans = 0
for i,x in enumerate(N):
    if x != 0:
        ans += i*x 

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

Solution (Python):
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)

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

c = collections.Counter(a)
ans = sum(a)
for i in range(q):
  add = bc[i][1] - bc[i][0]
  cnt = c[bc[i][0]]
  
  ans += cnt * add
  c[bc[i][1]] += c[bc[i][0]]
  c[bc[i][0]] = 0;
  
  print(ans)

Solution (Python):
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]
  d[b] = 0

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

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

dic = {}
check = [0]*100000
for i in A:
    if check[i-1] == 0:
        dic[i] = 1
        check[i-1] = 1
    else:
        dic[i] += 1
ans = 0
for i in dic.keys():
    ans += i*dic[i]

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

    else:
        print(ans)

Solution (Python):
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)

Solution (Python):
from collections import defaultdict
n = int(input())
As =list(map(int,input().split(" ")))
q = int(input())
s = sum(As)
di = defaultdict(lambda :0)
for a in As:
    if a not in di:
        di[a] = 1
    else:
        di[a] += 1
anss = []
for _ in range(q):
    b,c =list(map(int,input().split(" ")))
    
    diff1 = di[b] * b
    temp = di[b]
    di[b] = 0
    diff2 = temp * c
    di[c] += temp
    s -= diff1
    s += diff2
    anss.append(s)
print("\n".join(list(map(str,anss))))

Solution (Python):
n=int(input())
x=[int(x) for x in input().split()]
d={}
for i in x:
	if i not in d:
		d[i]=1
	else:d[i]+=1
s=sum(x)
q=int(input())
for i in range(q):
	b,c=map(int,input().split())
	if b in d:
		zz=d[b]
		d[b]=0
		if c not in d:
			d[c]=zz
		else:d[c]+=zz
		s-=b*zz
		s+=c*zz
		print(s)
	else:
		print(s)

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

for i in A:
  if i in D:
    D[i] += 1
  else:
    D[i] = 1
s = 0
for j in D:
  s += j * D[j]
for i in range(Q):
  bi, ci = map(int, input().split())
  if bi in D:
    if ci in D:
      D[ci] += D[bi]
    else:
      D[ci] = D[bi]
    s += (ci - bi) * D[bi]
    del D[bi]
  print(s)

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

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

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

for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    before = 0
    after = 0
    if l[b] != 0:
        before = l[b]*b + l[c]*c
        l[c] += l[b]
        l[b] = 0
        after = l[b]*b + l[c]*c
    ans = ans-(before-after)
    print(ans)

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

s=0
d={}
for i,Ai in enumerate(As):
    s+=Ai
    if Ai in d:
        d[Ai]+=1
    else:
        d[Ai]=1

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

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

A_dict = {}
for a in input().split():
    a = int(a)
    if a in A_dict:
        A_dict[a] += 1
    else:
        A_dict[a] = 1

total = 0
for key in A_dict:
    total += key * A_dict[key]

Q = int(input())

for _ in range(Q):
    B, C = [int(n) for n in input().split()]
    if B in A_dict:
        val_B = A_dict.pop(B)
        if C in A_dict:
            A_dict[C] += val_B
        else:
            A_dict[C] = val_B
        total += (C - B) * val_B
    print(total)

Solution (Python):
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

Solution (Python):
from collections import Counter

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

    Co = Counter(A)
    
    SUM = sum(A)
    for _ in range(Q):
        b,c = map(int,input().split())
        SUM += (c-b) * Co[b]
        Co[c] += Co[b]
        Co[b] = 0
        
        print(SUM)



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

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

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

for i in range(Q):
    b = BC[i][0]
    c = BC[i][1]
    m = L[b]
    total -= b * m
    total += c * m
    L[b] = 0
    L[c] = L[c] + m
    print(total)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
y = [0]*10**5
for i in li:
    y[i-1] += 1
q = int(input())
bc = [list(map(int,input().split())) for i in range(q)]
x = sum(li)

for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    d = y[b-1]
    e = y[c-1]
    y[b-1] = 0
    y[c-1] = d+e
    x = x + (c-b) * d
    print(x)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
cnt = defaultdict(int)
S = sum(A)
for a in A:
    cnt[a] += 1

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    num_of_b = cnt[b]
    S += num_of_b * c - num_of_b * b
    cnt[c] += num_of_b
    cnt[b] -= num_of_b

    print(S)

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

from collections import Counter
ans = sum(a.copy())
a = Counter(a)

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

Solution (Python):
def count(l):
    res = {}
    for d in l:
        if d in res:
            res[d] += 1
        else:
            res[d] = 1
    return res
n = int(input())
data = list(map(int, input().split()))
count = count(data)
ans = sum(data)
q = int(input())
answers = []
for _ in range(q):
    b, c = map(int, input().split())
    if b in count:
        num = count[b]
        ans += c*num - b*num
        if c in count:
            count[c] += num
        else:
            count[c] = num
        count[b] = 0
    answers.append(ans)
for a in answers:
    print(a)

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_array = [0 for _ in range(10**5 + 1)]
result = 0

for num in a:
  num_array[num] += 1
  result += num
  
for array in bc:
  dif = array[1] - array[0]
  result += dif * num_array[array[0]]
  num_array[array[1]] += num_array[array[0]]
  num_array[array[0]] = 0
  print(result)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
k=10**5+1
C=[0]*k
for i in range(N):
    C[A[i]]+=1
s=0
for i in range(k):
    s+=i*C[i]

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

Solution (Python):


import sys; import math; from collections import *


def get_ints(): return map(int, input().split())
def get_list(): return list(get_ints())
def printspx(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")

UGLYMOD = int(1e9+7); SEXYMOD = 998244353; MAXN = int(1e5)




n =  int(input())
li = get_list()
d = Counter(li)
s = sum(li)
q = int(input())
for _ in range(q):
    b, c = get_ints()
    item = 0
    if b in d:
        item = d[b]
        del d[b]
    s -= item * b
    if c in d:
        d[c] += item
    else:
        d[c] = item
    s += item * c
    print(s)




'''
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
THE LOGIC AND APPROACH IS MINE @luctivud ( UDIT GUPTA )
Link may be copy-pasted here if it's taken from other source.
DO NOT PLAGIARISE.
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
'''

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

Solution (Python):
n = int(input().strip())
a = list(map(int, input().split()))
q = int(input().strip())
bcs = [tuple(map(int, input().split())) for _ in range(q)]

dic = {}
for elem in a:
    if dic.get(elem):
        dic[elem] += 1
    else:
        dic[elem] = 1

ans = sum(a)

for b, c in bcs:
    if dic.get(b):
        diff = (c - b) * dic[b]
        if dic.get(c):
            dic[c] += dic[b]
        else:
            dic[c] = dic[b]
        dic[b] = 0
        ans += diff
    print(ans)

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

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


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


co = dict(Counter(a))

s = sum(a)

for b, c in bc:
    if co.get(b) == None:
        print(s)
    else:
        s = s + co[b] * (c - b)
        print(s)
        if co.get(c) == None:
            co[c] = co[b]
        else:
            co[c] += co[b]
        co[b] = 0

Solution (Python):

import sys
input = sys.stdin.readline
from collections import Counter

n = int(input())
a = [int(item) for item in input().split()]
total = sum(a)
cnt = Counter(a)

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

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

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

Solution (Python):
import numpy as np

n = int(input())
a = input().split()
np_a=np.array(a, dtype=int)

sum=np.int(sum(np_a))
count=np.zeros(100001, dtype='int64')
for i in range(0,n):
  count[np_a[i]]+= 1
  
q = int(input())
for i in range(0,q):
  b, c= map(int, input().split())
  sum=sum+(c-b)*count[b]
  count[c]=count[c]+count[b]
  count[b]=0
  
  print (sum)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
C = Counter(A)
ans = sum(A)

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

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

ans = sum(A)
d = {}
for a in A:
    try:
        d[a] += 1
    except:
        d[a] = 1

for b, c in BC:
    try:
        pre_c = d[b]
        d[b] = 0
    except:
        print(ans)
        continue

    ans += (c - b) * pre_c
    try:
        d[c] += pre_c
    except:
        d[c] = pre_c
    print(ans)

Solution (Python):
from collections import Counter
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
suma = sum(A)
counter = Counter(A)
for i in range(Q):
  b,c = (int(x) for x in input().split())
  suma = counter[b]*(c-b) + suma
  print(suma)
  if b in counter:
    if c in counter:
      counter[c] += counter[b]
      counter[b] = 0
    else:
      counter[c] = counter[b]
      counter[b] = 0

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
q=int(input())
go=0
jisho={}
for x in a:
    go+=x
    if x in jisho:
        jisho[x]+=1
    else:
        jisho[x]=1
for x in range(q):
    b,c=map(int, input().split())
    if b in jisho:
        go=go+(c-b)*jisho[b]
        if c in jisho:
            jisho[c]+=jisho[b]
        else:
            jisho[c]=jisho[b]
        jisho[b]=0
    print(go)

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

'''

'''

n = int(input())
arr = list(map(int, input().split()))
freq = [0] * (10**5 + 1)
cur_sum = 0
for v in arr:
    cur_sum += v
    freq[v] += 1

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

    if freq[b] > 0:
        cur_sum += (freq[b] * c - freq[b] * b)
        freq[c] += freq[b]
        freq[b] = 0
        print(cur_sum)
    else:
        print(cur_sum)

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


main()

Solution (Python):
from collections import Counter
N = int(input())
A_list = list(map(int, input().split()))
Q = int(input())
BC_list = [list(map(int, input().split())) for i in range(Q)]

count_dict = dict(Counter(A_list))
_sum = 0
for i, a in count_dict.items():
    _sum += i * a

for b, c in BC_list:
    if b in count_dict:
        count_b = count_dict[b]
        if c in count_dict:
            count_dict[c] += count_dict[b]
        else:
            count_dict[c] = count_dict[b]
        count_dict.pop(b)
        _sum -= b * count_b
        _sum += c * count_b
    print(_sum)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
dic = {}
for i in range(n):
    ai = a[i]
    if ai in dic:
        dic[ai] += 1
    else:
        dic[ai] = 1
s = sum(a)
q = int(input())
ans = []
for i in range(q):
    bi, ci = map(int, input().split())
    if bi in dic:
        num = dic[bi]
        s += (ci-bi)*num
        ans.append(s)
        if ci in dic:
            dic[ci] += num
        else:
            dic[ci] = num
        dic.pop(bi)
    else:
        ans.append(s)

for i in ans:
    print(i)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = []
c = []
sa = []
for i in range(q):
  B,C = map(int,input().split())
  b.append(B)
  c.append(C)
  sa.append(C-B)
lst = [0]*100000
su = 0
for i in a:
  lst[i-1] += 1
  su += i

for i in range(q):
  su += sa[i]*lst[b[i]-1]
  print(su)
  if lst[b[i]-1] != 0:
    lst[c[i]-1] += lst[b[i]-1]
    lst[b[i]-1] = 0

Solution (Python):
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)]

a_c = Counter(a)
a_sum = sum(a)
for i in range(q):
    if bc[i][0] in a_c:
        a_sum += a_c[bc[i][0]] * (bc[i][1] - bc[i][0])
        if bc[i][1] in a_c:
            a_c[bc[i][1]] += a_c[bc[i][0]]
        else:
            a_c[bc[i][1]] = a_c[bc[i][0]]
        a_c[bc[i][0]] = 0
    else:
        a_sum = a_sum
    print(a_sum)

Solution (Python):



import numpy as np
n = int(input())
a = list(map(int,input().split()))
a = np.array(a)
q = int(input())



import collections
co = collections.Counter(a)
s = 0
for k,v in co.items():
    s+=k*v
for i in range(q):
    b,c = map(int,input().split())
    if b in co.keys():
        s += (c-b)*co[b]
        co[c]+=co[b]
        co.pop(b)
    print(s)

Solution (Python):
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
D = np.zeros((10**5)+1)
S = sum(A)
for i in range(len(A)):
    D[A[i]] += 1
Q = int(input())
for _ in range(Q):
    b, c = list(map(int, input().split()))
    S += D[b]*(c-b)
    D[c] += D[b]
    D[b] = 0
    print(int(S))

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

Solution (Python):
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

Solution (Python):
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]

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

ADic = {}

for A in AN:
    if A in ADic:
        ADic[A] += 1
    else:
        ADic[A] = 1

sum = 0
for key, value in ADic.items():
    sum += key * value

for BC in BCList:
    diff = 0
    B, C = map(int, BC.split())
    if B in ADic:
        diff = (C - B) * ADic[B]
        if C in ADic:
            ADic[C] += ADic[B]
            ADic[B] = 0
        else:
            ADic[C] = ADic[B]
            ADic[B] = 0
    sum += diff
    print(sum)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
count = [0]*(10**5 +1)
for a in A:
    count[a] += 1
    
ans = 0
for i in range(10**5 +1):
    if count[i]==0:
        continue
    else:
        ans += i*count[i]

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dic = {i: i for i in range(1, 10**5 + 1)}
total = sum(A)
from collections import Counter
counter = dict(Counter(A))
for i in range(1, 1+10**5):
    if i not in counter:
        counter[i] = 0
for i in range(Q):
    b, c = map(int, input().split())
    dic[b] = c
    total += counter[b] * (c - b)
    counter[c] += counter[b]
    counter[b] = 0
    print(total)

Solution (Python):

def resolve():
    N = int(input())
    A = [0] * (10 ** 5 + 1)
    ans = 0
    for a in input().split():
        a = int(a)
        A[a] += 1
        ans += a

    Q = int(input())
    for _ in range(Q):
        a, b = map(int, input().split())
        l_a = A[a]
        ans -= l_a * a
        A[b] += l_a
        A[a] -= l_a
        ans += l_a * b
        print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):
import collections

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

counter = collections.Counter(A)
s = sum(A)

for i in range(Q):
    if B[i] not in counter:
        print(s)
        continue
    s += counter[B[i]] * (C[i] - B[i])
    counter[C[i]] += counter[B[i]]
    del counter[B[i]]
    print(s)

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

dic = {}
m = 10 ** 5 + 10
for i in range(1, m + 1):
    dic[i] = 0

for i in A:
    dic[i] += 1

ans = sum(A)

for i in range(Q):
    ans = ans + (-L[i][0] + L[i][1]) * dic[L[i][0]]
    dic[L[i][1]] += dic[L[i][0]]
    dic[L[i][0]] = 0
    print(ans)

Solution (Python):

import collections

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