import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

enum int INF = 1<<28;

void solve() {
    enum int L = 1000;
    int N, K; readf("%d %d\n", &N, &K);
    auto B = readarray!int;
    auto C = readarray!int;

    auto Z = new int[L + 1];
    Z[] = INF;
    Z[1] = 0;
    for (int m = 1; m <= L; m++) {
        for (int x = 1; x <= m; x++) {
            int n = m + m / x;
            if (n > L) continue;
            Z[n] = min(Z[n], Z[m] + 1);
        }
    }

    int M = 0; {
        foreach (z; Z) {
            if (z == INF) continue;
            M = max(M, z);
        }
    }

    int X = N * M;
    auto f = new int[][](N + 1, X + 1);
    f[0][0] = 0;
    for (int k = 0; k < N; k++) {
        for (int p = 0; p <= X; p++) {
            f[k + 1][p] = max(f[k + 1][p], f[k][p]);
            if (p + Z[B[k]] <= X) {
                f[k + 1][p + Z[B[k]]] = max(f[k + 1][p + Z[B[k]]], f[k][p] + C[k]);
            }
        }
    }
    int ans = f[N][min(K, X)];
    writeln(ans);

}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}

// cheese-cracker [2022-01-31]

long[][] dp;
int[] ks;
int[] dp2;
long[] cs;

long go(int ix, int cap){
    if(ix < 0){ return 0; }

    if(dp[ix][cap] != -1){
        return dp[ix][cap];
    }

    long res = go(ix-1, cap);
    if(cap >= ks[ix]){
        long take = go(ix - 1, cap - ks[ix]) + cs[ix];
        res = max(res, take);
    }
    dp[ix][cap] = res;
    return res;
}

void preprocess(){
    dp2[1] = 0;
    for(int k = 1; k <= 1000; ++k){
        for(int x = 1; x < 1002; ++x){
            if(k + k/x < 1002){
                dp2[k + k/x] = min(dp2[k] + 1, dp2[k + k/x]);
            }
        }
    }
}

void solve(){
    cs = [];
    ks = [];
    auto n = scan!int;
    long k = scan;
    auto bs = scanArray;
    cs = scanArray;
    int mcap = min(22005, k.to!int);
    dp = new long[][](n, mcap+1);

    for(int i = 0; i < n; ++i){
        int cnt = dp2[bs[i].to!int];
        /* show(cnt); */
        ks ~= cnt;
    }
    show(ks);
    for(int i = 0; i < n; ++i){ dp[i][] = -1; }

    writeln(go(n-1, mcap));
}

