#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define ll long long
#define ul unsigned long long

int main() {
        int N,Q,buf;
        ll count[100001]={};
        ll sum=0;
        cin >> N;
        rep(i,N){
                cin >> buf;
                count[buf]++;
                sum += buf;
        }
        cin >> Q;
        int b[Q]={};
        int c[Q]={};
        rep(i,Q) cin >> b[i] >> c[i];

        cout << "sum:" << sum << endl;

        rep(i,Q){
                cout << i << ":" <<b[i] <<"->" << c[i] << endl;
                cout << "count[b]:" << count[b[i]] << ",count[c]" << count[c[i]]<<endl;
                sum += count[b[i]]*(c[i]-b[i]);
                count[c[i]] += count[b[i]];
                count[b[i]] =0;
            cout << i<<":" <<sum << endl;
        }



}