
//basic c++ code is given here

#include<bits/stdc++.h>

using namespace std;
    int solve(){

    return 0 ;
    }
int v[1000000]={0};
    int main (  )
    {
        int n;

        int k=0,sum=0;

        cin >> n;
        int arr[n]= {0} ;
        for(int i = 0; i< n ; i++){

            cin >> k ;
            sum+=k;
            arr[i]= k;
            v[k] ++ ;
        }

        int t;
        int y, u;
        cin >> t;
        while(t--){
                cin >> u >> y;
                v[y] += v[u] ;

               // cout << arr[u] <<" " << arr[y] << "\n";
                //cout << v[y] << " \n" ;
               // cout << sum <<endl ;
                           sum -= u*v[u];
                          // cout << sum << endl ;
                           sum += y*v[u];
            cout << sum << endl ;
             v[u] = 0;
           //for(auto x : v) if(x!=0) cout << x << " " ;
           //cout << "\n";


        }
        return 0;
    }