void main(){
    long tests = scan;        // Toggle!
    dp2 = new int[](1005);
    dp2[] = 1005;
    preprocess;
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

// cheese-cracker [2022-01-31]

long[][] dp;
int[] ks;
long[] cs;

long go(int ix, int cap){
    if(ix < 0){ return 0; }

    if(dp[ix][cap] != -1){
        return dp[ix][cap];
    }

    long res = go(ix-1, cap);
    if(cap >= ks[ix]){
        long take = go(ix - 1, cap - ks[ix]) + cs[ix];
        res = max(res, take);
    }
    dp[ix][cap] = res;
    return res;
}

void solve(){
    cs = [];
    ks = [];
    auto n = scan!int;
    long k = scan;
    auto bs = scanArray;
    cs = scanArray;
    int mcap = min(22005, k.to!int);
    dp = new long[][](n, mcap+1);

    for(int i = 0; i < n; ++i){
        int cnt = 0;
        long num = bs[i];
        while(num > 1){
            num >>= 1;
            ++cnt;
        }
        num = 1L << cnt;
        long till = num;
        while(num != bs[i]){
            long diff = bs[i] - num;
            long take = 1;
            for(long x = 1; 2*x <= till; ++x){
                if(num / x <= diff){
                    take = max(num / x, take);
                }
            }
            num += take;
            ++cnt;
        }
        ks ~= cnt;
    }
    show(ks);
    for(int i = 0; i < n; ++i){ dp[i][] = -1; }

    writeln(go(n-1, mcap));
    /* show(dp); */
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

// cheese-cracker [2022-01-31]

long[][] dp;
int[] ks;
long[] cs;

long go(int ix, int cap){
    if(ix < 0){ return 0; }

    if(dp[ix][cap] != -1){
        return dp[ix][cap];
    }

    long res = go(ix-1, cap);
    if(cap >= ks[ix]){
        long take = go(ix - 1, cap - ks[ix]) + cs[ix];
        res = max(res, take);
    }
    dp[ix][cap] = res;
    return res;
}

void solve(){
    auto n = scan!int;
    long k = scan;
    auto bs = scanArray;
    cs = [];
    cs = scanArray;

    ks = [];
    for(int i = 0; i < n; ++i){
        int cnt = 0;
        long num = bs[i];
        while(num > 1){
            num >>= 1;
            ++cnt;
        }
        long tim = -1;
        num = bs[i];
        while(num > 0){
            tim += (num & 1);
            num >>= 1;
        }
        cnt += tim;
        ks ~= cnt;
    }
    show(ks);
    int mcap = min(22005, k.to!int);
    dp = new long[][](n+1, mcap+1);
    for(int i = 0; i <= n; ++i){ dp[i][] = -1; }

    writeln(go(n-1, mcap));
    /* show(dp); */
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

// cheese-cracker [2022-01-31]

long[][] dp;
int[] ks;
long[] cs;

long go(int ix, int cap){
    if(ix < 0){ return 0; }

    if(dp[ix][cap] != -1){
        return dp[ix][cap];
    }

    long res = go(ix-1, cap);
    if(cap >= ks[ix]){
        long take = go(ix - 1, cap - ks[ix]) + cs[ix];
        res = max(res, take);
    }
    dp[ix][cap] = res;
    return res;
}

void solve(){
    auto n = scan!int;
    long k = scan;
    auto bs = scanArray;
    cs = [];
    cs = scanArray;

    ks = [];
    for(int i = 0; i < n; ++i){
        int cnt = 0;
        long num = bs[i];
        while(num > 1){
            num >>= 1;
            ++cnt;
        }
        long tim = -1;
        num = bs[i];
        while(num > 0){
            tim += (num & 1);
            num >>= 1;
        }
        cnt += max(tim, 0);
        ks ~= cnt;
    }
    show(ks);
    int mcap = min(10005, k.to!int);
    dp = new long[][](n+1, mcap+1);
    for(int i = 0; i <= n; ++i){ dp[i][] = -1; }

    writeln(go(n-1, mcap));
    /* show(dp); */
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

// cheese-cracker [2022-01-31]

long[][] dp;
int[] ks;
long[] cs;

long go(int ix, int cap){
    if(ix < 0){ return 0; }

    if(dp[ix][cap] != -1){
        return dp[ix][cap];
    }

    long res = go(ix-1, cap);
    if(cap >= ks[ix]){
        long take = go(ix - 1, cap - ks[ix]) + cs[ix];
        res = max(res, take);
    }
    dp[ix][cap] = res;
    return res;
}

void solve(){
    auto n = scan!int;
    long k = scan;
    auto bs = scanArray;
    cs = [];
    cs = scanArray;

    ks = [];
    for(int i = 0; i < n; ++i){
        int cnt = 0;
        long num = bs[i];
        while(num > 1){
            num >>= 1;
            ++cnt;
        }
        long tim = -1;
        num = bs[i];
        while(num > 0){
            tim += (num & 1);
            num >>= 1;
        }
        show(bs[i], " ", tim);
        cnt += max(tim, 0);
        ks ~= cnt;
    }
    show(ks);
    int mcap = min(2005, k.to!int);
    dp = new long[][](n+1, mcap+1);
    for(int i = 0; i <= n; ++i){ dp[i][] = -1; }

    writeln(go(n-1, mcap));
    /* show(dp); */
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math, std.numeric;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }

import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

enum int INF = 1<<28;

void solve() {
    enum int L = 1000;
    int N, K; readf("%d %d\n", &N, &K);
    auto B = readarray!int;
    auto C = readarray!int;

    auto Z = new int[L + 1];
    Z[] = INF;
    Z[1] = 0;
    for (int m = 1; m <= L; m++) {
        for (int x = 1; x <= m; x++) {
            int n = m + m / x;
            if (n > L) continue;
            Z[n] = min(Z[n], Z[m] + 1);
        }
    }

    int M = 0; {
        foreach (z; Z) {
            if (z == INF) continue;
            M = max(M, z);
        }
    }

    int X = N * M;
    auto f = new int[][](N + 1, X + 1);
    f[0][0] = 0;
    for (int k = 0; k < N; k++) {
        for (int p = 0; p < X; p++) {
            f[k + 1][p] = max(f[k + 1][p], f[k][p]);
            if (p + Z[B[k]] <= X) {
                f[k + 1][p + Z[B[k]]] = max(f[k + 1][p + Z[B[k]]], f[k][p] + C[k]);
            }
        }
    }
    int ans = f[N][min(K, X)];
    writeln(ans);

}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}

import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

enum int INF = 1<<28;

void solve() {
    enum int L = 1000;
    int N, K; readf("%d %d\n", &N, &K);
    auto B = readarray!int;
    auto C = readarray!int;

    auto Z = new int[L + 1];
    Z[] = INF;
    Z[1] = 0;
    for (int m = 1; m <= L; m++) {
        for (int x = 1; x <= m; x++) {
            int n = m + m / x;
            if (n > L) continue;
            Z[n] = min(Z[n], Z[m] + 1);
        }
    }

    int M = 0; {
        foreach (z; Z) {
            if (z == INF) continue;
            M = max(M, z);
        }
    }

    int X = M * M;
    auto f = new int[][](N + 1, X + 1);
    f[0][0] = 0;
    for (int k = 0; k < N; k++) {
        for (int p = 0; p < X; p++) {
            f[k + 1][p] = max(f[k + 1][p], f[k][p]);
            if (p + Z[B[k]] <= X) {
                f[k + 1][p + Z[B[k]]] = max(f[k + 1][p + Z[B[k]]], f[k][p] + C[k]);
            }
        }
    }
    int ans = f[N][min(K, X)];
    writeln(ans);

}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}


// Vicfred
// https://codeforces.com/contest/1472/problem/A
// math
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;

void main() {
    int t = readln.chomp.to!int;
while(t--) {
    long w, h, n;
    readf("%s %s %s\n", &w, &h, &n);

    int x = 1;
    while(w % 2 == 0) {
        w /= 2;
        x *= 2;
    }

    int y = 1;
    while(h % 2 == 0) {
        h /= 2;
        y *= 2;
    }

    if(x*y >= n)
        "Yes".writeln;
    else
        "No".writeln;
}
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto a = readln.splitter.map !(to !(int)).array;
		int res = 1;
		foreach (j; 0..2)
		{
			while (a[j] % 2 == 0)
			{
				a[j] /= 2;
				res *= 2;
			}
		}
		writeln (res >= a[2] ? "Yes" : "No");
	}
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto w = RD;
		auto h = RD;
		auto n = RD;

		long x = 1, y = 1;
		while (w % 2 == 0)
		{
			x *= 2;
			w /= 2;
		}
		while (h % 2 == 0)
		{
			y *= 2;
			h /= 2;
		}

		ans[ti] = x*y >= n;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush;
	debug readln;
}


import std.stdio;
import std.math;
import std.array;
import std.algorithm;

unittest{
  assert(solve(2,[0,4,5,1,0]) == 1, "Teste 1");
  assert(solve(4,[0,1,2,3,4,5]) == 0, "Teste 2");
  assert(solve(5,[0,0,0,0,0,0]) == 2, "Teste 3");
}

ulong solve(ulong k, ulong[] l){
  ulong max = 5 - k;
  ulong cnt = 0;
  foreach(x;l){
    if (x <= max){
      cnt++;
    }
  }
  debug(1) writeln(cnt/3);
  return cnt/3;
}

void main(){
  size_t n;
  ulong k;
  readf("%s %s ",&n,&k);

  ulong[] l = new ulong[n];
  for(size_t i = 0; i < n; i++){
    ulong t;
    readf("%s ",&t);
    l[i] = t;
  }
  writeln(solve(k,l));
  // auto s = r.map!"to!string(a)".array;
}

import std.stdio;
import core.stdc.stdio;
import std.algorithm;
import std.math;
import std.array;

int main(string[] argv)
{
	int n, k;
	scanf("%d %d", &n, &k);
	int eligible_members = 0;
	foreach (int i; 0..n)
	{
		int a;
		scanf("%d", &a);
		eligible_members += (a + k <= 5 ? 1 : 0);
	}
	write (eligible_members / 3);
	return 0;
}


import std.stdio, std.string, std.algorithm;
import std.conv, std.array;
import std.container;
import std.typecons;

void main() {
    int n, m; readf("%d %d\n", &n, &m);
    int[] a = stdin.readln.chop.split.map!(to!int).array;
    int[] b = stdin.readln.chop.split.map!(to!int).array;
    a.sort!"a > b";
    b.sort!"a > b";
    int ai = 0, bi = 0;
    int x = n;
    while (ai < n && bi < m) {
        if (a[ai] <= b[bi]) {
            x--;
            ai++;
            bi++;
        } else {
            ai++;
        }
    }
    x.writeln;
}

import std.stdio;
import std.math;

void main()
{
	int n, m;
	int[] a, b;
	scanf("%d %d", &n, &m);
	a.length = n;
	b.length = m;
	foreach (i; 0 .. n) scanf("%d", &a[i]);
	foreach (i; 0 .. m) scanf("%d", &b[i]);
	int result;
	while (b.length) {
		if (a.length == 0) break;
		if (b[0] < a[0]) {
			b = b[1..$];
		} else {
			a = a[1..$];
			b = b[1..$];
		}
	}
	writeln(a.length);
}


import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math, std.typecons;

char[] s;

void main() {
    s = readln.chomp.to!(char[]);

    debug {
        writeln("s:", s);
    }

    int n = s.length.to!int;

    int[] acnt = new int[](26);

    foreach (ch ; s) {
        acnt[ch - 'a']++;
    }

    debug {
        writeln("acnt:", acnt);
    }

    auto u = new char[](n);
    int ui = 0;

    auto st = new Stack!char(n + 10);

    while (ui < n) {
        if (s.empty) {
            while (!st.empty) {
                u[ui++] = st.top.to!char;
                st.pop;
            }
            break;
        }
        if (st.empty) {
            st.push(s.front.to!char);
            acnt[s.front - 'a']--;
            s.popFront;
        }
        else {
            debug {
                writeln(st.top, " ", (st.top - 'a').to!int);
                writeln(acnt[0 .. (st.top - 'a').to!int]);
            }
            if (acnt[0 .. (st.top - 'a').to!int].sum > 0) {
                st.push(s.front.to!char);
                acnt[s.front - 'a']--;
                s.popFront;
            }
            else {
                u[ui++] = st.top;
                //acnt[(st.top - 'a').to!int]--;
                st.pop;
            }
        }

        debug {
            //writeln("s:", s);
            writeln("acnt:", acnt);
        }
    }

    writeln(u);
}

void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

struct Stack(T) {
private:
    int N, peek;
    T[] data;

public:
    this(int size)
    {
        N = size;
        data = new T[](N);
    }

    bool empty() @property
    {
        return peek == 0;
    }

    bool full() @property
    {
        return peek == N;
    }

    void push(T x) @property
    {
        assert(!full);
        data[peek++] = x;
    }

    void pop() @property
    {
        assert(!empty);
        --peek;
    }

    T top() @property
    {
        return data[peek - 1];
    }

    void clear() @property
    {
        peek = 0;
    }

    int length() @property
    {
        return peek;
    }

}

import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math, std.typecons;

char[] s;

void main() {
    s = readln.chomp.to!(char[]);

    int n = s.length.to!int;

    auto t = Stack!(char)(n + 10);
    auto u = new char[](n);

    auto cnt = new int[](26);

    foreach (ch ; s) {
        cnt[ch - 'a']++;
    }

    int ui;

    while (!s.empty) {
        if (t.empty) {
            t.push(s.front.to!char);
            cnt[s.front - 'a']--;
            s.popFront;
        }
        else {
            if (cnt[0 .. (t.top - 'a')].sum > 0) {
                t.push(s.front.to!char);
                cnt[s.front - 'a']--;
                s.popFront;
            }
            else {
                u[ui++] = t.top;
                t.pop;
            }
        }
    }

    while (!t.empty) {
        u[ui++] = t.top;
        t.pop;
    }

    writeln(u);
}

void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

struct Stack(T) {
private:
    int N, peek;
    T[] data;

public:
    this(int size)
    {
        N = size;
        data = new T[](N);
    }

    bool empty() @property
    {
        return peek == 0;
    }

    bool full() @property
    {
        return peek == N;
    }

    void push(T x) @property
    {
        assert(!full);
        data[peek++] = x;
    }

    void pop() @property
    {
        assert(!empty);
        --peek;
    }

    T top() @property
    {
        return data[peek - 1];
    }

    void clear() @property
    {
        peek = 0;
    }

    int length() @property
    {
        return peek;
    }

}

import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.container, std.math, std.typecons;

dchar[] s;

void main() {
    s = readln.chomp.to!(dchar[]);

    debug {
        writeln("s:", s);
    }

    int n = s.length.to!int;

    int[] acnt = new int[](26);

    foreach (ch ; s) {
        acnt[ch - 'a']++;
    }

    debug {
        writeln("acnt:", acnt);
    }

    auto u = new dchar[](n);
    int ui = 0;

    auto st = new Stack!dchar(n + 10);

    while (ui < n) {
        if (s.empty) {
            while (!st.empty) {
                u[ui++] = st.top.to!dchar;
                st.pop;
            }
            break;
        }
        if (st.empty) {
            st.push(s.front.to!dchar);
            s.popFront;
        }
        else {
            if (acnt[0 .. (st.top - 'a').to!int].sum > 0) {
                st.push(s.front.to!dchar);
                acnt[s.front - 'a']--;
                s.popFront;
            }
            else {
                u[ui++] = st.top;
                st.pop;
            }
        }

        debug {
            writeln("s:", s);
        }
    }

    writeln(u);
}

void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

struct Stack(T) {
private:
    int N, peek;
    T[] data;

public:
    this(int size)
    {
        N = size;
        data = new T[](N);
    }

    bool empty() @property
    {
        return peek == 0;
    }

    bool full() @property
    {
        return peek == N;
    }

    void push(T x) @property
    {
        assert(!full);
        data[peek++] = x;
    }

    void pop() @property
    {
        assert(!empty);
        --peek;
    }

    T top() @property
    {
        return data[peek - 1];
    }

    void clear() @property
    {
        peek = 0;
    }

    int length() @property
    {
        return peek;
    }

}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto L = new int[N];
      auto R = new int[N];
      foreach (i; 0 .. N) {
        L[i] = readInt() - 1;
        R[i] = readInt();
      }

      auto as = new int[M];
      foreach (i; 0 .. N) {
        ++as[L[i]];
        if (R[i] < M) {
          --as[R[i]];
        }
      }
      foreach (j; 1 .. M) {
        as[j] += as[j - 1];
      }
      debug {
        writeln("as = ", as);
      }

      int[] doIt() {
        auto dp = new int[M];
        int[] lis;
        foreach (j; 0 .. M) {
          const pos = lis.upperBound(as[j]);
          if (pos == lis.length) {
            lis ~= as[j];
          } else {
            lis[pos] = as[j];
          }
          dp[j] = pos + 1;
        }
        debug {
          writeln(as, ": ", dp);
        }
        return dp;
      }

      auto dpL = doIt();
      as.reverse;
      auto dpR = doIt();
      as.reverse;
      dpR.reverse;
      int ans;
      foreach (j; 0 .. M) {
        chmax(ans, dpL[j] + dpR[j] - 1);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

int [] fun (int [] a)
{
	auto p = new int [a.length + 1];
	p[] = int.max;
	p[0] = int.min;
	int [] res;
	foreach (c; a)
	{
		int lo = 0;
		int hi = p.length.to !(int) - 1;
		while (lo < hi)
		{
			int me = (lo + hi) / 2;
			if (p[me] <= c)
			{
				lo = me + 1;
			}
			else
			{
				hi = me;
			}
		}
		p[lo] = c;
		res ~= lo;
	}
	debug {writeln (a); writeln (p); writeln (res);}
	return res;
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto v = new int [m + 2];
		foreach (i; 0..n)
		{
			int x, y;
			readf (" %s %s", &x, &y);
			v[x] += 1;
			v[y + 1] -= 1;
		}
		foreach (j; 0..m + 1)
		{
			v[j + 1] += v[j];
		}
		v = v[1..$ - 1];

		auto x = fun (v);
		reverse (v);
		auto y = fun (v);
		reverse (y);
		int res = 0;
		foreach (i; 0..x.length)
		{
			res = max (res, x[i] + y[i] - 1);
		}
		writeln (res);
	}
}

import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

int fun (int [] a)
{
	auto p = new int [a.length + 1];
	p[] = int.max;
	p[0] = int.min;
	int res = 0;
	foreach (c; a)
	{
		int lo = 0;
		int hi = p.length.to !(int) - 1;
		while (lo < hi)
		{
			int me = (lo + hi) / 2;
			if (p[me] <= c)
			{
				lo = me + 1;
			}
			else
			{
				hi = me;
			}
		}
		p[lo] = c;
		res = max (res, lo);
	}
	debug {writeln (a); writeln (p);}
	return res;
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto v = new int [m + 2];
		foreach (i; 0..n)
		{
			int x, y;
			readf (" %s %s", &x, &y);
			v[x] += 1;
			v[y + 1] -= 1;
		}
		foreach (j; 0..m + 1)
		{
			v[j + 1] += v[j];
		}
		v = v.filter !(x => x > 0).array;

		int res = 0;
		res = max (res, fun (v));
		reverse (v);
		res = max (res, fun (v));
		writeln (res);
	}
}

