#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector <pair<long long, unsigned int>> ryby;

void dopisanie();
void usuniecie();
void szczupak();

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >>n;
for (int j=0; j<n; j++){
    dopisanie();
} /*mamy wpisane masy początkowe*/
int q;
cin >>q;
while (q--){
short przypadek;
cin >>przypadek;
switch (przypadek){
    cout <<"s";
case 2:
    dopisanie();
    break;
case 3:
    usuniecie();
    break;
default: /*(aka case 1) jest to szczupak*/
    szczupak();
    cout <<'\n';
    break;
}
}
return 0;
}

void dopisanie(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long x;
bool czy_jest=false;
cin >>x;
for (unsigned int i=0; i<ryby.size(); i++){
    if (ryby[i].first==x){
        ryby[i].second++;
    czy_jest=true;
    }
}
if (czy_jest==false){
    ryby.push_back(make_pair(x, 1));
}
}

void usuniecie(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long x;
cin >>x;
for (int i=0; true; i++){
    if (x==ryby[i].first){
        ryby[i].second--;
        break;
    }
}
}

void szczupak(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector <pair<long long, unsigned int>> ryby1;
ryby1=ryby;
sort(ryby1.begin(), ryby1.end());
long long s, k;
cin >>s >>k;
int licznik=0;
bool wyk;
while (s<k){
    wyk=false;
for (unsigned int i=0; i<ryby1.size(); i++){
    if (ryby1[i].first>=s){
        wyk=true;
        if (i==0){
            cout <<"-1";
            return;
        }
        else {
            int g=i-1;
            while (ryby1[g].second==0){
                g--;
                if (g==-1){
                    cout <<"-1";
                    return;
                }
            }
            s+=ryby1[g].first;
            ryby1[g].second--;
            licznik++;
            break;
        }
    }
}
if (wyk==false){
    int g=ryby.size()-1;
    while (ryby1[g].second==0){
        g--;
        if (g==-1){
            cout <<"-1";
            return;
        }
    }
    s+=ryby1[g].first;
    ryby1[g].second--;
    licznik++;
}
}
cout <<licznik;
licznik=0;
}
