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 collections import Counter

n = int(input())
a = input().split()
q = int(input())
counter = Counter(a)
total = 0
for k, v in counter.items():
    total += int(k) * v
for _ in range(q):
    b, c = input().split()
    if counter.get(b):
        total = total - int(b) * counter.get(b) + int(c) * counter.get(b)
        if counter.get(c):
            counter[c] = counter.get(b) + counter.get(c)
        else:
            counter[c] = counter.get(b)
        counter[b] = 0
    print(total)

Solution (Python):

import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline


n = int(input())
arr = list(map(int , input().split()))
q = int(input())
m = defaultdict(int)
for i in arr:
	m[i]+=1
ss = sum(arr)
for ii in range(q):
	b , c = map(int , input().split())
	temp = m[b]
	del m[b]
	m[c] += temp
	ss = ss - b*temp + c*temp
	print(ss)

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

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

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

s = 0
for i, x in enumerate(cnt):
    s += i * x

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

Solution (Python):
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())
numdic = {}
for i in range(n):
  if a[i] in numdic:
    numdic[a[i]] += 1
  else:
    numdic[a[i]] = 1
sum = 0
for x in numdic:
  sum += x*numdic[x]
for i in range(q):
  if b[i] in numdic:
    sum += (c[i]-b[i]) * numdic[b[i]]
    if c[i] in numdic:
      numdic[c[i]] += int(numdic[b[i]]) 
    else:  
      numdic[c[i]] = int(numdic[b[i]])
    del numdic[b[i]]
  print(sum)

Solution (Python):
n = int(input())
d = {}
for a in list(map(int, input().split())):
    if a not in d:
        d[a] = 0
    d[a] += 1
r = sum(k*d[k] for k in d.keys())

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

Solution (Python):
from collections import Counter

N = int(input())

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

dic = Counter(A)

Q = int(input())

res = sum(A)

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

Solution (Python):
import sys
from collections import deque
import copy
def main():
    N = int(input())
    MAX = 10 ** 15
    A = [int(i) for i in input().split()]
    dict_num = {}
    num_sum = 0
    for i in range(N):
        if dict_num.get(A[i]) is None:
            dict_num[A[i]] = 1
        else:
            dict_num[A[i]] += 1
        num_sum += A[i]
    Q = int(input())
    for i in range(Q):
        b, c = map(int, input().split())
        if dict_num.get(b) is None:
            print(num_sum)
        elif dict_num[b] == 0:
            print(num_sum)
        else:
            num_sum += (c - b) * dict_num[b]
            print(num_sum)
            if dict_num.get(c) is None:
                dict_num[c] = dict_num[b]
            else:
                dict_num[c] += dict_num[b]
            dict_num[b] = 0



if __name__ == '__main__':
    main()

Solution (Python):
import collections

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

cnt_dict=collections.Counter(a)
sum=sum(a)

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

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

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

d = dict(Counter(A))
ans = sum(A)
for i in range(Q):
    b, c = map(int, input().split())
    
    if(b not in d):
        d[b] = 0
    if(c not in d):
        d[c] = 0
    
    if(b >= c):
        n = d[b]*(c-b)
    else:
        n = d[b]*(c-b)
        
    ans += n
    
    if(c in d):
        d[c] += d[b]
    else:
        d[c] = d[b]
    d[b] = 0
    
    print(ans)

Solution (Python):

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

arg=[0]*(100005)
for a in A:
    arg[a]+=1
ans=sum(A)
for _ in range(Q):
    a,b=map(int,input().split())
    ans+=arg[a]*(b-a)
    print(ans)
    arg[b]+=arg[a]
    arg[a]=0

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
d=[0]*100001
for i in a:
    d[i-1]+=1
s=0
for num,i in enumerate(d):
    s+=i*(num+1)

bb=[]
cc=[]
for i in range(q):
    b,c=map(int,input().split())
    bb.append(b)
    cc.append(c)

for b,c in zip(bb,cc):
    k=d[b-1]
    s+=(c-b)*k
    d[b-1]=0
    d[c-1]+=k

    print(s)

Solution (Python):

N = int(input())
L = [int(i) for i in input().split()]
Q = int(input())
order = [[] for _ in range(Q)]

for i in range(Q):
    order[i] = [int(i) for i in input().split()]

cnt = {}
for i in L:
    if i in cnt:
        cnt[i] += 1
        
    else:
        cnt[i] = 1

