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