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 numpy as np
from collections import defaultdict

s = list(map(int, input().split()))
n=s[0]
a = np.array(list(map(int, input().split())))
s = list(map(int, input().split()))
q=s[0]
num=defaultdict(int)
ans=0

for i in a:
    num[i]+=1
    ans+=i

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

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = list()
C = list()
for i in range(Q):
  b, c = map(int, input().split())
  B.append(b)
  C.append(c)

A_sum = sum(A)
num_count = [0] * (10 ** 5)

for a in A:
  num_count[a-1] += 1
for i in range(Q):
  if num_count[B[i]-1] != 0:
    A_sum += (C[i] - B[i]) * num_count[B[i]-1]
    print(A_sum)
    num_count[C[i]-1] += num_count[B[i]-1]
    num_count[B[i]-1] = 0
  else:
    print(A_sum)

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=nl()
nums = [0]*10**6
for i in range(n):
  nums[a[i]]+=1
q=ni()
ans=0
for i in range(len(nums)):
  ans+=nums[i]*i

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

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

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

    S = sum(A)
    cnt = defaultdict(int)
    for a in A:
        cnt[a] += 1
    for _ in range(int(input())):
        b, c = map(int, input().split())
        nb = cnt[b]
        cnt[c] += nb
        cnt[b] = 0
        S += (c - b) * nb
        print(S)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
from collections import defaultdict
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n = I()
mylist = LI()
q = I()
d = defaultdict(int)

for i in mylist:
    d[i] += 1
result = sum(mylist)

for i in range(q):
    pastN, nextN= MI()
    num = d[pastN]
    result += num * (nextN-pastN)
    print(result)
    d[pastN] = 0
    d[nextN] += num

Solution (Python):
import collections
N = int(input())
a = [int(x) for x in input().split()]
aa = collections.Counter(a)
A = list(aa.items())
Alisted = [0]*(10**5+1)
for i in range(len(A)):
  Alisted[A[i][0]] = A[i][1]
Q = int(input())
BC = [[int(x) for x in input().split()]for i in range(Q)]

Sum = sum(a)
for i in range(Q):
  b = BC[i][0]
  c = BC[i][1]
  num = Alisted[b]
  Sum += (c-b)*num
  print(Sum)
  Alisted[b]=0
  Alisted[c]+=num

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
from collections import defaultdict
s = defaultdict(int)
f = {}
ans = 0
for i in a:
  s[i] += 1
  f[i] = None
  ans += i

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
TMP=sum(A)
Q=int(input())
import collections
List=collections.Counter(A)
for i in range(Q):
  S,T=map(int,input().split())  
  TMP=TMP+(T-S)*List[S]
  print(TMP)  
  List[T]+=List[S]
  List[S]=0

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

array = dict()
for i in data:
  if i in array:
    array[i] += 1
  else:
    array[i] = 1
    
total = 0
for i, j in array.items():
  total += i*j
  
for i in range(Q):
  dt = 0
  quary = [int(i) for i in input().split()]
  if quary[0] not in array:
    pass
  elif quary[1] in array:
    dt = (quary[1]-quary[0])*array[quary[0]]
    array[quary[1]] += array[quary[0]]
    del array[quary[0]]
  else:
    dt = (quary[1]-quary[0])*array[quary[0]]
    array[quary[1]] = array[quary[0]]
    del array[quary[0]]
  
  total += dt
  print(total)

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

total = sum(nums)

lst = [0] * 100001

for num in nums:
  lst[num] += 1

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
arr = [0] * 100005
for val in a:
  arr[val] += 1
sums = sum(a)
q = int(input())
for _ in range(q):
  b, c = map(int,input().split())
  diff = (c-b) * (arr[b])
  sums += diff
  print(sums) 
  arr[c] += arr[b]
  arr[b] = 0

Solution (Python):
import collections
n=int(input())
a=list(map(int,input().split()))
q=int(input())
numl=collections.Counter(a)
s=sum(a)
for qq in range(q):
  x,y=map(int,input().split())
  s+=numl[x]*(y-x)
  print(s)
  numl[y]+=numl[x]
  numl[x]=0

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

d=dict()
sum=0
for i in A:
  if not i in d:
    d[i]=0
  d[i]+=1
  sum+=i

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

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

Solution (Python):
import sys



