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

void main() {
    uint n, q; readf("%d %d\n", &n, &q);
    ulong[] a = stdin.readln.split.map!(to!ulong).array;
    ulong[] c = new ulong[a.length + 1]; c.fill(0);
    foreach (i; 0 .. q) {
        int f, t; readf("%d %d\n", &f, &t); f--; t--;
        c[f]++;
        c[t + 1]--;
    }
    ulong acc = 0;
    foreach (ref e; c) {
        acc += e;
        e = acc;
    }
    auto o = c[0 .. $ - 1].sort;
    auto x = a.sort;
    ulong ans = 0;
    foreach (i; 0 .. o.length) {
        ans += o[i] * x[i];
    }
    writeln(ans);
}

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

void main() {
    uint n, q; readf("%d %d\n", &n, &q);
    ulong[] a = stdin.readln.split.map!(to!ulong).array;
    ulong[] c = new ulong[a.length + 1]; c.fill(0);
    foreach (i; 0 .. q) {
        int f, t; readf("%d %d\n", &f, &t); f--; t--;
        c[f]++;
        c[t + 1]--;
    }
    ulong acc = 0;
    foreach (ref e; c) {
        acc += e;
        e = acc;
    }
    ulong[] o = c[0 .. $ - 1].sort!("a > b").array;
    ulong[] x = a.sort!("a > b").array;
    ulong ans = 0;
    foreach (i; 0 .. o.length) {
        ans += o[i] * x[i];
    }
    writeln(ans);
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n,q;
    readf!"%d %d"(n,q);
    readln;
    int[] a=readln.splitter
                  .map!(to!int)
                  .array;
    long[] d=new long[n];
    int l,r;
    while(q--){
        readf!"%d %d"(l,r);
        readln;
        l--;
        d[l]++;
        if(r<n) d[r]--;
    }
    foreach(i;1..n)
        d[i]+=d[i-1];
    sort!"a>b"(d);
    sort!"a>b"(a);
    ulong ans=0;
    foreach(i;0..n)
        ans+=a[i]*d[i];
    writeln(ans);
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n,q;
    readf!"%d %d"(n,q);
    readln;
    int[] a=readln.splitter
                  .map!(to!int)
                  .array,
        d=new int[n];
    int l,r;
    while(q--){
        readf!"%d %d"(l,r);
        readln;
        l--;
        d[l]++;
        if(r<n) d[r]--;
    }
    foreach(i;1..n)
        d[i]+=d[i-1];
    sort!"a>b"(d);
    sort!"a>b"(a);
    ulong ans=0;
    foreach(i;0..n)
        ans+=a[i]*d[i];
    writeln(ans);
}


import std.stdio;
import std.string : split, strip;
import std.algorithm : chunkBy, map, maxElement, sum;
import std.conv : to;

void main() {
  int t;
  readf!"%d\n"(t);

  foreach (i; 0..t) {
    int n;
    readf!"%d\n"(n);

    auto s = readln.strip.split
      .map!(to!long)
      .chunkBy!((a, b) => (a > 0) == (b > 0))
      .map!maxElement
      .sum;
    writeln(s);
  }
}

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!long;
        long[][] as = [[]];
        size_t i;
        long last = 0;
        foreach (a; readln.split.to!(long[])) {
            if (last*a < 0) {
                sort!"a > b"(as[i]);
                ++i;
                as ~= [[]];
            }
            as[i] ~= a;
            last = a;
        }
        sort!"a > b"(as[i]);
        long s;
        foreach (a; as) s += a[0];
        writeln(s);
    }
}

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

void main ()
{
	readln;
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		readln
		    .splitter
		    .map !(to !(int))
		    .chunkBy !(q{(a > 0) == (b > 0)})
		    .map !(maxElement)
		    .sum (0L)
		    .writeln;
	}
}

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(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

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

		long m = a[0];
		foreach (i; 1..n)
		{
			if (a[i] * a[i-1] < 0)
			{
				ans[ti] += m;
				m = long.min;
			}
			m.chmax(a[i]);
		}
		ans[ti] += m;
	}

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


import std.algorithm,std.conv,std.math,std.numeric,std.range,std.stdio;
void main()
{
    int n,d;
    readf("%s ",&n);
    d=min(50,n-2);
    auto a=n.iota.map!(_=>readln.split.map!(to!int).array).array,m=d.iota.map!(i=>pow(2.,d-i)-1).array;
    a~=a,m[]/=(d<50)?pow(2.,n)-1-n-n*.5*(n-1):m.sum*2;
    writef("%.9f",sum(n.iota.map!(i=>sum(iota(i+1,i+d+1).map!(j=>m[j-i-1]*((a[i][0]-a[j][0])*1.*(a[i][1]+a[j][1])-gcd(abs(a[i][0]-a[j][0]),abs(a[i][1]-a[j][1])))))))/2+1);
}

