#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int f[2000000];
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    f[a[i]]++;
    ans += a[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int b, c;
    cin >> b >> c;
    ans -= b * f[b];
    ans += c * f[b];
    f[c] += f[b];
    f[b] = 0;
    cout << ans << endl;
    ;
  }
}
