# D - Replacing

from collections import defaultdict





def main():

    N, *X = list(map(int, open(0).read().split()))

    A, BC = X[:N], X[N + 1 :]

    cnt = defaultdict(int)

    for a in A:

        cnt[a] += 1

    total = sum(A)

    res = []

    for b, c in zip(*[iter(BC)] * 2):

        total += (c - b) * cnt[b]

        res.append(total)

        cnt[c] += cnt[b]

        cnt[b] = 0

    print(("\n".join(map(str, res))))





if __name__ == "__main__":

    main()

# D - Replacing

def main():

    N, *X = list(map(int, open(0).read().split()))

    A, BC = X[:N], X[N + 1 :]

    cnt = [0] * (10 ** 5 + 1)

    for a in A:

        cnt[a] += 1

    total = sum(A)

    res = []

    for b, c in zip(*[iter(BC)] * 2):

        total += (c - b) * cnt[b]

        res.append(total)

        cnt[c] += cnt[b]

        cnt[b] = 0

    print(("\n".join(map(str, res))))





if __name__ == "__main__":

    main()

# D - Replacing
from collections import defaultdict


def main():
    N, *X = list(map(int, open(0).read().split()))
    A, BC = X[:N], X[N + 1 :]
    cnt = defaultdict(int)
    for a in A:
        cnt[a] += 1
    total = sum(A)
    res = []
    for b, c in zip(*[iter(BC)] * 2):
        total += (c - b) * cnt[b]
        res.append(total)
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(("\n".join(map(str, res))))


if __name__ == "__main__":
    main()

# D - Replacing
def main():
    N, *X = list(map(int, open(0).read().split()))
    A, BC = X[:N], X[N + 1 :]
    cnt = [0] * (10**5 + 1)
    for a in A:
        cnt[a] += 1
    total = sum(A)
    res = []
    for b, c in zip(*[iter(BC)] * 2):
        total += (c - b) * cnt[b]
        res.append(total)
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(("\n".join(map(str, res))))


if __name__ == "__main__":
    main()

-from collections import defaultdict - - -    cnt = defaultdict(int) +    cnt = [0] * (10**5 + 1)