n=int(input())
a=sorted([int(i) for i in input().split()])
m={}
for i in range(n):
    x=m.get(a[i],0)
    m[a[i]]=x+1
q=int(input())
b=[]
c=[]
for i in range(q):
    b,c=[int(i) for i in input().split()]
    x=m.get(b,0)
    y=m.get(c,0)
    m[b]=0
    m[c]=x+y
    print(sum(map(lambda x: x*m[x],m.keys())))
