#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  vector<long long> m(10001, 0);
  long long n;
  cin >> n;
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    m[a] += 1;
    ans += a;
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long b, c;
    cin >> b >> c;
    ans += (c - b) * m[b];
    m[c] += m[b];
    m[b] = 0;
    cout << ans << endl;
  }
}
