#include <iostream>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <climits>
#include <stdlib.h>
#include <queue>

#define mp make_pair
#define pb push_back
#define forn(n) for(long long i=0;i<n;++i)

#define ll long long
#define ull unsigned long long
#define mod 1000000007
typedef long double ld;
using namespace std;




vector<ll> sieve(ll n)
{
	vector<ll> a(n+1,0);
	for(ll i=3;i<=n;i+=2)
	a[i]=1;
	for(ll i=3;i<=n;i+=2)
	{
       if(a[i]==1)
	   {
		   for(ll j=i*2;j<=n;j+=i)
		   a[j]=0;
	   }
	}
	a[2]=1;
	a[1]=a[0]=0;
	return a;
}
ll reverse(ll n)
{
	ll s=0;
	while(n>0)
	{
       ll t=n%10;
	   s=s*10+t;
	   n/=10;
	}
	return s;
}
int count_setbits(int N)

{ int cnt=0;

while(N>0)

{

cnt+=(N&1);

N=N>>1;

}

return cnt;

}
ll power(ll x, ll y)  
{  
    int res = 1;     // Initialize result  
  
    x = x % mod; // Update x if it is more than or  
                // equal to p 
   
    if (x == 0) return 0; // In case x is divisible by p; 
  
    while (y > 0)  
    {  
        // If y is odd, multiply x with result  
        if (y & 1)  
            res = (res*x) % mod;  
  
        // y must be even now  
        y = y>>1; // y = y/2  
        x = (x*x) % mod;  
    }  
    return res;  
}  

void BellmanFord(vector< vector<ll> > &graph, int V, int E, int src) 
{ 
    // Initialize distance of all vertices as infinite. 
    int dis[V]; 
    for (int i = 0; i < V; i++) 
        dis[i] = INT_MAX; 
  
    // initialize distance of source as 0 
    dis[src] = 0; 
  
    // Relax all edges |V| - 1 times. A simple 
    // shortest path from src to any other 
    // vertex can have at-most |V| - 1 edges 
    for (int i = 0; i < V - 1; i++) { 
  
        for (int j = 0; j < E; j++) { 
            if (dis[graph[j][0]] + graph[j][2] < 
                               dis[graph[j][1]]) 
                dis[graph[j][1]] =  
                  dis[graph[j][0]] + graph[j][2]; 
        } 
    } 
  
    // check for negative-weight cycles. 
    // The above step guarantees shortest 
    // distances if graph doesn't contain 
    // negative weight cycle.  If we get a 
    // shorter path, then there is a cycle. 
    for (int i = 0; i < E; i++) { 
        int x = graph[i][0]; 
        int y = graph[i][1]; 
        int weight = graph[i][2]; 
        if (dis[x] != INT_MAX && 
                   dis[x] + weight < dis[y]) 
            cout << "Graph contains negative"
                    " weight cycle"
                 << endl; 
    } 
  
    
} 





bool vis[1000001];
int color[1000001];


vector<ll> adj[1000001];
vector<ll> edges;



void dfs(ll node)
{
    vis[node]=true;
    // for any processing of the node, like printing it, please do it here
    


    for(ll i=0;i<adj[node].size();i++)
    {
        ll child=adj[node][i];
        if(!vis[child])
        dfs(child);

    }
}

bool dfs_bipartite_check(ll node,int c)
{
    vis[node]=1;
    color[node]=c;
    for(ll i:adj[node])
    {
        if(vis[i]==0)
        {
           if(dfs_bipartite_check(i,c^1)==false)
           return false;
        }
        else
        {
            if(color[node]==color[i])
            return false;
        }
        
    }
    return true;
}

bool cycle_detect(ll node,ll parent)
{
    vis[node]=1;
    for(ll child:adj[node])
    {
        if(vis[child]==0)           // if not visited, make dfs call, and return true if dfs returns true
        if(cycle_detect(child,node))
        return true;
        else
        {
            if(child!=parent)         // check if visited child is parent of current node or ancestor of current node
            return true; // returns true if child is ancestor and not parent of current node
        }
        
    }
    return false;
}

// for graph questions having multiple test cases, dont forget to clear adjacency list and make visited array 0
/* Just add these lines in each testcases

       for(ll i=1;i<=nodes;i++)
       adj[i].clear(),vis[i]=0;

*/


int main()
{
    
   ll n;
   cin>>n;
   vector<ll> a(n);unordered_map<ll,ll> mp;
   ll sum=0;
   forn(n)
   {
       cin>>a[i];
       sum+=a[i];
       mp[a[i]]++;
   }
   ll t;
   cin>>t;
   while(t--)
   {
       ll a,b;
       cin>>a>>b;
       if(mp.find(a)!=mp.end())
       {
           ll cc=mp[a];
          
               sum+=cc*(b-a);
               mp[a]=0;
               mp[b]+=cc;
           

       }
      
       cout<<sum<<"\n";
   }
		




}


Measured memory_bytes: 31824