ans = [sum(L)]
for i in order:
    if i[0] not in cnt:
        print(ans[-1])
        continue
    temp = cnt[i[0]] * (i[1] - i[0])
    ans.append(temp + ans[-1])
    print(ans[-1])
    
    if i[1] in cnt:
        cnt[i[1]] += cnt[i[0]]
    else:
        cnt[i[1]] = cnt[i[0]]

    del cnt[i[0]]

Solution (Python):
def sum_dict(d, sum, b, c):
    val = 0
    if b in d:
        val = d[b]
        sum -= (b * val)
        del d[b]
        d[c] = d.get(c, 0) + val
        sum += (c * val)
    return sum, d
    
n = int(input())
a = [int(x) for x in input().split()]
d = {}
for i in a:
    d[i] = d.get(i, 0) + 1
s = sum(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    s, d = sum_dict(d, s, b, c)
    print(s)

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

opelates = []
for _ in range(q):
    opelates.append(list(map(int, input().split())))

numCntList = [0 for _ in range(10**5+1)]
for num in ali:
    numCntList[num] += 1
total = sum(ali)

for op in opelates:
    pre = op[0]
    nex = op[1]
    preCnt = numCntList[pre]
    if preCnt >= 1:
        numCntList[pre] = 0
        numCntList[nex] += preCnt
        total += (nex-pre)*preCnt
    print(total)

Solution (Python):
def main():
    import collections

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

    summ = sum(A_s)
    CNT_A = collections.Counter(A_s)


    for i in range(Q):
        before, after = map(int, input().split())
        
        tmp_cnt = CNT_A[before]
        
        if (tmp_cnt > 0):
            tmp_before = CNT_A[before] * (after - before)
            summ += tmp_before

            N_before=CNT_A[before]

            del CNT_A[before]
            N_after = CNT_A[after]
            
            if (N_after > 0):
                CNT_A[after] = N_after + N_before
            else:
                CNT_A[after] = N_before
            
        
        print(summ)


if __name__ == '__main__':
    main()

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.append(b);C.append(c)
d={}
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 = B[i]
    c = C[i]
    if b not in d:
        print(s)
    else:
        if c not in d:
            s=s+(c-b)*d[b]
            print(s)
            d[c] = d[b]
            del d[b]
        else:
            s=s+(c-b)*d[b]
            print(s)
            d[c] += d[b]
            del d[b]

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

ans=0
for k,v in C.items():
    ans+=k*v

for i in range(q):
    if S[i][0] in C:
        if C[S[i][0]]>=1:
            ans+=(S[i][1]-S[i][0])*C[S[i][0]]
            C[S[i][1]]+=C[S[i][0]]
            C[S[i][0]]=0
    print(ans)

Solution (Python):
import collections 
N=int(input())
A=list(map(int,input().split()))
A_count = collections.Counter(A)

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

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

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

    for i in range(q):
        b, c = map(int, input().split())
        if not b in dic1:
            print(total)
            continue
        bcount = dic1[b]
        if bcount == 0:
            print(total)
            continue
        inc = bcount * (c-b)
        if c in dic1:
            dic1[c] += bcount
        else:
            dic1[c] = bcount
        dic1[b] = 0
        total += inc
        print(total)
resolve()

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

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

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

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

ans = sum(a)


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

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

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

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

ans = sum(ans_lst)

val_dic = {}
for i in ans_lst:
  if i in val_dic:
    val_dic[i] += 1
  else:
    val_dic[i] = 1


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

  if b in val_dic:
    z = val_dic[b]
  
  ans = ans + (c - b) * z
  print(ans)

  if b in val_dic:
    num = val_dic[b]
    val_dic[b] = 0
    if c in val_dic:
      val_dic[c] += num
    else:
      val_dic[c] = num

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

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left
from functools import reduce
from decimal import Decimal, getcontext

def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n = i_input()
    num_list = i_list()
    q = i_input()
    current_sum = sum(num_list)
    cnt_list = Counter(num_list)
    
    

    

    for _ in range(q):
        b, c = i_map()
        b_cnt = cnt_list[b]
        cnt_list[b] = 0
        cnt_list[c] += b_cnt
        current_sum += (c-b) * b_cnt
        print(current_sum)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
q = int(input())
query = [tuple(map(int, input().split())) for _ in range(q)]

ctr = Counter(A)
sum_ini = sum(A)

for b, c in query:
    x = ctr[b]
    ctr[c] += x
    ctr[b] -= x
    sum_ini += (c - b) * x
    print(sum_ini)

Solution (Python):
n=int(input())
l=[0]*10**5
A=list(map(int,input().split()))

for a in A:
    l[a-1]+=1

ans=sum(A)

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

Solution (Python):
from collections import defaultdict, Counter
N = int(input())
A = list(map(int, input().split()))
count = Counter(A)
count = dict(count)
Q = int(input())

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

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

c = collections.Counter(a)

s = sum(a)
for bi, ci in bc:
    if bi not in c:
        print(s)
    else:
        s = s - c[bi] * bi + c[bi] * ci
        print(s)
        if ci in c:
            c[ci] = c[ci] + c[bi]
            c[bi] = 0
        else:
            c[ci] = c[bi]
            c[bi] = 0

Solution (Python):
from math import floor,ceil,sqrt
import fractions
from collections import deque
import itertools
from collections import deque
import collections
import heapq
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
q=[list(map(int,input().split())) for _ in range(Q)]
nums={}
su=sum(A)

for i in range(N):
    if nums.get(A[i])==None:
        nums[A[i]]=1
    else:
        nums[A[i]]=nums[A[i]]+1

for i in range(Q):
    b,c=q[i]
    bnum=nums.pop(b,None)
    cnum=nums.pop(c,None)
    
    if bnum!=None:
        if cnum!=None:
            su=su+(c-b)*bnum
            nums[c]=cnum+bnum
            print(su)
        else:
            su = su + (c - b) * bnum
            nums[c]=bnum
            print(su)
    else:
        if cnum!=None:
            nums[c]=cnum
        print(su)

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

ans = sum(A)
dp = [0]*(10**5+1)
for i in A:
    dp[i] += 1

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

Solution (Python):
R = lambda: map(int, input().split())
R()
a = [0] * 100001
s = 0
for x in R():
  s += x
  a[x] += 1
q, = R()
for _ in range(q):
  b, c = R()
  s += (c - b) * a[b]
  print(s)
  a[c]+=a[b]
  a[b] = 0

Solution (Python):
from sys import stdin, stdout
from collections import Counter
for _ in range(1):
    n=int(stdin.readline())
    a=list(map(int, stdin.readline().split()))
    d=Counter(a);s=sum(a)
    q=int(stdin.readline())
    for qq in range(q):
        old,new=map(int, stdin.readline().split())
        times=d[old]
        s+=times*(new-old)
        d[old]=0
        d[new]=d.get(new,0)+times
        print(s)

Solution (Python):
n = int(input())
m = {}
tot = 0
for a in map(int,input().split()):
  tot += a
  if a not in m:
    m[a]=0
  m[a] += 1
q = int(input())
for step in range(q):
  b,c = map(int,input().split())
  if c not in m:
    m[c] = 0
  if b in m:
    m[c] += m[b]
    tot += m[b]*(c-b)
    m[b] = 0
  print(tot)

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

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

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

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)


