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 collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for i in range(Q)]
 
ans = sum(A)
cnt = collections.Counter(A)
 
dp = [0]*(10**5+1)
 
for i in cnt.items():
    dp[i[0]] = i[1]
 
for i in range(Q):
    ans += (BC[i][1] - BC[i][0]) * dp[BC[i][0]]
    dp[BC[i][1]] += dp[BC[i][0]]
    dp[BC[i][0]] = 0
    print(ans)

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

d = {}
t = 0
for i in a:
    if not i in d.keys():
        d[i] = 1
    else:
        d[i] += 1
    t += int(i)

q = int(input())

for _ in range(q):
    b, c = map(str, input().split())
    diff = 0
    if b in d.keys() and c in d.keys():
        diff = (int(c) - int(b)) * d[b]
        d[c] += d[b]
        d.pop(b)
    elif b in d.keys():
        diff = int(c) * d[b] - int(b) * d[b]
        d[c] = d[b]
        d.pop(b)
    t += diff
    print(t)

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

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

dict = {}

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

s = sum(a)

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

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

for _ in range(Q):
    bi, ci = list(map(int, input().split()))
    B.append(bi)
    C.append(ci)

dic = {}
for i in A:
    dic[i] = dic.get(i,0) + 1

sum = sum(A)

for i in range(Q):
    sum += dic.get(B[i], 0) * (C[i] -B[i])
    dic[C[i]] = dic.get(C[i], 0) + dic.get(B[i], 0)
    dic[B[i]] = dic.get(B[i], 0)
    dic[B[i]] = 0
    print(sum)

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

d = {}
t = 0
for i in a:
    if not i in d.keys():
        d[i] = 1
    else:
        d[i] += 1
    t += int(i)

q = int(input())

for _ in range(q):
    b, c = map(str, input().split())
    diff = 0
    if b in d.keys() and c in d.keys():
        diff = (int(c) - int(b)) * d[b]
        d[c] += d[b]
        d.pop(b)
    elif b in d.keys():
        diff = int(c) * d[b] - int(b) * d[b]
        d[c] = d[b]
        d.pop(b)
    t += diff
    print(t)

Solution (Python):
from collections import Counter
N = int(input())
A = map(int, input().split())
Q = int(input())
ops = []
for _ in range(Q):
  b, c = map(int, input().split())
  ops.append((b,c))

cnt = Counter(A)
s = sum(k*v for k, v in cnt.items())
for b, c in ops:
  s -= b * cnt[b]
  s += c * cnt[b]
  cnt[c] += cnt[b]
  cnt[b] = 0
  print(s)

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

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

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

cnt = [0] * 100001
s = 0

for x in a:
    cnt[x] += 1
    s += x

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

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))
sum = 0
for x in arr:
	sum = sum+x
q = int(input())
newarr = [0]*10**6
for x in arr:
	newarr[x]=newarr[x]+1
for t in range(q):
	b, c = map(int, input().split())
	sum = sum - b*newarr[b]
	sum = sum + c*newarr[b]
	newarr[c] = newarr[c] + newarr[b]
	newarr[b] = 0
	print(sum)

Solution (Python):
import collections
N = int(input())
D = [0]*100001
A = list(map(int, input().split()))
a = sum(A)
for j in A:
    D[j] += 1
Q = int(input())
start = sum(A)
for i in range(Q):
    x,y=[int(i) for i in input().split()]
    a += D[x]*(y-x)
    print(a)
    D[y] +=D[x]
    D[x] = 0

Solution (Python):
def d_replacing():
    from collections import Counter
    N = int(input())
    A = [int(i) for i in input().split()]
    Q = int(input())
    Queries = [[int(i) for i in input().split()] for j in range(Q)]

    total = sum(A)
    sequence = Counter(A)

    ans = []
    for b, c in Queries:
        total += (c - b) * sequence[b]  
        ans.append(total)
        sequence[c] += sequence[b]  
        del sequence[b]  
    return ' '.join(map(str, ans))

print(d_replacing())

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

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

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

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

a_count = [0 for i in range(mx+1)]
for x in a:
    a_count[x] += 1

ans = sum(a)
for i in range(q):
    b,c = bc[i]
    
    ans += (c - b) * a_count[b]
    
    a_count[c] += a_count[b]
    a_count[b] = 0
    print(ans)

Solution (Python):
n = int(input().rstrip())

line = input().rstrip().split(' ')

