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;
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    
    int q;
    cin >> q;
    for(int z = 1; z<=q; z++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        int n = a+b;
        if(a>=b){
            int k = a/(b+1);
            if(a%(b+1)!=0){
                k++;
            }
            int end = 0;
            int strk =0;
            int low = 0;
            int high = a+b;
            while(low<high){
                int mid = (low+high+1)/2;
                int as = mid/(k+1);
                int bs = mid-as;
                if(as>a || bs>b){
                    high = mid-1;
                    continue;
                }
                int ca = a-as;
                int cb = b-bs;
                int nk = ca/(cb+1);
                if(ca%(cb+1)!=0){
                    nk++;
                }
                if(k!=nk){
                    high = mid-1;
                }
                else{
                    low = mid;
                }
            }
            end = low;
            a -= low/(k+1);
            b -= low-low/(k+1);
            // strk = low%(k+1);
            // while(b>0){
            //     if(strk==k){
            //         a--;
            //         strk=0;
            //         end++;
            //     }
            //     int nk = a/(b);
            //     if(a%b!=0){
            //         nk++;
            //     }
            //     if(k!=nk){
            //         break;
            //     }
            //     b--;
            //     k = nk;
            //     end++;
            //     strk++;
            // }
            c--;
            d--;
            string str = "";
            for(int i = c; i<=d; i++){
                int rev = (n-1-i);
                if(rev<end){
                    if(rev%(k+1)==k){
                        str += "A";
                    }
                    else{
                        str += "B";
                    }
                }
                else if(i%(k+1)==k){
                    str += "B";
                }
                else{
                    str += "A";
                }
            }
            cout << str << endl;
        }
        else{
            int k = b/(a+1);
            if(b%(a+1)!=0){
                k++;
            }
            int beg = 0;
            int strk = 0;
            int low = 0;
            int high = a+b;
            while(low<high){
                int mid = (low+high+1)/2;
                int bs = mid/(k+1);
                int as = mid-bs;
                if(as>a || bs>b){
                    high = mid-1;
                    continue;
                }
                int ca = a-as;
                int cb = b-bs;
                int nk = cb/(ca+1);
                if(cb%(ca+1)!=0){
                    nk++;
                }
                if(k!=nk){
                    high = mid-1;
                    continue;
                }
                else{
                    low = mid;
                }
            }
            // strk = low%(k+1);
            beg = low;
            b-= low/(k+1);
            a-= low-low/(k+1);
            // while(a>0){
            //     if(strk==k){
            //         strk=0;
            //         b--;
            //         beg++;
            //     }
            //     int nk = b/(a);
            //     if(b%a!=0){
            //         nk++;
            //     }
            //     if(k!=nk){
            //         break;
            //     }
            //     strk++;
            //     a--;
            //     k = nk;
            //     beg++;
            // }
            
            int ext = b%k;
            if(ext==0){
                ext += k;
            }
            ext++;
            c--;
            d--;
            string str = "";
            for(int i = c; i<=d; i++){
                if(i<beg){
                    if(i%(k+1)==k){
                        str += "B";
                    }
                    else{
                        str += "A";
                    }
                    continue;
                }
                int now = i-beg;
                if(now<ext){
                    if(now==ext-1){
                        str += "A";
                    }
                    else{
                        str += "B";
                    }
                    continue;
                }
                now-=ext;
                if(now%(k+1)==k){
                    str += "A";
                }
                else{
                    str += "B";
                }
            }
            cout << str << endl;
        }
    }
    return 0;
}