def resolve():
    n=int(input())
    a=list(map(int,input().split()))
    l=[0]*(10**5)
    for i in a:
        l[i]+=1
    ans=sum(a)
    q=int(input())
    for i in range(q):
        b,c=map(int,input().split())
        x=l[b]
        ans+=(c-b)*x
        l[c]+=x
        l[b]=0
        print(ans)


if __name__ == '__main__':
    resolve()