def solve(vals, queries):
    nv = len(vals)
    nq = len(queries)
    
    freqs = {}
    tot0 = 0
    for v in vals:
        if not v in freqs:
            freqs[v] = 1
        else:
            freqs[v] += 1
        tot0 += v

    for b,c in queries:
        if b in freqs:
            tot0 += freqs[b]*(c-b)
            if not c in freqs:
                freqs[c] = 0
            freqs[c] += freqs[b]
            del freqs[b]
        print(tot0)
        

        


if __name__ == '__main__':
    IN = [x.strip() for x in sys.stdin.readlines()]

    N = int(IN[0])
    vals = [int(x) for x in IN[1].split(' ')]
    Q = int(IN[2])
    queries = []
    for i in range(Q):
        b,c = [int(x) for x in IN[3+i].split(' ')]
        queries.append((b,c))
    solve(vals, queries)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

def main():
    n = int(input())
    a = list(map(int,input().split()))
    q = int(input())
    cnt = [0]*(10**5+7)
    for i in range(n):
        cnt[a[i]]+=1
    p = sum(a)
    for i in range(q):
        b,c = map(int,input().split())
        d = cnt[b]
        e = cnt[c]
        diff = d*(c-b)
        p += diff
        cnt[c]+=d
        cnt[b]-=d
        print(p)
if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict

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

dd = defaultdict(int)
s = 0
for i in a:
    s += i
    dd[i] = dd[i] + 1

q = int(input())
for i in range(q):
    bi, ci = map(int, input().split())
    if bi in dd:
        dd[ci] = dd[ci] + dd[bi]
        s = s - bi * dd[bi] + ci * dd[bi]
    dd[bi] = 0
    print(s)

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

d = {}
sum = sum(a)

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

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

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

s = sum(a)

num = [0]*(10**5 + 1)
for x in a:
    num[x] += 1

for b, c in bc:
    s += (c - b)*num[b]
    num[c] += num[b]
    num[b] = 0

    print(s)

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

sum_ = sum(A)
element_count_dict = {}
for elm in A:
    element_count_dict[elm] = element_count_dict.get(elm, 0) + 1
for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in element_count_dict:
        count_B = element_count_dict[B]
        element_count_dict[C] = element_count_dict.get(C, 0) + count_B
        sum_ = sum_ - B * count_B + C * count_B
        element_count_dict[B] = 0
    print(sum_)

Solution (Python):


import collections

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A_count_list = collections.Counter(A)
A_sum = sum(A)
B = [0] * Q
C = [0] * Q
for _ in range(Q):
    B, C = map(int, input().split())
    A_sum += (C - B)*A_count_list[B]
    A_count_list[C] += A_count_list[B]
    A_count_list[B] = 0
    print(A_sum)

Solution (Python):
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    max_num = 10**5
    n = int(input())
    a_list = list(map(int, input().split()))
    q = int(input())
    
    query = [map(int, input().split()) for _ in range(q)]
    num = [0 for _ in range(max_num+1)]
    for i in a_list:
        num[i] += 1

    ans = sum(a_list)

    for b,c in query:
        n = num[b]
        num[b] = 0
        num[c] += n
        ans += (c-b)*n
        print(ans)

    
if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
import collections

N = int(input())
A_list = list(map(int, input().split()))
A_count_list = collections.Counter(A_list)
A_sum = sum(A_list)
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    A_sum += (C - B)*A_count_list[B]
    A_count_list[C] += A_count_list[B]
    A_count_list[B] = 0
    print(A_sum)

Solution (Python):
import numpy as np
from numba import njit

n = int(input())
A = list(map(int,input().split()))
q = int(input())
BC = [list(map(int,input().split())) for _ in range(q)]
A = np.array(A)

ans = sum(A)


a_num = [0]*10**5
for  a in A:
    a_num[a-1]+=1

for bc in BC:
    change = bc[1]-bc[0]
    ans += change*a_num[bc[0]-1]
    a_num[bc[1]-1] += a_num[bc[0]-1]
    a_num[bc[0]-1] = 0
    print(ans)

Solution (Python):
import os
import sys
from io import BytesIO, IOBase
from math import gcd
from collections import Counter as cc

def main():
    n=int(input())
    l=list(map(int,input().split()))
    sm=sum(l)
    cnt=cc(l)
    q=int(input())
    for _ in range(q):
        p,r=map(int,input().split())
        sm-=(p*cnt[p])
        cnt[r]+=cnt[p]
        sm+=(r*cnt[p])
        cnt[p]=0
        print(sm)



    




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce



n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
c_n = np.array([(k, v) for k, v in c.items()])
total = np.dot(c_n.T[0], c_n.T[1])
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    num = c[cf]
    if ca in c:
        c[ca] += c[cf]
        c[cf] = 0


    else:

        c[ca] = c[cf]
        c[cf] = 0


    total += num * (ca - cf)
    print(total)

Solution (Python):
import collections

def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    cnt = collections.defaultdict(int)
    for var in a:
        cnt[var] += 1
    
    sum_a = sum(a)
    
    q = int(input())
    for _ in range(q):
        b, c = list(map(int, input().split()))
        diff = (c-b) * cnt[b]
        sum_a += diff
        print(sum_a)
        cnt[c] += cnt[b]
        cnt[b] = 0
        
if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
d = {}
s = 0
for i in a:
    s += i
    if i not in d:
        d[i] = 1
    else:
        d[i] += 1
for _ in range(int(stdin.readline())):
    b, c = list(map(int, stdin.readline().split()))
    if b in d:
        s += d[b]*(c-b)
        if c not in d:
            d[c] = d[b]
        else:
            d[c] += d[b]
    d[b] = 0
    print(s)

Solution (Python):
dic={}
n=int(input())
su=0
for i in input().split():
    i=int(i)
    if i in dic.keys():
        dic[i]+=1
    else:
        dic[i]=1
    su+=i
q=int(input())
for cas in range(q):
    b,c=map(int,input().split())
    if b in dic.keys():
        su+=(c-b)*dic[b]
        if c in dic.keys():
            dic[c]+=dic[b]
        else:
            dic[c]=dic[b]
        dic[b]=0
    print(su)

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






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

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


for b, c in zip(m, m):
    bcnt=lst[b]
    ans+=(c-b)*bcnt
    lst[c]+=bcnt
    lst[b]=0
    print(ans)

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

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

Solution (Python):
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    max_num = 10**5
    n = int(input())
    a_list = list(map(int, input().split()))
    q = int(input())
    
    query = [map(int, input().split()) for _ in range(q)]
    num = [0 for _ in range(max_num+1)]
    for i in a_list:
        num[i] += 1

    ans = sum(a_list)

    for b,c in query:
        n = num[b]
        num[b] = 0
        num[c] += n
        ans += (c-b)*n
        print(ans)

    
if __name__ == "__main__":
    main()

Solution (Python):
import collections

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

num = collections.Counter(A)

S = sum(A)
slist = []

for i in range(Q):
	B, C = map(int, input().split())
	numb = num[B]
	S += (C - B) * numb
	num[C] += numb
	num[B] = 0
	slist.append(S)

[print(i) for i in slist]

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

Solution (Python):
MAXA = 10 ** 5

n = int(input())
lista = [int(i) for i in input().split()]
q = int(input())
query = [map(int, input().split()) for _ in range(q)]

num = [0 for _ in range(MAXA + 1)]
for a in lista:
    num[a] += 1

s = sum(i * a for i, a in enumerate(num))

for b, c in query:
    n = num[b]

    num[b] = 0
    num[c] += n
    s += n * (c - b)

    print(s)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

from collections import Counter

n = readint()
a = readints()
q = readint()

count = Counter(a)
s = sum(a)

ans = []
for _ in range(q):
    b,c = readints()
    s += (c-b) * count[b]
    ans.append(s)
    count[c] += count[b]
    count[b] = 0
printrows(ans)

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

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

Replacing()

Solution (Python):
from collections import Counter

def Replacing():

    Num = int(input())

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

    Sum = sum(value_A)
    Counter_value = Counter(value_A)

    NumB = int(input())

    for i in range(NumB):
        value_B = list(map(int,input().split()))

        Sum += (value_B[1] - value_B[0])*Counter_value[value_B[0]]
        Counter_value[value_B[1]] += Counter_value[value_B[0]]
        Counter_value[value_B[0]] = 0

        print(Sum)

if __name__ == '__main__':
    Replacing()

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    ans = [sum(a)]
    a = dict(collections.Counter(a))
    for i in range(q):
        b, c = map(int, input().split())
        if b in a.keys():
            ans.append(ans[i] + ((c - b) * a[b]))
            if c in a.keys():
                a[c] = a[b] + a[c]
            else:
                a[c] = a[b]
            del a[b]
        else:
            ans.append(ans[i])
    print(*ans[1:], sep='\n')

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a_list=list(map(int,input().split()))
cnt = [0] * int(1e5 + 10)
sum=0
for i in a_list:
 cnt[i] +=1
 sum+=i
  