d = {a: 0 for a in set(A)}
for a in A:
    d[a] += 1

ans = sum(A)
for b, c in zip(B, C):
    if b not in d.keys():
        print(ans)
        continue

    ans += c * d[b] - b * d[b]

    if c not in d.keys():
        d[c] = 0
    d[c] += d[b]
    del d[b]

    print(ans)

Solution (Python):
def main():
    import sys
    from collections import Counter
    N = int(sys.stdin.readline())
    A = list(map(int, sys.stdin.readline().split()))
    Q = int(sys.stdin.readline())
    BC = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]
    C = Counter(A)
    '''
    L = [0 for _ in range(10**5+1)]
    for key, value in C.items():
        L[key] = value
    '''
        
    ans = 0
    for bc in BC:
        C[bc[1]] += C[bc[0]]
        tmp = C[bc[0]]
        C[bc[0]]= 0
        
        if ans == 0:
            for key, value in C.items():
                ans += key*value
        else:
            ans +=(bc[1]-bc[0])*tmp
        print(ans)
        



if __name__=='__main__':
    main()

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

q = int(input())

memo = {}

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

total = sum(A)

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

    total += (c - b) * memo[b]

    memo[c] += memo[b]
    memo[b] = 0

    print(total)

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

Solution (Python):
import numpy as np
N = int(input())
A = [int(x) for x in input().split(" ")]
Q = int(input())

S = 0

query = []
for _ in range(Q):
    query.append([int(x) for x in input().split(" ")])

array = np.zeros(10**5, dtype=int)
for a in A:
    S += a
    array[a-1] += 1


for B, C in query:
    
    D = array[B-1]
    S += (C - B) * D
    print(S)
    array[C-1] += D
    array[B-1] = 0

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
l=[]
for _ in range(Q):
    l.append(list(map(int,input().split())))
