#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using namespace std;
int main() {
  int N, Q, t, v_max, b, c;
  vector<int> map(1e5, 0);
  cin >> N;
  vector<int> idx;
  for (int i = 0; i < (N); ++i) {
    cin >> t;
    if (map[t] == 0) idx.push_back(t);
    map[t] += 1;
  }
  cin >> Q;
  ll sum;
  vector<ll> ret_list;
  for (int i = 0; i < (Q); ++i) {
    cin >> b >> c;
    if (map[b] != 0) {
      map[c] += map[b];
      map[b] = 0;
      bool find = false;
      for (int j = 0; j < idx.size(); ++j) {
        if (b == idx[j]) {
          idx[j] = 0;
        }
        if (c == idx[j]) {
          find = true;
        }
      }
      if (!find) idx.push_back(c);
    }
    sum = 0;
    for (auto elem : idx) {
      if (map[elem] != 0) {
        sum += elem * map[elem];
      }
    }
    ret_list.push_back(sum);
  }
  for (auto elem : ret_list) {
    cout << elem << endl;
  }
  return 0;
}
