#include <bits/stdc++.h>
 
using namespace std;
 
#define sf(n) scanf("%d", &n)
#define pd(n) printf("%.10lf", n)
#define pf(n) printf("%d", n)
#define p() printf("\n")
#define ps() printf(" ")
#define ll long long
#define ull unsigned long long
#define MAX 1e9
const int mod = 1000 * 1000 * 1000 + 7;
#define ex 1e18
#define dbl 2e09
#define qu(q) queue<int> q
#define pqu(q) priority_queue<int> q
#define len(s) s.length()
#define sz(x) int(x.size())
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second
#define clr(x) x.clear()
#define LOG(x) std::cout << x << std::endl;
 
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = 3.14159265359;
 
int dx[6] = {-1,-1,0,0,1,1};
int dy[6] = {-1,0,-1,1,0,1};

const int N=2e5+5;
int a[N];
map<int,int>mp;

int main(){
    int n,q;
    cin>>n;
    ll sum=0;
    for(int i=0;i<n;++i){
        cin>>a[i];
        mp[a[i]]+=1;
        sum+=a[i];
    }
    cin>>q;
    while(q--){
        int b,c;
        cin>>b>>c;
        //cout<<b<<" "<<c<<endl;
        //cout<<mp[b]<<" "<<mp[c]<<endl;
        sum-=b*1LL*mp[b];
        sum+=c**1LL*(mp[b]);
        mp[c]+=mp[b];
        mp[b]=0;
        cout<<sum<<endl;
        //cout<<mp[b]<<" "<<mp[c]<<endl;
    }
    return 0;
}





