#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  vector<long long> f(n, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
    f[A[i]]++;
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int b, c;
    cin >> b >> c;
    int diff = c - b;
    sum += diff * f[b];
    f[c] += f[b];
    f[b] = 0;
    cout << sum << "\n";
  }
}
