#include <iostream>
#include <map>
#include <cstdint>

int main() {
    uint32_t n, q;
    uint64_t s = 0;
    map<uint32_t, uint32_t> a;

    cin >> n;
    for (uint32_t i = 0; i < n; ++i) {
        uint32_t a_; cin >> a_;
        ++a[a_];
        s += a_;
    }
    cin >> q;

    while (q--) {
        uint32_t b, c;
        cin >> b >> c;

        if (auto it = a.find(b); it != a.end()) {
            s -= it->second * b;
            s += it->second * c;
            a[c] += it->second;
            a.erase(it);
        }

        cout << s << endl;
    }
}
