#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q, tmp;
  cin >> n;
  map<int, int> a;
  for (int i = 0; i < n; ++i) {
    cin >> tmp;
    a[tmp] += 1;
  }
  cin >> q;
  vector<int> b(q), c(q);
  vector<long long> s(q);
  for (int i = 0; i < q; ++i) {
    cin >> b.at(i) >> c.at(i);
  }
  for (int i = 0; i < q; ++i) {
    long long sum = 0;
    if (a.count(b.at(i))) {
      a[c.at(i)] += a[b.at(i)];
      a[b.at(i)] = 0;
    }
    auto begin = a.begin(), end = a.end();
    for (auto iter = begin; iter != end; ++iter) {
      sum += iter->first * iter->second;
    }
    cout << sum << endl;
  }
}
