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: Runtime Error)

#include <bits/stdc++.h>
#define ll long long
#define mid (l+r>>1)
using namespace std;
int main()
{
	ll T;
	cin>>T;
	while (T--)
	{
		ll a,b,c,d;
		cin>>a>>b>>c>>d;
		if (a==b)
		{
			for (ll i=c;i<=d;i++)
				putchar(i&1?'A':'B');
			puts("");
			continue;
		}
		ll now=max(a,b)/(min(a,b)+1);
		if (max(a,b)%(min(a,b)+1)) now++;
		if (now==1)
		{
			if (a>b)
				for (ll i=c;i<=d;i++)
					putchar(i&1?'A':'B');
			else
				for (ll i=c;i<=d;i++)
					putchar(i&1?'B':'A');
			puts("");
			continue;
		}
		ll l=0,r=a/now,ret=0;
		while (l<=r)
		{
			ll A=a-now*mid,B=b-mid;
			if (max(B/(A+1),(A-1)/(B+1))<now) ret=mid,l=mid+1;
			else r=mid-1;
		}
		ll lim=ret;
		l=0,r=min(now,a-lim*now),ret=0;
		while (l<=r)
		{
			ll A=a-now*lim-mid,B=b-lim;
			if (max((mid?B:B-(lim==0))/(A+1),(A-1)/(B+1))<now)
				ret=mid,l=mid+1;
			else r=mid-1;
		}
		lim*=now+1;
		lim+=mid+1;
		for (ll i=c;i<lim&&i<=d;i++)
			putchar(i%(now+1)==0?'B':'A');
		for (ll i=max(lim,c);i<=d;i++)
			putchar((a+b-i+1)%(now+1)==0?'A':'B');
		puts("");
	}
	return 0;
}