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 heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
from itertools import accumulate, permutations
import sys
sys.setrecursionlimit(10 ** 7)
import bisect


INF = float("inf")
MOD = 10 ** 9 + 7


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

Q = int(input())

sm = 0
for k, v in D.items():
    sm += k * v


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

Solution (Python):

import collections
n = int(raw_input())
nums = map(int,raw_input().split(' '))
q = int(raw_input())
s = sum(nums)
h = collections.Counter(nums)
for _ in range(q):
	ov,nv = map(int, raw_input().split(' '))
	h[nv] += h[ov]
	s += h[ov] * (nv - ov)
	
	h[ov] = 0
	print s

Solution (Python):
from collections import Counter

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

count_a = Counter(a)
ans = sum(a)

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

Solution (Python):
loop_cnt = 10 ** 5 + 1

N = map(int, input().split())
A = [0] * (loop_cnt + 1)

A_lis = list(map(int, input().split()))
for a in A_lis:
    A[a] += 1

Q = int(input())

ans = 0

for i in range(loop_cnt):
    ans += i * A[i]

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

Solution (Python):

import sys

stdin = sys.stdin
ns = lambda: stdin.readline().rstrip() 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))


def main():
    n = ni()
    a = na()
    nums = [0 for i in range(100001)]
    suma = 0
    for i in a:
        suma += i
        nums[i] += 1
    q = ni()
    for i in range(q):
        b,c = na()
        suma = suma + (c-b)*nums[b]
        nums[c] += nums[b]
        nums[b] = 0
        print(suma)
    return


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split(' ')))
Q = int(input())
Bs = []
for i in range(Q):
    Bs.append(list(map(int, input().split(' '))))
    
    
A_list = [0]*(10**5+1)
for i in A:
    A_list[i] += 1
    
sum_value = sum(A)
for B in Bs:
    b, c = B
    sum_value += A_list[b]*c
    sum_value -= A_list[b]*b
    
    A_list[c] += A_list[b]
    A_list[b] = 0
    print(sum_value)

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

count = [0 for i in range(0,10**5+1,1)]
for i in range(0,N,1):
    count[A[i]]+=1

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

res =[]
for i in range(0,Q,1):
    B,C = map(int,input().split())
    count[C]+=count[B]
    ans -= count[B]*B
    ans += count[B]*C
    count[B]=0
    res.append(ans)

for i in range(len(res)):
    print(res[i])

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

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

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


from collections import defaultdict

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

s = sum(A)

for i in range(Q):
    num = dic[B[i]]
    diff = num * (C[i] - B[i])
    
    s += diff

    dic[C[i]] += dic[B[i]]
    dic[B[i]] = 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)]

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

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

Solution (Python):

import sys
from collections import Counter
sys.setrecursionlimit(10**8)
INF = float("inf")


def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B, C = [0]*Q, [0]*Q
    for i in range(Q):
        B[i], C[i] = map(int, input().split())
    
    

    count = Counter(A)
    tot = sum(A)
    for i in range(Q):
        b = count[B[i]]
        c = count[C[i]]
        count[C[i]] += b
        count[B[i]] = 0
        tot = tot - B[i]*b + C[i]*b
        print(tot)

    return


if __name__ == '__main__':
    main()

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

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

count = [0]*(10**5+1)
for ai in a:
    count[ai] += 1

ans = sum(a)    

q = int(input())
for _ 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):

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

count_list = [0 for i in range(10 ** 5 + 1)]
for i in range(len(A_list)):
    count_list[A_list[i]] += 1

ans = sum(A_list)
for i in range(Q):
    B, C = map(int, input().split())
    ans = ans - count_list[B] * B + count_list[B] * C
    print(ans)
    count_list[C] += count_list[B]
    count_list[B] = 0

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

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

S = sum(A)
C = Counter(A)
for _ in range(int(input())):
	b, c = map(int, input().split())

	S -= C[b] * b
	S += C[b] * c
	C[c] += C[b]
	C[b] = 0

	print(S)

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

