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 as co
n = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = [list(map(int,input().split())) for i in range(q)]
c = co.Counter(a)
ans = sum(a)
for i in range(q):
    ans += (c[bc[i][0]] * (bc[i][1]-bc[i][0]))
    print(ans)
    c[bc[i][1]] += c[bc[i][0]]
    c[bc[i][0]] = 0

Solution (Python):
import sys
import time
import math
from collections import deque
import heapq
import itertools
from decimal import Decimal
import bisect
from operator import itemgetter
MAX_INT = int(10e18)
MIN_INT = -MAX_INT
mod = 1000000000+7
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

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

ans = 0
data = [0]*100010
for i in a:
  data[i] += 1
  ans += i

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

Solution (Python):



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

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

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

s = sum(A)
for i in range(Q):
    s = s - BC[i][0] * count[BC[i][0]] + BC[i][1] * count[BC[i][0]]
    count[BC[i][1]] += count[BC[i][0]]
    count[BC[i][0]] = 0
    print(s)

Solution (Python):


import copy
import random
import bisect 
import fractions 
import math
import sys
import collections
from decimal import Decimal 

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))


N = int(input())
A = LI()
Q = int(input())


move_dic = {}
revers_move_dic = {}
for i in range(1,10**5):
    revers_move_dic[i] = -1

BC = [() for i in range(Q)]
for q in range(Q):
    BC[q] = LI()


anss = []
A_collections = collections.Counter(A)


ans = sum(A)
for q in range(Q):
    b,c = BC[q]

    if b in A_collections.keys():
        A_collections[c] += A_collections[b]
        ans += (c - b) * A_collections[b]
        A_collections[b] = 0
    print(ans)

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

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

Solution (Python):

n=int(input())

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

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

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

Solution (Python):
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()
 
n=ii()
a=lmi()
suma=sum(a)
counter=[0]*(10**5+1)
for i in a:
  counter[i]+=1

q=ii()
 
for i in range(q):
  b,c=mi()
  suma+=(c-b)*counter[b]
  
  counter[c]+=counter[b]
  counter[b]=0
  
  print(suma)

Solution (Python):
def mi(): return map(int,input().split())
def lmi(): return list(map(int,input().split()))
def ii(): return int(input())
def isp(): return input().split()
 
n=ii()
a=lmi()
suma=sum(a)
counter=[0]*(10**5+5)
for i in a:
  counter[i]+=1

q=ii()
 
for i in range(q):
  b,c=mi()
  suma+=(c-b)*counter[b]
  
  counter[c]+=counter[b]
  counter[b]=0
  
  print(suma)

Solution (Python):

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

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

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

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)

Solution (Python):
from collections import Counter

N = int(input())

Alist = list(map(int, input().split()))
counter = Counter(Alist)

Q = int(input())
Clist = []
cnt_list = []

for _ in range(Q):
    b, c = map(int, input().split())
    Clist.append(c-b)
    num = counter[b]
    cnt_list.append(num)
    counter[b] = 0
    counter[c] += num

base = sum(Alist)

for (diff, cnt) in zip(Clist, cnt_list):
    ans = base + diff * cnt
    print(ans)
    base = ans

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))
q = int(input())
from collections import Counter
arr = Counter(arr)
ans = sum([i*arr[i] for i in arr])

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

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

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

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
counter = Counter(a)
sum_res = sum(a)
for _ in range(q):
    before, after = map(int, input().split())
    sum_res -= before * counter[before]
    sum_res += after * counter[before]
    counter[after] += counter[before]
    counter[before] = 0
    print(sum_res)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
counter = Counter(a)
sum_res = sum(a)
for _ in range(q):
    before, after = map(int, input().split())
    sum_res -= before * counter[before]
    sum_res += after * counter[before]
    counter[after] += counter[before]
    counter[before] = 0
    print(sum_res)

Solution (Python):
import bisect
import sys
input=sys.stdin.readline
n=int(input())
lst=sorted(map(int,input().split()))
s=sum(lst)
sum_lst=[0]
for i in range(1,10**5+1):
    sum_lst.append(bisect.bisect_right(lst,i)-bisect.bisect_left(lst,i))
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    s+=((c-b)*sum_lst[b])
    print(s)
    sum_lst[c]+=sum_lst[b]
    sum_lst[b]=0

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

Solution (Python):
import sys
import time
import math
st = time.perf_counter()
def inpl():
    return list(map(int, input().split()))


