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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <memory.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <bitset>

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

#define fst first
#define snd second
#define X first
#define Y second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) ((int)(v).size())
// #define sqr(x) ((x) * (x))

typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

// #define next ajksdslk
// #define prev aklsfjk

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}

template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

const int maxn = (int)(1e6 + 10);
const int maxlog = 21;
const int base = (int)1e9 + 7;
const ld eps = (ld)1e-9;
const ld PI = acos(-1.);
// const int pp = 41;
const int inf = 2e9;
const ll llinf = 2e18;

const int MOD = (int) 1e9 + 7;

#define fassert(x, out) if (!(x)) { cerr << "Assertion failed: " << #x << ", at " << __FILE__ << ":" << __LINE__ <<": " << out << endl; exit(1); }
#define assert(x) fassert(x, "")

// https://cp-algorithms.com/string/manacher.html
vector<int> manacher_odd(string s) {
  int n = s.size();
  s = "$" + s + "^";
  vector<int> p(n + 2);
  int l = 0, r = 1;
  for(int i = 1; i <= n; i++) {
      p[i] = min(r - i, p[l + (r - i)]);
      while(s[i - p[i]] == s[i + p[i]]) {
          p[i]++;
      }
      if(i + p[i] > r) {
          l = i - p[i], r = i + p[i];
      }
  }
  return vector<int>(begin(p) + 1, end(p) - 1);
}

vector<int> manacher(string s) {
  string t;
  for(auto c: s) {
      t += string("#") + c;
  }
  auto res = manacher_odd(t + "#");
  return vector<int>(begin(res) + 1, end(res) - 1);
}

int maxPalindrome(string s) {
  auto res = manacher(s);
  int cnt = 0;
  for (int i = 0; i < sz(res); i ++) {
    int val = res[i] - 1;
    upmax(cnt, val);
  }
  return cnt;
}

string brute(int n, int k) {
  for (int msk = 0; msk < (1 << n); msk ++) {
    string s;
    for (int i = 0; i < n; i ++) {
      if ( (msk >> i) & 1 ) s.push_back('P');
      else s.push_back('A');
    }
    int c = maxPalindrome(s);
    if (c == k) return s;
  }
  return "NIE";
}

string pattern = "AAPAPP";
string generate(int n, int k) {
  if (k < 4 && n > 8) return "NIE";
  if (k < 3 && n > 4) return "NIE";
  if (k < 2 && n > 2) return "NIE";

  if (n <= 9) return brute(n, k);


  string res(n, 'P');
  int ptr = 0;
  for (int i = k; i < n; i ++) {
    res[i] = pattern[ ptr ++ ];
    ptr %= 6;
  }
  return res;
}

void solve() {

  int n, k;
  cin >> n >> k;
  cout << generate(n, k) << endl;

  // for (int i = 1; i <= 20; i ++) {
  //   for (int k = 1; k <= i; k ++) {
  //     auto kek = generate(i, k);
  //     if (kek == "NIE") {
  //       auto b = brute(i, k);
  //       assert(b == kek);
  //       continue;
  //     }
  //     fassert(maxPalindrome(kek) == k, kek << " " << maxPalindrome(kek) << " != " << k);
  //   }
  // }

  // string curMin = "";
  // for (int i = 0; i < 100; i ++) {
  //   curMin += "001011";

  //   cout << sz(curMin) << " " << maxPalindrome(curMin) << endl;
  // }

  // for (int n = 1; n <= 27; n ++) {
  //   string curMin;
  //   int minVal = 25;
  //   for (int msk = 0; msk < (1 << n); msk ++) {
  //     string s;
  //     for (int i = 0; i < n; i ++) {
  //       if ( (msk >> i) & 1 ) s.push_back('1');
  //       else s.push_back('0');
  //     }
  //     int c = maxPalindrome(s);
  //     if (upmin(minVal, c)) curMin = s;
  //   }

  //   cout << n << " : " << curMin << " " << minVal << endl;
  // }
  
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif

  // precalc();
  // return 0;
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    // ferr << "Case #" << i << ": \n";
    solve();
  }
  // cerr << 1. * clock() / CLOCKS_PER_SEC << endl;
  return 0;
}