from collections import Counter
n = int(input())
ar = list(map(int, input().split()))
 
t = int(input())
c = Counter(ar)
s = sum(ar)
for i in range(t):
    a, b = list(map(int, input().split()))
    if b in c:
        c[b] += c[a]
    else:
        c[b] = c[a]
    s += ((b-a)*c[a])
    print(s)

  
  