a = {}
ans = 0
for i in range(n):
    n = int(line[i])
    ans += n
    if n in a:
        a[n] += 1
    else:
        a[n] = 1

q = int(input().rstrip())

for i in range(q):
    line = input().rstrip().split(' ')
    b = int(line[0])
    c = int(line[1])

    if b in a:
        ab = a[b]
        a[b] = 0
        if c in a:
            ac = a[c]
            a[c] += ab
        else:
            a[c] = ab
    else:
        ab = 0

    ans -= ab * b
    ans += ab * c

    print(ans)

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

a_sum = sum(a)

dic = {}
for i in range(n):
    if str(a[i]) not in dic:
        dic[str(a[i])] = 1
    else:
        dic[str(a[i])] += 1

for i in range(q):
    ans = 0
    if str(bc[i][0]) in dic.keys():
        buf = dic[str(bc[i][0])]
        if str(bc[i][1]) in dic.keys():
            dic[str(bc[i][1])] += buf
        else:
            dic[str(bc[i][1])] = buf

        a_sum += buf*(-bc[i][0] + bc[i][1])
        dic[str(bc[i][0])] = 0


    print(a_sum)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
dic = dict()
 
for i in A:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
 
ans = sum(A)
 
for i in range(Q):
    b, c = list(map(int, input().split()))
    x = 0
    if b in dic:
        x = dic.pop(b)
        if c in dic:    
            dic[c] += x
        else:
            dic[c] = x
    ans += x * (c-b)
    print(ans)

Solution (Python):
import math

n = int(input())
aL = list(map(int, input().split(" ")))
q = int(input())
bcL = [list(map(int, input().split(" "))) for _ in range(q)]

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

for a in aL:
    sums[a] += 1

_sum = 0
for i, a in enumerate(sums):
    _sum += i * a

for b, c in bcL:
    _sum += (c - b) * sums[b]

    sums[c] += sums[b]
    sums[b] = 0
    print(_sum)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A_cnt = [0] * 100001
A_sum = 0


for a in A:
    A_cnt[a]+=1
    
    A_sum+=a

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

Solution (Python):
n = int(input())
A = [*map(int, input().split())]
q = int(input())
BC = [[*map(int, input().split())] for _ in range(q)]

s = 0
C = {}
for a in A:
    s += a
    if a in C: C[a] += 1
    else: C[a] = 1

for b, c in BC:
    if not b in C: C[b] = 0
    if not c in C: C[c] = 0
    s += (c - b) * C[b]
    C[c] += C[b]
    C[b] = 0
    print(s)

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

num=[0]*(1+10**5)
total=0
for i in range(N):
	total+=A[i]
for j in range(N):
	num[A[j]]+=1

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

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

listB=[] 
while True:
    try:
        listB.append(list(map(int,input().split())))

    except:
        break;
        
sum=sum(listA)
numlist=[0]*(10**5+1)
for i in listA:
  numlist[i]+=1
  
for i in range(Q):
  B=listB[i][0]
  C=listB[i][1]
  if numlist[B]!=0:
    sum=sum+(C-B)*numlist[B]
    
    numlist[C]+=numlist[B]
    numlist[B]=0
    
  print(sum)

Solution (Python):
from collections import Counter

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

cnt = [0 for _ in range(100005)]
for i in range(N):
    cnt[A[i]] += 1

sumA = sum(A)



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

Solution (Python):
hyou = [0]*100001

N =  int(input())
a_data = [int(x) for x in input().split()]
Q = int(input())
b_data = [[int(x) for x in input().split()] for _ in range(Q)]


for x in a_data:
    hyou[x] += 1

total = 0
for i,x in enumerate(hyou):
    total += i*x


for before_value,after_value in b_data:
    total += (after_value - before_value) * hyou[before_value] 
    
    hyou[after_value] = hyou[before_value] + hyou[after_value] 
    hyou[before_value] = 0

    
    print(total)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
for i in A:
    AA[i]+=1 


sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):



    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
    AA[(B[i][0])] = 0
    print(sum)

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


A = list(map(int,input().split()))
ok = [0]*pow(10,6)
Q = int(input())

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

for i in range(N):
    ok[A[i]]+=1
memo1=0
memo2=0

S = sum(A)
for i in range(Q):

    S+=ok[li[i][0]]*(li[i][1]-li[i][0])
    print(S)
    a = ok[li[i][0]]
    ok[li[i][1]]+=a
    ok[li[i][0]]=0

