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

A_counter = collections.Counter(A)

sumA = sum(A)

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

    
    if A_counter[B] == 0:
        print(sumA)
    else:
        sumA =sumA - (B * A_counter[B])
        sumA = sumA + (C * A_counter[B])
        print(sumA)
        A_counter[C] = A_counter[B] + A_counter[C]  
        A_counter[B] = 0

Solution (Python):
from collections import Counter


n = int(input())
a = list(map(int, input().split()))
counter = Counter(a)
ini_sum = sum(a)
q = int(input())
s = [ini_sum] + [0] * q

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

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

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

list=[0]*100001
sum=sum(A)
for i in A:
    list[i]+=1

for bc in BC:
    x=list[bc[0]]
    sum+=(bc[1]-bc[0])*x
    print(sum)
    list[bc[0]]-=x
    list[bc[1]]+=x

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


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

s = sum(A)

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

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

Solution (Python):
import sys

stdin = sys.stdin

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

n = ni()
a = na()
q = ni()
l = [na() for _ in range(q)]

import collections

c = collections.Counter(a)
ans = sum(a)
for i in l:
  if c[i[0]] >0:
    ans = ans - c[i[0]]*i[0] + i[1]*(c[i[0]])
    c[i[1]] = c[i[1]] + c[i[0]]
    c[i[0]] = 0
    print(ans)
  else:
    print(ans)

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
q = int(input())
b = []
c = []
for i in range(q):
    x,y = map(int,input().split())
    b.append(x)
    c.append(y)
    
d = collections.Counter(a)
s = 0
for k,v in d.items():
    s += k*v

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

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
tot = sum(A)
Q = int(input())
for i in range(Q):
    f, t = map(int, input().split())
    dis = t - f
    num = c[f]
    tot += num * dis
    print(tot)
    c[f] = 0
    if t in c:
        c[t] = c[t] + num
    else:
        c[t] = num

Solution (Python):
from collections import Counter
dicy = {}
def swap(a,b):
	tmp = 0
	try:
		tmp = dicy.pop(a)
	except Exception:
		tmp = 0

	try:
		dicy[b] += tmp
	except Exception:
		dicy[b] = tmp

	return (int(b)-int(a)) * tmp
  

max = input()
dicy = dict(Counter(input().split(' ')))
result = 0
for v,k in dicy.items():
	result += int(v)*int(k)
num_oper = int(input())
for i in range(num_oper):
	a,b = input().split(' ')
	diff = swap(a,b)
	result += diff
	print(result)

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

q = int(input())

A = {}
a = 0
for i in L:
    a += i
    if i in A:
        A[i] = A[i] +1
    else:
        A[i] = 1



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

Solution (Python):
from collections import defaultdict
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    Q = int(input())
    Query = list()

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

    elem = defaultdict(int)
    s = 0

    for a in A:
        elem[a] += 1
        s += a

    for q in Query:
        B, C =  q

        if B in elem.keys():
            
            nB = elem[B]

            
            if C in elem.keys():
                elem[B] = 0
                elem[C] += nB
              
                s = s - B * nB + C * nB
                print(s) 
            else:
                elem[B] = 0
                elem[C] = nB
                
                s = s - B * nB + C * nB
                print(s)
            
        else:
            print(s)
           
if __name__ == "__main__":
    main()

Solution (Python):


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

D=dict()

for a in A:
    if a not in D:
        D[a]=1
    else:
        D[a]+=1
S=sum(A)

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

    if B in D:
        
        S-=D[B]*B
        
        S+=D[B]*C
        
        
        if C in D:
            D[C]+=D[B]
        else:
            D[C]=D[B]

        del D[B]

    print(S)

Solution (Python):
def solve():
	N = int(input())
	arr = list(map(int, input().split()))
	summ = sum(arr)
	freq = dict()
	for x in arr:
		freq[x] = freq.get(x, 0) + 1
	Q = int(input())
	for _ in range(Q):
		B, C = map(int, input().split())
		Bfreq = freq.get(B, 0)
		summ -= B * Bfreq
		summ += C * Bfreq
		freq[C] = freq.get(C, 0) + Bfreq
		if (B in freq):
			freq.pop(B)
		print(summ)


