/*
              ██████  ██████
              ██   ██ ██   ██
 Author ->    ██████  ██   ██
              ██      ██   ██
              ██      ██████
 
 nik is love.
 nik is motivation.
 Ek modulus aapki jindagi barbaad kar sakti hai
 Watch for integer overflows.
 A dream doesn't become reality through magic; it takes sweat, determination and hard work" — Colin Powell
 
    The Feynman Algorithm
 1. Write down the problem
 2. Think really hard
 3. Write down the solution.
 */
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <functional>
#define pb                    push_back
#define ll                    long long
#define ss                       second
#define ff                        first
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define eps                   0.000000001
#define sz(a)                int((a).size())
#define all(c)               (c).begin(),(c).end()
#define present(c,x)         ((c).find(x) != (c).end())
#define cpresent(c,x)        (find(all(c),x) != (c).end())
#define pi                   pair<int,int>
#define pll                  pair<ll,ll>
#define endl                 '\n'

using namespace std;
vector<string> split(const string& s, char c) {
    vector<string> v; stringstream ss(s); string x;
    while (getline(ss, x, c)) v.emplace_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
    stringstream s; s << "[";
    for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
    s << arr[n - 1] << "]";
    return s.str();
}

#define debug(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);}

inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __evars_out_var(T val) { cerr << val; }
inline void __evars(vector<string>::iterator it) { cerr << endl; }

template<typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
    cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
    __evars_out_var(a);
    cerr << "; ";
    __evars(++it, args...);
}
//using namespace __gnu_pbds;
//typedef tree<int, null_type, int>, rb_tree_tag,tree_order_statistics_node_update> policy;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef vector< int > vi;
typedef vector< vi > vvi;
int dx[8]={1,-1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
vector<ll> val(500010,0),dis(500010),fin(500010,0),arr(500010,0);
vector<int> adj[500010];
int myrandom (int i) { return std::rand()%i;}
const int MAX=10000009;
const int inf=1e9+7;
bool cmp(pair<int,int> &p1,pair<int,int> &p2){
    return p1.ss<p2.ss;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //ifstream fin;
    //ofstream fout;
    //fin.open("input.txt");
    //fout.open("output.txt");
    //clock_t launch=clock();
    int n;
    cin>>n;
    vector<ll> v(n);
    ll sum=0;
    map<ll,ll> mt;
    rep(i,0,n){
        cin>>v[i];
        sum+=v[i];
        mt[v[i]]++;
    }
    int q;
    cin>>q;
    while(q--){
        ll b,c;
        cin>>b>>c;
        ll cnt=mt[b];
        ll delta=cnt*(c-b);
        sum+=delta;
        mt[b]=0;
        mt[c]+=cnt;
        cout<<sum<<endl;
    }
    //clog<<((long double)(clock()-launch)/CLOCKS_PER_SEC)<<"\n";
}



Measured memory_bytes: 37348