Problem:
Score : 1100 points

Problem Statement

Let f(A, B), where A and B are positive integers, be the string satisfying the following conditions:

f(A, B) has length A + B;

f(A, B) contains exactly A letters A and exactly B letters B;

The length of the longest substring of f(A, B) consisting of equal letters (ex., AAAAA or BBBB) is as small as possible under the conditions above;

f(A, B) is the lexicographically smallest string satisfying the conditions above.

For example, f(2, 3) = BABAB, and f(6, 4) = AABAABAABB.

Answer Q queries: find the substring of f(A_i, B_i) from position C_i to position D_i (1-based).

Constraints

1 \leq Q \leq 10^3

1 \leq A_i, B_i \leq 5 \times 10^8

1 \leq C_i \leq D_i \leq A_i + B_i

D_i - C_i + 1 \leq 100

All input values are integers.

Partial Score

500 points will be awarded for passing the testset satisfying 1 \leq A_i, B_i \leq 10^3.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_Q B_Q C_Q D_Q

Output

For each query i in order of input, print a line containing the substring of f(A_i, B_i) from position C_i to position D_i (1-based).

Sample Input 1

5
2 3 1 5
6 4 1 10
2 3 4 4
6 4 3 7
8 10 5 8

Sample Output 1

BABAB
AABAABAABB
A
BAABA
ABAB

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <map>
#include <queue>
#include <cstring>
#include <set>
#include <stack>
using namespace std;
typedef long long int ll;


int min_k(ll a,ll b){
	ll left=0,right=5e8;
	while(right-left>1){
		ll mid=(left+right)/2;
		if((a+1)*mid>=b&&(b+1)*mid>=a){
			right=mid;
		}else{
			left=mid;
		}
	}
	return right;
}

int cul(int a,int b,int k){
	ll left=0,right=5e8;
	while(right-left>1){
		ll mid=(left+right)/2;
		ll aa=a-k*mid;
		ll bb=b-mid;
		if(aa<0||bb<0){
			right=mid;
			continue;
		}
		if((aa+1)*k>=b&&(bb+1)*k>=aa){
			left=mid;
		}else{
			right=mid;
		}
	}
	a-=k*left;
	b-=left;
	int res=left*(k+1);
	for(int i=0;i<k;i++){
		a--;
		if((a+1)*k>=b&&(b+1)*k>=a){
			res++;
		}else{
			break;
		}
	}
	return res;
}

void query(int a,int b,int l,int r){
	int k=min_k(a,b);
	int right=cul(a,b,k);
	for(int i=l;i<=r;i++){
		if(i<=right){
			if(i%(k+1)==0){
				printf("B");
			}else{
				printf("A");
			}
		}else{
			int j=(a+b)-i+1;
			if(j%(k+1)==0){
				printf("A");
			}else{
				printf("B");
			}
		}
	}
	printf("\n");
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int q; cin >> q;
	while(q--){
		int a,b,c,d; cin >> a >> b >> c >> d;
		query(a,b,c,d);
	}
	return 0;
}