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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>
#include <random>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

random_device dev;
mt19937 rng(dev());

LL maxPalEven(string &s)
{
    LL i1, i2, pal, maxPal;
    maxPal=0;
    LL n=s.size();
    FOR(i,0,n-1)
    {
        i1=i; i2=i+1; pal=0;
        while (i1>=0 && i2<n)
        {
            if (s[i1]==s[i2])
            {
                pal++; if (pal>maxPal) maxPal=pal;
                i1--; i2++;
            }
            else break;
        }
    }
    return maxPal*2;
}

LL maxPalOdd(string &s)
{
    LL i1, i2, pal, maxPal;
    maxPal=1;
    LL n=s.size();
    FOR(i,0,n-1)
    {
        i1=i-1; i2=i+1; pal=1;
        while (i1>=0 && i2<n)
        {
            if (s[i1]==s[i2])
            {
                pal+=2; if (pal>maxPal) maxPal=pal;
                i1--; i2++;
            }
            else break;
        }
    }
    return maxPal;
}

LL maxPal(string& s)
{
    return MAX(maxPalEven(s), maxPalOdd(s));
}

string small[9][9];
int smallest[9];
char pappaa[6];

void solve(LL n, LL k)
{
    if (n<=8)
    {
        if (k<smallest[n]) cout << "NIE\n";
        else cout << small[n][k] << "\n";
    }
    else if (k<4) cout << "NIE\n";
    else
    {
        FOR(i,1,k) cout << 'A';
        int j=0;
        FOR(i,k+1,n)
        {
            cout << pappaa[j++];
            if (j==6) j=0;
        }
        cout << "\n";
    }
}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);
    LL n,k,t;
	smallest[1]=1; small[1][1]="P";
	smallest[2]=1; small[2][1]="PA"; small[2][2]="PP";
    smallest[3]=2; small[3][2]="AAP"; small[3][3]="APA";
    smallest[4]=2; small[4][2]="AAPP"; small[4][3]="PPPA"; small[4][4]="PPPP";
    smallest[5]=3; small[5][3]="PPAAA"; small[5][4]="AAAAP"; small[5][5]="PPPPP";
    smallest[6]=3; small[6][3]="AAAPAP"; small[6][4]="PPAAAA"; small[6][5]="APAPAA"; small[6][6]="APAAPA";
    smallest[7]=3; small[7][3]="AAAPAPP"; small[7][4]="APPAAAA"; small[7][5]="AAAAAPA"; small[7][6]="APPPPAP"; small[7][7]="PAPAPAP";
    smallest[8]=3; small[8][3]="PPPAPAAA"; small[8][4]="AAAAPPPP"; small[8][5]="AAPAAPPP"; small[8][6]="PAPPAPAA"; small[8][7]="APAAPAAP"; small[8][8]="AAPAAPAA";
    pappaa[0]='P';
    pappaa[1]='A';
    pappaa[2]='P';
    pappaa[3]='P';
    pappaa[4]='A';
    pappaa[5]='A';
    cin >> t;
    FOR(q,1,t)
    {
        cin >> n; cin >> k;
        solve(n,k);
    }
	return 0;
}