import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int[][] PERM3S = [
	[ 0, 1, 2 ],
	[ 0, 2, 1 ],
	[ 1, 0, 2 ],
	[ 1, 2, 0 ],
	[ 2, 0, 1 ],
	[ 2, 1, 0 ],
];

long dot(long[] a, long[] b) {
	long ret;
	foreach (d; 0 .. 3) {
		ret += a[d] * b[d];
	}
	return ret;
}

long[][] A;

void solve() {
	foreach (i; 0 .. 8) {
		foreach (j; 0 .. 8) foreach (k; 0 .. 8) foreach (l; 0 .. 8) {
			if (j != i && k != i && l != i && j < k && k < l) {
				foreach (sj; 0 .. 6) foreach (sk; 0 .. 6) foreach (sl; 0 .. 6) {
					long[][] vs = new long[][](8, 3);
					foreach (d; 0 .. 3) {
						vs[1][d] = A[j][PERM3S[sj][d]] - A[i][d];
						vs[2][d] = A[k][PERM3S[sk][d]] - A[i][d];
						vs[4][d] = A[l][PERM3S[sl][d]] - A[i][d];
					}

					const pjj = dot(vs[1], vs[1]);
					const pjk = dot(vs[1], vs[2]);
					const pjl = dot(vs[1], vs[4]);
					const pkk = dot(vs[2], vs[2]);
					const pkl = dot(vs[2], vs[4]);
					const pll = dot(vs[4], vs[4]);
					if (pjj == pkk && pkk == pll && pll > 0 && pjk == 0 && pjl == 0 && pkl == 0) {
debug{
writeln(i,"  ",j," ",k," ",l);
writeln(vs[1]," ",vs[2]," ",vs[4]);
}
						long[][] as, bs;
						foreach (m; 0 .. 8) {
							if (m != i && m != j && m != k && m != l) {
								as ~= A[m];
							}
						}
						foreach (f; 0 .. 8) if (f & (f - 1)) {
							long[] b = new long[3];
							if (f & 1) b[] += vs[1][];
							if (f & 2) b[] += vs[2][];
							if (f & 4) b[] += vs[4][];
							bs ~= b;
						}
						assert(as.length == 4);
						assert(bs.length == 4);
						int[] perm = [ 0, 1, 2, 3 ];
						do {
							bool ok = true;
							int[] ss;
							foreach (x; 0 .. 4) {
								bool found;
								foreach (s; 0 .. 6) {
									bool eq = true;
									foreach (d; 0 .. 3) {
										eq = eq && (bs[perm[x]][d] == as[x][PERM3S[s][d]] - A[i][d]);
									}
									if (eq) {
										found = true;
										ss ~= s;
										break;
									}
								}
								ok = ok && found;
							}
							if (ok) {
debug{
writeln("ss = ",ss);
}
								writeln("YES");
								int pos;
								foreach (h; 0 .. 8) {
									const s = (h == i) ? 0 : (h == j) ? sj : (h == k) ? sk : (h == l) ? sl : ss[pos++];
									foreach (d; 0 .. 3) {
										if (d > 0) write(" ");
										write(A[h][PERM3S[s][d]]);
									}
									writeln;
								}
								return;
							}
						} while (perm.nextPermutation);
					}
				}
			}
		}
	}
	writeln("NO");
}

void main(string[] args) {
	try {
	for (; ; ) {
		A = new long[][](8, 3);
		foreach (i; 0 .. 8) {
			foreach (d; 0 .. 3) {
				A[i][d] = readLong;
			}
		}

		solve;
	}
	} catch (EOFException) {}
}

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;
import core.bitop;

immutable int MAX_N = 8;
immutable int MAX_D = 3;

long dist (int [MAX_D] p, int [MAX_D] q)
{
	long res = 0L;
	foreach (j; 0..MAX_D)
	{
        	long cur = q[j] - p[j];
        	res += cur * cur;
	}
	return res;
}

int [MAX_D] [] build (int [MAX_D] [] a)
{
	long d01 = dist (a[0], a[1]);
	if (d01 == 0L)
	{
		return null;
	}
	long d02 = dist (a[0], a[2]);
	if (d02 != d01)
	{
		return null;
	}
	long d03 = dist (a[0], a[3]);
	if (d03 != d01)
	{
		return null;
	}
	long d12 = dist (a[1], a[2]);
	if (d12 != d01 * 2)
	{
		return null;
	}
	long d13 = dist (a[1], a[3]);
	if (d13 != d12)
	{
		return null;
	}
	long d23 = dist (a[2], a[3]);
	if (d23 != d12)
	{
		return null;
	}

	int [MAX_D] [] f;
	foreach (i; 0..MAX_N)
	{
		int [MAX_D] c = a[0];
		if (i & 1)
		{
			c[] += a[1][] - a[0][];
		}
		if (i & 2)
		{
			c[] += a[2][] - a[0][];
		}
		if (i & 4)
		{
			c[] += a[3][] - a[0][];
		}
		f ~= c;
	}
	debug {writefln ("Candidate:\n%(%(%s %)\n%)", f);}

	int [MAX_D] [] g = a[MAX_N / 2..$].dup;
search_loop:
	foreach (i; [3, 5, 6, 7])
	{
		foreach (ref c; g)
		{
			do
			{
				if (c == f[i])
				{
					c[] = int.min;
					continue search_loop;
				}
			}
			while (nextPermutation (c[]));
		}
		return null;
	}

	return f;
}

void answer (int [MAX_D] [] f, int [MAX_D] [] c)
{
	writeln ("YES");
answer_loop:
	do
	{
local_loop:
		foreach (i; 0..MAX_N)
		{
			sort (c[i][]);
			do
			{
				if (f[i][] == c[i][])
				{
					continue local_loop;
				}
			}
			while (nextPermutation (c[i][]));
			continue answer_loop;
		}
		writefln ("%(%(%s %)\n%)", f);
		return;
	}
	while (nextPermutation (f));
	assert (false);
}

void main ()
{
	int [MAX_D] [] a = new int [MAX_D] [MAX_N];
	foreach (i; 0..MAX_N)
	{
		foreach (j; 0..MAX_D)
		{
			readf (" %s", &a[i][j]);
		}
	}

	int [MAX_D] [] c = a.dup;
	foreach (i; 0..MAX_N)
	{
		sort (a[i][]);
	}
	sort (a);

	int [MAX_D] [] b = new int [MAX_D] [MAX_N / 2];
	foreach (i; 0..MAX_N / 2)
	{
		foreach (j; 0..MAX_D)
		{
			b[i][j] = int.min;
		}
	}

	do
	{
		if (b[] == a[0..MAX_N / 2])
		{
			continue;
		}
		b[] = a[0..MAX_N / 2];
		do
		{
			do
			{
				do
				{
					do
					{
						auto f = build (a);
						if (f !is null)
						{
							answer (f, c);
							return;
						}
					}
					while (nextPermutation (a[3][]));
				}
				while (nextPermutation (a[2][]));
			}
			while (nextPermutation (a[1][]));
		}
		while (nextPermutation (a[0][]));
	}
	while (nextPermutation (a[1..$]));

	writeln ("NO");
}

// the following is a Phobos snipped with "ref" stripped
// library code start
bool nextPermutation(alias less="a<b", BidirectionalRange)
                    (BidirectionalRange range)
    if (isBidirectionalRange!BidirectionalRange &&
        hasSwappableElements!BidirectionalRange)
{
    // Ranges of 0 or 1 element have no distinct permutations.
    if (range.empty) return false;

    auto i = retro(range);
    auto last = i.save;

    // Find last occurring increasing pair of elements
    size_t n = 1;
    for (i.popFront(); !i.empty; i.popFront(), last.popFront(), n++)
    {
        if (binaryFun!less(i.front, last.front))
            break;
    }

    if (i.empty) {
        // Entire range is decreasing: it's lexicographically the greatest. So
        // wrap it around.
        range.reverse();
        return false;
    }

    // Find last element greater than i.front.
    auto j = find!((a) => binaryFun!less(i.front, a))(
                   takeExactly(retro(range), n));

    assert(!j.empty);   // shouldn't happen since i.front < last.front
    swap(i.front, j.front);
    reverse(takeExactly(retro(range), n));

    return true;
}
// library code end

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;
import core.bitop;

immutable int MAX_N = 8;
immutable int MAX_D = 3;

long dist (int [MAX_D] p, int [MAX_D] q)
{
	long res = 0L;
	foreach (j; 0..MAX_D)
	{
        	long cur = q[j] - p[j];
        	res += cur * cur;
	}
	return res;
}

int [MAX_D] [] build (int [MAX_D] [] a)
{
	long d01 = dist (a[0], a[1]);
	if (d01 == 0L)
	{
		return null;
	}
	long d02 = dist (a[0], a[2]);
	if (d02 != d01)
	{
		return null;
	}
	long d03 = dist (a[0], a[3]);
	if (d03 != d01)
	{
		return null;
	}
	long d12 = dist (a[1], a[2]);
	if (d12 != d01 * 2)
	{
		return null;
	}
	long d13 = dist (a[1], a[3]);
	if (d13 != d12)
	{
		return null;
	}
	long d23 = dist (a[2], a[3]);
	if (d23 != d12)
	{
		return null;
	}

	int [MAX_D] [] f;
	foreach (i; 0..MAX_N)
	{
		int [MAX_D] c = a[0];
		if (i & 1)
		{
			c[] += a[1][] - a[0][];
		}
		if (i & 2)
		{
			c[] += a[2][] - a[0][];
		}
		if (i & 4)
		{
			c[] += a[3][] - a[0][];
		}
		f ~= c;
	}
	debug {writefln ("Candidate:\n%(%(%s %)\n%)", f);}

	int [MAX_D] [] g = a[MAX_N / 2..$].dup;
search_loop:
	foreach (i; [3, 5, 6, 7])
	{
		foreach (ref c; g)
		{
			do
			{
				if (c == f[i])
				{
					c[] = int.min;
					continue search_loop;
				}
			}
			while (nextPermutation (c[]));
		}
		return null;
	}

	return f;
}

