CodeEff/ECCO huggingface dataset:
# Original solution
n = eval(input())
a = [int(i) for i in input().split()]
q = int(eval(input()))
bb = []
cc = []
for i in range(q):
    btmp, ctmp = [int(j) for j in input().split()]
    bb.append(btmp)
    cc.append(ctmp)



atol = sum(a)
for b,c in zip(bb,cc):
    
    count = len([ i for i in a  if i == b])
    a = [ c if b == i else i for i in a ]
    atol +=  (c - b) * count
    print(atol)


# Optimized solution
n = eval(input())
a = [int(i) for i in input().split()]
q = int(eval(input()))
bb = []
cc = []
for i in range(q):
    btmp, ctmp = [int(j) for j in input().split()]
    bb.append(btmp)
    cc.append(ctmp)


amap = {}
for i in a :
    if i not in amap :
        amap[i] = 1
    else:
        amap[i] += 1

atol = sum(a)

for b,c in zip(bb,cc):
    
    if b in amap:
        atol +=  (c - b) * amap[b]
    
        if c in amap :
            amap[c] += amap[b]
        else:
            amap[c] = amap[b]
        
        amap.pop(b)
        
    print(atol)
# Original solution
from collections import defaultdict

d = defaultdict(int)

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

operations = []

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


# n = 5
# a = [1, 2, 3, 4, 5]
# q = 3

# operations = [
#     (1, 2),
#     (2, 3),
#     (3, 4)
# ]

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


for op in operations:
    target, goto = op

    d[goto] += d[target]

    try:
        del d[target]
        sum = 0
        for key, value in list(d.items()):
            sum += key * value

        print(sum)

    except:
        sum = 0
        for key, value in list(d.items()):
            sum += key * value

        print(sum)     




# Optimized solution
from collections import defaultdict

d = defaultdict(int)

n = int(eval(input()))
a = [int(i) for i in input().split(" ")]
q = int(eval(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 list(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)
# Original solution
from collections import Counter 
N = int(eval(input()))
A = Counter(list(map(int, input().split())))
Q = int(eval(input()))

for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in list(A.keys()):
        if C in list(A.keys()):
            A[C] += A[B]
        else:
            A[C] = A[B]
        A[B] = 0
    ans = 0
    for k, v in list(A.items()):
        ans += k*v
    print(ans)

# Optimized solution
from collections import Counter 
N = int(eval(input()))
a = list(map(int, input().split()))
A = Counter(a)
Q = int(eval(input()))

ans = sum(a)
for _ in range(Q):
    B, C = list(map(int, input().split()))
    if B in list(A.keys()):
        if C in list(A.keys()):
            A[C] += A[B]
        else:
            A[C] = A[B]
        ans += (C-B)*A[B]
        A[B] = 0
    print(ans)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
for i in range(100001):
    AA[i]=A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
    AA[(B[i][0])] = 0
    print(sum)


# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
Q=int(eval(input()))
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
for i in A:
    AA[i]+=1 #A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
#    AA[(B[i][0])]=A.count(int((B[i][0])))
#    A.replace(B[i][0],B[i][1])
#    AA[(B[i][1])]=A.count(int((B[i][1])))
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
    AA[(B[i][0])] = 0
    print(sum)
# Original solution
#import numpy as np
from collections import Counter
from time import time

import sys
readline = sys.stdin.buffer.readline


N = int(eval(input()))
A = list(map(int, input().split()))
#A = np.random.randint(1, 10 ** 5 + 1, 10 ** 5)

S = sum(A)
C = Counter(A)

Q = int(eval(input()))
#Q = 10 ** 5

ans = []
s = time()
for _ in range(Q):
    b, c = list(map(int, input().split()))
    #b, c = np.random.randint(1, 10 ** 5 + 1, 2)
    #b, c = 2, 5

    count = C[b]
    S = S + (c - b) * count

    C[c] += count
    C[b] = 0
    
    ans.append(S)

print(('\n'.join(map(str, ans))))
#print('time: ', time() - s)

# Optimized solution
from collections import Counter


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

S = sum(A)
C = Counter(A)

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

print(('\n'.join(map(str, ans))))
# Original solution
def _print_sum(cache):
    sum_value = 0
    for v, indices in list(cache.items()):
        sum_value += v * len(indices)
    print(sum_value)


def _main(N, A_list, Q, BC_list):
    cache = {}
    values = set()
    for i, a in enumerate(A_list):
        cache.setdefault(a, []).append(i)
        values.add(a)

    for b, c in BC_list:
        indices = cache.get(b, [])
        cache[b] = []
        cache.setdefault(c, []).extend(indices)
        _print_sum(cache)


def _entry():
    N = int(eval(input()))
    A_list = list(map(int, input().split()))
    Q = int(eval(input()))
    BC_list = []
    for i in range(Q):
        B, C = list(map(int, input().split()))
        BC_list.append((B, C))
    _main(N, A_list, Q, BC_list)


if __name__ == "__main__":
    _entry()


# Optimized solution
def _calc_sum(cache):
    sum_value = 0
    for v, indices in list(cache.items()):
        sum_value += v * len(indices)
    return sum_value


def _main(N, A_list, Q, BC_list):
    cache = {}
    for i, a in enumerate(A_list):
        cache.setdefault(a, []).append(i)

    sum_value = _calc_sum(cache)

    for b, c in BC_list:
        indices = cache.get(b, [])
        cache[b] = []
        sum_value -= b * len(indices)
        cache.setdefault(c, []).extend(indices)
        sum_value += c * len(indices)
        print(sum_value)


def _entry():
    N = int(eval(input()))
    A_list = list(map(int, input().split()))
    Q = int(eval(input()))
    BC_list = []
    for i in range(Q):
        B, C = list(map(int, input().split()))
        BC_list.append((B, C))
    _main(N, A_list, Q, BC_list)


if __name__ == "__main__":
    _entry()
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
q = int(eval(input()))
for _ in range(q):
  b, c = list(map(int, input().split()))
  if a.count(b):
    l = a.index(b)
    r = n - a[::-1].index(b)
    for i in range(l, r):
      if a[i] == b:
        a[i] = c
  print((sum(a)))
    

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))
d = dict() 
for i in a:
  if i in d:
    d[i] += 1
  else:
    d[i] = 1
