#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, q, i, b, c, tmp, t, sum;
  cin >> n;
  vector<long long> a(n);
  vector<long long> cnt(1000000);
  vector<long long> ans;
  for (i = 0; i < n; ++i) {
    cin >> tmp;
    ++cnt.at(tmp);
    a.at(i) = tmp;
  }
  cin >> q;
  sum = accumulate(a.begin(), a.end(), 0);
  for (i = 0; i < q; ++i) {
    cin >> b >> c;
    t = (c - b) * cnt.at(b);
    cnt.at(c) += cnt.at(b);
    cnt.at(b) = 0;
    sum += t;
    ans.push_back(sum);
  }
  i = 0;
  while (true) {
    cout << ans.at(i) << endl;
    ++i;
    if (i == q) break;
  }
  return 0;
}
