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

using namespace std;

#define N 1000000

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  string w[5] = {"", "AP", "AAPP", "AAAPAPPP", "PPAPAA"};
  int t;
  cin>>t;
  while(t--) {
    int n, k;
    cin>>n>>k;
    if(k==1) {
      if(n > 2) {
        cout<<"NIE\n";
      }
      else {
        cout.write(w[1].c_str(), n);
        cout<<"\n";
      }
    }
    else if(k==2) {
      if(n > 4) {
        cout<<"NIE\n";
      }
      else {
        cout.write(w[2].c_str(), n);
        cout<<"\n";
      }
    }
    else if(k==3) {
      if(n > 8) {
        cout<<"NIE\n";
      }
      else {
        cout.write(w[3].c_str(), n);
        cout<<"\n";
      }
    }
    else if(k==4) {
      string a(k, 'A');
      n -= k;
      string b;
      int l_cop = n/6;
      b.reserve(6*l_cop);
      for (int i = 0; i < l_cop; i++) {
        b.append(w[4]);
      }
      cout<<a<<b;
      cout.write(w[4].c_str(), n%6);
      cout<<"\n";
    }
    else {
      int k2 = (int)((k-1)/2);
      int d = 2*(k - 1 + k2);
      string a(k, 'A');
      n -= k;
      string h(k-2, 'P');
      h.append(1,   'A');
      h.append(k2,  'P');
      h.append(k-2, 'A');
      h.append(1,   'P');
      h.append(k2,  'A');
      string b;
      b.reserve(d* (int)n/d);
      for(int i = 0; i < n/d; i++) {
        b.append(h);
      }
      cout<<a<<b;
      cout.write(h.c_str(), n%d);
      cout<<"\n";
    }
    cout<<flush;
  }

  return 0;
}