import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
dic = collections.Counter(a)
for i in range(q):
    b, c = map(int, input().split())
    if b in dic and c not in dic:
        dic[c] = dic[b]
        del dic[b]
    elif b in dic and c in dic:
        dic[c] += dic[b]
        del dic[b]
    sum_list = [x * y for (x, y) in dic.items()]
    print(sum(sum_list))
