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