#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rrep(i,a,b) for(int i = a ; i >= b; i--)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define sq(a) (a)*(a)
#define nl "\n"
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,
//tree_order_statistics_node_update> indexed_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef unsigned long long ull;
const int MAXN = 100005;
ll mod=1000000007;


int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  // cin>>t;
  while(t--){
      int n;
      cin>>n;
      vector<ll> a(n);
      map<ll,int> freq;
      ll sum = 0;
      rep(i,0,n-1){
        cin>>a[i];
        sum += a[i];
        freq[a[i]]++;
      }
      int q;
      cin>>q;
      while(q--){
        int b,c;
        cin>>b>>c;
        sum+= (c - b)*freq[b]
        freq[c] += freq[b];
        freq[b] = 0;
        cout<<sum<<nl;
      }
      cout<<nl;
  }
  return 0;
}
