import sys
from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    A = list(in_nn())
    Q = in_n()
    t = in_na()
    BC = list(zip(t, t))

    d = defaultdict(int)
    for i in range(N):
        d[A[i]] += 1

    for i in range(Q):
        b, c = BC[i]
        d[c] += d[b]
        d.pop(b)

        ans = 0
        for k, v in d.items():
            ans += k * v
        print(ans)


if __name__ == '__main__':
    main()
