#include <bits/stdc++.h>
using namespace std;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
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;
}
const long long INF = 1LL << 60;
int main() {
  int n;
  cin >> n;
  map<int, int> m;
  for (long long i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (!m.count(a))
      m[a] = 1;
    else
      m[a]++;
  }
  int q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (m.count(b)) {
      if (!m.count(c)) m[c] = 0;
      m[c] += m[b];
      m.erase(b);
    }
    long long ans = 0;
    for (auto x : m) ans += x.first * x.second;
    cout << ans << endl;
  }
}