import std.algorithm, std.math, std.numeric, std.stdio, std.typecons;
immutable int MUCH = 70;
void main ()
{
    int n;
    while (readf (" %s", &n) > 0)
    {
        alias Point = Tuple !(int, "x", int, "y");
        auto a = new Point [n];
        foreach (ref p; a)
            readf (" %s %s", &p.x, &p.y);
        a ~= a;

        int d = min (MUCH, n - 2);
        auto mult = new real [d];
        foreach (i; 0..d)
            mult[i] = pow (2.0, d - i) - 1.0;
        mult[] /= (d < MUCH) ?
            pow (2.0, n) - 1 - n - n * 0.5 * (n - 1) :
            sum (mult) * 2;

        real res = 0;
        foreach (i; 0..n)
            foreach (k; 0..d)
            {
                int j = i + k + 1;
                real s = a[i].x - a[j].x;
                s *= a[j].y + a[i].y;
                s -= gcd (abs (a[j].x - a[i].x),
                    abs (a[j].y - a[i].y));
                res += mult[k] * s;
            }
        writefln ("%.20f", res / 2 + 1);
    }
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio;
void main ()
{
    int n, d, j;
    readf (" %s ", &n);
    auto a = n.iota.map !(_ => readln.split.map !(to !(int)).array).array;
    a ~= a;
    d = min (50, n - 2);
    auto mult = d.iota.map !(i => pow (2.0, d - i) - 1.0).array;
    mult[] /= (d < 50) ? pow (2.0, n) - 1 - n - n * 0.5 * (n - 1) : sum (mult) * 2;
    real res = 0;
    res = sum (n.iota.map !(i => sum (iota (i + 1, i + d + 1).map !(j => mult[j - i - 1] *
        ((a[i][0] - a[j][0]) * 1.0 * (a[i][1] + a[j][1]) - gcd (abs (a[i][0] - a[j][0]), abs (a[i][1] - a[j][1])))))));
    writefln ("%.20f", res / 2 + 1);
}

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

immutable int MUCH = 70;

int euclid (int a, int b)
{
	a = abs (a);
	b = abs (b);
	while (a && b)
	{
		a %= b;
		if (a)
		{
			b %= a;
		}
	}
	return a + b;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		alias Point = Tuple !(int, "x", int, "y");
		auto a = new Point [n];
		foreach (ref p; a)
		{
			readf (" %s %s", &p.x, &p.y);
		}
		a ~= a;

		int d = min (MUCH, n - 2);
		auto mult = new real [d];
		foreach (i; 0..d)
		{
			real k = d - i;
			mult[i] = pow (2.0, k) - 1.0;
		}
		real ts;
		if (d < MUCH)
		{
			real total = pow (2.0, n);
			real skip = 1;
			skip += n;
			skip += n * 0.5 * (n - 1);
			ts = total - skip;
		}
		else
		{
			ts = sum (mult) * 2;
		}
		debug {writeln ("ts = ", ts);}
		mult[] /= ts;

		real res = 0.0;
		foreach (i; 0..n)
		{
			foreach (k; 0..d)
			{
				int j = i + k + 1;
				real s = (a[i].x - a[j].x);
				s *= (a[j].y + a[i].y);
				int t = euclid (a[j].x - a[i].x,
				    a[j].y - a[i].y);
//				debug {writeln (s, ' ', t, ' ', mult[k], ' ',
//				    (s - t) * mult[k]);}
				s -= t;
				res += mult[k] * s;
			}
		}
		debug {writeln ("raw res = ", res);}
		res /= 2;
		res += 1;
		writefln ("%.20f", res);
	}
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio;
void main()
{
    int n, d;
    readf(" %s ", &n);
    d = min(50, n - 2);
    auto a = n.iota.map!(_ => readln.split.map!(to!int).array).array, mult = d.iota.map!(i => pow(2.0, d - i) - 1).array;
    a ~= a;
    mult[] /= (d < 50) ? pow (2.0, n) - 1 - n - n * .5 * (n - 1) : mult.sum * 2;
    writef("%.9f", sum(n.iota.map!(i => sum(iota(i + 1, i + d + 1).map!(j => mult[j - i - 1] * ((a[i][0] - a[j][0]) * 1. * (a[i][1] + a[j][1]) - gcd(abs(a[i][0] - a[j][0]), abs(a[i][1] - a[j][1]))))))) / 2 + 1);
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio, std.string;
void main ()
{
    int n, d, j;
    readf (" %s ", &n);
    int [] [] a;
    foreach (i; 0..n)
        a ~= readln.split.map !(to !(int)).array;
    a ~= a;
    d = min (70, n - 2);
    auto mult = d.iota.map !(i => pow (2.0, d - i) - 1.0).array;
    mult[] /= (d < 70) ? pow (2.0, n) - 1 - n - n * 0.5 * (n - 1) : sum (mult) * 2;
    real res = 0;
    foreach (i; 0..n)
        foreach (k; 0..d)
        {
            j = i + k + 1;
            real s = a[i][0] - a[j][0];
            s *= a[i][1] + a[j][1];
            s -= gcd (abs (a[i][0] - a[j][0]), abs (a[i][1] - a[j][1]));
            res += mult[k] * s;
        }
    writefln ("%.20f", res / 2 + 1);
}

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

immutable int MUCH = 100;

int euclid (int a, int b)
{
	a = abs (a);
	b = abs (b);
	while (a && b)
	{
		a %= b;
		if (a)
		{
			b %= a;
		}
	}
	return a + b;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		alias Point = Tuple !(int, "x", int, "y");
		auto a = new Point [n];
		foreach (ref p; a)
		{
			readf (" %s %s", &p.x, &p.y);
		}
		a ~= a;

		int d = min (MUCH, n - 2);
		auto mult = new real [d];
		foreach (i; 0..d)
		{
			real k = d - i;
			mult[i] = pow (2.0, k) - 1.0;
		}
		real ts;
		if (d < MUCH)
		{
			real total = pow (2.0, n);
			real skip = 1;
			skip += n;
			skip += n * 0.5 * (n - 1);
			ts = total - skip;
		}
		else
		{
			ts = sum (mult);
		}
		debug {writeln ("ts = ", ts);}
		mult[] /= ts;

		real res = 0.0;
		foreach (i; 0..n)
		{
			foreach (k; 0..d)
			{
				int j = i + k + 1;
				real s = (a[i].x - a[j].x);
				s *= (a[j].y + a[i].y);
				int t = euclid (a[j].x - a[i].x,
				    a[j].y - a[i].y);
//				debug {writeln (s, ' ', t, ' ', mult[k], ' ',
//				    (s - t) * mult[k]);}
				s -= t;
				res += mult[k] * s;
			}
		}
		debug {writeln ("raw res = ", res);}
		res /= 2;
		res += 1;
		writefln ("%.20f", res);
	}
}

import std.algorithm, std.conv, std.math, std.numeric, std.range, std.stdio;
void main()
{
    int n, d;
    readf(" %s ", &n);
    auto a = n.iota.map!(_ => readln.split.map!(to!int).array).array;
    a ~= a;
    d = min (50, n - 2);
    auto mult = d.iota.map !(i => pow (2.0, d - i) - 1).array;
    mult[] /= (d < 50) ? pow (2.0, n) - 1 - n - n * .5 * (n - 1) : 2;
    writef("%.9f", sum(n.iota.map!(i => sum(iota(i + 1, i + d + 1).map!(j => mult[j - i - 1] * ((a[i][0] - a[j][0]) * 1. * (a[i][1] + a[j][1]) - gcd(abs(a[i][0] - a[j][0]), abs(a[i][1] - a[j][1]))))))) / 2 + 1);
}

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

immutable int MUCH = 50;

int euclid (int a, int b)
{
	a = abs (a);
	b = abs (b);
	while (a && b)
	{
		a %= b;
		if (a)
		{
			b %= a;
		}
	}
	return a + b;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		alias Point = Tuple !(int, "x", int, "y");
		auto a = new Point [n];
		foreach (ref p; a)
		{
			readf (" %s %s", &p.x, &p.y);
		}
		a ~= a;

		int d = min (MUCH, n - 2);
		auto mult = new real [d];
		foreach (i; 0..d)
		{
			real k = d - i;
			mult[i] = pow (2.0, k) - 1.0;
		}
		real ts;
		if (d < MUCH)
		{
			real total = pow (2.0, n);
			real skip = 1;
			skip += n;
			skip += n * 0.5 * (n - 1);
			ts = total - skip;
		}
		else
		{
			ts = sum (mult);
		}
		debug {writeln ("ts = ", ts);}
		mult[] /= ts;

		real res = 0.0;
		foreach (i; 0..n)
		{
			foreach (k; 0..d)
			{
				int j = i + k + 1;
				real s = (a[i].x - a[j].x);
				s *= (a[j].y + a[i].y);
				int t = euclid (a[j].x - a[i].x,
				    a[j].y - a[i].y);
//				debug {writeln (s, ' ', t, ' ', mult[k], ' ',
//				    (s - t) * mult[k]);}
				s -= t;
				res += mult[k] * s;
			}
		}
		debug {writeln ("raw res = ", res);}
		res /= 2;
		res += 1;
		writefln ("%.20f", res);
	}
}


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

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

    auto a = -(10^^9) ~ readln.split.map!( to!int ).array;
    a.sort();
    debug { writeln(a); }

    auto vs = DList!int(0);

    auto idxleft = 0;
    foreach ( i; k..n+1 ) {
        debug { write(i, ' '); }

        while ( a[ idxleft+1 ] < a[ i ] - d ) ++idxleft;
        while ( !vs.empty() && vs.front() < idxleft ) vs.removeFront();
        bool ok = !vs.empty() && vs.front() <= i - k;

        if ( ok ) vs ~= i;

        debug { writeln( idxleft, ' ', i - k, ' ', ok, ' ', vs.array ); }
    }

    writeln( !vs.empty() && vs.back() == n ? "YES" : "NO" );
}

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

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

    auto a = -(10^^9) ~ readln.split.map!( to!int ).array;
    auto sorted = a.sort();
    auto vs = [0];

    foreach ( i; k..n+1 ) {
        auto reach = sorted.lowerBound( a[ i ] - d );
        auto idxleft = reach.length - 1;
        auto vsRange = vs.assumeSorted();
        auto idcs = vsRange.equalRange( idxleft ).chain( vsRange.upperBound( idxleft ) );

        bool ok = !idcs.empty() && idcs.front() <= i - k;

        debug { write( idxleft, ' ', idcs, ' ', i - k, ' ', ok ); }

        if ( ok ) vs ~= i;
    }

    debug { writeln( vs ); }
    writeln( vs.back() == n ? "YES" : "NO" );
}

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

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

    auto a = readln.split.map!( to!int ).array;
    a = -(10^^9) ~ a;
    auto sorted = a.sort();
    debug { writeln( sorted ); }

    auto vs = [0];

    foreach ( i; k..n+1 ) {
        debug { write(i, ' '); }

        auto reach = sorted.lowerBound( a[ i ] - d );
        auto idxleft = reach.length - 1;
        auto idcs = vs.assumeSorted().equalRange( idxleft ).chain( vs.assumeSorted().upperBound( idxleft ) );

        debug { write( idxleft, ' ', idcs, ' ' ); }

        bool ok = !idcs.empty() && idcs.front() <= i - k;

        debug { writeln( i - k, ' ', ok ); }

        if ( ok ) vs ~= i;
    }
    debug { writeln( vs ); }
    writeln( vs.back() == n ? "YES" : "NO" );
}


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)
	{
		int a, b, c, x, y;
		readf !(" %s %s %s %s %s") (a, b, c, x, y);
		bool ok = (a + c >= x && b + c >= y && a + b + c >= x + y);
		writeln (ok ? "YES" : "NO");
	}
}

