#include<bits/stdc++.h>

using namespace std;

int N,a[101],b[101],t[101],Q,T,fun;

vector<int> c, ff, tt;

int dp1[34*100][25002], dp2[25002];

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin>>N;
	for(int i=0;i<N;i++){
		cin>>a[i]>>b[i]>>t[i];
		if(b[i]==0) c.push_back(i);
		else {
			int k = 1;
			while (a[i]-(k-1)*(k-1)*b[i]>0){
				ff.push_back(a[i]-(k-1)*(k-1)*b[i]);
				tt.push_back(t[i]);
				k++;
//				cout << *(ff.end()-1) << endl;
			}
		}
	}


	for(int i=0;i<=25000;i++){
		for(auto j: c){
//			if (i==0) cout << t[j] << " " << a[j] << endl;
			if(i-t[j]>=0)
				dp2[i] = max(dp2[i-t[j]]+a[j],dp2[i]);
		}
	}
	
	int msf = -1;
	for (int i=0;i<=25000;i++){
		msf = dp2[i] = max(msf,dp2[i]);
//	cout << msf<< " ";} cout << endl;
}

	int NN = ff.size();
	for(int i=0;i<NN;i++){
		for(int j=0;j<=25000;j++){
			dp1[i+1][j] = dp1[i][j];
			if (j-tt[i]>=0)
				dp1[i+1][j] = max(dp1[i+1][j],dp1[i][j-tt[i]]+ff[i]);
		}
	}

	msf = -1;
	for (int i=0;i<=25000;i++){
		msf = dp1[NN][i] = max(msf,dp1[NN][i]);
//		cout << msf << " ";}cout << endl;
		}
	//cout << "TIME FOR T"<<endl;
	cin>> Q;
	for(int i=0;i<Q;i++){
		cin>>T;
		fun = -1;
		for(int j=0;j<=T;j++){
			fun = max(fun,dp1[NN][j]+dp2[T-j]);
		}
		cout<<fun<<'\n';
	}
}