Solution (Python):
n, *r = map(int, open(0).read().split())
a = r[:n]
q = r[n]
bc = r[n+1:]

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1
ret = sum(a)
for i in range(0, min(q*2, len(bc)), 2):
    b = bc[i]
    c = bc[i+1]
    if b in d:
        v = d.pop(b)
        if c in d:
            d[c] += v
        else:
            d[c] = v
        ret += c * v - b * v
    
    print(ret)

Solution (Python):
n=int(input())
*a,=map(int, input().split())
from collections import Counter
ca=Counter(a)
q=int(input())
sm=sum(a)
for _ in range(q):
    bi,ci=map(int, input().split())
    if ci in ca:
        if bi in ca:
            sm+=(ci-bi)*ca[bi]
            ca[ci]+=ca[bi]
            del ca[bi]
    else:
        if bi in ca:
            sm+=(ci-bi)*ca[bi]
            ca[ci]=ca[bi]
            del ca[bi]
    print(sm)

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

cnt = [0]*(10**5+1)
ans = sum(a)

for aa in a:
  cnt[aa] += 1

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

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

Solution (Python):
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
d = defaultdict(int)
ans = 0
for i in A:
    ans += i
    d[i] += 1

T = []
result = []
for i in range(Q):
    b, c = map(int, input().split())
    before = b*d[b]
    after = c*d[b]
    ans += (after - before)
    result.append(ans)
    d[c] += d[b]
    d[b] = 0

for i in result:
    print(i)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
S = sum(A)
count = [0]*(10**5+1)
for i in range(len(A)):
  count[A[i]] += 1

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

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

num=[0]*10**5 
a=sum(A)

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

for _ in range(Q):
  b,c=map(int, input().split())
  
  a=a+(-num[b-1]*b+c*num[b-1])
  num[c-1]+=num[b-1]
  num[b-1]-=num[b-1]
  print(a)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A_cnt = [0] * 100001
A_sum = 0


for a in A:
    A_cnt[a]+=1
    
    A_sum+=a

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

Solution (Python):


import re
import math
from collections import defaultdict
from collections import deque
import collections
from fractions import Fraction
import bisect
from queue import PriorityQueue
import itertools
from itertools import accumulate
from copy import deepcopy
from collections import Counter
import heapq
import random
import time
import os
import sys
import datetime
from functools import lru_cache
from functools import reduce
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):
    if b:print("yes")
    else:print("no")
def Yn(b):
    if b:print("Yes")
    else:print("No")
def YN(b):
    if b:print("YES")
    else:print("NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(bbool,ret1,ret2):
    if bbool:return ret1
    else:return  ret2
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def pm(x):
    return x//abs(x)
def inputintlist(): 
    return list(map(int,input().split()))
def inputfloatlist(): 
    return list(map(float,input().split()))
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):
    return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):
    return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow






N=int(input())
A=inputintlist()
S=sum(A)
count=collections.Counter(A)
for q in range(int(input())):
    b,c=inputintlist()
    S-=count[b]*b
    S+=count[b]*c
    count[c]+=count[b]
    count[b]=0
    print(S)

Solution (Python):

n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []

def solve():
    for _ in range(q):
        bi, ci = list(map(int, input().split()))
        b.append(bi)
        c.append(ci)

    dic = {}
    for aa in a:
        dic[aa] = dic.get(aa, 0) + 1

    s = sum(a)
    for i in range(q):
        if b[i] not in dic:
            print (str(s))
            continue
        s += (c[i]-b[i]) * dic[b[i]]
        dic[c[i]] = dic.get(c[i], 0) + dic[b[i]]
        del dic[b[i]]
        print(s)

def main():
    solve()

if __name__ == "__main__":
    main()

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

ope = []

l = [0] * 100001
for i in A:
    l[i] += 1

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

A_sum = sum(A)

for x in ope:
    A_sum = A_sum + (x[1]-x[0])*l[x[0]]
    l[x[1]] += l[x[0]]
    l[x[0]] = 0
    print(A_sum)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
D = {}
res = 0
for a in A:
    if a in D.keys():
        D[a] += 1
    else:
        D[a] = 1
    res += a
Q = int(input())
BC = []
for _ in range(Q):
    b,c = map(int,input().split())
    BC.append([b,c])

for bc in BC:
    if bc[0] in D.keys():
        res += D[bc[0]]*(bc[1]-bc[0])
        if bc[1] in D.keys():
            D[bc[1]] += D[bc[0]]
        else:
            D[bc[1]] = D[bc[0]]
        D[bc[0]] = 0
    print(res)

