#include<bits/stdc++.h>
#include <math.h>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include<map>
using namespace std;
#define ll long long int
typedef unsigned long long int ull;
typedef long double ldb;
// #define B 1000000007
#define vi vector<int>
#define vll vector<long long>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define all(v) v.begin(),v.end()
#define INF 1e18
#define ani ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define PI 3.141592653589793238462643
#define BLOCK 555
//ans.resize(unique(ALL(ans)) - ans.begin());

ll smallestDivisor(ll n) 
{ 
    // if divisible by 2 
    if (n % 2 == 0) 
        return 2; 
  
    // iterate from 3 to sqrt(n) 
    for (ll i = 3; i * i <= n; i += 2) { 
        if (n % i == 0) 
            return i; 
    } 
  
    return n; 
} 
ll gcd(ll  a, ll  b) { 
   if (b == 0) 
      return a; 
   return gcd(b, a % b);  
} 
 
ll lcm(ll a, ll b){ 
    return (a*b)/(gcd(a, b));
}

 ll max(ll a, ll b){
  if (a > b)
    return a;
  return b;
}

int cnt[200005]={0};
vector<int> adj[200005];
void factorize(int n)
{
    int count=0;
    map<int,int> fact;
    while(!(n%2))
    {
        n/=2;
        count++;
    }
    if (count)
    fact[2]+=count;
    for(int i=3;i<=sqrt(n);i+=2)
    {
        count=0;
        while((n%i)==0)
        {
            count++;
            n/=i;
        }
        if (count)
        fact[i]+=count;
    }
    if(n>2)
        fact[n]++;
    // for(auto it:fact)
    // {
        // cnt[it.first]++;
        // adj[it.first].pb(it.second);
    // }
}

long long int power(long long int x, int p)
{
    if (p == 0)
        return 1;
    long long int ans = power(x, p / 2);
    ans = ans * ans;
    if (p % 2 == 1)
        ans = ans * x;
    return ans;
}
long long int factorial(long long int n){
    if(n <= 1)
        return 1;
    long long int ans = 1;
    for(ll i=1;i<=n;i++){
        ans = ans * i;
        // ans = ans % B;
    }
    return ans;
}


 
/*
ll nCr(ll n,ll k){ 
    ll C[n + 1][k + 1]; 
    ll i, j; 
    for (i=0;i<=n;i++){ 
        for(j=0;j<=min(i,k);j++){
            if(j==0||j==i) 
                C[i][j]=1;  
            else
                C[i][j]=(C[i-1][j-1]%B+C[i-1][j]%B)%B; 
        } 
    }
    return C[n][k]%B; 
}
*/
ll min(ll a, ll b){
  if (a < b)
    return a;
  return b;
}

bool isPrime(ll n) 
{ 
    // Corner cases 
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
    // This is checked so that we can skip  
    // middle five numbers in below loop 
    if (n%2 == 0 || n%3 == 0) return false; 
    for (ll i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false;   
    return true; 
} 
vector<ll> countDivisors(ll n) 
{ 
    ll cnt = 0; 
    std::vector<ll> v;
    for (ll i = 1; i <= sqrt(n); i++) 
    { 
        if (n % i == 0) 
        {  
         v.pb(i);
            // If divisors are equal, 
            // count only one 
            if (n / i == i) 
                cnt++; 
  
            else // Otherwise count both 
                {cnt = cnt + 2; 
                    v.pb(n/i);}
        } 
    } 
    return v; 
} 
ll highestPowerof2(ll n) 
{ 
   ll p = (ll)log2(n); 
    return (ll)pow(2, p);  
} 

bool isPowerOfTwo(ll x)
    {   // x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
        return (x && !(x & (x - 1)));
    }

map<ll,ll> factors(ll n)
 {
        vector<ll> f;
        map<ll,ll> m;
        for (ll x = 2; x*x <= n; x++) 
        {
           while (n%x == 0) {
            f.push_back(x);
            m[x]++;
            n /= x; }
        }
    if (n > 1) 
     {f.push_back(n);
        m[n]++;
     }
  return m;
}


ll binarySearch(ll arr[], ll l, ll r, ll x) 
{ 
    if (r >= l) { 
        ll mid = l + (r - l) / 2; 
        if (arr[mid] == x) 
            return mid;  
        if (arr[mid] > x) 
            return binarySearch(arr, l, mid - 1, x);  
        return binarySearch(arr, mid + 1, r, x); 
    } 
    return -1; 
} 
int rub[2000001];
bool ispali(int n){
    int no=n;
    int rev=0;
    while(no>0){
            int ld=no%10;
    no/=10;
        rev=rev*10+ld;
    }
    if(n==rev)
        return true;
    else
        return false;
}
void np(){
for(int i=1;i<=2000000;i++){
    if(ispali(i))
        rub[i]=rub[i-1]+1;
    else
        rub[i]=rub[i-1];
}
}

int pi[20000001];
bool isprime[20000001];
void seive(){
for(int i=4;i<=20000000;i+=2)
    isprime[i]=1;
for(int i=3;i*i<=20000000;i+=2){
    if(!isprime[i]){
        for(int j=i*i;j<=20000000;j+=i)
            isprime[j]=1;
    }
}
for(int i=2;i<=20000000;i++){
    if(!isprime[i])
    pi[i]=pi[i-1]+1;
    else
    pi[i]=pi[i-1];
}
}
int g1;
void bfs( vector<vector<int> > &g , int pos , vector<int> visited )
{
    queue <int> q;
    q.push( pos );
    while( q.size()>0 )
    {
       // cout<<q.front()<<" ";
        if(q.front()==g1){ cout<<"yes";return ;}
        int i = q.front();
        q.pop();
         visited[i]=1;
        
        for(int j = 0 ; j < g[i].size() ; j++)
        {
            if( visited[ g[i][j] ] == 0 )
            {
                q.push( g[i][j] );
                visited[g[i][j]] = 1;
            }
        }
    }
  return;   
}
void pairsort(ll a[], ll b[], ll n) 
{ 
    pair<ll, ll> pairt[n]; 
    for (ll i = 0; i < n; i++)  
    { 
        pairt[i].first = a[i]; 
        pairt[i].second = b[i]; 
    } 
    sort(pairt, pairt + n); 
    for (ll i = 0; i < n; i++)  
    { 
        a[i] = pairt[i].first; 
        b[i] = pairt[i].second; 
    } 
}
 
int main()
{
    ani;
// seive(); np();
int t=1;
// cin>>t;
while(t--)
{

    // ll n,k;cin>>n>>k;
   ll n;
   cin>>n;
   ll b[10000001]={0};
   ll a[n];
   ll s=0;
   rep(i,0,n)
{
    cin>>a[i];
    s+=a[i];
    b[a[i]]++;
}


   int q;
   cin>>q;
   while(q--)
   {
    ll b1,c1;
    cin>>b1>>c1;
    if(b[b1]>0)
    {
        int te=b[b1];
        b[b1]=0;
        b[c1]+=te;
        s-=b1*te;
        s+=c1*te;

    }
cout<<s<<endl;



   }


        













}
  return 0;
}

Measured memory_bytes: 87168