#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> vec(N + 1, 0);
  ll result = 0;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    vec[t]++;
    result += t;
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int replace_before, replace_after;
    cin >> replace_before >> replace_after;
    int replace_count = vec[replace_before];
    result = result - (replace_before * replace_count) +
             (replace_after * replace_count);
    vec[replace_after] += replace_count;
    vec[replace_before] = 0;
    cout << result << endl;
  }
  return 0;
}
