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 math  
from typing import Dict, List, Optional, Tuple, Union  


def count(A: List[int]) -> Dict[int, int]:
    D: Dict[int, int] = {}
    for a in A:
        if a not in D:
            D[a] = 1
        else:
            D[a] += 1
    return D


def main(N: int, A: List[int], Q: int, B: List[int], C: List[int]):
    D = count(A)
    sum_ = sum(A)
    for b, c in zip(B, C):
        sum_changing = D.get(b, 0) * (c - b)
        if c not in D:
            D[c] = 0
        D[c] += D.get(b, 0)
        D[b] = 0
        sum_ += sum_changing
        print(sum_)


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B: List[int] = []
    C: List[int] = []
    for _ in range(Q):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
    main(N, A, Q, B, C)

Solution (Python):




import collections

def main():
    n = int(input())
    s = input_list()
    sc = collections.Counter(s)
    q = int(input())
    data = []
    for i in range(q):
        b, c = input_list()
        sc[c] += sc[b]
        if i > 0:
            data.append(c*sc[b] - b * sc[b])
        if b in sc:
            sc.pop(b)
    ans = 0
    for k, v in sc.items():
        ans += k*v
    aa = [ans]
    for i, v in enumerate(data[::-1]):
        aa.append(aa[i] - v)
    for a in aa[::-1]:
        print(a)


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

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

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    alis = sorted(map(int, input().split()))
    total = sum(alis)
    adic = {}
    for num in alis:
        if num in adic:
            adic[num] += 1
        else:
            adic[num] = 1
    
    anslis = []

    q = int(input())
    bclis = []
    for i in range(q):
        b, c = map(int,input().split())
        if b in adic:
            kazu = adic[b]
            adic[b] = 0
            total -= b * kazu
            if c in adic:
                adic[c] += kazu
            else:
                adic[c] = kazu
            total += c * kazu
        print(total)
    
    
if __name__ == "__main__":
    main()

Solution (Python):
import collections
N = int(input())
nums = list(map(int, input().split()))
Q = int(input())
nums_hash = collections.Counter(nums)
sum_point = sum(nums)
for _ in range(Q):
    
    b, c = map(int, input().split())
    if b in nums_hash:
        sum_point = (sum_point - (b * nums_hash[b]) + (c * nums_hash[b]))
    print(sum_point)
    nums_hash[c] += nums_hash[b]
    nums_hash[b] = 0

Solution (Python):
n = int(input())
line_a = list(map(int,input().split()))
total = sum(line_a)
q = int(input())
numbers = [0 for i in range(10**5+1)]
for i in line_a:
  numbers[i] += 1

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

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

Solution (Python):
n = int(input())
temparr = input()
temparr = temparr.split()
dicts = {}
total = 0 
for i in temparr:
    i = int(i)
    if i not in dicts:
        dicts[i] = 1 
    else:
        dicts[i] += 1 
    total += i 
times = int(input())
for i in range(times):
    temparr = input()
    temparr = temparr.split()
    find = int(temparr[0])
    replace = int(temparr[1])
    if find not in dicts:
        print(total)
        continue 
    if find in dicts:
        howmany = dicts[find]
        total -= howmany * find 
        total += howmany * replace 
        if replace not in dicts:
            dicts[replace] = howmany 
        else:
             dicts[replace] += howmany 
        del dicts[find]
        
        print(total)

Solution (Python):
def resolve():
    from collections import defaultdict
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    S = sum(A)
    d = defaultdict(int)
    for a in A:
        d[a] += 1

    for _ in range(q):
        b, c = map(int, input().split())
        k = d[b]
        d[b] -= k
        d[c] += k
        S += k * (c - b)
        print(S)

resolve()

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
q = int(input())
l = [list(map(int, input().split())) for _ in range(q)]
dic = Counter(a)
S = sum(a)
for i in range(q):
    (b, c) = l[i] 
    num = dic[b]
    S -= b*num
    dic[b] = 0
    if dic[c] == 0:
        dic[c] = num
    else:
        dic[c] += num
    S += c*num
    print(S)

