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):
n=int(input())
A=list(map(int, input().split()))
q=int(input())
bc=[]
for _ in range(q):
    b,c=map(int, input().split())
    bc.append((b,c))

allsum=sum(A)
cnt={}
for a in A:
    if a not in cnt:
        cnt[a] = 0
    cnt[a]+=1

for b,c in bc:
    if b not in cnt:
        cnt[b] = 0
    if c not in cnt:
        cnt[c] = 0
    allsum += (c-b)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] =0
    print(allsum)

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

Solution (Python):
n=int(input())
A = list(map(int,input().split()))
B = {}
s = 0
for a in A:
  B[a] = B.get(a,0)+1
  s += a
q=int(input())
for _ in range(q):
  b,c=map(int,input().split())
  s = s - b*B.get(b,0) + c*B.get(b,0)
  B[c] = B.get(c,0)+B.get(b,0)
  B[b] = 0
  print(s)

Solution (Python):
import sys
import math
import collections

def set_debug(debug_mode=False):
    if debug_mode:
        fin = open('input.txt', 'r')
        sys.stdin = fin


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


def get(s):
    return str(s % 9) + '9' * (s // 9)

if __name__ == '__main__':
    

    
    t = 1

    for ti in range(1, t + 1):
        n = int(input())
        A = int_input()

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

        res = 0

        for x in A:
            res += x
            d[x] += 1

        q = int(input())

        for _ in range(q):
            b, c = int_input()

            res += (c - b) * d[b]
            d[c] += d[b]
            d[b] = 0

            print(res)

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

    g = {}
    ans = 0
    for a in A:
        ans += a
        if a not in g:
            g[a] = 1
        else:
            g[a] += 1

    for i in range(Q):
        B, C = tuple(BC[i])
        if B in g:
            t = g.pop(B)
            ans += t * (C - B)
            if C in g:
                g[C] += t
            else:
                g[C] = t
        print(ans)


if __name__ == "__main__":
    main()

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

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

Solution (Python):
N = int(input())
numbers = list(map(int,input().split()))
numOfOperation = int(input())
dic = {}

for num in numbers:
    if num in dic : dic[num] += 1
    else: dic[num] = 1
    

sums = sum(numbers)
for i in range(numOfOperation):
    B,C = map(int,input().split())
    if (B in dic) : 
        cnt = dic[B]
        dic[B] = 0
        if (C not in dic) : dic[C] = cnt
        else : dic[C] += cnt
        sums += cnt * (C - B)
    print(sums)

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

og_sum = sum(A)

m = {}

for num in A:
    if num not in m:
        m[num] = 1
    else:
        m[num] += 1

for _ in range(Q):
    B, C = map(int, input().split())
    if B in m:
        cnt = m[B]
        new_sum = og_sum - cnt * B + cnt * C

        print(new_sum)
        og_sum = new_sum

        if C in m:
            m[C] += cnt
        else:
            m[C] = cnt

        del m[B]
    else:
        print(og_sum)

Solution (Python):
ma = lambda :map(int,input().split())
ni = lambda:int(input())
import collections
import math
import itertools
gcd = math.gcd
n = ni()
A = list(ma())
q = ni()
co = collections.Counter(A)
s = sum(A)
for i in range(q):
    b,c = ma()
    s = s - co[b]*b +co[b]*c
    co[c] +=co[b]
    co[b] = 0
    print(s)

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

A = [0] * 100001
for i in a:
  A[i] += 1

ans  = 0
for i in range(len(A)):
  ans += i * A[i]  

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

Solution (Python):
from collections import defaultdict
N=int(input())
a_list = list(map(int,input().split()))
index = defaultdict(int)
total = 0
for a in a_list:
  total += a
  index[a] += 1

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

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    Q = int(input())
    BC = [[int(x) for x in input().split()] for _ in range(Q)]

    ans = sum(A)
    cnt = collections.Counter(A)

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



    

if __name__ == '__main__':
    main()

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

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

Solution (Python):









import sys

def input(): return sys.stdin.readline()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInM1(): return map(int1, input().split())
def iInLH(): return list(map(int, input().split()))
def iInLH1(): return list(map(int1, input().split()))
def iInLV(n): return [iIn() for _ in range(n)]
def iInLV1(n): return [iIn()-1 for _ in range(n)]
def iInLD(n): return [iInLH() for _ in range(n)]
def iInLD1(n): return [iInLH1() for _ in range(n)]
def sInLH(): return list(input().split())
def sInLV(n): return [input().rstrip('\n') for _ in range(n)]
def sInLD(n): return [sInLH() for _ in range(n)]

OutV = lambda x: print(*x, sep='\n')

sys.setrecursionlimit(10 ** 6)

int1 = lambda x: int(x) - 1

INF = int(1e9)
MOD = 1000000007
dx = (-1, 0, 1, 0)
dy = (0, -1, 0, 1)


N = iIn()
A = iInLH()
Q = iIn()
BC = iInLD(Q)

ssum = sum(A)
lst = [0]*100000
for i in range(N):
    lst[A[i]-1] += 1

for i in range(Q):
    n_b = lst[BC[i][0]-1]
    ssum = ssum - BC[i][0] * n_b + BC[i][1] * n_b
    print(ssum)
    lst[BC[i][0]-1] = 0
    lst[BC[i][1]-1] += n_b

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

Solution (Python):
import collections
N = int(input())
A = list(map(int,input().split()))
SUMA = sum(A)
C = collections.Counter(A)
Q = int(input())
BC = [list(map(int,input().split())) for _ in range(Q)]
for bc in BC:
    SUMA += C[bc[0]]*bc[1]-C[bc[0]]*bc[0]
    print(SUMA)
    C[bc[1]] = C[bc[1]]+C[bc[0]]
    C[bc[0]] = 0

Solution (Python):
from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
A.sort()
Q = int(stdin.readline())
D = dict()
S = set(A)
sum=0
count=0

for y in range(0,N):
    sum+=A[y]
    if y>0 and A[y]!=A[y-1]:
        D[A[y-1]]=count
        count=1
    elif y==0:
        count=1
    else:
        count+=1
    if y==N-1:
        D[A[y]]=count

for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    if U[0] in D:
        sum+=(D[U[0]])*(U[1]-U[0])
        if U[1] in D:
            D[U[1]]+=D[U[0]]
        else:
            D[U[1]]=D[U[0]]
        D[U[0]]=0


    print(sum)

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

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

  col[c]+=col[b]
  col[b]=0

Solution (Python):
MX = 100005
n = int(input())
cnt = [0] * MX
a = list(map(int, input().split()))

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

total = 0

for i in range(MX):
    total += i*cnt[i]

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

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

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

Solution (Python):
from collections import Counter 
from collections import defaultdict
from collections import deque
from functools import reduce
import math
import itertools
import heapq

import bisect
import sys

sys.setrecursionlimit(10**6)


def bfs(s,n,node):
    
    check=[False for _ in range(n)]
    check[s]=True
    
    queue=deque([s])
    visited_num=1

    
    
    color=[-1 for _ in range(n)]
    color[s]=0
    
    while visited_num<n:
        
        if len(queue)==0:
            
            
            return color

        now_vertex=queue.popleft()
        
        for next_vertex in node[now_vertex]:


            if check[next_vertex]==True:
                continue

            queue.append(next_vertex)
            check[next_vertex]=True

            
            color[next_vertex]=color[now_vertex]+1
            visited_num+=1

    return color

mod=10**9+7



ceil=lambda x,y: (x+y-1)//y
input_list = lambda : list(map(int,input().split()))

n=int(input())
tmp=list(map(int,input().split()))
ans=sum(tmp)
tmp=Counter(tmp)
di=defaultdict(int)
for i in tmp.keys():
    di[i]+=tmp[i]

q_num=int(input())

for _ in range(q_num):
    
    v,w=map(int,input().split())
    
    ans=ans-di[v]*v+di[v]*w
    di[w]+=di[v]
    del di[v]
    print(ans)

Solution (Python):
n = int(input())
m=list(map(int,input().split()))
d={}
k=0
for i in range(n):
    k+=m[i]
    if m[i] in d:
        d[m[i]]+=1
    else:
        d[m[i]]=1
for x in range(int(input())):
    
    b,c=list(map(int,input().split()))

    
    
    if b in d:
        k+=d[b]*(c-b)
        if c in d:
            d[c]+=d[b]
        else:
            d[c] = d[b]
        del d[b] 
    print(k)

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

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

  print(s)

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

a_cnts = {}
a_sum = 0
for an in a:
    if an in a_cnts.keys():
        a_cnts[an] += 1
    else:
        a_cnts[an] = 1
    a_sum += int(an)

q = int(input())

for i in range(q):
    (b, c) = input().split()

    if b in a_cnts.keys():
        hit_cnt = a_cnts[b]
        del a_cnts[b]
        if c in a_cnts.keys():
            a_cnts[c] += hit_cnt
        else:
            a_cnts[c] = hit_cnt
        a_sum += (int(c) - int(b))*hit_cnt

    print(a_sum)

Solution (Python):
import sys

def main(n, a_n, q, bc_n):
    m = {}
    for i in range(n):
        if a_n[i] not in m:
            m[a_n[i]] = 0
        m[a_n[i]] += 1

    s = []
    for j in range(q):
        b, c = bc_n[j]

        diff = 0
        if b in m:
            diff = m[b] * c  - m[b] * b
            if c not in m:
                m[c] = m[b]
            else:
                m[c] += m[b]
            del m[b]

        ts = 0
        if len(s) == 0:
            for k, v in m.items():
                ts += k * v
        else:
            ts = s[-1] + diff
        s.append(ts)
    return s


n = int(sys.stdin.readline().strip())
a_n = [int(x) for x in sys.stdin.readline().strip().split(" ")]
q = int(sys.stdin.readline().strip())
bc_n = []
for _ in range(q):
    b, c = [int(x) for x in sys.stdin.readline().strip().split(" ")]
    bc_n.append((b, c))
for i in main(n, a_n, q, bc_n):
    print(i)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
s=sum(A)
num=[0]*100001
for i in A:
    num[i]+=1
for _ in range(int(input())):
    B,C=map(int,input().split())
    s+=(C-B)*num[B]
    num[C]+=num[B]
    num[B]=0
    print(s)

Solution (Python):

N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())
process = []
proc_append = process.append
hash = [0]*100001
for i in range(Q):
    proc_append(tuple(input().split()))
    process[i] = tuple(map(int,process[i]))

