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