import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

int [] fun (int [] a)
{
	auto p = new int [a.length + 1];
	p[] = int.max;
	p[0] = int.min;
	int [] res;
	foreach (c; a)
	{
		int lo = 0;
		int hi = p.length.to !(int) - 1;
		while (lo < hi)
		{
			int me = (lo + hi) / 2;
			if (p[me] <= c)
			{
				lo = me + 1;
			}
			else
			{
				hi = me;
			}
		}
		p[lo] = c;
		res ~= lo;
	}
	debug {writeln (a); writeln (p); writeln (res);}
	return res;
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto v = new int [m + 2];
		foreach (i; 0..n)
		{
			int x, y;
			readf (" %s %s", &x, &y);
			v[x] += 1;
			v[y + 1] -= 1;
		}
		foreach (j; 0..m + 1)
		{
			v[j + 1] += v[j];
		}
		v = v.filter !(x => x > 0).array;

		auto x = fun (v);
		reverse (v);
		auto y = fun (v);
		reverse (y);
		int res = 0;
		foreach (i; 0..x.length)
		{
			res = max (res, x[i] + y[i] - 1);
		}
		writeln (res);
	}
}

import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

int fun (int [] a)
{
	auto p = new int [a.length];
	p[] = int.max;
	p[0] = int.min;
	int res = 0;
	foreach (c; a[1..$ - 1])
	{
		int lo = 0;
		int hi = a.length.to !(int) - 1;
		while (lo < hi)
		{
			int me = (lo + hi) / 2;
			if (p[me] <= c)
			{
				lo = me + 1;
			}
			else
			{
				hi = me;
			}
		}
		p[lo] = c;
		res = max (res, lo);
	}
	debug {writeln (a); writeln (p);}
	return res;
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto v = new int [m + 2];
		foreach (i; 0..n)
		{
			int x, y;
			readf (" %s %s", &x, &y);
			v[x] += 1;
			v[y + 1] -= 1;
		}
		foreach (j; 0..m + 1)
		{
			v[j + 1] += v[j];
		}

		int res = 0;
		res = max (res, fun (v));
		reverse (v);
		res = max (res, fun (v));
		writeln (res);
	}
}


