n = int(input())
A = list(map(int, input().split()))
q = int(input())
B,C = [], []

for _ in range(q):
    b, c = list(map(int, input().split()))
    B.append(b)
    C.append(c)

d = {}
for a in A:
    d.setdefault(a, 0)
    d[a] += 1
for b, c in zip(B,C):
    d.setdefault(b, 0)
    d.setdefault(c, 0)
    d[c] += d[b]
    d[b] = 0
    ans = 0
    for i, v in d.items():
        ans += i*v
    print(ans)
