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
#include <string>
#include <iostream>
using namespace std;

// build(8, 3, "") -> AAAPAPPP
// build(10, 4, "") -> AAAAPAPPAA
// void build(int n, int k, string s) {
//     if ((int)s.size() == n) {
//         int mx = 0;
//         for (int i = 0; i < n; i++) {
//             for (int j = i; j < n; j++) {
//                 string t = s.substr(i, j - i + 1);
//                 string rt = t;
//                 reverse(rt.begin(), rt.end());
//                 if (t == rt) mx = max(mx, j - i + 1);
//             }
//         }
//         if (mx <= k) cout << s << '\n';
//         return;
//     }
//     build(n, k, s + 'A');
//     build(n, k, s + 'P');
// }

void solve() {
    int n, k; cin >> n >> k;
    if ((k == 1 && n > 2)
        || (k == 2 && n > 4)
        || (k == 3 && n > 8)) {
        cout << "NIE\n";
        return;
    }

    string s = "";
    if (k == 1) s = "AP";
    else if (k == 2) s = "AAPP";
    else if (k == 3) s = "AAAPAPPP";
    else {
        for (int i = 0; i < k; i++) s += 'A';
        while ((int)s.size() < n) s += "PAPPAA";
    }
    cout << s.substr(0, n) << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t; cin >> t;
    while (t--) solve();
    return 0;
}