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 = input().split()
num_list = [0] * 10**5
ans = 0

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

for i in range(10**5):
    ans += (i+1) * num_list[i]

Q = int(input())

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

    ans = ans + C * num_list[B-1] - B * num_list[B-1]
    num_list[C-1] += num_list[B-1]
    num_list[B-1] = 0

    print(ans)

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

cnt_dict = {}


s = sum(A)

ans = 0

for item in A:
    if item not in cnt_dict:
        cnt_dict[item] = 0
    cnt_dict[item] += 1

for _ in range(Q):
    X,Y = map(int,input().split())

    if X not in cnt_dict:
        cnt_dict[X] = 0
    if Y not in cnt_dict:
        cnt_dict[Y] = 0

    s -= cnt_dict[X] * X
    s += cnt_dict[X] * Y

    cnt_dict[Y] += cnt_dict[X]
    cnt_dict[X] = 0


    print(s)





'''


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



for i in range(Q):
    X,Y = map(int,input().split())

    ans = 0

    for j in range(N):
        if A[j] == X:
            ans += Y
            A[j] = Y
        else:
            ans += A[j]

    print(ans)

    '''

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 Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())

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

Solution (Python):
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
cnt=[0]*(10**5+10)
for a in A:
    cnt[a]+=1
s=sum(A)
Q=int(input())
for _ in [0]*Q:
    b,c=map(int,input().split())
    s+=cnt[b]*(c-b)
    cnt[c]+=cnt[b]
    cnt[b]=0
    print(s)

Solution (Python):
import math , sys

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

count = [0 for _ in range( 10**5 + 1)]

for each in A:
    count[each]+=1
ans = sum(A)
for _ in range(Q):
    B , C = list( map( int , input().split() ) )
    D = count[B]
    ans += (C-B) * D
    count[B] = 0
    count[C] += D
    print(ans)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
ans = sum(a)
ans_li = []
q = int(input())

d = {}
for i in range(len(a)):
    if a[i] not in d:
        d[a[i]] = 0
    d[a[i]] += 1

for i in range(q):
    B,C = map(int,input().split())
    if B not in d:
        ans_li.append(ans)
    else:
        num = d[B]
        dis = abs(B - C)
        if B >= C:
            ans_li.append(ans - (num * dis))
            ans = ans_li[-1]
        else:
            ans_li.append(ans + (num * dis))
            ans = ans_li[-1]

        if C not in d:
            d[C] = num
            d[B] = 0
        else:
            d[C] += num
            d[B] = 0

for aa in ans_li:
    print(aa)

Solution (Python):
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    q = int(input())
    sum_a = sum(a_list)

    a_dict = {}
    for a in a_list:
        if a not in a_dict:
            a_dict[a] = 1
        else:
            a_dict[a] += 1

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

        if b not in a_dict:
            a_dict[b] = 0
        sum_a += (c - b) * a_dict[b]

        if c not in a_dict:
            a_dict[c] = 0
        a_dict[c] += a_dict[b]
        a_dict[b] = 0

        print(sum_a)


if __name__ == "__main__":
    main()

Solution (Python):
import collections

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

    a_dict = collections.Counter(a)
    a_dict = {key: key*value for key, value in a_dict.items()}
    answer = sum(a_dict.values())
    for _ in range(q):
        b, c = map(int, input().split())
        diff = 0
        if b in a_dict:
            b_sum = a_dict.pop(b)
            c_add = b_sum//b * c
            diff = c_add - b_sum
            if c in a_dict:
                a_dict[c] += c_add
            else:
                a_dict[c] = c_add
        answer += diff
        print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N = int(input())

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

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

all_count = Counter(A)
S = sum(A)

for B, C in B_C:

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

    print(S)

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

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

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
Q = int(input())
 
sumAs = 0
dictAs = [0] * 100000
for i in range(len(As)):
  sumAs += As[i]
  dictAs[As[i] - 1] += 1


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

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

s = sum(a)
nums = {}
for i in range(n):
    if a[i] in nums:
        nums[a[i]] += 1
    else:
        nums[a[i]] = 1

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

