#include <stdio.h>
#define INF 10000000

int main(void){
	int n,m;
	
	while(scanf("%d %d",&m,&n),n,m){
		int i,j;
		int a,b,cost,time,c;
		int DPc[256][256];
		int DPt[256][256];
		int k,ans[256];
		
		for(i=0;i<256;i++){
			for(j=0;j<256;j++){
				if(i==j){
					DPc[i][j]=0;
					DPt[i][j]=0;
				}
				else{
					DPc[i][j]=INF;
					DPt[i][j]=INF;
				}
			}
		}
		
		for(i=0;i<m;i++){
			scanf("%d %d %d %d", &a, &b, &cost, &time);
			DPc[a][b]=cost;
			DPc[b][a]=cost;
			DPt[a][b]=time;
			DPt[b][a]=time;
		}
		
		for(k=0;k<=n;k++){
			for(i=0;i<=n;i++){
				for(j=0;j<=n;j++){
					if(DPt[i][k]+DPt[k][j]<DPt[i][j]){
						DPt[i][j]=DPt[i][k]+DPt[k][j];
					}
					if(DPc[i][k]+DPc[k][j]<DPc[i][j]){
						DPc[i][j]=DPc[i][k]+DPc[k][j];
					}
				}
			}
		}
		
		scanf("%d", &m);
		for(i=0;i<m;i++){
			scanf("%d %d %d", &a, &b, &c);
			if(c==1){
				ans[i]=DPt[a][b];
			}
			else {
				ans[i]=DPc[a][b];
			}
		}
		for(i=0;i<m;i++){
			printf("%d\n",ans[i]);
		}
	}
	return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;
        for(int i = 0; i < n; i++){
            int pm,pe,pj;
            int heikin = 0;
            cin >> pm >> pe >> pj;
            heikin = pm + pe + pj;
            heikin /= 3;
            if(pm == 100 || pe == 100 || pj == 100) cout << "A" << endl;
            else if((pm + pe) / 2 >= 90) cout << "A" << endl;
            else if(heikin >= 80) cout << "A" << endl;
            else if(heikin >= 70) cout << "B" << endl;
            else if(heikin >= 50 && pm >= 80 || pe >= 80) cout << "B" << endl;
            else cout << "C" << endl;
        }
    }
	return 0;
}