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