Solution (Python):

"""
Created on Mon Sep  7 01:13:08 2020

@author: liang
"""
"""
ABC  171 D 差分更新　パケット変換
【無駄な探索は行わない（メモリ化） ⇒　パケット変換】
パケット
　値vが出現する回数を記録しておく

dict()方式　⇒　探索にO(logn) or O(n)がかかる
配列方式　⇒　探索がO(1)となる!

【差分更新】
値Bが全て値Cになる
　⇒　(C - B) * 値Bの個数だけ増加する
 ⇒　値Cの個数　+= 値Bの個数
 ⇒　値Bの個数　= 0
"""

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

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

for i in A:
    d[i-1] += 1

ans = sum(A)

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

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

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

for i in A:
    d[i-1] += 1

ans = sum(A)

for i in range(Q):
    b, c = map(int,input().split())
    ans += (c-b)*d[b-1]
    d[c-1] += d[b-1]
    d[b-1] = 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):
    before, after = map(int, input().split())

    
    sum_res -= before * counter[before]
    sum_res += after * counter[before]

    
    counter[after] += counter[before]
    counter[before] = 0

    print(sum_res)

Solution (Python):
if __name__ == '__main__':

    n = int(input())
    B = [0]*(10**5+1)
    A = list(map(int,input().split()))
    for a in A:
        B[a] += 1
    
    sm = sum(A)
    m = int(input())

    for j in range(m):
        b,c = map(int,input().split())
        
        cnt_b = B[b]
        cnt_c = B[c]
        if cnt_b == 0:
            
            print(sm)
        else:
            B[b] = 0
            B[c] += cnt_b
            sm = sm + c*cnt_b - b*cnt_b
            print(sm)

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

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

s = sum([k * v for k, v in d.items()])

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

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

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

cnt_dict = {}

s = sum(A)
ans = []

for item in A:
    if item not in cnt_dict:
        cnt_dict[item] = 0
    cnt_dict[item] += 1

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

    if b not in cnt_dict:
        cnt_dict[b] = 0
    if c not in cnt_dict:
        cnt_dict[c] = 0

    s -= b * cnt_dict[b]
    s += c * cnt_dict[b]

    cnt_dict[c] += cnt_dict[b]
    cnt_dict[b] = 0

    ans.append(s)

for i in range(Q):
    print(ans[i])

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    S = sum(A)
    cunt_A = Counter(A)
    for q in range(Q):
        b, c = map(int,input().split())
        S += (c-b)*cunt_A[b]
        cunt_A[c] += cunt_A[b]
        cunt_A[b] = 0
        print(S)

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    S = sum(A)
    cunt_A = Counter(A)
    for q in range(Q):
        b, c = map(int,input().split())
        S += (c-b)*cunt_A[b]
        cunt_A[c] += cunt_A[b]
        cunt_A[b] = 0
        print(S)

if __name__=='__main__':
    main()

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

cnt = [0 for i in range(10**5)]
a = sorted(a)
s = sum(a)

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

q = int(input().strip())

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

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

a_dict = {}

for i in a:
    if not i in a_dict:
        a_dict[i] = 0
    a_dict[i] += 1

q = int(input())

s = sum(a)


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

    if b in a_dict:
        s -= b * a_dict[b]
        a_dict[c] += a_dict[b]
    s += c * a_dict[c]

    a_dict[b] = 0

    print(s)

Solution (Python):
from collections import Counter

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

sum_a = sum(A)

d = Counter(A)

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

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

sum_a = sum(A)

d = dict()

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

for _ in range(int(input())):
    b, c = map(int, input().split())
    if d.get(b, 0) != 0:

        temp = d[b]         
        d[b] -= temp
        d[c] = d.get(c, 0) + temp

        sum_a += (c - b) * temp
    print(sum_a)

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

    sum_a = sum(A)

    d = dict()

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

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:

            temp = d[b]         
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

            sum_a += (c - b) * temp
        print(sum_a)
        
