#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
typedef vector<int> vec;
typedef vector<ll> lvec;
typedef vector<char> cvec;
typedef vector<double> dvec;
typedef pair<ll, ll> LP;
typedef tuple<ll, ll, ll> LT;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
#define fs first
#define sc second
int main()	{
  int n;
  ll sum=0;
  cin>>n;
  vector<int>d(10001);
  rep(i,n){
    int a;
    cin>>a;
    d[a]++;
    sum+=a;
  }
  int q;
  cin>>q;
  rep(i,q){
    int b,c;
    cin>>b>>c;
    sum-=b*d[b];
    d[c]+=d[b];
    sum+=c*d[c];
  }
  cout<<sum<<endl;