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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Karol Kosinski 2026
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) int((c).size())
#define X first
#define Y second
#define endl '\n'
#define NAM(x) #x,'=',x
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
template<class...T> void _cout(T...a){(cout<<...<<a);}

#ifndef ENABLE_DEBUG
#define DEB(k,p,f,x...)
#else
#define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);}
#endif
#define DEBF(f,x...)    DEB(1,1,f,x)
#define DEBL            DEBF(void,0)
#define DEBC(p,x...)    DEB(0,p,_cout,x)
#define DEBUG(x...)     DEBC(1,x)

constexpr int NX = 1'000'005;

char T[NX], A[NX];
int p[NX], q[NX];
int Rmin;

void manacher_p(int n)
{
    p[0] = 0;
    int i = 1, j = 1, k;
    while(i<n) {
        while(i-j>=0 && i+j<n && T[i-j]==T[i+j]) ++j;
        p[i] = j-1, k = 1;
        while(i-k>=0 && i+k<n && k<=p[i] && p[i-k]!=p[i]-k)
            p[i+k] = min( p[i-k], p[i]-k ), ++k;
        j = max( j-k, 1 ), i += k;
    }
    FOR(i,0,n) p[i] = 2*p[i] + 1;
}

void manacher_q(int n)
{
    q[0] = 0;
    int i = 1, j = 1, k;
    while(i<n) {
        while(i-j>=0 && i+j-1<n && T[i-j]==T[i+j-1]) ++j;
        q[i] = j-1, k = 1;
        while(i-k>=0 && i+k<n && k<=q[i] && q[i-k]!=q[i]-k)
            q[i+k] = min( q[i-k], q[i]-k ), ++k;
        j = max( j-k, 1 ), i += k;
    }
    FOR(i,0,n) q[i] = 2*q[i];
}

void construct(int k, int n)
{
    if ( k == n )
    {
        manacher_p(n);
        manacher_q(n);
        int r = 0;
        FOR(i,0,n)
        {
            r = max( r, p[i] );
            r = max( r, q[i] );
        }
        if ( r < Rmin )
        {
            Rmin = r;
            FOR(i,0,n) A[i] = T[i];
        }
    }
    else
    {
        T[k] = 'A';
        construct( k+1, n );
        T[k] = 'P';
        construct( k+1, n );
    }
}

void test_txt()
{
    int mxx = NX - 2;
    FOR(i,0,4) T[i] = 'A';
    FOR(i,4,mxx)
    {
        switch ( i % 6 )
        {
            case 0: case 1: case 4:
            T[i] = 'P'; break;
            default:
            T[i] = 'A';
        }
    }
    // manacher_p(mxx);
    // manacher_q(mxx);
    // int r = 0;
    // FOR(i,0,mxx)
    // {
        // r = max( r, p[i] );
        // r = max( r, q[i] );
    // }
    // DEBUG( NAM(mxx), " : ", r, endl );

    // int block = 20, start = 0;
    // int mxx = 2'000'0;
    // FR_(i,1,mxx)
    // {
        // Rmin = i + 1;
        // construct( start, i );
        // DEBUG( NAM(i), " : ", Rmin, endl, A, endl );
        // if ( i > block )
        // {
            // int new_start = i - 5;
            // FR_(j,start,new_start) T[j] = A[j];
            // start = new_start;
        // }
    // }
    // DEBUG( NAM(mxx), " : ", Rmin, endl, A, endl );

    // string s = "AAAAPAPPAAPAPPAAPAPP";
    // FOR(i,0,s.length()) T[i] = s[i];
    // FR_( i, s.length()+1, s.length()+20 )
    // {
        // Rmin = i * 2;
        // construct(s.length(), i);
        // DEBUG( NAM(i), " : ", Rmin, endl, A, endl );
    // }
}

bool solve()
{
    int n, k;
    cin >> n >> k;
    A[n] = 0;
    if ( k == 1 )
    {
        if ( n > 2 ) return false;
        A[0] = 'A';
        if ( n == 1 ) return true;
        A[1] = 'P';
        return true;
    }
    if ( k == 2 )
    {
        if ( n > 4 ) return false;
        A[0] = 'A';
        A[1] = 'A';
        if ( n == 2 ) return true;
        A[2] = 'P';
        if ( n == 3 ) return true;
        A[3] = 'P';
        return true;
    }
    if ( k == 3 )
    {
        if ( n > 8 ) return false;
        A[0] = 'A';
        A[1] = 'A';
        A[2] = 'A';
        if ( n == 3 ) return true;
        A[3] = 'P';
        if ( n == 4 ) return true;
        A[4] = 'A';
        if ( n == 5 ) return true;
        A[5] = 'P';
        if ( n == 6 ) return true;
        A[6] = 'P';
        if ( n == 7 ) return true;
        A[7] = 'P';
        return true;
    }
    int rd = k - 4;
    FOR(i,0,n-rd) A[i] = T[i];
    reverse( A, A + n-rd );
    FOR(i,n-rd,n) A[i] = 'A';
    return true;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    test_txt();
    int z;
    cin >> z;
    while ( z -- )
    {
        cout << ( solve() ? A : "NIE" ) << endl;
    }
    return 0;
}