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 ok = new bool[](t);
	auto ans = new int[][][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;
		ok[ti] = true;

		foreach (i; 1..n)
		{
			if (a[i] < i+1)
			{
				if (a[0]+a[i] >= i+1)
				{
					auto rem = i+1 - a[i];
					ans[ti] ~= [1, i+1, rem];
					ans[ti] ~= [i+1, 1, 1];
					a[0] += a[i];
					a[i] = 0;
				}
			}
			else
			{
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = (i+1) - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (ti; 0..t)
	{
		if (!ok[ti])
			writeln(-1);
		else
		{
			writeln(ans[ti].length);
			foreach (ee; ans[ti])
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = 0L ~ readln.splitter.map !(to !(long)).array;
		if (a.sum % n != 0)
		{
			writeln (-1);
			continue;
		}
		alias Move = Tuple !(int, q{i}, int, q{j}, long, q{x});
		Move [] answer;

		void go (int i, int j, long x)
		{
			a[i] -= i * x;
			a[j] += i * x;
			answer ~= Move (i, j, x);
		}

		foreach (j; 2..n + 1)
		{
			if (a[j] % j != 0)
			{
				go (1, j, j - (a[j] % j));
			}
			assert (a[1] >= 0);
			assert (a[j] % j == 0);
			go (j, 1, a[j] / j);
		}
		auto target = a.sum / n;
		foreach (j; 2..n + 1)
		{
			go (1, j, target);
		}

		writeln (answer.length);
		foreach (ref c; answer)
		{
			writeln (c.i, " ", c.j, " ", c.x);
		}
	}
}

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 a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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 a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;

		foreach (i; 1..n)
		{
			if (a[i] < i+1) continue;
			auto cnt = a[i] / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = (i+1) - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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 a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]-(i+1) < -x) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] = 0;
			ans[ti] ~= [i+1, 1, cnt];
		}

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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 a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]+x < i+1) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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 a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]-(i+1) < -x) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] = 0;
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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 ok = new bool[](t);
	auto ans = new int[][][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;
		ok[ti] = true;

		foreach (i; 1..n)
		{
			if (a[i] < i+1) continue;
			auto cnt = a[i] / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = (i+1) - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (ti; 0..t)
	{
		if (!ok[ti])
			writeln(-1);
		else
		{
			writeln(ans[ti].length);
			foreach (ee; ans[ti])
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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 a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;

		auto x = tot / n;
		a[] -= x;
		debug writeln(a);

		foreach (i; 1..n)
		{
			if (a[i]+x < i+1) continue;
			auto cnt = (a[i]+x) / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		debug writeln(a);

		foreach_reverse(i; 0..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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 a = RDA!int;

		auto tot = a.sum;
		if (tot % n) continue;
		auto x = tot / n;

		foreach (i; 1..n)
		{
			if (a[i] < i+1) continue;
			auto cnt = a[i] / (i+1);
			a[0] += cnt*(i+1);
			a[i] -= cnt*(i+1);
			ans[ti] ~= [i+1, 1, cnt];
		}
		a[] -= x;
		debug writeln(a);

		foreach_reverse(i; 1..n)
		{
			if (a[i] == 0) continue;
			else if (a[i] < 0)
			{
				a[0] += a[i];
				ans[ti] ~= [1, i+1, -a[i]];
				a[i] = 0;
			}
			else
			{
				if (a[i]%(i+1))
				{
					auto rem = x - (a[i]%(i+1));
					a[0] -= rem;
					a[i] += rem;
					ans[ti] ~= [1, i+1, rem];
				}
				auto cnt = a[i] / (i+1);
				a[0] += cnt*(i+1);
				a[i] -= cnt*(i+1);
				ans[ti] ~= [i+1, 1, cnt];
			}
		}
		/*if (n != 1)
		{
			if (a[1] != 0)
				ans[ti].length = 0;
		}*/
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				ee.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}


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; }

int[] kmp(char[] s) {
  auto r = new int[s.length];
  foreach (i; 1 .. s.length) {
    int j = r[i - 1];
    while (j > 0 && s[i] != s[j]) j = r[j - 1];
    if (s[j] == s[i]) j++;
    r[i] = j;
  }

  return r;
}

void main() {
  int T;
  read(T);
  while (T--) {
    string s = readln.strip;
    string t = readln.strip;

    if (s == t) { writeln("Yes"); continue; }

    bool yes = false;

    foreach (w; 0 .. s.length) {
      yes |= kmp(t ~ "#" ~ s[0 .. w + 1] ~ s[0 .. w].dup.reverse).canFind(t.length);
    }

    writeln(yes ? "Yes" : "No");
  }
}

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

void main()
{
	int q = readInt!int;
	while (q--)
	{
		auto s = cast(char[])readString;
		auto t = cast(char[])readString;
		bool can = false;
		foreach(i; 0 .. cast(int)s.length)
		{
			foreach(j; i .. cast(int)s.length)
			{
				int currLength = cast(int)(j - i + 1);
				auto str = s[i .. j + 1];
				int remLength = cast(int)t.length - currLength;
				if (remLength < 0) continue;
				if (remLength > j) continue;
				auto other = s[j - remLength .. j].dup;
				str ~= other.reverse;
				if (str == t) { can = true; break; }
			}
			if (can) break;
		}
		if (can) writeln("yes"); else writeln("no");
	}
}

/* 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);
	}
}

// code by cutx64

module main;

import std.string, std.array, std.container, std.typecons;
import std.stdio, std.format, std.file, std.outbuffer;
import std.algorithm, std.conv, std.functional, std.range, core.bitop, std.random;
// import std.bigint, std.complex, std.bitmanip;
// import std.math, std.mathspecial, std.numeric;

void main() {

  int tests; readf!"%s\n"(tests);
  while(tests--) {
    string s, t;
    readf!"%s\n%s\n"(s, t);
    auto n = s.length, m = t.length;
    auto rs = s.dup.retro.to!string;
    bool ok = false;
    foreach(i; 0 .. n) {
      foreach(j; i .. n) {
        int l = j - i + 1;
        if (l > m || l + j < m) continue;
        if (s[i .. j + 1] == t[0 .. l] && rs[n - j .. n - j + m - l] == t[l .. $]) {
          ok = true;
        }
      }
    }
    (ok ? "YES" : "NO").writeln;
  }

} // main

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 q;
  readf!" %d "(q);
  while (q--) {
    string s = readln.strip;
    string t = readln.strip;
    string[] a;
    foreach (i ; 1 .. s.length + 1) {
      string tmp = s[0 .. i];
      a ~= tmp ~ tmp.retro.text[1 .. $];
    }
    bool good = false;
    foreach (sx ; a) {
      if (sx.indexOf(t) != -1) {
        good = true;
        break;
      }
    }
    writeln(good ? "YES" : "NO");
  }
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
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 bool[](t);
	foreach (ti; 0..t)
	{
		auto s = RD!string;
		auto ss = RD!string;

		(){
		foreach (i; 0..s.length)
		{
			foreach (j; i..min(s.length, i+ss.length))
			{
				auto len = j - i + 1;
				bool ok = true;
				foreach (k; 0..len)
				{
					if (s[i+k] != ss[k])
					{
						ok = false;
						break;
					}
				}
				if (!ok) continue;
				foreach_reverse (k; 0..ss.length-len)
				{
					if (j <= k)
					{
						ok = false;
						break;
					}
					auto p = j - k - 1;
					if (s[p] != ss[len+k])
					{
						ok = false;
						break;
					}
				}
				if (ok)
				{
					ans[ti] = true;
					return;
				}
			}
		}}();
	}

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

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

bool solve (string s, string t)
{
	auto n = s.length.to !(int);
	foreach (pos; 0..n)
	{
		foreach (next; pos..n)
		{
			foreach (prev; 0..next + 1)
			{
				if (equal (t, chain (s[pos..next + 1],
				    s[prev..next].retro)))
				{
					return true;
				}
			}
		}
	}
	return false;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		auto t = readln.strip;
		writeln (solve (s, t) ? "YES" : "NO");
	}
}


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

void main() {
    long n = readln.chomp.to!long;
    long[] a = readln.split.map!(to!long).array;

    if(n >= 35) {
        long ans = 0;
        foreach(x; a)
            ans += x - 1;
        ans.writeln;
        return;
    }

    a.sort;

    long ans = 10L^^14;
    for(long c = 1L; c <= 10^^9; c++) {
        //writefln("c: %s", c);
        long minima = 0L;
        for(int i = 0L; i < n; i++) {
            minima += abs(a[i] - c^^i);
            //writefln("i: %s", i);
        }
        //writefln("minima: %s", minima);
        if(minima > ans && ans != 10^^18)
            break;
        ans = min(ans,minima);
    }

    ans.writeln;
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(real)).array;
		sort (a);
		real res = real.max;
		for (real base = 1; log10 (base) * (n - 1) <= 12; base++)
		{
			res = min (res, n.iota.map !(i =>
			    abs (a[i] - pow (base, i))).sum);
		}
		writefln !("%.0f") (res);
	}
}

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

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    int n = read!int;
    readln;
    auto arr = readln.strip.split.map!(x => x.to!long).array.sort;
    debug writeln("length = ", arr.length);
    auto up = pow(10_000_000_001, 1.0/(n-1)).floor.to!long;

    long m = long.max;
    debug writeln("up = ", up);
    foreach (c; 1..up + 1)
    {
	long t = arr[0] - 1;
	long q = 1;
	debug writeln("c = ", c);
	for (int i = 1; i < n; i++)
	{
	    q *= c;
	    debug writeln("q = ", q);
	    t += abs(q - arr[i]).to!long;
	}

	if (t < m)
	    m = t;

	debug writeln(t);
    }

    //debug writeln ("c = ", c, " m = ", m);
    writeln (m);

    return 0;
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!long();
  auto a = next!long(n);
  sort(a);
  bool admissibleC(long c)
  {
    auto p = checked!Saturate(long(1));
    foreach(i; 0 .. n)
      p *= c;
    return p != long.max;
  }
  long mincost = long.max;
  foreach(c; 0 .. 1_000_000_000)
    {
      if (!admissibleC(c))
        break;
      long cost = 0;
      long p = 1;
      foreach(i; 0 .. n)
        {
          cost += abs(a[cast(size_t)i] - p);
          p *= c;
        }
      mincost = min(mincost, cost);
    }
  writeln(mincost);
}

// INPUT

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

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 4096;
    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;
    }
}

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

void main() {
    long n = readln.chomp.to!long;
    long[] a = readln.split.map!(to!long).array;

    if(n >= 35) {
        long ans = 0;
        foreach(x; a)
            ans += x - 1;
        ans.writeln;
        return;
    }

    a.sort;

    long ans = 10L^^14;
    for(long c = 1L; c <= 10^^9; c++) {
        long minima = 0L;

        for(int i = 0; i < n; i++)
            minima += abs(a[i] - c^^i);

        if(minima > ans)
            break;

        ans = min(ans,minima);
    }

    ans.writeln;
}

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

void main() {
    long n = readln.chomp.to!long;
    long[] a = readln.split.map!(to!long).array;

    if(n >= 30) {
        long ans = 0;
        foreach(x; a)
            ans += x - 1;
        ans.writeln;
        return;
    }

    a.sort;

    long ans = 10^^14;
    for(long c = 1; c <= 10^^9; ++c) {
        //writefln("c: %s", c);
        long minima = 0;
        for(int i = 0; c^^i <= 10^^14; i++) {
            if(i >= n) break;
            minima += abs(a[i] - c^^i);
            //writefln("i: %s", i);
        }
        //writefln("minima: %s", minima);
        if(minima > ans)
            break;
        else
            ans = min(ans,minima);
    }

    ans.writeln;
}

import std.math;
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!long();
  auto a = next!long(n);
  sort(a);
  bool admissibleC(long c)
  {
    long p = 1;
    foreach(i; 0 .. n)
      {
        if (p >= 2_000_000_000)
          {
            return false;
          }
        p *= c;
      }
    return true;
  }
  long mincost = long.max;
  foreach(c; 0 .. 1_000_000_000)
    {
      if (!admissibleC(c))
        break;
      long cost = 0;
      long p = 1;
      foreach(i; 0 .. n)
        {
          cost += abs(a[cast(size_t)i] - p);
          p *= c;
        }
      mincost = min(mincost, cost);
    }
  writeln(mincost);
}

// INPUT

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

File inputFile;
string popWord();
static if (inputStyle == InputStyle.byChunk)
  {
    const chunkSize = 4096;
    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.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = read.to!int;
	long[] ss;
	foreach(i; 0 .. n) ss ~= read.to!long;
	log("ss:", ss);

	auto seg = new SegTree!long(0, n, ((x, y) => x + y), 0);
	foreach(i; 0 .. n) seg.setValue(i, n, i + 1);
	log("seg:", n.iota.map!(i => seg.getValue(i)));

	long[] ans;
	foreach_reverse(s; ss){
		int j = uplimit!int(0, n - 1, (i => (seg.getValue(i) <= s))) + 1;
		long an = j + 1;
		ans ~= an;
		seg.setValue(j, n, -an);
		log("seg:", n.iota.map!(i => seg.getValue(i)));
	}
	log("ans:", ans);

	ans.reverse.map!(to!string).join(" ").writeln;

}

// a <= x <=  c の中でfをみたす最大(二分探索; binary search) ※テンプレート版
T uplimit(T)(T a, T c, bool delegate(T) f){
	if(f(c)) return c;
	if(! f(a)) return a - 1;
	while(a + 1 < c){
		T b = (a + c) / 2;
		if(f(b)) a = b;
		else c = b;
	}
	return a;
}

/*
セグメント木 区分木 segtree segment tree
範囲に書き込み、1点で読み出す。
たとえば、 [l, r) の範囲に一律 x を足すクエリがくるなど。

引数
a, b　添字の範囲。 この意味はa から b - 1 までなので注意。
apply　(x, y) => すでに値 x があるところに y を加えるとどうなるか ※結合的な演算である必要がある
neutral　すべての x に対して apply(z, x) = apply(x, z) = x をみたす z

*/
class SegTree(T){
	T delegate(T, T) apply;
	T neutral;
	int a, b; // a <= x < b を担当する
	bool isTerminal;
	SegTree left, right; // 左の子(a <= x < c)、右の子(c <= x < b)
	T value;
	this(int a, int b, T delegate(T, T) apply, T neutral = T.init){
		this.a = a, this.b = b;
		this.apply = apply;
		this.neutral = neutral;
		this.value = neutral;
		if(b - a == 1){
			this.isTerminal = 1;
		}
		else{
			int c = (a + b) / 2;
			this.left = new SegTree(a, c, apply, neutral);
			this.right = new SegTree(c, b, apply, neutral);
		}
	}

	// [a, b) に値を設定する ※[a, b) はこのノードの担当区間とかぶっていなくてもよい
	void setValue(int a, int b, T value){
		if(b <= this.a || this.b <= a) return;
		if(a <= this.a && this.b <= b){
			this.value = apply(this.value, value);
		}
		else{
			divideValue();
			left.setValue(a, b, value);
			right.setValue(a, b, value);
		}
	}

	// 自分の持っていた値を子に引き継ぐ
	void divideValue(){
		left.value = apply(left.value, this.value);
		right.value = apply(right.value, this.value);
		this.value = neutral;
	}

	// iにおける値 ※iは必ずこのノードの担当区間内である前提
	T getValue(int i){
		assert(a <= i && i < b);
		if(this.isTerminal) return this.value;
		else divideValue();
		if(i < left.b) return left.getValue(i);
		else return right.getValue(i);
	}

	// 配列に変換したもの（デバッグ用）
	T[] array(){
		T[] res;
		foreach(i; a .. b) res ~= this.value;
		return res;
	}

	// 文字列に変換したもの（デバッグ用）
	override string toString(){
		if(this.isTerminal) return this.array.to!string;
		else	return this.array.to!string ~ "\n" ~ left.toString ~ right.toString ~ "\n";
	}
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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)); }

enum INF = 10L^^18;
alias Entry = Tuple!(long, "val", int, "key");

class SegmentTree {
  int n;
  Entry[] mx;
  long[] add;
  this(long[] ini) {
    const n_ = cast(int)(ini.length);
    for (n = 1; n < n_; n <<= 1) {}
    mx = new Entry[n << 1];
    add = new long[n << 1];
    mx[] = Entry(-INF, -1);
    add[] = 0;
    foreach (i; 0 .. n_) {
      mx[n + i] = Entry(ini[i], i);
    }
    foreach_reverse (a; 1 .. n) {
      mx[a] = max(mx[a << 1], mx[a << 1 | 1]);
    }
  }
  // [a, b)
  private Entry query(int u, int x, int y, int a, int b, long val) {
    chmax(a, x);
    chmin(b, y);
    if (a >= b) {
      return Entry(-INF, -1);
    }
    if (a == x && b == y) {
      mx[u].val += val;
      add[u] += val;
      return mx[u];
    }
    assert(u < n);
    const uL = u << 1 | 0;
    const uR = u << 1 | 1;
    const mid = (x + y) >> 1;
    if (add[u] != 0) {
      mx[uL].val += add[u];
      mx[uR].val += add[u];
      add[uL] += add[u];
      add[uR] += add[u];
      add[u] = 0;
    }
    const resL = query(uL, x, mid, a, b, val);
    const resR = query(uR, mid, y, a, b, val);
    mx[u] = max(mx[uL], mx[uR]);
    return max(resL, resR);
  }
  // [a, b)
  Entry query(int a, int b, long val) {
    return query(1, 0, n, a, b, val);
  }
}

int N;
long[] S;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      S = new long[N];
      foreach (i; 0 .. N) {
        S[i] = readLong();
      }

      S[] *= -1;
      debug {
        writeln("S = ", S);
      }
      auto seg = new SegmentTree(S);
      auto ans = new int[N];

      foreach (x; 1 .. N + 1) {
        const e = seg.query(0, N, 0);
        debug {
          writefln("x = %s: e = %s", x, e);
        }
        assert(e.val == 0);
        ans[e.key] = x;
        seg.query(e.key, e.key + 1, -INF);
        seg.query(e.key + 1, N, x);
      }

      foreach (i; 0 .. N) {
        if (i > 0) {
          write(" ");
        }
        write(ans[i]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}

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.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    struct fwtr {
        int mx;
        long[] arr;
        this(int n) {
            mx = n;
            arr = new long[] (n+1);
        }

        void add(int p, int v) {
            while (p <= mx) {
                arr[p] += v;
                p += (p & (-p));
            }
        }

        long sm(int p) {
            long ret = 0;
            while (p > 0) {
                ret += arr[p];
                p -= (p & (-p));
            }

            return ret;
        }
    }

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

    auto arr = readln.chomp.split.map!(to!long).array;
    auto sums = (n+1).iota.dropOne.cumulativeFold!((a, b) => a + b);

    debug { sums.writeln; }

    auto f = new fwtr(n);

    int binS(long sm) {
        long getSm(int idx) {
            return (idx.to!long * (idx + 1)) / 2 - f.sm(idx);
        }

        int le = 1, r = n;
        while (le < r) {
            int m = (le + r) / 2 + 1;
            if (getSm(m-1) <= sm) { le = m; }
            else { r = m - 1; }
        }

        return le;
    }

    auto ans = new int[] (n);

    foreach_reverse (i, sm; arr) {
        int val = binS(sm);
        f.add(val, val);
        ans[i] = val;
    }

    ans.map!(to!string).join(" ").writeln;
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		int [int] b;
		readln.splitter.map !(to !(int)).filter !(x => x % k > 0)
		    .map !(x => k - x % k).each !(x => b[x + 1] += 1);
		b.byKeyValue.map !(s => s.key + k * (s.value - 1L))
		    .maxElement (0L).writeln;
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		a[] = (k - a[] % k) % k;
		sort (a);
		long res = 0;
		long cur = 0;
		foreach (i; 0..n)
		{
			if (i > 0 && a[i] == a[i - 1])
			{
				cur += k;
			}
			else
			{
				cur = 0;
			}
			if (a[i] > 0)
			{
				res = max (res, cur + a[i] + 1);
			}
		}
		writeln (res);
	}
}

import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;
import std.math;
import std.typecons;

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

  long n, k;
  @Dim("n") long[] a;

  void solve(long tc = -1)
  {
    long[long] cnt;
    foreach(ai; a)
      if (ai % k != 0)
      {
        cnt.require((-ai%k + k)%k, 0)++;
      }
    if (cnt.length == 0)
      {
        writeln(0);
        return;
      }
    long ops = 0;
    foreach(m, c; cnt)
      {
        ops = max(ops, m + (c - 1) * k);
      }
    writeln(ops + 1);
  }
}

