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