#include "bits/stdc++.h"
#define int long long int;
using namespace std;
 
int n, q;
 
void solve(){
    cin >> n;
    int sum = 0;
    vector<int> freq(100069, 0);
    for(int i = 0, inp; i < n; ++i){
        cin >> inp;
        freq[inp]++;
        sum += inp;
    }
    cin >> q;
    while(q--){
        int b, c;
        cin >> b >> c;
        sum -= freq[b]*b;
        sum += freq[b]*c;
        freq[c] += freq[b];
        freq[b] = 0;
        cout << sum << endl;
    }
}
 
int32_t main(){
    ios_base::sync_with_stdio(false),cin.tie(nullptr);
    // int t; cin >> t;
    // while(t--)
        solve();
    return 0;
}