#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector <int> A(100003);
  vector <int> b(100003);
  vector <int> c(100003);
  queue <int> que;

  for (int i=1; i<N+1; i++){
    int j=0;
    cin >> j;
    A.at(j)++;

    if(b.at(j)==0){
      b.at(j)==j;
    }

  }

  int Q;
  cin >> Q;

  vector <int> B(Q),C(Q);
  for(int i=0; i<Q; i++){
    cin >> B.at(i) >> C.at(i);
  }

  for(int i=0; i<Q; i++){
    A.at(C.at(i)) += A.at(B.at(i));
    A.at(B.at(i)) = 0;
    if(b.at(C.at(i))!=0){
      b.at(C.at(i))=C.at(i);
    }
    b.at(B.at(i))=0;
    c=b;
    sort(c.begin(),c.end()greater<int>());

    int sum=0;
    int k=0;
    while(c.at(k)!=0){
      sum += c.at(k)*A.at(c.at(k));
      k++;
    }

    cout << sum << endl;
  }

}
