#include <bits/stdc++.h>
using namespace std;
long long solve() {
  long long n;
  cin >> n;
  map<int, int> m;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    int x;
    cin >> x;
    m[x]++;
    sum += x;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    sum += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << sum;
    cout << '\n';
  }
}
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}
