n=int(input())
*a,=map(int, input().split())
from collections import Counter
ca=Counter(a)
q=int(input())
sm=sum(a)
for _ in range(q):
    bi,ci=map(int, input().split())
    if ci in ca:
        if bi in ca:
            sm+=(ci-bi)*ca[ci]
            ca[ci]+=ca[bi]
            del ca[bi]
    else:
        if bi in ca:
            sm+=(ci-bi)*ca[bi]
            ca[ci]=ca[bi]
            del ca[bi]
    print(sm)
        
        