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
// Template generated by Clank
#include<bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define all(x) x.begin(), x.end()
#define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false);
// #define int ll
typedef long long ll;

vector<int> pattern = {1,1,0,0,1,0};
map<pair<int,int>, vector<int>> preproc_ans;

void Manacher(vector<int> &s, vector<int> &p){
    int n = s.size();
    p.resize(n, 1);
    int bpi = 0;
    for(int i = 1; i < n; i++){
        if(i < bpi + p[bpi]) p[i] = min(bpi + p[bpi] - i, p[2 * bpi - i]);
        while(0 <= i - p[i] && i + p[i] < n && s[i - p[i]] == s[i + p[i]]) 
            p[i]++;
        if(bpi + p[bpi] < i + p[i]) bpi = i; 
    }
}

int max_pal(vector<int> &s){
    vector<int> t(s.size() * 2 + 1, 2);
    vector<int> p(s.size() * 2 + 1, 0);
    for(int i = 0; i < s.size(); i++){
        t[i * 2 + 1] = s[i];
    }
    Manacher(t, p);
    int maxi = 0;
    for(int i : p) maxi = max(maxi, i - 1);
    return maxi;
}

int k;
void print_ans(vector<int> &ans){
    for(int i : ans){
        if(i){
            cout << 'P';
        } else {
            cout << 'A';
        }
    }
    // int tmp = max_pal(ans);
    // cout << " " << k << " " << tmp;
    // if(tmp != k){
    //     cout << " WA! " << ans.size() << " " << k; 
    // }
    cout << "\n";

}

void solve(){
    int n;
    cin >> n >> k;
    if(n <= 10){
        if(preproc_ans.count({n,k})){
            print_ans(preproc_ans[{n,k}]);
        } else {
            cout << "NIE\n";
        }
        return;
    }
    if(k <= 3){
        cout << "NIE\n";
        return;
    }
    if(k == n){
        for(int i = 0; i < n; i++){
            cout << 'A';
        }
        cout << "\n";
        return;
    }
    vector<int> ans(n);
    for(int i = 0; i < n; i += 6){
        for(int j = 0; j < 6 && i + j < n; j++){
            ans[i + j] = pattern[j];
        }
    }
    if(k == 4){
        print_ans(ans);
        return;
    }
    // if(k == 5 && n % 6 == 2){
    //     ans[n - 2] ^= 1;
    //     ans[n - 1] ^= 1;
    //     print_ans(ans);
    //     return;
    // }
    if(k == 5 && (n % 6 == 0 || n % 6 == 2 || n % 6 == 3) ){
        ans[n - 2] ^= 1;
        ans[n - 1] ^= 1;
        print_ans(ans);
        return;
    }
    int c = ans[n - 1 - k]^1;
    for(int i = n - k; i < n; i++){
        ans[i] = c;
    }
    if(k == 6 && (n % 6 == 1)){
        ans[n - 5] ^= 1;
        ans[n - 4] ^= 1;
        ans[n - 3] ^= 1;
        ans[n - 2] ^= 1;
        print_ans(ans);
        return;
    }
    print_ans(ans);
}

int32_t main(){
    BOOST;
    preproc_ans[{1, 1}] = {0};
    preproc_ans[{2, 1}] = {0,1};
    preproc_ans[{2, 2}] = {0,0};
    preproc_ans[{3, 2}] = {0,0,1};
    preproc_ans[{3, 3}] = {0,0,0};
    preproc_ans[{4, 2}] = {0,0,1,1};
    preproc_ans[{4, 3}] = {0,0,0,1};
    preproc_ans[{4, 4}] = {0,0,0,0};
    preproc_ans[{5, 3}] = {0,0,0,1,0};
    preproc_ans[{5, 4}] = {0,0,0,0,1};
    preproc_ans[{5, 5}] = {0,0,0,0,0};
    preproc_ans[{6, 3}] = {0,0,0,1,0,1};
    preproc_ans[{6, 4}] = {0,0,0,0,1,0};
    preproc_ans[{6, 5}] = {0,0,0,0,0,1};
    preproc_ans[{6, 6}] = {0,0,0,0,0,0};
    preproc_ans[{7, 3}] = {0,0,0,1,0,1,1};
    preproc_ans[{7, 4}] = {0,0,0,0,1,0,1};
    preproc_ans[{7, 5}] = {0,0,0,0,0,1,0};
    preproc_ans[{7, 6}] = {0,0,0,0,0,0,1};
    preproc_ans[{7, 7}] = {0,0,0,0,0,0,0};
    preproc_ans[{8, 3}] = {0,0,0,1,0,1,1,1};
    preproc_ans[{8, 4}] = {0,0,0,0,1,0,1,1};
    preproc_ans[{8, 5}] = {0,0,0,0,0,1,0,0};
    preproc_ans[{8, 6}] = {0,0,0,0,0,0,1,0};
    preproc_ans[{8, 7}] = {0,0,0,0,0,0,0,1};
    preproc_ans[{8, 8}] = {0,0,0,0,0,0,0,0};
    preproc_ans[{9, 4}] = {0,0,0,0,1,0,1,1,0};
    preproc_ans[{9, 5}] = {0,0,0,0,0,1,0,0,1};
    preproc_ans[{9, 6}] = {0,0,0,0,0,0,1,0,0};
    preproc_ans[{9, 7}] = {0,0,0,0,0,0,0,1,0};
    preproc_ans[{9, 8}] = {0,0,0,0,0,0,0,0,1};
    preproc_ans[{9, 9}] = {0,0,0,0,0,0,0,0,0};
    preproc_ans[{10, 4}] = {0,0,0,0,1,0,1,1,0,0};
    preproc_ans[{10, 5}] = {0,0,0,0,0,1,0,0,1,1};
    preproc_ans[{10, 6}] = {0,0,0,0,0,0,1,0,0,1};
    preproc_ans[{10, 7}] = {0,0,0,0,0,0,0,1,0,0};
    preproc_ans[{10, 8}] = {0,0,0,0,0,0,0,0,1,0};
    preproc_ans[{10, 9}] = {0,0,0,0,0,0,0,0,0,1};
    preproc_ans[{10, 10}] = {0,0,0,0,0,0,0,0,0,0};

    int t; cin >> t;
    while(t--){
        solve();
    }
    return 0;
}