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



from math import *
from copy import *					
from heapq import *					
from string import *				
from random import *				
from bisect import *				
from sys import stdin				
from sys import maxsize				
from operator import *				
from itertools import *				
from decimal import Decimal 		
from collections import Counter		
from collections import defaultdict 

'''

'''

def solve(l):
	n = len(l)
	

N = int(stdin.readline())
l = list(map(int, stdin.readline().rstrip().split()))
q = int(input())
s = sum(l)
d = dict(Counter(l))
for qq in range(q):
	b,c = list(map(int, stdin.readline().rstrip().split()))
	if(b not in d):
		print(s)
	else:
		s += d[b]*(c-b)
		if(c in d):
			d[c] += d[b]
		else:
			d[c] = d[b]
		d[b] = 0
		print(s)

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

sum_ = sum(A)
counter = dict(collections.Counter(A))


for i in range(Q):
    b, c = BC[i]
    if not b in counter:
        counter[b] = 0
    if not c in counter:
        counter[c] = 0
    cnt = counter[b]
    sum_ += (c-b)*cnt
    print(sum_)
    counter[b] -= cnt
    counter[c] += cnt

Solution (Python):
from collections import defaultdict
inpl = lambda: list(map(int,input().split()))
N = int(input())
A = inpl()
D = defaultdict(int)
for a in A:
    D[a] += 1
Q = int(input())
S = sum( k*v for k, v in D.items())
for _ in range(Q):
    B, C = inpl()
    S += (C-B) * D[B]
    D[C] += D[B]
    del D[B]
    print(S)

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

M = 10**5

memo = [0]*(M+1)

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

Q = int(input())

s = sum(i*c for i,c in enumerate(memo))
for _ in range(Q):
    b,c = map(int,input().split())

    cnt = memo[b]
    s += (c-b)*cnt
    memo[c] += memo[b]
    memo[b] = 0
    print(s)

Solution (Python):
from collections import Counter

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

ctr = Counter(A)
ans = sum(A)
for b, c in X:
    v = ctr[b]
    ans = ans - b * v + c * v
    ctr[b] -= v
    ctr[c] += v
    print(ans)

Solution (Python):


from collections import defaultdict
from heapq import heappush, heappop
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.buffer.readline

INF = 10 ** 10


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


def dp(*x):  
    print(*x)


def main():
    N = int(input())
    AS = map(int, input().split())
    sum = 0
    for a in AS:
        count[a] += 1
        sum += a
    Q = int(input())
    for q in range(Q):
        B, C = map(int, input().split())
        sum += (C - B) * count[B]
        count[C] += count[B]
        count[B] = 0
        print(sum)


def _test():
    import doctest
    doctest.testmod()


if __name__ == "__main__":
    import sys
    argv = sys.argv
    if len(sys.argv) == 1:
        
        main()
    elif sys.argv[1] == "-t":
        _test()
    else:
        input = open(sys.argv[1]).buffer.readline

Solution (Python):
f=lambda:list(map(int,input().split()))
n,l,q=f()[0],f(),f()[0]
from collections import *
C=Counter(l)
s=sum(k*C[k] for k in C)
for _ in range(q):
  b,c=f()
  v=C[b]
  C[b]-=v
  C[c]+=v
  s+=(c-b)*v
  print(s)

Solution (Python):
from collections import Counter

N = int(input())
A = [int(x) for x in input().split()]
C = Counter(A)
Q = int(input())
S = sum(n * c for n, c in C.items())
for i in range(Q):
    b, c = map(int, input().split())
    t = C[b]
    S += (c-b)*t
    C[c] += t
    C[b] = 0
    print(S)

Solution (Python):
from collections import Counter
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
tot = sum(A)
Q = int(input())
ans = [-1] * Q
for i in range(Q):
    b, c = map(int, input().split())
    x = C[b]
    C[b] = 0
    C[c] += x
    tot -= x * b
    tot += x * c
    ans[i] = tot

print(*ans, sep="\n")

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

cnt = {}
S = sum(A)
for a in A:
    cnt[a] = cnt.get(a,0)+1
for b,c in BC:
    n = cnt.get(b,0)
    S += (c-b)*n
    cnt[b] = 0
    cnt[c] = cnt.get(c,0)+n
    print(S)

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

d = {}
for a in A:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
tmp_sum = sum(A)


