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 i in range(q):
    bc.append(list(map(int, input().split())))

s = 0
for i in range(n):
    s += a[i]

import collections
cnt = dict(collections.Counter(a))

for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    if b in cnt:
        if c in cnt:
            num = cnt[b]
            s += (c - b) * num
            cnt[c] += cnt[b]
            del cnt[b]
        else:
            num = cnt[b]
            s += (c - b) * num
            cnt[c] = num
            del cnt[b]
        print(s)
    else:
        print(s)

Solution (Python):
import collections

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

cur_sum = 0
value_count_map = collections.defaultdict(int)
for a in A:
    cur_sum += a
    value_count_map[a] += 1

for b, c in zip(B, C):
    b_cnt = value_count_map.get(b, 0)
    value_count_map[b] = 0
    value_count_map[c] += b_cnt
    cur_sum += (b_cnt * (c - b))
    print(cur_sum)

Solution (Python):
from collections import Counter
n = int(input())
al = list(map(int,input().split()))
q = int(input())
bc = []
for i in range(q):
    bc.append(list(map(int,input().split())))

C_al = Counter(al)

wa = sum(al)

for i in range(q):
    dt =  bc[i][1]-bc[i][0]
    wa += C_al[bc[i][0]] * dt
    print(wa)
    C_al[bc[i][1]] += C_al[bc[i][0]]
    C_al[bc[i][0]] = 0

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 a 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 = list(map(int, input().split()))
dic = {}
for i in range(n):
  if a[i] in dic:
    dic[a[i]] += 1
  else:
    dic[a[i]] = 1
q = int(input())
total = sum(a)
for _ in range(q):
  b, c = map(int, input().split())
  if b not in dic:
    print(total)
  else:
    total += (c - b) * dic[b]
    if c in dic:
      dic[c] += dic[b]
    else:
      dic[c] = dic[b]
    dic[b] = 0
    print(total)

Solution (Python):
def solve(n, a, q, b, c):
    D = {}
    for x in a:
        if not x in D:
            D[x] = 0
        D[x] += 1
    total = sum(a)
    res = []
    for x, y in zip(b, c):
        if not x in D:
            D[x] = 0
        if not y in D:
            D[y] = 0
        diff = y*(D[x]+D[y]) - (x*D[x] + y*D[y])
        D[x], D[y] = 0, (D[x]+D[y])
        total += diff
        res.append(total)
    return "\n".join(map(str, res))

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())
print(solve(n, a, q, b, c))

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

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

for i in range(q):
    if number[b[i]] > 0:
        ans += (c[i] - b[i]) * number[b[i]]
        number[c[i]] += number[b[i]]    
        number[b[i]] = 0
    print(ans)

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

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

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
total, nums = 0, {}
for n in A:
    total += n
    if n in nums:
        nums[n] += 1
    else:
        nums[n] = 1

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

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())
g={}
ans=0
for i in map(int,input().split()):
    if g.get(i):g[i]+=1
    else:g[i]=1
    ans+=i
for i in range(int(input())):
    b,c=map(int,input().split())
    if g.get(b):
        cnt=g.pop(b)
        if g.get(c):g[c]+=cnt
        else:g[c]=cnt
        ans-=cnt*b
        ans+=cnt*c
    print(ans)

Solution (Python):


import collections


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B, C = [], []
    for q in range(Q):
        B_q, C_q = list(map(int, input().split()))
        B.append(B_q)
        C.append(C_q)

    return N, A, Q, B, C


def main(A: list, B: list, C: list) -> None:
    """
    求解処理.

    Args:\n
        A (list): N個の正整数からなる数列
        B (list): 操作における変換前の値
        C (list): 操作における変換後の値
    """
    
    sum_A = sum(A)
    counter_A = collections.Counter(A)
    for b, c in zip(B, C):
        sum_A += (c - b) * counter_A[b]
        
        print(sum_A)
        
        counter_A[c] += counter_A[b]
        counter_A[b] = 0


