#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int *arr = new int[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  int q;
  cin >> q;
  int prevsum = 0;
  while (q--) {
    int b, c;
    cin >> b >> c;
    int sum = 0;
    for (int i = 0; arr[i] == b; i++) {
      arr[i] = c;
      sum += arr[i];
      prevsum = sum;
    }
    if (sum == 0) {
      cout << prevsum << endl;
    } else
      cout << sum << endl;
  }
}
