#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main () 
{
  ll n,q,x,y;cin>>n;
  ll a[n],sum=0;
  map<ll,ll>m;
  for (ll i=0;i<n;i++) {cin>>a[i];m[a[i]]++;sum+=a[i];}
  cin>>q;
  while (q--) {
      cin>>x>>y;
      sum+=(y-x)*m[x];
      m[y]+=m[x];m[x]=0;
      cout<<sum<<endl;
  }
}