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


int main()
{

int n;
cin>>n;

int freq[n+5]={0};

long long sum=0;

for(int i=0;i<n;i++)
{
 int num;
 cin>>num;
freq[num]++;
sum+=num;
}

    
int q;
cin>>q;
for(int j=1;j<=q;j++)
{

int ak,bk;
cin>>ak>>bk;

//cout<<"Freq of ak "<<freq[ak]<<"\n";

if(freq[ak]!=0)
{
sum=sum-(ak*freq[ak])-(bk*freq[bk]);
freq[bk]+=freq[ak];
freq[ak]=0;
sum=sum+bk*freq[bk];
}

cout<<sum<<endl;



}//query case ends

return 0;	
}
