#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  ll n, q, b, c, w, ans = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) {
    cin >> w;
    a[w]++;
    ans += w;
  }
  cin >> q;
  for (int i = 0; i < (q); ++i) {
    cin >> b >> c;
    ans -= a[b] * (b - c);
    a[c] += a[b];
    a[b] = 0;
    cout << ans << endl;
  }
}
