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 <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 2001;

string s;
int X[N];
bool vis[N];

int main() {
  int q;
  S(q);
  while(q--) {
    memset(X, 0, sizeof(X));
    int a,b,c,d;
    S2(a,b);
    S2(c,d);
    int idx = 0;
    if(a == b) {
      rep(i,c,d+1) {
        if(i&1) printf("A");
        else printf("B");
      }
      printf("\n");
      continue;
    }
    if(a > b) {
      int x = (a + b) / (b + 1);
      int req = (a + x - 1) / x;
      rep(i,0,req) {
        X[idx] = 1;
        vis[idx] = true;
        a--; idx++;
        if(i + 1 != req) {
          X[idx] = 1;
          vis[idx] = false;
          b--;idx++;
        }
      }
      if(b) {
        b--;
        X[idx] = 1;
        vis[idx] = false;
        idx++;
      }
      x--;
      for(int i = idx - 1; i >= 0; i--) if(b && vis[i] == false) {
        X[i] += min(b, x);
        b -= min(b, x);
      }
      for(int i = 0; i < idx; i++) if(a && vis[i] == true) {
        X[i] += min(a, x);
        a -= min(a, x);
      }
    } else {
      int x = (a + b) / (b + 1);
      int req = (b + x - 1) / x;
      rep(i,0,req) {
        X[idx] = 1;
        vis[idx] = false;
        b--; idx++;
        if(i + 1 != req) {
          X[idx] = 1;
          vis[idx] = true;
          a--;idx++;
        }
      }
      x--;
      for(int i = idx - 1; i >= 0; i--) if(b && vis[i] == false) {
        X[i] += min(b, x);
        b -= min(b, x);
      }
      for(int i = 0; i < idx; i++) if(a && vis[i] == true) {
        X[i] += min(a, x);
        a -= min(a, x);
      }
    }
    s.clear();
    int tmp = 0;
    rep(i,0,idx) {
      while(X[i]) {
        X[i]--;
        tmp++;
        if(tmp >= c && tmp <= d) {
          if(vis[i]) printf("A");
          else printf("B");
        }
      }
    }
    printf("\n");
  }
  return 0;
}