if __name__ == "__main__":
    
    N, A, Q, B, C = get_input()

    
    main(A, B, C)

Solution (Python):

import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
u, count = np.unique(A,return_counts=True)
L = np.zeros(10**5,int)
for i in range(len(u)):
    L[u[i]-1] = count[i]
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    asum += L[B-1] * (C-B)
    L[C-1] += L[B-1]
    L[B-1] = 0
    print(asum)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
ans = sum(A)
 
rep = dict()
for i in range(N):
    rep[A[i]] = rep.get(A[i], 0) + 1

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

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

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

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

int_in = lambda: int(stdin.readline())
arr_in = lambda: [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda: stdin.readline().strip()
out = lambda o: stdout.write("{}\n".format(o))
arr_out = lambda o: out(" ".join(map(str, o)))
bool_out = lambda o: out("YES" if o else "NO")
tests = lambda: range(1, int_in() + 1)
case_out = lambda i, o: out("Case 



def solve(n, a, q, bc):
    buckets = defaultdict(int)
    for item in a:
        buckets[item] += 1
    curr_sum = sum(a)
    result = []
    for b, c in bc:
        curr_sum -= b * buckets[b]
        curr_sum += c * buckets[b]
        buckets[c] += buckets[b]
        buckets[b] = 0
        result.append(curr_sum)
    return result


if __name__ == "__main__":
    n = int_in()
    a = arr_in()
    q = int_in()
    bc = mat_in(q)
    for row in solve(n, a, q, bc):
        out(row)

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

Solution (Python):
from collections import Counter
n = int(input())
a = [int(i) for i in input().split()]
cnt_a = Counter(a)
q = int(input())
s = [sum(a)] + [0] * q
for i in range(q):
    b, c = map(int, input().split())
    try:
        cnt_b = cnt_a.pop(b)
        cnt_a[c] += cnt_b
        s[i + 1] = s[i] + (c - b)*cnt_b
    except KeyError:
        s[i + 1] = s[i]
print(*s[1:], sep='\n')

Solution (Python):
from collections import defaultdict
n = int(input())
a = [int(i) for i in input().split()]
dict_a = defaultdict(int)
for i in range(n):
    dict_a[a[i]] += 1
q = int(input())
s = [sum(a)] + [0] * q
for i in range(q):
    b, c = map(int, input().split())
    try:
        cnt_b = dict_a.pop(b)
        dict_a[c] += cnt_b
        s[i + 1] = s[i] + (c - b)*cnt_b
    except KeyError:
        s[i + 1] = s[i]
print(*s[1:], sep='\n')

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

Solution (Python):
from collections import Counter

N = int(input())
a = list(input().split())
A = [int(a[i]) for i in range(N)]
Q = int(input())
Query = [input().split() for i in range(Q)]
SUM = sum(A)
A_count = Counter(A)
num = [A_count[i+1] for i in range(100000)]

for i in range(Q):
    B = int(Query[i][0])
    C = int(Query[i][1])
    SUM += (C-B) * num[B-1]
    print(SUM)
    num[C-1] += num[B-1]
    num[B-1] = 0

Solution (Python):
from collections import defaultdict

d = defaultdict(int)

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

operations = []

for i in range(q):
    temp = input().split(" ")
    operations.append((int(temp[0]), int(temp[1])))

for hoge in a:
    d[hoge] += 1

initial_sum = 0
for key, value in d.items():
    initial_sum += key * value

for op in operations:
    target, goto = op

    before = goto * d[goto] + target * d[target]
    d[goto] += d[target]
    d[target] = 0

    after = goto * d[goto]
    diff = after - before
    
    initial_sum += diff
    print(initial_sum)

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

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

L = [0] * 100001
for item in An:
    L[item] += 1

sum = sum(An)

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

Solution (Python):


N = int(input())
A = list(map(int, input().split()))
cnt = [0] * 100001
for i in A:
  cnt[i] += 1
Q = int(input())
S = sum(A)

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
B=[]
C=[]
for _ in range(Q):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)
dic = {}
for a in A:
    if a in dic:
        dic[a] += 1
        continue
    dic[a] = 1

ans = 0
for key,val in dic.items():
    ans += key*val

for b,c in zip(B,C):
    if b in dic:
        ans -= dic[b] * b
        ans += dic[b] * c

        if c in dic:
            dic[c] += dic[b]
        else:
            dic[c] = dic[b]
        dic[b] = 0
    print(ans)

Solution (Python):

"""
Created on Tue Jun 23 21:04:33 2020

@author: naoki
"""

N = int(input()) 
A = list(map(int,input().split())) 
Q = int(input()) 
BC = [list(map(int,input().split())) for i in range(Q)]
S = 0
num = [0 for i in range(100001)] 
for i in A:
    S += i 
    num[i]+=1 

for B,C in BC:
    S = 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())
