#include <bits/stdc++.h>
using namespace std;
long long a[10000005], n, m, b, c, i, s;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> m;
    a[m]++;
    s += m;
  }
  cin >> b;
  while (b--) {
    cin >> m >> c;
    a[c] += a[m];
    s -= a[m] * m + a[m] * c;
    a[m] = 0;
    cout << s << endl;
  }
}
