import numpy as np

n = int(input())

a = list(map(int, input().split()))

a_array = np.zeros((max(a)+1), dtype = np.int)
a_array = a_array.reshape((1, max(a)+1))
a_val = np.arange(max(a)+1)
a_val = a_val.reshape((1, max(a)+1))
a_val = a_val.transpose()

for i in range(len(a)):
  a_array[0, a[i]] += 1

q = int(input())

ans = np.dot(a_array, a_val)[0,0]
max_a = max(a)

for i in range(q):
  b, c = map(int, input().split())
  if b < a_array.shape[1]:
    if a_array[0, b] != 0:
      if c > max_a:
        a_array = np.append(a_array, np.zeros((1, c - max_a)), axis = 1)
        a_val = np.arange(c+1)
        a_val = a_val.reshape((1, c+1))
        a_val = a_val.transpose()
        max_a = c
      a_array[0, c] += a_array[0 ,b]
      a_array[0, b] = 0
      ans = np.dot(a_array, a_val)[0,0]
  print(int(ans))