Solution (Python):
from collections import defaultdict
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
arr = defaultdict(int)
for k, v in Counter(a).items():
    arr[k] = v
q = int(input())
bc = []
for _ in range(q):
    b, c = map(int, input().split())
    bc.append((b, c))

whole = sum([x * y for x, y in arr.items()])

for b, c in bc:
    arr[c] += arr[b]
    tmp = arr[b]
    arr[b] = 0
    whole += (c - b) * tmp
    print(whole)

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

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

a_sum = sum(a)

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

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

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

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

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

Solution (Python):

import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial
from bisect import bisect_left 
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

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




d = defaultdict(int)
sumv = sum(A)
for a in A:
    d[a] += 1
for bc in BC:
    b, c = bc
    sumv -= b * d[b]
    sumv += c * d[b]
    d[c] += d[b]
    d[b] = 0
    print(sumv)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
numl = [0] * 100005
resl = []
suma = sum(a)
for i in range(n):
    numl[a[i]]+=1
for i in range(q):
    b, c = map(int, input().split())
    bn = numl[b]
    suma += (c - b)* bn
    resl.append(suma)
    numl[b] -= bn
    numl[c] += bn
for i in resl:
    print(i)

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

A_dic = dict()
for i in range(N):
    if A[i] in A_dic:
        A_dic[A[i]] += 1
    else:
        A_dic[A[i]] = 1

ans = sum(A)
for i in range(Q):
    B, C = map(int, input().split())
    
    if B in A_dic:
        if C in A_dic:
            A_dic[C] += A_dic[B]
        else:
            A_dic[C] = A_dic[B]
        if B > C:
            ans += (C-B) * A_dic[B]
        else:
            ans -= (B-C) * A_dic[B]
        A_dic.pop(B)
    else:
        pass
    
    print(ans)

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

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

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

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

Solution (Python):
import collections

def resolve():
    N = int(input())
    A_list = list(map(int, input().split()))
    sum_A_list = sum(A_list)
    
    A_counter_dict = collections.Counter(A_list)
    Q = int(input())
    for i in range(Q):
        B, C = list(map(int, input().split()))
        if B in A_counter_dict.keys():
            changing_count = A_counter_dict.pop(B)
            sum_A_list += (C - B) * changing_count
            if C in A_counter_dict.keys():
                A_counter_dict[C] += changing_count
            else:
                A_counter_dict[C] = changing_count
        print(sum_A_list)

resolve()

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
Ct = Counter(A)
ans = 0
for item in Ct.items():
    ans += item[0]*item[1]
for _ in range(Q):
    B, C = list(map(int, input().split()))
    B_n = Ct[B]
    ans += (C-B)*B_n
    print(ans)
    Ct[C] += Ct[B]
    Ct[B] = 0

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

if __name__ == '__main__':
    main()

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

dict = Counter(A)
for i in range(Q):
    l = list(map(int, input().split()))
    d = l[1] - l[0]
    cnt = dict[l[0]]
    dict[l[1]] += dict[l[0]]
    dict.pop(l[0], None)
    S += d*cnt
    print(S)

Solution (Python):
mod=pow(10,5)+2
N=int(input())
num=[0]*mod
ans=0
A=list(map(int, input().split()))
for i in range(N):
    num[A[i]]+=1
    ans+=A[i]
Q=int(input())
for i in range(Q):
    B,C=map(int, input().split())
    cnt=num[B]
    num[C]+=num[B]
    num[B]=0
    ans+=(C-B)*cnt
    print(ans)

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

Solution (Python):
from collections import Counter

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

sum = sum(a)

a = Counter(a)
a_keys = list(a.keys())
a_values = list(a.values())

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
l = [0] * (10**5+1)
for i in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

