#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, q;
  cin >> n;
  vector<int> a(n + 1);
  unordered_map<int, int> m;
  long long s = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    m[a[i]]++;
    s += a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int c, b;
    cin >> b >> c;
    if (m.count(b) > 0) {
      s = s - m[b] * b + m[b] * c;
      m[c] += m[b];
      m[b] = 0;
    }
    cout << s << '\n';
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
}