main()

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

    sum_a = sum(A)

    d = dict()

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

    for _ in range(int(input())):
        b, c = map(int, input().split())
        if d.get(b, 0) != 0:

            temp = d[b]         
            d[b] -= temp
            d[c] = d.get(c, 0) + temp

            sum_a += (c - b) * temp
        print(sum_a)
        
main()

Solution (Python):


import sys
input = sys.stdin.buffer.readline
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

temp = [0]*(10**5+1)
ans = 0

for i in A:
    temp[i] += 1
    ans += i
for i in range(Q):
    B, C = map(int,input().split()) 
    ans += temp[B]*(C-B)
    temp[C] += temp[B]
    temp[B] = 0

    print(ans)

Solution (Python):
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    import collections
    counter = collections.Counter(A)
    
    Q = int(input())
    BC = [list(map(int, input().split())) for _ in range(Q)]
    ans = sum(A)
    for b, c in BC:
        if b in counter:
            ans -= b*counter[b]
            ans += c*counter[b]
            if c not in counter:
                counter[c] = 0
            counter[c] += counter[b]
        counter[b] = 0
        
        print(ans)


if '__main__' == __name__:
    resolve()

Solution (Python):
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b in t.keys():
            s += (c - b) * t[b]
            t[b], t[c] = 0, t[b] + t[c]
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if b not in t.keys():
            ans.append(s)
            continue
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        s += (c - b) * t[b]
        t[c] += t[b]
        t[b] = 0
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
from collections import Counter


def solve(string):
    n, *aqbc = map(int, string.split())
    a, _, bc = aqbc[:n], aqbc[n], aqbc[n + 1:]
    s = sum(a)
    t = Counter(a)
    ans = []
    for b, c in zip(*[iter(bc)] * 2):
        if c in t.keys():
            t[c] += t[b]
        else:
            t[c] = t[b]
        s += (c - b) * t[b]
        del t[b]
        ans.append(s)
    return "\n".join(map(str, ans))


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
import collections

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

count = collections.Counter(a)
sums = sum(a)

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

Solution (Python):
n=int(input())
A=list(map(int,input().split()) )
q=int(input())
B=[]
C=[]

a_sum = sum(A)
counter = [0]*10**5

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

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

Solution (Python):
N = int(input())
natural = list(map(int,input().split()))
Q = int(input())
number = [0]*(10**5+1)
for i in range(N):
    number[natural[i]] += 1
Nsum = sum(natural)
ans = []
for i in range(Q):
    B,C = map(int,input().split())
    count = number[B]
    diff = C-B
    number[C] += count
    number[B] = 0
    Nsum += diff * count
    ans.append(Nsum)

for i in range(Q):
    print(ans[i])

Solution (Python):
n = int(input())
lst = [0]*(10**5)

l = list(map(int,input().split()))
total = sum(l)  
for x in l:
  lst[x-1] += 1

  
m = int(input())  
for i in range(m):
  x,y = map(int,input().split())
  lst[y-1] += lst[x-1]
  total += (y-x) * lst[x-1]
  lst[x-1] = 0  
  print(total)

Solution (Python):
from collections import defaultdict
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    q = int(input())
    bc = []
    sum1 = sum(a)
    for _ in range(q):
        bc.append(list(map(int, input().split(" "))))
    d = defaultdict(lambda:0)
    for i in range(n):
        d[a[i]] += 1

    for i in range(q):
        if bc[i][0] in d:
            sum1 += (bc[i][1] - bc[i][0]) * d[bc[i][0]]
            d[bc[i][1]] += d[bc[i][0]]
            del d[bc[i][0]] 
        print(sum1)
        

if __name__ == "__main__":
    main()

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

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

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

total = sum(A)

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

for i in range(Q):
  j = BC[i][0]
  k = BC[i][1]
  if li[j - 1] > 0:
    li[k - 1] += li[j - 1]
    total += li[j - 1] * (k - j)
    li[j - 1] = 0
  print(total)

Solution (Python):
from collections import Counter

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

cou = Counter(a)
s = sum(a)

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

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):
    before, after = map(int, input().split())

    
    sum_res -= before * counter[before]
    sum_res += after * counter[before]

    
    counter[after] += counter[before]
    counter[before] = 0

    print(sum_res)