Solution (Python):

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    b = []
    c = []

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

    dic = {}
    for aa in a:
        dic[aa] = dic.get(aa, 0) + 1

    s = sum(a)
    for i in range(q):
        if b[i] not in dic:
            print (str(s))
            continue
        s += (c[i]-b[i]) * dic[b[i]]
        dic[c[i]] = dic.get(c[i], 0) + dic[b[i]]
        del dic[b[i]]
        print(s)

def main():
    solve()

if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
    if a[i] in dic:
        dic[a[i]]+=1
    else:
        dic.setdefault(a[i],1)
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    if b not in dic:
        dic.setdefault(b,0)
    elif c not in dic:
        dic.setdefault(c,dic[b])
    else:
        dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

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

dp=[0 for n in range(100000+1)]
for n in range(len(A)):
    dp[A[n]-1]+=1

init=0
for n in range(1,len(dp)):
    init+=n*dp[n-1]

ans=init
for n in range(Q):
    B,C=map(int,input().strip().split())
    ans=ans-dp[B-1]*B+dp[B-1]*C
    dp[C-1]+=dp[B-1]
    dp[B-1]=0
    print(ans)

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

s = sum(a)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
d={}
ans=0
for key in a:
    try:
        d[key]+=1
    except:
        d[key]=1
    ans+=key

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

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

adic=dict()

for ai in a:
    if ai in adic:
        adic[ai]+=1
    else:
        adic[ai]=1
        
q=int(input())
b=[0]*(q)
c=[0]*(q)
bc=[[0]*2 for i in range(q)]
for i in range(q):
    bc[i]=map(int,input().split())
    
asum=0
for i,ad in adic.items():
    asum+=i*ad
    

for bi,ci in bc:
    if bi in adic and ci in adic:
        asum-=bi*adic[bi]
        adic[ci]=adic[ci]+adic[bi]
        asum+=ci*adic[bi]
        del adic[bi]
    elif bi in adic and (not ci in adic):
        asum-=bi*adic[bi]
        adic[ci]=adic[bi]
        asum+=ci*adic[bi]
        del adic[bi]

    print(asum)

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

Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    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()))
q = int(input())

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

bc = [input() for _ in range(q)]

for ai in a:
  dp[ai] += 1
ans = 0
for i in range(1,len(dp)):
  ans += dp[i] * i
  
for bci in bc:
  b,c = map(int,bci.split())
  ans += (dp[b] * c) -(dp[b] * b)
  print(ans)
  dp[c] += dp[b] 
  dp[b] = 0

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

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()]
sumA = sum(A)
counter = [0]*(10**5+1)
for i in range(n):
    counter[A[i]] += 1

q = int(input())

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

for b,c in queue:    
    b_cnt = counter[b]
    sumA -= (b*b_cnt)
    counter[b] = 0
    sumA += c*(b_cnt)
    counter[c] += b_cnt
    print(sumA)

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

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

l = [0] * 100001
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)

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

cnt = [0] * (100001)
s = sum(A)
for a in A:
    cnt[a] += 1

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

Solution (Python):
from collections import Counter


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

S = sum(A)
C = Counter(A)

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

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

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

Solution (Python):


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

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

max_i = max(max(A), max(C))

A_N = [0 for _ in range(max_i + 1)]
sum_i = 0

for a in A:
    A_N[a] += a
    sum_i += a

for i in range(Q):
    diff_replace = int(A_N[B[i]] / B[i]) * C[i]
    sum_i += diff_replace - A_N[B[i]]
    
    A_N[C[i]] += diff_replace
    A_N[B[i]] = 0
    
    print(sum_i)

Solution (Python):

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

temp = [0] * (10 ** 5 + 1)
for i in range(N):
    temp[A[i]] += 1
    
s = sum(A)

for i in range(Q):
    num = BandC[i][0]
    num_temp = temp[BandC[i][0]]
    temp[BandC[i][0]] = 0
    temp[BandC[i][1]] += num_temp
    s = s - BandC[i][0] * num_temp + BandC[i][1] * num_temp
    print(s)

Solution (Python):
import sys

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

    answer = sum(A)
    setA = set(A)
    numNumberList = [0] * 100001

    for Ai in A:
        numNumberList[Ai] = numNumberList[Ai] + 1    

    for i in range(Q):
        B, C = list(map(int, sys.stdin.readline().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

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