#include<bits/stdc++.h>
using nmaespace std;
int main()
{
int n,q;
  cin>>n;
  int a[n];
  map<int,int>m;
  long long sum=0;
  for(int i=0;i<n;i++)
    cin>>a[i],m[a[i]]++,sum+=a[i];
  cin>>q;
  while(q--)
  {
    int a,b;
    cin>>a>>b;
    sum=sum-a*m[a]+m[a]*b;
    m[b]+=m[a];
    m[a]=0;
    cout<<sum<<"\n";
  }
  return 0;
  
}