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 <vector> 
#include <list> 
#include <map> 
#include <set> 
#include <deque> 
#include <queue> 
#include <stack> 
#include <bitset> 
#include <algorithm> 
#include <functional> 
#include <numeric> 
#include <utility> 
#include <sstream> 
#include <iostream> 
#include <iomanip> 
#include <cstdio> 
#include <cmath> 
#include <cstdlib> 
#include <cctype> 
#include <string> 
#include <cstring> 
#include <ctime> 

using namespace std;

#define _int64 long long

pair<int,int> cnt(int l,int ll)
{
  int tmp;
  pair<int,int> ans;
  tmp=l/(ll+1);
  ans=make_pair(tmp*ll,tmp);
  ans.first+=l%(ll+1);
  return ans;
}

int main()
{
  pair<int,int> p1,p2;
  int a,b,c,d,q,tmp,mi,ma,minseg,left,right,mid,x,i,l,tmp1;
  scanf("%d",&q);
  for (l=0;l<q;l++)
  {
    scanf("%d%d%d%d",&a,&b,&c,&d);
    if (a<b)
    {
      mi=a;ma=b;
    }
    else
    {
      mi=b;ma=a;
    }
    tmp=ma/mi;
    while (1)
    {
      //cout<<tmp<<endl;
      minseg=ma/tmp;
      if (ma%tmp!=0) minseg++;
      if (mi+1<minseg) tmp++;
      else break;
    }
    for (tmp1=a+b;tmp1>=0;tmp1--)
    {
      if ((tmp1<0)||(tmp1>a+b)) continue;
      p1=cnt(tmp1,tmp);
      p2=cnt(a+b-tmp1,tmp);
      swap(p2.first,p2.second);
      if (p1.first+p2.first==a)
      {
        if (((tmp1+1)%(tmp+1)==0)&&((a+b-tmp1)%(tmp+1)==0)) continue;
        if ((tmp1%(tmp+1)==0)&&((a+b-tmp1+1)%(tmp+1)==0)) continue;
        left=tmp1;
        break;
      }
    }
      //cout<<"left:"<<left<<endl;
    for (i=c;i<=d;i++)
    {
      if (i<=left)
      {
        x=(i-1)%(tmp+1);
        if (x==tmp) printf("B");
        else printf("A");
      }
      else
      {
        //cout<<tmp<<endl;
        x=(a+b-i)%(tmp+1);
        if (x==tmp) printf("A");
        else printf("B");
      }
    }
    printf("\n");
  }
  return 0;
}