#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v;
  unordered_map<int, int> mp;
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
    mp[x]++;
    sum += x;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    if (mp[b] > 0) {
      sum = sum - mp[b] * b;
      sum = sum + mp[b] * c;
      cout << sum << endl;
      mp[c] += mp[b];
      mp.erase(b);
    }
  }
  return 0;
}
