#include <bits/stdc++.h>
using namespace std;
int main() {
  char s;
  int n;
  long long sum = 0;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  unordered_map<int, vector<int> > m;
  for (int i = 0; i < n; i++) {
    m[arr[i]].push_back(i);
    sum += arr[i];
  }
  int q, b, c;
  cin >> q;
  while (q--) {
    cin >> b >> c;
    int len = m[b].size();
    sum = (sum - (b * len)) + (c * len);
    for (int i : m[b]) m[c].push_back(i);
    m.erase(b);
    cout << sum << endl;
  }
  return 0;
}