import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

immutable int MOD = 998244353;

long addmod(long a, long b) {
    long c = a + b;
    if (c > MOD) c -= MOD;
    return c;
}

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    auto binom = new long[][] (n+1, n+1);

    binom[0][0] = 1;
    foreach (nn; 1 .. n+1) {
        binom[nn][0] = 1;
        foreach (kk; 1 .. nn+1) {
            binom[nn][kk] = addmod(binom[nn-1][kk], binom[nn-1][kk-1]);
        }
    }

    debug { binom.writeln; }

    auto dp = new long[] (n+1);
    dp.fill(0);
    dp[n] = 1;

    long ans = 0;
    foreach_reverse (i; 0 .. n) {
        if (arr[i] <= 0) continue;

        auto k = arr[i] + 1;
        foreach (j; i + k-1 .. n) {
            auto opts = binom[j - i][k - 1];
            dp[i] = dp[i].addmod(opts * dp[j+1] % MOD);

            debug { writeln(i, ' ', j, ' ', opts, ' ', dp[j+1]); }
        }

        ans = ans.addmod(dp[i]);

        debug { writeln(i, ' ', dp[i]); }
    }

    ans.writeln;
}

import std.stdio, std.string;
import std.algorithm;

int saiki(int idx, int n, int[] a, int[] dp, int[][] c)
{
    if (dp[idx] != -1)
    {
        return dp[idx];
    }
    if (idx == n)
    {
        dp[idx] = 1;
        return 1;
    }
    if (a[idx] <= 0 || a[idx] > n - idx - 1)
    {
        dp[idx] = 0;
        return 0;
    }
    static const int mod = 998244353;
    auto res = 0;
    foreach (i; idx + a[idx] + 1 .. n + 1)
    {
        auto ret = saiki(i, n, a, dp, c);
        res += (cast(long)c[i - idx - 1][a[idx]] * ret) % mod;
        res %= mod;
    }
    dp[idx] = res;
    return res;
}

