#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  unordered_map<int, int> cnt;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    cnt[arr[i]]++;
  }
  long long sm = accumulate(arr.begin(), arr.end(), 0);
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sm -= cnt[b] * b;
    sm += cnt[b] * c;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << sm << endl;
  }
}
