import collections as collect
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
BC=[list(map(int,input().split())) for _ in range(Q)]
mylis=collect.Counter(A)
for bc in BC:
    tmp=mylis[bc[0]]
    mylis[bc[0]]=0
    mylis[bc[1]]+=tmp
    out=0
    for k,v in mylis.items():
        out+=k*v
    print(out)