from collections import defaultdict
n=int(input())
num_dict=defaultdict(int)
a_sum=0
for a in map(int,input().split()):
    num_dict[a]+=1
    a_sum+=a
for i in range(int(input())):
    b,c=map(int,input().split())
    a_sum+=c*num_dict[c]-b*num_dict[b]
    num_dict[c]+=num_dict[b]
    num_dict[b]=0
    print(a_sum)