Solution (Python):
from collections import defaultdict,deque
from sys import stdin,setrecursionlimit
import heapq,bisect,math,itertools,string,queue,datetime,copy
setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, stdin.readline().split()))

def main():
    N = int(input())
    Als = inpl()
    sm = sum(Als)
    dd = [0 for _ in range(10**5+1)]
    for a in Als:
        dd[a] +=1
    Q = int(input())
    order = []
    for _ in range(Q):
        b,c = inpl()
        order.append((b,c))

    for item in order:
        o1 = item[0]
        o2 = item[1]
        count = dd[o1]
        dif = count*(o2 - o1)
        sm += dif
        print(sm)
        dd[o1] = 0
        dd[o2] += count

main()

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


d = [0]*(10**5+1)
ans = 0
for i in range(N):
  d[A[i]] += 1
  ans += A[i]

for j in range(Q):
  ans = ans - d[B[j][0]]*B[j][0] + d[B[j][0]]*B[j][1]
  d[B[j][1]] += d[B[j][0]]
  d[B[j][0]] = 0
  print(ans)

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

ls = sorted(ls)
ans = sum(ls)
newls = [0]*(100000+1+1)
for p in ls:
  newls[p] += 1
ls = newls

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

Solution (Python):
import sys
from collections import Counter


def input():
    return sys.stdin.readline().strip()


N = int(input())
A = list(map(int, input().split()))
cntr = Counter(A)
Q = int(input())
curr_sum = sum(A)
for _ in range(Q):
    B, C = map(int, input().split())
    if B in cntr:
        curr_sum += (C - B) * cntr[B]
        cntr[C] += cntr[B]
        del cntr[B]
    print(curr_sum)

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

m = {}
sum = 0

for a in A:
  m[a] = m.get(a, 0) + 1
  sum += a

L = []
for _ in range(Q):
  B, C = list(map(int, input().split()))
  if m.get(B, 0) != 0:
    m[C] = m.get(C, 0) + m[B]
    sum += (C - B) * m[B]
    m[B] = 0
  L.append(sum)

for l in L:  
  print(l)

Solution (Python):
import os
import sys
from io import BytesIO, IOBase


BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')





def main():
    n = N()
    arr = RLL()
    dic = defaultdict(int)
    for i in arr: dic[i]+=1
    sm = sum(arr)
    q = N()
    for _ in range(q):
        b, c = RL()
        num = dic[b]
        dic[c]+=dic[b]
        dic[b] = 0
        sm = sm-b*num+c*num
        print(sm)

if __name__ == "__main__":
    main()

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, = map(int, input().split())
A = list(map(int, input().split()))
Q, = map(int, input().split())
B = []
C = []
for _ in range(Q):
    Bi, Ci = map(int, input().split())
    B.append(Bi)
    C.append(Ci)

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

ans = sum(A)

for b, c in zip(B, C):
    keys = e_dict.keys()
    if b in keys:
        poped = e_dict.pop(b)
        gap = c - b
        ans += (gap * poped)

        if c in keys:
            e_dict[c] += poped
        else:
            e_dict[c] = poped

    print(ans)

Solution (Python):
from collections import defaultdict
n=int(input())
arr=list(map(int,input().split()))
d=defaultdict(int)
for i in arr:
	d[i]+=1
total=sum(arr)
ans=""
for i in range(int(input())):
	b,c=map(int,input().split())
	total=total-d[b]*b
	total=total+d[b]*c
	d[c]+=d[b]
	d[b]=0
	ans+=str(total)+"\n"

print(ans)

Solution (Python):
S=lambda:input()
I=lambda:int(input())
L=lambda:list(map(int,input().split()))
LS=lambda:list(map(str,input().split()))

