#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  unordered_map<int, int> h;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    h[t]++;
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    long long sum = 0;
    cin >> b >> c;
    if (h[b]) {
      int t = h[b];
      h[b] = 0;
      h[c] = t;
    }
    for (auto& [key, val] : h) {
      sum += val * key;
    }
    cout << sum << endl;
  }
  return;
}
int main() {
  solve();
  return 0;
}
