
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define ff first
#define ss second
int main() {
    ll n, ai, bi, ci, sum = 0, q;
    int arra[100002];
    memset(arra, 0, sizeof(arra));
    cin >> n;
    while(n--){
        cin >> ai;
        arra[ai]++;
        sum += ai;
    }
    cin >> q;
    while(q--){
        cin >> bi >> ci;
        sum = sum - (bi*arra[bi]) + (ci*arra[bi]);
        arra[ci] += arra[bi];
        arra[bi] = 0;
        cout << sum << '\n';
    }
    return 0;
}