for i in range(len(A)):
    l[A[i]] += 1
ans = 0
for i in range(1, len(l)):
    ans += l[i]*i
for i in range(Q):
    tmp = l[B[i]]
    tmp2 = tmp * B[i]
    l[C[i]] += tmp
    tmp3 = tmp * C[i]
    l[B[i]] = 0
    ans += tmp3 - tmp2
    print(ans)

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

cnt = collections.Counter(a)

sum = sum(a)
sl = []
for i in range(q):
    b, c = map(int, input().split())
    bc = cnt[b]
    sum = sum - b*bc + c*bc
    sl.append(sum)

    if cnt[b] > 0:
        cnt[c] += cnt[b]
        cnt[b] = 0

for j in range(len(sl)):
    print(sl[j])

Solution (Python):


import collections

n = int(input())
a = list(map(int, input().split(" ")))
q = int(input())
have_map = collections.Counter(a)

total_cost = 0
for k,v in have_map.items():
    total_cost += k * v

for i in range(q):
    x, y = map(int, input().split(" "))
    if x in have_map:
        diff = have_map[x] * -x  + have_map[x] * y
        total_cost += diff

    have_map[y] = have_map.get(x, 0) + have_map.get(y, 0)
    have_map.pop(x, 0)

    print(total_cost)

Solution (Python):
N = input()
arr = [int(x) for x in input().split()]

q = int(input())
ans = sum(arr)

hashmap = {}
for val in arr:
    if val in hashmap:
        hashmap[val] += 1
    else:
        hashmap[val] = 1


while q > 0:

    [x, y] =  [int(x) for x in input().split()]
    if x in hashmap:
        if y in hashmap:
            hashmap[y] += hashmap[x]
            ans -= hashmap[x]*x
            ans += y*hashmap[x]
            del hashmap[x]
        else:
            diff = hashmap[x] * x
            ans -= diff
            hashmap[y] = hashmap[x]
            del hashmap[x]
            add = hashmap[y] * y
            ans += add
        
    print(ans)
    q -=1

Solution (Python):
import numpy as np
N = int(input())

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

zero = np.zeros(10**5+1)
total = np.sum(A)

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

for j in range(Q):
    total += (BC[j][1]-BC[j][0])*zero[BC[j][0]]
    zero[BC[j][1]] += zero[BC[j][0]]
    zero[BC[j][0]] = 0
    print(int(total))

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

Q = int(input())

total = sum(A)
counter = dict()

for i in range(100001):
    counter[i] = 0

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

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

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
d = Counter(a)
sumd = sum([k*v for k, v in d.items()])

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

for b, c in t:
   sum_minus = b * d[b]
   tmp = d.pop(b, None)
   if tmp:
      if d[c] > 0:
         d[c] += tmp
      else:
         d[c] = tmp
      sumd = sumd - sum_minus + c * tmp
   print(sumd)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num_dic = {}
for i in A:
    if i not in num_dic:
        num_dic[i] = 1
    else:
        num_dic[i] += 1
ans = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    if not b in num_dic:
        print(ans)
        continue
    else:
        if c in num_dic:
            ans -= num_dic[b] * b
            ans += num_dic[b] * c
            num_dic[c] += num_dic[b]
            num_dic[b] = 0
        else:
            ans -= num_dic[b] * b
            ans += num_dic[b] * c
            num_dic[c] = num_dic[b]
            num_dic[b] = 0
        print(ans)

Solution (Python):
n = int(input())
ai = [int(i) for i in input().split()]
q = int(input())
bc = [[int(i) for i in input().split()] for j in range(q)]

kosu = [0]*(10**5+1)
for i in range(n):
    kosu[ai[i]] += 1
    
num = [0]*(10**5+1)
tmp = sum(ai)