void solve(int[] a, int n)
{
    static const int mod = 998244353;
    auto c = new int[][](n + 1, n + 1);
    foreach (i; 0 .. n + 1)
    {
        fill(c[i], 0);
    }
    foreach (i; 0 .. n + 1)
    {
        c[i][0] = 1;
    }
    foreach (i; 1 .. n + 1)
    {
        foreach (j; 1 .. i + 1)
        {
            c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
        }
    }
    auto dp = new int[n + 1];
    fill(dp, -1);
    auto res = 0;
    foreach (i; 0 .. n)
    {
        auto ret = saiki(i, n, a, dp, c);
        res = (res + ret) % mod;
    }
    writeln(res);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a, n);
    }
    return 0;
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

enum mod = 998244353;
alias M = Mod!(long, mod);
M[2000] fact;
M bin(int n, int k)
{
  if (k > n) return M(0);
  return fact[n] * (fact[n - k] * fact[k])^^-1;
}

void main(string[] args)
{
  fact[0] = M(1);
  foreach(i; 1 .. fact.length)
    fact[i] = M(i) * fact[i - 1];

  auto n = next!int;
  auto a = next!int(n);
  debug writeln("got ", a);
  auto noStartingIn = new M[](n);
  foreach_reverse(start; 0 .. n)
    {
      debug writeln("doing from start = ", start);
      if (a[start] <= 0) continue;
      noStartingIn[start] = bin(n - 1 - start, a[start]);
      foreach(otherStart; start + 1 .. n)
	{
	  noStartingIn[start] += bin(otherStart - start - 1, a[start]) * noStartingIn[otherStart];
	}
    }
  noStartingIn.sum.writeln;
  return;
}

