#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N), memo(10001);
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    memo.at(A.at(i))++;
    ans += A.at(i);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int B, C;
    cin >> B >> C;
    ans += memo.at(B) * (C - B);
    memo.at(C) += memo.at(B);
    memo.at(B) = 0;
    cout << ans << endl;
  }
}
