#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define ll long long
#define ul unsigned long long

int main() {
        int N,Q,buf;
        ll count[100000]={};
        ll sum=0;
        cin >> N;
        rep(i,N){
                cin >> buf;
                count[buf]++;
                sum += buf;
        }
        cin >> Q;
        int b[Q]={};
        int c[Q]={};
        rep(i,Q) cin >> b[i] >> c[i];

        rep(i,Q){
                if(c[i]!=b[i]){
                        sum += count[b[i]]*(c[i]-b[i]);
                        count[c[i]] += count[b[i]];
                        count[b[i]] =0;
                }
            cout << sum << endl;
        }



}