#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#include <chrono> 
using namespace std::chrono;
using namespace std;
typedef long long ll;
using namespace __gnu_pbds;   
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 
#define ordered_mset tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update> 
#define fr(i,a,b) for (int i=(a); i<(b);++i)
#define frr(i,a,b) for(int i=(a-1);i>=(b);--i)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<int,int> 
#define rev(a) reverse(a.begin(),a.end())
#define srt(a) sort(a.begin(),a.end())
#define rsrt(a) sort(a.begin(),a.end(),greater<int>())
#define sz(a) (ll)a.size()
#define endl "\n"
#define po(a,b) (ll)(pow(a,b)+0.5)
#define double long double
#define print(a) for(auto x:a)cout<<x<<" ";cout<<endl;
const ll M=1e9+7;
const int MaxM=501;
ll dp[2000005];


int main()
{ios_base::sync_with_stdio(false);
 cin.tie(NULL);cout.tie(NULL);

   
   ll n;
   cin>>n;
     
    int a[n];
    map<int,int> m;
    int sum=0;
    fr(i,0,n)
    {
      cin>>a[i];
      m[a[i]]++;sum+=a[i];
    }

    int q;cin>>q;
    while(q--)
    {
       int x,y;
       cin>>x>>y;
       sum-=x*m[x];
       sum+=y*m[x];
       m[y]+=m[x];
       m.erase(x);
       cout<<sum<<endl;
    }

   }

