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;
    readf("%s", &n);
    readln;

    Tuple!(int, int)[] arr;
    foreach (_; 0 .. n-1) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        arr ~= tuple(a, b);
    }

    if (arr.map!(t => t[1]).any!(x => x != n)) {
        writeln("NO");
        return;
    }

    auto mn = arr.map!(t => t[0]).array;

    mn.sort();

    debug { mn.writeln; }

    if (mn.enumerate(1).any!(t => t[0] > t[1])) {
        writeln("NO");
        return;
    }

    auto isFree = new bool[] (n+1);
    isFree[] = true;
    int nxt = 1;

    auto grouped = mn.group.array;

    Tuple!(int, int)[] ans;
    foreach (t; grouped) {
        int e = t[0], cnt = t[1];

        int[] cur;
        foreach (_; 1 .. cnt) {
            while (!isFree[nxt]) { ++nxt; }
            cur ~= nxt;
            isFree[nxt] = false;
        }

        cur ~= e;
        isFree[e] = false;

        foreach (a, b; lockstep(cur, cur.dropOne)) { ans ~= tuple(a, b); }

        ans ~= tuple(n, cur.front);
    }

    writeln("YES");
    ans.each!(t => writeln(t[0], ' ', t[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;

void main() {
    auto N = readln.chomp.to!int;
    auto E = (N-1).iota.map!(_ => readln.split.map!(to!int).array).array;

    if (E.map!(e => e[1] != N).any) {
        writeln("NO");
        return;
    }

    auto A = E.map!(e => e[0]).array;
    A.sort();

    foreach (i; 0..N-1) {
        if (A[i] < i + 1) {
            writeln("NO");
            return;
        }
    }

    auto used = new int[](N+1);
    foreach (a; A) used[a] = true;
    auto ans = new int[](N-1);
    int start = 0;
    int x = 1;

    foreach (i; 0..N-1) {
        if (i == N - 2 || A[i] != A[i+1]) {
            ans[start] = A[i];
            foreach (j; start+1..i+1) {
                while (used[x]) ++x;
                used[x] = true;
                ans[j] = x;
            }
            start = i + 1;
        }
    }

    writeln("YES");
    foreach (i; 0..ans.length.to!int - 1) {
        writeln(ans[i], " ", ans[i+1]);
    }
    writeln(ans.back, " ", N);
}

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;

void main() {
    auto N = readln.chomp.to!int;
    auto E = (N-1).iota.map!(_ => readln.split.map!(to!int).array).array;

    if (E.map!(e => e[1] != N).any) {
        writeln("NO");
        return;
    }

    auto A = E.map!(e => e[0]).array;
    A.sort();

    foreach (i; 0..N-1) {
        if (A[i] < i + 1) {
            writeln("No");
            return;
        }
    }

    auto used = new int[](N+1);
    foreach (a; A) used[a] = true;
    auto ans = new int[](N-1);
    int start = 0;
    int x = 1;

    foreach (i; 0..N-1) {
        if (i == N - 2 || A[i] != A[i+1]) {
            ans[start] = A[i];
            foreach (j; start+1..i+1) {
                while (used[x]) ++x;
                used[x] = true;
                ans[j] = x;
            }
            start = i + 1;
        }
    }

    writeln("YES");
    foreach (i; 0..ans.length.to!int - 1) {
        writeln(ans[i], " ", ans[i+1]);
    }
    writeln(ans.back, " ", N);
}

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;
    readf("%s", &n);
    readln;

    Tuple!(int, int)[] arr;
    foreach (_; 0 .. n-1) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        arr ~= tuple(a, b);
    }

    if (arr.map!(t => t[1]).any!(x => x != n)) {
        writeln("NO");
        return;
    }

    auto mn = arr.map!(t => t[0]).array;

    mn.sort();

    debug { mn.writeln; }

    if (mn.enumerate(1).any!(t => t[0] > t[1])) {
        writeln("NO");
        return;
    }

    auto isFree = new bool[] (n+1);
    isFree[] = true;
    int nxt = 1;

    auto grouped = mn.group.array;

    Tuple!(int, int)[] ans;
    foreach (t; grouped) {
        int e = t[0], cnt = t[1];

        int[] cur;
        foreach (_; 1 .. cnt) {
            while (!isFree[nxt]) { ++nxt; }
            cur ~= nxt;
        }

        isFree[e] = false;
        cur ~= e;

        foreach (a, b; lockstep(cur, cur.dropOne)) { ans ~= tuple(a, b); }

        ans ~= tuple(n, cur.front);
    }

    writeln("YES");
    ans.each!(t => writeln(t[0], ' ', t[1]));
}

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;
    readf("%s", &n);
    readln;

    int[] arr;
    foreach (_; 0 .. n-1) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        arr ~= a;
    }

    debug { arr.writeln; }

    arr.sort();

    Tuple!(int, int)[] ans;
    auto lst = 1;
    foreach (i, e; arr) {
        if (e < i+1) {
            writeln("NO");
            return;
        }

        if (i+1 < e) { continue; }

        foreach (v; lst .. e) {
            ans ~= tuple(v, v+1);
        }

        ans ~= tuple(lst, n);

        lst = e+1;
    }

    writeln("YES");
    ans.each!(t => writeln(t[0], ' ', t[1]));
}

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;
    readf("%s", &n);
    readln;

    Tuple!(int, int)[] arr;
    foreach (_; 0 .. n-1) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        arr ~= tuple(a, b);
    }

    if (arr.map!(t => t[1]).any!(x => x != n)) {
        writeln("NO");
        return;
    }

    auto mn = arr.map!(t => t[0]).array;

    mn.sort();

    debug { mn.writeln; }

    arr.sort();

    Tuple!(int, int)[] ans;
    auto lst = 1;
    foreach (i, e; mn) {
        if (e < i+1) {
            writeln("NO");
            return;
        }

        if (i+1 < e) { continue; }

        foreach (v; lst .. e) {
            ans ~= tuple(v, v+1);
        }

        ans ~= tuple(lst, n);

        lst = e+1;
    }

    writeln("YES");
    ans.each!(t => writeln(t[0], ' ', t[1]));
}

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;
    readf("%s", &n);
    readln;

    Tuple!(int, int)[] arr;
    foreach (_; 0 .. n-1) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        arr ~= tuple(a, b);
    }

    if (arr.map!(t => t[1]).any!(x => x != n)) {
        writeln("NO");
        return;
    }

    auto mn = arr.map!(t => t[1]).array;

    mn.sort();

    debug { mn.writeln; }

    arr.sort();

    Tuple!(int, int)[] ans;
    auto lst = 1;
    foreach (i, e; mn) {
        if (e < i+1) {
            writeln("NO");
            return;
        }

        if (i+1 < e) { continue; }

        foreach (v; lst .. e) {
            ans ~= tuple(v, v+1);
        }

        ans ~= tuple(lst, n);

        lst = e+1;
    }

    writeln("YES");
    ans.each!(t => writeln(t[0], ' ', t[1]));
}


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

