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