N = int(input())
A = list(map(int,input().split()))
Q = int(input())
L = []
for i in range(Q):
    B,C = map(int,input().split())
    L.append([B,C])
c = {}
for i in range(1,10002):
    c[i] = 0
for k in A:
    c[k] += 1
s = sum(A)
for i in range(Q):
    if c[L[i][0]] == 0:
        print(s)
    else:
        n = c[L[i][0]]
        c[L[i][1]] += n
        c[L[i][0]] = 0
        s += (L[i][1] - L[i][0])*n
        print(s)