struct Interval
{
  long l, h;

  bool empty()
  {
    return l > h;
  }
}

Interval intersect(Interval a, Interval b)
{
  return Interval(max(a.l, b.l), min(a.h, b.h));
}

struct If
{
  string condition;
}
enum ignore = If("false");
struct Dim
{
  string dimensions;
  int levels;
  this(string dimensions)
  {
    this.dimensions = dimensions;
    levels = cast(int)dimensions.count(",") + 1;
  }
}

template getArrayLevel(W)
{
  static if (isDynamicArray!W && !is(W == string))
    enum getArrayLevel = 1 + getArrayLevel!(removeArray!(W, 1));
  else
    enum getArrayLevel = 0;
}

static foreach(fieldName; 'a' .. cast(char)('z' + 1))
  {
    static if (fieldName != 'o')
      mixin("enum d", fieldName, " = Dim(\"", fieldName, "\");");
  }

mixin template prettyPrinting()
{
  string toString()
  {
    alias structType = typeof(this);
    auto res = appender!(string)();
    res.put(structType.stringof);
    res.put("(");
    static foreach(fieldName; FieldNameTuple!structType)
      {
        res.put(text(" ", fieldName, ": ", mixin(fieldName)));
      }
    res.put(" )");
    res.put("\n");
    return res[];
  }
}