c = collections.Counter(A)
sum=sum(A)
for i in range(Q):
    if c[l[i][0]] == 0:
        print(sum)
        continue
    else:
        sum+=c[l[i][0]]*(l[i][1]-l[i][0])
        c[l[i][1]]+=c[l[i][0]]
        del c[l[i][0]]
        print(sum)

Solution (Python):
import bisect
from collections import defaultdict
d = defaultdict(int)
n=int(input())
a=list(map(int,input().split()))
q=int(input())
a.sort()
for key in a:
  d[key] += 1

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

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
Q=int(input())
X=[i+1 for i in range((10**5))]
dic={}
ans=sum(A)
for i in A:
	try:
		dic[i]+=1
	except:
		dic.setdefault(i,1)
for i in range(Q):
	[B,C]=[int(i) for i in input().split()]
	try:
		ans+=dic[B]*(C-B)
		try:
			dic[C]+=dic[B]
		except:
			dic.setdefault(C,dic[B])
		dic[B]=0
	except:
		ans+=0
	print(ans)

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

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

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

import collections
c = collections.Counter(a)

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

    b=c[l[i][0]]
    c[l[i][0]]=0
    c[l[i][1]]+=b

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

dictA = {}

for z in A:
    dictA.setdefault(z, 0)
    dictA[z] += 1

sumA = sum(A)

BC = [list(map(int, input().split())) for i in range(Q)]
for x in BC:
    if x[0] in dictA.keys():
        sumA += (x[1] - x[0]) * dictA[x[0]]
        dictA.setdefault(x[1], 0)
        dictA[x[1]] += dictA[x[0]]
        dictA.pop(x[0])
    print(sumA)

Solution (Python):

N = int(input())
*A, = map(int, input().split())
cnt = [0]*(10**5+1)
s = 0
for i in range(N):
    cnt[A[i]] += 1
    s += A[i]

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

Solution (Python):

from collections import defaultdict
n=int(input())
l=list(map(int,input().split()))
d=defaultdict(int)
s=0
for i in l:
  d[i]+=1
  s+=i

q=int(input())
for _ in range(q):
  a,b=map(int,input().split())
  temp=a*d[a]
  if temp==0:
    print(s)
  else:
    k=b*d[a]
    s+=k-temp
    print(s)
    d[b]+=d[a]
    del d[a]

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


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

Solution (Python):
def main():
    n = int(input())
    a = list(map(int,input().split()))
    cnt = {}
    for i in range(n):
        if a[i] in cnt.keys():
            cnt[a[i]] += 1
        else:
            cnt[a[i]] = 1
    q = int(input())
    s = sum(a)
    for i in range(q):
        b,c = map(int,input().split())
        if b in cnt.keys():
            if c in cnt.keys():
                cnt[c] += cnt[b]
            else:
                cnt[c] = cnt[b]
            s += cnt[b]*(c-b)
            cnt[b] = 0
            print(s)
        else:
            print(s)

if __name__ == "__main__":
    main()

Solution (Python):

import sys
from collections import defaultdict


def solve(N: int, A: "List[int]", Q: int, B: "List[int]", C: "List[int]"):
    nums = defaultdict(int)
    for i in range(N):
        nums[A[i]] +=1
    s = 0
    for key in nums.keys():
        s += nums[key] * key
    for i in range(Q):
        count = nums[B[i]]
        nums[C[i]]+=nums[B[i]]
        nums[B[i]] = 0
        s = s - count*B[i] + count*C[i]
        print(s)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    Q = int(next(tokens))  
    B = [int()] * (Q)  
    C = [int()] * (Q)  
    for i in range(Q):
        B[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, A, Q, B, C)

if __name__ == '__main__':
    main()

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)]
tmp_counter = Counter(a)
tmp_sum = sum(a)
for b, c in bc:
    if b in tmp_counter.keys():
        tmp_sum += (c - b) * tmp_counter[b]
        tmp_counter[c] += tmp_counter[b]
        tmp_counter[b] = 0
    print(tmp_sum)

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





counterA = dict(Counter(A))
counterAkeys=set(counterA.keys())


sumA = sum(A)


for _ in range(Q):
  b,c=map(int,input().split())
  
  if b not in counterAkeys:
    print(sumA)
    continue
  b_cnt = counterA[b]
  counterA[b]=0
  if c in counterAkeys:
    counterA[c] += b_cnt
  else:
    counterA[c] = b_cnt
    counterAkeys.add(c)

  sumA+=(c-b)*b_cnt
  print(sumA)

