#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> dict;
  for (int i = 0; i < N; i++) {
    int temp;
    cin >> temp;
    dict[temp]++;
  }
  int Q;
  cin >> Q;
  vector<long long> ans(Q);
  for (int i = 0; i < Q; i++) {
    int b, c;
    cin >> b >> c;
    dict[c] += dict[b];
    dict[b] = 0;
    for (auto p : dict) {
      auto key = p.first;
      auto value = p.second;
      ans.at(i) += value * key;
    }
  }
  for (auto x : ans) cout << x << endl;
  return 0;
}
