from collections import Counter

N=int(input())
A=Counter(list(map(int,input().split())))
Q=int(input())

import numpy as np
for _ in range(Q):
    B,C=map(int,input().split())
    A[C]+=A[B]
    A[B]=0
    print(sum(np.array(list(A.keys())) * np.array(list(A.values()))))