for i in range(q):
    tmp += kosu[bc[i][0]] * (bc[i][1]-bc[i][0])
    kosu[bc[i][1]] += kosu[bc[i][0]]
    kosu[bc[i][0]] = 0
    print(tmp)

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

T = [0]*200000


for i in A:
  T[i]+=1
S = sum(A)
for i in BC:
  b,c = i
  n = int(T[b])
  sa = c*n - b*n
  S = S + c*n - b*n
  print(S)
  T[b]=0
  T[c]+=n

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))

from collections import Counter
def resolve():
    N,=pin()
    An=lispin()
    count_An=dict(Counter(An))
    S=sum(An)
    Q,=pin()
    erachan=0
    for i in range(Q):
        a,b=pin()
        temp=0
        try:
            temp=count_An[a]
            count_An[a]=0
            count_An.setdefault(b,0)
            count_An[b]+=temp
        except KeyError:erachan+=1
        S=S+(b-a)*temp
        print(S)
        

resolve()

Solution (Python):
from collections import Counter 

n = int(input())
a = list(map(int, input().split()))
co = Counter(a)
q = int(input())
ans = 0
for i in co.items():
    ans += i[0] * i[1]
for _ in range(q):
    b, c = map(int, input().split())
    if b in co.keys():
        ans -= b * co[b]
        co[c] += co[b]
        ans += c * co[b]
        co[b] = 0
    print(ans)

Solution (Python):
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
INF = 1000000000000

import bisect

n = I()
a = LI()
q = I()

cnt = [0]*1000000
for i in a:
    cnt[i] += 1
sm = sum(a)

for i in range(q):
    b,c = LI()
    sm += (c-b)*cnt[b]
    if(c != b):
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(sm)

Solution (Python):


N = int(input())

A = [int(y) for y in input().split()]

lif = [0] * (10 ** 5 + 1)
sasa = 0

for y in A:
    lif[y] += 1
    sasa += y

Q = int(input())

k = 0

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

    k = lif[b]

    lif[c] += k
    lif[b] = 0
    sasa += (k * (c - b))

    print("{}".format(sasa))

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

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

data = {}

total = 0
for i in range(N):
  total += A[i]
  if A[i] in data:
    data[A[i]] += 1
  else:
    data[A[i]] = 1

Q = int(input())
for i in range(Q):
  B, Q = map(int, input().split())
  if B in data:
    num = data[B]
    data[B] = 0
    if Q in data:
      data[Q] += num
    else:
      data[Q] = num
    total = total - B * num + Q * num
  print(total)

Solution (Python):
import math

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N = ii()
    A = list(mi())
    Q = ii()
    BC = [map(int, input().split()) for _ in range(Q)]
    B, C = [list(i) for i in zip(*BC)]
    ans = sum(A)

    dic = {i+1: 0 for i in range(10**5)}
    for v in A:
        dic[v] += 1
    for i in range(Q):
        k = dic[B[i]]
        ans += k*(C[i]-B[i])
        dic[B[i]] -= k
        dic[C[i]] += k

        print(ans)
    


if __name__ == '__main__':
    main()

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

res = sum(A)
c = cl.Counter(A)
for i in range(Q):
  BC = [int(j) for j in input().split()]
  B = BC[0]
  C = BC[1]
  if B in c:
    res += (C-B)*c[B]
    if C in c:
      c[C] += c[B]
    else:
      c[C] = c[B]
    del(c[B])
  print(res)

Solution (Python):
N = int(input())
A = input().split()
Q = int(input())
BC = []
for _ in range(Q):
    b,c = input().split()
    BC.append([b,c])
dic = {}
for i in range(N):
    if A[i] in dic:
        dic[A[i]] += 1
    else: dic[A[i]] = 1
S = sum([int(a) for a in A])
for i in range(Q):
    b,c = BC[i]
    if b in dic:
        num = dic[b]
        S += num*(int(c)-int(b))
        print(S)
        if c in dic:
            dic[c] += num
            dic.pop(b)
        else:
            dic[c] = num
            dic.pop(b)
    else: print(S)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