void main ()
{
	int [MAX_D] [] a = new int [MAX_D] [MAX_N];
	foreach (i; 0..MAX_N)
	{
		foreach (j; 0..MAX_D)
		{
			readf (" %s", &a[i][j]);
		}
		sort (a[i][]);
	}
	sort (a);

	int [MAX_D] [] b = new int [MAX_D] [MAX_N / 2];
	foreach (i; 0..MAX_N / 2)
	{
		foreach (j; 0..MAX_D)
		{
			b[i][j] = int.min;
		}
	}

	do
	{
		if (b[] == a[0..MAX_N / 2])
		{
			continue;
		}
		b[] = a[0..MAX_N / 2];
		do
		{
			do
			{
				do
				{
					do
					{
						auto f = build (a);
						if (f !is null)
						{
							writeln ("YES");
							writefln
							    ("%(%(%s %)\n%)",
							    f);
							return;
						}
					}
					while (nextPermutation (a[3][]));
				}
				while (nextPermutation (a[2][]));
			}
			while (nextPermutation (a[1][]));
		}
		while (nextPermutation (a[0][]));
	}
	while (nextPermutation (a[1..$]));

	writeln ("NO");
}

// the following is a Phobos snipped with "ref" stripped
// library code start
bool nextPermutation(alias less="a<b", BidirectionalRange)
                    (BidirectionalRange range)
    if (isBidirectionalRange!BidirectionalRange &&
        hasSwappableElements!BidirectionalRange)
{
    // Ranges of 0 or 1 element have no distinct permutations.
    if (range.empty) return false;

    auto i = retro(range);
    auto last = i.save;

    // Find last occurring increasing pair of elements
    size_t n = 1;
    for (i.popFront(); !i.empty; i.popFront(), last.popFront(), n++)
    {
        if (binaryFun!less(i.front, last.front))
            break;
    }

    if (i.empty) {
        // Entire range is decreasing: it's lexicographically the greatest. So
        // wrap it around.
        range.reverse();
        return false;
    }

    // Find last element greater than i.front.
    auto j = find!((a) => binaryFun!less(i.front, a))(
                   takeExactly(retro(range), n));

    assert(!j.empty);   // shouldn't happen since i.front < last.front
    swap(i.front, j.front);
    reverse(takeExactly(retro(range), n));

    return true;
}
// library code end

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int[][] PERM3S = [
	[ 0, 1, 2 ],
	[ 0, 2, 1 ],
	[ 1, 0, 2 ],
	[ 1, 2, 0 ],
	[ 2, 0, 1 ],
	[ 2, 1, 0 ],
];

long dot(long[] a, long[] b) {
	long ret;
	foreach (d; 0 .. 3) {
		ret += a[d] * b[d];
	}
	return ret;
}

long[][] A;

void solve() {
	foreach (i; 0 .. 8) {
		foreach (j; 0 .. 8) foreach (k; 0 .. 8) foreach (l; 0 .. 8) {
			if (j != i && k != i && l != i && j < k && k < l) {
				foreach (sj; 0 .. 6) foreach (sk; 0 .. 6) foreach (sl; 0 .. 6) {
					long[][] vs = new long[][](8, 3);
					foreach (d; 0 .. 3) {
						vs[1][d] = A[j][PERM3S[sj][d]] - A[i][d];
						vs[2][d] = A[k][PERM3S[sk][d]] - A[i][d];
						vs[4][d] = A[l][PERM3S[sl][d]] - A[i][d];
					}

					const pjj = dot(vs[1], vs[1]);
					const pjk = dot(vs[1], vs[2]);
					const pjl = dot(vs[1], vs[4]);
					const pkk = dot(vs[2], vs[2]);
					const pkl = dot(vs[2], vs[4]);
					const pll = dot(vs[4], vs[4]);
					if (pjj == pkk && pkk == pll && pll > 0 && pjk == 0 && pjl == 0 && pkl == 0) {
// writeln(vs[1]," ",vs[2]," ",vs[4]);
						long[][] as, bs;
						foreach (m; 0 .. 8) {
							if (m != i && m != j && m != k && m != l) {
								as ~= A[m];
							}
						}
						foreach (h; 0 .. 8) if (h & (h - 1)) {
							long[] b = new long[3];
							if (h & 1) b[] += vs[1][];
							if (h & 2) b[] += vs[2][];
							if (h & 4) b[] += vs[4][];
							bs ~= b;
						}
						assert(as.length == 4);
						assert(bs.length == 4);
						int[] perm = [ 0, 1, 2, 3 ];
						do {
							bool ok = true;
							int[] ss;
							foreach (x; 0 .. 4) {
								bool found;
								foreach (s; 0 .. 6) {
									bool eq = true;
									foreach (d; 0 .. 3) {
										eq = eq && (bs[x][d] == as[perm[x]][PERM3S[s][d]] - A[i][d]);
									}
									if (eq) {
										found = true;
										ss ~= s;
										break;
									}
								}
								ok = ok && found;
							}
							if (ok) {
								writeln("YES");
								int pos;
								foreach (h; 0 .. 8) {
									if (h == i) {
										writeln(A[i].to!string.removechars("[],"));
									} else if (h == j) {
										foreach (d; 0 .. 3) {
											if (d > 0) write(" ");
											write(A[j][PERM3S[sj][d]]);
										}
										writeln;
									} else if (h == k) {
										foreach (d; 0 .. 3) {
											if (d > 0) write(" ");
											write(A[k][PERM3S[sk][d]]);
										}
										writeln;
									} else if (h == l) {
										foreach (d; 0 .. 3) {
											if (d > 0) write(" ");
											write(A[l][PERM3S[sl][d]]);
										}
										writeln;
									} else {
										const s = ss[pos];
										foreach (d; 0 .. 3) {
											if (d > 0) write(" ");
											write(as[pos][PERM3S[s][d]]);
										}
										writeln;
										++pos;
									}
								}
								return;
							}
						} while (perm.nextPermutation);
					}
				}
			}
		}
	}
	writeln("NO");
}

void main(string[] args) {
	try {
	for (; ; ) {
		A = new long[][](8, 3);
		foreach (i; 0 .. 8) {
			foreach (d; 0 .. 3) {
				A[i][d] = readLong;
			}
		}

		solve;
	}
	} catch (EOFException) {}
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int[][] PERM3S = [
	[ 0, 1, 2 ],
	[ 0, 2, 1 ],
	[ 1, 0, 2 ],
	[ 1, 2, 0 ],
	[ 2, 0, 1 ],
	[ 2, 1, 0 ],
];

long dot(long[] a, long[] b) {
	long ret;
	foreach (d; 0 .. 3) {
		ret += a[d] * b[d];
	}
	return ret;
}

long[][] A;

void solve() {
	foreach (i; 0 .. 8) {
		foreach (j; 0 .. 8) foreach (k; 0 .. 8) foreach (l; 0 .. 8) {
			if (j != i && k != i && l != i && j < k && k < l) {
				foreach (sj; 0 .. 6) foreach (sk; 0 .. 6) foreach (sl; 0 .. 6) {
					long[][] vs = new long[][](8, 3);
					foreach (d; 0 .. 3) {
						vs[1][d] = A[j][PERM3S[sj][d]] - A[i][d];
						vs[2][d] = A[k][PERM3S[sk][d]] - A[i][d];
						vs[4][d] = A[l][PERM3S[sl][d]] - A[i][d];
					}

					const pjj = dot(vs[1], vs[1]);
					const pjk = dot(vs[1], vs[2]);
					const pjl = dot(vs[1], vs[4]);
					const pkk = dot(vs[2], vs[2]);
					const pkl = dot(vs[2], vs[4]);
					const pll = dot(vs[4], vs[4]);
					if (pjj == pkk && pkk == pll && pll > 0 && pjk == 0 && pjl == 0 && pkl == 0) {
// writeln(vs[1]," ",vs[2]," ",vs[4]);
						long[][] as, bs;
						foreach (m; 0 .. 8) {
							if (m != i && m != j && m != k && m != l) {
								as ~= A[m];
							}
						}
						foreach (h; 0 .. 8) if (h & (h - 1)) {
							long[] b = new long[3];
							if (h & 1) b[] += vs[1][];
							if (h & 2) b[] += vs[2][];
							if (h & 4) b[] += vs[4][];
							bs ~= b;
						}
						assert(as.length == 4);
						assert(bs.length == 4);
						int[] perm = [ 0, 1, 2, 3 ];
						do {
							bool ok = true;
							int[] ss;
							foreach (x; 0 .. 4) {
								bool found;
								foreach (s; 0 .. 6) {
									bool eq = true;
									foreach (d; 0 .. 3) {
										eq = eq && (bs[x][d] == as[perm[x]][PERM3S[s][d]] - A[i][d]);
									}
									if (eq) {
										found = true;
										ss ~= s;
										break;
									}
								}
								ok = ok && found;
							}
							if (ok) {
								writeln("YES");
								int pos;
								foreach (h; 0 .. 8) {
									if (h == i) {
										writeln(A[i].to!string.removechars("[],"));
									} else if (h == j) {
										writeln(vs[1].to!string.removechars("[],"));
									} else if (h == k) {
										writeln(vs[2].to!string.removechars("[],"));
									} else if (h == l) {
										writeln(vs[4].to!string.removechars("[],"));
									} else {
										const s = ss[pos];
										foreach (d; 0 .. 3) {
											if (d > 0) write(" ");
											write(as[pos][PERM3S[s][d]]);
										}
										writeln;
										++pos;
									}
								}
								return;
							}
						} while (perm.nextPermutation);
					}
				}
			}
		}
	}
	writeln("NO");
}

void main(string[] args) {
	try {
	for (; ; ) {
		A = new long[][](8, 3);
		foreach (i; 0 .. 8) {
			foreach (d; 0 .. 3) {
				A[i][d] = readLong;
			}
		}

		solve;
	}
	} catch (EOFException) {}
}


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

void solve()
{
    auto N = readln.chomp.to!long;
    long x;
    foreach (long i; 0..32) {
        x += x + (2L^^i)^^2;
        if (N >= x) {
            N -= x;
        } else {
            writeln(i);
            return;
        }
    }
}

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

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

alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void solve(){
    long n;
    n = rd;
    long tim = 2;
    ll[] arr,  pref;
    arr ~= 1;
    pref ~= 1;
    foreach(i; 1..32){
        arr ~= 2*arr[i-1] + tim * tim;
        tim *= 2;
        pref ~= arr[i] + pref[i-1];
    }
    /* writeln(pref); */
    ll maxi = 0;
    foreach(i; 1..32){
        if(pref[i] > n){
            maxi = i;
            break;
        }
    }
    /* writeln(arr); */
    writeln(maxi);
}

void main(){
    long t = 1;
    t = rd;
    while(t--) solve();
    stdout.flush;
}

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 x = RD-1;
		ans[ti] = 1;
		long y = 1;
		long cnt = 1;
		foreach (i; 0..10^^7)
		{
			y *= 2;
			long z = cnt*2 + y^^2;
			if (z > x) break;
			x -= z;
			++ans[ti];
			cnt = z;
		}
	}

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

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

alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void solve(){
    long n;
    n = rd;
    long tim = 2;
    ll[] arr;
    arr ~= 1;
    foreach(i; 1..62){
        arr ~= 2*arr[i-1] + tim * tim;
        if(n < arr.back){
            break;
        }
        tim *= 2;
    }
    arr.popBack;
    /* writeln(arr); */
    writeln(arr.length);
}

void main(){
    long t = 1;
    t = rd;
    while(t--) solve();
    stdout.flush;
}

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 x = RD-1;
		ans[ti] = 1;
		long y = 1;
		foreach (i; 0..10^^7)
		{
			y *= 2;
			long z = y + y^^2;
			if (z > x) break;
			x -= z;
			++ans[ti];
		}
	}

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


import std.algorithm;
import std.container;
import std.exception;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int MED_L = 1 << 17;
immutable int MAX_L = MED_L << 1;

void main ()
{
	int n, m;
	while (scanf (" %d %d", &n, &m) > 0)
	{
		auto a = new int [n];
		foreach (i; 0..n)
		{
			scanf (" %d", &a[i]);
		}
		auto b = new int [n];
		foreach (i; 0..n)
		{
			scanf (" %d", &b[i]);
		}

		auto x = new int [m];
		auto y = new int [m];
		auto z = new int [m];
		auto t = new int [MAX_L];
		t[] = -1;

		void mark (int q, int lo, int hi)
		{
			lo += MED_L;
			hi += MED_L;
			while (lo <= hi)
			{
				if (lo & 1)
				{
					debug {writefln ("t[%s] = %s", lo, q);}
					t[lo] = q;
					lo++;
				}
				if (!(hi & 1))
				{
					debug {writefln ("t[%s] = %s", hi, q);}
					t[hi] = q;
					hi--;
				}
				lo >>= 1;
				hi >>= 1;
			}
		}

		int value (int p)
		{
			int res = b[p];
			int q = -1;
			for (int s = p + MED_L; s > 0; s >>= 1)
			{
				q = max (q, t[s]);
			}
			if (q != -1)
			{
				debug {writefln ("q = %s, p = %s", q, p);}
				debug {writefln ("index = %s + %s - %s = %s",
				                 x[q], p, y[q],
				                 x[q] + p - y[q]);}
				res = a[x[q] + p - y[q]];
			}
			return res;
		}

		foreach (j; 0..m)
		{
			int k;
			scanf (" %d", &k);
			if (k == 1)
			{
				scanf (" %d %d %d", &x[j], &y[j], &z[j]);
				x[j]--;
				y[j]--;
				mark (j, y[j], y[j] + z[j] - 1);
			}
			else if (k == 2)
			{
				int p;
				scanf (" %d", &p);
				debug {writefln ("p = %d", p);}
				p--;
				int v = value (p);
				printf ("%d\n", v);
			}
			else
			{
				assert (false);
			}
		}
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
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()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

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

    struct q {
        int t, x, y, k;
    }

    auto qs = new q[] (m);
    foreach (i; 0 .. m) {
        readf("%s", &qs[i].t);
        if (qs[i].t == 1) {
            readf(" %s %s %s", &qs[i].x, &qs[i].y, &qs[i].k);
        } else {
            readf(" %s", &qs[i].x);
        }
        readln;
    }

    debug { qs.writeln; }

    auto ans = new int[] (m);

    alias elem = Tuple!(int, int);
    auto rbt = make!(RedBlackTree!elem);
    foreach_reverse (i, cq; qs) {
        if (cq.t == 2) {
            rbt.insert(tuple(cq.x, i.to!int));
        } else {
            auto seq = rbt.upperBound(tuple(cq.y, 0)).until!(t => t[0] > cq.y + cq.k - 1);
            debug { cq.y.writeln; seq.writeln; }

            foreach (e; seq) {
                ans[e[1]] = a[cq.x + e[0] - cq.y - 1];
            }

            auto cpy = seq.array.dup;
            foreach (e; cpy) { rbt.removeKey(e); }
        }
    }

    foreach (e; rbt) {
        ans[e[1]] = b[e[0] - 1];
    }

    foreach (i; 0 .. m) {
        if (qs[i].t == 1) { continue; }

        ans[i].writeln;
    }
}

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

alias Tuple!(int, "x", int, "y") Pair;

class SegmentTree {
    int l;
    int r;
    Pair val;
    SegmentTree l_son;
    SegmentTree r_son;

    this(int l, int r) {
        this.l = l;
        this.r = r;
        this.val.x = this.val.y = -1;
        if (l != r) {
            this.l_son = new SegmentTree(l, (l+r)/2);
            this.r_son = new SegmentTree((l+r)/2+1, r);
        }
    }

    void Upd() {
        if (this.val.x != -1) {
            this.l_son.val = this.val;
            this.r_son.val = this.val;
            this.val.x = this.val.y = -1;
        }
    }

    void Col(int l, int r, int x, int y) {
        if (l > this.r || r < this.l) {
            return;
        }
        if (l <= this.l && r >= this.r) {
            this.val.x = x;
            this.val.y = y;
            return;
        }
        Upd();
        this.l_son.Col(l, r, x, y);
        this.r_son.Col(l, r, x, y);
    }

    Pair Val(int pos) {
        if (this.val.x != -1 || this.l == this.r) {
            return this.val;
        }
        return pos <= this.l_son.r ? this.l_son.Val(pos) : this.r_son.Val(pos);
    }
}

void main() {
    //stdin.open("input.txt", "r");
    //stdout.open("output.txt", "w");
    int n;
    int m;
    readf("%d %d ", &n, &m);
    int[] a = new int[n];
    int[] b = new int[n];
    foreach (i; 0..n) {
        readf("%d ", &a[i]);
    }
    foreach (i; 0..n) {
        readf("%d ", &b[i]);
    }
    SegmentTree tree = new SegmentTree(0, n-1);
    foreach (i; 0..m) {
        int t;
        readf("%d ", &t);
        if (t == 1) {
            int x;
            int y;
            int k;
            readf("%d %d %d ", &x, &y, &k);
            x--;
            y--;
            tree.Col(y, y+k-1, x, y);
        } else {
            int pos;
            readf("%d ", &pos);
            pos--;
            Pair cur_val = tree.Val(pos);
            if (cur_val.x == -1) {
                writeln(b[pos]);
            } else {
                writeln(a[cur_val.x + pos - cur_val.y]);
            }
        }
    }
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
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()
{
    int n, m;
    readf("%s %s", &n, &m);
    readln;

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

    struct q {
        int t, x, y, k;
    }

    auto qs = new q[] (m);
    foreach (i; 0 .. m) {
        readf("%s", &qs[i].t);
        if (qs[i].t == 1) {
            readf(" %s %s %s", &qs[i].x, &qs[i].y, &qs[i].k);
        } else {
            readf(" %s", &qs[i].x);
        }
        readln;
    }

    debug { qs.writeln; }

    auto ans = new int[] (m);

    alias elem = Tuple!(int, int);
    auto rbt = make!(RedBlackTree!elem);
    foreach_reverse (i, cq; qs) {
        if (cq.t == 2) {
            rbt.insert(tuple(cq.x, i.to!int));
        } else {
            auto seq = rbt.upperBound(tuple(cq.y, 0)).until!(t => t[0] > cq.y + cq.k);
            debug { cq.y.writeln; seq.writeln; }

            foreach (e; seq) {
                ans[e[1]] = a[cq.x + e[0] - cq.y - 1];
            }

            auto cpy = seq.array.dup;
            foreach (e; cpy) { rbt.removeKey(e); }
        }
    }

    foreach (e; rbt) {
        ans[e[1]] = b[e[0] - 1];
    }

    foreach (i; 0 .. m) {
        if (qs[i].t == 1) { continue; }

        ans[i].writeln;
    }
}


import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

alias Pos = Tuple!(int, `y`, int, `x`);

enum { U, R, D, L }

char[101][100] grid;
char[100][100] way;
Pos[10^^4] q;
char[4] dirs = "URDL";
Pos cur, finish;

void move(int d) {
    writeln(dirs[d]);
    stdout.flush();
    readf(" %s %s", &cur.y, &cur.x);
    if (!~cur.y)
        exit(1);
    cur.y--;
    cur.x--;
    if (cur == finish)
        exit(0);
}

void swapLR() {
    dirs[R] = 'L';
    dirs[L] = 'R';
}

void swapUD() {
    dirs[D] = 'U';
    dirs[U] = 'D';
}

void main() {
    int n, m;
    readf("%s %s ", &n, &m);
    memset(grid.ptr, '*', grid.sizeof);
    foreach (int i, ref row; grid[0 .. n]) {
        auto s = row[ ];
        readln(s);
        s[$ - 1] = '*';
        auto tail = s[0 .. m].find('F');
        if (!tail.empty)
            finish = Pos(i, m - cast(int)tail.length);
    }
    assert(finish.y || finish.x);

    if (n == 1) {
        move(R);
        if (!cur.x)
            swapLR();
        while (true)
            move(R);
    }

    if (m == 1) {
        move(D);
        if (!cur.y)
            swapUD();
        while (true)
            move(D);
    }

    const lrFound = grid[0][1] != '*';
    if (lrFound) {
        move(R);
        if (!cur.x)
            swapLR();
        else
            move(L);
    }

    const udFound = grid[1][0] != '*';
    if (udFound) {
        move(D);
        if (!cur.y)
            swapUD();
        else
            move(U);
    }

    if (!lrFound) {
        assert(udFound);
        foreach (i; 1 .. n) {
            assert(grid[i][0] != '*');
            if (grid[i][1] != '*') {
                foreach (k; 0 .. i)
                    move(D);
                move(R);
                if (!cur.x)
                    swapLR();
                break;
            }
        }
    } else if (!udFound) {
        assert(lrFound);
        foreach (j; 1 .. m) {
            assert(grid[0][j] != '*');
            if (grid[1][j] != '*') {
                foreach (k; 0 .. j)
                    move(R);
                move(D);
                if (!cur.y)
                    swapUD();
                break;
            }
        }
    }

    int qr = 0, qw = 1;
    q[0] = finish;
    while (true) {
        assert(qr != qw);
        Pos p = q[qr++];
        if (p == cur)
            while (true)
                move(way[cur.y][cur.x]);

        enum gen(string cond, char c) = `
            if (`~cond~` && grid[nextPos.y][nextPos.x] != '*') {
                grid[nextPos.y][nextPos.x] = '*';
                way[nextPos.y][nextPos.x] = `~c~`;
                q[qw++] = nextPos;
            }
        `;

        Pos nextPos = Pos(p.y - 1, p.x);
        mixin(gen!(`nextPos.y >= 0`, 'D'));
        nextPos = Pos(p.y, p.x + 1);
        mixin(gen!(`nextPos.x < m`, 'L'));
        nextPos = Pos(p.y + 1, p.x);
        mixin(gen!(`nextPos.y < n`, 'U'));
        nextPos = Pos(p.y, p.x - 1);
        mixin(gen!(`nextPos.x >= 0`, 'R'));
    }
}

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.stdlib;;

void main() {
    immutable int INF = 1 << 28;

    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto B = H.iota.map!(_ => readln.chomp).array;

    int gr, gc;
    foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;

    int[] dr = [0, 0, -1, 1];
    int[] dc = [-1, 1, 0, 0];
    auto dist = new int[](H * W);
    fill(dist, INF);
    dist[0] = 0;
    auto prev = new int[](H * W);
    fill(prev, -1);
    alias Tuple!(int, "from", int, "to", int, "cost") Edge;
    auto q = new BinaryHeap!(Array!Edge, "a.cost >= b.cost");
    q.insert(Edge(-1, 0, 0));

    while (!q.empty) {
        auto n = q.front;
        auto pr = n.from / W;
        auto pc = n.from % W;
        auto r = n.to / W;
        auto c = n.to % W;
        q.removeFront;
        if (dist[n.to] < n.cost)
            continue;
        dist[n.to] = n.cost;
        prev[n.to] = n.from;

        foreach (i; 0..4) {
            auto nr = r + dr[i];
            auto nc = c + dc[i];
            auto next = nr * W + nc;
            if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')
                continue;
            if (dist[next] <= dist[n.to] + 1)
                continue;
            dist[next] = dist[n.to] + 1;
            q.insert(Edge(n.to, next, dist[n.to] + 1));
        }
    }

    int[] root;
    int x = gr * W + gc;
    while (prev[x] != -1) {
        root = x ~ root;
        x = prev[x];
    }

    bool swapLR, swapUD;
    Tuple!(int, int) cmd(char dir) {
        if      (swapLR && dir == 'L') dir = 'R';
        else if (swapLR && dir == 'R') dir = 'L';
        else if (swapUD && dir == 'U') dir = 'D';
        else if (swapUD && dir == 'D') dir = 'U';

        writeln(dir);
        stdout.flush;
        s = readln.split.map!(to!int);

        if (s[0] - 1 == gr && s[1] - 1 == gc)
            exit(0);
        return tuple(s[0] - 1, s[1] - 1);
    }

    Tuple!(int, int) pos;

    if (H == 1) {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        while (true) pos = cmd('R');
        return;
    }
    if (W == 1) {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        while (true) pos = cmd('D');
        return;
    }

    if (B[0][1] != '*') {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        pos = cmd('L');
        while (true) {
            if (B[pos[0]+1][pos[1]] != '*')
                break;
            pos = cmd('R');
        }
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        pos = cmd('U');
        while (pos[1] != 0) pos = cmd('L');
    }
    else {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        pos = cmd('D');
        while (true) {
            if (B[pos[0]][pos[1]+1] != '*')
                break;
            pos = cmd('D');
        }
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        pos = cmd('L');
        while (pos[0] != 0) pos = cmd('U');
    }

    foreach (co; root) {
        int nr = co / W;
        int nc = co % W;
        if (pos[0] - nr == 1) pos = cmd('U');
        else if (pos[0] - nr == -1) pos = cmd('D');
        else if (pos[1] - nc == 1) pos = cmd('L');
        else pos = cmd('R');
    }
}

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

void main() {
    immutable int INF = 1 << 28;

    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto B = H.iota.map!(_ => readln.chomp).array;

    int gr, gc;
    foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;

    int[] dr = [0, 0, -1, 1];
    int[] dc = [-1, 1, 0, 0];
    auto dist = new int[](H * W);
    fill(dist, INF);
    dist[0] = 0;
    auto prev = new int[](H * W);
    fill(prev, -1);
    alias Tuple!(int, "from", int, "to", int, "cost") Edge;
    auto q = new BinaryHeap!(Array!Edge, "a.cost >= b.cost");
    q.insert(Edge(-1, 0, 0));

    while (!q.empty) {
        auto n = q.front;
        auto pr = n.from / W;
        auto pc = n.from % W;
        auto r = n.to / W;
        auto c = n.to % W;
        q.removeFront;
        if (dist[n.to] < n.cost)
            continue;
        dist[n.to] = n.cost;
        prev[n.to] = n.from;

        foreach (i; 0..4) {
            auto nr = r + dr[i];
            auto nc = c + dc[i];
            auto next = nr * W + nc;
            if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')
                continue;
            if (dist[next] <= dist[n.to] + 1)
                continue;
            dist[next] = dist[n.to] + 1;
            q.insert(Edge(n.to, next, dist[n.to] + 1));
        }
    }

    int[] root;
    int x = gr * W + gc;
    while (prev[x] != -1) {
        root = x ~ root;
        x = prev[x];
    }

    bool swapLR, swapUD;
    Tuple!(int, int) cmd(char dir) {
        if      (swapLR && dir == 'L') dir = 'R';
        else if (swapLR && dir == 'R') dir = 'L';
        else if (swapUD && dir == 'U') dir = 'D';
        else if (swapUD && dir == 'D') dir = 'U';

        writeln(dir);
        stdout.flush;
        s = readln.split.map!(to!int);
        return tuple(s[0] - 1, s[1] - 1);
    }

    Tuple!(int, int) pos;

    if (H == 1) {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        while (pos[0] != gr || pos[1] != gc) pos = cmd('R');
        return;
    }
    if (W == 1) {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        while (pos[0] != gr || pos[1] != gc) pos = cmd('D');
        return;
    }

    if (B[0][1] == '.') {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        while (true) {
            if (pos[0] == gr && pos[1] == gc)
                return;
            if (B[pos[0]+1][pos[1]] == '.')
                break;
            pos = cmd('R');
        }
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        pos = cmd('U');
        while (pos[1] != 0) pos = cmd('L');
    }
    else {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        while (true) {
            if (pos[0] == gr && pos[1] == gc)
                return;
            if (B[pos[0]][pos[1]+1] == '.')
                break;
            pos = cmd('D');
        }
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        pos = cmd('L');
        while (pos[0] != 0) pos = cmd('U');
    }

    foreach (co; root) {
        int nr = co / W;
        int nc = co % W;
        if (pos[0] - nr == 1) pos = cmd('U');
        else if (pos[0] - nr == -1) pos = cmd('D');
        else if (pos[1] - nc == 1) pos = cmd('L');
        else pos = cmd('R');
    }
}

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.stdlib;;

void main() {
    immutable int INF = 1 << 28;

    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto B = H.iota.map!(_ => readln.chomp).array;

    int gr, gc;
    foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;

    int[] dr = [0, 0, -1, 1];
    int[] dc = [-1, 1, 0, 0];
    auto dist = new int[](H * W);
    fill(dist, INF);
    dist[0] = 0;
    auto prev = new int[](H * W);
    fill(prev, -1);
    alias Tuple!(int, "from", int, "to", int, "cost") Edge;
    auto q = new BinaryHeap!(Array!Edge, "a.cost >= b.cost");
    q.insert(Edge(-1, 0, 0));

    while (!q.empty) {
        auto n = q.front;
        auto pr = n.from / W;
        auto pc = n.from % W;
        auto r = n.to / W;
        auto c = n.to % W;
        q.removeFront;
        if (dist[n.to] < n.cost)
            continue;
        dist[n.to] = n.cost;
        prev[n.to] = n.from;

        foreach (i; 0..4) {
            auto nr = r + dr[i];
            auto nc = c + dc[i];
            auto next = nr * W + nc;
            if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')
                continue;
            if (dist[next] <= dist[n.to] + 1)
                continue;
            dist[next] = dist[n.to] + 1;
            q.insert(Edge(n.to, next, dist[n.to] + 1));
        }
    }

    int[] root;
    int x = gr * W + gc;
    while (prev[x] != -1) {
        root = x ~ root;
        x = prev[x];
    }

    bool swapLR, swapUD;
    Tuple!(int, int) cmd(char dir) {
        if      (swapLR && dir == 'L') dir = 'R';
        else if (swapLR && dir == 'R') dir = 'L';
        else if (swapUD && dir == 'U') dir = 'D';
        else if (swapUD && dir == 'D') dir = 'U';

        writeln(dir);
        stdout.flush;
        s = readln.split.map!(to!int);

        if (s[0] - 1 == gr && s[1] - 1 == gc)
            exit(0);
        return tuple(s[0] - 1, s[1] - 1);
    }

    Tuple!(int, int) pos;

    if (H == 1) {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        while (true) pos = cmd('R');
        return;
    }
    if (W == 1) {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        while (true) pos = cmd('D');
        return;
    }

    if (B[0][1] != '*') {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        while (true) {
            if (B[pos[0]+1][pos[1]] != '*')
                break;
            pos = cmd('R');
        }
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        pos = cmd('U');
        while (pos[1] != 0) pos = cmd('L');
    }
    else {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        while (true) {
            if (B[pos[0]][pos[1]+1] != '*')
                break;
            pos = cmd('D');
        }
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        pos = cmd('L');
        while (pos[0] != 0) pos = cmd('U');
    }

    foreach (co; root) {
        int nr = co / W;
        int nc = co % W;
        if (pos[0] - nr == 1) pos = cmd('U');
        else if (pos[0] - nr == -1) pos = cmd('D');
        else if (pos[1] - nc == 1) pos = cmd('L');
        else pos = cmd('R');
    }
}

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

void main() {
    immutable int INF = 1 << 28;

    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto B = H.iota.map!(_ => readln.chomp).array;

    int gr, gc;
    foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] == 'F') gr = i, gc = j;

    int[] dr = [0, 0, -1, 1];
    int[] dc = [-1, 1, 0, 0];
    auto dist = new int[](H * W);
    fill(dist, INF);
    dist[0] = 0;
    auto prev = new int[](H * W);
    fill(prev, -1);
    alias Tuple!(int, "from", int, "to", int, "cost") Edge;
    auto q = new BinaryHeap!(Array!Edge, "a.cost >= b.cost");
    q.insert(Edge(-1, 0, 0));

    while (!q.empty) {
        auto n = q.front;
        auto pr = n.from / W;
        auto pc = n.from % W;
        auto r = n.to / W;
        auto c = n.to % W;
        q.removeFront;
        if (dist[n.to] < n.cost)
            continue;
        dist[n.to] = n.cost;
        prev[n.to] = n.from;

        foreach (i; 0..4) {
            auto nr = r + dr[i];
            auto nc = c + dc[i];
            auto next = nr * W + nc;
            if (nr < 0 || nr >= H || nc < 0 || nc >= W || B[nr][nc] == '*')
                continue;
            if (dist[next] <= dist[n.to] + 1)
                continue;
            dist[next] = dist[n.to] + 1;
            q.insert(Edge(n.to, next, dist[n.to] + 1));
        }
    }

    int[] root;
    int x = gr * W + gc;
    while (prev[x] != -1) {
        root = x ~ root;
        x = prev[x];
    }

    bool swapLR, swapUD;
    Tuple!(int, int) cmd(char dir) {
        if      (swapLR && dir == 'L') dir = 'R';
        else if (swapLR && dir == 'R') dir = 'L';
        else if (swapUD && dir == 'U') dir = 'D';
        else if (swapUD && dir == 'D') dir = 'U';

        writeln(dir);
        stdout.flush;
        s = readln.split.map!(to!int);
        return tuple(s[0] - 1, s[1] - 1);
    }

    Tuple!(int, int) pos;

    if (H == 1) {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        while (pos[0] != gr || pos[1] != gc) pos = cmd('R');
        return;
    }
    if (W == 1) {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        while (pos[0] != gr || pos[1] != gc) pos = cmd('D');
        return;
    }

    if (B[0][1] == '.') {
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        while (true) {
            if (pos[0] == gr && pos[1] == gc)
                return;
            if (B[pos[0]+1][pos[1]] == '.')
                break;
            pos = cmd('R');
        }
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        pos = cmd('U');
        while (pos[1] != 0) pos = cmd('L');
    }
    else {
        pos = cmd('D');
        if (pos[0] == 0) swapUD = true;
        while (true) {
            if (pos[0] == gr && pos[1] == gc)
                return;
            if (B[pos[0]][pos[1]+1] == '.')
                break;
            pos = cmd('D');
        }
        pos = cmd('R');
        if (pos[1] == 0) swapLR = true;
        pos = cmd('L');
        while (pos[0] != 0) pos = cmd('U');
    }

    foreach (co; root) {
        int nr = co / W;
        int nc = co % W;
        if (pos[0] - nr == 1) pos = cmd('U');
        else if (pos[0] - nr == -1) pos = cmd('D');
        else if (pos[0] - nc == 1) pos = cmd('L');
        else pos = cmd('R');
    }
}


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