def main():
	
	solve()


if __name__ == '__main__':
	main()

Solution (Python):
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
Counter = defaultdict(int)
for a in A:
    Counter[a] += 1
sum_val = sum(A)
ans = []
for _ in range(Q):
    B, C = map(int, input().split())
    sum_val += (C - B) * Counter[B]
    Counter[C] += Counter[B]
    Counter[B] = 0
    ans.append(sum_val)
print(*ans, sep='\n')

Solution (Python):
import numpy as np

N = int(input())
A = np.fromstring(input(), sep=" ", dtype=np.int64)
Q = int(input())
B = np.zeros(Q)
C = np.zeros(Q)
D = np.fromstring(" ".join([input() for q in range(Q)]), sep=" ", dtype=np.int64).reshape((Q, 2))
B = D[:, 0]
C = D[:, 1]

d = dict(np.c_[np.unique(A, return_counts=True)])
s = np.sum(A)
for q in range(Q):
    if(B[q] in d):
        n = d.pop(B[q])
        s += n * (C[q] - B[q])
        if(C[q] in d):
            d[C[q]] += n
        else:
            d[C[q]] = n
    print(s)

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

from collections import Counter

counter = Counter(A)
ans = sum([key*value for key, value in counter.items()])

for q in range(Q):
  ans = ans+((BC[q][1]-BC[q][0])*counter[BC[q][0]])
  counter[BC[q][1]] += counter[BC[q][0]]
  counter[BC[q][0]] = 0
  print (ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mp, ans = {}, 0
for i in range(n):
    if(a[i] in mp.keys()):
        mp[a[i]] += 1
    else:
        mp[a[i]] = 1
for k in mp.keys():
    ans += k * mp[k]
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    if(b not in mp.keys()):
        mp[b] = 0
        print(ans)
        continue
    if(c in mp.keys()):
        mp[c] += mp[b]
    else:
        mp[c] = mp[b]
    ans += (c-b) * mp[b]
    mp[b] = 0
    print(ans)

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

Solution (Python):
import collections

num = int(input())
a_i = list(map(int, input().split()))
q = int(input())
c = collections.Counter(a_i)
ans = sum(a_i)

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

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

N=int(input())
A=map(int,input().split())
Q=int(input())
numbers=[]
for _ in range(Q):
    B,C=map(int,input().split())
    numbers.append([B,C])
answers=[0 for _ in range(100001)]
for a in A:
    answers[a]+=a
whole=sum(answers)
for number in numbers:
    suuji=answers[number[0]]//number[0]
    answers[number[0]]=0
    answers[number[1]]+=suuji*number[1]
    whole+=(number[1]-number[0])*suuji
    print(whole)

Solution (Python):
N = int(input())
l = [0]*(10**5+1)
A = [j for j in map(int, input().split())]
for i in A:
    l[i] += 1

Q = int(input())

BC = []
ans = sum(A)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for i in range(Q):
    b, c = map(int, input().split())
    B += [b]
    C += [c]
s = set()
d = {}
for i in range(N):
    if A[i] not in d:
        d[A[i]] = 1
        s.add(A[i])
    else:
        d[A[i]] += 1

result = sum(A)
for i in range(len(B)):
    if B[i] in s:
        result -= B[i]*d[B[i]]
        result += C[i]*d[B[i]]
        if C[i] not in d:
            d[C[i]] = d[B[i]]
        else:
            d[C[i]] += d[B[i]]
        s.add(C[i])
        s.remove(B[i])
        del d[B[i]]
    print(result)

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

s = 0
for k,v in A.items():
    s += k*v

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

    print (s)

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

main()

Solution (Python):
class Box:
  def __init__(self, line):
    self.__b = {}
    for i in map(int, line.split(' ')):
      if i not in self.__b:
        self.__b[i] = 1
      else:
        self.__b[i] += 1

    self.__sum = 0
    for k, v in self.__b.items():
      self.__sum += k * v    

  def convert(self, b, c):
    if b in self.__b:
      if c in self.__b:
        self.__b[c] += self.__b[b]
      else:
        self.__b[c] = self.__b[b]

      self.__sum += (c - b) * self.__b[b]

      self.__b[b] = 0

  def sum(self):
    return self.__sum

if __name__ == "__main__":
  n = int(input())
  b = Box(input())
  q = int(input())
  for i in range(q):
    b.convert(*list(map(int, input().split(' '))))
    print(b.sum())

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bc = [map(int, input().split()) for _ in range(Q)]
b, c = [list(i) for i in zip(*bc)]

mydict = {}
for i in A:
    if i in mydict:
        score = mydict[i]
        mydict[i] = score + 1
    else:
        mydict[i] = 1


count = 0
for k, v in mydict.items():
    count = count + k*v
    
for i in range(Q):
    if b[i] in mydict:
        if c[i] in mydict:
            score1 = mydict[b[i]]
            score2 = mydict[c[i]]
            
            count1 = score1* b[i]
            count2 = score1* c[i]
            del mydict[b[i]]
            mydict[c[i]] = score1 + score2
            count = count - count1 + count2

            
        else:
            count = count - b[i] * mydict[b[i]] + c[i] * mydict[b[i]]
            mydict[c[i]] = mydict[b[i]]
            del mydict[b[i]]
            

    
    print(count)

Solution (Python):
import collections
n=int(input())
a=input()
a=a.split()
a=[int(n) for n in a]
q=int(input())
al=collections.Counter(a)
s=sum(a)
for i in range (q):
    x=input()
    x0=x.split()
    b=int(x0[0])
    c=int(x0[1])
    r=al[b]*(c-b)
    s=s+r
    print(s)
    al[c]=al[c]+al[b]
    al[b]=0

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[list(map(int,input().split()))for i in range(Q)]
dic_A=dict()
sum_A=sum(A)
ans=list()
tmp=0
for i in A:
    if i in dic_A:
        dic_A[i]+=1
    else:
        dic_A[i]=1
for j in range(Q):
    if B[j][0] in dic_A:
        tmp=sum_A+ dic_A[B[j][0]]*(B[j][1]-B[j][0])
        ans.append(tmp)
        sum_A=tmp
        if B[j][1] not in dic_A:
            dic_A[B[j][1]]=dic_A[B[j][0]]
        else:
            dic_A[B[j][1]]+=dic_A[B[j][0]]
        dic_A[B[j][0]]=0
    else:
        if tmp==0:
            ans.append(sum_A)
        else:
            ans.append(tmp)
for k in ans:
    print(k)

Solution (Python):
import collections
N = int(input())
A_list = list(map(int, input().split()))
c = collections.Counter(A_list)
result = sum(A_list)
Q = int(input())

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

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:
    
    d = c-b
    if not b in C:
        C[b] = 0
    s += d*C[b]
    if not c in C:
        C[c] = 0
    C[c] += C[b]
    C[b] = 0
    print(s)

Solution (Python):
from collections import Counter
import sys

input = sys.stdin.readline


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

    d = Counter(A)
    
    ans = sum(A)

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

        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
n = int(input())


a = list(map(int, sys.stdin.readline().split()))

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

ans = sum(a)
for i in range(q):
    b, c = map(int, sys.stdin.readline().split())
    ans += (c-b) * l[b]
    l[c] += l[b]
    l[b] = 0
    print(ans)

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

for i in range (q):
    bc.append(list(map(int, input().split())))
num=[0]*(10**5+1)
for i in range(n):
    x=a[i]
    num[x]+=1

kk=0
for l in range(10**5+1):
    kk+=l*num[l]

for k in range (q):
    b=bc[k][0]
    c=bc[k][1]
    kk+=c*num[b]-b*num[b]
    num[c]+=num[b]
    num[b]=0
    print(kk)

Solution (Python):

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)


