n=int(input())
a=list(map(int,input().split()))
q=int(input())
b=[]
c=[]
for _ in range(q):
    x,y=map(int,input().split())
    b.append(x)
    c.append(y)

d=[0]*10000
for i in a:
    d[i]+=1

for m,p in zip(b,c):
    minus=d[m]
    d[m]=0
    d[p]+=minus
    ans=0
    for i in range(len(d)):
        ans+=i*d[i]
    print(ans)
