def sum_dic(dic):
    ans=0
    for i in dic:
        ans+=dic[i]*i
    return ans

n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
t=int(input())
dic={}
for i in arr:
    dic[i]=dic.get(i,0)+1
    

for _ in range(t):
    a,b=[int(i) for i in input().split(" ")]
    if b in dic:
        dic[b]+=dic[a]
    else:
        dic[b]=dic[a]
    dic[a]=0
    print(sum_dic(dic))