Solution (Python):


from collections import defaultdict

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):
    bb,cc = map(int,input().split())
    b[i] = bb
    c[i] = cc

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

for i in range(q):
    if b[i] <= c[i]:
        dd = c[i]-b[i]
        sub += d[b[i]] * dd
        d[c[i]] += d[b[i]]
        d[b[i]] = 0
    else:
        dd = b[i]-c[i]
        sub -= d[b[i]] * dd
        d[c[i]] += d[b[i]]
        d[b[i]] = 0
    print(sub)

Solution (Python):

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

count = [0 for i in range(int(1e5+1))]

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

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

Solution (Python):
from collections import defaultdict

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

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

Solution (Python):
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
Q = int(input())
ans = []
s = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    n = cnt[b]
    if b in cnt:
        cnt[c] += cnt[b]
        del cnt[b]
    s += (c-b)*n
    print(s)

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

l=[0]*10**6
total=0

for i in range(N) :
    l[A[i]]+=1   
    total+=A[i]  

for i in range(Q) :
    s0=S[i][0]  
    s1=S[i][1]  
    total+=s1*l[s0]-s0*l[s0]
    l[s1]+=l[s0]
    l[s0]=0
    print(total)

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

num_dic = {}
for i in range(N):
    if A[i] in num_dic:
        num_dic[A[i]] += 1
    else:
        num_dic[A[i]] = 1

tmp = sum(A)
for query in querys:
    if query[0] in num_dic:
        tmp -= num_dic[query[0]] * query[0]
        tmp += num_dic[query[0]] * query[1]

        if query[1] in num_dic:
            num_dic[query[1]] += num_dic[query[0]]
        else:
            num_dic[query[1]] = num_dic[query[0]]
        num_dic[query[0]] = 0

    print(tmp)

Solution (Python):
import math
from itertools import combinations
from itertools import combinations_with_replacement
from itertools import product
from collections import defaultdict
from collections import deque
from collections import Counter
import heapq


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


N = int(input())
A_LI = input_li()
Q = int(input())
cnt = Counter(A_LI)
ans = sum(A_LI)
BC_LI = []
for _ in range(Q):
    BC_LI.append(input_li())
for b, c in BC_LI:
    ans -= b * cnt[b]
    ans += c * cnt[b]
    print(ans)
    if c in cnt:
        cnt[c] += cnt[b]
    else:
        cnt[c] = cnt[b]
    cnt[b] = 0

Solution (Python):
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
ANS = sum(A)
A = Counter(A)
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    print(ANS + (C - B) * A[B])
    ANS += (C - B) * A[B]
    A[C] = A[C] + A[B]
    A[B] = 0

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

l = [0] * 10**6
tot = 0
for i in range(N) :
    l[A[i]]+=1
    tot += A[i]
for i in range(Q):
    s0 = S[i][0]
    s1 = S[i][1]
    tot += s1 * (l[s0] + l[s1]) - (s0 * l[s0] + s1*l[s1])
    l[s1] += l[s0]
    l[s0] = 0
    print(tot)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
S = [list(map(int, input().split())) for l in range(Q)]
 
numList = {}
sum = 0

for n in range(N):
  if A[n] not in numList:
    numList[A[n]] = 0
  numList[A[n]] += 1
  sum += A[n]

for q in range(Q):
  if S[q][0] in numList:
    if S[q][1] not in numList:
      numList[S[q][1]] = 0
    sum += (S[q][1] - S[q][0]) * numList[S[q][0]]
    numList[S[q][1]] += numList[S[q][0]]
    numList[S[q][0]] = 0
  print(sum)

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def main():
    n = II()
    aa = MII()
    q = II()
    bc = [MII() for _ in range(q)]
    sumv = sum(aa)
    numbers = [0]*(10**5+1)
    for a in aa:
        numbers[a] += 1

    for b,c in bc:
        numb = numbers[b]
        sumv += numb*(c-b)
        print(sumv)
        numbers[b] = 0
        numbers[c] += numb