q=int(input())
ans=0
 
for j in range(q):
 x,y=map(int,input().split())
 sum+=(y-x)*cnt[x]
 cnt[y]+=cnt[x]
 cnt[x]=0
 print(sum)

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

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

q=int(input())
dic={}
for a in al:
    dic[a]=dic.get(a,0)+1

total=sum(al)
ansl=[]
for i in range(q):
    b,c=map(int,input().split())
    b_num=dic.get(b,0)
    total=total+b_num*(c-b)
    ansl.append(total)
    dic[b]=0
    dic[c]=dic.get(c,0)+b_num

for ans in ansl:
    print(ans)

Solution (Python):
def main():
    n, *i = map(int, open(0).read().split())
    a = i[:n]
    _ = i[n]
    s = sum(a)

    m = [0] * (10 ** 5 + 1)
    for x in a:
        m[x] += 1

    ans = []
    for b, c in zip(*[iter(i[n + 1:])] * 2):
        m[c] += m[b]
        s += (c - b) * m[b]
        m[b] = 0
        ans.append(s)

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

if __name__ == '__main__':
    main()

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
S = sum(A)
Q = int(input())
T = collections.Counter(A)
for i in range(Q):
    b, c = map(int, input().split())
    S += T[b]*(c-b)
    T[c] += T[b]
    T[b] = 0
    print(S)

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
array = []
replace = [0]*(10**5+1)
for i in A:
    replace[i] += 1
sum_s = sum(A)
for i in range(Q):
    m,n = [int(x) for x in input().split()]
    replace[n] += replace[m]

    diff = n*replace[m] - replace[m]*m
    sum_s += diff
    replace[m] = 0
    print(sum_s)

Solution (Python):
n = int(input())
lst = list(map(int, input().split()))
nums = [0] * int(1e5 + 10)
sum = 0
for i in lst:
    nums[i] += 1
    sum += i

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

Solution (Python):
n = int(input())
lst = list(map(int, input().split()))
nums = [0] * int(1e5 + 10)
sum = 0
for i in lst:
    nums[i] += 1
    sum += i

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

Solution (Python):
def main():
    N, *X = map(int, open(0).read().split())
    A = X[:N]
    Q = X[N]
    s = sum(A)
    B = {}
    for a in A:
        if a not in B:
            B[a] = 0
        B[a] += 1

    ans = []
    for i in range(Q):
        b, c = X[N + 1 + 2*i], X[N + 1 + 2*i + 1]
        if b in B:
            num = B[b]
            s += (c - b)*num
            if c not in B:
                B[c] = 0
            B[c] += num
            B[b] = 0
        ans.append(str(s))
    print('\n'.join(ans))

if __name__ == '__main__':
    main()

Solution (Python):
import sys

if sys.subversion[0] == "PyPy":
    import io, atexit

    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))

    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()
RS = raw_input
RA = lambda x=int: map(x, RS().split())
RN = lambda x=int: x(RS())


def solve():
    numbers = {}
    for ai in a:
        if not ai in numbers:
            numbers[ai] = 0
        numbers[ai] += 1
    s = sum(a)
    for b, c in bc:
        if b in numbers:
            s = s + (-b + c) * numbers[b]
            if not c in numbers:
                numbers[c] = 0
            numbers[c] += numbers[b]
            del numbers[b]
        print s
    return




n = RN()
a = RA()
q = RN()
bc = [RA() for _ in range(q)]
solve()

Solution (Python):
val = [0]*(10**5)
N = int(input())
a = list(map(int, input().split()))
Q = int(input())
ans = sum(a)

for i in a:
    val[i-1] += 1

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

Solution (Python):
from collections import Counter

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

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

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

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

bckt = [0]*(int(1e5)+1)
for i in a:
    bckt[i] += 1
    
s = sum(a)

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

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
a_dic = collections.Counter(A)
Q = int(input())
sum_a = sum(A)
for i in range(Q):
    b, c = list(map(int, input().split()))
    sum_a += (c-b) * a_dic[b]
    if b not in a_dic:
        pass
    else:
        if c not in a_dic:
            a_dic[c] = a_dic[b]
        else:
            a_dic[c] += a_dic[b]
        a_dic[b] = 0
    print(sum_a)

Solution (Python):
import collections

n=int(input())
arr=list(map(int,input().split()))
cnt=collections.defaultdict(int)
for val in arr: 
 cnt[val]+=1
