#include <bits/stdc++.h>
using namespace std;
int n, x, d[9000003], sum = 0;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    d[x]++;
    sum += x;
  }
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    sum += (d[x] * (y - x));
    d[y] += d[x];
    d[x] = 0;
    cout << sum << '\n';
  }
}
