#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<int, int> A;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A[a]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (A.count(b)) {
      A[c] += A.at(b);
      A.erase(b);
    }
    long long sum = 0;
    for (auto p : A) {
      auto key = p.first;
      auto value = p.second;
      sum += key * value;
    }
    cout << sum << endl;
  }
}