import sys
input=sys.stdin.readline
n=I()
a=L()
suma=sum(a)
dic={}
for i in a:
    if i in dic:
        dic[i]+=1
    else:
        dic[i]=1
q=I()
for i in range(q):
    b,c=L()
    if b not in dic:
        print(suma)
        continue
    if c not in dic:
        suma-=b*dic[b]
        dic[c]=dic[b]
        suma+=c*dic[c]
        del dic[b]
    else:
        suma-=b*dic[b]
        suma+=c*dic[b]
        dic[c]+=dic[b]
        del dic[b]
    print(suma)

Solution (Python):
import sys
from io import StringIO
import unittest
import os
import collections


sys.setrecursionlimit(999999999)



def resolve(test_def_name=""):
    
    
    
    
    
    

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

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

    
    counter = [0 for i in range(100001)]

    for i in a_s:
        counter[i] += 1

    sum_val = sum(a_s)
    for query in query_s:
        sum_val += (query[1] * counter[query[0]]) - (query[0] * counter[query[0]])
        counter[query[1]] += counter[query[0]]
        counter[query[0]] = 0
        print(sum_val)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """4
1 2 3 4
3
1 2
3 4
2 4"""
        output = """11
12
16"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """4
1 1 1 1
3
1 2
2 1
3 5"""
        output = """8
4
4"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """2
1 2
3
1 100
2 100
100 1000"""
        output = """102
200
2000"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

Solution (Python):
from collections import defaultdict

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

A_dict = defaultdict(int)
for a in A_list:
    A_dict[a] += 1

total = sum(A_list)

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

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

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

Solution (Python):


n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
bc = []
d = {}
suma = sum(a)
for nr in a:
    if nr not in d:
        d[nr] = 1
    else:
        d[nr] += 1

for i in range(q):
    bc = [int(i) for i in input().split()]
    b = bc[0]
    c = bc[1]
    if b not in d:
        print(suma)
        continue
    suma = suma + (c - b) * d[b]
    if c not in d:
        d[c] = d[b]
    else:
        d[c] += d[b]
    d[b] = 0
    print(suma)

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

q=int(input())

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

lis=[0 for i in range(10**5)]

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

s=0
for i in range(10**5):
    s+=(i+1)*lis[i]


for i in range(q):
    bi,ci=bc[i]
    s+=lis[bi-1]*(ci-bi)
    print(s)
    lis[ci-1]+=lis[bi-1]
    lis[bi-1]=0

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

ans = 0
for num, count in A.items():
    ans += (num * count)

for a, b in change:
    ans -= (a * A[a])
    temp = A[a]
    A[a] = 0
    A[b] += temp
    ans += (b * temp)
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
a = [0]*100000
for i in A:
    a[i-1] += 1
Q = int(input())

ans = sum(A)

q = []

for i in range(Q):
    B, C = list(map(int, input().split()))
    if a[B-1] >= 1:
        ans = ans - B*a[B-1] + C*a[B-1]
        a[C-1] += a[B-1]
        a[B-1] = 0
    q.append(ans)

for i in q:
    print(i)

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

CNT = {}
S = 0

for a in map(int, input().split()):
    if not(a in CNT):
        CNT[a] = 0

    CNT[a] += 1
    S += a

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

    if B in CNT:
        cnt = CNT[B]
        del CNT[B]

        S -= B*cnt
        S += C*cnt
        
        if not(C in CNT):
            CNT[C] = 0

        CNT[C] += cnt
        
    print(S)

Solution (Python):
from collections import Counter

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

A_counter = Counter(A)
A_sum = sum(A)

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

Solution (Python):
from collections import defaultdict
def main():
    N = int(input())
    A =list(map(int, input().split()))
    s = sum(A)
    d = defaultdict(lambda: 0)

    for v in A:
        d[v] +=1

    Q = int(input())

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import time




import math
start=time.time()
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
n=ri()
a=rl()
q=ri()
dp=[0]*(10**5+1)
bc=[]
for i in range(n):
    dp[a[i]]+=1
