#include <bits/stdc++.h>
using namespace std;
long long freq[100005], n, q, b, c, t, sum;
int main() {
    cin >> n;
    while (n--) {
        cin >> t;
        freq[t]++;
        sum+=t;
    }
    
    cin >> q;
    while (q--) {
        cin >> b >> c;
        sum+=(c-b)*freq[b];
        freq[c]+=freq[b];
        freq[b]=0;
        cout << sum << "\n";
    }
}
