#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; 

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    long long s = 0, q;
    map<long long, long long>mp;
    vector<long long>v(n);
    for(auto &x:v)
    {
        cin>>x;
        s+=x;
        mp[x]++;
    }
    cin>>q;
    while(q--)
    {
        long long b,c;
        cin>>b>>c;
        s-=mp[b]*b;
        s+=mp[b]*c;
        mp[c]+=mp[b];
        cout<<s<<'\n';
    }

    return 0;
}