s = sum(a)
for _ in range(int(eval(input()))):
  b, c = list(map(int, input().split()))
  if b in d:
    s += ((c - b) * d[b])
    if c in d:
      d[c] += d[b]
    else:
      d[c] = d[b]
    d[b] = 0
  print(s)
# Original solution
def main():
    N=int(eval(input()))
    l=[0]*10**5
    A=list(map(int, input().split()))
    for a in A:
        l[a-1]+=a
    Q=int(eval(input()))
    BC=[list(map(int,input().split())) for _ in range(Q)]
    for bc in BC:
        cnt=l[bc[0]-1]//bc[0]
        l[bc[0]-1]=0
        l[bc[1]-1]+=bc[1]*cnt
        print((sum(l)))
    return

if __name__=='__main__':
    main()


# Optimized solution
def main():
    N=int(eval(input()))
    l=[0]*10**5
    sm=0
    A=list(map(int, input().split()))
    for a in A:
        l[a-1]+=1
        sm+=a
    Q=int(eval(input()))
    BC=[list(map(int,input().split())) for _ in range(Q)]
    for bc in BC:
        cnt=l[bc[0]-1]
        l[bc[0]-1]=0
        l[bc[1]-1]+=cnt
        sm=sm-bc[0]*cnt+bc[1]*cnt
        print(sm)
    return

if __name__=='__main__':
    main()
# Original solution
from collections import Counter
n = int(eval(input()))
a = list(map(int, input().split()))
count = Counter(a)
q = int(eval(input()))
b_c = [list(map(int, input().split())) for i in range(q)]
for i in b_c:
  count[i[1]] += count[i[0]]
  count[i[0]] = 0
  ans = 0
  for k, v in list(count.items()):
    ans += k * v
  print(ans)

# Optimized solution
from collections import Counter
n = int(eval(input()))
a = [int(i) for i in input().split()]
s = sum(a)
count = Counter(a)
q = int(eval(input()))
b_c = [list(map(int, input().split())) for i in range(q)]
for i in b_c:
  s += (i[1] - i[0]) * count[i[0]]
  count[i[1]] += count[i[0]]
  count[i[0]] = 0
  print(s)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
BC = [list(map(int, input().split())) for _ in range(Q)]

index_A = { a: 0 for a in set(A) }

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