bool ask (int x, int y)
{
	writeln ("?", " ", x, " ", y);
	stdout.flush ();
	auto s = readln.strip;
	if (s == "e")
	{
		assert (false);
	}
	return (s == "x");
}

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		if (s == "end")
		{
			break;
		}
		if (s != "start")
		{
			assert (false);
		}

		immutable int start = 0;
		int x = start;
		int y = x;
		int d = 1;
		do
		{
			x = y;
			y += d;
			d *= 2;
		}
		while (!ask (x, y));

		int lo = x + 1;
		int hi = y;
		while (lo != hi)
		{
			auto me = (lo + hi) / 2;
			if (ask (x, me))
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}

		writeln ("!", " ", lo);
		stdout.flush ();
	}
}

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

long X;
int cnt = 0;

void ans(long x) {
    debug {
        writeln((x == X) ? "OK ": "NG");
        writeln(cnt);
        return;
    }
    writeln("! ", x);
    stdout.flush;
}

bool ask(long x, long y) {
    debug {
        cnt += 1;
        return x % X >= y % X;
    }
    writeln("? ", x, " ", y);
    stdout.flush;
    return readln.chomp == "x";
}

void solve() {
    if (ask(1, 2)) {
        if (ask(2, 1)) {
            ans(1);
        } else {
            ans(2);
        }
        return;
    }

    long mn = 2;
    long mx = 4;

    while (true) {
        if (ask(mn, mx)) {
            break;
        } else {
            mx *= 2;
            mn *= 2;
        }
    }

    while (mx - mn > 1) {
        long next_mn = (mx + mn) / 2;
        if (ask(next_mn, mx)) {
            mn = next_mn;
        } else {
            mx = next_mn;
        }
    }

    ans (mx);
}

