n=int(input())
s=""
l=list(map(int,input().split()))
q=int(input())
d={}
for i in l:
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
    
for i in range(q):
    j,k=map(int,input().split())
    if k in d:
        d[k]+=d[j]
    else:
        d[k]=d[j]
    t=0
    d[j]=0
    for val,key in d.items():
        t+=val*key
    print(t)