#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0)
using namespace std;

int main()
{
    fastio;
  	int n;
  	cin>>n;
  	LL a[n],sum=0;
  	  count[100000+7];
    
  memset(count,0,sizeof(count));
  for(int i=0;i<n;i++){
    cin>>a[i];
    count[a[i]]++;
    sum+=a[i];
  }
  

  int q;
  cin>>q;
  while(q--){
   
   int b,c;
   cin>>b>>c;
   if(count[b]>0){
     sum = sum-(b*count[b]+c*count[b]);
     count[c]+= count[b];
     count[b]=0;

   }
     cout<<sum<<"\n";
  }
   
}