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
#include <bits/stdc++.h>
using namespace std;

bool pal(string& s, int i, int j) {
    while (i < j) {
        if (s[i] != s[j]) {
            return false;
        }
        i++;
        j--;
    }
    return true;
}

int palindrom(string& s) {
    for (int l = s.length(); l > 0; l--) {
        for (int i = 0; i + l - 1 < s.length(); i++) {
            if (pal(s, i, i + l - 1)) {
                return l;
            }
        }
    }
}

void gen(string& s, int i) {
    if (i == s.length()) {
        int l = palindrom(s);
        cout << " " << l << " " << s << "\n";
        return;
    }
    s[i] = 'A';
    gen(s, i + 1);
    s[i] = 'P';
    gen(s, i + 1);
}

map<pair<int, int>, string> cases;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cases[{1, 1}] = "P";
    cases[{2, 1}] = "PA";
    cases[{2, 2}] = "PP";
    cases[{3, 2}] = "PPA";
    cases[{3, 3}] = "PPP";
    cases[{4, 2}] = "PPAA";
    cases[{4, 3}] = "PPPA";
    cases[{5, 3}] = "PPPAP";
    cases[{6, 3}] = "PPPAPA";
    cases[{7, 3}] = "PPPAPAA";
    cases[{8, 3}] = "PPPAPAAA";

    int t;
    cin >> t;
    while (t > 0) {
        t--;
        int n, k;
        cin >> n >> k;

        if (k <= 3) {
            auto it = cases.find({n, k});
            if (it == cases.end()) {
                cout << "NIE\n";
            } else {
                cout << it->second << "\n";
            }
        }

        else {
            string s(k, 'A');
            string p = "PPAPAA";
            for (int i = 0; i < n - k; i++) {
                s.push_back(p[i % p.length()]);
            }
            cout << s << "\n";
        }
    }

}