void main() {
    debug {
        X = readln.chomp.to!int;
        solve;
        return;
    }
    while (true) {
        auto s = readln.chomp;
        if (s == "start") solve;
        else break;
    }
}


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

immutable int INF = 10L ^^ 9 + 23;

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

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

    arr.sort().reverse();

    debug { arr.writeln; }

    bool isOk(int md) {
        long eng = 0;
        int sb = 0;
        foreach (i, e; arr) {
            if (i > 0 && i % md == 0) { sb += 1; }

            eng += max(e - sb, 0);
        }

        return eng >= m;
    }

    int le = 1, r = n+1;
    while (le < r) {
        int md = (le + r) / 2;
        if (isOk(md)) { r = md; }
        else { le = md + 1; }
    }

    writeln(le == n+1 ? -1 : le);
}

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

  int n;
  long m;
  rd(n, m);
  auto a = readln.split.to!(long[]);

  sort!"a>b"(a);
  if (m > reduce!"a+b"(a)) {
    writeln(-1);
    return;
  }
  long _s = 0;
  foreach (int i; 0 .. n) {
    _s += max(0L, a[i] - i);
  }
  if (_s >= m) {
    writeln(1);
    return;
  }
  bool enough(long k) {
    long s = 0;
    foreach (i; 0 .. n) {
      s += max(0L, a[i] - (i / k));
    }
    return s >= m;
  }

  long ng = 1, ok = n;
  while (ok - ng > 1) {
    auto md = (ok + ng) / 2;
    if (enough(md)) {
      ok = md;
    } else {
      ng = md;
    }
  }
  writeln(ok);
}

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.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip, std.regex;

immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Tuple!(int, int)[](N);
    foreach (i; 0..N) {
        auto s = readln.split.map!(to!int);
        A[i] = tuple(s[0], s[1]);
    }

    Tuple!(int, int)[][int] B;
    foreach (i, a; A.enumerate)
        B[a[0]] ~= tuple(a[1], (i+1).to!int);

    foreach (k; B.keys)
        B[k].sort();

    foreach (k; B.keys)
        if (B[k].length > 1) {
            writeln(B[k].front[1], " ", B[k].back[1]);
            return;
        }

    auto K = B.keys.dup;
    K.sort!"a > b"();
    Tuple!(int, int) minv = B[K[0]].front;

    foreach (i; 1..K.length) {
        if (B[K[i]].back[0] >= minv[0]) {
            writeln(minv[1], " ", B[K[i]].back[1]);
            return;
        }
        minv = min(minv, B[K[i]].front);
    }

    writeln(-1, " ", -1);
}

import std.stdio, std.string;
import std.typecons: Tuple, tuple;
import std.algorithm;

bool within(Tuple!(int, int, int) t0, Tuple!(int, int, int) t1)
{
    if (t0[0] >= t1[0] && t0[1] <= t1[1])
    {
        return true;
    }
    return false;
}

void solve(Tuple!(int, int, int)[] s)
{
    s.sort();
    foreach (idx; 0 .. s.length - 1)
    {
        if (within(s[idx], s[idx + 1]))
        {
            writeln(s[idx][2], " ", s[idx + 1][2]);
            return;
        }
        if (within(s[idx + 1], s[idx]))
        {
            writeln(s[idx + 1][2], " ", s[idx][2]);
            return;
        }
    }
    writeln("-1 -1");
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto s = new Tuple!(int, int, int)[n];
        foreach (i; 0 .. n)
        {
            int l, r;
            readf("%d %d\n", &l, &r);
            s[i] = tuple(l, r, i + 1);
        }
        solve(s);
    }
    return 0;
}

import std.algorithm.sorting;
import std.conv;
import std.stdio;
import std.typecons;

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

  Tuple!(uint, uint, uint)[] x;

  foreach (i; 0..n) {
    uint a, b;

    readf!"%d %d\n"(a, b);

    x ~= tuple(a, b, i+1);
  }

  x.sort!((a, b) => (a[0] == b[0]) ? (a[1] > b[1]) : (a[0] < b[0]));

  // current max
  auto m = x[0][1];
  auto mi = x[0][2];

  foreach (i; 1..n) {
    if (x[i][0] <= m && x[i][1] <= m) {
      writefln("%d %d", x[i][2], mi);
      return;
    }

    if (x[i][1] > m) {
      m = x[i][1];
      mi = x[i][2];
    }
  }
  writeln("-1 -1");
}

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

void main()
{
    int n;
    readf ("%s", &n);
    readln;
    Tuple!(ulong, ulong)[] r;
    foreach (_; 0..n) {
        auto nrs = readln.splitter.map!(to!ulong).array;
        r ~= tuple(nrs[0], nrs[1]);
    }
    int[] b = new int[] (r.length);
    makeIndex!(q{a[0] != b[0] ? a[0] < b[0] : a[1] > b[1]})(r, b);

    debug { writeln (r); }
    debug { writeln (b); }

    int outer = cast(int) b.front;
    auto ans = tuple(-1, -1);
    foreach (p; b.dropOne()) {
        if (r[p][1] <= r[outer][1]) {
            ans = tuple(cast(int) p, outer);
            break;
        } else {
            outer = cast(int) p;
        }
    }

    if (ans[0] != -1) ans[0] += 1, ans[1] += 1;
    writeln (ans[0], ' ', ans[1]);
}

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

