from sys import stdin
from sys import setrecursionlimit
from collections import Counter
setrecursionlimit(10 ** 7)

n = int(input())
a = list(map(int,stdin.readline().rstrip().split()))

q = int(input())
cc = Counter(a)

for _ in range(q):
    b,c = map(int,input().split())
    cc[c] += cc[b]
    cc[b] -= cc[b]
    su = 0
    for i,j in cc.items():
        su += i*j
    print(su)