struct Graph(N)
{
  size_t size;
  this(S)(S size)
  {
    adjacencyList.length = cast(size_t) size;
    this.size = cast(size_t) size;
    this.visited = makeSlice!bool(size);
  }
  N[][] adjacencyList;
  alias ne = neighbours;
  N[] neighbours(N n)
  {
    return adjacencyList.at(n);
  }
  alias bcon = biconnect;
  void biconnect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
    adjacencyList.at(b) ~= a;
  }
  alias con = connect;
  void connect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
  }
  void connect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        connect(edge[0], edge[1]);
      }
  }
  void biconnect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        biconnect(edge[0], edge[1]);
      }
  }
  alias deg = degree!long;
  auto degree(T)(N a)
  {
    return cast(T) adjacencyList.at(a).length;
  }

  auto visited = new bool[0];
  void dfs(Visitor)(long startNode)
  {
    auto visitor = Visitor.init;
    void internalDfs(long node)
    {
      visited.set(node, true);
      static if (is(typeof({visitor.n = node;})))
        visitor.n = node;
      static if (is(typeof({visitor.pre;})))
        visitor.pre;
      foreach(w; ne(node))
        if (!visited.at(w))
          {
            static if (is(typeof({visitor.w = w;})))
              visitor.w = w;
            static if (is(typeof(visitor.pren)))
              visitor.pren;
            internalDfs(w);
            static if (is(typeof(visitor.postn)))
              visitor.postn;
          }
      static if (is(typeof(visitor.post)))
        visitor.post;
    }
    internalDfs(startNode);
  }
  void dfs(long startNode)
  {
    struct DefVisitor {}
    dfs!DefVisitor(startNode);
  }
}