bc=[tuple(rl()) for i in range(q)]
ans=0
for i,j in enumerate(dp):
    ans+=i*j
  
for b,c in bc:
    ans+=-dp[b]*b+dp[b]*c
    print(ans)
    dp[c]+=dp[b]
    dp[b]=0

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

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

Solution (Python):




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


al = [0] * (10 ** 5 + 1)
sum = 0


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


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

Solution (Python):
from collections import defaultdict

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)
dic = defaultdict(int)
for i in a:
    dic[i] += 1
for b,c in bc:
    if dic[b] == 0:
        print(ans)
        continue
    ans += (c-b) * dic[b]
    print(ans)
    dic[c] += dic[b]
    dic[b] = 0

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

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
ans = sum(a)
d = {}
q = int(input())
for i in range(q):
    x, y = map(int, input().split())
    ans -= c[x] * x
    ans += c[x] * y
    c[y] = c[y] + c[x]
    c[x] = 0
    print(ans)

Solution (Python):
from collections import Counter

n=int(input())
a=Counter(list(map(int,input().split())))
q=int(input())
sm=0
for x in a:
    sm+=x*a[x]

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

Solution (Python):

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

L=[]

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




s=0



s=sum(a)

for j in range(q):

    b,c=map(int,input().split())
    s+=d[b]*(c-b)
    d[c]+=d[b]
    d[b]=0
    L.append(s)
for j in range(len(L)):
    print(L[j])

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

Solution (Python):
from collections import defaultdict

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

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

Q = int(input())
total = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    if b not in d:
        print(total)
    else:
        total += (c - b) * d[b]
        d[c] += d[b]
        d[b] = 0
        print(total)

Solution (Python):
from collections import defaultdict

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

Solution (Python):
t=1
while t:
	t-=1
	n=int(input())
	
	l=list(map(int,input().split()))
	c=[0 for i in range(100001)]
	sumi=sum(l)
	for i in range(n):
		c[l[i]]+=1
	q=int(input())
	while(q):
		q-=1
		a,b=map(int,input().split())
		sumi+=(c[a]*(b-a))
		c[b]+=c[a]
		c[a]=0
		print(sumi)

Solution (Python):
N = int(input())
A_list = [int(e) for e in input().split()]

A_dict = dict()
S = 0

for A in A_list:
    if A not in A_dict:
        A_dict[A] = 0
        
    A_dict[A] += 1
    S += A

Q = int(input())

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

        A_dict[B] = 0
        
    print(S)

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

for i in range(Q):
    b, c = list(map(int, input().split()))
    b_list.append(b)
    c_list.append(c)
data = dict()
for a in A:
    if a in data:
        data[a] += 1
    else:
        data[a] = 1
ans = sum(A)
for i in range(Q):
    if b_list[i] in data:
        ans += (c_list[i] - b_list[i]) * data[b_list[i]]
        num = data.pop(b_list[i])
        if c_list[i] in data:
            data[c_list[i]] += num
        else:
            data[c_list[i]] = num
    print(ans)

Solution (Python):
n = int(input())
l = [0 for _ in range(100001)]
s = 0
for a in input().split():
    l[int(a)] += 1
    s += int(a)
q = int(input())
ans = []
for i in range(q):
    b,c = map(int,input().split())
    s += (c-b)*l[b]
    ans.append(s)
    l[c] += l[b]
    l[b] = 0

for k in ans:
    print(k)

Solution (Python):
from collections import Counter

n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
bc = [[int(i) for i in input().split()] for _ in range(q)]