from collections import Counter,deque,defaultdict





n=n0()
A=n1()
Q=n0()
BC=n3(Q)

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

s=sum(A)

for b,c in BC:
    if d[b]==0:
        print(s)
        continue
    a=d[b]
    s+=(c-b)*a
    d[b]=0
    d[c]+=a
    print(s)

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


s = sum(A)
nord = [0]*(10**5 +1)

for a in A:
    nord[a] += 1

for b,c in zip(B,C):
    s +=  (c-b)*nord[b]
    nord[c] += nord[b]
    nord[b] = 0
    print(s)

Solution (Python):
from collections import Counter

n = input()
N = int(n)
A = [int(a) for a in input().split()]
q = input()
Q = int(q)
B, C = [], []
for i in range(Q):
    b, c = input().split()
    B.append(int(b))
    C.append(int(c))


my_dict = Counter(A)

def cals_sum(my_dict):
    values = [ my_dict[key]*key for key in my_dict.keys() if my_dict[key] != 0]
    
    
    
    return sum(values)
  
my_sum = cals_sum(my_dict)

for i in range(len(B)):
    b = B[i]
    c = C[i]
    tmpb = my_dict[b] 
    my_dict[b] = 0
    tmpc = my_dict[c]
    my_dict[c] = tmpb + tmpc
    
    my_sum += ((tmpb)*c - tmpb*b)

    print(my_sum)

