#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){
    int n;
    // scanf("%d", &n);
    cin >> n;
    int a[n], cnt[100010];
    ll sum = 0;
    for(int i = 0;  i < n; i++){
        // scanf("%d", &a[i]);
        cin >> a[i];
        sum += a[i];
        cnt[a[i]]++;a
    }
    int q, b, c;
    // scanf("%d", &q);
    cin >> q;
    for(int i = 0; i < q; i++){
        // scanf("%d%d", &b, &c);
        cin >> b >> c;
        if(cnt[b]){
            sum += cnt[b]*(c-b);
            cnt[c] += cnt[b];
            cnt[b] = 0;
        }
        // printf("%ld\n", sum);
        cout << sum << "\n";
    }
}
