#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  long long int sum = 0;
  unordered_map<int, int> um;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    um[arr[i]]++;
    sum += arr[i];
  }
  int q;
  cin >> q;
  int b, c;
  while (q--) {
    cin >> b >> c;
    sum = sum + um[b] * c - um[b] * b;
    um[c] += um[b];
    um[b] = 0;
    cout << sum << endl;
  }
  return 0;
}
