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
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

static int longestPalindromicSubstring(const string &s) {
    const int n = static_cast<int>(s.size());
    if (n == 0)
        return 0;
    int best = 1;
    for (int c = 0; c < n; ++c) {
        int l = c, r = c;
        while (l >= 0 && r < n && s[l] == s[r]) {
            best = max(best, r - l + 1);
            --l;
            ++r;
        }
        l = c;
        r = c + 1;
        while (l >= 0 && r < n && s[l] == s[r]) {
            best = max(best, r - l + 1);
            --l;
            ++r;
        }
    }
    return best;
}

static string wordFromIndex(long long idx, int n) {
    string s(n, 'A');
    for (int pos = 0; pos < n; ++pos) {
        int bit = static_cast<int>((idx >> (n - 1 - pos)) & 1);
        s[pos] = bit ? 'P' : 'A';
    }
    return s;
}

static void calculateBig(int n, int k) {
    if (k < 4) {
        cout << "NIE\n";
        return;
    }
    const int numA = k - 1;
    if (numA < 0 || numA > n) {
        cout << "NIE\n";
        return;
    }
    for (int i = 0; i < numA; ++i)
        cout << 'A';
    const char pat[] = "APAPPA";
    const int plen = 6;
    const int rest = n - numA;
    for (int i = 0; i < rest; ++i)
        cout << pat[i % plen];
    cout << '\n';
}

static void enumerateSmall(int n, int k) {
    const long long total = 1LL << n;
    for (long long i = 0; i < total; ++i) {
        const string w = wordFromIndex(i, n);
        if (longestPalindromicSubstring(w) == k) {
            cout << w << '\n';
            return;
        }
    }
    cout << "NIE\n";
}

int main() {
    int t;
    cin >> t;

    while (t-- > 0) {
        int n, k;
        cin >> n >> k;
           
        if (n > 8)
            calculateBig(n, k);
        else
            enumerateSmall(n, k);
    }
    return 0;
}