import collections

N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())

aCount = dict(collections.Counter(A))

for _ in range(Q):
    b, c = map(int, input().split())
    aCount.setdefault(c, 0)
    aCount[c] += aCount.pop(b, 0)
    print(sum([k * v for k, v in aCount.items()]))