string FMT_F = "%.10f";

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; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

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

void main()
{
	auto T = RD!int;
	auto ans = new long[](T);
	foreach (i; 0..T)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto a = RDR.ARR;
		auto diff = new long[](n-k);
		foreach (j; k..n)
		{
			diff[j-k] = a[j] - a[j-k];
		}
		auto pos = MIN_POS(diff);
		ans[i]   = (a[pos+k] + a[pos]) / 2;
	}

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

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int d = int.max;
		int x;
		foreach (i; 0..n - k)
		{
			int cur = a[i + k] - a[i];
			if (d > cur)
			{
				d = cur;
				x = (a[i + k] + a[i]) / 2;
			}
		}
		writeln (x);
	}
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

class InputReader {
  private:
  ubyte[] p;
  ubyte[] buffer;
  size_t cur;
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
    p = stdin.rawRead (buffer);
  }
  final ubyte skipByte (ubyte lo) {
    while (true) {
      auto a = p[cur .. $];
      auto r = a.find! (c => c >= lo);
      if (!r.empty) {
        cur += a.length - r.length;
        return p[cur++];
      }
      p = stdin.rawRead (buffer);
      cur = 0;
      if (p.empty) return 0;
    }
  }
  final ubyte nextByte () {
    if (cur < p.length) {
       return p[cur++];
    }
    p = stdin.rawRead (buffer);
    if (p.empty) return 0;
    cur = 1;
    return p[0];
  }

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  final T[] nextA(T) (int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader;
  immutable nt = r.next!uint;
  foreach (tid; 0 .. nt) {
    immutable n = r.next!uint;
    immutable k = r.next!uint;
    auto a = r.nextA!int (n);
    long res = int.max;
    int x = int.max;
    foreach (i; k .. n) {
      int d = a[i] - a[i - k];
      if (res > d) {
        res = d;
        x = (a[i] + a[i - k]) / 2;
      }
    }
    writeln (x);
  }
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){

	int t = read.to!int;
	foreach(_; 0 .. t){
		int n = read.to!int;
		int k = read.to!int;

		long[] as;
		foreach(i; 0 .. n) as ~= read.to!long;

		long best = as[k] - as[0];
		int bestleft = 0;
		foreach(i; 0 .. n - k){
			long tmp = as[i + k] - as[i];
			if(tmp < best) best = tmp, bestleft = i;
		}

		long ans = as[bestleft] + best / 2;
		ans.writeln;

	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){

	int t = read.to!int;
	foreach(_; 0 .. t){
		int n = read.to!int;
		int k = read.to!int;

		long[] as;
		foreach(i; 0 .. n) as ~= read.to!long;

		long best = as[k] - as[0];
		int bestleft = 0;
		{
			long tmp = as[n - 1] - as[n - 1 - k];
			if(tmp < best) best = tmp, bestleft = n - 1 - k;
		}
		foreach(i; 0 .. n - k - 1){
			long tmp = as[i + (k + 1)] - as[i];
			if(tmp < best) best = tmp, bestleft = i;
		}

		long ans = as[bestleft] + best / 2;
		ans.writeln;

	}
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int a, b;
		readf !(" %s %s") (a, b);
		writeln ((a * b + 1) / 2);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		if (n % 2 == 0)
			ans[ti] = (n/2) * m;
		else if (m % 2 == 0)
			ans[ti] = (m/2) * n;
		else
		{
			ans[ti] = (n/2) * m;
			ans[ti] += (m+1)/2;
		}
	}

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


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

int [] [] a;
double res;

void recur (int v, int p, int d)
{
	res += 1.0 / d;
	foreach (u; a[v])
	{
		if (u != p)
		{
			recur (u, v, d + 1);
		}
	}
}

void main ()
{
	int n;
	while (scanf (" %d", &n) > 0)
	{
		a = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			scanf (" %d %d", &u, &v);
			u--;
			v--;
			a[u] ~= v;
			a[v] ~= u;
		}

		res = 0;
		recur (0, -1, 1);
		writefln ("%.10f", res);
	}
}

import std.stdio;

int [] [] a;
double res;
int n;

void recur (int v, int p, int d)
{
	res += 1.0 / d;
	foreach (u; a[v])
		if (u != p)
			recur (u, v, d + 1);
}

void main ()
{
	while (readf (" %s", &n))
	{
		a = new int [] [n + 1];
		foreach (i; 1..n)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			a[u] ~= v;
			a[v] ~= u;
		}

		res = 0;
		recur (1, 0, 1);
		writefln ("%.7f", res);
	}
}


immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto s = [0] ~ (cast(char[])readString).map!(c => c == 'a'? 1 : -1).array;
	auto ss = s.cumulativeFold!((a, b) => a + b).array;
	foreach(i; 1 .. n+1)
		foreach(j; i+1 .. n+1)
		{
			if (ss[i-1] == ss[j])
			{
				return writeln(i, " ", j);
			}
		}
	return writeln("-1 -1");
}

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

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

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

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

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

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

