#include <iostream>
#include <vector>
using namespace std;

const int N = 1e5 + 6;
vector<int> a(N);

int main() {
    int n, q, b, c;
    long long sum = 0ll;
    cin >> n;
    for (int i=0; i<n; i++) {
        int x;
        cin >> x;
        sum += long long(x);
        ++a[x];
    }
    
    cin >> q;
    while (q--) {
        cin >> b >> c;
        sum += long long((c-b) * a[b]);
        a[c] += a[b];
        a[b] = 0;
        cout << sum << '\n';
    }
    return 0;
}
