#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 #define endl '\n'
ll b[100009];
int main()
//b[100009];
{
ll n,k,i,sum=0,x,q,B,A;
cin>>n;
for(i=0;i<n;i++){
    cin>>x;
    b[x]++;
    sum+=x;
}
cin>>q;
while(q--){
        cin>>A>>B;
    sum+=(B-A)*b[A];
    cout<<sum<<endl;
b[B]=b[A]+b[B];b[A]=0;
}
}
