import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)


'''
N = ii()
ans = ''
def keta(N):
    for i in range(1,15):
        if 26**i 
K = keta(N)
alpha = 0
for i in range(26):
    if 26**(K-1) + (26*i) + 1 <= N and N <= (26**(K-1)) + (26*(i+1)):
        alpha = i
        break
table = [chr(i) for i in range(97,123)]
alpha = table[i]
ans += (K-1) * alpha
last = (N-26**(K-1) - (26*memo) - 1)%26
last = table[last]
ans += last
print(ans)
'''

N = ii()
A = [el for el in mips()]
Q = ii()
check = dict()
for i in range(1,max(A)+1):
    check[i] = 0
for el in A:
    check[el] += 1
for i in range(Q):
    B,C = mips()
    if B in check:
        #ans -= check[B] * B
        if C in check:
            check[C] += check[B]
        else:
            check[C] = check[B]
        #ans += check[C] * C
        A.append(-(B*check[B]))
        A.append(C*check[C])
        check[B] = 0
    print(sum(A))

