#include <iostream>

using namespace std;

int main(int argc, char** argv){
	int n, pm, pe, pj;
	
	while( 1 ){
		cin >> n;
		if(n==0) break;
		
		for(int i=0; i<n; i++){
			cin >> pm;
			cin >> pe;
			cin >> pj;
			
			if(pm==100 || pe==100 || pj==100){
				cout << "A" << endl;
				continue;
			}
			
			if((pm+pe) / 2 >= 90){
				cout << "A" << endl;
				continue;
			}
			
			int avg = (pm + pe + pj) / 3;
			if(avg >= 80){
				cout << "A" <<endl;
				continue;
			}
			
			if(avg >= 70){
				cout << "B" << endl;
				continue;
			}
			
			if(avg>=50 && pm>=80){
				cout << "B" << endl;
				continue;
			}
			
			cout << "C" << endl;
		}
	}
	
	return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    const double Inf=1e9;
    vector<pair<double,string> >vegi;
    for(int i=0;i<n;i++){
      string str;cin>>str;
      double a,b,c,d,e,f,g,h,m;
      cin>>a>>b>>c>>d>>e>>f>>g>>h>>m;
      double time=b+c+d+m*(e+f);
      int value=m*h*g-a;
      vegi.pb(mp(Inf-(value/time),str));
    }
    sort(vegi.begin(),vegi.end());
    for(int i=0;i<n;i++)
      cout<<vegi[i].S<<endl;
    cout<<"#"<<endl;
  }
  return 0;
}

