#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  long long sum = 0;
  vector<int> backet(1e7, 0);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    backet[A[i]]++;
    sum += A[i];
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b, c;
    cin >> b >> c;
    sum -= backet[b] * b;
    sum += backet[b] * c;
    backet[c] += backet[b];
    backet[b] = 0;
    cout << sum << endl;
  }
}
