#include <bits/stdc++.h>

using namespace std;

struct expand_type {
    template <typename... T>
    expand_type (T&&...) {}
};
template <typename... ArgTypes>
void print (ArgTypes... args) {
    expand_type {0, (cerr << args << ", ", 0)...};
    cerr << "\b\b \n";
}
#define printv(x) cerr << "["; for(auto i : (x)) cerr << i << ", "; cerr << "\b\b]\n";
#define __lcm(a,b) a * b / __gcd(a, b)

typedef unsigned long long ull;
typedef long double ld;


int main () {
    std::ios::sync_with_stdio (false);
    cin.tie (nullptr);
    cout.tie (nullptr);
    unordered_map<int, int> m;
    long long n ,ans=0,k,a,b;
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cin >> k;
		m[k]++;
		ans += k;
	}
	cin >> q;
	while (q--)
	{
		cin >> a >> b;
		int counta = m[a];
		ans -= counta * a;
		ans += counta * b;
		cout << ans << endl;
		m.erase(a);
		m[b] += counta;
	}
    return 0;
}