b= [0]*q
c= [0]*q
for i in range(q):
    b[i],c[i] = map(int,input().split())

import collections
co = collections.Counter(a)

ans = sum(a)

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

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)]
suma = sum(a)
counter = Counter(a)
for b, c in bc:
    suma -= b * counter[b]
    suma += c * counter[b]
    counter[c] += counter[b]
    counter[b] = 0
    print(suma)

Solution (Python):
import collections
N=int(input())
A=list(map(int,input().split()))
S=sum(A)
D=collections.Counter(A)
Q=int(input())
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):
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)]
suma = sum(a)
counter = Counter(a)
for i in range(q):
    suma -= bc[i][0] * counter[bc[i][0]]
    suma += bc[i][1] * counter[bc[i][0]]
    counter[bc[i][1]] += counter[bc[i][0]]
    counter[bc[i][0]] = 0
    print(suma)

Solution (Python):
import collections
 
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
 
sum_ = sum(A)


counter = dict(collections.Counter(A))

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

Solution (Python):
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

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
C = collections.Counter(A)
ans = sum(A)

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

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

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

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

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

Solution (Python):
from collections import Counter

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

counter = Counter(a_list)
sum_res = sum(a_list)

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

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

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

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

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')







def solve():
    N = II()
    A = LI()
    Q = II()
    memo = {}
    sm = 0
    for a in A:
        memo[a] = memo.get(a, 0) + 1
        sm = sm + a
    
    for i in range(Q):
        b, c = MI()
        cnt = memo.get(b, 0)
        if cnt == 0:
            print(sm)
            continue
        sm = sm - b * cnt + c * cnt
        memo[b] = 0
        memo[c] = memo.get(c, 0) + cnt
        print(sm)


if __name__ == '__main__':
    solve()

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):
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
B_to_C = []
for i in range(Q):
  B_to_C.append([int(x) for x in input().split()])

dict_A = {}
sums = 0

for a in A:
  if a in dict_A:
    dict_A[a] += 1
  else:
    dict_A[a] = 1
    
for k, v in dict_A.items():
  sums += k*v
  
for B, C in B_to_C:
  if B in dict_A:
    count_of_num = dict_A[B]
    if C in dict_A:
      dict_A[C] += dict_A[B]
      del dict_A[B]
    else:
      dict_A[C] = dict_A[B]
      del dict_A[B]
      
    sums = sums - B*count_of_num + C*count_of_num
    print(sums)
    
  else:
    print(sums)

Solution (Python):
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

Solution (Python):
def Replacing():

    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):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

n=int(input().rstrip())
a=[0]*(10**5+1)
now=0
for i in input().split():
    a[int(i)]+=1
    now+=int(i)
for _ in range(int(input().rstrip())):
    b,c=map(int,input().split())
    now-=b*a[b]
    now+=c*a[b]
    print(now)
    a[b],a[c]=0,a[c]+a[b]

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

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