for _ in range(Q):
    b, c = list(map(int, input().split()))
    if b in d:
        b_cnt = d[b]
    else:
        b_cnt = 0

    d[b] = 0
    if c in d:
        d[c] += b_cnt
    else:
        d[c] = b_cnt

    sum_diff = (c - b) * b_cnt
    tmp_sum += sum_diff
    print(tmp_sum)

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

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

S = sum(a)
cnt = Counter(a)
for _ in range(q):
    b, c = map(int, input().split())
    if cnt.get(b):
        ans = S - b*cnt[b] + c*cnt[b]

        if cnt.get(c):
            cnt[c] += cnt[b]
            cnt[b] = 0
        else:
            cnt[c] = cnt[b]
            cnt[b] = 0
        S = ans
    print(S)

Solution (Python):

from collections import Counter, defaultdict, deque

import sys


buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, Q, BC):
    ac = Counter(A)
    ans = sum(A)
    for b, c in BC:
        n = ac[b]
        ac[b] = 0
        ac[c] += n
        ans += -n*b + n*c
        print(ans)
    


def main():
    N = read_int()
    A = read_int_n()
    Q = read_int()
    BC = [read_int_n() for _ in range(Q)]
    slv(N, A, Q, BC)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import itertools

import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines




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

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

total = 0
for a in A:
    count[a] += 1
    total += a

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

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

Solution (Python):
N = int(input())
A = input().split()
A = [int(x) for x in A]
Q = int(input())
S = sum(A)
L = [0 for _ in range(10**5)]

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

