#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
	int n;
	int i,j;
	for(;;)
	{
		cin >> n ;
		if(n==0)
			break;
		int table[12][12]={0};
		
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				cin >> table[i][j] ;
			}
		}
		
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				if(j!=n)
				{
					printf("%5d",table[i][j]);
					table[i][n]+=table[i][j];
					table[n][j]+=table[i][j];
				}
				else
					printf("%5d\n",table[i][j]);
			}
		}
	}
}
#include<bits/stdc++.h>
using namespace std;

int main() {

  int n;
  while( cin >> n, n ) {
    for(int i=0; i<n; i++) {
      int pm, pe, pj;
      cin >> pm >> pe >> pj;
      int ave = (pm + pe + pj) / 3;
      
      if( pm == 100 || pe == 100 || pj == 100 ) cout << 'A' << endl;
      else if( (pm + pe) / 2 >= 90 ) cout << 'A' << endl;
      else if( ave >= 80 ) cout << 'A' << endl;
      else if( ave >= 70 ) cout << 'B' << endl;
      else if( ave >= 50 && ( pm >= 80 || pe >= 80 ) ) cout << 'B' << endl;
      else cout << 'C' << endl;
    }
  }
}