#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    long long int numberOfElements=0;
    long long int numberOfOperations=0;
   
    cin>>numberOfElements;
    vector<long long int> arr(numberOfElements);
    
    for(int i=0; i<numberOfElements; i++){
        cin>>arr[i];
    }
    
    cin>>numberOfOperations;
    vector<vector<long long int>> op(numberOfOperations, vector<int>(2,0));
    
    for(int i=0; i<numberOfOperations; i++){
        cin>>op[i][0];
        cin>>op[i][1];
    }

    sort(arr.begin(), arr.end());

    long long int sum=0;
    
    for(int i=0; i<numberOfElements; i++){
        sum=sum+arr[i];
    }

    vector<int> auxiliary(arr[numberOfElements-1]+1, 0);
    vector<int> output;

    for(int i=0; i<numberOfElements; i++){
        auxiliary[arr[i]-1]++;
    }

    for(int i=0; i<numberOfOperations; i++){
        if(auxiliary[op[i][0]-1]){
            if(numberOfElements<op[i][1]){
                auxiliary.resize(op[i][1], 0);
            }
            sum=sum-(auxiliary[op[i][0]-1])*op[i][0]-(auxiliary[op[i][1]-1])*op[i][1]; 
            
            auxiliary[op[i][1]-1]+=auxiliary[op[i][0]-1];
            sum=sum+(auxiliary[op[i][1]-1])*op[i][1];
            output.push_back(sum);
        }
    }

    for(int i=0; i<numberOfOperations; i++){
        cout<<output[i]<<endl;
    }

    return 0;
}
