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())
b = [list(map(int,input().split(" "))) for i in range(0,q)]

ans = sum(a)
anum = collections.Counter(a)

for i in range(q):
    x, y = b[i][0], b[i][1]
    ans += anum[x] * (y - x)
    anum[y] += anum[x]
    anum[x] = 0
    print(ans)

Solution (Python):
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
q=int(input())
num=np.zeros(100001)
for i in a:
  num[i]+=1
sum=a.sum()
for i in range(q):
  b,c=map(int,input().split())
  sum+=(c-b)*num[b]
  num[c]+=num[b]
  num[b]=0
  print(int(sum))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
    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)

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

a_count = collections.Counter(a)
a_list = dict(list(a_count.items()))

total = sum(a)
for b, c in bc:
    if b in a_list:
        total += (c-b)*a_list[b]
        num = a_list[b]
        a_list[b] = 0
        if c in a_list:
            a_list[c] += num
        else:
            a_list[c] = num
            
    print(total)

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

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

s = sum(A)
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):
import sys
readline = sys.stdin.readline


def st(line):
    tmp = ''
    for l in line:
        if l == '\n':
            yield tmp
        elif l == ' ':
            yield tmp
            tmp = ''
        else:
            tmp += l


def solve():
    N = int(input())
    As = st(readline())

    d = dict()
    total = 0
    for a in As:
        a = int(a)

        if a in d:
            d[a] += 1
        else:
            d[a] = 1
        total += a

    Q = int(input())
    queries = (map(int, readline().strip().split()) for _ in range(Q))

    for que in queries:
        b, c = que

        if b not in d:
            print(total)
            continue

        n_b = d[b]
        d[b] = 0

        if c not in d:
            d[c] = 0

        d[c] += n_b
        total += (c - b) * n_b

        print(total)


def main():
    solve()


if __name__ == "__main__":
    main()

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

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

sum=0
num=[0]*100010

for i in range(N):
    num[A[i]]+=1
    sum+=A[i]

for i in range(Q):
    b=x[i][0]
    c=x[i][1]

    sum=sum-b*num[b]+c*num[b]
    num[c]+=num[b]
    num[b]=0

    print(sum)

Solution (Python):
import collections
n = int(input())
l = list(map(int, input().split()))
a = int(input())
c = collections.Counter(l)
answer = sum(l)
for i in range(a):
    j,k =  map(int, input().split())
    save = c[j]
    del c[j]
    c[k] += save
    answer = answer + k*save - j*save
    print(answer)

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

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

q = int(input())



a_dic ={}
ans = 0

for i in a:
    if i not in a_dic:
        a_dic[i] = 1
    else:
        a_dic[i] += 1
    
    ans += i



for i in range(q):
    
    b,c = map(int, input().split())
    
    
    if b in a_dic:
        if c not in a_dic:
            a_dic[c] = a_dic[b]

        else:
            tmp_c = a_dic[c]
            tmp_c += a_dic[b]
            a_dic[c] = tmp_c
        tmp_a = (c-b) * a_dic[b]
        a_dic.pop(b)
        ans += tmp_a
    print(ans)

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
    c_cnt = counter[c]
    sumA += c*(b_cnt)
    counter[c] += b_cnt
    print(sumA)

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

A_list = [0]*10**5
for i in A:
    A_list[i - 1]+=1

goukei = sum(A)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
    dic.setdefault(a[i],0)
    dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    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)

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

S=sum(a)

table=[0]*(10**6)

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

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

        print(ans)

        table[b]=0
        S=ans
    else:
        print(S)

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

table = [0] * 100001

c = 0
s = 0

for i in A:
    table[i] += 1
    s += i

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
hako = [0]*(10**6)
ind = list(range(10**6))
S = sum(a)
s = []

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

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

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

Solution (Python):
import sys
readline = sys.stdin.readline
 
 
def st(line):
    tmp = ''
    for l in line:
        if l == '\n':
            yield tmp
        elif l == ' ':
            yield tmp
            tmp = ''
        else:
            tmp += l



N = int(input())
As = st(readline())

d = dict()
total = 0
for a in As:
    a = int(a)

    if a in d:
        d[a] += 1
    else:
        d[a] = 1
    total += a

Q = int(input())
queries = (map(int, readline().strip().split()) for _ in range(Q))

for que in queries:
    b, c = que

    if b not in d:
        print(total)
        continue

    n_b = d[b]
    d[b] = 0

    if c not in d:
        d[c] = 0

    d[c] += n_b
    total += (c - b) * n_b

    print(total)

