n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
for i in range(q):
  a.sort()
  su = 0
  b,c = map(int,input().split())
  for j in range(n):
    if(a[j] > b ):
      break
    if(a[j] == b):
      a[j] = c
    su += a[j]
  print(su)