void main()
{
    int t;
    scanf("%d", &t);
    foreach(_; 0..t)
    {
        int str_len;
        scanf("%d", &str_len);
        getchar();
        auto str = readln.strip();
        // writeln(str);
        int[] check = new int[str_len];
        bool flag = true;
        for(int i = 0; i < str_len; i++)
        {
            if (flag == false)
                break;
            if (str[i] == 'a')
            {
                for (int j = i; j >= 0; j--)
                {
                    check[j] += 1;
                    if (check[j] == 0)
                    {
                        writeln(j + 1,' ', i + 1);
                        flag = false;
                    }
                }
            }
            else
            {
                for (int j = i; j >= 0; j--)
                {
                    check[j] -= 1;
                    if (check[j] == 0)
                    {
                        writeln(j + 1,' ', i + 1);
                        flag = false;
                    }
                }
            }
        }
        if (flag)
            writeln("-1 -1");
    }
}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto s = [0] ~ (cast(char[])readString).map!(c => c == 'a'? 1 : -1).array;
	auto ss = s.cumulativeFold!((a, b) => a + b).array;
	foreach(i; 1 .. n)
		foreach(j; i+1 .. n)
		{
			if (ss[i-1] == ss[j])
			{
				return writeln(i, " ", j);
			}
		}
	return writeln("-1 -1");
}

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

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

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

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

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

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto s = [0] ~ (cast(char[])readString).map!(c => c == 'a'? 1 : -1).array;
	auto ss = s.cumulativeFold!((a, b) => a + b).array;
	foreach(i; 1 .. n)
		foreach(j; 1 .. n)
		{
			if (ss[i] == ss[j-1])
			{
				return writeln(i, " ", j);
			}
		}
	return writeln("-1 -1");
}

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

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

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

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

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


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

string FMT_F = "%.10f";

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; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
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; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

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

void main()
{
	auto q = RD!int;
	auto ans = new long[][](q);
	foreach (i; 0..q)
	{
		auto n = RD!int;
		auto p = RDA!int(-1);
		auto root = new int[](n);
		root[] = -1;
		ans[i].length = n;
		foreach (j; 0..n)
		{
			if (root[j] != -1)
			{
				ans[i][j] = ans[i][root[j]];
				continue;
			}
			int pos = p[j];
			root[j] = j;
			int cnt = 1;
			while (pos != j)
			{
				++cnt;
				root[pos] = j;
				pos = p[pos];
			}
			ans[i][j] = cnt;
		}
	}

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

	stdout.flush();
	debug readln();
}


// 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 lim = 10^^6 + 1;

int n, a;
int[] c;

void main() {
    scan(n, a);
    c = readln.split.to!(int[]);

    auto cnt = new int[](lim);
    auto ok = new bool[](lim);
    ok[] = true;

    foreach (ci ; c){
        cnt[ci]++;

        if (ci != a && cnt[ci] <= cnt[a]) {
            ok[ci] = false;
        }
    }

    foreach (i ; 1 .. lim) {
        if (i == a) continue;
        if (cnt[i] >= cnt[a] && ok[i]) {
            writeln(i);
            return;
        }
    }

    writeln(-1);

}

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

class SegTree(T){
private:
    T[] data;
    int N;

public:
    int depth;

    this(int size){
        N = size;

        while(2^^depth < N){
            depth++;
        }

        data = new int[](2^^(depth + 1) - 1);
    }

    void update(int i, T x){
        i += 2^^depth - 1;
        data[i] = x;

        while(i > 0){
            i = (i - 1) / 2;
            data[i] = max(data[2*i + 1], data[2*i + 2]);
        }
    }

    T find(int s, int t, int k, int l, int r){
        if (r <= s || t <= l) {
            return 0;
        }

        if (s <= l && r <= t) {
            return data[k];
        }

        auto vl = find(s, t, 2*k + 1, l, (l + r) / 2);
        auto vr = find(s, t, 2*k + 2, (l + r) / 2, r);

        return max(vl, vr);
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto A = s[1];
    auto C = readln.split.map!(to!int).array;

    immutable int MAX = 10^^6 + 1;
    auto cnt = new bool[int][](MAX);
    foreach (i; 1..MAX) if (i != A) cnt[0][i] = true;
    auto cnt2 = new int[](MAX);

    bool[int] ans;
    foreach (i; 1..MAX) if (i != A) ans[i] = true;

    foreach (c; C) {
        if (c == A) {
            cnt2[c] += 1;
            int old = cnt2[c] - 1;
            foreach (i; cnt[old].keys) {
                if (i in ans) ans.remove(i);
                cnt2[i] = -1;
            }
            cnt[old].clear;
        } else if (cnt2[c] != -1) {
            cnt[cnt2[c]].remove(c);
            cnt2[c] += 1;
            cnt[cnt2[c]][c] = true;
        }
    }

    if (ans.keys.length == 0) writeln(-1);
    else writeln(ans.keys[0]);
}

// 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 lim = 10^^6 + 1;

int n, a;
int[] c;

void main() {
    scan(n, a);
    c = readln.split.to!(int[]);

    auto cnt = new int[](lim);
    auto sg = new SegTree!(int)(lim);

    foreach (ci ; c) {
        if (ci == a) {
            cnt[a]++;

            if (cnt[a] > sg.find(0, lim, 0, 0, 2^^sg.depth)) {
                writeln(-1);
                return;
            }
        }
        else {
            cnt[ci]++;
            sg.update(ci, cnt[ci]);
        }

        debug {
            //sg.print();
            writeln("cnt:", cnt[0 .. 20]);
        }
    }

    foreach (i ; 1 .. lim) {
        if (i == a) continue;

        if (cnt[i] >= cnt[a]) {
            writeln(i);
            return;
        }
    }
}

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

class SegTree(T){
private:
    T[] data;
    int N;

public:
    int depth;

    this(int size){
        N = size;

        while(2^^depth < N){
            depth++;
        }

        data = new int[](2^^(depth + 1) - 1);
    }

    void update(int i, T x){
        i += 2^^depth - 1;
        data[i] = x;

        while(i > 0){
            i = (i - 1) / 2;
            data[i] = max(data[2*i + 1], data[2*i + 2]);
        }
    }