Solution (Python):
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
a0,a1= np.unique(a,return_counts=True)
suma=np.sum(a)
a2=np.array([0]*100001)
for j in range(a0.size):
  a2[a0[j]]=a1[j]



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

Solution (Python):
summ=0
mapper=dict()
n=int(input())
arr=[int(i) for i in input().split()]
for i in range(0,n):
    temp=arr[i]
    if temp not in mapper:
        mapper[temp]=0
    mapper[temp]+=1
    summ+=temp
q=int(input())
for i in range(0,q):
    temp,temp1=[int(j) for j in input().split()]
    if((temp in mapper) and  temp1 not in mapper):
        summ-=temp*mapper[temp]
        summ+=temp1*mapper[temp]
        mapper[temp1]=0
        mapper[temp1]+=mapper[temp]
        mapper.pop(temp)
    elif((temp in mapper) and (temp1 in mapper)):
        summ-=temp*mapper[temp]
        summ+=temp1*mapper[temp]
        val=mapper[temp]
        mapper.pop(temp)
        mapper[temp1]=mapper[temp1]+val
    print(summ)

Solution (Python):
import collections
n=int(input())
a=dict(collections.Counter(list(map(int,input().split()))))
q=int(input())
ans=0
for i in a:
  ans+=i*a[i]
for i in range(q):
  b,c=map(int,input().split())
  ans+=a.get(b,0)*(c-b)
  print(ans)
  a[c]=a.get(b,0)+a.get(c,0)
  a[b]=0

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]


q = int(input())
b = []
for _ in range(q):
    b.append([int(i) for i in input().split()])

al = {}
suma = sum(a)

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

for i in range(q):
    if b[i][0] in al:
        suma += (b[i][1] - b[i][0]) * al[b[i][0]]
        
        if b[i][1] in al:
            
            al[b[i][1]] += al[b[i][0]]
            al[b[i][0]] = 0
        else:
            al[b[i][1]] = al[b[i][0]]
            al[b[i][0]] = 0
    print(suma)

Solution (Python):
import collections

n = int(input())
a = list(map(int, input().split()))
a_ = collections.Counter(a)
sum = 0
for x in a_.items():
    sum += x[0]*x[1]
q = int(input())
res = []
for _ in range(q):
    b, c = map(int, input().split())
    d = a_.get(b, 0)
    if d != 0:
        a_[b] = 0
        a_[c] += d
    sum += (c-b)*d
    res.append(sum)
for x in res:
    print(x)

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

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

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

Solution (Python):















mod = int(1e9+7)

INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    n = int(input())
    a = readInt()
    q = int(input())
    bc = []
    mx = max(a)
    for i in range(q):
        bc.append(readInt())
        mx = max(mx,max(bc[i][0],bc[i][1]))
    A = [0 for i in range(mx+1)]
    for x in a:
        A[x] += 1
    ans = sum(a)
    for i in range(q):
        b,c = bc[i]
        ans += (c-b)*A[b]
        A[c] += A[b]
        A[b] = 0
        print(ans)
    return

if __name__=='__main__':
    main()

Solution (Python):
from collections import Counter