DList!string _words;

template removeArray(W, int times)
{
  static if (times == 0)
    alias removeArray = W;
  else static if (times == 1)
    alias removeArray = typeof(function(){W w; return w[0];}());
  else
    {
      static assert(isDynamicArray!W);
      alias removeArray = removeArray!(removeArray!(W, 1), times - 1);
    }
}

template baseType(W)
{
  static if (isDynamicArray!W && !is(W == string))
    alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));
  else
    alias baseType = W;
}

auto makeSlice(E, W, T...)(W size, T otherSizes)
{
  static if (T.length == 0)
    {
      E[] res;
      res.length = cast(size_t) size;
      return res;
    }
  else
    {
      typeof(makeSlice!E(otherSizes))[] res;
      res.length = cast(size_t) size;
      foreach(ref row; res)
        row = makeSlice!E(otherSizes);
      return res;
    }
}

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, If))
              enum condition = getUDAs!(fieldSymbol, If)[0].condition;
            else
              enum condition = "true";

            if (mixin(condition))
              {
                static if (hasUDA!(fieldSymbol, Dim))
                  {
                    static assert(isDynamicArray!(typeOfField)
                                  , text(W.stringof, ".", fieldName, " has Dim UDA but isn't a Dynamic Array."));
                    enum uda = getUDAs!(fieldSymbol, Dim)[0];
                    static assert(getArrayLevel!(typeOfField) == uda.levels,
                                  text(W.stringof, ".", fieldName, " has a Dim UDA with different number of dimensions than it's type."));
                    mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda.dimensions, q{)});
                  }
                else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
                  {
                    pragma(msg, "Warning: You probably want to add dimension to input ", W.stringof, ".", fieldName);
                  }
                read(mixin(fieldName));
              }
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  static if (is(typeof(f(i))))
    foreach(i; 0 .. times)
      f(i);
  else
    foreach(i; 0 .. times)
      f();
}