s = sum(a)

bc = [[0] * 2 for _ in range(q)]

for i in range(q):
    bc[i][0], bc[i][1] = map(int, input().split())

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

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

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

d = dict()

res = 0
for e in a:
    if e in d:
        d[e] += 1
    else:
        d[e] = 1
    res += e

q = int(input())

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

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

l =  [int(i) for i in input().split()]

c = Counter(l)

m = int(input())

ans = sum(l)
for i in range(m):
    x,y = [int(i) for i in input().split()]
    
    ans += (y-x) * c[x]
    print(ans)
    
    c[y] += c[x]
    c[x] = 0

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 i in range(Q)]

ans = sum(A)
l = Counter(A)
for i in range(Q):
    ans += l[b_c[i][0]] * (b_c[i][1] - b_c[i][0])
    l[b_c[i][1]] += l[b_c[i][0]]
    l[b_c[i][0]] = 0
    print(ans)

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

indexA = {}

for a in A:
  if a not in indexA:
    indexA[a] = 0
  indexA[a] += 1

sumA = sum(A)

for (b,c) in BC:
  if b in indexA:
    sumA += (c-b) * indexA[b]
    if c not in indexA:
      indexA[c] = 0
    indexA[c] += indexA[b]
    indexA[b] = 0
  print(sumA)

Solution (Python):
from collections import Counter


def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = Counter(a)
    q = int(input())
    s = sum(a)
    for _ in range(q):
        b, c = map(int, input().split())
        s -= cnt[b]*b

        s += cnt[b]*c
        cnt[c] += cnt[b]
        cnt[b] = 0
        print(s)

if __name__ == "__main__":
    main()

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

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [tuple(map(int, input().split())) for _ in range(Q)]
dic = defaultdict(int)
for a in A:
    dic[a] += 1
tot = 0
for k,v in dic.items():
    tot += k*v
ans = []
for b,c in BC:
    tmp = dic[b]
    tot -= b*tmp
    tot += c*tmp
    dic[c] += tmp
    dic[b] = 0
    ans.append(tot)
print(*ans, sep="\n")

Solution (Python):

import sys

from collections import Counter

def solve(N: int, A: "List[int]", Q: int, B: "List[int]", C: "List[int]"):
    c = Counter(A)
    s = sum(A)
    for i in range(Q):
        s += c[B[i]] * (C[i] - B[i])
        c[C[i]] += c[B[i]]
        c[B[i]] = 0
        print(s)
    return



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

if __name__ == '__main__':
    main()

Solution (Python):
import sys


from collections import defaultdict, Counter
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))





def solve():
	ans = ""
	n = int(input()); count = Counter(read())
	s = sum([i * count[i] for i in count])
	for _ in range(int(input())):
		a, b = read(); 
		s += (b-a)* count[a]
		count[b] += count[a]
		count[a] = 0
		ans += str(s)+"\n"
	print(ans)
		

if __name__ == "__main__":
	solve()

Solution (Python):
x = [0] * 100_001
value = 0

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

Solution (Python):
def resolver(N, A, Q, BC):
    total = sum(A)
    counter = dict()
    ans = []
    for a in A:
        counter[a] = counter.get(a, 0) + 1
    
    for (b, c) in BC:
        c0 = counter.pop(b) if b in counter else 0
        counter[c] = counter.get(c, 0) + c0
        total -= c0 * b
        total += c0 * c
        ans.append(total)
    
    return ans

def test():
    pass

def main():

    

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

    ans = resolver(N, A, Q, BC)

    for a in ans:
        print(a)



if __name__ == '__main__':
    main()

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

table = {}
s = 0
for i in A:
    s+=i
    if i in table: table[i]+=1
    else:table[i] = 1
        