// cheese-cracker [2022-05-05]

void solve(){
    long a = scan;
    long b = scan;
    long c = scan;
    long x = scan;
    long y = scan;
    long left = max(x - a, 0L) + max(y - b, 0);
    if(c >= left){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm;
import std.container, std.math, std.numeric, std.functional;
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; } }


immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto s = readString;
	int[] p0, p1;
	foreach(i, ch; s)
	{
		if (ch == '0')
		{
			p0 ~= cast(int) i;
		}
		else
		{
			p1 ~= cast(int) i;
		}
	}
	int j = 0;
	int[] ans;
	debug writeln(p0, p1);
	while (p0.length>0 && p1.length>0
	       && p0[$-1] > p1[0])
	{
		ans ~= p0[$-1];
		ans ~= p1[0];
		p0 = p0[0 .. $ - 1];
		p1 = p1[1 .. $];
	}
	if (ans.length == 0) return writeln(0);
	writeln(1);
	sort(ans);
	ans.length.write(" ");
	foreach(v; ans) write(v+1, " ");
	writeln;
}

//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
	{
		popChar;
	}
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

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

void solve(){
    int n = scan!int;
    auto arr = new long[](n);
    auto word = scan!(dchar[]);
    long cnt = 0;
    for(int i = 0; i < n; ++i){
        if(word[i] == '1'){
            ++cnt;
            ++arr[i];
        }
    }
    for(int i = n-1; i >= n - cnt; --i){
        arr[i] = !arr[i];
    }
    ll[] res;
    for(int i = 0; i < n; ++i){
        if(arr[i]){
            res ~= i;
        }
    }
    if(res.length){
        writeln(1);
        write(res.length, " ");
        res.each!((k) => write(k+1, " "));
        writeln;
    }else{
        writeln(0);
    }
}

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;
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;
import std.conv;
import std.string;
import std.algorithm;
import std.math;

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n;
        readf!"%d\n"(n);

        string a;
        readf!"%s\n"(a);

        int zeros = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == '0') zeros ++;
        }

        int[] indices = new int[0];

        for (int i = 0; i < n; i++) {
            if (a[i] == '0') {
                zeros --;
            } else {
                zeros --;
                indices ~= [i];
            }
            if (zeros <= 0) {
                // add the next indices.length zeros
                int to_add = cast(int) indices.length;
                for (int j = i+1; j < n; j++) {
                    if (a[j] == '0') {
                        indices ~= [j];
                        to_add --;
                    }
                    if (to_add <= 0) break;
                }

                break;
            }
        }

        if (indices.length != 0) writeln(1);
        write(indices.length);
        write(' ');
        for (int i = 0; i < indices.length; i++) {
            write(indices[i] + 1);
            write(' ');
        }
        writeln();

    }
}