N = int(input())
nl = [0] * 100100
ls = inpl()
ans = 0
for i in ls:
	nl[i] += 1
	ans += i
M = int(input())
for _ in range(M):
	a, b = map(int, input().split())
	ans = ans - (nl[a] * a) + (nl[a] * b)
	nl[b] += nl[a]
	nl[a] = 0
	print(ans)


ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

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())
    
num = [0]*100000
for i in range(len(A)):
    num[A[i]-1] += 1
    
S =[sum(A)]
for i in range(Q):
    S.append(S[i]+(C[i]-B[i])*num[B[i]-1])
    num[C[i]-1] += num[B[i]-1]
    num[B[i]-1] = 0
 
S.pop(0)    
for i in range(Q):
    print(S[i])

Solution (Python):
N = int(input())
A = [0] * (10**5 + 1)
for a in map(int, input().split()):
    A[a] += 1
Q = int(input())
acc = sum(i * a for i, a in enumerate(A))
for _ in range(Q):
    b, c = map(int, input().split())
    k = A[b]
    A[b] = 0
    A[c] += k
    acc += c * k - b * k
    print(acc)

Solution (Python):
import collections

n = int(input())
l_a = list(map(int, input().split()))
col = collections.Counter(l_a)
bef = sum(l_a)
q = int(input())
ans = []
for _ in range(q) :
    b, c = map(int, input().split())
    bef += (c - b)*col[b]
    ans.append(bef)
    col[c] += col[b]
    col[b] = 0

for value in ans :
    print(value)

Solution (Python):
n = int(input())
l_a = list(map(int, input().split()))
many = [0]*101000
for i in range(0, len(l_a)) :
    many[l_a[i]-1]+=1
bef = sum(l_a)
q = int(input())
ans = []
for _ in range(q) :
    b, c = map(int, input().split())
    bef += (c - b)*many[b - 1]
    ans.append(bef)
    many[c - 1] += many[b - 1]
    many[b - 1] = 0
 
for value in ans :
    print(value)

Solution (Python):
import collections

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

cnt_list = collections.Counter(A)
total = sum(A)
for i in range(Q):
    cnt_list[BC[i][1]] += cnt_list[BC[i][0]]
    total += (BC[i][1] - BC[i][0]) * cnt_list[BC[i][0]]
    cnt_list[BC[i][0]] = 0
    print(total)

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

d = {}
s = 0
for i in a:
  if d.get(i) is None:
    d[i] = 0
  d[i] += 1
  s += i

query = int(input())

for q in range(query):
  b, c = map(int, input().split())
  if d.get(b, 0) > 0:
    t = d[b]
    d[b] = 0
    if d.get(c) is not None:
      d[c] += t
    else:
      d[c] = t
    s += (c-b) * t
  print(s)

Solution (Python):
from collections import defaultdict

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

dd = defaultdict(int)

for a in arr:
    dd[a]+=1

sum = sum(arr)

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


    if dd[b]>0:
        sum += dd[b]*(c-b)
        dd[c]+=dd[b]
        dd[b]=0

    print(sum)

Solution (Python):
def ad(a,bank):
	if a not in bank:
		bank[a] = 1
	else:
		bank[a] += 1

def rp(a,b,bank):
	ans = 0
	if a in bank:
		t = bank[a]
		ans = t
		bank[a] = 0
		if b not in bank:
			bank[b] = t
		else:
			bank[b] += t
		
	return ans

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

bank = dict()
su = 0

for a in arr:
	ad(a,bank)
	su += a

q = int(input())
for i in range(q):
	b,c = map(int, input().split())
	t = rp(b,c,bank)
	if b in bank:
		su = su - (t*b) + (t*c)
	
	print(su)

Solution (Python):
from collections import Counter

N=int(input())
A=list(map(int,input().split()))
c=Counter(A)
commons=c.most_common()

ans=0

dic={}
for common in commons:
    dic[common[0]]=common[1]
    ans+=common[0]*common[1]



Q=int(input())
for i in range(Q):
    b,c=map(int,input().split())
    if b not in dic:
        print(ans)
        continue
    else:
        pass
    if c not in dic:
        ans+=(c-b)*dic[b]
        print(ans)
        dic[c]=dic[b]
        dic[b]=0
        continue
    else:
        pass
    ans+=(c-b)*dic[b]
    dic[c]+=dic[b]
    dic[b]=0
    print(ans)

Solution (Python):
import numpy as np
from collections import Counter as C

n=int(input())
a = list(input().split())
a = [int(i) for i in a]
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]
bc=np.array(bc).T
b=bc[0]
c=bc[1]

