// Don't be dead serious about your life --  it's just like a program
//while(!(succeed==try()));

// Program showing a policy-based data structure. 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
using namespace __gnu_pbds; 
using namespace std; 
typedef tree<int, null_type, less<int>, rb_tree_tag, 
             tree_order_statistics_node_update> 
    new_data_set; 
//value at 3rd index in sorted array==*p.find_by_order(3) p.insert()
//index of number 6 ==p.order_of_key(6)
typedef  long long  int ll;
typedef long double ld;
//fundamentals
#define pb push_back
#define pf push_front
#define popf pop_front
#define popb pop_back
#define lb(v,val)  (lower_bound(v.begin(),v.end(),val)-v.begin())
#define ub(v,val)  (upper_bound(v.begin(),v.end(),val)-v.begin())
#define line cout<<endl;
#define sortv(v) sort(v.begin(),v.end())
#define lcm(a,b) (a*b/gcd(a,b))
#define all(v) ((v).begin(),(v).end())
#define v_at_in(n) *p.find_by_order(ll n)
//loops
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define f0 for(ll i=0;i<n;i++)
#define f1 for(ll i=1;i<=n;i++)
#define fr for(ll i=n-1;i>=0;i--)
//input of array
#define inarray ll a[n];for(ll i=0;i<n;i++)cin>>a[i];
// starter pack
#define test ll T;cin>>T;while(T--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//maximum and minimum
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define min4(a,b,c,d) min(a,min3(b,c,d))
//some values
#define PI 3.14159265
const ll MOD=1000000000+7;
const ll MOD1 = 998244353;
const ll Val=1000000000;
ll no_of_divisors(ll n){ 
 ll c = 0; 
 for (ll i = 1; i <= sqrt(n); i++) { 
  if (n % i == 0){ 
   if(n/i==i)
    c=c+1;
   else
    c=c+2;
  }
 }
 return c; 
} 

ll pwr(ll x, ll y){
 ll r=1;
 while(y>0){
  if(y&1)
   r=r*x;
  x*=x;
  y>>=1;
 }
 return r;
}
 
ll pwrm(ll x, ll y, ll m){
 x%=m;
 ll r=1;
 while(y>0){
  if(y&1)
   r=r*x%m;
  x=x*x%m;
  y>>=1;
 }
 return r;
} 
int isPrime(ll n) 
{ 
    // Corner cases
    if (n <= 1) 
        return 0; 
    if (n <= 3) 
        return 1; 
  
    if (n % 2 == 0 || n % 3 == 0) 
        return 0; 
  
    for (int i = 5; i * i <= n; i = i + 6) 
        if (n % i == 0 || n % (i + 2) == 0) 
            return 0; 
  
    return 1; 
} 
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a%b);
}
vector<ll>vprime;
vector<ll>vsqr;
void SieveOfEratosthenes(ll n) 
{   
    bool prime[n+1]; 
    memset(prime, true, sizeof(prime)); 
  
    for (ll p=2; p*p<=n; p++) 
    { 
        if (prime[p] == true) 
        { 
            for (ll i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    }
    for (ll p=2; p<=n; p++) 
    {  if (prime[p]) 
       {vprime.pb(p);vsqr.pb(p*p);}
    }
} 

// Shuru karte hai bina kisi backchodi ke_____
int main()
{
#ifdef ENABLE_FILE_IO
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
fast;
    
    {
       ll n,x;cin>>n;inarray
       sort(a,a+n);
        cin>>x;
        
            ll m[100000]={0};
            f0{
                m[a[i]]++;
            }
            ll s=0;
           f0{
                s+=a[i];
            }
        while(x--)
        {
            ll b,c;cin>>b>>c;
            //cout<<m[b]<<" "<<s;
            s=s-(m[b])*b+(m[b])*c;
            m[c]+=m[b];
            m[b]=0;
            cout<<s<<endl;
        }
    }

    return 0;
}
// End of program Logic