void multi()
{
  foreach(tc; 0 .. next!long)
    next!TestCase.solve(tc);
}

void single()
{
  auto tc = next!TestCase;
  tc.solve;
}

void main()
{
  type;
}

import std.stdio;
import std.string : split, strip;
import std.conv : to;
import std.algorithm : map;
import std.array;

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

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

    auto a = readln.strip.split.map!(to!long);
    auto d = a.map!(a => a % k == 0 ? 0 : k - a % k);

    int[long] f;
    foreach (e; d) {
      f[e]++;
    }
    f.remove(0);

    long m = 0;
    int c;
    foreach (key, occ; f) {
      if (m == 0 || occ > c || (occ == c && key > m)) {
        m = key;
        c = occ;
      }
    }

    if (m == 0) {
      writeln(0);
    } else {
      writeln(k * (c - 1) + m + 1);
    }
  }
}

import std.algorithm, std.conv, std.stdio;
void main () {
	readln;
	int n, k;
	while (readf !(" %s %s") (n, k) > 0) {
		readln;
		int [int] b;
		readln.splitter.map !(to !(int)).filter !(x => x % k > 0).map !(x => k - x % k).each !(x => b[x + 1] += 1);
		b.byKeyValue.map !(s => s.key + k * (s.value - 1L)).maxElement (0L).writeln;
	}
}


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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto X = new bool[](10^^6+10);
    int[] Y;
    auto zero = 0;
    auto one = 0;
    int last = 0;
    int[] ans;

    foreach (i, a; A) {
        if (a > 0) {
            if (X[a]) {
                writeln(-1);
                return;
            }
            X[a] = true;
            zero -= 1;
            one += 1;
        } else {
            a *= -1;
            if (!X[a]) {
                writeln(-1);
                return;
            }
            Y ~= a;
            zero += 1;
            one -= 1;
            if (one == 0) {
                ans ~= (i.to!int + 1) - last;
                last = i.to!int + 1;
                foreach (y; Y) X[y] = false;
                Y = [];
            }
        }
    }

    if (X.any) {
        writeln(-1);
        return;
    }

    writeln(ans.length.to!int);
    ans.map!(to!string).join(" ").writeln;
}

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

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

