#include <bits/stdc++.h>
#include<boost/multiprecision/cpp_int.hpp>
#define ll long long
const double PI=4*atan(1);
using namespace std;
using boost::multiprecision::cpp_int; 
#define ull cpp_int
#define MOD 1000000007

int findmin(int a,int b) {
    return a<b?a:b;
}
int findmax(int a,int b) {
    return a>b?a:b;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long n,q;
    cin>>n;
    long a[n];
    ull sum=0;
    unordered_map<long,long> um;
    for(long i=0;i<n;i++) {
		cin>>a[i];
		sum+=a[i];
		um[a[i]]++;
	}
	cin>>q;
	long b,c;
	while(q--) {
		cin>>b>>c;
		ull diff=um[b]*(c-b);
		um[c]+=um[b];
		sum+=diff;
		cout<<sum<<"\n";
	}
    cout.flush();
    return 0;
}
