#include<iostream>
#include<cmath>
#include<numeric>
#include<functional>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<iomanip>
#include<queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<n;i++)
#define ll long long
#define INF 2147483647
using namespace std;
using Graph=vector<vector<int>>;

int main(){
  ll n,sum=0,q; cin>>n; ll a[n];
  map<ll,ll> mp;
  rep(i,n){
    cin>>a[i]; s+=a[i];
    mp[a[i]]++;
  }
  cin>>q;
  ll b,c;
  rep(i,q){
    cin>>b>>c;
    mp[c]+=mp[b]; 
    sum+=mp[b]*(c-b);
    cout<<sum<<"\n";
    mp[b]=0;
  }
}