sums=sum(arr) 
q=int(input())
for _ in range(q):
 b,c=map(int,input().split())
 diff=(c-b)*(cnt[b]) 
 sums+=diff
 print(sums) 
 cnt[c]+=cnt[b] 
 cnt[b]=0

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

ans = sum(a)
numbers = [0]*(10**5+1)
for v in a:
    numbers[v] += 1

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

Solution (Python):
from collections import Counter
N = int(input())
A = [int(i) for i in input().split()]
S = sum(A)
C = Counter(A)
Q = int(input())
for i in range(Q):
    Bi, Ci = map(int, input().split())
    S += (Ci - Bi) * C[Bi]
    C[Ci] += C[Bi]
    C[Bi] = 0
    print(S)

Solution (Python):
n = int(input())
total = 0
a = list(map(int,input().split()))
table = [0 for i in range(100000+1)]
for i in a:
    total+=i
    table[i]+=1

q = int(input())

for _ in range(q):
    a,b = map(int,input().split())
    total -= table[a]*a
    table[b] += table[a]
    total += table[a]*b
    table[a]= 0
    print(total)

Solution (Python):





from collections import Counter

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

dic = Counter(A)

ans = 0
for i in range(max(A)+1):
    ans += i * dic[i]
for i in range(Q):
    ans += dic[B[i]] * (C[i]-B[i])
    print(ans)
    dic[C[i]] += dic[B[i]]
    dic[B[i]] = 0

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

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

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

        S += (C - B) * Acount[B]
        Acount[C] += Acount[B]
        Acount[B] = 0

        print(S)


if __name__ == '__main__':
	main()

exit()

Solution (Python):
import numpy as np

N = int(input())
A = np.array([int(a) for a in input().split()])
ans = sum(A)
lst = np.zeros(10**5+1)
for a in A:
    lst[a] += 1
   
for q in range(int(input())):
    B,C = map(int,input().split())
    ans += (C - B) * lst[B]
    print(int(ans))
    lst[C] += lst[B]
    lst[B] = 0

Solution (Python):
from sys import *

input = stdin.readline
maxn = 10 ** 5 + 5

Sum = [0] * maxn
Cnt = [0] * maxn

n = int(input())
arr = list(map(int, input().split()))
ans = 0
for x in arr:
	ans += x
	Sum[x] += x
	Cnt[x] += 1 
q = int(input())
result = []
for i in range(q):
	x, y = map(int, input().split())
	ans -= Sum[x]
	ans += y * Cnt[x]
	Sum[y] += y * Cnt[x]
	Cnt[y] += Cnt[x]
	Sum[x] = 0
	Cnt[x] = 0
	result.append(ans)
print('\n'.join(map(str, result)))

Solution (Python):
from sys import *

input = stdin.readline
maxn = 10 ** 5 + 5

Sum = [0] * maxn
Cnt = [0] * maxn

n = int(input())
arr = list(map(int, input().split()))
ans = 0
for x in arr:
	ans += x
	Sum[x] += x
	Cnt[x] += 1 
q = int(input())
for i in range(q):
	x, y = map(int, input().split())
	ans -= Sum[x]
	ans += y * Cnt[x]
	Sum[y] += y * Cnt[x]
	Cnt[y] += Cnt[x]
	Sum[x] = 0
	Cnt[x] = 0
	print(ans)

Solution (Python):
from collections import Counter

n=int(input())
A=list(map(int,input().split()))
q=int(input())
 
counter = Counter(A)
sum_res = sum(A)
for _ in range (q):
       b,af = map(int,input().split())
       sum_res -= b*counter[b]
       sum_res += af*counter[b]
       
       counter[af] += counter[b]
       counter[b] = 0
       print(sum_res)

Solution (Python):
from sys import stdin
from collections import defaultdict
def main():
    
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))
    q=int(readline())
    b=[0]*q
    c=[0]*q
    for i in range(q):
        b[i],c[i]=map(int,readline().split())

    d=defaultdict(int)
    for i in range(n):
        d[a[i]]+=1

    s=sum(a)

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

if __name__=="__main__":
    main()

Solution (Python):
import collections

n = int(input())
a = [int(w) for w in input().split()]
q = int(input())

a_counter = collections.Counter(a)
now_sum = sum(a)
for i in range(q):
    b, c = [int(w) for w in input().split()]

    num = a_counter[b]
    a_counter[b] = 0
    a_counter[c] += num
    

    now_sum += (c - b) * num
    print(now_sum)