goukei = sum(A)

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

for b,c in process:
	if hash[b] != 0:
		goukei += (c-b)*hash[b]
		hash[c] += hash[b]
		hash[b] = 0
	print(goukei)

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

ans = sum(A)
d = collections.defaultdict(int)

for key in A:
    d[key] += 1

for i in range(Q):
    b,c = BC[i][0],BC[i][1]
    ans += d[b]*(c-b)
    d[c] += d[b]
    d[b] = 0
    print(ans)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
l=[0]*100001
a=sum(A)
for i in A:
    l[i]+=1
for i in range(Q):
    b,c=map(int,input().split())
    a+=(c-b)*l[b]
    l[c]+=l[b]
    l[b]=0
    print(a)

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

S = sum(A)

idx = dict()
for i in range(len(A)):
	idx.setdefault(A[i], 0)
	idx[A[i]] += 1

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

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

NumCnt = {}
for a in A:
  if a in NumCnt:
    NumCnt[a]+=1
  else:
    NumCnt[a]=1
    
S = sum(A)
for b,c in zip(B,C):
  if b in NumCnt:
    cnt_c = NumCnt.pop(b)
    S += (c-b)*cnt_c
    if c in NumCnt:
      NumCnt[c] +=cnt_c
    else:
      NumCnt[c] = cnt_c
  else:
    pass
  print(S)

