#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
    int n;
    cin>>n;
    ll a[n];
    ll sum=0;
    map<ll>mp;
    for(int i=0;i<n;i++){
      cin>>a[i];
      sum+=a[i];
      mp[a[i]]++;
    }
    int q;
    cin>>q;
    while(q--){
      ll x,y;
      cin>>x>>y;
      cout<<sum-mp[x]*x+mp[x]*y<<endl;
      sum=sum-mp[x]*x+mp[x]*y;
      mp[y]+=mp[x];
      mp[x]=0;

    }


}