Solution (Python):
import sys
readline = sys.stdin.readline
 
 
def st(line):
    tmp = ''
    for l in line:
        if l == '\n':
            yield tmp
        elif l == ' ':
            yield tmp
            tmp = ''
        else:
            tmp += l
 
 
def solve():
    N = int(input())
    As = st(readline())
 
    d = dict()
    total = 0
    for a in As:
        a = int(a)
 
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
        total += a
 
    Q = int(input())
    queries = (map(int, readline().strip().split()) for _ in range(Q))
 
    for que in queries:
        b, c = que
 
        if b not in d:
            print(total)
            continue
 
        n_b = d[b]
        d[b] = 0
 
        if c not in d:
            d[c] = 0
 
        d[c] += n_b
        total += (c - b) * n_b
 
        print(total)
 
 
def main():
    solve()
 
 
if __name__ == "__main__":
    main()

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

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

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

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

for i in range(N):
    count_list[arr[i]] +=1

ans = sum(arr)

for i in que:
    keep = count_list[i[0]]
    c_ans = ans+keep*(i[1]-i[0])
    print(c_ans)
    ans =c_ans

    count_list[i[1]] += count_list[i[0]]
    count_list[i[0]] = 0

Solution (Python):
from collections import defaultdict
N = int(input())
raw_numbers = list(map(int, input().split()))
numbers = defaultdict(int)

for num in raw_numbers:
    numbers[num] += 1
    
total = sum(raw_numbers)
    
Q = int(input())

for _ in range(Q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= a * numbers[a]
    numbers[b] += numbers[a]
    total += b * taken
    numbers[a] = 0
    print(total)

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = map(int, input().split())
    taken = numbers[a]
    total -= a * numbers[a]
    numbers[b] += numbers[a]
    total += b * taken
    numbers[a] = 0
    print(total)

Solution (Python):
from collections import defaultdict


def main():
  N = int(input())
  A = list(map(int, input().split()))
  Q = int(input())
  
  
  
  
  map_ = [list(map(int, input().split())) for _ in range(Q)]

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

  sum_ = sum(A)
  for i in range(Q):
    from_, to_ = map_[i]
    if not(from_ in mem):
      print(sum_)
      continue
    idiff = mem[from_]
    mem[to_] += mem[from_]
    mem[from_] = 0
    diff = idiff * (to_ - from_)
    sum_ += diff
    
    print(sum_)


if(__name__ == '__main__'):
  main()

Solution (Python):
import collections
N = int(input())
inp = list(map(int,input().split()))
dic = dict(collections.Counter(inp))
wa = 0
for i, j in dic.items():
	wa += i * j
se = set(dic.keys())
for i in range(int(input())):
	a, b = map(int,input().split())
	if a in se:
		wa += dic[a] * (b-a)
		if b in se:
			pass
		else:
			dic[b] = 0
		se.add(b)
		dic[b] += dic[a]
		dic[a] = 0
	print(wa)

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

table = [0] * 100001

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

Solution (Python):
n = int(input())
num = list(map(int, input().split()))
q = int(input())
change = [0 for i in range(q)]
for i in range(q):
    change[i] = list(map(int, input().split()))

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
ans = 0
for i, j in enumerate(dic):
    ans += j*dic[j]

for i in range(q):
    if change[i][0] in dic:
        ans += dic[change[i][0]] * (change[i][1] - change[i][0])
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    print(ans)

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = map(int, input().split())
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    total += b * taken
    numbers[a] = 0
    print(total)

Solution (Python):
import collections
N = int(input())
lsn = list(map(int,input().split()))
Q = int(input())
ls2 = []
for i in range(Q):
    Bi,Ci = map(int,input().split())
    ls2.append([Bi,Ci])
index = collections.Counter(lsn)
sum1 = sum(lsn)
for l in ls2:
    sum1 = sum1 +(l[1]-l[0])*index[l[0]]
    index[l[1]] += index[l[0]]
    index[l[0]] = 0
    print(sum1)

Solution (Python):
N = int(input())
line = list(map(int, input().split()))
countList = [0] * ((10 ** 5) + 1)

ansSum = sum(line)
for i in line:
    countList[i] += 1
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    ansSum += (C - B) * countList[B]
    countList[C] += countList[B]
    countList[B] = 0
    print(ansSum)

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = map(int, input().split())
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    total += taken*b
    numbers[a] = 0
    print(total)

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

res = sum(A)
A_counter = Counter(A)
for i, j in B:
    
    
    
    

    res += (j - i) * A_counter[i]
    print(res)

    A_counter[j] += A_counter[i]
    A_counter[i] = 0

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
Q = INT()
L = [LIST() for i in range(Q)]

graph = defaultdict(list)
c = Counter(A)
ans = sum(A)

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

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

res = sum(A)
A_counter = Counter(A)
for i, j in B:
    if i not in A_counter:
        A_counter[i] = 0
    if j not in A_counter:
        A_counter[j] = 0

    res += (j - i) * A_counter[i]
    print(res)
    
    A_counter[j] += A_counter[i]
    A_counter[i] = 0

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = list(map(int, input().split()))
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)

