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

int main(){

  map<int64_t,int64_t> m;
  int64_t sum=0;
  int N;
  cin>>N;
  for(int i=0;i<N;++i){
    int A;
    cin>>A;
    ++m[A];
    sum += A;
  }

  int Q;
  cin>>Q;
  for(int i=0;i<Q;++i){
    int64_t B,C;
    cin>>B>>C;
    
    sum += (C-B)*m[B];
    
    m[C] += m[B];
    m[B]=0;
    
    cout<<sum<<endl;
  }
  
}