void main()
{
    int n;
    readf ("%s", &n);
    readln;
    Tuple!(int, int)[] r;
    foreach (_; 0..n) {
        auto nrs = readln.splitter.map!(to!int).array;
        r ~= tuple(nrs[0], nrs[1]);
    }
    auto b = r.length.iota.array
        .multiSort!((x, y) => r[x][0] < r[y][0], (x,y) => r[x][1] > r[y][1]);

    debug { writeln (r); }
    debug { writeln (b); }

    int right = cast(int) b.front();
    auto ans = tuple(-1, -1);
    foreach (p; b.dropOne()) {
        debug { writeln(p); }
        if (r[p][1] <= r[right][1]) {
            ans = tuple(cast(int) p, right);
            break;
        } else {
            right = cast(int) p;
        }
    }

    if (ans[0] != -1) ans[0] += 1, ans[1] += 1;
    writeln (ans[0], ' ', ans[1]);
}

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](0);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq~=P(a, b);
  }

  struct LRi{int l, r, id;}
  auto lri=new LRi[](0);
  foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);
  sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l<b.l))(lri);
  struct Ri{int r, id;}
  import std.container;
  auto tree=new RedBlackTree!(Ri, "a.r==b.r ? a.id<b.id : a.r<b.r", true);
  foreach(i; 0..n) tree.insert(Ri(seq[i].r, i));
  foreach(elem; lri){
    // writeln(elem.id, " ", elem.l, " ", elem.r);
    int id=elem.id, r=seq[id].r;
    // writeln(id, " ", r);
    // writeln(tree);
    tree.removeKey(Ri(r, id));
    auto eq=tree.equalRange(Ri(r, -1));
    if((eq.empty)==false){
      writeln(-1, " ", -1); return;
      writeln(eq.front.id+1, " ", id+1);
      goto heaven;
    }else{
      auto lb=tree.lowerBound(Ri(r, 1_000_000_000));
      if(lb.empty) continue;
      writeln(lb.back.id+1, " ", id+1);
      goto heaven;
    }
  }

  writeln(-1, " ", -1);
  heaven:;
}

// rbt.equalRange(elem) の挙動？

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

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](0);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq~=P(a, b);
  }

  struct LRi{int l, r, id;}
  auto lri=new LRi[](0);
  foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);
  sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l<b.l))(lri);
  struct Ri{int r, id;}
  import std.container;
  auto tree=new RedBlackTree!(Ri, "a.r==b.r ? a.id<b.id : a.r<b.r", true);
  foreach(i; 0..n) tree.insert(Ri(seq[i].r, i));
  foreach(elem; lri){
    int id=elem.id, r=seq[id].r;
    tree.removeKey(Ri(r, id));
    auto lb=tree.lowerBound(Ri(r, 1_000_000_000));
    if((lb.empty)==false){
      writeln(lb.back.id+1, " ", id+1);
      goto heaven;
    }
  }

  writeln(-1, " ", -1);
  heaven:;
}

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

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](0);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq~=P(a, b);
  }

  struct LRi{int l, r, id;}
  auto lri=new LRi[](0);
  foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);
  sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l<b.l))(lri);
  struct Ri{int r, id;}
  import std.container;
  auto tree=new RedBlackTree!(Ri, "a.r==b.r ? a.id<b.id : a.r<b.r", true);
  foreach(i; 0..n) tree.insert(Ri(seq[i].r, i));
  foreach(elem; lri){
    // writeln(elem.id, " ", elem.l, " ", elem.r);
    int id=elem.id, r=seq[id].r;
    // writeln(id, " ", r);
    // writeln(tree);
    tree.removeKey(Ri(r, id));
    auto eq=tree.equalRange(Ri(r, -1));
    if((eq.empty)==false){
      writeln(eq.front.id+1, " ", id+1);
      goto heaven;
    }else{
      auto lb=tree.lowerBound(Ri(r, 1_000_000_000));
      if(lb.empty) continue;
      writeln(lb.back.id+1, " ", id+1);
      goto heaven;
    }
  }

  writeln(-1, " ", -1);
  heaven:;
}

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

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

immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Tuple!(int, int)[](N);
    foreach (i; 0..N) {
        auto s = readln.split.map!(to!int);
        A[i] = tuple(s[0], s[1]);
    }

    Tuple!(int, int)[int] B;
    foreach (i, a; A.enumerate) {
        if (a[0] in B && B[a[0]][0] == a[1]) {
            writeln(i + 1, " ", B[a[0]][1]);
            return;
        }
        if (a[0] in B) B[a[0]] = min(B[a[0]], tuple(a[1], (i+1).to!int));
        else B[a[0]] = tuple(a[1], (i+1).to!int);
    }

    auto K = B.keys.dup;
    K.sort!"a > b"();
    Tuple!(int, int) minv = B[K[0]];

    foreach (i; 1..K.length) {
        if (B[K[i]][0] >= minv[0]) {
            writeln(minv[1], " ", B[K[i]][1]);
            return;
        }
        minv = min(minv, B[K[i]]);
    }

    writeln(-1, " ", -1);
}

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

immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Tuple!(int, int)[](N);
    foreach (i; 0..N) {
        auto s = readln.split.map!(to!int);
        A[i] = tuple(s[0], s[1]);
    }

    Tuple!(int, int)[][int] B;
    foreach (i, a; A.enumerate)
        B[a[0]] ~= tuple(a[1], (i+1).to!int);

    foreach (k; B.keys)
        B[k].sort();

    foreach (k; B.keys)
        foreach (i; 0..B[k].length.to!int-1)
            if (B[k][i][0] == B[k][i+1][0]) {
                writeln(B[k][i][1], " ", B[k][i+1][1]);
                return;
            }

    auto K = B.keys.dup;
    K.sort!"a > b"();
    Tuple!(int, int) minv = B[K[0]].front;

    foreach (i; 1..K.length) {
        if (B[K[i]].back[0] >= minv[0]) {
            writeln(minv[1], " ", B[K[i]].front[1]);
            return;
        }
        minv = min(minv, B[K[i]].back);
    }

    writeln(-1, " ", -1);
}

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

immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Tuple!(int, int)[](N);
    foreach (i; 0..N) {
        auto s = readln.split.map!(to!int);
        A[i] = tuple(s[0], s[1]);
    }

    Tuple!(int, int)[][int] B;
    foreach (i, a; A.enumerate)
        B[a[0]] ~= tuple(a[1], (i+1).to!int);

    foreach (k; B.keys)
        B[k].sort();

    foreach (k; B.keys)
        foreach (i; 0..B[k].length.to!int-1)
            if (B[k][i][0] == B[k][i+1][0]) {
                writeln(B[k][i][1], " ", B[k][i+1][1]);
                return;
            }

    auto K = B.keys.dup;
    K.sort!"a > b"();
    Tuple!(int, int) minv = B[K[0]].front;

    foreach (i; 1..K.length) {
        if (B[K[i]].back[0] >= minv[0]) {
            writeln(minv[1], " ", B[K[i]].back[1]);
            return;
        }
        minv = min(minv, B[K[i]].front);
    }

    writeln(-1, " ", -1);
}

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

immutable long INF = 1L << 59;

void main() {
    auto sc = new Scanner(stdin);

    int N; sc.read(N);
    auto A = new Tuple!(int, int)[](N);
    foreach (i; 0..N) {
        int a, b; sc.read(a); sc.read(b);
        A[i] = tuple(a-1, b-1);
    }

    Tuple!(int, int)[][int] B;
    foreach (i, a; A.enumerate)
        B[a[0]] ~= tuple(a[1], (i+1).to!int);

    foreach (k; B.keys)
        B[k].sort();

    foreach (k; B.keys)
        foreach (i; 0..B[k].length.to!int-1)
            if (B[k][i][0] == B[k][i+1][0]) {
                writeln(B[k][i][1], " ", B[k][i+1][1]);
                return;
            }

    auto K = B.keys.dup;
    K.sort!"a > b"();
    Tuple!(int, int) minv = B[K[0]].front;

    foreach (i; 1..K.length) {
        if (B[K[i]].back[0] >= minv[0]) {
            writeln(minv[1], " ", B[K[i]].back[1]);
            return;
        }
        minv = min(minv, B[K[i]].front);
    }

    writeln(-1, " ", -1);
}

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

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

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else {
                auto buf = line.split.map!(to!E).array;
                static if (isStaticArray!T) {

                    assert(buf.length == T.length);
                }
                x = buf;
                line.length = 0;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}

import std.algorithm.sorting;
import std.conv;
import std.stdio;
import std.typecons;

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

  Tuple!(uint, uint)[] x;

  foreach (i; 0..n) {
    uint a, b;

    readf!"%d %d\n"(a, b);

    x ~= tuple(a, b);
  }

  x.sort!((a, b) => (a[0] == b[0]) ? (b[0] > a[0]) : (a[0] < b[0]));

  // current max
  auto m = x[0][1];
  auto mi = 1;

  foreach (i; 1..n) {
    if (x[i][0] <= m && x[i][1] <= m) {
      writefln("%d %d", i+1, mi);
      return;
    }

    if (x[i][1] > m) {
      m = x[i][1];
      mi = i+1;
    }
  }
  writeln("-1 -1");
}

import std.algorithm.sorting;
import std.conv;
import std.stdio;
import std.typecons;

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

  Tuple!(uint, uint, uint)[] x;

  foreach (i; 0..n) {
    uint a, b;

    readf!"%d %d\n"(a, b);

    x ~= tuple(a, b, i+1);
  }

  x.sort!((a, b) => (a[0] == b[0]) ? (b[0] > a[0]) : (a[0] < b[0]));

  // current max
  auto m = x[0][1];
  auto mi = x[0][2];

  foreach (i; 1..n) {
    if (x[i][0] <= m && x[i][1] <= m) {
      writefln("%d %d", x[i][2], mi);
      return;
    }

    if (x[i][1] > m) {
      m = x[i][1];
      mi = x[i][2];
    }
  }
  writeln("-1 -1");
}

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

