#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  int cnt[1000000];
  int64_t sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    cnt[a]++;
    sum += a;
  }
  int q, b, c;
  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> b >> c;
    sum -= b * cnt[b];
    sum -= c * cnt[c];
    cnt[c] += cnt[b];
    cnt[b] = 0;
    sum += b * cnt[b];
    sum += c * cnt[c];
    cout << sum << endl;
  }
}
