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;
const int maxn=5005;
const int maxm=205;
int za,zb,la,lb;

bool check(int z,int a,int b,int pat)
{
    a-=z;b-=z;
    za=a/(pat-1)+(a%(pat-1)!=0);
    zb=b/(pat-1)+(b%(pat-1)!=0);
    if(za+zb>z+1) return 1;
    return 0;

}
int main()
{
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int a,b,c,d;
        scanf("%d%d",&a,&b);
        scanf("%d%d",&c,&d);
        //c=1;
       // d=a+b;
        int pat,num;
        if(a>b){
            pat=a/(b+1)+(a%(b+1)!=0);
            num=a/pat+(a%pat!=0);
            if(b<num){
                for(int i=c;i<=d;i++)
                {
                    printf("%c",i%(pat+1)==0?'B':'A');
                }
                printf("\n");
                continue;
            }
        }
        else if(a<b) {
            pat=b/(a+1)+(b%(a+1)!=0);
            num=b/pat+(b%pat!=0);
            if(a<num){
                for(int i=c;i<=d;i++)
                {
                    printf("%c",i%(pat+1)==0?'A':'B');
                }
                printf("\n");
                continue;
            }
        }
        else{
            for(int i=c;i<=d;i++)
                printf("%c",i&1?'A':'B');
            printf("\n");
            continue;
        }
        int l=1,r=min(a,b),ans;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            int t=check(mid,a,b,pat);
            //printf("%d %d\n",mid,(int)t);
            if(t){
                l=mid+1;
            }
            else{
                ans=mid;
                r=mid-1;
            }
        }
        check(3,a,b,pat);
        int ta=a,tb=b;
        a-=ans;b-=ans;
        int tmpa=a/(pat-1)*(pat+1)+a%(pat-1)+(a%(pat-1)!=0);
        int tmpb=b/(pat-1)*(pat+1)+b%(pat-1)+(b%(pat-1)!=0);
        //printf("%d %d %d %d\n",ans,pat,tmpa,tmpb);
        for(int i=c;i<=d;i++)
        {
            char s;
            if(i<=tmpa)
                printf("%c",i%(pat+1)==0?'B':'A');
            else if((ta+tb+1-i)<=tmpb)
                printf("%c",(ta+tb+1-i)%(pat+1)==0?'A':'B');
            else
                printf("%c",((i-tmpa+(a%(pat-1)==0))&1)?'B':'A');
        }
        printf("\n");
    }
    return 0;
}