#define _CRT_SECURE_NO_WARNINGS


#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <fstream>
#include <array>
#include <map>
#include <queue>
#include <time.h>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <assert.h>
#include <bitset>
#include <unordered_set>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) (min(a,b) <= (x) && (x) <= max(a,b)) //hei
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 998244353LL 
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
 
 
#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
	std::fill((T*)array, (T*)(array + N), val);
}
 
//[a, b)
#define Getsum(ar, a,b) (ar[b] - ar[a])
#define INF 10000000000000000LL

 
struct Edge {
	int from, to;
	ll w;
	bool operator<(const Edge& rhs) const {
		return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
	}
 
};
typedef vector<vector<Edge>> Graph;
typedef unsigned long long ull;
 
 
inline ll ksum(ll n, ll k){
  return k * (2*n - k - 1);
}

pii updtp2(pii p, int nw){
  if(p.first == -1) p.first = nw;
  else if(p.second == -1) p.second = nw;
  else if(p.first >= nw) {
    p.second = p.first;
    p.first = nw;
  }
  else if(p.second >= nw) p.second = nw;
  return p;
}
 
#define isValid(y,x) (y >=0 && y < n && x >= 0 && x < m)

const pii Dir[4] = {{-1,0}, {0,1}, {1,0}, {0,-1}};

ll mod_pow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = (res * x) %mod;
		x = (x*x)%mod;
		n >>= 1;
	}
	return res;
}

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) { //ｘの入ってる集合と yの入ってる集合を併合
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { //xとyが同じ集合に入っているかどうかを判定
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	bool isroot(int x) {
		return data[x] < 0;
	}
	int size(int x) {
		return -data[root(x)];
	}
};

int main(){
    int n, q;
    cin >> n;
    vector<int> a(n);
    ll sum = 0;
    UnionFind uf(n);
    vector<int> roots(100001, -1);

    REP(i,n){
        cin >> a[i];
        sum += a[i];
        if(roots[a[i]] != -1)
            uf.unionSet(i, roots[a[i]]);
        roots[a[i]] = uf.root(i);
    }
    
    cin >> q;

    REP(i,q){
        ll b,c;
        cin >> b >> c;

        if(roots[b] != -1){
            sum += uf.size(roots[b]) * (c-b);
            if(roots[c] != -1)
                uf.unionSet(roots[b], roots[c]);
            roots[c] = uf.root(roots[b]);
            roots[b] = -1;
        }
        cout << sum << endl;
    }
	return 0;
} 

Measured memory_bytes: 4564