#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <map>
#include <cmath>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;

#define rep(i, o) for (ll i = 0; i < (o);i++)
#define rep_s(i, o) for (int i = 1; i <= (o);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<(int)(b);i++)
#define NUM 1e5

typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }


template<class T>
bool chmax(T &a, const T &b) { if (a < b) { return b; } return a; }

template<class T>
bool chmin(T &a, const T &b) { if (a > b) { a = b; return a; } return a; }

int main() {
    ll n; cin >> n;
    vector<ll> a(n), s(n+1);
    ll count_n[1000002] = {0};
    rep(i, n) {
        cin >> a[i]; count_n[a[i]]++;
        if (i != 0) s[i] = s[i-1] + a[i-1];
        cout << s[i] << endl;
    }
    s[n] = s[n-1] + a[n-1];
    // cout << s[n] << endl;
    // rep(i, n+2) cout << count_n[i] << endl;

    ll q; cin >> q;
    ll sum[q] = {0};
    ll replaced = 0;
    rep(i, q) {
        ll b, c; cin >> b >> c;
        sum[i] = s[n] + (c-b) * count_n[b];
        count_n[c] += count_n[b];
        count_n[b] -= count_n[b];
        s[n] = sum[i];
        // rep(i, n+2) cout << count_n[i] << endl;
    }

    rep(i, q) {
        cout << sum[i] << endl;
    }
    return 0;
}