#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mx = 10000007;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
#define FIO ios_base::sync_with_stdio(false);  cin.tie(NULL);

int main() {
    ll n, a, b;
    cin >> n;
    ll fre[n] = {0};
    ll s = 0;
    for (ll i = 0; i < n; i++) {
        ll x;
        cin >> x;
        fre[x]++;
        s += x;
    }
    ll q;
    cin >> q;
    while(q--) {
        cin >> a >> b;
        s -=  ((a - b)*fre[a]);
        fre[b] += fre[a];
        fre[a] = 0;
        cout << s << endl;
    }
    return 0;

}


