#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Q;
  map<int, int> BC;
  long long int res = 0;
  cin >> N;
  for (int i = 0; i < (int)(N); i++) {
    int tmp;
    cin >> tmp;
    BC[tmp] = BC[tmp] + 1;
  }
  cin >> Q;
  for (int i = 0; i < (int)(Q); i++) {
    int B, C;
    cin >> B >> C;
    BC[C] = BC[B] + BC[C];
    BC[B] = 0;
    auto begin = BC.begin(), end = BC.end();
    for (auto iter = begin; iter != end; iter++) {
      res += (iter->first) * (iter->second);
    }
    cout << res << endl;
    res = 0;
  }
  return 0;
}
