#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int cnt[maxn] = {0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
    sum += a[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - cnt[b] * b + cnt[b] * c;
    cout << sum << '\n';
    cnt[c] = cnt[b] + cnt[c];
    cnt[b] = 0;
  }
  return 0;
}