Solution (Python):

import sys 
input=sys.stdin.readline 
def getsum(BITTree,i): 
	s = 0 
	i = i+1
	while i > 0: 
		s += BITTree[i] 
		i -= i & (-i) 
	return s 
def updatebit(BITTree , n , i ,v): 
	i += 1
	while i <= n: 
		BITTree[i] += v 
		i += i & (-i) 
def construct(arr, n): 
	BITTree = [0]*(n+1) 
	for i in range(n): 
		updatebit(BITTree, n, i, arr[i]) 
	return BITTree 
n=int(input())
arr=[int(i) for i in input().split()]
from collections import Counter 
c=Counter(arr)
m=int(input())
sm=sum(arr)
for i in range(m): 
    a,b=map(int,input().split())
    if c[a]:
        sm-=c[a]*a 
        sm+=c[a]*b 
        c[b]+=c[a]
        c[a]=0 
    print(sm)

Solution (Python):

"""
Created on Sun Jun 21 21:34:50 2020

@author: NEC-PCuser
"""
import collections

N = int(input())

A = list(map(int, input().split()))
c = collections.Counter(A)

list_sum = sum(A)

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

for i in sum_list:
    print(i)

Solution (Python):
import sys
import numpy as np

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    
    
    
    summ = sum([k*v for k, v in dic.items()])
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        diff = c*dic.get(b,0) - b*dic.get(b,0)
        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0
        summ += diff
        print(summ)

Solution (Python):


def main():
    N = int(input())
    a = list(map(int, input().split()))
    Q = int(input())
    maxa = max(a)
    x = [0] * ((10**5)+1)
    ans = 0
    for i in range(0, N):
        e = a[i]
        x[e] += 1
        ans += e
    for j in range(Q):
        b, c = list(map(int, input().split()))
        ans += (c - b) * x[b]
        x[c] += x[b]
        x[b] = 0
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
s = sum(A)
d = {}
for i in range(N):
    if A[i] in d.keys():
        d[A[i]] += 1
    else:
        d[A[i]] = 1
Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    if b not in d.keys() or d[b] == 0:
        pass
    else:
        tmp = d[b]
        s -= d[b] * b
        s += d[b] * c
        d[b] = 0
        if c in d.keys():
            d[c] += tmp
        else:
            d[c] = tmp
    print(s)

Solution (Python):
from collections import Counter

n=int(input())
x = list(map(int,input().split()))
c = Counter(x)
y=[0]*(10**5+1)
for i in c.keys():
	y[i]=c[i]
ad=0
su=sum(x)
q=int(input())
for i in range(q):
	b , c = map(int, input().split())
	su=su-y[b]*b+y[b]*c
	y[c]+=y[b]
	y[b]=0
	print(su)

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

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
change = []
for i in range(q):
  change.append(list(map(int, input().split())))
dict = Counter(a)
ans = sum(a)
ans_list = []
for b, c in change:
  if b in dict.keys():
    if c in dict.keys():
      dict[c] += dict[b]
      ans = ans + (c-b)*dict[b]
      dict[b] = 0
      ans_list.append(ans)
    else:
      dict[c] = dict[b]
      ans = ans + (c-b)*dict[b]
      dict[b] = 0
      ans_list.append(ans)
  else:
    ans_list.append(ans)
for i in ans_list:
  print(i)

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

import collections as cl

def main() :
    N = int(input())
    A_list = [int(i) for i in input().split()]
    current_sum = sum(A_list)

    
    c = cl.Counter(A_list)
    
    
    
    
    Q = int(input())
    for i in range(Q) :
        B,C = map(int,input().split())
        diff = C-B
        num = c[B]
        current_sum += diff * num
        c[B] = 0
        c[C] += num
        print(current_sum)

main()

Solution (Python):
import sys
from collections import Counter


def read():
    return sys.stdin.readline().rstrip()


def main():
    n = int(read())
    a = Counter(map(int, read().split()))
    s = sum(k * v for k, v in a.items())
    q = int(read())
    for _ in range(q):
        b, c = map(int, read().split())
        k = a[b]
        a[b] = 0
        a[c] += k
        s += (c - b) * k
        print(s)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    n = int(readline())
    a = list(map(int, readline().split()))
    q = int(readline())
    counter = Counter(a)
    res = sum(a)

    for i in range(q):
        b, c = map(int, readline().split())
        res -= counter[b] * b
        res += counter[b] * c
        counter[c] += counter[b]
        counter[b] = 0
        print(res)

if __name__ == '__main__':
    main()

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

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