def resolve():
  N = int(input())
  countA = Counter([int(x) for x in input().split(" ")])
  Q = int(input())

  sum_A = 0
  for a in countA.keys():
    sum_A += a*countA[a]

  for _ in range(Q):
    B, C = map(int, input().split(" "))
    if B in countA.keys():
      target_value = countA.pop(B)
      sum_A += (C - B) * target_value
      countA[C] += target_value
      print(sum_A)
    else:
      print(sum_A)


if __name__ == "__main__":
  resolve()

Solution (Python):
from collections import Counter

def update(dic, b, c):
    n = dic.pop(b)
    dic[c] = n + dic.get(c, 0)
    
def calc_diff(dic, b, c):
    if dic.get(b):
        return c * dic[b] - b * dic[b]
    return 0

def calc_total(dic):
    return sum(k * v for k, v in dic.items())

N = int(input())
lis = list(map(int, input().split()))
dic = dict(Counter(lis))
Q = int(input())
total = calc_total(dic)
for _ in range(Q):
    B, C = map(int, input().split())
    diff = calc_diff(dic, B, C)
    total += diff
    print(total)
    if diff:
        update(dic, B, C)

Solution (Python):
import itertools

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





distribution = {}
max_A = max(A)
max_BC = max(list(itertools.chain.from_iterable(BC)))
maximum = max([max_A, max_BC])

for i in range(maximum + 1):
    distribution[i] = 0

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


total = sum(A)
for i in range(Q):
    Bi = BC[i][0]
    Ci = BC[i][1]
    num_Bi = distribution.get(Bi)
    diff = (Ci - Bi) * num_Bi

    distribution[Bi] = 0
    
    distribution[Ci] += num_Bi

    total += diff
    print(total)

Solution (Python):
N =int(input())
A = list(map(int, input().split()))
Q = int(input())
A2 = [0]*(10**5)
B = [0]*Q
C = [0]*Q
for q in range(Q):
    B[q],C[q]=map(int,input().split())
total = 0
Adict = dict()
for n in range(N):
    A2[A[n]-1]+=1
    total+=A[n]
for q in range(Q):
    total = total - B[q]*A2[B[q]-1] + C[q]*A2[B[q]-1]
    A2[C[q]-1] += A2[B[q]-1]
    A2[B[q]-1] = 0
    print(total)

Solution (Python):
from collections import Counter

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

x = Counter(A)
s = sum(A)

res = list()
for _ in range(Q):
    b, c = list(map(int, input().split()))
    s += (c - b) * x[b]
    x[c] = x[b] + x[c]
    x[b] = 0
    res.append(s)

for r in res:
    print(r)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
q = int(input())
nums = Counter(a)
m = sum(a)
for _ in range(q):
  b,c = map(int,input().split())
  nums[c] += nums[b]
  k = nums[b]
  nums[b] = 0
  m = m + (c-b)*k
  print(m)

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

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

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

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

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

q=int(input())

y=[]

sam=sum(a)

for i in range(q):
  b,c=map(int,input().split())
  z[c]+=z[b]
  sam-=z[b]*b
  sam+=z[b]*c
  z[b]=0
  
  
  y.append(sam)

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

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


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

a = [0]*(10**5+3)

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

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

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())
import collections
A=collections.Counter(a)
S=0
for j in range(N):
	S=S+a[j]
for i in range(Q):
	if B[i] in A:
		f=A.pop(B[i])
		if C[i] in A:
			g=A[C[i]]
			h=f+g
		else:
			h=f
		A[C[i]]=h
	else:
		f=0
	S=S+f*(C[i]-B[i])
	print(S)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
mx=max(a)
num=[]
for i in range(100002):
  num.append(0)
bc=[]
for i in range(q):
  b,c=map(int,input().split())
  bc.append([b,c])
sm=0
for i in a:
  num[i]+=1
  sm+=i
for i in bc:
  b=i[0]
  c=i[1]
  sm-=b*num[b]
  sm+=c*num[b]
  num[c]=num[b]+num[c]
  num[b]=0
  print(sm)

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

  MAX = 10 ** 5

  total = sum(A)
  counts = [0] * (MAX + 1)

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

  for _ in range(Q):
    B, C = list(map(int, input().split()))
    diff = C - B
    t = counts[B]
    counts[B] = 0
    counts[C] += t

    total += diff * t
    print(total)

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