void solve(){
	int n  = rint;
	int[] as = rint(n);

	int[] xs = new int[](1_000_999);
	long[] ys = new long[](1_000_999);
	int cnt = 0;
	int[] ans;

	int an = 0;
	foreach(a; as){
		an += 1;
		if(a > 0){
			if(xs[a] > 0){
				"-1".writeln;
				return;
			}
			xs[a] = 1;
			if(ys[a] == ans.length + 1){
				"-1".writeln;
				return;
			}
			ys[a] = ans.length + 1;
			cnt += 1;
		}
		else{
			if(xs[-a] == 0){
				"-1".writeln;
				return;
			}
			xs[-a] = 0;
			cnt -= 1;
			if(cnt == 0){
				ans ~= an;
				an = 0;
			}
		}
		log("a:", a, "cnt:", cnt, "an:", an, "ans:", ans);
		log("xs:", xs[0..10]);
		log("ys:", ys[0..10]);
	}

	if(cnt > 0){
		"-1".writeln;
		return;
	}

	ans.length.writeln;
	ans.map!(to!string).array.join(" ").writeln;

}

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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto X = new bool[](10^^6+10);
    int[] Y;
    auto zero = 0;
    auto one = 0;
    int last = 0;
    int[] ans;

    foreach (i, a; A) {
        if (a > 0) {
            if (X[a]) {
                writeln(-1);
                return;
            }
            X[a] = true;
            zero -= 1;
            one += 1;
        } else {
            a *= -1;
            if (!X[a]) {
                writeln(-1);
                return;
            }
            Y ~= a;
            zero += 1;
            one -= 1;
            if (one == 0) {
                ans ~= (i.to!int + 1) - last;
                last = i.to!int + 1;
                foreach (y; Y) X[y] = false;
                Y = [];
            }
        }
    }

    writeln(ans.length.to!int);
    ans.map!(to!string).join(" ").writeln;
}


immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto m = readInt!int;
	auto mat = ma(n, readString.map!(c => c == '0'? int(0) : int(1)).array);
	auto rowSum = new int[][](m, n);
	auto colSum = new int[][](n, m);
	foreach(j; 0 .. m)
	{
		rowSum[j][0] = mat[0][j];
	}
	foreach(j; 0 .. m)
	{

		foreach(i; 1 .. n)
		{
			rowSum[j][i] = rowSum[j][i-1] + mat[i][j];
		}
	}
	foreach(i; 0 .. n)
	{
		colSum[i][0] = mat[i][0];
	}
	foreach(i; 0 .. n)
	{
		foreach(j; 1 .. m)
		{
			colSum[i][j] = colSum[i][j-1] + mat[i][j];
		}
	}
	auto ans = int.max;
	foreach(i1; 0 .. n)
	{
		foreach(i2; i1 + 4 .. n)
		{
			auto minTail = int.max;
			auto height = i2 - i1 - 1;
			auto railsSum = new int[](m);
			auto bodySum = new int[](m);
			auto pileRemove = new int[](m);

			railsSum[0] = (1-mat[i1][0]) + (1-mat[i2][0]);
			bodySum[0] = rowSum[0][i2-1] - rowSum[0][i1];
			pileRemove[0] = height - (rowSum[0][i2-1] - rowSum[0][i1]);
			foreach(j; 1 .. m)
			{
				railsSum[j] = (1-mat[i1][j]) + (1-mat[i2][j]) + railsSum[j-1];
				bodySum[j] = (rowSum[j][i2-1] - rowSum[j][i1]) + bodySum[j-1];
				pileRemove[j] = height - (rowSum[j][i2-1] - rowSum[j][i1]);

				debug writeln("body sum ", i1, " ", i2, " ", j, " = ", bodySum[j]);
				debug writeln("pile remove ", i1, " ", i2, " ", j, " = ", pileRemove[j]);
			}

			foreach(j; 3 .. m)
			{
				minTail = min(minTail, -railsSum[j-3]+pileRemove[j-3]-bodySum[j-3]);
				auto localOperations = minTail + pileRemove[j] + railsSum[j-1] + bodySum[j-1];
				ans = min(ans, localOperations);
			}
		}
	}
	ans.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;
// }}}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto m = readInt!int;
	auto mat = ma(n, readString.map!(c => c == '0'? int(0) : int(1)).array);
	auto rowSum = new int[][](m, n);
	auto colSum = new int[][](n, m);
	foreach(j; 0 .. m)
	{
		rowSum[j][0] = mat[0][j];
	}
	foreach(j; 0 .. m)
	{

		foreach(i; 1 .. n)
		{
			rowSum[j][i] = rowSum[j][i-1] + mat[i][j];
		}
	}
	foreach(i; 0 .. n)
	{
		colSum[i][0] = mat[i][0];
	}
	foreach(i; 0 .. n)
	{
		foreach(j; 1 .. m)
		{
			colSum[i][j] = colSum[i][j-1] + mat[i][j];
		}
	}
	auto railsSum = new int[](m);
	auto bodySum = new int[](m);
	auto pileRemove = new int[](m);
	auto ans = int.max;
	foreach(i1; 0 .. n)
	{
		foreach(i2; i1 + 4 .. n)
		{
			auto minTail = int.max;
			auto height = i2 - i1 - 1;
			railsSum[0] = (1-mat[i1][0]) + (1-mat[i2][0]);
			bodySum[0] = rowSum[0][i2-1] - rowSum[0][i1];
			pileRemove[0] = height - (rowSum[0][i2-1] - rowSum[0][i1]);
			foreach(j; 1 .. m)
			{
				railsSum[j] = (1-mat[i1][j]) + (1-mat[i2][j]) + railsSum[j-1];
				bodySum[j] = (rowSum[j][i2-1] - rowSum[j][i1]) + bodySum[j-1];
				pileRemove[j] = height - (rowSum[j][i2-1] - rowSum[j][i1]);
			}
			foreach(j; 3 .. m)
			{
				minTail = min(minTail, -railsSum[j-3]+pileRemove[j-3]-bodySum[j-3]);
				auto localOperations = minTail + pileRemove[j] + railsSum[j-1] + bodySum[j-1];
				ans = min(ans, localOperations);
			}
		}
	}
	ans.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;
// }}}

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

