#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  map<long long, long long> m;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    m[x]++;
  }
  long long q;
  cin >> q;
  while (q--) {
    long long b, c;
    cin >> b >> c;
    long long sum = 0;
    if (m[b] != 0) m[c] += m[b];
    for (auto x : m) {
      sum += (x.first) * (x.second);
    }
    cout << sum << "\n";
  }
  return 0;
}
