#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q;
  cin >> n;
  int a[n];
  map<int, int> mp;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
    sum += a[i];
  }
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum -= b * mp[b];
    mp[c] += mp[b];
    sum += c * mp[b];
    mp[b] = 0;
    cout << sum << '\n';
  }
}
