import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a.sort()
s = set(a)
c = collections.Counter(a)
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    if ca in c:
        c[ca] += c[cf]
#         del sd[cf]
        del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
#         del sd[cf]
        del c[cf]
    total = 0
    for key, val in c.items():
#        print(key, val)
        total += key * val
    print(total)


