#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  long long n, ans = 0, a, b, c, q;
  cin >> n;
  unordered_map<long long, long long> mp;
  for (int i = 0; i < (n); i++) cin >> a, mp[a]++;
  cin >> q;
  for (int i = 0; i < (q); i++) {
    cin >> b >> c;
    mp[c] += mp[b];
    mp.erase(b);
    ans = 0;
    for (auto m : mp) ans += m.second * m.first;
    cout << ans << endl;
  }
  return 0;
}
