#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  long N, Q, B, C, T, Sum=0;
  cin >> N;
  vector<long> A(100000,0)
  rep(i, N){
    cin >> T;
    A.at(T)++;
    Sum+=T;
  }
  cin >> Q;
  rep(i, Q){
    cin >> B >> C;
    Sum+=A.at(B)*(C-B);
    A.at(C)+=A.at(B);
    A.at(B)=0;
    cout << Sum << endl;
  }
}