#include <bits/stdc++.h>
using namespace std;
using ll=long long;
vector<int> a(100010);
ll sum;
int main(){
  int n;cin>>n;
  for(int i=0;i<n;++i){
    int x;cin>>x;
    sum+=x;
    a[x]++;
  }
  
  int q;cin>>q;
  for(int i=0;i<q;++i){
    int b,c;cin>>b>>c;
    a[c]+=a[b];
    ll sa=c*a[b]-b*a[b];
    sum+=sa;
    a[b]=0;
    cout<<sum<<endl;
  }
}
