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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int q;
int main(){
	cin>>q;
	rep(w,q){
		ll a,b,c,d;
		cin>>a>>b>>c>>d;
		ll lb = -1,ub = 1e9+100;
		while(ub-lb > 1){
			ll mid = (lb+ub)/2;
			if(a*mid+mid>=b && b*mid+mid>=a) ub = mid;
			else lb = mid;
		}
		ll cnt = ub; 
		lb=0,ub=a+b+1;
		while(ub-lb > 1){
			ll mid = (lb+ub)/2;
			ll bcnt = mid/(cnt+1);
			ll acnt = mid-bcnt;
			acnt += (a+b-mid)/(cnt+1);
			bcnt += (a+b-mid)-(a+b-mid)/(cnt+1);
			if(acnt > a) ub=mid;
			else lb = mid;
		}
		for(ll x=max(0LL,lb-334LL);x<=min(a+b,lb+334LL);x++){
			ll bcnt = x/(cnt+1);
			ll acnt = x-bcnt;
			acnt += (a+b-x)/(cnt+1);
			bcnt += (a+b-x)-(a+b-x)/(cnt+1);
			if(acnt != a || bcnt != b) continue;
			for(ll i=c;i<=d;i++){
				if(i<=x){
					if(i%(cnt+1) == 0) printf("B");
					else printf("A");
				}
				else{
					ll j = a+b+1-i;
					if(j%(cnt+1) == 0) printf("A");
					else printf("B");
				}
			}
			puts("");
			break;
		}
	}
}