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