from collections import defaultdict
from collections import Counter

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

arr = defaultdict(int)

for k, v in Counter(a).items():
    arr[k] = v

q = int(input())
    
for _ in range(q):
    b, c = map(int, input().split())
    arr[c] += arr[b]
    arr[b] = 0
    print(sum([x * y for x, y in arr.items()]))