N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
for i in Q:
  b,c = map(int, input().split())
  cnt = 0
  while b in A:
    A.remove(b)
    A.append(c)
    cnt += 1
  ans += (c-b) * cnt
  
print(ans)