#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int anums[10005] = {};
  int as[10005] = {};
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> as[i];
    anums[as[i]]++;
    sum += 1ll * as[i];
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    sum = sum - 1ll * anums[b] * b + 1ll * anums[b] * c;
    anums[c] += anums[b];
    anums[b] = 0;
    cout << sum << endl;
  }
}