for bc in BC:
  if bc[0] in index_A:
    if bc[1] not in index_A:
      index_A[bc[1]] = 0
    index_A[bc[1]] += index_A[bc[0]]
    index_A[bc[0]] = 0
  print((sum([i[0]*i[1] for i in list(index_A.items())])))


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(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)
# Original solution
from collections import Counter

n = eval(input())
N = int(n)
A = [int(a) for a in input().split()]
q = eval(input())
Q = int(q)
B, C = [], []
for i in range(Q):
    b, c = input().split()
    B.append(int(b))
    C.append(int(c))


my_dict = Counter(A)

def cals_sum(my_dict):
    values = [ my_dict[key]*key for key in list(my_dict.keys()) if my_dict[key] != 0]
    #for key in my_dict.keys():
    #    value = my_dict[key]
    #    sum += int(key) * int(value)
    return sum(values)

for i in range(len(B)):
    b = B[i]
    c = C[i]
    tmpb = my_dict[b] 
    my_dict[b] = 0
    tmpc = my_dict[c]
    my_dict[c] = tmpb + tmpc

    print((cals_sum(my_dict)))




# Optimized solution
from collections import Counter

n = eval(input())
N = int(n)
A = [int(a) for a in input().split()]
q = eval(input())
Q = int(q)
B, C = [], []
for i in range(Q):
    b, c = input().split()
    B.append(int(b))
    C.append(int(c))


my_dict = Counter(A)

def cals_sum(my_dict):
    values = [ my_dict[key]*key for key in list(my_dict.keys()) if my_dict[key] != 0]
    #for key in my_dict.keys():
    #    value = my_dict[key]
    #    sum += int(key) * int(value)
    return sum(values)
  
my_sum = cals_sum(my_dict)

for i in range(len(B)):
    b = B[i]
    c = C[i]
    tmpb = my_dict[b] 
    my_dict[b] = 0
    tmpc = my_dict[c]
    my_dict[c] = tmpb + tmpc
    
    my_sum += ((tmpb)*c - tmpb*b)

    print(my_sum)
# Original solution
import math

input_num = int(eval(input()))
input_numline = list(map(int,input().split(' ')))
q = int(eval(input()))

op_list = []
for i in range(q):
    op_list.append(list(map(int,input().split(' '))))

maxnum = 100000

num_list = [0 for m in range(maxnum+1)]

for n in input_numline:
    num_list[n] += 1

for o in op_list:
    num_list[o[1]] += num_list[o[0]]
    num_list[o[0]] = 0

    ans = 0
    for j in range(maxnum+1):
        ans += j*num_list[j]
    print(ans)


# Optimized solution
import math

input_num = int(eval(input()))
input_numline = list(map(int,input().split(' ')))
q = int(eval(input()))

op_list = []
for i in range(q):
    op_list.append(list(map(int,input().split(' '))))

maxnum = 100000

num_list = [0 for m in range(maxnum+1)]

for n in input_numline:
    num_list[n] += 1

ans = 0

for j in range(maxnum+1):
    ans += j*num_list[j]

for o in op_list:
    ans -= o[0]*num_list[o[0]]
    ans += o[1]*num_list[o[0]]
    num_list[o[1]] += num_list[o[0]]
    num_list[o[0]] = 0

    print(ans)
# Original solution
def csum(c):
  r = 0
  for index, item in enumerate(c):
    r += index * item
  return r

n = int(eval(input()))
l= list(map(int,input().split()))
c = [0] * 100001
for i in l:
  c[i] += 1
n = int(eval(input()))
for i in range(0, n):
  be,af = list(map(int,input().split()))
  c[af] += c[be]
  c[be] = 0
  print((csum(c)))

# Optimized solution
N = int(eval(input()))
A= list(map(int,input().split()))
Q = int(eval(input()))
heavy = [0] * (10 ** 5 + 1)
for i in A:
  heavy[i] += 1
s = sum(A)
for i in range(Q):
  be,af = list(map(int,input().split()))
  s -= be * heavy[be]
  s += af * heavy[be]
  heavy[af] += heavy[be]
  heavy[be] = 0
  print(s)
# Original solution
n = int(eval(input()))
num = list(map(int, input().split()))
q = int(eval(input()))
change = [0 for i in range(q)]
for i in range(q):
    change[i] = list(map(int, input().split()))

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1

for i in range(len(change)):
    ans = 0
    if change[i][0] in dic:
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    for i, j in enumerate(dic):
        ans += j*dic[j]
    print(ans)

