#include <bits/stdc++.h>
using namespace std;
#define int       long long
#define w(x)      int x;cin >> x;while (x--)
#define rep(d,a,n)  for(int d=a;d<=n;d++)
#define rep_(d,a,n)  for(int d=a;d>=n;d--)
#define nl        "\n" 
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define oj  freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
const int inf=1e18;
int gcd(int a,int b,int temp){
    if(temp==1){
        return 1;
    }else if(a%temp==0 && b%temp==0){
        return temp;
    }
    temp-=max(a%temp,b%temp);
    return gcd(a,b,temp);
}

void fun(){
    int n;cin>>n;
    int sum=0;
    int val[100001];
  rep(l,0,100000){val[i]=0;}
    rep(i,0,n-1){
        int tem;cin>>tem;
        sum+=tem;
        val[tem]++;
    }
    int q;cin>>q;
    while(q--){
        int b,c;cin>>b>>c;
        sum+=(c)*val[b];
        sum-=b*val[b];
        val[c]+=val[b];
        val[b]=0;
        cout<<sum<<nl;
    }
}
int32_t main()
{
   
    fast_io;
    fun();
    return 0;
}