if __name__ == '__main__':
    main()

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    v_freq = collections.Counter(A)
    v_sum = 0
    for v, freq in v_freq.items():
        v_sum += v * freq
    Q = int(input())
    for _ in range(Q):
        B, C = map(int, input().split())
        if B in v_freq:
            v_sum -= (B - C) * v_freq[B]
            if C in v_freq:
                v_freq[C] += v_freq[B]
            else:
                v_freq[C] = v_freq[B]
            del v_freq[B]
            print(v_sum)
        else:
            print(v_sum)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
cnt = [0]*(10**5+1)

s = sum(A)
for i in A:
  cnt[i] += 1

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

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

total=sum(a)

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
A = tuple(map(int,input().split()))
Q = int(input())
cnt = [0]*100005
s = sum(A)
for i in A:
    cnt[i] += 1
for i in range(Q):
    B, C = map(int,input().split())
    s += (C-B)*cnt[B]
    print(s)
    cnt[C] += cnt[B]
    cnt[B] = 0

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
cnt = [0]*100005
s = sum(A)
for i in A:
    cnt[i] += 1
for i in range(Q):
    B, C = map(int,input().split())
    s += (C-B)*cnt[B]
    print(s)
    cnt[C] += cnt[B]
    cnt[B] = 0

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

Solution (Python):
import array

N = int(input())
A = array.array('L', list(map(int, input().split())))
MAX = 10**5+1
X = array.array('L', [0]) * MAX
Y = array.array('L', range(MAX))

for i in range(len(A)): X[A[i]] += 1
    
Q = int(input())
cur = sum(array.array('L', (X[i]*Y[i] for i in range(len(X)))))

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

Solution (Python):
def main():
	N = int(input())
	A = [int(a) for a in input().split(" ")]
	S = sum(A)
	cA = [0] * 1000001
	for i in range(len(A)):
		cA[A[i]] += 1
	Q = int(input())
	for i in range(Q):
		B, C = [int(x) for x in input().split(" ")]
		diff = (C-B)*cA[B]
		S += diff
		cA[C] += cA[B]
		cA[B] = 0
		print(S)

main()

Solution (Python):
n = int(input())
a_list = [int(i) for i in input().split()]
a_dict = {}
for a in a_list:
  if a in a_dict:
    a_dict[a] += 1
  else:
    a_dict[a] = 1
q = int(input())
si = 0
for a in a_dict:
  si += a * a_dict[a]
for _ in range(q):
  bi, ci = [int(i) for i in input().split()]
  if not bi in a_dict:
    print(si)
    continue
  if not ci in a_dict:
    a_dict[ci] = 0
  a_dict[ci] += a_dict[bi]
  si += (ci - bi) * a_dict[bi]
  a_dict[bi] = 0
  print(si)

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

X=[0]*(10**5+1)
for i in range(N):
  X[A[i]]+=1
  S=S+A[i]





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

Solution (Python):

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

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
C_A = Counter(A)
S = 0
for ca in C_A:
  S += ca * C_A[ca]

Q = int(input())
for q in range(Q):
  b, c = map(int,input().split())
  S += (c - b) * C_A[b]
  C_A[c] += C_A[b]
  C_A[b] = 0
  
  print(S)

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

Solution (Python):
from collections import*
_,A,_,*Q=[map(int,l.split())for l in open(0)];s=sum(A:=[a for a in A]);C=Counter(A)
for b,c in Q:s+=(c-b)*C[b];C[c]+=C[b];C[b]=0;print(s)

Solution (Python):
from collections import*
_,A,_,*Q=[map(int,l.split())for l in open(0)];A=[a for a in A];s=sum(A);C=Counter(A)
for b,c in Q:s+=(c-b)*C[b];C[c]+=C[b];C[b]=0;print(s)

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

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

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

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

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

asum = sum(a)
adic = {}

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


for i in range(q):
  b, c = map(int, input().split())
  if b in adic:
    if c not in adic:
      adic[c] = 0
    cnt = adic.pop(b)
    adic[c] += cnt
    asum += (c-b) * cnt
  print(asum)