data = {}
N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())

for a in A:
    data[a] = data.get(a, 0) + 1

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