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