from collections import Counter

n = int(input())
a = Counter([int(i) for i in input().split()])
q = int(input())
lst = [[int(i) for i in input().split()] for j in range(q)]

for i in lst:
    a[i[1]] += a[i[0]]
    del a[i[0]]
    print(sum(map(lambda item: item[0]*item[1], a.items())))