for i in range(Q):
    B,C = map(int,input().split())
    n = L[B-1]
    if n > 0:
        S = S - n*(B-C)
        L[C-1] += n
        L[B-1] = 0
    print(S)

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
class BIT:
  def __init__(self, n):
    self.n = n
    self.data = [0] * (n + 1)
    self.el = [0] * (n + 1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.data[i]
      i -= i & -i
    return s
  def add(self, i, x):
    self.el[i] += x
    while i <= self.n:
      self.data[i] += x
      i += i & -i
  def get(self, i, j = None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i)
  def lowerbound(self, s):
    x = 0
    y = 0
    for i in range(self.n.bit_length(), -1, -1):
      k = x + (1 << i)
      if k <= self.n and (y + self.data[k] < s):
        y += self.data[k]
        x += 1 << i
    return x + 1
fwk = BIT(10 ** 5 + 1)
for x in a: fwk.add(x, x)
Q = int(input())
for _ in range(Q):
  b, c = map(int, input().split())
  fwk.add(c, fwk.get(b) // b * c)
  fwk.add(b, -fwk.get(b))
  print(fwk.sum(10 ** 5))

Solution (Python):
import collections
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
table = defaultdict(int)
for i in a:
    table[i] += 1
q = int(input())
bc = [tuple(map(int, input().split())) for _ in range(q)]
whole = sum(a)
ans = 0
for b, c in bc:
    if b in table:
        whole = whole + table[b] * (c-b)
        print(whole)
        table[c] += table[b]
        table[b] = 0
    else:
        print(whole)

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

Solution (Python):
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

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


def main():
	N = int(input())
	A = getlist()
	Q = int(input())
	D = defaultdict(int)
	S = sum(A)
	for i in range(N):
		D[A[i]] += 1
	for i in range(Q):
		B, C = getlist()
		S += (C - B) * D[B]
		D[C] += D[B]
		D[B] = 0
		print(S)


if __name__ == '__main__':
	main()

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

Solution (Python):


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

D = {}
for i in range(1, 10**5+1):
    D[i] = 0

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

ret = sum(A)

for b, c in BC:
    sa = c - b
    
    
    plus = sa * D[b]
    D[c] += D[b]
    D[b] = 0
    ret += plus
    print(ret)

Solution (Python):
from collections import defaultdict

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

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

S=sum(A)

Q = int(input())

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

    m = D[b]

    D[c]+=D[b]
    D[b]=0
    diff = (c-b)*m
    S+=diff
    print(S)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    aa=LI()
    q=II()
    mx=10**5+5
    cnt=[0]*mx
    for a in aa:cnt[a]+=1
    s=sum(aa)
    for _ in range(q):
        b,c=MI()
        s+=(c-b)*cnt[b]
        cnt[c]+=cnt[b]
        cnt[b]=0
        print(s)

main()

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


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

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


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

Solution (Python):
N=int(input())
l=list(map(int,input().split()))
d={}
for j in range(N):
	if l[j] not in d:
		d[l[j]]=0
	d[l[j]]+=1
ans=sum(l)
Q=int(input())
for i in range(Q):
	B,C=map(int,input().split())
	if B in d:
		v=d[B]
		if C in d:
			d[C]+=d[B]
		else:
			d[C]=d[B]
		del(d[B])
		ans+=(C-B)*v
	print(ans)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, numpy, string
import sys
from bisect import bisect_left, bisect_right, insort
from collections import deque
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def MS(): return map(str, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())




n = I()
a = LI()
box = [0] * 100000
q = I()
for i in range(n):
    box[a[i]-1] += 1
ans = 0
for i in range(len(box)):
    ans += (i+1) * box[i]

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

Solution (Python):
N = int(input())
Ali = list(map(int,input().split()))
Q = int(input())
s = 0

alin = [0]*100001
for i in range(N):
  alin[Ali[i]] += 1
  s += Ali[i]
  
for i in range(Q):
  B,C = map(int,input().split())
  alin[C] += alin[B]
  s += (C-B)*alin[B]
  alin[B] = 0
  print(s)

Solution (Python):
import sys

N=int(sys.stdin.readline())

A=[int(i) for i in sys.stdin.readline().split()]

Q=int(sys.stdin.readline())

ans=sum(A)

arr=[0]*(pow(10,5)+1)

for g in range(N):
	arr[A[g]]+=1

for q in range(Q):
	[B,C]=[int(x) for x in sys.stdin.readline().split()]
	ans=ans+arr[B]*(-B+C)
	arr[C]+=arr[B]
	arr[B]=0
	print(ans)

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

D = dict()
for a in A:
    if a not in D.keys():
        D[a] = 0
    D[a] += 1

ans = 0
for k, v in D.items():
    ans += k * v
    
for _ in range(Q):
    B, C = [int(n) for n in input().split()]
    
    if B in D.keys():
        count = D[B]
        
        if C not in D.keys():
            D[C] = 0
        D[C] += count
        del D[B]
        
        ans += (C - B) * count

    print(ans)

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

sum = sum(a)

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

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

Q = int(input())
ans = [0 for i in range(Q)]

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

Solution (Python):
n=int(input())
from collections import Counter as co
g=list(map(int,input().split()))
l=co(g)
f=sum(g)
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    if b in l:
        v=l[b]
        l[b]=0
        l[c]+=v
        f=f-v*b+c*v
    print(f)

Solution (Python):
import sys
from collections import Counter
N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7

N = int(sys.stdin.readline())
A = [int(x) for x in sys.stdin.readline().split()]

Q = int(sys.stdin.readline())

sum = sum(A)

C = Counter(A)

for i in range(Q):
    b, c = map(int, sys.stdin.readline().split())

    
    n = C[b]
    
    
    sum -= b * n
    C[b] = 0
    
    
    sum += c * n
    C[c] += n
    print(sum)

Solution (Python):
def main():
    n = int(input())
    arr = list(map(int,input().split()))
    nums = {}
    for i in arr:
        if i not in nums.keys():
            nums[i] = 0

        nums[i] += 1

    total = sum(arr)
    ans = []
    q = int(input())
    for i in range(q):
        b,c = map(int,input().split())
        if b in nums.keys():
            total -= b*nums[b]
            total += c*nums[b]
            count = nums[b]
            del nums[b]
            if c not in nums.keys():
                nums[c] = 0

            nums[c] += count
            
        ans.append(total)

    for i in ans:
        print(i)

main()

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

Solution (Python):
from collections import Counter
def get_sum(count):
    return sum([key * count[key] for key in count.keys()])
n = int(input())
a = list(map(int, input().split()))
q = int(input())
count = Counter(a)
s = sum(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    count[c] += count[b]
    s -= count[b] * b
    s += count[b] * c
    count[b] = 0
    print(s)

Solution (Python):
import collections

n = int(input())
al = [int(i) for i in input().split()]
q = int(input())

counter = collections.Counter(al)

total = 0
for (v, c) in counter.most_common():
  total += v * c
for _ in range(q):
  b, c = [int(i) for i in input().split()]
  count = counter[b]
  counter[b] = 0
  counter[c] += count
  total = total + (c - b) * count
  print(total)

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

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

Solution (Python):


from sys import stdin, stdout
 
def input():
    return stdin.readline().strip()
    
input()
A = input().split(' ')
A = list(map(int, A))
Q = int(input())

curr_sum = sum(A)
curr_freqs = dict()

for a in A:
    if a not in curr_freqs: curr_freqs[a] = 0
    curr_freqs[a] += 1

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

    if B not in curr_freqs:
        print(curr_sum)
        continue

    if C not in curr_freqs: curr_freqs[C] = 0

    curr_sum += (C - B)*curr_freqs[B]

    curr_freqs[C] += curr_freqs[B]
    curr_freqs[B] = 0


    print(curr_sum)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
X = [0]*(100001)
S = 0
for a in A:
    X[a] += 1
    S += a

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n):
    res = [None] * n
    for i in range(n):
        res[i] = II()
    return res
def LIR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI()
    return res
def FR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LFR(n):
    res = [None] * n
    for i in range(n):
        res[i] = IF()
    return res
def LIR_(n):
    res = [None] * n
    for i in range(n):
        res[i] = LI_()
    return res
def SR(n):
    res = [None] * n
    for i in range(n):
        res[i] = S()
    return res
def LSR(n):
    res = [None] * n
    for i in range(n):
        res[i] = LS()
    return res
mod = 1000000007
inf = float('INF')


def solve():
    n = II()
    a = LI()
    q = II()
    bc = LIR(q)
    d = defaultdict(int)
    for i in a:
        d[i] += 1
    ans = 0
    for key, value in d.items():
        ans += key * value 
    for b, c in bc:
        x = d[b]
        ans -= x * b
        d[b] = 0
        d[c] += x
        ans += x * c
        print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

from decimal import *
import heapq
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
a = LIST()
q = INT()
arr = [0 for _ in range(10 ** 5 + 100)]
for i in a:
    arr[i] += 1
ans = sum(a)
for _ in range(q):
    b, c = MAP()
    arr[c] += arr[b]
    ans += (c - b) * arr[b]
    arr[b] = 0
    print(ans)

Solution (Python):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    A = list(map(int, input().split()))
    l = Counter(A)
    
    Q = int(input())
    ans = sum(A)
    
    for i in range(Q):
        B, C = map(int, input().split())
        tmp = l[B]
        l[C]+=tmp
        l[B] = 0
        ans += (C-B)*tmp
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

lst=[0]*(10**5+1)
ans=0
for i in range(n):
    lst[a[i]]+=1
    ans+=a[i]


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

Solution (Python):
from collections import Counter
n = int(input())
a = [int(x) for x in input().split()]

f = Counter()

for x in a:
    f[x] += 1

ans = 0
for x, val in f.items():
    ans += x*val

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

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

Solution (Python):
import sys

stdin = sys.stdin


def ni(): return int(ns())


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


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  


N = ni()
A_array = na()
a_dic = {}

ans = 0
for a in A_array:
    ans += a
    if a in a_dic:
        a_dic[a] += 1
    else:
        a_dic[a] = 1

ans_array = []

Q = ni()
for _ in range(Q):
    b, c = na()
    if b in a_dic:
        if c in a_dic:
            a_dic[c] += a_dic[b]
        else:
            a_dic[c] = a_dic[b]
        ans = ans + (c - b) * a_dic[b]
        a_dic[b] = 0
    ans_array.append(ans)

print("\n".join(map(str, ans_array)))

Solution (Python):
import math
import time
from collections import defaultdict,deque
from sys import stdin,stdout
from bisect import bisect_left,bisect_right
n=int(input())
a=list(map(int,stdin.readline().split()))
q=int(input())
count=defaultdict(lambda:0)
for i in a:
    count[i]+=1
s=sum(a)
for _ in range(q):
    b,c=map(int,stdin.readline().split())
    s+=count[b]*(c-b)
    count[c]+=count[b]
    count[b]=0
    print(s)

Solution (Python):
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
q = int(input())
x = [list(map(int, input().split())) for _ in range(q)]

s = sum(a)
d = defaultdict(int)
for ai in a:
    d[ai] += 1

for xi in x:
    b, c = xi
    s += d[b] * (c - b)
    print(s)
    d[c] += d[b]
    d[b] = 0

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
BC = []
B = 0
C = 1
for i in range(q):
   BC.append(list(map(int, input().split())))

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

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

for bc in BC:
    bCount = cnt[bc[B]]
    cnt[bc[B]] = 0
    cnt[bc[C]] += bCount
    S += (bc[C] - bc[B])*bCount
    print(S)

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

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

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

N = int(input())
As = list(map(int, input().split()))
Q = int(input())
BCs = [tuple(map(int, input().split())) for _ in range(Q)]

cnt = Counter(As)
ans = sum(As)

anss = []
for B, C in BCs:
    num = cnt[B]
    diff = num * (C-B)
    ans += diff
    anss.append(ans)
    cnt[B] -= num
    cnt[C] += num

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

Solution (Python):
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    Q, *BC = map(int, read().split())

    total = sum(A)
    d = Counter(A)
    for b, c in zip(BC[::2], BC[1::2]):
        total -= b * d[b]
        total += c * d[b]
        d[c] += d[b]
        d[b] = 0
        print(total)

    return


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
A_dict = {}
res = 0
for a in A_list:
    res += a
    if a in A_dict.keys():
        A_dict[a] += 1
    else:
        A_dict[a] = 1

Q = int(input())
for i in range(Q):
    b,c = map(int, input().split())
    tmp = A_dict[b] if b in A_dict.keys() else 0
    A_dict[b] = 0

    if c in A_dict.keys():
        A_dict[c] += tmp
    else:
        A_dict[c] = tmp

    res += c*tmp - b*tmp
    print(res)

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

n = int(input())
a = [int(x) for x in input().split()]
chk = [0] * 100001
now = sum(a)

for x in a:
    chk[x] += 1

for _ in range(int(input())):
    b, c = map(int, input().split())
    now = now - chk[b] * b + chk[b] * c
    chk[c] += chk[b]
    chk[b] = 0

    print(now)

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

total = sum(A_list)

table = {}
for A in A_list:
	if A in table:
		table[A] += 1
	else:
		table[A] = 1

Q = int(input())
for _ in range(Q):
	B, C = map(int, input().split())
	if B in table:
		count = table[B]
		total -= count * B
		total += count * C
		if C in table:
			table[C] += count
		else:
			table[C] = count
		del table[B]
	print(total)

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = map(int,readline().split())
*a, = map(int,readline().split())
q,*bc = map(int,read().split())

from collections import Counter
d = Counter(a)
res = sum(a)


m = iter(bc)
for b,c in zip(m,m):
    if b in d:
        v = d[b]
        d[b] = 0
        d[c] += v
        res += (c-b)*v
    print(res)

Solution (Python):
import sys
from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = tuple(map(int, readline().split()))
Q = int(readline())
m = map(int, read().split())

counter = defaultdict(int)
for x in A:
    counter[x] += 1

total = sum(A)
for b, c in zip(m, m):
    total += (c - b) * counter[b]
    counter[c] += counter[b]
    counter[b] = 0
    print(total)

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

aa=list(map(int,input().split()))
q = int(input())
b=[]
c=[]
from collections import Counter as cnt
ans = sum(aa)

a = dict(cnt(aa))
for i in range(q):
    bb,cc=list(map(int,input().split()))
    b.append(bb)
    c.append(cc)
    if bb not in a:
        a[bb]=0
    if cc not in a:
        a[cc]=0



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

    a[b[i]] = 0
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
s = sum(a)
d = {}
for i in a:
    if i not in d:
        d[i] = 0
    d[i] += 1
for _ in range(q):
    b,c = map(int,input().split())
    if b not in d:
        print(s)
        continue
    if c not in d:
        d[c] = 0
    new = d[c] + d[b]
    s -= d[b]*b
    s += (new - d[c])*c
    d[b] = 0
    d[c] = new
    print(s)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
val = [0]*(10**5)
for a in A:
    val[a-1] += 1

t = 0
for i,n in enumerate(val):
    t += (i + 1)*n

ans = []
Q = int(input())
for _ in range(Q):
    B,C = [int(i) for i in input().split()]
    t += (C - B)*val[B-1]
    val[C-1] += val[B-1]
    val[B-1] = 0
    ans.append(str(t))
print("\n".join(map(str, ans)))