    T find(int s, int t, int k, int l, int r){
        if (r <= s || t <= l) {
            return 0;
        }

        if (s <= l && r <= t) {
            return data[k];
        }

        auto vl = find(s, t, 2*k + 1, l, (l + r) / 2);
        auto vr = find(s, t, 2*k + 2, (l + r) / 2, r);

        return max(vl, vr);
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

// 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 lim = 10^^6 + 1;

int n, a;
int[] c;

void main() {
    scan(n, a);
    c = readln.split.to!(int[]);

    auto cnt = new int[](lim);
    auto sg = new SegTree!(int)(lim);

    foreach (ci ; c) {
        if (ci == a) {
            cnt[a]++;

            if (cnt[a] > sg.find(0, lim, 0, 0, 2^^sg.depth)) {
                writeln(-1);
                return;
            }
        }
        else {
            cnt[ci]++;
            sg.update(ci, cnt[ci]);
        }

        debug {
            //sg.print();
            writeln("cnt:", cnt[0 .. 20]);
        }
    }

    foreach (i ; 1 .. lim) {
        if (i == a) continue;

        if (cnt[i] > 0 && cnt[i] >= cnt[a]) {
            writeln(i);
            return;
        }
    }
}

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

class SegTree(T){
private:
    T[] data;
    int N;

public:
    int depth;

    this(int size){
        N = size;

        while(2^^depth < N){
            depth++;
        }

        data = new int[](2^^(depth + 1) - 1);
    }

    void update(int i, T x){
        i += 2^^depth - 1;
        data[i] = x;

        while(i > 0){
            i = (i - 1) / 2;
            data[i] = max(data[2*i + 1], data[2*i + 2]);
        }
    }

    T find(int s, int t, int k, int l, int r){
        if (r <= s || t <= l) {
            return 0;
        }

        if (s <= l && r <= t) {
            return data[k];
        }

        auto vl = find(s, t, 2*k + 1, l, (l + r) / 2);
        auto vr = find(s, t, 2*k + 2, (l + r) / 2, r);

        return max(vl, vr);
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}


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

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n, m;
    readf!" %d %d "(n, m);
    foreach (i ; 0 .. m) {
      long x, y;
      readf!" %d %d "(x, y);
    }
    writeln(m < n ? "YES" : "NO");
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		foreach (i; 0..k)
		{
			readln;
		}
		writeln (k < n ? "YES" : "NO");
	}
}

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

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

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

void solve() {
    int N, M; readf("%d %d\n", &N, &M);
    auto usedY = new bool[N];
    auto usedX = new bool[N];
    foreach (i; 0 .. M) {
        int Y, X; readf("%d %d\n", &Y, &X);
        Y--; X--;
        usedY[Y] = true;
        usedX[X] = true;
    }
    bool f() {
        foreach (i; 0 .. N) {
            if (!usedY[i] || !usedX[i]) return true;
        }
        return false;
    }
    writeln(f() ? "YES" : "NO");
}


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

int main()
{
    int t = readInt!int;
    while (t--)
    {
        long x = readInt!long;
        long m111 = 0;
        bool can = false;
        int maxrep = 100;
        int i = 0;
        while (m111 <= x && i < maxrep)
        {
            if ((x - m111) % 11 == 0)
            {
                can = true;
                break;
            }
            m111 += 111;
            i++;
        }
        if (can) writeln("YES"); else writeln("NO");
    }
    return 0;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
    currChar = getchar();
}
char topChar()
{
    return cast(char) currChar;
}
void popChar()
{
    currChar = getchar();
}
auto readInt(T)()
{
    T num = 0;
    int sgn = 0;
    while (topChar.isWhite) popChar;
    while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
    while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
    if (sgn) return -num; return num;
}
string readString()
{
    string res = "";
    while (topChar.isWhite) popChar;
    while (!topChar.isWhite) { res ~= topChar; popChar; }
    return res;
}

// Vicfred
// https://codeforces.com/contest/1526/problem/B
// math
import std.stdio;

void main() {
    int t;
    readf("%s\n", &t);
while(t--) {
    long x;
    readf("%s\n", &x);
    if(x > 2_000) {
        "YES".writeln;
        continue;
    }
    bool found = false;
    for(long a = 0; a < 200; ++a) {
        for(long b = 0; b < 200; ++b) {
            if(x == a*11L + b*111L) {
                found = true;
                break;
            }
        }
        if(found) break;
    }
    if(found)
        "YES".writeln;
    else
        "NO".writeln;
}
}

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

int main()
{
    int t = readInt!int;
    while (t--)
    {
        long x = readInt!long;
        long m111 = 0;
        bool can = false;
        int maxrep = 20;
        int i = 0;
        while (m111 <= x && i < maxrep)
        {
            if ((x - m111) % 11 == 0)
            {
                can = true;
                break;
            }
            m111 += 111;
            i++;
        }
        if (can) writeln("YES"); else writeln("NO");
    }
    return 0;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
    currChar = getchar();
}
char topChar()
{
    return cast(char) currChar;
}
void popChar()
{
    currChar = getchar();
}
auto readInt(T)()
{
    T num = 0;
    int sgn = 0;
    while (topChar.isWhite) popChar;
    while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
    while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
    if (sgn) return -num; return num;
}
string readString()
{
    string res = "";
    while (topChar.isWhite) popChar;
    while (!topChar.isWhite) { res ~= topChar; popChar; }
    return res;
}

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

bool check(long n)
{
  for (long i = 0; i < 15; i++) {
    long n2 = n - i * 111L;
    if (n2 == 0)
      return true;
    if (n2 > 0 && n2 % 11L == 0)
      return true;
  }
  return false;
}

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

