

import collections

n = int(input())
a = list(map(int, input().split(" ")))
q = int(input())
have_map = collections.Counter(a)
for i in range(q):
    x, y = map(int, input().split(" "))
    have_map[y] = have_map.get(x, 0) + have_map.get(y, 0)
    have_map.pop(x, 0)

    cost = 0
    for k,v in have_map.items():
        cost += k * v
    print(cost)
