#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  unordered_map<long long, long long> m;
  for (long long i = 0, tmp; i < n; i++) cin >> tmp, m[tmp]++;
  long long q;
  cin >> q;
  while (q--) {
    long long a, b;
    cin >> a >> b;
    m[b] += m[a];
    m[a] = 0;
    long long ans = 0;
    for (auto itr = m.begin(); itr != m.end(); itr++) {
      ans += itr->first * itr->second;
    }
    cout << ans << "\n";
  }
}
