#include <bits/stdc++.h>
using namespace std;
vector<size_t> vUnique(vector<size_t> vec) {
  std::sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return vec;
}
int main() {
  size_t N, Q;
  size_t A[10000 + 1];
  vector<size_t> Alist;
  for (int i = 0; i < 10000 + 1; ++i) {
    A[i] = 0;
  }
  cin >> N;
  for (size_t i = 0; i < N; ++i) {
    size_t t;
    cin >> t;
    A[t]++;
    Alist.push_back(t);
  }
  Alist = vUnique(Alist);
  cin >> Q;
  for (size_t i = 0; i < Q; ++i) {
    size_t b, c;
    cin >> b;
    cin >> c;
    A[c] += A[b];
    A[b] = 0;
    auto result = find(Alist.begin(), Alist.end(), b);
    if (result != Alist.end()) {
      Alist.erase(result);
    }
    result = find(Alist.begin(), Alist.end(), c);
    if (result == Alist.end()) {
      Alist.push_back(c);
    }
    size_t sum = 0;
    for (size_t j = 0; j < Alist.size(); ++j) {
      sum += A[Alist[j]] * Alist[j];
    }
    cout << sum << endl;
  }
}
