import sys
import collections
sys.setrecursionlimit(10**6)


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    counter = collections.Counter(a)
    q = int(input())
    for i in range(q):
        b, c = map(int, input().split())
        cnt = counter[b]
        del counter[b]
        counter[c] += cnt
        ans = 0
        for key in counter:
            ans += key * counter[key]
        print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)
