from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
changes = [tuple(map(int, input().split())) for i in range(Q)]

num = defaultdict(int)
for i in A:
    num[i] += 1
 
for change in changes:
    num[change[1]] += num[change[0]]
    del num[change[0]]
    s = sum([i * c for i, c in num.items()])
    print(s)