from sys import stdin

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))
Q = int(stdin.readline())
D = dict()
S = set()
sum=0

for y in A:
    sum+=y
    s = len(S)
    S.add(y)
    if len(S)==s:
        D[y]+=1
    else:
        D[y]=1



for u in range(0,Q):
    U= list(map(int,stdin.readline().split()))
    SS=S.copy()
    SS.add(U[0])
    if len(SS)==len(S):

        SS.add(U[1])
        if len(SS)==len(S):
            sum+=(D[U[0]])*(U[1]-U[0])

            D[U[1]]+=D[U[0]]
            D[U[0]]=0
        else:
            sum += (D[U[0]]) * (U[1] - U[0])
            D[U[1]]=D[U[0]]
            D[U[0]]=0
        S=SS

    print(sum)