Solution (Python):
_ = int(input())
A = list(map(int, input().split()))
Q = int(input())
Query = [tuple(map(int, input().split())) for _ in range(Q)]
from collections import Counter
cntr = Counter(A)
ans = sum(A)
for b, c in Query:
    if cntr.get(b) == None:
        print(ans)
        continue
    diff = (c - b) * cntr[b]
    ans += diff
    print(ans)
    cntr[c] += cntr[b]
    cntr[b] = 0

Solution (Python):
















N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())


from itertools import groupby
L = [[k, len(list(g))] for k, g in groupby(A)]


sumA = 0
from collections import defaultdict
d = defaultdict(int)
for k,v in L:
  d[k] += v
  sumA += k*v



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

  sumA += (c-b) * d[b]
  print(sumA)

  d[c] += d[b]
  d[b] = 0

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for _ in range(Q):
    a, b = map(int, input().split())
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)

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


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

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

s = sum(A)

for i in range(Q):
    s += LIST[b[i]]*(c[i]-b[i])
    LIST[c[i]]+=LIST[b[i]]
    LIST[b[i]] = 0
    print(s)

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

Q = int(input())

for i in range(Q):
    a, b = map(int, input().split())
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)

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

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

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

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

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

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for i in range(Q):
    a, b = map(int, input().split())
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)

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

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


def main():
    n = int(si())
    l = [int(e) for e in si().split()]
    s = sum(l)
    l.sort()
    tab = dict()
    for e in l:
        if e not in tab:
            tab[e] = 1
        else:
            tab[e] += 1

    t = int(si())
    for _ in range(t):
        [fr, to] = [int(e) for e in si().split()]
        if fr not in tab:
            print(s)
            continue
        s += (to-fr)*tab[fr]
        if to in tab:
            tab[to] += tab[fr]
        else:
            tab[to] = tab[fr]
        tab[fr] = 0
        print(s)


if __name__ == '__main__':
    main()

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

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

Solution (Python):
from collections import defaultdict
 
N = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for item in raw_numbers:
    numbers[item] += 1

for i in range(Q):
    a, b = map(int, input().split())
    taken = numbers[a]
    total -= numbers[a]*a
    numbers[b] += numbers[a]
    numbers[a] = 0
    total += taken*b
    print(total)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
S = []
c = 0
s = sum(A)
table = [0] * 100001
 
for i in A:
    table[i] += 1
 
for i in range(Q):
    B, C = map(int,input().split())
    c = table[B]
    s += (C - B) * c
    table[C] += table[B]
    table[B] = 0
    S.append(s)

for s in S:
    print(s)

Solution (Python):

n = int(input())
a = list(map(int,input().split()));
q = int(input())
aa = 0;
for i in range(n):
    aa += a[i];

b = [input().split() for l in range(q)]

bu = [0]*(100000);

for i in range(n):
    bu[a[i]-1] += 1;
    
for x in range(q):
    c = int(b[x][0])
    d = int(b[x][1])
    aa += (d-c)*(bu[c-1]);
    bu[d-1] += bu[c-1];
    bu[c-1] = 0;
    print(aa);

Solution (Python):


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


sum_a = sum(a)
a_dict = {i: 0 for i in set(a)}
for i in a:
    a_dict[i] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    if not b in a_dict:
        print(sum_a)
        continue
    num = a_dict[b]
    sum_a -= num*b
    sum_a += num*c
    print(sum_a)
    a_dict[b] = 0
    if c in a_dict:
        a_dict[c] += num
    else:
        a_dict[c] = num

Solution (Python):

n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
 
bc = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]
 
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)

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

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
summary = sum(A)
d = {}
for a in A:
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
    
Q = int(input())
for _ in range(Q):
  B, C = list(map(int,input().split()))
  if B not in d:
    b = 0
  else:
    b = d[B]
    d[B] = 0
  if C in d:
    d[C] += b
  else:
    d[C] = b
  summary = summary + b * (C - B)
  print(summary)

