#D                 more faster method than above               see it ..........
l = lambda : list(map(int,input().split().strip()))
n = int(input())
a = l
t = [0]*(10**5+1)
s = 0
for i in a:
    s += i
    t[i] += 1
q = int(input())
for _ in range(q):
    b,c = l
    s += (c-b)*t[b]
    t[c] += t[b]
    t[b] = 0
    print(s)
