#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main()
{
    int n, q;
    std::cin >> n;
    std::vector<std::pair<int, int>> dat;
    for (int i = 0; i < n; i++) {
        int input;
        std::cin >> input;
        auto it = std::find_if(dat.begin(), dat.end(), [input](auto&& e) { return input == e.first; });
        if (it != dat.end()) {
            it->second++;
        } else {
            dat.emplace_back(input, 1);
        }
    }

    std::cin >> q;
    long long ans = 0;
    for (int i = 0; i < q; i++) {
        long long from, to;
        std::cin >> from >> to;
        int from_i = -1, to_i = -1;

        for (int i = 0; i < dat.size(); i++) {
            auto& p = dat.at(i);
            long long add;
            if (p.first == to) {
                to_i = i;
            }
            if (p.first == from) {
                from_i = i;
                p.first = to;
            }
            add = static_cast<long long>(p.first) * static_cast<long long>(p.second);
            ans += add;
        }
        if (to_i != -1 && from_i != -1) {
            dat.at(to_i).second += dat.at(from_i).second;
            dat.erase(dat.begin() + from_i);
        }

        std::cout << ans << "\n";
        ans = 0;
    }
    return 0;
}