num = {i:0 for i in range(10**5+1)}
for a in A:
    num[a] += 1
ans = sum(A)
for _ in range(Q):
    B, C = map(int, input().split())
    num_B = num[B]
    num_C = num[C]
    num[C] += num_B
    num[B] = 0
    
    
    ans += -num_B*B + (num[C]-num_C)*C
    print(ans)

Solution (Python):
import numpy as np
def solve():
    N = int(input())
    A = np.array(list(map(int, input().split())),dtype=np.int64)
    Q = int(input())
    BC = np.array([list(map(int, input().split())) for _ in range(Q)])

    nums = np.zeros(max(A.max(), BC.max())+1)
    sm = np.sum(A, dtype=np.int64)
    for i in A:
        nums[i]+=1

    for b,c in BC:
        sm += nums[b]*(c-b)
        nums[c] +=nums[b]
        nums[b] = 0
        print(int(sm))








if __name__ == "__main__":
    solve()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
import collections
coll=collections.Counter(a)
q=int(input())
suma=sum(a)
lis=[0]*(10**5+1)
for i in a:
    lis[i-1]=coll[i] 

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

Solution (Python):




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

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

    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))

    sm=0
    for a in arr:
        db[a]+=1
        sm+=a

    q=int(readline())

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

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
l = [0]*100010
ans = sum(a)

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

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

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

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

ls = [0] * 10 ** 6

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

s = sum(A)

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

Solution (Python):
from collections import Counter

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

chlist = []
for i in range(Q):
    chlist.append(tuple(map(int, input().split())))

counter = Counter(A)

sums = 0
for key, value in counter.items():
    sums += key * value

for t in chlist:
    if t[0] in counter.keys():
        t0s = counter[t[0]]

        if t[1] in counter.keys():
            counter[t[1]] += counter[t[0]]
        else:
            counter[t[1]] = counter[t[0]]

        counter.pop(t[0])
        sums += (t[1] - t[0]) * t0s

    print(sums)

Solution (Python):
from collections import Counter
def main():
    import sys
    def input():
        return sys.stdin.readline()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    C = Counter(A)
    s = set(C.keys())
    ans = sum(A)
    for _ in range(q):
        b, c = list(map(int, input().split()))
        if b in s:
            d = C.pop(b)
            ans = ans + (c - b) * d
            C[c] += d
            s.remove(b)
            s.add(c)
        print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
memo = {}
ans = 0

for a in A:
  ans += a
  if a in memo:
    memo[a] = memo[a] + 1
  else:
    memo[a] = 1
    
Q = int(input())

for _ in range(Q):
  B, C = map(int, input().split())
  
  if B in memo and memo[B] != 0:
    n = memo[B]
    memo[B] = 0
    ans -= B * n
    if C in memo:
      memo[C] += n
    else:
      memo[C] = n
    ans += C * n
    
    print(ans)
  else:
    print(ans)

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

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

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

Solution (Python):
import numpy as np

N = int(input())
A = []
A = np.array(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)

m = max(max(A), max(B), max(C))+1
res = np.arange(m,dtype=int)
newA = np.zeros((m),dtype=int)


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

sum_r = np.dot(res,newA)


for i in range(Q):
    b = B[i]
    c = C[i]
    sum_r = sum_r - b*newA[b] + c*newA[b]
    print(sum_r)
    newA[c] += newA[b]
    newA[b] = 0

Solution (Python):
n = int(input())
nums = [0] * 100001
A = [int(i) for i in input().split()]
for a in A:
    nums[a] += 1
q = int(input())
B = [None] * q
C = [None] * q
for i in range(q):
    B[i], C[i] = (int(i) for i in input().split())

ans = 0
for i in range(100001):
    ans += i * nums[i]
      
