n=int(input())
a=list(map(int,input().split()))
q=int(input())

d={}

for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1

for _ in range(q):
    b,c=map(int,input().split())
    ans=0
    if c in d:
        if b in d:
            d[c]+=d[b]
    else:
        if b in d:
            d[c]=d[b]

    if b in d:
        d[b]=0

    for k,v in d.items():
        ans+=k*v

    print(ans)