Solution (Python):



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


nums = [0] * (10**5 + 1)
cur_sum = sum(a_nums)
for a in a_nums:
    nums[a] += 1


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

Solution (Python):



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


nums = [0] * (10**5 + 1)
cur_sum = sum(a_nums)
for a in a_nums:
    nums[a] += 1


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

Solution (Python):
def kazu_up(kosu, a, kazu):
    if a in kazu:
        kazu[a] += kosu
    else:
        kazu[a] = kosu
    
    return kazu

def kazu_down(a, kazu):
    if a in kazu:
        del kazu[a]
            
    return kazu

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC=[]
kazu = {}

for q in range(Q):
    BC.append(list(map(int, input().split())))
    
A_sum = 0
    
for a in A:
    A_sum += a
    kazu = kazu_up(1, a, kazu)

for bc in BC:
    try:
        A_sum += ( bc[1] - bc[0] ) * kazu[bc[0]]
        print(A_sum)
        kazu = kazu_up(kazu[bc[0]], bc[1], kazu)
        kazu = kazu_down(bc[0], kazu)
        
    except:
        print(A_sum)

Solution (Python):
from sys import stdin, setrecursionlimit
from collections import defaultdict


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    ans = sum(a)
    dic = defaultdict(int)
    for ai in a:
        dic[ai] += 1
    q = int(input())
    bc = [list(map(int, input().split())) for _ in range(q)]
    for b, c in bc:
        count = dic[b]
        ans += count * (c - b)
        print(ans)
        dic[b] -= count
        dic[c] += count


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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


dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1

ans = sum(A)
for q in range(Q):
  bc = C[q] - B[q]
  ans = ans + bc * dic.get(B[q],0)
  dic[C[q]] = dic.get(C[q],0) + dic.get(B[q],0)
  dic[B[q]] = 0
  print(ans)

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

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

def main():
    mod=10**9+7
    N=I()
    A=LI()
    from collections import defaultdict
    dd = defaultdict(int)
    for i in range(N):
        dd[A[i]]+=1
        
    ans=sum(A)
        
    Q=I()
    for i in range(Q):
        b,c=MI()
        cnt=dd[b]
        ans=ans+cnt*(c-b)
        print(ans)
        dd[c]+=cnt
        dd[b]=0

        
    
    

main()

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

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

Solution (Python):
N = int(input())
A = [0]*N
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())


S = [0]*Q









cnt = [0] * 100005 
for i in A:
    cnt[i]+=1 

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

Solution (Python):
input()
arr = [int(num) for num in input().strip().split(' ')]
q = int(input())

queries = []

for i in range(q):
    query = [ int(num) for num in input().strip().split(' ')]
    queries.append(query)

nums = dict()
summ = 0

for number in arr:
    summ += number
    if number not in nums:
        nums[number] =  1
    else:
        nums[number] += 1

for query in queries:
    curr, new = query
    if curr not in nums: 
        print(summ)
        continue
    count = nums[curr]
    diff = (new-curr)*count
    summ += diff
    del nums[curr]
    if new not in nums:
        nums[new] = count
    else:
        nums[new] += count
    print(summ)

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import Counter
def resolve():
    n = int(input())
    C = Counter(map(int, input().split()))
    total = 0
    for key, val in C.items():
        total += key * val

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

Solution (Python):
from collections import Counter

N = int(input())
numbers = [int(i) for i in input().split()]
Q = int(input())

counted = Counter(numbers)
sum_n = sum(numbers)

for _ in range(Q):
    b, c = [int(i) for i in input().split()]
    sum_n = sum_n - ((b-c) * counted[b])
    counted[c] += counted[b]
    counted[b] -= counted[b]
    print(sum_n)

Solution (Python):






n = int(input())
a = list(map(int, input().split()))
q = int(input())
req = [list(map(int, input().split())) for _ in range(q)]
count = [0]*100001
sum = sum(a)

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

for k in range(q):
    sum += (req[k][1] - req[k][0]) * count[req[k][0]]
    count[req[k][1]] += count[req[k][0]]
    count[req[k][0]] = 0
    print(sum)

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

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[]
c=[]
for _ in range(q):
    x,y=map(int,input().split())
    b.append(x)
    c.append(y)

d=[0]*100001
for i in a:
    d[i]+=1

ans=sum(a)
for m,p in zip(b,c):
    minus=d[m]
    d[m]=0
    d[p]+=minus
    ans+=(p-m)*minus
    print(ans)

Solution (Python):


def func(x,y): 
    if x not in d:
        v = 0
    else:
        v = d[x]
        if y not in d:
            d[y] = 0
            d[y] = d[x]
            del d[x]
        else:
            d[y] += d[x]
            del d[x]
    return v

n = int(input())
a = list(map(int, input().split()))
d = {}
for key in a:
    if key not in d:
        d[key] = 0
    d[key] += 1

q = int(input())
sum_a = sum(a)
for i in range(q):
    b,c = map(int, input().split())
    v = func(b,c)
    sum_a += (c-b)*v 
    print(sum_a)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
L = [0] * (100001)
s = sum(A)
for a in A:
    L[a] += 1
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().split())
    L[C] += L[B]
    s += L[B] * (C-B)
    L[B] = 0
    print(s)

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

for ai in a:
  if a_dict.get(ai):
    a_dict[ai] += 1
  else:
    a_dict[ai] = 1

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

Solution (Python):
from collections import defaultdict


def main():
    N = int(input())
    A = list(map(int, input().split()))
    cnt = defaultdict(int)
    total = 0
    for a in A:
        cnt[a] += 1
        total += a
    Q = int(input())
    for _ in range(Q):
        b, c = map(int, input().split())
        cnt[c] += cnt[b]
        total += (c-b) * cnt[b]
        cnt[b] = 0
        print(total)