  foreach (casenum ; 0 .. t) {
    long n;
    readf!" %d "(n);
    if (check(n))
      writeln("YES");
    else
      writeln("NO");
  }
}

// Vicfred
// https://codeforces.com/contest/1526/problem/B
// math
import std.stdio;

void main() {
    int t;
    readf("%s\n", &t);
while(t--) {
    long x;
    readf("%s\n", &x);
    if(x > 2_000) {
        "YES".writeln;
        continue;
    }
    bool found = false;
    for(long a = 0; a < 200; ++a) {
        for(long b = 0; b < 20; ++b) {
            if(x == a*11L + b*111L) {
                found = true;
                break;
            }
        }
        if(found) break;
    }
    if(found)
        "YES".writeln;
    else
        "NO".writeln;
}
}

// Vicfred
// https://codeforces.com/contest/1526/problem/B
// math
import std.stdio;

void main() {
    int t;
    readf("%s\n", &t);
while(t--) {
    long x;
    readf("%s\n", &x);
    if(x > 2_000) {
        "YES".writeln;
        continue;
    }
    bool found = false;
    for(long a = 0; a < 100; ++a) {
        for(long b = 0; b < 1000; ++b) {
            if(x == a*11L + b*111L) {
                found = true;
                break;
            }
        }
        if(found) break;
    }
    if(found)
        "YES".writeln;
    else
        "NO".writeln;
}
}

// Vicfred
// https://codeforces.com/contest/1526/problem/B
// math
import std.stdio;

void main() {
    int t;
    readf("%s\n", &t);
while(t--) {
    long x;
    readf("%s\n", &x);
    if(x > 10_000) {
        "YES".writeln;
        continue;
    }
    bool found = false;
    for(long a = 0; a < 100; ++a) {
        for(long b = 0; b < 100; ++b) {
            if(x == a*11L + b*111L) {
                found = true;
                break;
            }
        }
        if(found) break;
    }
    if(found)
        "YES".writeln;
    else
        "NO".writeln;
}
}

// Vicfred
// https://codeforces.com/contest/1526/problem/B
// math
import std.stdio;

void main() {
    int t;
    readf("%s\n", &t);
while(t--) {
    long x;
    readf("%s\n", &x);
    bool found = false;
    for(long a = 0; a < 100; ++a) {
        for(long b = 0; b < 100; ++b) {
            if(x == a*11L + b*111L) {
                found = true;
                break;
            }
        }
        if(found) break;
    }
    if(found)
        "YES".writeln;
    else
        "NO".writeln;
}
}

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

bool check(long n)
{
  long i, j;
  for (j = 0; j <= 11; j++) {
    long n2 = n - 111L * j;
    if (n2 > 0 && n2 % 11L == 0)
      return true;
  }
  return false;
}

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

  foreach (casenum ; 0 .. t) {
    long n;
    readf!" %d "(n);
    while (n >= 1222222221L)
      n -= 1222222221L;
    while (n >= 12222221L)
      n -= 12222221L;
    while (n >= 122221L)
      n -= 122221L;
    while (n >= 1221L)
      n -= 1221L;

    if (check(n))
      writeln("YES");
    else
      writeln("NO");
  }
}

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

bool check(long n)
{
  auto a = [11L, 111L, 1111L, 11111L, 111111L, 1111111L, 11111111L, 111111111L, 1111111111L];
  foreach_reverse (v ; a) {
    n -= n / v * v;
  }
  return n == 0;
}

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

  foreach (casenum ; 0 .. t) {
    long n;
    readf!" %d "(n);
    if (check(n))
      writeln("YES");
    else
      writeln("NO");
  }
}


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

alias Tuple!(long, "x", long, "y", long, "r") Circle;

bool include(Circle c1, Circle c2) {
    auto d2 = (c1.x - c2.x)^^2 + (c1.y - c2.y)^^2;
    auto r2 = (c1.r - c2.r)^^2;
    return c1.r > c2.r && d2 <= r2;
}

real area(Circle c1) {
    return c1.r * c1.r * PI;
}

void main() {
    auto N = readln.chomp.to!int;
    auto C = new Circle[](N);
    foreach (i; 0..N) {
        auto s = readln.split.map!(to!long);
        C[i] = Circle(s[0], s[1], s[2]);
    }
    C.sort!"a.r > b.r"();

    auto edges = new int[][](N);
    int[] roots;

    foreach (i; 0..N) {
        bool included = false;
        for (int j = i - 1; j >= 0; j--) {
            if (include(C[j], C[i])) {
                edges[j] ~= i;
                included = true;
                break;
            }
        }
        if (!included) roots ~= i;
    }

    real ans = 0;

    void dfs(int n, int p, int d) {
        if (d < 2) ans += area(C[n]);
        else if (d % 2 == 0) ans -= area(C[n]);
        else ans += area(C[n]);
        foreach (m; edges[n]) if (m != p) dfs(m, n, d+1);
    }

    foreach (i; roots) dfs(i, -1, 0);

    writefln("%.9f", ans);
}

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

int[][1001] adj;
double[3][1001] dp;
bool[1001] vis;
tup[] circles;

ll sqdist(tup c1, tup c2){
    return (c1[1] - c2[1])^^2 + (c1[2] - c2[2])^^2;
}

double area(tup c){
    double ar =  PI * to!double(c[0]^^2);
    return ar;
}

void dfs(int cur){
    assert(!vis[cur]);
    vis[cur] = 1;
    double dp0 = 0, dp1 = 0, dp2 = 0;
    foreach(v; adj[cur]){
        dfs(v);
        dp0 += dp[v][0];
        dp1 += dp[v][1];
        dp2 += dp[v][2];
    }
    double curarea = area(circles[cur]);
    dp[cur][0] = (-curarea) + dp1;
    dp[cur][1] = max(curarea + dp0, (-curarea) + dp2);
    dp[cur][2] = curarea + dp1;
}

void play(){
    int n;
    n = rd!int;
    ll x, y, r;
    foreach(i; 0..n){
        x = rd, y = rd, r = rd;
        circles ~= tup(r, x, y);
    }
    sort(circles);

    // Make Trees
    foreach(i; 0..n){
        foreach(j; i+1..n){
            if(sqdist(circles[i], circles[j]) < circles[i][0]^^2 + circles[j][0]^^2){
                adj[j] ~= i;
                break;
            }
        }
    }

    // DFS and add DP value
    double res = 0;
    foreach_reverse(i; 0..n){
        if(!vis[i]){
            dfs(i);
            show(dp[i][1], dp[i][2]);
            res += max(dp[i][1], dp[i][2]);
        }
    }
    writefln("%.9f", res);
}

int main(){
    long t = 1;
    /* t = rd;        // Toggle! */
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
alias ll = long;
alias rbt = RedBlackTree;
alias sr = SortedRange;
alias tup = Tuple!(long, 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 show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }


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

void main()
{
    int w, l;
    readf(" %s %s", w, l);

    int[] a = new int[w-1];

    foreach (ref t; a) readf(" %s", t);

    int[] b = new int[w-l];

    foreach (i; 0 .. l)
    {
        b[0] += a[i];
    }

    foreach (i; 1 .. b.length)
    {
        b[i] = b[i-1] - a[i-1] + a[i-1+l];
    }

    int mn = int.max;

    foreach (t; b)
    {
        mn = min(mn, t);
    }

    writeln(mn);
}

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 len, w;
    readf("%s %s", &w, &len);
    readln;

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

    int ans = 10 ^^ 9 + 23;
    int cur = 0;
    foreach (i; 0 .. len-1) {
        cur += arr[i];
    }

    foreach (i; len-1 .. w-1) {
        cur += arr[i];
        ans = min(ans, cur);
        cur -= arr[i - (len-1)];
    }

    ans.writeln;
}

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

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

