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

#define LL long long

LL n,bil,a,b,tot=0,freq[100100],q;
int main() {
	cin>>n;
    for(int i=1;i<=n;i++){
        cin>>bil;
        freq[bil]++;
        tot+=bil;
    }
    cin>>q;
    while(q--){
        cin>>a>>b;
        tot+=(b-a)*freq[a];
        freq[b]+=freq[a];
        freq[a]=0;
        cout<<tot<<endl;
    }
}