# Optimized solution
n = int(eval(input()))
num = list(map(int, input().split()))
q = int(eval(input()))
change = [0 for i in range(q)]
for i in range(q):
    change[i] = list(map(int, input().split()))

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
ans = 0
for i, j in enumerate(dic):
    ans += j*dic[j]

for i in range(q):
    if change[i][0] in dic:
        ans += dic[change[i][0]] * (change[i][1] - change[i][0])
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    print(ans)
# Original solution
data = {}
N = int(eval(input()))

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

Q = int(eval(input()))

for _ in range(Q):
    b, c = list(map(int, input().split()))
    if data.get(b):
        data[c] = data.get(c, 0) + data.pop(b)
    print((sum([k * v for k, v in list(data.items())])))

# Optimized solution
data = {}
total = 0
N = int(eval(input()))

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

Q = int(eval(input()))

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

# 値がiであるようなA_iの個数を格納する
val_count = [0] * (10 ** 5 + 1)

total_A = 0
for a in A:
    total_A += a
    val_count[a] += 1

S = []
for i in range(Q):
    B, C = list(map(int, input().split()))
    # cur_S = total_A
    B_count = val_count[B]
    # cur_S -= B * B_count
    # cur_S += C * B_count
    total_A -= B * B_count
    total_A += C * B_count
    # S.append(cur_S)
    S.append(total_A)

    val_count[B] = 0
    val_count[C] += B_count

for s in S:
    print(s)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

# 値がiであるようなA_iの個数を格納する
val_count = [0] * (10 ** 5 + 1)

total_A = 0
for a in A:
    total_A += a
    val_count[a] += 1

S = []
for i in range(Q):
    B, C = list(map(int, input().split()))
    B_count = val_count[B]
    total_A -= B * B_count
    total_A += C * B_count
    S.append(total_A)

    val_count[B] = 0
    val_count[C] += B_count

for s in S:
    print(s)
# Original solution
import sys
import re
input = sys.stdin.readline

if __name__ == '__main__':
  n=int(eval(input()))
  #A=list(map(int,input().strip("\n").split()))
  A=input().strip("\n")
  q=int(eval(input()))

  b,c=input().strip("\n").split()
  A=A.replace(b,c).replace(" ","+")
  ans=''
  ans+=str(eval(A))
  ans+="\n"
  for _ in range(1,q-1):
    #b,c=map(int,input().strip("\n").split())
    b,c=input().strip("\n").split()
    A=A.replace(b,c)
    ans+=str(eval(A))
    ans+="\n"
  b,c=input().strip("\n").split()
  A=A.replace(b,c)
  ans+=str(eval(A))
  print(ans)


# Optimized solution
import sys
input = sys.stdin.readline

if __name__ == '__main__':
  n=int(eval(input()))
  A_str=input().strip("\n")
  A=A_str.split()
  q=int(eval(input()))

  C={}
  for a in A:
    if a in C:
      C[a]+=1
    else:
      C[a]=1
  
  def rep(b,c):
    if c in C:
      C[c]+=C[b]
    else:
      C[c]=C[b] 
    C[b]=0
    #print(b,c,C)

  Ans=[]
  ans=''
  b,c=input().strip("\n").split()
  #tmp=eval(A_str.replace(b,c).replace(" ","+"))
  tmp=0
  hoge=list(map(int,A))
  for a in hoge:
    if str(a)==b:
      tmp+=int(c)
    else:
      tmp+=a
  #ans+=str(tmp)
  #ans+="\n"
  Ans.append(tmp)
  if b in C:
    rep(b,c)
  for i in range(1,q-1):
    b,c=input().strip("\n").split()
    if b in C:
      tmp+=(int(c)-int(b))*C[b]
      rep(b,c)
    #ans+=str(tmp)+"\n"
    Ans.append(tmp)
  b,c=input().strip("\n").split()
  if b in C:
    tmp+=(int(c)-int(b))*C[b]
  #ans+=str(tmp)
  Ans.append(tmp)
  #print(ans)
  for ans in Ans:
    print(ans)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
bc = [list(map(int,input().split())) for _ in range(q)]
N=[0]*(10**5+1)
for i in a:
    N[i]+=1
for b,c in bc:
    N[c] += N[b]
    N[b] = 0
    print((sum([i*x for i,x in enumerate(N) if x != 0])))


# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
q=int(eval(input()))
bc = [list(map(int,input().split())) for _ in range(q)]
N=[0]*(10**5+1)
for i in a:
    N[i]+=1
