#include <bits/stdc++.h>
using namespace std;
int a[100010];
int cnt[100010];
int main() {
  long long n, q, b, c, s;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    s += a[i];
  }
  cin >> q;
  while (q--) {
    cin >> b >> c;
    long long tmp = cnt[b] * (b - c);
    s = s - tmp;
    cnt[c] += cnt[b];
    cnt[b] = 0;
    cout << s << endl;
  }
  return 0;
}