for i in range(q):
    if nums[B[i]] == 0:
        print(ans)
        continue
    t = nums[B[i]]
    ans += (C[i]-B[i])*t
    nums[B[i]] = 0
    nums[C[i]] += t
    print(ans)

Solution (Python):
N = int(input().rstrip())
As = {}
answer = 0
for a in [int(x) for x in input().rstrip().split()]:
    answer += a
    if a in As.keys():
        As[a] += 1
    else:
        As[a] = 1

Q = int(input().rstrip())
d = {}
for i in range(Q):
    b, c = map(int, input().rstrip().split())
    d[i] = {'b': b, 'c': c}

for i in sorted(d.keys()):
    if d[i]['b'] in As.keys():
        diff = d[i]['c'] - d[i]['b']
        diff_num = As.pop(d[i]['b'])
        if d[i]['c'] in As.keys():
            As[d[i]['c']] += diff_num
        else:
            As[d[i]['c']] = diff_num
        answer += (diff * diff_num)
    print(answer)

Solution (Python):
n = int(input())
arr = [int(x) for x in input().split()]

count = {}
for x in arr:
    if x in count:
        count[x] += 1
    else:
        count[x] = 1

s = sum(arr)

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

    if b in count and count[b] > 0:
        s -= b*count[b]
        s += c*count[b]

        if c in count:
            count[c] += count[b]
        else:
            count[c] = count[b]

        count[b] = 0

    print(s)

Solution (Python):
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(n):
    dic[a[i]]+=1
ans = 0
for j in dic:
    ans += dic[j]*j

Q= int(input())
for q in range(Q):
    b, c = map(int, input().split())
    s= dic[b]*(c-b)
    ans += s
    
    dic[c]+=dic[b]
    dic[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())))

s = sum(a)
count = [0] * (10**5+1)
for i in range(n):
    count[a[i]] += 1

for i in range(q):
    deg = bc[i][1] - bc[i][0]
    s += deg*count[bc[i][0]]
    count[bc[i][1]] += count[bc[i][0]]
    count[bc[i][0]] = 0
    print(s)

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

Solution (Python):
N=int(input())
a=list(map(str,input().split()))
s=0
box=[]
dp={}
for x in a:
    s+=int(x)
    if x not in dp.keys():
        dp[x]=1
    else:
        dp[x]=dp[x]+1
q=int(input())
for i in range(q):
    b,c=map(str,input().split())
    if b not in dp.keys():
        box.append(s)
        continue
    s+=int(dp[b])*(int(c)-int(b))
    if c not in dp.keys():
        dp[c]=dp[b]
    else:
        dp[c]+=dp[b]
    del dp[b]
    box.append(s)
for x in box:
    print(x)

Solution (Python):
import numpy

tmp=input()
list=input().split(" ")
list=[int(x) for x in list]
array=numpy.array(list,dtype=">i4")
sum=numpy.sum(array)
all=numpy.zeros(100001,dtype=">i4")
for i in array:
  all[i]+=1



a=int(input())

for i in range(a):
  b=input().split(" ")
  b0=int(b[0])
  b1=int(b[1])
  diff=all[b0]*(b1-b0)
  sum+=diff
  all[b1]+=all[b0]
  all[b0]=0
  print(sum)

Solution (Python):
data = {}
total = 0
N = int(input())

for a in input().split():
    a = int(a)
    data[a] = data.get(a, 0) + 1
    total += a

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    diff = c - b
    count = data.get(b)
    if count:
        data[c] = data.get(c, 0) + data.pop(b)
        total += diff * count
    print(total)

Solution (Python):
from collections import defaultdict

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

dic = defaultdict(int)

ans = 0
for a in A:
    ans += a
    dic[a] += 1

for _ in range(Q):
    b,c = [int(v) for v in input().split()]
    if b in dic:
        value = dic.pop(b)
        ans += c * value
        ans -= value * b
        dic[c] = dic[c] + value
    print(ans)