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

q = int(input())

from collections import Counter

ca = Counter(a)

def sum_dict(dic):
    return sum([num*cnt for num,cnt in dic.items()])

for _ in range(q):
    b,c = list(map(int, input().split()))
    
    if b in ca:
        cnt = ca.pop(b)
        ca[c] += cnt
    
    print(sum_dict(ca))