if __name__ == "__main__":
    main()

Solution (Python):
import collections

N = int(input())
a = list(map(int,input().split()))
su = sum(a)

Q = int(input())

c = collections.Counter(a)

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

    if(C not in c):
        c[C] = 0

    num_B = c[B] 
    c[B] = 0
    c[C] += num_B
    
    

    
    
    

    su -= num_B * B 
    su += num_B * C 

    print(su)

Solution (Python):
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
S = sum(A)
cA = Counter(A)

q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    num_b = cA[b]
    if num_b > 0:
        cA[b] = 0
        cA[c] += num_b
        S += num_b * (c - b)
    print(S)

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


counter = [0] * 1000000
for i in a:
    counter[i] += 1
 
ans = sum(a)
 
for _ in p:
    ans = ans + counter[_[0]] * _[1] - counter[_[0]] * _[0]
    counter[_[1]] += counter[_[0]]
    counter[_[0]] = 0
    print(ans)

Solution (Python):
import math
import collections
import fractions
import itertools

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    sushi = sum(a)
    coll = collections.Counter(a)
    for _ in range(q):
        bef, aft = map(int, input().split())
        sushi += (aft-bef)*coll[bef]
        coll[aft] += coll[bef]
        coll[bef] = 0
        print(sushi)
    return 0

if __name__ == "__main__":
    solve()

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

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

n = int(input())
A = list(map(int,input().split()))
q = int(input())
M=[list(map(int,input().split())) for _ in range(q)]
Sum = sum(A)
d={}

for i in range(n):
    if A[i] not in d:
        d[A[i]]=1
    else:
        d[A[i]]+=1

for i in M:
    prev = 0
    a,b=i[0],i[1]
    if a not in d:
        print(Sum)
        continue
    
    elif b in d:
        prev = d[b]*b
        d[b]+=d[a]
    else:
        d[b]=d[a]
    
    Sum += d[b]*b-d[a]*a-prev

    d.pop(a)
    print(Sum)

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

Solution (Python):
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    D = Counter(A)
    res = sum(A)
    for b, c in query:
        if D.get(b) is not None:
            res -= b * D.get(b)
            res += c * D.get(b)

        if D.get(c) is not None:
            D[c] += D[b]
        else:
            D[c] = D[b]
        D[b] = 0

        print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import bisect

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

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

for i in range(len(A)):
    DP[A[i]] += 1

Sum = sum(A)

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

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

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

d = dict()
for num in S:
    d.setdefault(num, 0)
    d[num] += 1


ans = sum(s*t for s, t in d.items())

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

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

q = int(input())
import collections
cc = collections.Counter(a)

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

Solution (Python):
import collections

n=int(input())
a=list(map(int,input().split()))
q=int(input())
s=sum(a)
cc=collections.Counter(a)


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

Solution (Python):
import collections

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

c=collections.Counter(a)
s=sum([j*k for j,k in c.items()])
for x in b:
    s=s-x[0]*c[x[0]]+x[1]*c[x[0]]
    c[x[0]],c[x[1]]=0,c[x[0]]+c[x[1]]
    print(s)

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

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
Q = int(input())
bc = [[int(i) for i in input().split()] for j in range(Q)]
 
counter = [0] * 1000000
for i in a:
    counter[i] += 1

ans = sum(a)
 
for _ in bc:
    ans = ans + counter[_[0]] * _[1] - counter[_[0]] * _[0]
    counter[_[1]] += counter[_[0]]
    counter[_[0]] = 0
    print(ans)

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

d = {}
for x in arr:
    d[x] = d.get(x,0) + 1

tot = sum(arr)

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

    if b not in d:
        print(tot)
    else:
        diff = (c-b)*d[b]
        tot+=diff
        print(tot)
        val = d[b]
        del d[b]
        d[c] = d.get(c,0) + val

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

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
q = int(input())
a_dict = dict()
for i in a:
    if i in a_dict:
        a_dict[i] = a_dict[i] + 1
    else:
        a_dict[i] = 1
a_sum = sum(a)

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

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):
from collections import defaultdict

N = int(input())
A = tuple(map(int, input().split()))
dic = defaultdict(int)

for a in A:
  dic[a] += 1
    
Q = int(input())

base = sum(A)

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

Solution (Python):
import numpy as np


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

sumA = sum(A)
cnt_num = np.zeros(10**5 + 1)

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

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