#include<bits/stdc++.h>
using namespace std;

int main()
{
  int n,inp, sum = 0,b,c;
  cin>>n;
  int mp[1'00'001] {0};
  for(int i =0 ; i< n; i++)
  {
    cin>>inp;
    mp[inp]++;
    sum = sum + inp;

  }
  int q;
  cin>>q;
  for (int i =0; i<q; i++)
  {
    cin>>b>>c;
    sum += (c-b) * mp[b];
    mp[c] += mp[b];
    mp[b] = 0;
    cout<<sum<<endl;

  }
  return 0;
}