

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i, n)         for(int i=0; i<n; i++)
#define fo(i, k, n)     for(int i=k; i<n; i++)
#define forr(i, a, b)   for(int i=a; i<=b; i++)
#define rf(i,n)         for(int i=n;i>=0;i--)
typedef unsigned long long ull;
typedef pair<int,int> pr;
#define MAX 100005
#define INF 1000000009
#define   sq(n)         (n*n)
#define p(n) cout<<n<<endl
#define pb push_back
#define py cout<<"YES"<<endl;
#define pn cout<<"NO"<<endl;
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
ll lcm ( ll a, ll b )
{
    return ( a / __gcd ( a, b ) ) * b;
}
int main()
{
    __FastIO;

    ll n,s=0;
    cin>>n;
    ll a[n+1];
    map<ll,ll>mp;
    f(i,n)
    {
        cin>>a[i];
        mp[a[i]]++;
        s+=a[i];
    }
    ll q;
    cin>>q;
    while(q--)
    {
        // vector<ll>v;
        ll b,c;
        cin>>b>>c;
        s+=mp[b]*(c-b);
        while(mp[b]--)
        {
            mp[c]++;
        }
        cout<<s<<endl;
    }
    return 0;
}
