#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int sum = 0;
  unordered_map<int, int> m;
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    sum += x;
    m[x]++;
  }
  int q;
  cin >> q;
  int i = 0;
  while (i < q) {
    int b, c;
    cin >> b >> c;
    if (m.find(b) != m.end()) {
      int count = m[b];
      sum -= count * b;
      sum += count * c;
      m[c] += count;
      m[b] = 0;
      i++;
      cout << sum << endl;
    }
  }
  return 0;
}