Solution (Python):
from collections import Counter

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

sum = 0
for i in range(len(A)):
    sum += A[i]

counter = Counter(A)

ans = []
Q = int(input())
for i in range(Q):
    B,C=map(int,input().split())
    if counter[B] != 0:
        add = int(( C - B ) * counter[B])
        sum += add
        counter[C] += counter[B]
        counter[B] = 0
    ans.append(sum)


for i in range(Q):
    print(ans[i])

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

Solution (Python):
from collections import defaultdict
 
n = int(input())
raw_numbers = list(map(int, input().split()))
q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for item in raw_numbers:
    numbers[item] += 1

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
Q = int(input())

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

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

Solution (Python):
from collections import defaultdict
 
n = int(input())
raw_numbers = list(map(int, input().split()))
q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for item in raw_numbers:
    numbers[item] += 1

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

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

ans = sum(a)

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

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

Solution (Python):
from collections import defaultdict
 
n = int(input())
a = list(map(int, input().split()))
q = int(input())
 
total = sum(a)
 
numbers = defaultdict(int)
 
for item in a:
    numbers[item] += 1
    
for i in range(q):
    b, c = map(int, input().split())
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
BC = [list(map(int,input().split())) for _ in range(Q)]
total = sum(A)
dict = {}
for i in range(len(A)):
  if A[i] not in dict:
    dict[A[i]] = 1
  else:
    dict[A[i]] += 1
for i in range(Q):
  if BC[i][0] not in dict:
    dict[BC[i][0]] = 0
  if BC[i][1] not in dict:
    dict[BC[i][1]] = 0
  if dict[BC[i][0]] > 0:
    num = (BC[i][1] - BC[i][0]) * (dict[BC[i][0]])
    total += num
  print(total)
  dict[BC[i][1]] += dict[BC[i][0]]
  dict[BC[i][0]] = 0

Solution (Python):
n=int(input())
a = map(int, input().split())
q = int(input())
r = {}
s = 0
for i in a:
    s += i
    if i in r:
        r[i] += 1
    else:
        r[i] = 1

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
S = []
c = 0
s = sum(A)
table = [0] * 100010




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

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

        
        
        
    
    
    
        



for s in S:
    print(s)

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
q = int(input())
total = sum(lis)

nlis = [0] * 100002

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

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

Solution (Python):

from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce


MOD = 1000000007
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
def keta(kazu): 
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): 
    return reduce(math.gcd, numbers)

def combination(m,n): 
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def main():
    n = int(stdin.readline().rstrip())
    a = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
    dic = {}
    ans = 0
    for i in range(len(a)):
        if a[i] in dic:
            dic[a[i]] += 1
        else:
            dic[a[i]] = 1
    for k, v in dic.items():
        if v != 0:
            ans += k*v
    q = int(stdin.readline().rstrip())
    for i in range(q):
        b, c = [int(x) for x in stdin.readline().rstrip().split()]
        if b in dic:
            ans += (c-b)*dic[b]
            print(ans)
            if c in dic:
                dic[c] += dic[b]
            else:
                dic[c] = dic[b]
            dic[b] = 0
        else:
            print(ans)

main()

Solution (Python):
import collections
_ = input()
a = [*map(int, input().split())]
q = int(input())
s = sum(a)
b = [[*map(int,input().split())] for _ in range(q)]
c = collections.Counter(a)
for i,j in b:
    if j not in c:
        c[j] = 0
    if i not in c:
        c[i] = 0
    s += (j-i)*c[i]
    c[j]+=c[i]
    c[i] = 0
    print(s)

Solution (Python):


def exist_or_not(list, value):
  for i in range(len(list)):
    if list[i] < value:
      continue
    elif list[i] == value:
      return i
    else:
      break
  return -1

n = int(input())
a = [0] * 100001
numbers = input().split(" ")
sum = 0
for x in numbers:
  int_x = int(x)
  a[int_x] += 1
  sum += int_x

q = int(input())

r = []
b = []
c = []



for i in range(q):
  numbers = input().split(" ")
  b.append(int(numbers[0]))
  c.append(int(numbers[1]))

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

Solution (Python):
from collections import defaultdict
 
n = int(input())
a = list(map(int, input().split()))
q = int(input())
 
total = sum(a)
 
numbers = defaultdict(int)
 
for item in a:
    numbers[item] += 1
 
for i in range(q):
    b, c = map(int, input().split())
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)