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