d=C(a)
d2={}
for k,v in d.items():
  d2[k]=k*v


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

Solution (Python):
import collections

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

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

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

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

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

total = sum(a)

for j in range(q):
    b = bc[j][0]
    c = bc[j][1]
    cnt[c] += cnt[b]
    total += (c-b)*cnt[b]
    cnt[b] = 0
    print(total)

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

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
A = LI()
Q = I()
BC = [LI() for _ in range(Q)]
ans = sum(A)
count_A = collections.defaultdict(int)
for a in A:
    count_A[a] += 1
for b,c in BC:
    if b in count_A:
        ans += (c-b)*count_A[b]
        count_A[c] += count_A[b]
        count_A[b] = 0
    print(ans)

Solution (Python):
from itertools import groupby
def main():
    n=int(input())
    aa=sorted(map(int,input().split()))
    d={}
    for k,g in groupby(aa):
        d[k] = len(list(g))
    q=int(input())
    bc=[]
    for _ in range(q):
        b,c=map(int,input().split())
        bc.append((b,c))
    s = sum(k*v for k,v in d.items())
    for b,c in bc:
        if b in d:
            s += (c-b)*d[b]
            d[c] = d.setdefault(c, 0) + d[b]
            del d[b]
        print(s)
main()

Solution (Python):
import collections

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

clist = collections.Counter(l)

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

Solution (Python):
from collections import Counter
a=int(input())
l=list(map(int,input().split()))
b=int(input())
s=Counter(l)
initial_sum=sum(l)

for _ in range(b):
  x,y=map(int,input().split())
  k=s[x]
  s[x]=0
  s[y]+=k
  initial_sum+=(y-x)*k
  print(initial_sum)

Solution (Python):
import sys

input_data = []
input_pointer = 0

output_data = []

def input_scan():
    global input_data
    input_data = sys.stdin.readlines()
    input_data = [x[:-1] for x in input_data]

def next_line():
    global input_pointer
    global input_data
    if input_pointer < len(input_data):
        input_pointer += 1
        return input_data[input_pointer - 1]
    else:
        return ""

def append_output(string):
    global output_data
    output_data.append(string)

def print_output():
    global output_data
    output_data.append("")
    sys.stdout.write("\n".join(output_data))

































input_scan()

next_line()
from collections import Counter
nums = [int(x) for x in next_line().split(" ")]
total_sum = sum(nums)
nums = Counter(nums)


q = int(next_line())
for i in range(q):
    [b, c] = [int(x) for x in next_line().split(" ")]
    
    diff = c - b
    total_sum += nums[b] * diff
    occ = nums[b]
    if c in nums:
        nums[c] += occ
    else:
        nums[c] = occ
    nums[b] = 0

    
    append_output(str(total_sum))

print_output()

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

from collections import Counter, defaultdict

ca = Counter(a)

suma = sum(a)
diff = 0
d = defaultdict(int)

for x in bc:
  d[x[0]] = d[x[1]]
  suma += ca[x[0]] * (x[1] - x[0])
  ca[x[1]] += ca[x[0]]
  ca[x[0]] = 0
  print(suma)

Solution (Python):
from collections import defaultdict
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
SA=sum(A)
Q=II()
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
cnt=defaultdict(int)
for a in A:
  cnt[a]+=1
for b,c in queries:
  if b in cnt:
    SA-=cnt[b]*b
    SA+=cnt[b]*c
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(SA)

Solution (Python):
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
SA=sum(A)
Q=II()
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
cnt={}
for a in A:
  if a not in cnt:
    cnt[a]=0
  cnt[a]+=1
for b,c in queries:
  if b in cnt:
    SA-=cnt[b]*b
    SA+=cnt[b]*c
    if c not in cnt:
      cnt[c]=0
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(SA)

Solution (Python):
from sys import stdin,stdout
LI=lambda:list(map(int,input().split()))
MAP=lambda:map(int,input().split())
IN=lambda:int(input())
S=lambda:input()
import math
from collections import Counter,defaultdict


n=IN()
a=LI()
q=IN()
d=defaultdict(int)
for i in range(n):
    d[a[i]]+=1
s=sum(a)
for i in range(q):
    x,y=MAP()
    s+=(y-x)*d[x]
    d[y]+=d[x]
    d[x]=0
    print(s)

Solution (Python):
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N=II()
A=LI()
Sum=sum(A)
Q=II()
cnt={}
queries=[]
for i in range(Q):
  b,c=MI()
  queries.append((b,c))
