#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
  map<int,int> F;
    map<int,int> G;

  int N,Q;
  cin>>N;
  int A;
 vector<int> s;
  for(int i=0;i<N;i++){
  cin>>A;
 if(F[A]==0){s.push_back(A);}
F[A]++;
    G[A]=1;
  }
  cin>>Q;
  int ans;
  for(int i=0;i<Q;i++){
    int B,C;
  cin>>B>>C;
    if(F[C]==0&G[C]==0){s.push_back(C);}
    F[C]+=F[B];
    F[B]=0;
    int ans=0;
    int j=size(s);
    for(int k=0;k<j;k++){
      if(F[s[k]]!=0){
    ans+=s[k]*F[s[k]];}
    }
    cout<<ans<<endl;
  }
  
  
}