void main()
{
    int n;
    readf ("%s", &n);
    readln;
    Tuple!(int, int)[] a;
    foreach (_; 0..n) {
        auto nrs = readln.splitter.map!(to!int).array;
        a ~= tuple(nrs[0], nrs[1]);
    }

    debug { writeln (a); }

    a.multiSort!(q{ a[0] < b[0] }, q{ a[1] > b[1] });

    debug { writeln (a); }

    int right = 0;
    auto ans = tuple(-1, -1);
    foreach (int i, e; a.dropOne()) {
        debug { writeln(e); }
        if (e[1] <= a[right][1]) {
            ans = tuple(right, i);
            break;
        } else {
            right = i;
        }
    }

    writeln (ans[0], ' ', ans[1]);
}

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

void main()
{
    int n;
    readf ("%s", &n);
    readln;
    Tuple!(int, int)[] r;
    foreach (_; 0..n) {
        auto nrs = readln.splitter.map!(to!int).array;
        r ~= tuple(nrs[0], nrs[1]);
    }
    auto b = r.length.iota.array;

    debug { writeln (r); }

    b.multiSort!((x, y) => r[x][0] < r[y][0], (x,y) => r[x][1] > r[y][1]);

    debug { writeln (b); }

    int right = 0;
    auto ans = tuple(-1, -1);
    foreach (p; b.dropOne()) {
        debug { writeln(p); }
        auto e = r[p];
        if (e[1] <= r[right][1]) {
            ans = tuple(cast(int) p, right);
            break;
        } else {
            right = cast(int) p;
        }
    }

    if (ans[0] != -1) ans[0] += 1, ans[1] += 1;
    writeln (ans[0], ' ', ans[1]);
}

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

void main()
{
    int n;
    readf ("%s", &n);
    readln;
    Tuple!(int, int)[] a;
    foreach (_; 0..n) {
        auto nrs = readln.splitter.map!(to!int).array;
        a ~= tuple(nrs[0], nrs[1]);
    }

    debug { writeln (a); }

    a.multiSort!(q{ a[0] < b[0] }, q{ a[1] > b[1] });

    debug { writeln (a); }

    int right = 0;
    auto ans = tuple(-1, -1);
    foreach (int i, e; a.dropOne().enumerate(1)) {
        debug { writeln(e); }
        if (e[1] <= a[right][1]) {
            ans = tuple(right, i);
            break;
        } else {
            right = i;
        }
    }

    if (ans[0] != -1) ans[0] += 1, ans[1] += 1;
    writeln (ans[0], ' ', ans[1]);
}

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](0);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq~=P(a, b);
  }

  struct LRi{int l, r, id;}
  auto lri=new LRi[](0);
  foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);
  sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l<b.l))(lri);
  struct Ri{int r, id;}
  import std.container;
  auto tree=new RedBlackTree!(Ri, "a.r==b.r ? a.id<b.id : a.r<b.r", true);
  foreach(i; 0..n) tree.insert(Ri(seq[i].r, i));
  foreach(elem; lri){
    int id=elem.id, r=seq[id].r;
    auto lb=tree.lowerBound(Ri(r, 1_000_000_000));
    if((lb.empty)==false){
      writeln(lb.back.id+1, " ", id+1);
      goto heaven;
    }
  }

  writeln(-1, " ", -1);
  heaven:;
}

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

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](n);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq[i]=P(a, b);
  }

  struct T{int x, idx;}
  import std.container;
  auto tree=new RedBlackTree!(T, "a.x<b.x", true);
  foreach(i; 0..n) tree.insert(T(seq[i].r, i));
  auto y=new T[](n);
  foreach(i; 0..n) y[i]=T(seq[i].l, i);
  sort!"a.x<b.x"(y);
  foreach(i; 0..n){
    // writeln(i);
    int id=y[i].idx;
    auto lb=tree.lowerBound(T(seq[id].r, id));
    if(lb.empty==false){
      writeln(lb.front.idx+1, " ", id+1);
      goto heaven;
    }else{
      auto eq=tree.equalRange(T(seq[id].r, id));
      if((eq.front==eq.back)==false){
        int j1=eq.front.idx.to!int, j2=eq.back.idx.to!(int);
        if(i!=j1){
          writeln(j1+1, " ", id+1);
        }else if(i!=j2){
          writeln(j2+1, " ", id+1);
        }
        goto heaven;
      }
    }
    // writeln("re");
    tree.removeKey(T(seq[y[i].idx].r, y[i].idx));
  }
  writeln(-1, " ", -1);

  heaven:;
  // writeln(tree);
}

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

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](0);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq~=P(a, b);
  }

  struct LRi{int l, r, id;}
  auto lri=new LRi[](0);
  foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);
  sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l<b.l))(lri);
  struct Ri{int r, id;}
  import std.container;
  auto tree=new RedBlackTree!(Ri, "a.r<b.r");
  foreach(i; 0..n) tree.insert(Ri(seq[i].r, i));
  foreach(elem; lri){
    int id=elem.id, r=seq[id].r;
    tree.removeKey(Ri(r, id));
    auto eq=tree.equalRange(Ri(r, -1));
    if((eq.empty)==false){
      writeln(eq.front.id+1, " ", id+1);
      goto heaven;
    }else{
      auto lb=tree.lowerBound(Ri(r, -1));
      if(lb.empty) continue;
      writeln(lb.back.id+1, " ", id+1);
      goto heaven;
    }
  }

  writeln(-1, " ", -1);
  heaven:;
}

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

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](0);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq~=P(a, b);
  }

  struct LRi{int l, r, id;}
  auto lri=new LRi[](0);
  foreach(i; 0..n) lri~=LRi(seq[i].l, seq[i].r, i);
  sort!((a, b)=>(a.l==b.l ? a.r>b.r : a.l<b.l))(lri);
  struct Ri{int r, id;}
  import std.container;
  auto tree=new RedBlackTree!(Ri, "a.r<b.r", true);
  foreach(i; 0..n) tree.insert(Ri(seq[i].r, i));
  foreach(elem; lri){
    int id=elem.id, r=seq[id].r;
    tree.removeKey(Ri(r, id));
    auto eq=tree.equalRange(Ri(r, -1));
    if((eq.empty)==false){
      writeln(eq.front.id+1, " ", id+1);
      goto heaven;
    }else{
      auto lb=tree.lowerBound(Ri(r, -1));
      if(lb.empty) continue;
      writeln(lb.back.id+1, " ", id+1);
      goto heaven;
    }
  }

  writeln(-1, " ", -1);
  heaven:;
}

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

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

  int n; scanf("%d", &n);
  struct P{int l, r;}
  auto seq=new P[](n);
  foreach(i; 0..n){
    int a, b; scanf("%d %d", &a, &b);
    seq[i]=P(a, b);
  }

  struct T{int x, idx;}
  import std.container;
  auto tree=new RedBlackTree!(T, "a.x<b.x", true);
  foreach(i; 0..n) tree.insert(T(seq[i].r, i));
  auto y=new T[](n);
  foreach(i; 0..n) y[i]=T(seq[i].l, i);
  sort!"a.x<b.x"(y);
  foreach(i; 0..n){
    // writeln(i);
    int id=y[i].idx;
    auto lb=tree.lowerBound(T(seq[id].r, id));
    if(lb.empty==false){
      writeln(id+1, " ", lb.front.idx+1);
      goto heaven;
    }else{
      auto eq=tree.equalRange(T(seq[id].r, id));
      if((eq.front==eq.back)==false){
        int j1=eq.front.idx.to!int, j2=eq.back.idx.to!(int);
        if(i!=j1){
          writeln(id+1, " ", j1+1);
        }else if(i!=j2){
          writeln(id+1, " ", j2+1);
        }
        goto heaven;
      }
    }
    // writeln("re");
    tree.removeKey(T(seq[y[i].idx].r, y[i].idx));
  }
  writeln(-1, " ", -1);

  heaven:;
  // writeln(tree);
}

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


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

int solve (int n)
{
	if (n == 1)
	{
		return 0;
	}
	else if (n % 3 != 0)
	{
		return -1;
	}
	else if (n % 2 != 0)
	{
		int res = solve (n * 2);
		return (res == -1) ? -1 : res + 1;
	}
	else
	{
		int res = solve (n / 6);
		return (res == -1) ? -1 : res + 1;
	}
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		readln.strip.to !(int).solve.writeln;
	}
}

import std.stdio;

void main() {
    uint t, n, twos, threes;

    scanf("%d", &t);
    while(t--) {
        int ans;
        scanf("%d", &n);

        twos = 0;
        while(n % 2 ==0) {
            twos++;
            n /= 2;
        }

        threes = 0;
        while(n % 3 == 0) {
            threes++;
            n /= 3;
        }

        if(n != 1 || twos > threes) ans = -1;
        else if (twos == threes) ans = twos;
        else ans = twos + (threes - twos) * 2;

        printf("%d\n", ans);
    }

}

import std.stdio;

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

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

    auto s = 0;
    // remove as many factors of 6 as possible
    while (n % 6 == 0) {
      n /= 6;
      s += 1;
    }

    while (n % 3 == 0) {
      n /= 3;
      s += 2; // multiply, then divide
    }

    if (n == 1) {
      writeln(s);
    } else {
      writeln(-1);
    }
  }
}


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 S = readln.chomp;
        int max_l, l;
        foreach (c; S) {
            if (c == 'L') {
                l += 1;
                max_l = max(l, max_l);
            } else {
                l = 0;
            }
        }
        writeln(max_l + 1);
    }
}

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

T binarySearch(alias pred, T)(T ok, T ng)
{
	while (abs(ok-ng) > 1)
	{
		auto mid = (ok+ng)/2;
		if (unaryFun!pred(mid)) ok = mid;
		else ng = mid;
	}
	return ok;
}

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto s = RD!string;
		auto n = cast(int)s.length;
		bool f(int d)
		{
			int[] open = [0, d-1];
			auto used = new bool[](n+1);
			while (!open.empty)
			{
				auto pos = open.front; open.popFront;
				if (pos == n) return true;
				int nPos1, nPos2;
				if (s[pos] == 'L')
				{
					nPos1 = max(0, pos-1);
					nPos2 = max(0, pos-d);
				}
				else
				{
					nPos1 = min(n, pos+1);
					nPos2 = min(n, pos+d);
				}
				if (used[nPos1] == false)
				{
					open ~= nPos1;
					used[nPos1] = true;
				}
				if (used[nPos2] == false)
				{
					open ~= nPos2;
					used[nPos2] = true;
				}
			}
			return false;
		}
		ans[ti] = binarySearch!(f)(n+1, 0);
	}

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


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

immutable int mod = 10^^9 + 7;