for a in A:
  if a not in cnt:
    cnt[a]=0
  cnt[a]+=1

for b,c in queries:
  if b in cnt:
    Sum-=cnt[b]*b
    Sum+=cnt[b]*c
    if c not in cnt:
      cnt[c]=0
    cnt[c]+=cnt[b]
    cnt[b]=0
  print(Sum)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
L = [0]*100001
s = sum(A)
for a in A:
    L[a] += 1 
Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    s += L[B]*(C-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)]

res = 0
a_state = {}
for a in A:
    a_state[a] = a_state.get(a, 0) + 1
    res += a

for b, c in BC:
    b_num = a_state.get(b, 0)
    c_num = a_state.get(c, 0)
    res = res - b_num*b + b_num*c
    a_state[c] = c_num + b_num
    a_state[b] = 0

    print(res)

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

from collections import Counter
counter=Counter(a)
s=sum(a)
for b,c in bc:
    if b in counter:
        sa=counter[b]*(c-b)
        counter[c]+=counter[b]
        counter.pop(b)
        s+=sa
    print(s)

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

ans=sum(a)
for i in range(q):
  a,b=map(int,input().split())
  if a in dic:
    ans=ans-a*dic[a]+b*dic[a]
    if b in dic:
      keep=dic[a]
      dic[b]+=keep
      dic[a]=0
    else:
      keep=dic[a]
      dic[b]=keep
      dic[a]=0
  print(ans)

Solution (Python):


import sys
stdin = sys.stdin
from collections import defaultdict

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    counter = defaultdict(int)
    for ai in a:
        counter[ai] += 1
    asum = sum(a)

    q = ni()
    for i in range(q):
        b, c = na()
        asum += counter[b] * (c-b)
        counter[c] += counter[b]
        counter[b] = 0
        print(asum)

if __name__ == "__main__":
    main()

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

Solution (Python):


import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    alist = [0] * (10**5 + 1)
    for ai in a:
        alist[ai] += 1
    asum = 0
    for i, num in enumerate(alist):
            asum += i*num

    q = ni()
    for i in range(q):
        b, c = na()
        asum += alist[b] * (c-b)
        alist[c] += alist[b]
        alist[b] = 0
        print(asum)

if __name__ == "__main__":
    main()

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

n = [0 for i in range(100001)]
S = 0
for i in range(N):
    n[A[i]] = n[A[i]] + 1
    S = S + A[i]

for i in range(Q):
    S = S + (C[i]-B[i])*n[B[i]]
    n[C[i]] = n[C[i]] + n[B[i]]
    n[B[i]] = 0
    print(S)

Solution (Python):
import sys
import collections

N = int(input())
A = list(map(int, sys.stdin.readline().split()))
Q = int(input())
BC = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

A_dic = collections.Counter(A) 
S = sum(A)
for i in range(Q):
    num = A_dic[BC[i][0]]
    A_dic[BC[i][0]] = 0
    A_dic[BC[i][1]] += num
    S += (BC[i][1] - BC[i][0]) * num
    print(S)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
a_dict = {}
a_sum = sum(a_list)
for a in a_list:
    if a in a_dict:
        a_dict[a] += 1
    else:
        a_dict[a] = 1

q = int(input())

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

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

main()

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

answer = sum(a)
a_num_cnt = {}

for i in range(n):
    num = a[i]

    if num in a_num_cnt:
        a_num_cnt[num] += 1
    else:
        a_num_cnt[num] = 1

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

    if b in a_num_cnt:
        cnt = a_num_cnt[b]
        answer += (increament * cnt)
        a_num_cnt[b] -= cnt

        if c in a_num_cnt:
            a_num_cnt[c] += cnt
        else:
            a_num_cnt[c] = cnt


    print(answer)

Solution (Python):
n = int(input())
r = list(map(int,input().split(" ")))
ar = {i: 0 for i in range(1,10 ** 5 + 1)}
s = sum(r)
for a in r:
    ar[a] += 1
q = int(input())
for i in range(q):
    x,y = map(int,input().split(" "))
    s += (y - x) * ar[x]
    ar[y] += ar[x]
    ar[x] = 0
    print(s)

Solution (Python):
N=int(input())
array=map(int,input().split())
A=[]
for i in array:
    A.append(i)

dict_num={i:0 for i in range(100050)}
Q=int(input())
matrix=[]
for i in range(Q):
    matrix.append(list(map(int,input().split())))


