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

int main() {
    std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);

    int n;
    cin >> n;
    const int ms = 100100;
    long long sum = 0;
    vector<int> count(mx, 0);
    for (int i = 0; i < n; i++) {
        int x; cin >> x;
        sum += x;
        count[x]++;
    }

    int q;
    cin >> q;
    while (q--) {
        int b, c;
        cin >> b >> c;
        sum -= count[b] * b;
        sum += count[b] * c;
        count[c] += count[b];
        count[b] = 0;
        cout << sum << "\n";
    }

    
    return 0;
}