#include <bits/stdc++.h>
using namespace std;
vector<int> stree(100005);
int main() {
  int n;
  cin >> n;
  int a[n];
  unordered_map<int, int> cnt;
  long long int s = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
    cnt[a[i]]++;
  }
  int q;
  cin >> q;
  int b, c;
  while (q--) {
    cin >> b >> c;
    s -= cnt[b] * b;
    s += cnt[b] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << s << "\n";
  }
  return 0;
}
