import sys
readline = sys.stdin.readline

from collections import defaultdict


def solve():
    N = int(input())
    As = map(int, readline().strip().split())

    d = defaultdict(int)
    total = 0
    for a in As:
        d[a] += 1
        total += a

    Q = int(input())
    queries = (map(int, readline().strip().split()) for _ in range(Q))

    for que in queries:
        b, c = que

        n_b = d[b]
        d[c] += n_b
        total += (c - b) * n_b

        print(total)


def main():
    solve()


if __name__ == "__main__":
    main()