total = sum(a)
from collections import Counter
ans = Counter(a)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sa=sum(a)
bc=[]
for _ in range(q):
    b,c=map(int,input().split())
    bc.append([b,c])
d={}
for i in range(n):
    if a[i] not in d:
        d[a[i]]=1
    else:
        d[a[i]]+=1

for i in range(q):
    b=bc[i][0]
    c=bc[i][1]
    cb=c-b
    if b in d:
        sa+=cb*d[b]
        if c in d:
            d[c]+=d[b]
            d[b]=0
        else:
            d[c]=d[b]
            d[b]=0

    print(sa)

Solution (Python):
import sys
from collections import Counter










stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    N = int(stdin.readline().rstrip())
    
    A = list(map(int, stdin.readline().rstrip().split()))
    Q = int(stdin.readline().rstrip())
    
    
    
    
    ans = sum(A)
    counting = Counter(A)
    for i in range(Q):
        B, C = map(int, stdin.readline().rstrip().split())
        dif = C-B
        ans += counting[B]*dif
        counting[C] += counting[B]
        counting[B] = 0
        print(ans)


main()

Solution (Python):
from collections import Counter

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

Q = int(input())
Bs = []
Cs = []
for _ in range(Q):
  b,c = map(int,input().split())
  Bs.append(b)
  Cs.append(c)

cnt = Counter(As)
sum0 = sum(As)
for i in range(Q):
  sum0 += cnt[Bs[i]] * (Cs[i] - Bs[i])
  cnt[Cs[i]] += cnt[Bs[i]]
  cnt[Bs[i]] = 0
  print(sum0)

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
result = sum(A)
dictionaly = {}
for i in A:
    dictionaly[i] = dictionaly.get(i, 0) + 1
RESULT = []
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    sub = C - B
    if B in dictionaly:
        result += (dictionaly[B] * sub)
        dictionaly[C] = dictionaly.get(C,0) + dictionaly[B]
        dictionaly[B] = 0
    RESULT.append(result)
for i in RESULT:
    print(i)

Solution (Python):


from collections import defaultdict

N = int(input())
A = list(int(a) for a in input().split())
d = defaultdict(int)
ans = 0
for a in A:
    d[a] += 1
    ans += a

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

Solution (Python):
from collections import Counter as c
n = int(input())
x = list(map(int, input().split()))
d = dict(c(x))
q = int(input())
s = sum(x)
for i in range(q):
    x, y = list(map(int, input().split()))
    if y in d:
        if x in d:
            d[y] += d[x]
        else:
            d[x] = 0
    else:
        if x in d:
            d[y] = d[x]
        else:
            d[x] = 0
    s += d[x]*(y-x)
    print(s)
    d[x] = 0

Solution (Python):
import numpy as np

ans = 0

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

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

for a in npA:
	counts[a]+=1

ans = np.sum(npA)

Q=int(input())

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

	if 0 < counts[B]:
		diff = ( C - B ) * counts[B]
		counts[C] += counts[B]
		counts[B] = 0

	ans += diff
	print(ans)

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

app = [0] * 100001
for num in a:
  app[num] += 1

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

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

numbers = [0] * (10**5+2)
total = sum(A)

for a in A:
    numbers[a] += 1

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

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

n_dic = {}
for i in n_list:
    n_dic.setdefault(i, 0)
    n_dic[i] += 1

result = 0
for k in n_dic:
    result += k * n_dic[k]

for i in range(q):
    b, c = map(int, input().split())
    if b in n_dic:
        n_dic.setdefault(c, 0)
        n_dic[c] += n_dic[b]
        result = result + (c-b)*n_dic[b]
        del n_dic[b]

    print(result)

Solution (Python):
import sys
from collections import deque, Counter
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    n = I()
    a = list(IL())
    C = Counter(a)
    m = sum(a)
    q = I()
    s = []
    for rep in range(q):
        b,c = IL()
        if b in C:
            m += (c-b)*C[b]
            C[c] += C[b]
            C[b] = 0
        s.append(m)
    for rep in s:
        print(rep)
    return

if __name__=='__main__':
    Main()