from collections import Counter

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

a = Counter(a_list)
for i in range(Q):
    b, c = map(int, input().split())
    a[c] += a[b]
    a[b] = 0
    print(sum([x*y for x, y in a.items()]))
    #print(sum(a.elements()))