#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int main(void) {
  int n;
  cin >> n;
  map<int, int> mp;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    sum += a;
    mp[a]++;
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum += mp[b] * (c - b);
    mp[c] += mp[b];
    mp[b] = 0;
    cout << sum << endl;
  }
  return 0;
}
