#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int a,b,n,t,sum=0,x;
vector <int> count(100005,0) ;
cin>>n;
for(int i=0;i<n;i++)
{
cin >> x;
count[x]++;
sum += x;
}
cin >>t;
while(t--)
{
cin >> a >> b;
sum-= a*count[a];
sum+= b*count[a];
count[b]+= count[a];
count[a] = 0;
cout <<sum <<endl ;
}

return 0;
}