/*
100001010101111000110011
|    | | | |   |||  ||
000000000000111111111111

1001010100100010101010111111111010101110011
|  | | |  |   | |
0000000000000000011111111111111111111111111

101
|
011

11111111111111111101
|
01111111111111111111*/

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

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n;
        readf!"%d\n"(n);

        string a;
        readf!"%s\n"(a);

        int zeros = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == '0') zeros ++;
        }

        int[] indices = new int[0];

        for (int i = 0; i < n; i++) {
            if (a[i] == '0') {
                zeros --;
            } else {
                zeros --;
                indices ~= [i];
            }
            if (zeros <= 0) {
                // add the next indices.length zeros
                int to_add = cast(int) indices.length;
                for (int j = i+1; j < n; j++) {
                    if (a[j] == '0') {
                        indices ~= [j];
                    }
                    to_add --;
                    if (to_add <= 0) break;
                }

                break;
            }
        }

        if (indices.length != 0) writeln(1);
        write(indices.length);
        write(' ');
        for (int i = 0; i < indices.length; i++) {
            write(indices[i] + 1);
            write(' ');
        }
        writeln();

    }
}

/*
100001010101111000110011
|    | | | |   |||  ||
000000000000111111111111

1001010100100010101010111111111010101110011
|  | | |  |   | |
0000000000000000011111111111111111111111111

101
|
011

11111111111111111101
|
01111111111111111111*/


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 numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const S = readToken();

        auto cs = S.dup;
        foreach (_; 0 .. N - 1) foreach (i; 0 .. N - 1) {
          if (cs[i] > cs[i + 1]) {
            swap(cs[i], cs[i + 1]);
          }
        }
        int ans;
        foreach (i; 0 .. N) {
          if (S[i] != cs[i]) {
            ++ans;
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

import std.stdio;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;

void main()
{
	int t = readInt!int;
	while (t--)
	{
		solve();
	}
}

void solve()
{
	int n = readInt!int;
	char[] s = cast(char[])readString;
	int[] si = s.map!(c => cast(int)c).array;
	int[] ssi = si.dup; sort(ssi);
	iota(0, n).count!(i => si[i] != ssi[i]).writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

void read(S...)(ref S args) {
  auto input = readln.split;
  assert(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

void main() {
  int T;
  read(T);

  while (T--) {
    int n;
    read(n);
    auto s = readln.strip.array();
    auto cp = s.dup;
    sort(s);

    int t = 0;

    foreach (i; 0 .. n) {
      if (s[i] != cp[i]) t++;
    }

    writeln(t);
  }
}

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)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

//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 n = RD!int;
		auto s = RD!string;

		long[] a;
		foreach (c; s)
		{
			a ~= c - 'a';
		}
		a.sort;
		foreach (i; 0..n)
		{
			auto x = s[i]-'a';
			if (a[i] != x)
				++ans[ti];
		}
	}

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

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    auto a = readln.strip.split("");
    auto sa = a.dup.sort;
    int result;
    foreach (i ; 0 .. a.length) {
      if (a[i] != sa[i])
        result++;
    }
    writeln(result);
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;
		auto t = s.dup;
		sort (t.byChar);
		writeln (zip (s, t).count !(q{a[0] != a[1]}));
	}
}


import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int mins(int v)
  {
    if (ch[v].len == 0)
      return 0;
    auto cr = ch[v].map!mins;
    final switch(op[v])
      {
      case 0:
        return cast(int)(cr.sum + ch[v].len - 1);
      case 1:
        return cast(int)(cr.fold!min);
      }
  }
  writeln(cast(int)ch.map!len.count(0) - mins(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int nl = 0;
  int mins(int v)
  {
    if (ch[v].len == 0)
      {
        nl++;
        return 0;
      }
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  auto pres = mins(0);
  writeln(nl - pres);
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!int;
  auto operation = next!int(n);
  debug writeln(operation);
  auto children = new int[][](n);
  foreach(i; 1 .. n)
    children[next!int - 1] ~= i;
  debug writeln(children);
  auto noleafs = cast(int) children.count!(child => child.length == 0);
  int dfs(int node)
  {
    if (children[node].length == 0)
      return 0;
    if (operation[node] == 0) // min
      return cast(int)(children[node].map!(child => dfs(child)).sum + children[node].length - 1);
    else // max
      return cast(int)(children[node].map!(child => dfs(child)).fold!min);
  }
  writeln(noleafs - dfs(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int mins(int v)
  {
    if (ch[v].len == 0)
      return 0;
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  writeln(cast(int)ch.map!len.count(0) - mins(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  int[300_000] sop;
  int[][300_000] sch;
  auto n = nt!int;
  auto op = sop[0 .. n];
  foreach(i; 0 .. n)
    op[i] = nt!int;
  auto ch = sch[0 .. n];
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int nl = 0;
  int mins(int v)
  {
    if (ch[v].len == 0)
      {
        nl++;
        return 0;
      }
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  auto pres = mins(0);
  writeln(nl - pres);
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!int;
  auto operation = next!int(n);
  debug writeln(operation);
  auto children = new int[][](n);
  foreach(i; 1 .. n)
    children[next!int - 1] ~= i;
  debug writeln(children);
  auto noleafs = cast(int) children.count!(child => child.length == 0);
  int dfs(int node)
  {
    if (children[node].length == 0)
      {
        return 0;
      }
    if (operation[node] == 0) // min
      {
        int acc = 0;
        foreach(child; children[node])
          acc += dfs(child);
        acc += children[node].length - 1;
        return acc;
      }
    else // max
      {
        int m = int.max;
        foreach(child; children[node])
          m = min(m, dfs(child));
        return m;
      }
  }
  writeln(noleafs - dfs(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byLine;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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, core.stdc.string;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto P = -1 ~ readln.split.map!(a => a.to!int - 1).array;

    auto G = new int[][](N);
    foreach (i; 1..N) G[i] ~= P[i], G[P[i]] ~= i;
    int k = 0;

    int dfs(int n, int p) {
        if (p != -1 && G[n].length == 1) {
            k += 1;
            return 0;
        } else if (A[n] == 0) {
            int res = 0;
            foreach (m; G[n]) {
                if (m == p) continue;
                res += max(1, dfs(m, n));
            }
            return res;
        } else {
            int res = 1 << 29;
            foreach (m; G[n]) {
                if (m == p) continue;
                res = min(res, dfs(m, n));
            }
            return res;
        }
    }

    int ans = dfs(0, -1);
    if (ans == 0) {
        k.writeln;
    } else {
        (k - ans + 1).writeln;
    }
}

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

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

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

    auto f = readln.chomp.split.map!(to!int).map!(x => x-1).array;

    auto g = new int[][] (n);
    foreach (i; 1 .. n) {
       g[f[i-1]] ~= i;
    }

    debug { g.writeln; }

    int leaves = 0;
    int dfs(int v) {
        if (g[v].empty()) {
            leaves += 1;
            return 1;
        }

        int ans = dfs(g[v].front);
        foreach (u; g[v].dropOne) {
            int res = dfs(u);
            if (mnmx[v] == 0) { ans += res; }
            else { ans = min(ans, res); }
        }

        debug { writeln(v, ' ', ans); }

        return ans;
    }

    int ans = dfs(0);

    ans = leaves - ans + 1;

    ans.writeln;
}

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

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

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

    auto f = readln.chomp.split.map!(to!int).map!(x => x-1).array;

    auto g = new int[][] (n);
    foreach (i; 1 .. n) {
       g[f[i-1]] ~= i;
    }

    debug { g.writeln; }

    int leaves = 0;
    int dfs(int v, ref int leaves) {
        if (g[v].empty()) {
            leaves += 1;
            return 1;
        }

        int ans = dfs(g[v].front, leaves);
        foreach (u; g[v].dropOne) {
            int res = dfs(u, leaves);
            if (mnmx[v] == 0) { ans += res; }
            else { ans = min(ans, res); }
        }

        debug { writeln(v, ' ', ans); }

        return ans;
    }

    int ans = dfs(0, leaves);

    ans = leaves - ans + 1;

    ans.writeln;
}

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

const long mod = 1_000_000_007;

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}

	int n = read.to!int;

	Node[] nodes;
	foreach(i; 0 .. n){
		bool isMax = read.to!int.to!bool;
		nodes ~= new Node(i, isMax);
	}

	foreach(i; 1 .. n){
		int j = read.to!int - 1;
		nodes[i].parent = nodes[j];
		nodes[j].kids ~= nodes[i];
	}

	int v = nodes[0].solve;

	auto leafcount = nodes.count!(x => x.kids.length == 0);
	(leafcount + 1 - v).writeln;

	print!1(nodes.map!(x => x.solve()));

}

/*

Tree DP.
Each node holds "min of at least how many leaves comes here?".
For example, if a node has only "max" descenders its value will be 1.

If a node is max node, its value is the min of its direct sons.
If a node is min node, its value is the sum of its direct sons.

*/

class Node{
	bool isMax;
	long _b, _w;
	int id;
	Node[] nodes;
	Node[] kids;
	Node parent;
	bool isVisited;
	this(int id, bool isMax){
		this.id = id;
		this.isMax = isMax;
	}
	int solve(){
		int res;
		if(kids.length == 0) return 1;
		else if(isMax){
			res = 900_000;
			foreach(nd; kids) res = min(res, nd.solve);
		}
		else{
			res = 0;
			foreach(nd; kids) res += nd.solve;
		}
		return res;
	}
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;

auto length(T)(T t) {return t.length;}
alias len = length;
alias nt = next;

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = nt!int;
  auto op = nt!int(n);
  auto ch = new int[][](n);
  foreach(i; 1 .. n) ch[nt!int - 1] ~= i;
  int mins(int v)
  {
    if (ch[v].len == 0)
      return 0;
    final switch(op[v])
      {
      case 0:
        int res = ch[v].length - 1;
        foreach(c; ch[v])
          res += mins(c);
        return res;
      case 1:
        int res = int.max;
        foreach(c; ch[v])
          res = min(res, mins(c));
        return res;
      }
  }
  writeln(cast(int)ch.map!len.count(0) - mins(0));
}

// INPUT

enum InputStyle
  {
    byChunk, byLine
  };
enum inputStyle = InputStyle.byChunk;

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 1024 * 1024 * 8;
    const wordSize = 4096;
    char[chunkSize] chunkBuff;
    char[] currChunk;
    void renewChunk()
    {
      if (inputFile.eof)
        currChunk = null;
      else
        currChunk = inputFile.rawRead(chunkBuff);
    }
    char[wordSize] wordBuff;
    char[] word;
    string popWord()
    {
      if (currChunk.length == 0)
        renewChunk;
      assert(currChunk.length > 0);
      while (currChunk[0].isWhite)
        {

          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            renewChunk;
        }
      word = wordBuff[0 .. 0];
      while (!currChunk[0].isWhite)
        {
          word.length++;
          word[$ - 1] = currChunk[0];
          currChunk = currChunk[1 .. $];
          if (currChunk.length == 0)
            {
              renewChunk;
              if (currChunk.length == 0)
                return cast(string) word;
            }
        }
      return cast(string) word;
    }
  }
 else
   {
     DList!string _words;
     string popWord()
     {
       while (_words.empty)
         {
           foreach(w; inputFile.readln.split)
             {
               _words.insertBack(w);
             }
         }
       auto word = _words.front;
       _words.removeFront;
       return word;
     }
   }
T next(T)()
{
  return to!T(popWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}


import std.bigint,std.container,std.array,std.algorithm,std.conv,
std.functional,std.math,std.numeric,std.string,std.range,std.complex,
std.typecons,std.regex,std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias gcd=std.numeric.gcd;
immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
	{
		pair!(X,Y) pp;
		pp.fi=x_;
		pp.se=y_;
		return pp;
	}
	big gcd(big a,big b)
	{
		while(b)
		{
			a%=b;
			swap(a,b);
		}
		return a;
	}
	X binpow(X,Y)(X base,Y exp)if(is(typeof(exp>>1)))
	{
		if(exp<0)return X(0);
		X res=X(1);
		while(exp>0)
		{
			if(exp&1)res*=base;
			base*=base;
			exp>>=1;
		}
		return res;
	}
	auto binpow(X,Y,Z)(X base,Y exp,in Z mm)if(is(typeof(exp>>1)) && is(typeof(base%mm)))
	{
		if(mm==0) return binpow(base,exp);
		if(exp<0)return X(0);
		auto res=X(1)%mm;
		while(exp>0)
		{
			if(exp&1)res=(res*base)%mm;
			base*=base;
			base%=mm;
			exp>>=1;
		}
		return res;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~"\n", ptrs)==ptrs.length;}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	loop:while(read(&n))
	{
		auto a=arread!int;

		sort(a);int m=int.max,k=0;
		foreach(i;0..a.length-1)
		{
			if(a[i+1]-a[i]<m)
			{
				m=a[i+1]-a[i];
				k=1;
			}
			else if(a[i+1]-a[i]==m)k++;
		}
		writeln(m,' ',k);
	}
	debug system("pause");
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;
		sort (a);
		auto b = (n - 1).iota.map !(i => a[i + 1] - a[i]).array;
		auto m = b.reduce !(min);
		writeln (m, " ", b.count (m));
	}
}


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(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T 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 k = RD!int;
		auto a = RDA;

		foreach (i; 1..n)
		{
			if (a[i] > a[i-1])
				++ans[ti];
		}
		if (k == 1)
		{
			if (ans[ti] != 0)
				ans[ti] = -1;
			else
				ans[ti] = 1;
			continue;
		}
		else if (ans[ti] == 0)
			ans[ti] = 1;
		ans[ti] = (ans[ti]+k-2) / (k-1);
	}

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

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 numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const K = readInt();
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt();
        }

        int ans;
        if (N == 1) {
          ans = 1;
        } else {
          int num;
          foreach (i; 0 .. N - 1) {
            if (A[i] < A[i + 1]) {
              ++num;
            }
          }
          if (K == 1) {
            if (num == 0) {
              ans = 1;
            } else {
              ans = -1;
            }
          } else {
            ans = (num + (K - 1) - 1) / (K - 1);
            chmax(ans, 1);
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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 numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const K = readInt();
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt();
        }

        int ans;
        if (N == 1) {
          ans = 0;
        } else {
          int num;
          foreach (i; 0 .. N - 1) {
            if (A[i] < A[i + 1]) {
              ++num;
            }
          }
          if (K == 1) {
            if (num == 0) {
              ans = 1;
            } else {
              ans = -1;
            }
          } else {
            ans = (num + (K - 1) - 1) / (K - 1);
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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 numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const K = readInt();
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt();
        }

        int ans;
        if (N == 1) {
          ans = 0;
        } else {
          int num;
          foreach (i; 0 .. N - 1) {
            if (A[i] < A[i + 1]) {
              ++num;
            }
          }
          if (K == 1) {
            if (num == 0) {
              ans = 1;
            } else {
              ans = -1;
            }
          } else {
            ans = (num + (K - 1) - 1) / (K - 1);
            chmax(ans, 1);
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T 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 k = RD!int;
		auto a = RDA;

		foreach (i; 1..n)
		{
			if (a[i] > a[i-1])
				++ans[ti];
		}
		if (k == 1)
		{
			if (ans[ti] != 0)
				ans[ti] = -1;
			else
				ans[ti] = 1;
			continue;
		}
		ans[ti] = (ans[ti]+k-2) / (k-1);
	}

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


import std.stdio;
import std.typecons;
import core.stdc.stdio : scanf;
import core.bitop;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

struct FindAncestor {
    int[][] G;
    int N;
    int root;
    int[] parent;
    int[] depth;        // depth[v]: distance from root
    int[][] ancestor;   // ancestor[v][i]: v's {2^i}-th ancestor

    void init_parent() {
        void dfs(int v, int prev, int d) {
            parent[v] = prev;
            depth[v] = d;
            foreach (next; G[v]) {
                if (next == prev) continue;
                dfs(next, v, d + 1);
            }
        }
        dfs(root, -1, 0);
    }

    void init_ancestor() {
        int L = bsr(N) + 1;
        this.ancestor = new int[][](N, L);
        foreach (ref e; ancestor) e[] = -1;
        for (int v = 0; v < N; v++) {
            ancestor[v][0] = parent[v];
        }
        for (int i = 1; i < L; i++) {
            for (int v = 0; v < N; v++) {
                if (ancestor[v][i-1] == -1) continue;
                ancestor[v][i] = ancestor[ ancestor[v][i-1] ][i-1];
            }
        }
    }

    this(int[][] G, int root) {
        this.N = cast(int)G.length;
        this.G = G;
        this.root = root;
        this.depth = new int[N];
        this.parent = new int[N];
        init_parent();
        init_ancestor();
    }

    int query(int v, int k) {
        // return v's k-th ancestor
        for (int i = 0; i <= bsr(N); i++) {
            if (k & (1<<i)) {
                v = ancestor[v][i];
                if (v == -1) return v;
            }
        }
        return v;
    }
}

void solve() {
    int N = readln.chomp.to!int;
    int[][] G = new int[][N];
    int[][] A = new int[][N];
    int[][] B = new int[][N];
    int[] P = new int[N]; P[] = -1;
    for (int j = 1; j < N; j++) {
        int p, a, b; readf("%d %d %d\n", &p, &a, &b);
        p--;
        G[p] ~= j;
        A[p] ~= a;
        B[p] ~= b;
        P[j] = p;
    }
    long[] Sa = new long[N];
    long[] Sb = new long[N];
    void dfs(int v, int[][] c, long[] Sc, long s) {
        Sc[v] = s;
        foreach (i, n; G[v]) {
            auto cost = c[v][i];
            dfs(n, c, Sc, s + cost);
        }
    }
    dfs(0, A, Sa, 0);
    dfs(0, B, Sb, 0);

    auto ancestor = FindAncestor(G, 0);
    int[] ans = new int[N];
    for (int v = 0; v < N; v++) {
        long L = Sa[v];
        int lb = 0, ub = N;

        bool C(int x) {
            int nv = ancestor.query(v, x);
            return nv < 0 || L >= Sb[nv];
        }

        if (C(lb)) {
            ans[v] = ancestor.depth[v];
        } else {
            while (lb + 1 < ub) {
                int mid = (lb + ub) / 2;
                (C(mid) ? ub : lb) = mid;
            }
            ans[v] = ancestor.depth[ancestor.query(v, ub)];
        }
    }
    writefln("%(%s %)", ans[1..$]);
}

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

import std.stdio;
import std.typecons;
import core.stdc.stdio : scanf;
import core.bitop;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

struct Tree {
    int[][] G;
    int N;
    int root;
    int[] parent;
    int[] depth;        // depth[v]: distance from root
    int[][] ancestor;   // ancestor[v][i]: v's {2^i}-th ancestor

    void init_parent() {
        void dfs(int v, int prev, int d) {
            parent[v] = prev;
            depth[v] = d;
            foreach (next; G[v]) {
                if (next == prev) continue;
                dfs(next, v, d + 1);
            }
        }
        dfs(root, -1, 0);
    }

    void init_ancestor() {
        int L = bsr(N) + 1;
        this.ancestor = new int[][](N, L);
        foreach (ref e; ancestor) e[] = -1;
        for (int v = 0; v < N; v++) {
            ancestor[v][0] = parent[v];
        }
        for (int i = 1; i < L; i++) {
            for (int v = 0; v < N; v++) {
                if (ancestor[v][i-1] == -1) continue;
                ancestor[v][i] = ancestor[ ancestor[v][i-1] ][i-1];
            }
        }
    }

    this(int[][] G, int root) {
        this.N = cast(int)G.length;
        this.G = G;
        this.root = root;
        this.depth = new int[N];
        this.parent = new int[N];
        init_parent();
        init_ancestor();
    }

    int kth_ancestor(int v, int k) {
        // return v's k-th ancestor
        for (int i = 0; i <= bsr(N); i++) {
            if (k & (1<<i)) {
                v = ancestor[v][i];
                if (v == -1) return v;
            }
        }
        return v;
    }
}

void solve() {
    int N = readln.chomp.to!int;
    int[][] G = new int[][N];
    int[][] A = new int[][N];
    int[][] B = new int[][N];
    int[] P = new int[N]; P[] = -1;
    for (int j = 1; j < N; j++) {
        int p, a, b; readf("%d %d %d\n", &p, &a, &b);
        p--;
        G[p] ~= j;
        A[p] ~= a;
        B[p] ~= b;
        P[j] = p;
    }
    long[] Sa = new long[N];
    long[] Sb = new long[N];
    void dfs(int v, int[][] c, long[] Sc, long s) {
        Sc[v] = s;
        foreach (i, n; G[v]) {
            auto cost = c[v][i];
            dfs(n, c, Sc, s + cost);
        }
    }
    dfs(0, A, Sa, 0);
    dfs(0, B, Sb, 0);

    auto ancestor = Tree(G, 0);
    int[] ans = new int[N];
    for (int v = 0; v < N; v++) {
        long L = Sa[v];
        int lb = 0, ub = N;

        bool C(int x) {
            int nv = ancestor.kth_ancestor(v, x);
            return nv < 0 || L >= Sb[nv];
        }

        if (C(lb)) {
            ans[v] = ancestor.depth[v];
        } else {
            while (lb + 1 < ub) {
                int mid = (lb + ub) / 2;
                (C(mid) ? ub : lb) = mid;
            }
            ans[v] = ancestor.depth[ancestor.kth_ancestor(v, ub)];
        }
    }
    writefln("%(%s %)", ans[1..$]);
}

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


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

  int n;
  rd(n);
  const long mod = 998244353;

  long powmod(long b, long e) {
    if (e == 0)
      return 1;
    else if (e == 1)
      return b % mod;
    else if (e & 1)
      return b * powmod(b, e - 1) % mod;
    else
      return powmod(b * b % mod, e / 2);
  }

  auto fac = new long[](n + 1), inv = new long[](n + 1);
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = powmod(fac[i], mod - 2);
  }
  long ans = n * fac[n] % mod;
  foreach (i; 1 .. n) {
    ans -= fac[n] * inv[i] % mod;
    if (ans < 0) {
      ans += mod;
    }
  }
  writeln(ans);
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  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;

immutable int mod = 998_244_353;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int res = 0;
		int cur = n % mod;
		int prev = cur;
		int k = 0;
		foreach_reverse (i; 2..n)
		{
			k += 1;
			cur = (cur * 1L * i) % mod;
			debug {writeln (k, " * (", cur, " - ", prev, ")");}
			res = (res + (cur - prev + mod) * 1L * k) % mod;
//			res = (res + (cur - prev) * 1L * k) % mod;
			prev = cur;
		}
		res = (res + cur) % mod;
		writeln (res);
	}
}

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

enum mod = 998244353L;

void main() {
    int n;
    scan(n);

    auto f = new long[](n + 1);
    f[0] = f[1] = 1;
    foreach (i ; 2 .. n + 1) {
        f[i] = f[i-1] * i % mod;
    }
    auto rf = new long[](n + 1);
    rf[n] = powmod(f[n], mod - 2, mod);
    foreach_reverse (i ; 1 .. n) {
        rf[i] = rf[i+1] * (i + 1) % mod;
    }

    long comb(int n, int k) {
        return f[n] * rf[k] % mod * rf[n-k] % mod;
    }

    long ans = f[n];

    foreach (i ; 2 .. n) {
        long t = comb(n, i) * (f[i] - 1 + mod) % mod * f[n - i] % mod;
        ans += t;
        ans %= mod;
    }

    writeln(ans);
}

long powmod(long x, long y, long mod) {
    return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1;
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

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

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

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

  int n;
  rd(n);
  const long mod = 998244353;

  long powmod(long b, long e) {
    if (e == 0)
      return 1;
    else if (e == 1)
      return b % mod;
    else if (e & 1)
      return b * powmod(b, e - 1) % mod;
    else
      return powmod(b * b % mod, e / 2);
  }

  auto fac = new long[](n + 1), inv = new long[](n + 1);
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = powmod(fac[i], mod - 2);
  }
  long ans = n * fac[n] % mod;
  foreach (i; 1 .. n) {
    ans -= fac[n] * inv[i] % mod;
    ((ans %= mod) += mod) %= mod;
  }
  writeln(ans);
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

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

enum mod = 998244353L;

void main() {
    int n;
    scan(n);

    auto f = new long[](n + 1);
    f[0] = f[1] = 1;
    foreach (i ; 2 .. n + 1) {
        f[i] = f[i-1] * i % mod;
    }
    auto rf = new long[](n + 1);
    rf[n] = powmod(f[n], mod - 2, mod);
    foreach_reverse (i ; 1 .. n) {
        rf[i] = rf[i+1] * (i + 1) % mod;
    }

    long comb(int n, int k) {
        return f[n] * rf[k] % mod * rf[n-k] % mod;
    }

    long ans = f[n];

    foreach (i ; 2 .. n) {
        long t = comb(n, i) * (f[i] - 1 + mod) % mod * f[n - i] % mod;
        ans += t;
    }

    writeln(ans);
}

long powmod(long x, long y, long mod) {
    return y > 0 ? powmod(x, y>>1, mod)^^2 % mod * x^^(y&1) % mod : 1;
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

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

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


import std.stdio, std.algorithm, std.array, std.string, std.format, std.range, std.bigint, std.conv;

void main()
{
    int t;
    scanf("%d", &t);
    foreach(_; 0..t)
    {
        long n, k;
        scanf("%lld %lld", &n, &k);
        getchar();
        auto s = format("%b", k);
        BigInt res;
        long i = 0;
        foreach(el; s.retro)
        {
            if (el == '1')
                res += (to!BigInt(n) ^^ i) % (10^^9 + 7);
            i++;
        }
        writeln(res % (10^^9 + 7));
    }
}

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

void solve(){
    ll n = scan;
    ll k = scan;
    ll MOD = to!long(1e9) + 7L;
    ll num = 0;
    ll add = 1;
    for(ll st = 0; st < 31; ++st){
        ll msk = 1L<<st;
        if(k & msk){
            num += add;
            num %= MOD;
        }
        add *= n;
        add %= MOD;
    }
    writeln(num);
}

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;
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");


void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto N = scan!long;

      if (N < 4 || N % 2 == 1) return "-1";

      long ans1, ans2;
      long t = N;
      while(t % 6 != 0) {
        t -= 4;
        ans1++;
      }
      ans1 += t / 6;

      t = N;
      while(t % 4 != 0) {
        t -= 6;
        ans2++;
      }
      ans2 += t / 4;

      return [ans1, ans2].toAnswerString;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    long n;
    foreach(_; 0..t)
    {
        n = readln.strip.to!(long);
        long amin, amax;
        if (n % 2 != 0 || n < 4) {
            writeln(-1);
            continue;
        }
        else {
            if (n % 4 == 0)
                amax = n / 4;
            else {
                auto tmp = n;
                while (tmp > 0) {
                    amax++;
                    tmp -= 6;
                    if (tmp % 4 == 0) {
                        amax += tmp / 4;
                        break;
                    }
                }
            }
            if (n % 6 == 0)
                amin = n / 6;
            else {
                auto tmp2 = n;
                while (tmp2 > 0) {
                    amin++;
                    tmp2 -= 4;
                    if (tmp2 % 6 == 0) {
                        amin += tmp2 / 6;
                        break;
                    }
                }
            }
        writeln(max(1,amin)," ", amax);
        }
    }
}

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    long n;
    foreach(_; 0..t)
    {
        n = readln.strip.to!(long);
        long amin, amax;
        if (n % 2 != 0 || n < 4) {
            writeln(-1);
            continue;
        }
        else {
            if (n % 4 == 0)
                amax = n / 4;
            else {
                auto tmp = n;
                while (tmp > 3) {
                    amax++;
                    tmp -= 6;
                    if (tmp % 4 == 0) {
                        amax += tmp / 4;
                        break;
                    }
                }
            }
            if (n % 6 == 0)
                amin = n / 6;
            else {
                auto tmp2 = n;
                while (tmp2 > 5) {
                    amin++;
                    tmp2 -= 4;
                    if (tmp2 % 6 == 0) {
                        amin += tmp2 / 6;
                        break;
                    }
                }
            }
        writeln(max(1,amin)," ", amax);
        }
    }
}

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    long n;
    foreach(_; 0..t)
    {
        n = readln.strip.to!(long);
        long amin, amax;
        if (n % 2 != 0) {
            writeln(-1);
            continue;
        }
        else {
            if (n % 4 == 0)
                amax = n / 4;
            else {
                auto tmp = n;
                while (tmp > 3) {
                    amax++;
                    tmp -= 6;
                    if (tmp % 4 == 0) {
                        amax += tmp / 4;
                        break;
                    }
                }
            }
            if (n % 6 == 0)
                amin = n / 6;
            else {
                auto tmp2 = n;
                while (tmp2 > 5) {
                    amin++;
                    tmp2 -= 4;
                    if (tmp2 % 6 == 0) {
                        amin += tmp2 / 6;
                        break;
                    }
                }
            }
        writeln(max(1,amin)," ", amax);
        }
    }
}

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto N = scan!long;
      long ans1, ans2;

      long p6 = N / 6;
      N -= p6 * 6;
      if (N % 4 == 0) {
        ans1 = p6 + N / 4;
        ans2 = (p6*6 + N) / 4;
      } else if(N % 4 == 2 && p6 > 0) {
        p6--;
        N += 6;
        ans1 = p6 + N / 4;
        ans2 = (p6 / 2)*3 + N / 4;
      } else {
        return "-1";
      }

      return [ans1, ans2].toAnswerString;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto N = scan!long;

      long ans1, ans2;

      long t = N;
      if (t % 6 == 0) {
        ans1 += t / 6;
        t -= ans1 * 6;
      }
      if (t % 4 > 0) {
        return "-1";
      }

      ans1 += t / 4;

      ans2 = N / 4;
      N -= ans2 * 4;
      ans2 += N / 6;

      return [ans1, ans2].toAnswerString;
    }

    foreach(i; 0..QN) {
      subSolve().writeln;
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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