import math

n = int(input())
aL = list(map(int, input().split(" ")))
q = int(input())
bcL = [list(map(int, input().split(" "))) for _ in range(q)]

sums = [0 for i in range(10**5 + 1)]

for a in aL:
    sums[a] += 1

for b, c in bcL:
    sums[c] += sums[b]
    sums[b] = 0
    _sum = 0
    for i, s in enumerate(sums):
        _sum += i * s
    print(_sum)