#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  map<long long int, long long int> m;
  long long int temp;
  long long int a, b;
  long long int total = 0;
  while (n--) {
    cin >> temp;
    m[temp]++;
    total += temp;
  }
  long long int q;
  cin >> q;
  while (q--) {
    cin >> a >> b;
    if (m.find(a) != m.end()) {
      total = total - a * m[a] + b * m[a];
      m[b] += m[a];
      m.erase(m[a]);
    }
    cout << total << endl;
  }
}
