n = int(input())
*a, = map(int, input().split())
q = int(input())
d = [0]*(10**5+1)
for i in a:
    d[i] += 1
for _ in [0]*q:
    b, c = map(int, input().split())
    d[c] += d[b]
    d[b] = 0
    print(sum(i*j for i, j in enumerate(d)))