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