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