auto noDigs(long num)
{
  auto res = int(0);
  while(num)
    {
      num /= 10;
      res++;
    }
  return res;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  this(Mod!(T, mod) other)
  {
    _rep = other._rep;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   if (exp == -1) exp = mod - 2;
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.stdio, std.string;
import std.algorithm;

int saiki(int idx, int n, int[] a, int[] dp, int[][] c)
{
    if (dp[idx] != -1)
    {
        return dp[idx];
    }
    if (idx == n)
    {
        dp[idx] = 1;
        return 1;
    }
    if (a[idx] < 0 || a[idx] > n - idx - 1)
    {
        dp[idx] = 0;
        return 0;
    }
    static const int mod = 998244353;
    auto res = 0;
    foreach (i; idx + a[idx] + 1 .. n + 1)
    {
        auto ret = saiki(i, n, a, dp, c);
        res += (cast(long)c[i - idx - 1][a[idx]] * ret) % mod;
        res %= mod;
    }
    dp[idx] = res;
    return res;
}

void solve(int[] a, int n)
{
    static const int mod = 998244353;
    auto c = new int[][](n + 1, n + 1);
    foreach (i; 0 .. n + 1)
    {
        fill(c[i], 0);
    }
    foreach (i; 0 .. n + 1)
    {
        c[i][0] = 1;
    }
    foreach (i; 1 .. n + 1)
    {
        foreach (j; 1 .. i + 1)
        {
            c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
        }
    }
    auto dp = new int[n + 1];
    fill(dp, -1);
    auto res = 0;
    foreach (i; 0 .. n)
    {
        auto ret = saiki(i, n, a, dp, c);
        res = (res + ret) % mod;
    }
    writeln(res);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a, n);
    }
    return 0;
}

import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

immutable int MOD = 998244353;

long addmod(long a, long b) {
    long c = a + b;
    if (c > MOD) c -= MOD;
    return c;
}

void main()
{

    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    auto binom = new long[][] (n+1, n+1);

    binom[0][0] = 1;
    foreach (nn; 1..n+1) {
        foreach (kk; 0..n+1) {
            binom[nn][kk] = (binom[nn-1][kk] + (kk > 0 ? binom[nn-1][kk-1] : 0)) % MOD;
        }
    }

    debug { binom.writeln; }

    auto dp = new long[] (n+1);
    dp[n] = 0;

    foreach_reverse (i; 0 .. n) {
        auto k = arr[i];
        if (k < 1) continue;

        foreach (j; i + k .. n) {
            auto opts = k == 1 ? 1L : binom[j - 1 - i][k - 2];
            dp[i] = addmod(dp[i], addmod(opts, opts * dp[j+1] % MOD));

            debug { writeln(i, ' ', j, ' ', opts, ' ', dp[j+1]); }
        }

        debug { writeln(i, ' ', dp[i]); }
    }

    auto ans = dp.fold!((a, b) => addmod(a, b));
    ans.writeln;
}

import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

immutable int MOD = 998244353;

long addmod(long a, long b) {
    long c = a + b;
    if (c > MOD) c -= MOD;
    return c;
}

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    auto binom = new long[][] (n+1, n+1);

    binom[0][0] = 1;
    foreach (nn; 1 .. n+1) {
        foreach (kk; 0 .. nn+1) {
            binom[nn][kk] = addmod(binom[nn-1][kk], kk > 0 ? binom[nn-1][kk-1] : 0L);
        }
    }

    debug { binom.writeln; }

    auto dp = new long[] (n+1);
    dp.fill(0);

    foreach_reverse (i; 0 .. n) {
        if (arr[i] <= 0) continue;

        auto k = arr[i] + 1;
        foreach (j; i + k-1 .. n) {
            auto opts = binom[j-1 - i][k - 2];
            dp[i] = dp[i].addmod(opts * addmod(dp[j+1], 1) % MOD);

            debug { writeln(i, ' ', j, ' ', opts, ' ', dp[j+1]); }
        }

        dp[i] = dp[i].addmod(dp[i+1]);

        debug { writeln(i, ' ', dp[i]); }
    }

    dp[0].writeln;
}

import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

immutable int MOD = 998244353;

long addmod(long a, long b) {
    long c = a + b;
    if (c > MOD) c -= MOD;
    return c;
}

void main()
{
    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    auto binom = new long[][] (n+1, n+1);

    binom[0][0] = 1;
    foreach (nn; 1 .. n+1) {
        foreach (kk; 0 .. nn+1) {
            binom[nn][kk] = addmod(binom[nn-1][kk], kk > 0 ? binom[nn-1][kk-1] : 0L);
        }
    }

    debug { binom.writeln; }

    auto dp = new long[] (n+1);
    dp.fill(0);

    foreach_reverse (i; 0 .. n) {
        if (arr[i] <= 0) continue;

        auto k = arr[i] + 1;
        foreach (j; i + k .. n) {
            auto opts = binom[j - i][k - 1];
            dp[i] = dp[i].addmod(opts * addmod(dp[j+1], 1) % MOD);

            debug { writeln(i, ' ', j, ' ', opts, ' ', dp[j+1]); }
        }

        dp[i] = dp[i].addmod(dp[i+1]);

        debug { writeln(i, ' ', dp[i]); }
    }

    dp[0].writeln;
}

import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

immutable int MOD = 998244353;

long addmod(long a, long b) {
    long c = a + b;
    if (c > MOD) c -= MOD;
    return c;
}