sum=0

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


for i in matrix:
    sum+=(i[1]-i[0])*dict_num[i[0]]
    dict_num[i[1]]+=dict_num[i[0]]
    dict_num[i[0]]=0
    print(sum)

Solution (Python):

import collections

n = int(input())
a = list(map(int, input().split()))
q = int(input())
sum_a = sum(a)
count = collections.Counter(a)


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

    print(sum_a)

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

Solution (Python):
import collections
from sys import stdin
data = stdin.readlines()

n = int(data[0].split()[0])
a = [int(s) for s in data[1].split()]
q = int(data[2].split()[0])

sum = sum(a)

l = []
for i in range(q):
    l.append(int(data[i+3].split()[0]))
    l.append(int(data[i+3].split()[1]))

d = collections.Counter(a)

for i in range(q):
    sum += (l[2*i+1]-l[2*i]) * d[l[2*i]]
    d[l[2*i+1]] += d[l[2*i]]
    d[l[2*i]] = 0
    print(sum)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
AC = Counter(A)
S = sum(A)
Q = int(input())
for _ in range(Q):
  B,C = map(int, input().split())
  if B not in AC:
    pass
  else:
    count = AC[B]
    AC[B] = 0
    S = S + (C-B)*count
    if C in AC:
      AC[C] += count
    else:
      AC[C] = count
  print(S)

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') 
inf = float('inf') ;mod = 10**9+7 
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
A = list(map(int, input().split()))
ans = sum(A)
cnt = collections.Counter(A)

q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    ans += (c-b)*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())
x=dict()
for i in range(N):
    val=A[i]
    if val in x:
        x[val]+=1
    else:
        x[val]=1

S=sum(A)

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

Solution (Python):
from collections import Counter

N = int(input())

A = [int(x) for x in input().split()]
T = sum(A)
Z = Counter(A)

Q = int(input())

for _ in range(Q):
  B,C = map(int,input().split())
  x = Z[B]
  y = Z[C]
  Z.pop(B,0)
  Z[C] = x+y
  T = T-(B*x)+(C*x)
  print(T)

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

Solution (Python):
import collections

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())
counter = collections.Counter(a)

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

Solution (Python):
mod = int(1e9+7)
def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c

def main():
    
    n = int(raw_input())
    
    arr = [int(x) for x in raw_input().split()]
    
    cnt = {}
    
    cur_sum = 0
    
    for num in arr:
        cnt[num] = cnt.get(num, 0) + 1
        cur_sum += num 
    
    q = int(raw_input())
    
    for _ in range(q):
        frm, to = [int(x) for x in raw_input().split()]
        
        frm_cnt, to_cnt = cnt.get(frm, 0), cnt.get(to, 0)
        
        diff = to - frm
        
        total_diff = frm_cnt * diff
        
        cur_sum += total_diff
        
        cnt[to] = to_cnt + frm_cnt
        
        cnt[frm] = 0
        
        print(cur_sum)
        
        
           
        
    
    
    

main()

Solution (Python):
n = int(input())
A = map(int, input().split())
q = int(input())
BC = [None for i in range(q)]
for i in range(q):
    b,c = map(int, input().split())
    BC[i] = (b,c)

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

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

for i in BC:
    ans+=count[i[0]]*(i[1]-i[0])
    count[i[1]]+=count[i[0]]
    count[i[0]]=0
    print(ans)

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

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

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

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

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

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

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

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

first_sum = sum(t)

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

for i in range(n):
    memo[t[i]] += 1

q=int(input())

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

Solution (Python):
from collections import defaultdict

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

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

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

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


def get_sum(dic):
	sum = 0
	
	for k, v in dic.items():
		sum += k * v
	
	return sum


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

for ai in a:
	dic.setdefault(ai, 0)
	dic[ai] += 1

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

	count = dic.pop(b, 0)
	
	if count > 0:
		dic.setdefault(c, 0)
		dic[c] += count

	if i == 0:	
		sum = get_sum(dic)
	else:
		sum += (c - b) * count
	
	print(sum)

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


def get_sum(dic):
	sum = 0
	
	for k, v in dic.items():
		sum += k * v
	
	return sum


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

for ai in a:
	dic.setdefault(ai, 0)
	dic[ai] += 1

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

	count = dic.pop(b, 0)
	
	if count > 0:
		dic.setdefault(c, 0)
		dic[c] += count

	if i == 0:	
		sum = get_sum(dic)
	else:
		sum += (c - b) * count
	
	print(sum)