N=int(input())
Ax = map(int,input().split())
Q=int(input())

t=[0]*(10**5+1)
s = 0
for a in Ax:
    t[a] += 1
    s += a

for _ in range(Q):
    b,c=map(int,input().split())
    s += t[b]*c
    s -= t[b]*b
    t[c] += t[b]
    t[b] = 0
    print(s)