1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator<<(auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
auto operator<<(auto &o, auto a) -> decltype(all(a), o);
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x)
#else
#define deb(...) 0
#endif

array<vi, 2> manacher(const string& s) {
	int n = sz(s);
	array<vi,2> p = {vi(n+1), vi(n)};
	rep(z,2) for (int i=0,l=0,r=0; i < n; i++) {
		int t = r-i+!z;
		if (i<r) p[z][i] = min(t, p[z][l+t]);
		int L = i-p[z][i], R = i+p[z][i]-!z;
		while (L>=1 && R+1<n && s[L-1] == s[R+1])
			p[z][i]++, L--, R++;
		if (R>r) l=L, r=R;
	}
	return p;
}

int lon(string s) {
    int pal = 0;
    auto mana = manacher(s);
    for (int i : mana[0]) pal = max(pal, 2*i);
    for (int i : mana[1]) pal = max(pal, 2*i+1);
    return pal;
}

int32_t main() {
    deb(manacher("AAAB"));

    map<pii, string> mp;

    fwd(n, 1, 15) {
        string s(n, 'x');
        rep(msk, 1 << n) {
            rep(i, n) s[i] = (msk & (1 << i)) ? 'A' : 'P';
            reverse(all(s));
            mp[{n, lon(s)}] = s;
        }
    }

    for (auto x : mp) deb(x);

    int z;
    cin >> z;

    rep(__, z) {
        int n, k;
        cin >> n >> k;

        auto it = mp.find({n, k});
        if (it != mp.end())
            cout << it->second;
        else if (n >= 12 && k >= 4) {
            string ans(k, 'A');
            rep(i, n-k) {
                if (i % 6 == 0) ans.pb('P');
                if (i % 6 == 1) ans.pb('A');
                if (i % 6 == 2) ans.pb('P');
                if (i % 6 == 3) ans.pb('P');
                if (i % 6 == 4) ans.pb('A');
                if (i % 6 == 5) ans.pb('A');
            }
            cout << ans;
            // assert(lon(ans) == k);
        }
        else cout << "NIE";
        cout << '\n';
    }
}