void main()
{

    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    auto binom = new long[][] (n+1, n+1);

    binom[0][0] = 1;
    foreach (nn; 1..n+1) {
        foreach (kk; 0..n+1) {
            binom[nn][kk] = addmod(binom[nn-1][kk], kk > 0 ? binom[nn-1][kk-1] : 0L);
        }
    }

    debug { binom.writeln; }

    auto dp = new long[] (n+1);
    dp[n] = 0;

    foreach_reverse (i; 0 .. n) {
        auto k = arr[i] + 1;
        if (k < 2) continue;

        foreach (j; i + k-1 .. n) {
            auto opts = binom[j-1 - i][k - 2];
            dp[i] = dp[i].addmod(opts * addmod(dp[j+1], 1) % MOD);

            debug { writeln(i, ' ', j, ' ', opts, ' ', dp[j+1]); }
        }

        dp[i] = dp[i].addmod(dp[i+1]);

        debug { writeln(i, ' ', dp[i]); }
    }

    dp[0].writeln;
}

import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

immutable int MOD = 998244353;

long addmod(long a, long b) {
    long c = a + b;
    if (c > MOD) c -= MOD;
    return c;
}

void main()
{

    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    auto binom = new long[][] (n+1, n+1);

    binom[0][0] = 1;
    foreach (nn; 1..n+1) {
        foreach (kk; 0..n+1) {
            binom[nn][kk] = addmod(binom[nn-1][kk], kk > 0 ? binom[nn-1][kk-1] : 0L);
        }
    }

    debug { binom.writeln; }

    auto dp = new long[] (n+1);
    dp[n] = 0;

    foreach_reverse (i; 0 .. n) {
        auto k = arr[i];
        if (k < 1) continue;

        foreach (j; i + k .. n) {
            auto opts = k == 1 ? 1L : binom[j - 1 - i][k - 2];
            dp[i] = addmod(dp[i], addmod(opts, opts * dp[j+1] % MOD));

            debug { writeln(i, ' ', j, ' ', opts, ' ', dp[j+1]); }
        }

        dp[i] += dp[i+1];

        debug { writeln(i, ' ', dp[i]); }
    }

    dp[0].writeln;
}

import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

immutable int MOD = 998244353;

long addmod(long a, long b) {
    long c = a + b;
    if (c > MOD) c -= MOD;
    return c;
}

void main()
{

    int n;
    readf("%s", &n);
    readln;

    auto arr = readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    auto binom = new long[][] (n+1, n+1);

    binom[0][0] = 1;
    foreach (nn; 1..n+1) {
        foreach (kk; 0..n+1) {
            binom[nn][kk] = addmod(binom[nn-1][kk], kk > 0 ? binom[nn-1][kk-1] : 0L);
        }
    }

    debug { binom.writeln; }

    auto dp = new long[] (n+1);
    dp[n] = 0;

    foreach_reverse (i; 0 .. n) {
        auto k = arr[i];
        if (k < 1) continue;

        foreach (j; i + k .. n) {
            auto opts = k == 1 ? 1L : binom[j - 1 - i][k - 2];
            dp[i] = dp[i].addmod(opts * addmod(dp[j+1], 1) % MOD);

            debug { writeln(i, ' ', j, ' ', opts, ' ', dp[j+1]); }
        }

        dp[i] = dp[i].addmod(dp[i+1]);

        debug { writeln(i, ' ', dp[i]); }
    }

    dp[0].writeln;
}


import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}
Input input;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t; get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
void main()
{
  int q; get(q);
  while(q--)
    {
      long[] a =  new long[3]; get(a);
      auto sa = sort(a);
      auto res = 2 * (a[2] - a[0]);
      if (a[2] - a[0] <= 1)
	wr(0);
      else
	wr(res - 4);
    }

}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto q = RD!int;
	auto ans = new int[](q);
	foreach (i; 0..q)
	{
		auto a = RD!int;
		auto b = RD!int;
		auto c = RD!int;
		if (max(a, b, c) - min(a, b, c) <= 2)
			continue;
		auto x = max(a, b, c) - min(a, b, c) - 2;
		ans[i] = x * 2;
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        auto s = readln.split.map!(to!long);
        auto N = s[0];
        auto M = s[1];
        auto K = s[2];
        long ans = 1L << 59;
        foreach (i; -1..2) foreach (j; -1..2) foreach (k; -1..2) {
            auto a = N + i;
            auto b = M + j;
            auto c = K + k;
            ans = min(ans, abs(a - b) + abs(b - c) + abs(c - a));
        }
        ans.writeln;
    }
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int q = rint;
	foreach(_; 0 .. q){
		long a = rlong, b = rlong, c = rlong;
		long ans = (a + b + c) * 100;
		foreach(i; [a - 1, a, a + 1]) foreach(j; [b - 1, b, b + 1]) foreach(k; [c - 1, c, c + 1]){
			ans.chmin(abs(i - j) + abs(j - k) + abs(k - i));
		}
		ans.writeln;
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int q = rint;
	foreach(_; 0 .. q){
		int a = rint, b = rint, c = rint;
		int ans = a + b + c;
		foreach(i; [a - 1, a, a + 1]) foreach(j; [b - 1, b, b + 1]) foreach(k; [c - 1, c, c + 1]){
			ans.chmin(abs(i - j) + abs(j - k) + abs(k - i));
		}
		ans.writeln;
	}
}


import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto S = readln.chomp;

    auto cnt = new int[](26);
    foreach (i; 0..N) cnt[S[i]-'A'] += 1;

    int ans = 1 << 29;
    foreach (i; 0..K) ans = min(ans, cnt[i]);

    writeln(ans * K);
}

import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    int n, k;
    readf("%s %s", &n, &k);
    readln;

    auto s = readln.chomp;
    auto cnt = new int[k];
    foreach (e; s) ++cnt[e-'A'];

    auto ans = k * cnt.minElement;

    ans.writeln;
}

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n, k; rd(n, k);
  auto s=readln.chomp.to!(char[]);

  int[char] freq;
  foreach(c; s){
    if(c in freq) freq[c]++;
    else freq[c]=1;
  }

  int mn=10^^9;
  for(char c='A'; c<'A'+k; c++){
    if(c in freq) mn=min(mn, freq[c]);
    else mn=min(mn, 0);
  }
  writeln(mn*k);

}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		a.group.map !(q{a[1]}).maxElement.writeln;
	}
}

