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 <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>

using namespace std;

typedef long long ll;

void Solve() {
    ll a, b, c, d, C = 0, tot;
    scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
    c--;
    tot = a + b;
    ll k = (a >= b) ? (a - 1) / (b + 1) + 1 : (b - 1) / (a + 1) + 1;
    if(k == 1) {
        if(b > a) {
            a -= c / 2;
            b -= c - c / 2;
            for(int i = c + 1; i <= d; i++) putchar(i % 2 == 0 ? 'A' : 'B');
        } else {
            b -= c / 2;
            a -= c - c / 2;
            C = c % 2 ? 1 : 0;
            for(int i = c + 1; i <= d; i++) putchar(i % 2 == 0 ? 'B' : 'A');
        }
    } else {
        ll s = max(0ll, min(a / k - 1, min(b, (a * k - k * k - b + k) / (k * k - 1) + 1)));
        if(c / (k + 1) <= s) {
            s = c / (k + 1);
            b -= s;
            a -= c - s;
            C = c % (k + 1);
            for(ll i = c + 1; i <= d; i++) {
                if(C == k || a * k < b) {
                    b--;
                    if(c < i) putchar('B');
                    C = 0;
                } else {
                    a--;
                    if(c < i) putchar('A');
                    C++;
                }
            }
        } else {
            if(d > 1e4) return;
            for(ll i = 1; i <= d; i++) {
                if(C == k || a * k < b) {
                    b--;
                    if(c < i) putchar('B');
                    C = 0;
                } else {
                    a--;
                    if(c < i) putchar('A');
                    C++;
                }
            }
            /*a -= s * k;
             b -= s;
             a -= (C = max(min(a + b - (tot - c), (a * k - b + k) / k), 0ll));
             if(a + b > tot - c && b > a * k) {
             b -= min(b - a * k, (a + b) - (tot - c));
             C = 0;
             }
             ll q = (a + b - (tot - c)) / (k + 1);
             a -= q;
             b -= q * k;
             if(q) C = 0;
             if(a + b > tot - c) {
             a--;
             C = 1;
             }
             if(a + b > tot - c) {
             b = tot - c - a;
             C = 0;
             }*/
        }
    }
    putchar('\n');
}

int main() {
    int Q;
    scanf("%d", &Q);
    while(Q--) Solve();
    return 0;
}