ans = 0
for i,x in enumerate(N):
    if x != 0:
        ans += i*x #初期値

for b,c in bc:
    ans += (c-b)*N[b]
    N[c] += N[b]
    N[b] = 0
    print(ans)
# Original solution
import collections

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
q = int(input())
ans = []
for i in range(q):
    b, c = map(int, input().split())
    tmp = ac[b]
    del ac[b]
    ac[c]+= tmp
    ans.append(sum(num*size for num, size in ac.items()))

[print(total) for total in ans]


# Optimized solution
import collections

n = int(input())
a = list(map(int, input().split()))
a_sum = sum(a)
ac = collections.Counter(a)
q = int(input())
ans = []
for i in range(q):
    b, c = map(int, input().split())
    b_num = ac[b]
    before = b * b_num
    after = c * (b_num)
    a_sum = a_sum - before + after
    ans.append(a_sum)

    tmp = ac[b]
    del ac[b]
    ac[c]+= tmp

[print(total) for total in ans]
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
sum_res = sum(A)
counter = [0 for _ in range(10 ** 5 + 1)]

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

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


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
sum_res = sum(A)
counter = [0 for _ in range(10 ** 5 + 1)]

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

for _ in range(Q):
    B, C = list(map(int, input().split()))
    sum_res += (C - B) * counter[B]
    counter[C] += counter[B]
    counter[B] = 0
    print(sum_res)
# Original solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D_fix
# CreatedDate:  2020-06-21 22:17:03 +0900
# LastModified: 2020-06-21 22:25:13 +0900
#




def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    cnt = [0]*100001
    for s in a:
        cnt[s]+=1
    q = int(eval(input()))
    for _ in range(q):
        b, c = list(map(int, input().split()))
        spare = cnt[b]
        cnt[b] = 0
        cnt[c] += spare
        print((sum(i*cnt[i] for i in range(len(cnt)))))



if __name__ == "__main__":
    main()


# Optimized solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D_fix_2
# CreatedDate:  2020-06-22 17:32:46 +0900
# LastModified: 2020-06-22 17:39:47 +0900
#


import os
import sys
#import numpy as np
#import pandas as pd


def main():
    n = int(eval(input()))
    a = list(map(int, input().split()))
    cnt = [0]*100001
    for a_ in a:
        cnt[a_] += 1

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




if __name__ == "__main__":
    main()
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

change_pair = []
for i in range(Q):
    b, c = list(map(int, input().split()))
    change_pair.append((b, c))

# Aの出現値毎に回数をカウント
counts = {}
for a in A:
    if a in counts:
        counts[a] += 1
    else:
        counts[a] = 1

# 置き換え毎に回数を移動
for b, c in change_pair:
    if b in counts:
        count = counts[b]
        counts[b] = 0
    else:
        count = 0
    if c in counts:
        counts[c] += count
    else:
        counts[c] = count

    # 総和を求める
    total = 0
    for a, count in list(counts.items()):
        total += a * count
    print(total)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))

change_pair = []
for i in range(Q):
    b, c = list(map(int, input().split()))
    change_pair.append((b, c))

# Aの出現値毎に回数をカウント
counts = {}
total = 0
for a in A:
    if a in counts:
        counts[a] += 1
    else:
        counts[a] = 1
    total += a

# 置き換え毎に回数を移動
for b, c in change_pair:
    if b in counts:
        count = counts[b]
        counts[b] = 0
        total -= b * count
    else:
        count = 0
    if c in counts:
        counts[c] += count
    else:
        counts[c] = count
    total += c * count
    print(total)
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    s = 0
    l = {i:0 for i in range(1,100001)}
    for num in A:
        l[num] += 1
        s += num



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


if __name__ == '__main__':
    main()



# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    s = 0
    l = {i:0 for i in range(1,100001)}

    A.sort()
    for num in A:
        l[num] += 1
        s += num



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


if __name__ == '__main__':
    main()
# Original solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))
    s = 0
    l = {i:0 for i in range(1,100001)}

    s = sum(A)
    for num in A:
        l[num] += 1



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


if __name__ == '__main__':
    main()



# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    Q = int(eval(input()))

    l = {i:0 for i in range(1,100001)}
    for num in A:
        l[num] += 1

    s = sum(A)

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


if __name__ == '__main__':
    main()