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