import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N; get(N);
        int last, c, max_c;
        foreach (a; readln.split.to!(int[])) {
            if (last != a) {
                max_c = max(max_c, c);
                c = 0;
            }
            last = a;
            ++c;
        }
        writeln(max(max_c, c));
    }
}

// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    int n;
    n = scan!int;
    auto arr = scanArray;
    ll eq = 1, maxeq = 1;
    for(int i = 1; i < n; ++i){
        if(arr[i] == arr[i-1]){
            ++eq;
        }else{
            maxeq = max(eq, maxeq);
            eq = 1;
        }
    }
    maxeq = max(maxeq, eq);
    writeln(maxeq);
}

void main(){
    long tests = 1;
    tests = scan;        // Toggle!
    while(tests--) theCode();
}

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int(-1);

		auto cnt = new int[](n);
		foreach (e; a)
			++cnt[e];
		ans[ti] = cnt.maxElement;
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}


import std.stdio;

long mulmod (long a, long b, long m)
{
	long res = 0;
	while (b)
	{
		if (b & 1)
		{
			res += a;
			if (res >= m)
			{
				res -= m;
			}
		}
		a <<= 1;
		if (a >= m)
		{
			a -= m;
		}
		b >>= 1;
	}
	return res;
}

void main ()
{
	int tests;
	readf (" %s", &tests);
	foreach (test; 0..tests)
	{
		long p, q, b;
		readf (" %s %s %s", &p, &q, &b);
		immutable int steps = 6;
		b %= q;
		foreach (step; 0..steps)
		{
			b = mulmod (b, b, q);
		}
		p %= q;
		p = mulmod (p, b, q);
		writeln (p == 0 ? "Finite" : "Infinite");
	}
}

import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

T gcd(T) (T a, T b) pure nothrow @nogc {
  if (a < b) {
    swap (a, b);
  }
  while (b) {
    T c = a % b; a = b; b = c;
  }
  return a;
}

void main() {
  int nt;
  readf (" %d", &nt);
  foreach (t; 0 .. nt) {
    long p, q, b;
    readf (" %d %d %d", &p, &q, &b);
    long g = gcd (p, q);
    q /= g;
    while (true) {
      debug stderr.writefln ("q = %d b = %d", q, b);
      g = gcd (q, b);
      if (g == 1) break;
      while (true) {
        long r = q / g;
        if (r * g != q) break;
        q = r;
      }
    }
    writeln (q == 1 ? "Finite" : "Infinite");
  }
}

import std.stdio;

long mulmod (long a, long b, long m)
{
	long res = 0;
	while (b)
	{
		a <<= 1;
		if (a >= m)
		{
			a -= m;
		}
		if (b & 1)
		{
			res += a;
			if (res >= m)
			{
				res -= m;
			}
		}
		b >>= 1;
	}
	return res;
}

void main ()
{
	int tests;
	readf (" %s", &tests);
	foreach (test; 0..tests)
	{
		long p, q, b;
		readf (" %s %s %s", &p, &q, &b);
		immutable int steps = 6;
		b %= q;
		foreach (step; 0..steps)
		{
			b = mulmod (b, b, q);
		}
		p %= q;
		p = mulmod (p, b, q);
		writeln (p == 0 ? "Finite" : "Infinite");
	}
}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const S = readToken();
      auto grp = S.group.array;
      bool ans = true;
      ans = ans && (grp.length == 3);
      ans = ans && (grp[0][0] == 'a');
      ans = ans && (grp[1][0] == 'b');
      ans = ans && (grp[2][0] == 'c');
      ans = ans && (grp[2][1] == grp[0][1] || grp[2][1] == grp[1][1]);
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}

/+ dub.sdl:
    name "A"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv, std.regex;
// import dkh.foundation, dkh.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    scope(exit) assert(!sc.hasNext);

    string s;
    sc.read(s);
    auto r = regex(r"^a+b+c+$");
    if (!matchFirst(s, r)) {
        writeln("NO");
    } else {
        if (s.count('a') != s.count('c') && s.count('b') != s.count('c')) {
            writeln("NO");
        } else {
            writeln("YES");
        }
    }
    return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }

    int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
    void read(Args...)(auto ref Args args) {
        import std.exception;
        static if (args.length != 0) {
            enforce(readSingle(args[0]));
            read(args[1..$]);
        }
    }
    bool hasNext() {
        return succ();
    }
}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;
import std.regex;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  string s; readV(s);

  auto m = s.matchFirst(r"^(a+)(b+)(c+)$");
  writeln(m && (m[1].length == m[3].length || m[2].length == m[3].length) ? "YES" : "NO");
}

/+ dub.sdl:
    name "A"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv, std.regex;
// import dkh.foundation, dkh.scanner;

int main() {
    Scanner sc = new Scanner(stdin);
    scope(exit) assert(!sc.hasNext);

    string s;
    sc.read(s);
    auto r = regex(r"a+b+c+");
    if (!matchFirst(s, r)) {
        writeln("NO");
    } else {
        if (s.count('a') != s.count('c') && s.count('b') != s.count('c')) {
            writeln("NO");
        } else {
            writeln("YES");
        }
    }
    return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }

    int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
    void read(Args...)(auto ref Args args) {
        import std.exception;
        static if (args.length != 0) {
            enforce(readSingle(args[0]));
            read(args[1..$]);
        }
    }
    bool hasNext() {
        return succ();
    }
}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/