#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
tree<int,int,greater<int>,rb_tree_tag,tree_order_statistics_node_update> map_t;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for(int i = 0; i < (a); ++i)
#define sz(x) (int)x.size()
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define REV(x) reverse(x.begin(),x.end())
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};

int main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    map<ll,int> freq;
    ll tot=0;
    int n;
    cin >> n;
    F0R(i,n) {
        int e;
        cin >> e;
        freq[e]++;
        tot+=e;
    }
    int tt;
    cin >> tt;
    while(tt--) {
        int b,c;
        cin >> b >> c;
        if(freq.find(b)==freq.end()) {
            cout << tot << "\n";
            continue;
        }
        tot-=freq[b]*b;
        tot+=freq[b]*c;
        freq[c]+=freq[b];
        freq.erase(b);
        cout << tot << "\n";
    }

    return 0;
}
