s = int(input()) 

dict={}
num_list = list(map(int, input().split()))

length=len(num_list)
count=[0]*(s+1000)
for i in range(length):
    count[num_list[i]]+=1
    dict[num_list[i]]=count[num_list[i]]
    
#print(dict)


sum=0
count=0
n = int(input()) 
for i in range(n):
    a,b = map(int, input().split())  
    if dict.get(a)!=None:
        if dict.get(b)!=None:
            count=dict[b]
            dict[b]=dict[a]+dict[b]
        else:
            dict[b]=dict[a]
        dict[a]=0
    else:
        pass
    #print(dict)
    for k, v in dict.items():
        sum+=k*v
    print(sum)
    sum=0

   
