n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = dict()

for i in range(n):
    if a[i] not in d.keys():
        d[a[i]] = 1
    else:
        d[a[i]] += 1

for i in range(q):
    b, c = list(map(int, input().split()))
    if b in d.keys():
        if c in d.keys():
            d[c] += d[b]
            del d[b]
        else:
            d[c] = d[b]
            del d[b]
    print(sum([k * v for k, v in d.items()]))