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