#include <bits/stdc++.h>

using namespace std;


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(); cout.tie();
    long long y = 1e5 + 2;
    long long v[y] = {0}, n, s=0, s2=0, x;
    long long q, b, c;
    cin >> n;

    for(int i = 1; i<=n; i++ )
    {
        cin>>x;
        v[x]++;
        s += x;
    }

    cin>>q;
    for(int i = 1; i<=q; i++)
    {
        cin>>b>>c;
        v[c] += v[b];
        s2 = s - b*v[b] + c*v[b];
        s = s2;
        v[b] = 0;
        for(int j=1; j<=4; j++)
            cout<<v[j]<<" ";
        cout<<s2<<"\n";
    }

    return 0;
}