void main()
{
  int w, l; readV(w, l);
  int[] a; readA(w-1, a);

  auto ac = cumulativeSum(a);

  auto calc(int k)
  {
    foreach (i; l..w)
      if (ac[i-l..i] < k) return true;
    return false;
  }

  auto bs = iota(ac[0..$]+1).map!(k => tuple(k, calc(k))).assumeSorted!"a[1] < b[1]";
  writeln(bs.lowerBound(tuple(0, true)).back[0]);
}

class CumulativeSum(T)
{
  size_t n;
  T[] s;

  this(T[] a)
  {
    n = a.length;
    s = new T[](n+1);
    s[0] = T(0);
    foreach (i; 0..n) s[i+1] = s[i] + a[i];
  }

  T opSlice(size_t l, size_t r) { return s[r]-s[l]; }
  size_t opDollar() { return n; }
}
auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }

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

void main(){

	int[100010] a;
	int n, m;

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

	for (int i = 1; i < n - 1; ++i) readf("%s ", &a[i]);
	readf("%s", &a[n - 1]);
	readln;

	for (int i = 1; i < n; ++i) a[i] += a[i - 1];

	int ans = 1000000000;
	for (int i = m; i < n; ++i) ans = min(ans, a[i] - a[i - m]);
	writeln(ans);
}

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

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

void main()
{
  int w, l; readV(w, l);
  int[] a; readA(w-1, a);

  auto ac = cumulativeSum(a);

  auto calc(int k)
  {
    foreach (i; l+1..w+1)
      if (ac[i-l+1..i] > k) return false;
    return true;
  }

  auto bs = iota(ac[0..$]+1).map!(k => tuple(k, calc(k))).assumeSorted!"a[1] < b[1]";
  writeln(bs.upperBound(tuple(0, false)).front[0]);
}

class CumulativeSum(T)
{
  size_t n;
  T[] s;

  this(T[] a)
  {
    n = a.length;
    s = new T[](n+1);
    s[0] = T(0);
    foreach (i; 0..n) s[i+1] = s[i] + a[i];
  }

  T opSlice(size_t l, size_t r) { return s[r]-s[l]; }
  size_t opDollar() { return n; }
}
auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }

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

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

void main()
{
  int w, l; readV(w, l);
  int[] a; readA(w-1, a);

  auto ac = cumulativeSum(a);

  auto calc(int k)
  {
    foreach (i; l..w+1)
      if (ac[i-l+1..i] > k) return false;
    return true;
  }

  auto bs = iota(ac[0..$]+1).map!(k => tuple(k, calc(k))).assumeSorted!"a[1] < b[1]";
  writeln(bs.upperBound(tuple(0, false)).front[0]);
}

class CumulativeSum(T)
{
  size_t n;
  T[] s;

  this(T[] a)
  {
    n = a.length;
    s = new T[](n+1);
    s[0] = T(0);
    foreach (i; 0..n) s[i+1] = s[i] + a[i];
  }

  T opSlice(size_t l, size_t r) { return s[r]-s[l]; }
  size_t opDollar() { return n; }
}
auto cumulativeSum(T)(T[] a) { return new CumulativeSum!T(a); }


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;

		int [] [int] s;
		foreach (int i, ref c; a)
		{
			s[c] ~= i;
		}

		auto answer = new int [n];
		answer[] = int.max;
		foreach (k, v; s)
		{
			int len = max (v.front, n - 1 - v.back);
			foreach (i; 1..v.length)
			{
				len = max (len, v[i] - v[i - 1] - 1);
			}
			answer[len] = min (answer[len], k);
		}

		foreach (i; 1..n)
		{
			answer[i] = min (answer[i], answer[i - 1]);
		}
		foreach (ref c; answer)
		{
			if (c == int.max)
			{
				c = -1;
			}
		}

		writefln !("%(%s %)") (answer);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto pos = new int[][](n);
		foreach (i; 0..n)
		{
			pos[a[i]] ~= i;
		}
		auto diff = new int[](n);
		foreach (i; 0..n)
		{
			if (pos[i].empty)
			{
				diff[i] = n+1;
				continue;
			}
			int d = pos[i][0] + 1;
			foreach (j; 1..pos[i].length)
			{
				d.chmax(pos[i][j] - pos[i][j-1]);
			}
			d.chmax(n-pos[i][$-1]);
			diff[i] = d;
		}
		auto num = new int[](n+2);
		num[] = int.max;
		foreach (i; 0..n)
		{
			num[diff[i]].chmin(i);
		}

		ans[ti].length = n;
		int last = int.max;
		foreach (i; 0..n)
		{
			last.chmin(num[i+1]);
			if (last == int.max)
				ans[ti][i] = -1;
			else
				ans[ti][i] = last+1;
		}
	}

	foreach (e; ans)
	{
		e.map!(to!string).join(" ").writeln;
	}
	stdout.flush;
	debug readln;
}


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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long c, d;
    readln.formattedRead!" %d %d "(c, d);
    if (c > d)
      swap(c, d);
    if ((d - c) % 2 == 1) {
      writeln(-1);
    } else if (c == d) {
      writeln(c == 0 ? 0 : 1);
    } else {
      writeln(2);
    }
  }
}

immutable multi = true;

