Language: C++

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL

template<class T, class U>
ostream& operator<<(ostream& os, pair<T, U> p) {
	os << "(" << p.first << ", " << p.second << ")";
	return os;
}

template<template<class, class...> class C, class... A>
typename enable_if<!is_same<C<A...>, string>::value, ostream&>::type
operator<<(ostream& os, class C<A...> c) {
	auto i = c.begin();
	while (i != c.end()) {
		os << " {"[i == c.begin()] << *i;
		os << ",}"[++i == c.end()];
	}
	return os;
}

#define debug(x) cerr << #x << " = " << x << endl;
#else
#define debug(...) {}
#endif

const int N = 1e6;
long long infLL = 1e18;
int inf = 1e9;

void rowny(int a, int b) {
	string ans;
	for (int i = a; i <= b; i ++) {
		ans += (i % 2 ? 'A' : 'B');
	}
	cout << ans;
}

void wypisz(vector<pair<pair<int,int>,int>> v, int c, int d) {
	string ans;
	debug(v);
	int id = 0;
	for (auto q : v) {
		int a = q.first.first;
		int b = q.first.second;
		int r = q.second;
		if (id >= d) {
			cout << ans;
			return;
		}
		int nid = id + r * (a + b);
		if (nid >= c) {
// 			debug(q);
// 			debug(c);
// 			debug(nid);
			for (int i = c; i <= min(d, nid); i ++) {
				int j = (i - id - 1);
				ans += (j % (a + b) < a ? 'A' : 'B');
			}
			c = nid + 1;
		}
		id = nid;
	}
	cout << ans;
}

int szukajB(int a, int b, int s, int k) {
	if (a - k < (b - s + s - 1) / s) {
		return s + 1;
	}
	debug(a);
	debug(b);
	debug(s);
	debug(k);
	debug(b - (a - k) * s);
	return b - (a - k) * s;
}

int szukajA(int a, int b, int s) {
	if (a < (b + s - 1)/ s) {
		return 0;
	}
	debug(a);
	debug(b);
	debug(s);
	debug(a - (b - s + s - 1)/ s);
	return a - (b - s + s - 1)/ s;
}

void wiecejA(int a, int b, int c, int d) {
	int segA = (a + b) / (b + 1);
	vector<pair<pair<int,int>,int>> v;
	/*
	int ileW;
	if (segA == 1) ileW = b + 1;
	else ileW = a / segA;
	int dod = 0;
	if (a % segA) dod = 1;
	int ileV;
	if (segA == 1) ileV = b;
	else segV = (b - (ileW + dod)) / (segA - 1);
	int odrzutek = b - (ileW + dod) - segV * (segA - 1) + 1;
	v.push_back({{segA, 1}, ile - segV - 1});
	v.push_back({{(segV > 0 || dod == 0? segA : a % segA), odrzutek}, 1});
	v.push_back({{segA, segA}, segV - 1});
	v.push_back({{(dod == 0? segA : a % segA), segA}, min(segV, 1)});
	if (segA == 1) {
		v.push_back({{1, 1}, 1});
	}
	*/
	{
		int p = (s * a + b + s - 2) / (s - 1);
		v.push_back({{segA, 1}, p});
		a -= p * segA;
		b -= p;
	}
	while (a >= segA) {
		if (a - segA >= (b - 1 + segA - 1)/segA) {
			v.push_back({{segA, 1}, 1});
			a -= segA;
			b -= 1;
		}
		else if (a - segA == (b - 1 + segA - 1) / segA - 1) {
			int x = szukajB(a, b, segA, segA);
			if (x > segA) break;
			assert(x >= 1);
			v.push_back({{segA, x}, 1});
			a -= segA;
			b -= x;
		}
		else break;
	}
	while (a > 0) {
		int x = szukajA(a, b, segA);
		debug(v);
		assert(x >= 1);
		int y = szukajB(a, b, segA, x);
		v.push_back({{x, y}, 1});
		a -= x;
		b -= y;
		if (x == 1 && b == segA) {
			break;
		}
	}
	v.push_back({{1, segA}, b / segA});
	a -= 1 * (b / segA);
	if (a > 0) {
		v.push_back({{a, 0}, 1});
	}
	wypisz(v, c, d);
}

void wiecejB(int a, int b, int c, int d) {
	int segA = (a + b) / (a + 1);
	vector<pair<pair<int,int>,int>> v;
	if (segA * a < b) {
		v.push_back({{0, b - segA * a}, 1});
		b = segA * a;
	}
	{
		int p = (s * a + b + s - 2) / (s - 1);
		v.push_back({{segA, 1}, p});
		a -= p * segA;
		b -= p;
	}
	while (a >= segA) {
		if (a - segA >= (b - 1 + segA - 1)/segA) {
			v.push_back({{segA, 1}, 1});
			a -= segA;
			b -= 1;
		}
		else if (a - segA == (b - 1 + segA - 1) / segA - 1) {
			int x = szukajB(a, b, segA, segA);
			if (x > segA) break;
			
			assert(x >= 1);
			v.push_back({{segA, x}, 1});
			a -= segA;
			b -= x;
		}
		else break;
	}
	while (a > 0) {
		debug(v);
		int x = szukajA(a, b, segA);
		assert(x >= 1);
		int y = szukajB(a, b, segA, x);
		v.push_back({{x, y}, 1});
		a -= x;
		b -= y;
		if (x == 1 && b == segA) {
			break;
		}
	}
	v.push_back({{1, segA}, a});
	wypisz(v, c, d);
}

void test() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	if (a == b) {
		rowny(c, d);
	}
	if (a > b) {
		wiecejA(a, b, c, d);
	}
	if (b > a) {
		wiecejB(a, b, c, d);
	}
	cout << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int q;
	cin >> q;
	for (int i = 0; i < q; i ++) {
		test();
	}
}


Compiler output:
a.cc: In function 'void wiecejA(int, int, int, int)':
a.cc:113:26: error: 's' was not declared in this scope
  113 |                 int p = (s * a + b + s - 2) / (s - 1);
      |                          ^
a.cc: In function 'void wiecejB(int, int, int, int)':
a.cc:162:26: error: 's' was not declared in this scope
  162 |                 int p = (s * a + b + s - 2) / (s - 1);
      |                          ^