#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
vector<int> a(N);
int main() {
  int n, q, b, c;
  long long sum = 0ll;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    sum += static_cast<long long>(x);
    ++a[x];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    sum = sum - b * a[b] + c * a[b];
    a[c] += a[b];
    a[b] = 0;
    cout << sum << '\n';
  }
  return 0;
}