void main(){
    int n, m;
    readVars(n, m);

    int z;
    auto uf = new UnionFind(m + 1);
    auto exist = new bool[](m + 1);

    foreach(i ; 0 .. n){
        auto line = readln.split.to!(int[]);
        if(line[0] == 0){
            z++;
        }
        else{
            foreach(j ; 1 .. line.length){
                exist[line[j]] = true;

                if(j > 1){
                    uf.unite(line[j], line[j - 1]);
                }
            }
        }
    }

    if(z == n){
        writeln(n);
        return;
    }

    auto rbt = redBlackTree!int;

    foreach(i ; 1 .. m + 1){
        if (exist[i]) {
            rbt.insert(uf.find_root(i));
        }
    }

    auto ans = rbt.length.to!int + z - 1;

    writeln(ans);
}

class UnionFind {
private:
    int[] p;
    int[] rank;

public:
    this(int N){
        p = iota(N).array;
        rank = new int[](N);
    }

    int find_root(int x){
        if(x != p[x]){
            p[x] = find_root(p[x]);
        }

        return p[x];
    }

    bool same(int x, int y){
        return find_root(x) == find_root(y);
    }

    void unite(int x, int y){
        x = find_root(x);
        y = find_root(y);

        if(x == y) return;

        if(rank[x] < rank[y]){
            p[x] = y;
        }
        else {
            p[y] = x;

            if(rank[x] == rank[y]) rank[x]++;
        }
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}

import std.algorithm;
import std.container;
import std.exception;
import std.math;
import std.stdio;
import std.string;
import std.typecons;

immutable int MAX_N = 205;

bool [MAX_N] [MAX_N] a;
bool [MAX_N] b;
int n, m;

void recur (int v)
{
	if (b[v])
	{
		return;
	}
	b[v] = true;
	foreach (i; 0..n + m)
	{
		if (a[v][i])
		{
			recur (i);
		}
	}
}

void main ()
{
	while (readf (" %s %s", &n, &m) > 0)
	{
		foreach (i; 0..n + m)
		{
			foreach (j; 0..n + m)
			{
				a[i][j] = false;
			}
		}
		bool ok = false;
		foreach (i; 0..n)
		{
			int k;
			readf (" %s", &k);
			foreach (j; 0..k)
			{
				int p;
				readf (" %s", &p);
				p--;
				a[i][n + p] = true;
				a[n + p][i] = true;
				ok = true;
			}
		}
		foreach (i; 0..n + m)
		{
			b[i] = false;
		}
		int res = !ok;
		foreach (i; 0..n)
		{
			if (!b[i])
			{
				recur (i);
				res++;
			}
		}
		res--;
		writefln ("%s", res);
	}
}

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

void main() {
    int n, m; readf("%d %d\n", &n, &m);
    bool[][] F = new bool[][n];
    foreach (ref L; F) L = new bool[m];
    for (int i = 0; i < n; i++) {
        int c; scanf("%d ", &c);
        for (int j = 0; j < c; j++) {
            int l; scanf("%d", &l);
            l--;
            F[i][l] = true;
        }
    }
    bool[] U = new bool[n];
    int c = 0;
    void dfs(int v) {
        if (U[v]) return;
        U[v] = true;
        for (int i = 0; i < m; i++) {
            if (F[v][i]) {
                for (int j = 0; j < n; j++) {
                    if (F[j][i]) {
                        dfs(j);
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        if (U[i]) continue;
        c++;
        dfs(i);
    }
    int f = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (F[i][j])
               f = 1;
    writeln(c-f);
}

import std.algorithm;
import std.container;
import std.exception;
import std.math;
import std.stdio;
import std.string;
import std.typecons;

immutable int MAX_N = 205;

bool [MAX_N] [MAX_N] a;
bool [MAX_N] b;
int n, m;

void recur (int v)
{
	if (b[v])
	{
		return;
	}
	b[v] = true;
	foreach (i; 0..n + m)
	{
		if (a[v][i])
		{
			recur (i);
		}
	}
}

void main ()
{
	while (readf (" %s %s", &n, &m) > 0)
	{
	        foreach (i; 0..n + m)
	        {
	        	foreach (j; 0..n + m)
	        	{
	        		a[i][j] = false;
	        	}
	        }
		foreach (i; 0..n)
		{
			int k;
			readf (" %s", &k);
			foreach (j; 0..k)
			{
				int p;
				readf (" %s", &p);
				p--;
				a[i][n + p] = true;
				a[n + p][i] = true;
			}
		}
		foreach (i; 0..n + m)
		{
			b[i] = false;
		}
		int res;
		foreach (i; 0..n)
		{
			if (!b[i])
			{
				recur (i);
				res++;
			}
		}
		res--;
		writefln ("%s", res);
	}
}

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

immutable int mod = 10^^9 + 7;

void main(){
    int n, m;
    readVars(n, m);

    int z;
    auto uf = new UnionFind(m + 1);
    auto exist = new bool[](m + 1);

    foreach(i ; 0 .. n){
        auto line = readln.split.to!(int[]);
        if(line[0] == 0){
            z++;
        }
        else{
            foreach(j ; 1 .. line.length){
                exist[line[j]] = true;

                if(j > 1){
                    uf.unite(line[j], line[j - 1]);
                }
            }
        }
    }

    auto rbt = redBlackTree!int;

    foreach(i ; 1 .. m + 1){
        if (exist[i]) {
            rbt.insert(uf.find_root(i));
        }
    }

    auto ans = rbt.length.to!int + z - 1;

    writeln(ans);
}

class UnionFind {
private:
    int[] p;
    int[] rank;

public:
    this(int N){
        p = iota(N).array;
        rank = new int[](N);
    }

    int find_root(int x){
        if(x != p[x]){
            p[x] = find_root(p[x]);
        }

        return p[x];
    }

    bool same(int x, int y){
        return find_root(x) == find_root(y);
    }

    void unite(int x, int y){
        x = find_root(x);
        y = find_root(y);

        if(x == y) return;

        if(rank[x] < rank[y]){
            p[x] = y;
        }
        else {
            p[y] = x;

            if(rank[x] == rank[y]) rank[x]++;
        }
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}

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

void main() {
    int n, m; readf("%d %d\n", &n, &m);
    bool[][] F = new bool[][n];
    foreach (ref L; F) L = new bool[m];
    for (int i = 0; i < n; i++) {
        int c; readf("%d ", &c);
        int[] ls = stdin.readln.split.map!(function int(x){return x.to!int - 1;}).array;
        foreach (l; ls) {
            F[i][l] = true;
        }
    }
    bool[] U = new bool[n];
    int c = 0;
    void dfs(int v) {
        if (U[v]) return;
        U[v] = true;
        for (int i = 0; i < m; i++) {
            if (F[v][i]) {
                for (int j = 0; j < n; j++) {
                    if (F[j][i]) {
                        dfs(j);
                    }
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        if (U[i]) continue;
        c++;
        dfs(i);
    }
    writeln(c-1);
}


import core.bitop;
import std.algorithm;
import std.conv;
import std.numeric;
import std.range;
import std.stdio;
import std.string;

immutable int mod = 998_244_353;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		int res = 1;
		int cur = 0;
		foreach (ref next; a)
		{
			if (cur % next != 0)
			{
				res = 0;
				break;
			}

			int num;
			if (cur == 0)
			{
				num = 1;
			}
			else
			if (cur == next || cur == 0)
			{
				num = m / next;
			}
			else
			{
				int s = cur / next;
				int [] p;
				for (int d = 2; d * d <= s; d++)
				{
					if (s % d == 0)
					{
						p ~= d;
						do
						{
							s /= d;
						}
						while (s % d == 0);
					}
				}
				if (s > 1)
				{
					p ~= s;
				}
				debug {writeln ("p = ", p);}

				num = 0;
				auto len = p.length.to !(int);
				foreach (u; 0..(1 << len))
				{
					int e = 1;
					foreach (i; 0..len)
					{
						if (u & (1 << i))
						{
							e *= -p[i];
						}
					}
					num += m / (next * e);
				}
			}

			debug {writeln ("num = ", num);}
			res = (res * 1L * num) % mod;
			cur = next;
		}
		writeln (res);
	}
}


// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

int n;

void main() {
    scan(n);

    auto bh = new BinaryHeap!(Array!int, "a > b")();

    int ans;
    int nn = 1;
    int b = 0;

    foreach (i ; 0 .. 2*n) {
        auto line = readln.split;

        if (line[0] == "add") {
            int x = line[1].to!int;

            if (!bh.empty && x > bh.front) {
                b = bh.front;
            }

            bh.insert(x);
        }
        else {
            debug {
                writeln("next:", nn);
                writeln("b:", b);
                writeln("top:", bh.front);
                writeln;
            }
            if (nn == b) {
                ans++;
                b = 0;
            }

            bh.popFront();
            nn++;
        }
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    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);
        }
    }
}

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable inf = 10^^7;
int n;

void main() {
    scan(n);

    auto st = Stack!int(n);
    int ans, nn = 1;

    foreach (i ; 0 .. 2*n) {
        auto line = readln.split;

        if (line[0] == "add") {
            int x = line[1].to!int;
            st.push(x);
        }
        else {
            if (!st.empty && st.top != nn) {
                debug {
                    writeln("nn:", nn);
                }
                ans++;
                st.clear();
            }
            else if (!st.empty) {
                st.pop();
            }

            nn++;
        }
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    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);
        }
    }
}

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

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

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

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

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

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

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

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

    int length() @property
    {
        return peek;
    }

}


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

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

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

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

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

      long val = H.sum;
      foreach (i; 0 .. N) {
        val -= i;
      }
      auto ans = new long[N];
      ans[] = val / N;
      ans[0 .. cast(int)(val % N)] += 1;
      foreach (i; 0 .. N) {
        ans[i] += i;
      }

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

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto h = readln.splitter.map !(to !(long)).array;
		foreach (i; 0..n)
		{
			h[i] -= i;
		}

		long total = h.sum;
		foreach (i; 0..n)
		{
			int num = n - i;
			long cur = (total + num - 1) / num;
			if (h[i] < cur)
			{
				long delta = cur - h[i];
				h[i] += delta;
				h[i + 1] -= delta;
			}
			total -= h[i];
		}

		foreach (i; 0..n)
		{
			h[i] += i;
		}
		writefln !("%(%s %)") (h);
	}
}