enum INF = 10^^9;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const M = readInt();
        const N = readInt();
        auto A = new string[M];
        foreach (x; 0 .. M) {
          A[x] = readToken();
        }

        auto ASums = new int[][](M, N + 1);
        foreach (x; 0 .. M) {
          foreach (y; 0 .. N) {
            ASums[x][y + 1] = ASums[x][y] + (A[x][y] - '0');
          }
        }

        int ans = INF;
        auto fs = new int[M + 1];
        foreach (y0; 0 .. N) foreach (y1; y0 + 3 .. N) {
          foreach (x; 0 .. M) {
            fs[x + 1] = fs[x];
            fs[x + 1] += 1 - (A[x][y0] - '0');
            fs[x + 1] += 1 - (A[x][y1] - '0');
            fs[x + 1] += (ASums[x][y1] - ASums[x][y0 + 1]);
          }
          int mn = INF;
          foreach (x; 0 .. M) {
            if (x >= 4) {
              int cost;
              cost += (y1 - y0 - 1) - (ASums[x - 4][y1] - ASums[x - 4][y0 + 1]);
              cost -= fs[x - 4 + 1];
              chmin(mn, cost);
            }
            {
              int cost = mn;
              cost += (y1 - y0 - 1) - (ASums[x][y1] - ASums[x][y0 + 1]);
              cost += fs[x];
              chmin(ans, cost);
            }
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

immutable multi = true;

int[400][400] mat, rowSum;
int[400] railsSum, bodySum, pileRemove;

void solve(int tc)
{
	auto n = readInt!int;
	auto m = readInt!int;
	foreach(i; 0 .. n)
	{
		auto str = readString;
		foreach(j; 0 .. m)
			mat[i][j] = int(str[j] == '1');
	}
	foreach(j; 0 .. m)
	{
		rowSum[j][0] = mat[0][j];
	}
	foreach(j; 0 .. m)
	{

		foreach(i; 1 .. n)
		{
			rowSum[j][i] = rowSum[j][i-1] + mat[i][j];
		}
	}
	auto ans = int.max;
	foreach(i1; 0 .. n)
	{
		foreach(i2; i1 + 4 .. n)
		{
			auto minTail = int.max;
			auto height = i2 - i1 - 1;
			railsSum[0] = (1-mat[i1][0]) + (1-mat[i2][0]);
			bodySum[0] = rowSum[0][i2-1] - rowSum[0][i1];
			pileRemove[0] = height - (rowSum[0][i2-1] - rowSum[0][i1]);
			foreach(j; 1 .. m)
			{
				railsSum[j] = (1-mat[i1][j]) + (1-mat[i2][j]) + railsSum[j-1];
				bodySum[j] = (rowSum[j][i2-1] - rowSum[j][i1]) + bodySum[j-1];
				pileRemove[j] = height - (rowSum[j][i2-1] - rowSum[j][i1]);
			}
			foreach(j; 3 .. m)
			{
				minTail = min(minTail, -railsSum[j-3]+pileRemove[j-3]-bodySum[j-3]);
				auto localOperations = minTail + pileRemove[j] + railsSum[j-1] + bodySum[j-1];
				ans = min(ans, localOperations);
			}
		}
	}
	ans.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;
// }}}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto m = readInt!int;
	auto mat = ma(n, readString.map!(c => c == '0'? int(0) : int(1)).array);
	auto rowSum = new int[][](m, n);
	foreach(j; 0 .. m)
	{
		rowSum[j][0] = mat[0][j];
	}
	foreach(j; 0 .. m)
	{

		foreach(i; 1 .. n)
		{
			rowSum[j][i] = rowSum[j][i-1] + mat[i][j];
		}
	}
	auto railsSum = new int[](m);
	auto bodySum = new int[](m);
	auto pileRemove = new int[](m);
	auto ans = int.max;
	foreach(i1; 0 .. n)
	{
		foreach(i2; i1 + 4 .. n)
		{
			auto minTail = int.max;
			auto height = i2 - i1 - 1;
			railsSum[0] = (1-mat[i1][0]) + (1-mat[i2][0]);
			bodySum[0] = rowSum[0][i2-1] - rowSum[0][i1];
			pileRemove[0] = height - (rowSum[0][i2-1] - rowSum[0][i1]);
			foreach(j; 1 .. m)
			{
				railsSum[j] = (1-mat[i1][j]) + (1-mat[i2][j]) + railsSum[j-1];
				bodySum[j] = (rowSum[j][i2-1] - rowSum[j][i1]) + bodySum[j-1];
				pileRemove[j] = height - (rowSum[j][i2-1] - rowSum[j][i1]);
			}
			foreach(j; 3 .. m)
			{
				minTail = min(minTail, -railsSum[j-3]+pileRemove[j-3]-bodySum[j-3]);
				auto localOperations = minTail + pileRemove[j] + railsSum[j-1] + bodySum[j-1];
				ans = min(ans, localOperations);
			}
		}
	}
	ans.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;
// }}}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k1 = RD!int;
		auto k2 = RD!int;
		auto a = RDA;
		auto b = RDA;
		foreach (i; 0..k1)
		{
			if (a[i] == n)
			{
				ans[ti] = true;
				break;
			}
		}
	}

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

void main() {
	auto t = ri;
	foreach(_; 0..t) {
		auto ip = readAs!(int[]);
		auto a = readAs!(int[]);
		auto b = readAs!(int[]);
		a.sort!"a > b"();
		b.sort!"a > b"();
		if(a[0] > b[0]) writeln("YES");
		else writeln("NO");
	}
}

// ===================================

import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

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

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

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

    auto maxa = readln.strip.split.map!(to!int).maxElement;
    auto maxb = readln.strip.split.map!(to!int).maxElement;

    writeln(maxa > maxb ? "YES" : "NO");
  }
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n, k1, k2;
		readf !(" %s %s %s") (n, k1, k2);
		readln;
		auto a = readln.splitter.map !(to !(int));
		auto b = readln.splitter.map !(to !(int));
		writeln (a.maxElement > b.maxElement ? "YES" : "NO");
	}
}

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

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

void solve(){
	foreach(_; 0 .. rint){
		int n = rint, k1 = rint, k2 = rint;
		int[] as = rint(k1);
		int[] bs = rint(k2);

		string ans = "NO";
		foreach(a; as) if(a == n) ans = "YES";

		ans.writeln;
	}
}

import std.stdio;
import std.algorithm;
int main()
{
	int t=0;
	readf(" %d", &t);
	for (int i=0; i<t; i++)
	{
		int a=0;
		int b=0;
		int c=0;
		readf(" %d", &a);
		readf(" %d", &b);
		readf(" %d", &c);
		int max1=-1;
		int max2=-1;
		for (int j=0; j<b; j++)
		{
			int d=0;
			readf(" %d", &d);
			if (d>max1)
			{
				max1=d;
			}
		}
		for (int j=0; j<c; j++)
		{
			int d=0;
			readf(" %d", &d);
			if (d>max2)
			{
				max2=d;
			}
		}
		if (max1>max2)
		{
			writeln("YES");
		}
		else
		{
			writeln("NO");
		}
	}
	return 0;
}