for i in range(Q):
    B,C = map(int,input().split())
    if B in table:
        s -= table[B]*B
        if C in table: table[C]+=table[B]
        else: table[C] = table[B]
        s += table[B]*C
        table[B] = 0
    print(s)

Solution (Python):
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    ans = sum(A)
    counter = Counter(A)

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    dict = defaultdict(int)
    for a in A:
        dict[a] += 1
    answer = sum(A)
    for _ in range(Q):
        b, c = map(int, input().split())
        B = dict[b]

        answer -= b * B
        answer += c * B

        dict[b] = 0
        dict[c] += B
        print(answer)


if __name__ == "__main__":
    main()

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

for a in A:
    num[a] += 1
_sum = sum(A)

result = []
q = int(input())
for i in range(q):
    b, c = list(map(int, input().split()))
    _sum += (c - b) * num[b]
    num[c] += num[b]
    num[b] = 0
    result.append(str(_sum))
print('\n'.join(result))

Solution (Python):

l = lambda : list(map(int,input().strip().split()))
n = int(input())
a = l()
t = [0]*(10**5+1)
s = 0
for i in a:
    s += i
    t[i] += 1
q = int(input())
for _ in range(q):
    b,c = l()
    s += (c-b)*t[b]
    t[c] += t[b]
    t[b] = 0
    print(s)

Solution (Python):
from collections import Counter

n = int(input())
count = Counter(input().split())
ans = 0
for v, c in count.items():
    ans += int(v) * c
for i in range(int(input())):
    b, c = input().split()
    if b in count:
        ans -= count.get(b, 0) * int(b)
        prev = count.get(c, 0)
        count[c] = count[b] + count.get(c, 0)
        count[b] = 0
        ans += (count.get(c, 0) - prev) * int(c)
    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 i in range(Q):
  b,c=map(int,input().split())
  sum_res-=counter[b]*b
  sum_res+=counter[b]*c
  counter[c]+=counter[b]
  counter[b]=0
  print(sum_res)

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

for i in range(Q):
    dev = B[i][1]-B[i][0]
    res += numList[B[i][0]]*dev
    numList[B[i][1]] +=numList[B[i][0]]
    numList[B[i][0]] = 0
    print(res)

Solution (Python):
from collections import defaultdict
it = lambda: list(map(int, input().strip().split()))
INF = float('inf')

def solve():

    N = int(input())
    A = it()
    Q = int(input())

    S = 0
    E = defaultdict(int)
    for i, a in enumerate(A):
        E[a] += 1
        S += a

    for _ in range(Q):
        B, C = it()
        if E[B] > 0:
            S += (C - B) * E[B]
            E[C] += E[B]
            E[B] = 0
        print(S)
    

if __name__ == '__main__':
    solve()

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 i in range(Q):
  b,c=map(int,input().split())
  sum_res-=counter[b]*b
  sum_res+=counter[b]*c
  counter[c]+=counter[b]
  counter[b]=0
  print(sum_res)

Solution (Python):
t=int(input())
import math
a=list(map(int,input().strip().split()))
q=int(input())
h=[0]*(1000000+1)
sumi=0
for i in range(len(a)):
    h[a[i]]+=1
    sumi+=a[i]
 
for i in range(q):
    b,c=map(int,input().strip().split())
    
    sumi+=h[b]*(c-b)
    h[c]+=h[b]
    h[b]=0
    
    print(sumi)

Solution (Python):


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10**5+1)
for i in range(len(A)):
    
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] += nizi[B]
    nizi[B] = 0
    print(sumall)

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

    As = list(map(int, input().split()))
    nums = [0] * (10**5+1)
    ans = 0
    for a in As:
        ans += a
        nums[a] += 1

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


if __name__ == "__main__":
    main()

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

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

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

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
l=[0]*1000000
ans=sum(A)

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

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

Solution (Python):
n = int(input())
a =list(map(int,input().split()))
s =sum(a)
x = [0]*1000001
for i in a:
    x[i] += 1

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