#include <bits/stdc++.h>
const long long INF = 1e18;
int main() {
  int N, Q, i, j, a, b, c, min = 10001;
  long long res;
  bool flag = false;
  std::vector<int> A(10000, 0);
  std::cin >> N;
  for (i = 0; i < N; ++i) {
    std::cin >> a;
    A[a - 1]++;
    if (a < min) min = a;
  }
  std::cin >> Q;
  for (i = 0; i < Q; ++i) {
    res = 0;
    std::cin >> b >> c;
    while (A[b - 1] > 0) {
      A[b - 1]--;
      A[c - 1]++;
    }
    if (c < min)
      min = c;
    else if (A[b - 1] == 0 && b == min)
      flag = true;
    for (j = min; j < 10001; ++j) {
      if (A[j - 1] == 0) continue;
      if (flag) {
        flag = false;
        min = j;
      }
      res += (j * (A[j - 1]));
    }
    std::cout << res << std::endl;
    ;
  }
  return 0;
}