void solve(int tc)
{
	auto c = readInt!int;
	auto d = readInt!int;
	auto diff = abs(c - d);
	if (c == d && c == 0)
	{
		return writeln(0);
	}
	if (diff % 2 == 0)
	{
		writeln(1 + int(diff != 0));
	}
	else
	{
		writeln(-1);
	}
}

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

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

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

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

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int c, d;
		readf !(" %s %s") (c, d);
		auto total = c + d;
		if (total % 2 != 0)
		{
			writeln (-1);
			continue;
		}
		int res = 0;
		if (total != 0)
		{
			auto half = total / 2;
			c -= half;
			d -= half;
			res += 1;
		}
		if (c != 0 || d != 0)
		{
			res += 1;
		}
		writeln (res);
	}
}


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

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

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

    arr.sort();

    auto ans = cast(long)(arr[n-1] - arr[0]) * (arr[$-1] - arr[n]);

    auto sz = arr[$-1] - arr[0];
    foreach (i; 1 .. n) {
        ans = min(ans, cast(long)sz * (arr[i+n-1] - arr[i]));
    }
    ans.writeln;
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		long res = (a[n - 1] - a[0]) * 1L * (a[$ - 1] - a[$ - n]);
		foreach (i; 0..n - 1)
		{
			res = min (res, (a[i + n] - a[i + 1]) * 1L *
			    (a[$ - 1] - a[0]));
		}
		writeln (res);
	}
}

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 A = new long[2 * N];
      foreach (i; 0 .. 2 * N) {
        A[i] = readLong();
      }
      A.sort;

      long ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N]);
      foreach (i; 1 .. N) {
        chmin(ans, (A[i + N - 1] - A[i]) * (A[2 * N - 1] - A[0]));
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

    arr.sort();

    auto ans = cast(long)(arr[n-1] - arr[0]) * (arr[$-1] - arr[n]);

    ans.writeln;
}

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 A = new long[2 * N];
      foreach (i; 0 .. 2 * N) {
        A[i] = readLong();
      }
      A.sort;

      long ans = 1;
      ans *= (A[N - 1] - A[0]);
      ans *= (A[2 * N - 1] - A[N]);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


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

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

    auto g = new int[][n+1];
    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto trace = make!(DList!int);
    trace.insertBack(1);

    auto step = new int[n+1];
    step[] = 0;
    step[1] = 1;

    outer: foreach (i; 2 .. n+1) {
        auto v = trace.back;

        foreach (u; g[v]) {
            if (step[u] == 0) {
                step[u] = i;
                trace.insertBack(u);
                break;
            }

            if (i - step[u] <= k) { continue; }

            while (trace.front != u) { trace.removeFront(); }
            break outer;
        }
    }

    auto ans = trace.array;
    ans.length.writeln;
    ans.writefln!"%(%s %)";
}

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

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

    auto g = new int[][n+1];
    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto trace = make!(DList!int);
    auto vis = new bool[n+1];
    vis[] = false;
    auto curIn = make!(RedBlackTree!int);
    auto curOrd = make!(DList!int);

    curIn.insert(1);
    trace.insertBack(1);
    curOrd.insertBack(1);
    vis[1] = true;
    outer: while (true) {
        auto v = curOrd.back;
        foreach (u; g[v]) {
            if (u in curIn) { continue; }

            debug { writeln(v, ' ', u,  ' ', curIn); }

            if (curIn.length == k) {
                if (vis[u]) {
                    while (trace.front != u) { trace.removeFront(); }
                    break outer;
                }

                auto prev = curOrd.front;
                curOrd.removeFront();
                curIn.removeKey(prev);
            }

            vis[u] = true;
            curOrd.insertBack(u);
            trace.insertBack(u);
            curIn.insert(u);

            break;
        }
    }

    auto ans = trace.array;
    ans.length.writeln;
    ans.writefln!"%(%s %)";
}

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

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

    auto g = new int[][n+1];
    foreach (_; 0 .. m) {
        int u, v;
        readf("%s %s", &u, &v);
        readln;

        g[u] ~= v;
        g[v] ~= u;
    }

    auto vis = new bool[n+1];
    vis[] = false;
    auto curIn = make!(RedBlackTree!int);
    auto curOrd = make!(DList!int);

    curIn.insert(1);
    curOrd.insertBack(1);
    vis[1] = true;
    outer: while (true) {
        auto v = curOrd.back;
        foreach (u; g[v]) {
            if (u in curIn) { continue; }

            debug { writeln(v, ' ', u,  ' ', curIn); }

            if (curIn.length == k) {
                if (vis[u]) {
                    curOrd.insertBack(u);
                    break outer;
                }

                auto prev = curOrd.front;
                curOrd.removeFront();
                curIn.removeKey(prev);
            }

            vis[u] = true;
            curOrd.insertBack(u);
            curIn.insert(u);

            break;
        }
    }

    writeln(k+1);
    curOrd.array.writefln!"%(%s %)";
}


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

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

void solve(){
	foreach(_; 0 .. rint){
		long n = rint, m = rint;
		long k = m + 1;
		long u = n - m;
		long x = u / k, y = (u + k - 1) / k;
		long i = k - (u % k), j = u % k;

		long ans = n * (n + 1) / 2 - i * x * (x + 1) / 2 - j * y * (y + 1) / 2;
		ans.writeln;
	}
}

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.ascii, std.typecons;

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

    while (t--) {
        int n, m;
        readf("%s %s", &n, &m);
        readln;

        auto mn = (n - m) / (m+1);
        auto mxcnt = (n - m) % (m+1);
        auto mncnt = m+1 - mxcnt;

        auto mnval = (mn+1).to!long * mn / 2;
        auto mntot = mnval * mncnt;

        auto mx = mn+1;
        auto mxval = (mx+1).to!long * mx / 2;
        auto mxtot = mxval * mxcnt;

        auto all = (n+1).to!long * n / 2;

        auto ans = all - mntot - mxtot;

        debug { writeln(all, ' ', mn, ' ', mncnt, ' ', mnval, ' ', mxtot); }

        ans.writeln;
    }
}

import std.stdio : writeln;

struct IO {
    string readToken() {
        import std.range : empty, front, popFront, split;
        import std.stdio : readln;

        while (tokens.empty) {
            tokens = readln.split;
        }
        auto token = tokens.front;
        tokens.popFront;
        return token;
    }

    int readInt() {
        import std.conv : to;

        return readToken.to!int;
    }

    string[] tokens;
}

long binom2(int n) {
    return n * (n + 1L) >> 1;
}