cnt = Counter(a)
s = sum(a)
for b, c in bc:
    s += (c - b) * cnt[b]
    cnt[c] += cnt[b]
    cnt[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 = {}
for i, Ai in enumerate(A):
    cnt.setdefault(str(Ai), 0)
    cnt[str(Ai)] += 1

ans = 0
for key, val in cnt.items():
    num = int(key)
    ans += num * val

for j, (Bj, Cj) in enumerate(BC):
    key_B = str(Bj)
    cnt.setdefault(key_B, 0)
    val_B = cnt[key_B]
    cnt[key_B] = 0
    ans -= Bj * val_B
    
    key_C = str(Cj)
    cnt.setdefault(key_C, 0)
    cnt[key_C] += val_B
    ans += Cj * val_B
    
    print(ans)

Solution (Python):
n = int(input())
dp = [0 for _ in range(10**5)]
a = list(map(int, input().split()))
Sum = sum(a)
for i in a:
  dp[i-1] += 1
  
q = int(input())
for i in range(q):
  sa, sb = map(int, input().split())
  x = dp[sa-1]
  dp[sa-1] = 0
  Sum -= x*sa
  dp[sb-1] += x
  Sum += x*sb
  print(Sum)

Solution (Python):
from collections import defaultdict

n = int(input())
a = list(map(int,input().split()))
ans = sum(a)
d = defaultdict(int)
for i in a:
    d[i] += 1

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

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

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

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

    A_map = {}
    for a in A:
        if a in A_map:
            A_map[a] += 1
        else:
            A_map[a] = 1

    for b,c in BC:
        if b not in A_map:
            print(S)
            continue

        num = A_map.pop(b)
        if c in A_map:
            A_map[c] += num
        else:
            A_map[c] = num

        S -= b * num
        S += c * num
        print(S)
main()

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
counts = Counter(A)
Q = int(input())
cur = sum(A)
for _ in range(Q):
    B, C = map(int, input().split())
    if B in counts:
        cur -= counts[B] * B
        cur += counts[B] * C
        print(cur)
        counts[C] += counts[B]
        del counts[B]
    else:
        print(cur)

Solution (Python):
big = 10**5 + 1

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

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

counts = [0]*big
total = 0
for Ai in A:
    counts[Ai] += 1
    total += Ai

for i in range(Q):
    Bi, Ci = B[i], C[i]
    total += (Ci - Bi)*counts[Bi]

    counts[Ci] += counts[Bi]
    counts[Bi] = 0
    print(total)

Solution (Python):

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

sum_i = 0
map_ai = [0]*100001
for ai in a:
    sum_i += ai
    map_ai[ai] += 1

for i in range(q):
    bi = b[i]
    ci = c[i]
    num_bi = map_ai[bi]
    sum_i -= (bi-ci) * num_bi
    print(sum_i)
    map_ai[bi] -= num_bi
    map_ai[ci] += num_bi

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

cnt = [0] * (10**5+1)
for l in lst:
    cnt[l] += 1

calc = sum(lst)
for d in bc:
    buf = cnt[d[0]]
    cnt[d[1]] += buf
    cnt[d[0]] = 0
    calc += buf * (d[1] - d[0])
    print(calc)

Solution (Python):
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    S = sum(A)
    Q = int(input())
    for _ in range(Q):
        b, c = map(int, input().split())
        if C[b] > 0:
            cb = C[b]
            S -= b*cb
            S += c*cb
            C[b] = 0
            C[c] += cb
        print(S)


if __name__ == "__main__":
    main()

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

lst = [0] * (10**5+1)
for A in As:
    lst[A] += 1

val = sum(As)

vals = []
for i in range(Q):
    B, C = map(int,input().split())
    val += (C-B) * lst[B]
    lst[C] += lst[B]
    lst[B] = 0
    vals.append(val)
for val in vals:
    print(val)

Solution (Python):
from collections import defaultdict


def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    d = defaultdict(int)
    s = sum(A)
    for a in A:
        d[a] += 1
    
    for i in range(Q):
        B, C = map(int, input().split())
        num = d[B]
        d[B] = 0
        d[C] += num
        s += num * (C - B)
        print(s)


if __name__ == '__main__':
    main()