#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < (N); i++) cin >> A[i];
  int Q;
  cin >> Q;
  map<int, int> cnt;
  ll sum = 0;
  for (auto a : A) {
    cnt[a]++;
    sum += a;
  }
  int B, C;
  for (int _ = 0; _ < (Q); _++) {
    cin >> B >> C;
    if (cnt[B]) {
      sum -= cnt[B] * B;
      sum += cnt[B] * C;
      cnt[C] += cnt[B];
      cnt[B] = 0;
    }
    cout << sum << endl;
  }
}
