#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N, A, Q, B, C;
  long long sum{};
  cin >> N;
  vector<int> v(100001,0);
  for (int i = 0; i != N; ++i) {
    cin >> A;
    ++v[A];
    sum += A;
  }
  cin >> Q;
  while(cin >> B >> C) {
    sum += (C-B)*v[B];
    v[C] += v[B];
    v[B] = 0;
    cout << sum << endl;
  }
}