void main() {
    IO io;
    int T = io.readInt;
    while (T--) {
        int n = io.readInt;
        int m = io.readInt;
        int sz = (n - m) / (m + 1);
        int cnt = (n - m) % (m + 1);
        writeln(binom2(n) - (m + 1 - cnt) * binom2(sz) - cnt * binom2(sz + 1));
    }
}

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

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

  long n, m;

  void solve(long tc = -1)
  {
    auto z = n - m;
    auto c0 = (m + 1 - z % (m + 1));
    auto c1 = z % (m + 1);
    auto total = n * (n + 1) / 2;
    auto perbox = z / (m + 1);
    auto withz = (perbox * (perbox + 1) / 2) * c0 + ((perbox + 1) * (perbox + 2) / 2) * c1;
    writeln(total - withz);
  }
}

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

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

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

DList!string _words;

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

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

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, string))
              {
                static assert(isDynamicArray!(typeOfField)
                              , " A field with dimension initialization UDA must be a dynamic array.");
                enum uda = getUDAs!(fieldSymbol, string)[0];
                mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda, q{)});
              }
            else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
              {
                pragma(msg, "Warning: You probably want to add dimension to input ", fieldName);
              }
            read(mixin(fieldName));
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

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

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

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

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

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

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

    }
}

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

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

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

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

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

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

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

void main()
{
  type;
}

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

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

  long n, m;

  void solve(long tc = -1)
  {
    auto k = (m - n) / 2;
    writeln((n - k) * m - (m * (m - 1) / 2));
  }
}

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

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

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

DList!string _words;

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

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

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, string))
              {
                static assert(isDynamicArray!(typeOfField)
                              , " A field with dimension initialization UDA must be a dynamic array.");
                enum uda = getUDAs!(fieldSymbol, string)[0];
                mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda, q{)});
              }
            else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
              {
                pragma(msg, "Warning: You probably want to add dimension to input ", fieldName);
              }
            read(mixin(fieldName));
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

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

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

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

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

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

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

    }
}

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

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

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

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

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

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

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

void main()
{
  type;
}

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

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

  long n, m;

  void solve(long tc = -1)
  {
    if (m == 0)
      {
        writeln(0);
        return;
      }
    auto k = (n - m) / 2;
    writeln((n - k) * (m + k) - (m * (m - 1) / 2));
  }
}

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

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

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

DList!string _words;

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

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

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, string))
              {
                static assert(isDynamicArray!(typeOfField)
                              , " A field with dimension initialization UDA must be a dynamic array.");
                enum uda = getUDAs!(fieldSymbol, string)[0];
                mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda, q{)});
              }
            else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
              {
                pragma(msg, "Warning: You probably want to add dimension to input ", fieldName);
              }
            read(mixin(fieldName));
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

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

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

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

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

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

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

    }
}

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

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

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

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

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

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

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

void main()
{
  type;
}


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

long n, k, x;

long xorp(long x, long y, long param)
{
  long ans = 0;
  long ansmul = 1;
  while (x > 0 || y > 0) {
    long digitx = x % k;
    long digity = y % k;
    long ansdigit;
    switch (param & 7) {
      case 0:
        ansdigit = 0;
        break;
      case 1:
        ansdigit = k - 1;
        break;
      case 2:
        ansdigit = digitx;
        break;
      case 3:
        ansdigit = (k + digity - digitx) % k;
        break;
      case 4:
        ansdigit = (k + digitx - digity) % k;
        break;
      case 5:
        ansdigit = (digity + digitx) % k;
        break;
      case 6:
        ansdigit = (2 * k - digity - digitx) % k;
        break;
      default:
        ansdigit = digity;
        break;
    }
    ans += ansdigit * ansmul;
    x /= k;
    y /= k;
    ansmul *= k;
  }
  return ans;
}

bool solve(long param, bool function(long) judge)
{
  long param1 = param & 7;
  param >>= 3;
  long param2 = param & 7;
  param >>= 3;
  long param3 = param & 7;
  param >>= 3;
  long param4 = param & 7;
  param >>= 3;
  long param5 = param & 7;
  param >>= 3;
  long param6 = param & 7;
  param >>= 3;

  long distortion = 0;
  foreach (i ; 0 .. n) {
    if (i % 2 == 0) {
      if (judge(xorp(distortion, i, param1)))
        return true;
      distortion = xorp(distortion, xorp(distortion, i, param2), param3);
    } else {
      if (judge(xorp(distortion, i, param4)))
        return true;
      distortion = xorp(distortion, xorp(distortion, i, param5), param6);
    }
  }
  return false;
}

bool interactive_judge(long y)
{
  writeln(y);
  stdout.flush;
  return readln.strip == "1";
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    auto a = readln.strip.split.map!(to!long).array;
    n = a[0];
    k = a[1];
    solve(0x0001C8ED, &interactive_judge);
  }
}

// code by cutx64

module main;

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

void main() {

  int tests; readf!"%s\n"(tests);
  while(tests--) {
    int n, k, r;
    readf!"%s %s\n"(n, k);
    stdout.writefln!"0";
    stdout.flush;
    readf!"%s\n"(r);
    int kXor(int x, int y) {
      int c = 0, p = 1;
      while(x > 0 || y > 0) {
        int a = x % k; x /= k;
        int b = y % k; y /= k;
        c += ((a - b + k) % k) * p;
        p *= k;
      }
      return c;
    }
    if (r == 0) {
      foreach(i; 1 .. n) {
        if (i % 2 == 0) {
          stdout.writefln!"%s"(kXor(i, i - 1));
          stdout.flush;
        }
        else {
          stdout.writefln!"%s"(kXor(i - 1, i));
          stdout.flush;
        }
        readf!"%s\n"(r);
        if (r == 1) {
          break;
        }
      }
    }
  }

} // main

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

long xork(long x, long y, long k)
{
  long ans = 0;
  long ansmul = 1;
  while (ansmul < 10000000) {
    long digitx = x % k;
    long digity = y % k;
    long ansdigit = (digitx + digity) % k;
    ans += ansdigit * ansmul;
    x /= k;
    y /= k;
    ansmul *= k;
  }
  return ans;
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n, k;
    auto a = readln.strip.split.map!(to!long).array;
    n = a[0];
    k = a[1];
    long i = 0;
    long distortion = 0;
    while (true) {
      writeln(xork(i, distortion, k));
      stdout.flush;
      string ans = readln;
      if (ans.strip == "1") {
        break;
      }
      distortion = xork(distortion, xork(i, distortion, k), k);
      i++;
    }
  }
}