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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        int[] rs;
        int k = 0;
        while (N) {
            if (N%10 != 0) {
                rs ~= N%10 * 10^^k;
            }
            N /= 10;
            ++k;
        }
        writeln(rs.length);
        writeln(rs.to!(string[]).join(" "));
    }
}

void main() {
	auto t = ri;
	foreach(i; 0..t) {
		auto S = rs;
		ulong cnt;
		ulong[] l;
		foreach(j, c; S) {
			if(c != '0') {
				cnt++;
				l ~= 10 ^^ (S.length - j - 1) * (c - '0');
			}
		}
		cnt.writeln;
		writefln("%(%d %)", l);
	}
}

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

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

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

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

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

int ri() {
	return readAs!int;
}

long rl() {
	return readAs!long;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

import std.stdio;

int main() {
    int t;
    readf("%d\n", &t);
    while(t--) {
    	int n;
    	readf("%d\n", &n);
    	int[] result;
    	int d = 1;
    	while(n > 0) {
    		if(n % 10 != 0)
    			result ~= (n % 10) * d;
    		d *= 10;
    		n /= 10;
    	}

    	writeln(result.length);
		foreach(x; result)
			write(x, " ");
    	writeln();
    }

    return 0;
}

// Author: Ivan Kazmenko (gassa@mail.ru)
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		auto d = s.length;
		string [] ans;
		foreach (i, c; s)
		{
			if (c > '0')
			{
				ans ~= c ~ "0".repeat (d - i - 1).joiner.text;
			}
		}
		writeln (ans.length);
		writefln !("%-(%s %)") (ans);
	}
}

// Author: Ivan Kazmenko (gassa@mail.ru)
// dynamic programming with large constant, run once at the start
import std.algorithm;
import std.conv;
import std.random;
import std.range;
import std.stdio;
import std.string;

auto prepare (int limit)
{
	auto moves = iota (1, limit)
	    .filter !(j => j.text.count !(q{a > '0'}) == 1).array;
	auto f = new int [] [limit];
	foreach (i; moves)
	{
		f[i] ~= i;
	}
	foreach (i; 1..limit)
	{
		foreach (j; moves)
		{
			if (j < i)
			{
				if (f[i] is null || f[i].length >
				    f[i - j].length + uniform (0, 2))
				{
					f[i] = f[i - j] ~ j;
				}
			}
		}
	}
	return f;
}

void main ()
{
	rndGen.seed (25256);
	auto f = prepare (10001);
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		writeln (f[n].length);
		writefln !("%(%s %)") (f[n]);
	}
}

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

void calc(int n) {
    int[] ans;
    int x = 1000000;
    while (x > 0) {
        int t = n / x;
        if (t > 0) ans ~= t * x;
        n %= x;
        x /= 10;
    }
    writeln(ans.length);
    writeln(ans.map!text.join(' '));
}

void main() {
    int t; scan(t);
    foreach (_; 0..t) {
        int n; scan(n);
        calc(n);
    }
}

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T=string)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readints = reads!int;

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

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto t = readNum!int;

  foreach(i; 0 .. t){
    auto s = readStr;
    auto n = s.length - count(s, "0");
    writeln(n);

    foreach_reverse(j, e; s){
      if(e != '0'){
        write(e);
        foreach(k; 0 .. s.length - j - 1){
          write("0");
        }
        write(" ");
      }
    }

    writeln();
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!string;
		foreach (i; 0..n.length)
		{
			if (n[i] != '0')
			{
				ans[ti] ~= (n[i]-'0') * 10^^(n.length-i-1);
			}
		}
	}

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


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;

    int[int] cnt;
    readln.chomp.split.each!(x => ++cnt[x.to!int]);

    debug { cnt.writeln; }

    cnt.remove(0);
    if (cnt.values.any!(x => x > 2)) {
        writeln(-1);
        return;
    }

    cnt.values.count!(x => x == 2).writeln;
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int [int] v;
		foreach (i; 0..n)
		{
			int id;
			readf (" %s", &id);
			if (id != 0)
			{
				v[id]++;
			}
		}
		int res = 0;
		foreach (u; v)
		{
			if (u > 2)
			{
				res = -1;
				break;
			}
			else if (u == 2)
			{
				res++;
			}
		}
		writeln (res);
	}
}

module sigod.codeforces.p291A;

import std.algorithm;
import std.stdio;

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

	int[] d = new int[n];
	foreach (i; 0 .. n) {
		stdin.readf(" %s", &d[i]);
	}

	stdout.writeln(solve(d));
}

//*

int solve(int[] d)
{
	d.sort();

	int start = -1;
	foreach (i, di; d) {
		if (di != 0) {
			start = i;
			break;
		}
	}

	if (start != -1) {
		d = d[start .. $];
	}
	else if (d[d.length - 1] == 0) {
		return 0;
	}

	int count = 0;

	foreach (tuple; d.group()) {
		if (tuple[1] == 2) {
			++count;
		}
		else if (tuple[1] > 2) {
			return -1;
		}
	}

	return count;
}

unittest {
	assert(solve([0, 1, 7, 1, 7, 10]) == 2);
	assert(solve([1, 1, 1]) == -1);
	assert(solve([0]) == 0);
}

import std.stdio, std.conv, std.string;
void main()
{
    uint n;
    readf("%d\n", &n);
    uint[uint] sessions;
    uint session;
    uint pairs;
    string sessionString;
    sessionString = readln();
    foreach(i;0..n)
    {
        session = parse!uint(sessionString);
        munch(sessionString, " ");
        if (session == 0) continue;
        if (session !in sessions)
        {
            sessions[session] = 1;
        }
        else
        {
            ++sessions[session];
            ++pairs;
            if (sessions[session] > 2)
            {
                write(-1);
                return;
            }
        }
    }
    write(pairs);
}

import std.stdio;
import std.algorithm;

void main() {
	int n;
	readf(" %s", &n);
	int[] a;
	a.length = n;
	for (int i = 0; i < n; i++) {
		readf(" %s", &a[i]);
	}

	sort(a);
	int cnt = 0;
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (a[i] == 0)
			continue;
		if (i > 0 && a[i] == a[i - 1])
			++cnt;
		else
			cnt = 1;

		if (cnt > 2) {
			ans = -1;
			break;
		}
		if (cnt == 2)
			++ans;
	}
	writeln(ans);
}

import std.stdio, std.conv, std.string;
void main()
{
	uint n;
	readf("%d\n", &n);
	uint[uint] sessions;
	uint session;
	uint pairs;
	string sessionString;
	sessionString = readln();
	foreach(i;0..n)
	{
		session = parse!uint(sessionString);
		munch(sessionString, " ");
		if (session !in sessions)
		{
			sessions[session] = 1;
		}
		else
		{
			++sessions[session];
			++pairs;
			if (sessions[session] > 2)
			{
				write(-1);
				return;
			}
		}
	}
	write(pairs);
}

import std.stdio;
import std.algorithm;

void main() {
	int n;
	readf(" %s", &n);
	int[] a;
	a.length = n;
	for (int i = 0; i < n; i++) {
		readf(" %s", &a[i]);
	}

	sort(a);
	int cnt = 1;
	int ans = 0;
	for (int i = 1; i < n; i++) {
		if (a[i] == a[i - 1])
			++cnt;
		else
			cnt = 1;

		if (cnt > 2) {
			ans = -1;
			break;
		}
		if (cnt == 2)
			++ans;
	}
	writeln(ans);
}


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

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

		auto c = new int [] [] (n, k);
		auto d = new long [] [] (n, k);
		long res = 0;

		void recur (int v, int p)
		{
			c[v][0] = 1;
			c[v][1..$] = 0;
			d[v][0] = 0;
			d[v][1..$] = 0;
			foreach (u; a[v])
			{
				if (u == p)
				{
					continue;
				}
				recur (u, v);
				foreach (i; 0..k)
				{
					foreach (j; 0..k)
					{
						int carry = (i + j + 1 > 0) +
						    (i + j + 1 > k);
						res += d[v][i] * c[u][j];
						res += d[u][j] * c[v][i];
						res += carry * c[u][j] * 1L *
						    c[v][i];
					}
				}
				foreach (i; 0..k)
				{
					int j = i + 1;
					if (j == k)
					{
						j = 0;
					}
					c[v][j] += c[u][i];
					d[v][j] += d[u][i];
					if (j == 0)
					{
						d[v][j] += c[u][i];
					}
				}
			}
		}

		recur (0, -1);
		debug {writeln (c); writeln (d);}

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

int N, K;
int[] A, B;

int[][] G;
long ans;

void solveCentroidDecomp() {
  auto sz = new int[N];
  auto del = new bool[N];
  void dfsSz(int u, int p) {
    sz[u] = 1;
    foreach (i; G[u]) {
      const v = A[i] ^ B[i] ^ u;
      if (v != p) {
        dfsSz(v, u);
        sz[u] += sz[v];
      }
    }
  }
  dfsSz(0, -1);

  // r: centroid
  void solveSubtree(int r) {
    debug {
      string dfsDebug(int u, int p) {
        string ret = u.to!string;
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          if (!del[v] && v != p) {
            ret ~= "(" ~ dfsDebug(v, u) ~ ")";
          }
        }
        return ret;
      }
      writeln("solveSubtree ", dfsDebug(r, -1));
    }

    int[] vs;
    foreach (i; G[r]) {
      const v = A[i] ^ B[i] ^ r;
      if (!del[v]) {
        vs ~= v;
      }
    }

    const len = cast(int)(vs.length);
    auto fs0 = new long[K];
    auto fs1 = new long[K];
    auto gs0 = new long[][](len, K);
    auto gs1 = new long[][](len, K);

    void dfs(int j, int u, int p, int d) {
      fs0[d % K] += 1;
      fs1[d % K] += d;
      gs0[j][d % K] += 1;
      gs1[j][d % K] += d;
      foreach (i; G[u]) {
        const v = A[i] ^ B[i] ^ u;
        if (!del[v] && v != p) {
          dfs(j, v, u, d + 1);
        }
      }
    }
    fs0[0] += 1;
    fs1[0] += 0;
    foreach (j; 0 .. len) {
      dfs(j, vs[j], r, 1);
    }

    debug {
      writeln("fs0 = ", fs0);
      writeln("gs1 = ", fs1);
      writeln("fs0 = ", gs0);
      writeln("gs1 = ", gs1);
    }
    foreach (x; 0 .. K) foreach (y; 0 .. K) {
      const z = (K - (x + y) % K) % K;
      ans += fs1[x] * fs0[y];
      ans += fs0[x] * fs1[y];
      ans += fs0[x] * fs0[y] * z;
      foreach (j; 0 .. len) {
        ans -= gs1[j][x] * gs0[j][y];
        ans -= gs0[j][x] * gs1[j][y];
        ans -= gs0[j][x] * gs0[j][y] * z;
      }
    }
  }

  void solveRec(int u) {
    for (; ; ) {
      int vm = -1;
      foreach (i; G[u]) {
        const v = A[i] ^ B[i] ^ u;
        if (!del[v]) {
          if (vm == -1 || sz[vm] < sz[v]) {
            vm = v;
          }
        }
      }
      if (vm == -1 || sz[u] >= 2 * sz[vm]) {
        solveSubtree(u);
        del[u] = true;
        foreach (i; G[u]) {
          const v = A[i] ^ B[i] ^ u;
          if (!del[v]) {
            solveRec(v);
          }
        }
        break;
      } else {
        sz[u] -= sz[vm];
        sz[vm] += sz[u];
        u = vm;
      }
    }
  }
  solveRec(0);
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      K = readInt();
      A = new int[N - 1];
      B = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
      }

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }

      ans = 0;
      solveCentroidDecomp;
      ans /= K;
      ans /= 2;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!int;
  auto k = next!int - 1;
  auto perm = new int[](n);
  void solve(int i, int j, int l)
  {
    debug writeln(i, " ", j, " ", l, " ", k);
    if (k == 0 || j - i == 1)
      {
	int v = l;
	foreach(k; i .. j)
	  {
	    perm[k] = v;
	    v++;
	  }
	return;
      }
    int mid = (i + j) / 2;
    auto ls = mid - i;
    auto rs = j - i - ls;
    k -= 2;
    solve(i, mid, l + rs);
    solve(mid, j, l);
  }
  solve(0, n, 1);
  if (k != 0) return (-1).writeln;
  perm.each!(p => p.write(" ")), writeln;
}

// INPUT

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

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

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

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

    if (k % 2 == 0) {
        writeln(-1);
        return;
    }

    auto nodes = new int[] (n+1);
    nodes[1] = 1;
    foreach (i; 2 .. n+1) {
        int lft = i/2;
        int rt = i - lft;
        nodes[i] = nodes[lft] + 1 + nodes[rt];
    }

    if (nodes[n] < k) {
        writeln(-1);
        return;
    }

    auto arr = (n+1).iota.dropOne.array;

    void go(int le, int r, int need) {
        if (le+1 == r) { return; }

        if (need == 0) { return; }

        int md = (le + r) / 2;

        swap(arr[md-1], arr[md]);
        need -= 2;

        int capLft = nodes[md - le] - 1;
        int needLft = min(need, capLft);
        go(le, md, needLft);
        go(md, r, need - needLft);
    }

    go(0, n, k-1);

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

    if (k % 2 == 0) {
        writeln(-1);
        return;
    }

    auto szCap = new int[] (n+1);

    int getCap(int sz) {
        if (szCap[sz] != 0) { return szCap[sz]; }

        foreach (i; 1 .. 20) {
            int val = 1 << i;
            if (val > n) { break; }

            if (! (val & sz) ) { continue; }

            szCap[sz] += val * 2 - 1;
        }

        return szCap[sz];
    }

    if (getCap(n) < k) {
        writeln(-1);
        return;
    }

    auto arr = (n+1).iota.dropOne.array;

    void go(int le, int r, int need) {
        if (le+1 == r) { return; }

        if (need == 0) { return; }

        int md = (le + r) / 2;

        swap(arr[md-1], arr[md]);
        need -= 2;

        int capLft = getCap(md - le);
        int needLft = min(need, capLft);
        go(le, md, needLft);
        go(md, r, need - needLft);
    }

    go(0, n, k-1);

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

    if (k % 2 == 0) {
        writeln(-1);
        return;
    }

    auto nodes = new int[] (n+1);

    int getNodes(int sz) {
        if (nodes[sz] != 0) { return nodes[sz]; }

        foreach (i; 0 .. 20) {
            int val = 1 << i;
            if (val > n) { break; }

            if (! (val & sz) ) { continue; }

            nodes[sz] += val * 2 - 1;
        }

        return nodes[sz];
    }

    if (getNodes(n) < k) {
        writeln(-1);
        return;
    }

    auto arr = (n+1).iota.dropOne.array;

    void go(int le, int r, int need) {
        if (le+1 == r) { return; }

        if (need == 0) { return; }

        int md = (le + r) / 2;

        swap(arr[md-1], arr[md]);
        need -= 2;

        int capLft = getNodes(md - le) - 1;
        int needLft = min(need, capLft);
        go(le, md, needLft);
        go(md, r, need - needLft);
    }

    go(0, n, k-1);

    arr.writefln!"%(%s %)";
}


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

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

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

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

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

      long df(int i, long b) {
        return A[i] - 1 - 3 * b * (1 + b);
      }

      long calc(int i, long val) {
        // >= val, < val
        long lo = -1, hi = A[i];
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          ((df(i, mid) >= val) ? lo : hi) = mid;
        }
        return hi;
      }

      // >= K, < K
      long lo = -4 * 10L^^18, hi = 2 * 10L^^9;
      for (; lo + 1 < hi; ) {
        const mid = (lo + hi) / 2;
        long num;
        foreach (i; 0 .. N) {
          num += calc(i, mid);
        }
        ((num >= K) ? lo : hi) = mid;
      }
      debug {
        // writeln("lo = ", lo, ", hi = ", hi);
      }

      auto bs = new long[N];
      long now;
      foreach (i; 0 .. N) {
        now += bs[i] = calc(i, hi);
      }
      foreach (i; 0 .. N) {
        if (now < K && bs[i] < A[i] && df(i, bs[i]) == lo) {
          ++now;
          ++bs[i];
        }
      }
      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(bs[i]);
      }
      writeln();

      debug {
        auto brt = new long[N];
        foreach (k; 0 .. K) {
          long mx = long.min;
          int im = -1;
          foreach (i; 0 .. N) {
            if (brt[i] < A[i]) {
              if (chmax(mx, df(i, brt[i]))) {
                im = i;
              }
            }
          }
          assert(im != -1);
          // writeln(im, " ", mx);
          ++brt[im];
        }
        writeln(brt);
        assert(brt == bs);
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable long infinity = 3 * 10L ^^ 18;

long fun (int a, int b) {
	return b * 3L * (b - 1) - a + 1;
}

int lastB (long border, int a) {
	int lo = 0;
	int hi = a;
	while (lo < hi) {
		int me = (lo + hi + 1) / 2;
		long cur = fun (a, me);
		if (cur > border)
			hi = me - 1;
		else
			lo = me;
	}
	return lo;
}

void main () {
	int n;
	long k;
	while (readf !(" %s %s") (n, k) > 0) {
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		long lo = -infinity;
		long hi = +infinity;
		while (lo < hi) {
			long me = lo + (hi - lo) / 2;
			long cur = 0;
			foreach (i; 0..n)
				cur += lastB (me, a[i]);
			if (cur >= k)
				hi = me;
			else
				lo = me + 1;
		}

		auto b = a.dup;
		foreach (i; 0..n) {
			b[i] = lastB (lo, a[i]);
			k -= b[i];
		}
		foreach (i; 0..n) {
			if (k < 0 && fun (a[i], b[i]) == lo) {
				k += 1;
				b[i] -= 1;
			}
		}
		writefln !("%(%s %)") (b);
	}
}

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

immutable long infinity = 3 * 10L ^^ 18;

long fun (int a, int b)
{
	return b * 3L * (b - 1) - a + 1;
}

int lastB (long border, int a)
{
	int lo = 0;
	int hi = a;
	while (lo < hi)
	{
		int me = (lo + hi + 1) / 2;
		long cur = fun (a, me);
		if (cur > border)
		{
			hi = me - 1;
		}
		else
		{
			lo = me;
		}
	}
	return lo;
}

void main ()
{
	int n;
	long k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		long total = a.sum (0L);
		long lo = -infinity;
		long hi = +infinity;
		while (lo < hi)
		{
			long me = lo + (hi - lo) / 2;
			long cur = 0;
			foreach (i; 0..n)
			{
				cur += lastB (me, a[i]);
			}
//			writeln (lo, " ", me, " ", hi, " ", cur);
			if (cur >= k)
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}

		auto b = a.dup;
		foreach (i; 0..n)
		{
			b[i] = lastB (lo, a[i]);
			k -= b[i];
		}
		foreach (i; 0..n)
		{
//			writeln (i, " ", k, " ", fun (a[i], b[i]), " ", lo);
			if (k < 0 && fun (a[i], b[i]) == lo)
			{
				k += 1;
				b[i] -= 1;
			}
		}
		writefln !("%(%s %)") (b);
	}
}

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

immutable long infinity = 4 * 10L ^^ 18;

long fun (int a, int b)
{
	return b * 3L * (b - 1) - a + 1;
}

int lastB (long border, int a)
{
	int lo = 0;
	int hi = a;
	while (lo < hi)
	{
		int me = (lo + hi + 1) / 2;
		long cur = fun (a, me);
		if (cur > border)
		{
			hi = me - 1;
		}
		else
		{
			lo = me;
		}
	}
	return lo;
}

void main ()
{
	int n;
	long k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		long total = a.sum (0L);
		long lo = 0;
		long hi = infinity;
		while (lo < hi)
		{
			long me = (lo + hi) / 2;
			long cur = 0;
			foreach (i; 0..n)
			{
				cur += lastB (me, a[i]);
			}
//			writeln (lo, " ", me, " ", hi, " ", cur);
			if (cur >= k)
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}

		auto b = a.dup;
		foreach (i; 0..n)
		{
			b[i] = lastB (lo, a[i]);
			k -= b[i];
		}
		foreach (i; 0..n)
		{
//			writeln (i, " ", k, " ", fun (a[i], b[i]), " ", lo);
			if (k < 0 && fun (a[i], b[i]) == lo)
			{
				k += 1;
				b[i] -= 1;
			}
		}
		writefln !("%(%s %)") (b);
	}
}


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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

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

void main()
{
	auto t = RD!int;
	auto ans = new string[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD;
		auto s = RD!string;

		long x;
		long[] cnt;
		foreach (i; 0..n-1)
		{
			++x;
			if (s[i] != s[i+1])
			{
				cnt ~= x;
				x = 0;
			}
		}
		cnt ~= x+1;

		long p = s[0] == '0' ? 0 : 1;
		if (cnt.length != 1)
		{
			foreach (i; 0..cnt.length)
			{
				if (i % 2 != p) continue;

				if (i == 0)
				{
					cnt[i+1] += min(cnt[i], m);
					cnt[i] = max(cnt[i]-m, 0);
				}
				else if (i == cnt.length-1)
				{
					cnt[i-1] += min(cnt[i], m);
					cnt[i] = max(cnt[i]-m, 0);
				}
				else
				{
					cnt[i-1] += min(cnt[i]/2, m);
					cnt[i+1] += min(cnt[i]/2, m);
					cnt[i] = max(cnt[i]-m*2, cnt[i]%2);
				}
			}
		}

		foreach (i; 0..cnt.length)
		{
			foreach (e; 0..cnt[i])
				ans[ti] ~= cast(char)('0'+p);
			p ^= 1;
		}
	}

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

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

void main()
{
  int t, n, m;
  readf!" %d "(t);
  foreach (i ; 0 .. t) {
    readf!" %d %d"(n, m);
    readln;
    auto a = readln.chomp.split("").map!(to!int).array;
    for (int j = 0; j < a.length; j++) {
      if (a[j] == 1) {
        a[j] = 0;
      } else {
        a[j] = -1;
      }
    }
    int step = 0;
    bool progress = true;
    while (progress) {
      progress = false;
      for (int j = 0; j < a.length; j++) {
        if (a[j] != -1)
          continue;
        int left = -1;
        if (j - 1 >= 0)
          left = a[j - 1];
        int right = -1;
        if (j + 1 < a.length)
          right = a[j + 1];
        if (left == step && right != step) {
          a[j] = step + 1;
          progress = true;
        } else if (left != step && right == step) {
          a[j] = step + 1;
          progress = true;
        }
      }
      step++;
    }
    auto b = a.map!(x => (x >= 0 && x <= m) ? "1" : "0").joiner("");
    writeln(b);
  }
}

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, m;
		readf !(" %s %s") (n, m);
		readln;
		auto s = '#' ~ readln.strip.dup ~ '#';
		m = min (m, n);
		foreach (step; 0..m)
		{
			auto t = s.dup;
			foreach (i; 1..n + 1)
			{
				if ((s[i - 1] == '1') ^ (s[i + 1] == '1'))
				{
					t[i] = '1';
				}
			}
			s = t;
		}
		writeln (s[1..$ - 1]);
	}
}


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

immutable int NA = -1;

void main ()
{
	int n, m, q;
	while (readf (" %s %s %s", &n, &m, &q) > 0)
	{
		auto a = new int [] [n];

		foreach (i; 0..m)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u--;
			v--;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto b = new int [n];

		auto d = new int [n];
		auto p = new int [n];
		foreach (k; 0..n)
		{
			if (b[k] > 0)
			{
				continue;
			}

			Tuple !(int, int) recur (int v, int c, int r)
			{
				if (b[v] >= c)
				{
					return Tuple !(int, int) (0, 0);
				}
				b[v] = c;
				p[v] = k;

				auto res = Tuple !(int, int) (r, v);
				foreach (u; a[v])
				{
					res = max (res, recur (u, c, r + 1));
				}
				return res;
			}

			int diameter (int v)
			{
				auto p = recur (v, 1, 0);
				auto q = recur (p[1], 2, 0);
				return q[0];
			}

			d[k] = diameter (k);
			debug {writeln (k, ' ', d[k]);}
		}

		int root (int v)
		{
			if (p[v] == v)
			{
				return v;
			}
			return (p[v] = root (p[v]));
		}

		void unite (int x, int y)
		{
			x = root (x);
			y = root (y);
			if (x == y)
			{
				return;
			}
			d[x] = max (d[x], d[y],
			 ((d[x] + 1) >> 1) + ((d[y] + 1) >> 1) + 1);
			debug {writeln (x, ' ', y, ' ', d[x]);}
			p[y] = x;
		}

		foreach (j; 0..q)
		{
			int t;
			readf (" %s", &t);
			if (t == 1)
			{
				int x;
				readf (" %s", &x);
				x--;
				x = root (x);
				writeln (d[x]);
			}
			else if (t == 2)
			{
				int x, y;
				readf (" %s %s", &x, &y);
				x--;
				y--;
				unite (x, y);
			}
			else
			{
				assert (false);
			}
		}
	}
}

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

immutable int NA = -1;

void main ()
{
	int n, m, q;
	while (readf (" %s %s %s", &n, &m, &q) > 0)
	{
		auto a = new int [] [n];

		foreach (i; 0..m)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u--;
			v--;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto b = new int [n];

		auto d = new int [n];
		auto p = new int [n];
		foreach (k; 0..n)
		{
			if (b[k] > 0)
			{
				continue;
			}

			Tuple !(int, int) recur (int v, int c, int r)
			{
				if (b[v] >= c)
				{
					return Tuple !(int, int) (0, 0);
				}
				b[v] = c;
				p[v] = k;

				auto res = Tuple !(int, int) (r, v);
				foreach (u; a[v])
				{
					res = max (res, recur (u, c, r + 1));
				}
				return res;
			}

			int diameter (int v)
			{
				auto p = recur (v, 1, 0);
				auto q = recur (p[1], 2, 0);
				return q[0];
			}

			d[k] = diameter (k);
			debug {writeln (k, ' ', d[k]);}
		}

		int root (int v)
		{
			if (p[v] == v)
			{
				return v;
			}
			return (p[v] = root (p[v]));
		}

		void unite (int x, int y)
		{
			x = root (x);
			y = root (y);
			if (x == y)
			{
				return;
			}
			int rx = (d[x] + 1) >> 1;
			int ry = (d[y] + 1) >> 1;
			int dx = rx + max (d[x] - rx, 1 + ry);
			int dy = ry + max (d[y] - ry, 1 + rx);
			d[x] = max (dx, dy);
			debug {writeln (x, ' ', y, ' ', d[x]);}
			p[y] = x;
		}

		foreach (j; 0..q)
		{
			int t;
			readf (" %s", &t);
			if (t == 1)
			{
				int x;
				readf (" %s", &x);
				x--;
				x = root (x);
				writeln (d[x]);
			}
			else if (t == 2)
			{
				int x, y;
				readf (" %s %s", &x, &y);
				x--;
				y--;
				unite (x, y);
			}
			else
			{
				assert (false);
			}
		}
	}
}

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

immutable int NA = -1;

void main ()
{
	int n, m, q;
	while (readf (" %s %s %s", &n, &m, &q) > 0)
	{
		auto a = new int [] [n];

		foreach (i; 0..m)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u--;
			v--;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto b = new int [n];

		auto d = new int [n];
		auto p = new int [n];
		foreach (k; 0..n)
		{
			if (b[k] > 0)
			{
				continue;
			}

			Tuple !(int, int) recur (int v, int c, int r)
			{
				if (b[v] >= c)
				{
					return Tuple !(int, int) (0, 0);
				}
				b[v] = c;
				p[v] = k;

				auto res = Tuple !(int, int) (r, v);
				foreach (u; a[v])
				{
					res = max (res, recur (u, c, r + 1));
				}
				return res;
			}

			int diameter (int v)
			{
				auto p = recur (v, 1, 0);
				auto q = recur (p[1], 2, 0);
				return q[0];
			}

			d[k] = diameter (k);
			debug {writeln (k, ' ', d[k]);}
		}

		int root (int v)
		{
			if (p[v] == v)
			{
				return v;
			}
			return (p[v] = root (p[v]));
		}

		void unite (int x, int y)
		{
			x = root (x);
			y = root (y);
			if (x == y)
			{
				return;
			}
			int rx = (d[x] + 1) >> 1;
			int ry = (d[y] + 1) >> 1;
			int dx = rx + max (d[x] - rx, 1 + ry);
			int dy = ry + max (d[y] - ry, 1 + rx);
			d[x] = min (dx, dy);
			debug {writeln (x, ' ', y, ' ', d[x]);}
			p[y] = x;
		}

		foreach (j; 0..q)
		{
			int t;
			readf (" %s", &t);
			if (t == 1)
			{
				int x;
				readf (" %s", &x);
				x--;
				x = root (x);
				writeln (d[x]);
			}
			else if (t == 2)
			{
				int x, y;
				readf (" %s %s", &x, &y);
				x--;
				y--;
				unite (x, y);
			}
			else
			{
				assert (false);
			}
		}
	}
}


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

long ask (int lo, int hi)
{
	writeln ("? ", lo, " ", hi);
	stdout.flush ();
	return readln.strip.to !(long);
}

void solve (int n)
{
	auto total = ask (1, n);

	int lo = 1;
	int hi = n;
	while (lo < hi)
	{
		int me = (lo + hi) / 2;
		auto cur = ask (1, me);
		if (cur < total)
		{
			lo = me + 1;
		}
		else
		{
			hi = me;
		}
	}

	int k = hi;
	auto all1 = ask (1, k);
	auto cut1 = ask (1, k - 1);
	auto delta1 = cast (int) (all1 - cut1);

	int j = k - delta1;
	auto all2 = ask (1, j - 1);
	auto cut2 = ask (1, j - 2);
	auto delta2 = cast (int) (all2 - cut2);

	int i = j - 1 - delta2;
	writeln ("! ", i, " ", j, " ", k);
	stdout.flush ();
}

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

immutable multi = true;

void solve(int tc)
{
	import std.functional;
	import core.stdc.stdlib;
	auto n = readInt!long;
	long[long] cache;
	long getInvs(long k)
	{
		if (k in cache) return cache[k];
		writeln("? ", 1, " ", k);
		stdout.flush;
		auto ans = readInt!long;
		if (ans == -1) exit(0);
		return cache[k] = ans;
	}
	long total = getInvs(n);
	long k = -1;
	long low = 1;
	long high = n;
	while (low <= high)
	{
		long mid = (low + high) / 2;
		if (getInvs(mid) == total)
		{
			k = mid;
			high = mid - 1;
		}
		else
		{
			low = mid + 1;
		}
	}
	assert (k != -1);
	long diffK = getInvs(k) - getInvs(k-1);
	long j = k - diffK;
	auto diffJ = getInvs(j-1) - getInvs(j-2);
	long i = j - 1 - diffJ;
	writeln("! ", i, " ", j, " ", k);
	stdout.flush;
}

//{{{
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 core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

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

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

    if (n == 1) {
        writeln(arr.front);
        return;
    }

    auto s = arr.map!abs.sum(0L);

    debug { s.writeln; }

    int[] mnle;
    mnle ~= arr.front;
    foreach (e; arr.dropOne) mnle ~= min(mnle.back, e);
    int[] mnr;
    mnr ~= arr.back;
    foreach (e; arr.retro.dropOne) mnr ~= min(mnr.back, e);
    reverse(mnr);

    debug { mnle.writeln; mnr.writeln; }

    auto ans = arr.front.to!long - mnr[1] + (s - abs(arr.front) - abs(mnr[1]));
    ans = max(ans, arr.back.to!long - mnle[$-1] + (s - abs(arr.back) - abs(mnle[$-1])));

    foreach (i, e; arr.dropOne.dropBackOne.enumerate(1)) {
        auto cur = e.to!long - mnle[i-1] - mnr[i+1] + (s - abs(e) - abs(mnle[i-1]) - abs(mnr[i+1]));
        ans = max(ans, cur);
    }

    ans.writeln;
}

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

long saiki(int idx, int s, int n, int[] a, long[][] dp)
{
    if (dp[idx][s] != -1)
    {
        return dp[idx][s];
    }
    if (idx == n)
    {
        dp[idx][s] = (s == 3) ? 0 : long.min;
        return dp[idx][s];
    }
    auto ret0 = saiki(idx + 1, s | 1, n, a, dp);
    auto ret1 = saiki(idx + 1, s | 2, n, a, dp);
    auto res = long.min;
    if (ret0 != long.min) res = max(res, ret0 + a[idx]);
    if (ret1 != long.min) res = max(res, ret1 - a[idx]);
    dp[idx][s] = res;
    return res;
}

void solve(int[] a, int n)
{
    if (n == 1)
    {
        writeln(a[0]);
        return;
    }
    auto dp = new long[][](n + 1, 4);
    foreach (i; 0 .. n + 1)
    {
        fill(dp[i], -1);
    }
    auto res = saiki(0, 0, n, a, dp);
    writeln(res);
}

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

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

immutable long MOD = 10^^9 + 7;
immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;

    if (N == 1) {
        writeln(A[0]);
        return;
    }

    long S = A.map!(a => abs(a)).sum;
    long ans = -INF;

    foreach (i; 0..N-1) {
        auto sub = abs(A[i] - A[i+1]);
        ans = max(ans, S - abs(A[i]) - abs(A[i+1]) + sub);
    }

    ans.writeln;
}

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

immutable long MOD = 10^^9 + 7;
immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;

    if (N == 1) {
        writeln(A[0]);
        return;
    }

    auto S = A.sum;
    long ans = -INF;

    foreach (i; 0..N-1) {
        auto sub = abs(A[i] - A[i+1]);
        ans = max(ans, S - A[i] - A[i+1] + sub);
    }

    ans.writeln;
}

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

immutable long MOD = 10^^9 + 7;
immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;

    auto dp = new long[][](N+1, 2);
    foreach (i; 0..N+1) dp[i][0] = -INF;
    foreach (i; 0..N+1) dp[i][1] =  INF;
    dp[1][0] = dp[1][1] = A[0];

    foreach (i; 1..N) {
        dp[i+1][0] = max(dp[i+1][0], dp[i][0] - A[i]);
        dp[i+1][0] = max(dp[i+1][0], A[i] - dp[i][0]);
        dp[i+1][0] = max(dp[i+1][0], dp[i][1] - A[i]);
        dp[i+1][0] = max(dp[i+1][0], A[i] - dp[i][1]);
        dp[i+1][1] = min(dp[i+1][1], dp[i][0] - A[i]);
        dp[i+1][1] = min(dp[i+1][1], A[i] - dp[i][0]);
        dp[i+1][1] = min(dp[i+1][1], dp[i][1] - A[i]);
        dp[i+1][1] = min(dp[i+1][1], A[i] - dp[i][1]);
    }

    dp[N][0].writeln;
}


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()
{
    immutable int MD = 10 ^^ 9 + 7;

    auto s = readln.chomp;

    s ~= 'b';
    int ans = 0;
    int cur = 0;
    foreach (c; s) {
        if (c == 'a') ++cur;
        else if (c == 'b') {
            auto toadd = (cast(long)cur * ans % MD + cur) % MD;
            ans = (ans + toadd) % MD;
            cur = 0;
        }
    }

    ans.writeln;
}

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

int solve(string s)
{
    static int mod = 1000000007;
    auto n = to!int(s.length);
    auto dp = new int[n];
    auto next = new int[][](n, 2);
    next[n - 1][0] = next[n - 1][1] = n;
    if (s[n - 1] == 'a') dp[n - 1] = 1;
    foreach_reverse (i; 0 .. n - 1)
    {
        foreach (j; 0 .. 2) next[i][j] = next[i + 1][j];
        if (s[i + 1] == 'a') next[i][0] = i + 1;
        else if (s[i + 1] == 'b') next[i][1] = i + 1;
        if (s[i] == 'a')
        {
            dp[i] = 1;
            auto idx = next[i][1];
            if (idx < n)
            {
                idx = next[idx][0];
                if (idx < n) dp[i] = (dp[i] + dp[idx]) % mod;
            }
            idx = next[i][0];
            if (idx < n) dp[i] = (dp[i] + dp[idx]) % mod;
        }
    }
    if (s[0] == 'a') return dp[0];
    auto idx = next[0][0];
    if (idx < n) return dp[idx];
    return 0;
}

int main(string[] args)
{
    auto s = readln.strip;
    auto ret = solve(s);
    writeln(ret);
    return 0;
}

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()
{
    immutable int MD = 10 ^^ 9 + 7;

    auto s = readln.chomp;

    s ~= 'b';
    int ans = 0;
    int cur = 0;
    foreach (c; s) {
        if (c == 'a') ++cur;
        else if (c == 'b') {
            auto toadd = (cur * ans % MD + cur) % MD;
            ans = (ans + toadd) % MD;
            cur = 0;
        }
    }

    ans.writeln;
}


module cf_245A;

import std.stdio;

void main() {
    int n, t, x, y;
    int lostPCA = 0, acceptPCA = 0;
    int lostPCB = 0, acceptPCB = 0;

    readf("%d", &n);
    for (int i = 0; i < n; ++i) {
        readf(" %d %d %d", &t, &x, &y);

        if (t == 1) {
            acceptPCA += x;
            lostPCA += y;
        } else {
            acceptPCB += x;
            lostPCB += y;
        }
    }

    lostPCA += acceptPCA;
    lostPCB += acceptPCB;

    writeln((acceptPCA >= lostPCA / 2)? "LIVE": "DEAD");
    writeln((acceptPCB >= lostPCB / 2)? "LIVE": "DEAD");
}

import std.stdio;

int main() {
    debug stdin = File("in.txt", "r");

    int[2][3] table;
    int n;
    readf(" %s\n", &n);
    foreach(i; 0 .. n) {
        int a, b, c;
        readf(" %s %s %s\n", &a, &b, &c);
        table[a][0] += b;
        table[a][1] += c;
    }
    if (table[1][0] >= table[1][1]) writeln("LIVE");
    else writeln("DEAD");
    if (table[2][0] >= table[2][1]) writeln("LIVE");
    else writeln("DEAD");

    return 0;
}

import std.stdio;
int main()
{
	int n=0;
	readf(" %d", &n);
	int l1=0;
	int l2=0;
	int d1=0;
	int d2=0;
	for (int i=0; i<n; i++)
	{
		int a=0;
		int b=0;
		int c=0;
		readf(" %d", &a);
		readf(" %d", &b);
		readf(" %d", &c);
		if (a==1)
		{
			l1=l1+b;
			d1=d1+c;
		}
		else
		{
			l2=l2+b;
			d2=d2+c;
		}
	}
	if (l1>=d1)
	{
		writeln("LIVE");
	}
	else
	{
		writeln("DEAD");
	}
	if (l2>=d2)
	{
		writeln("LIVE");
	}
	else
	{
		writeln("DEAD");
	}
	return 0;
}


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

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

    auto g = new int[][] (n+1);

    foreach (i, e; p.enumerate(2)) {
        g[i] ~= e;
        g[e] ~= i;
    }

    debug { g.each!writeln; }

    immutable int MX_LVL = log2(n).to!int + 1;
    auto lca = new int[][] (MX_LVL, n+1);
    auto h = new int[] (n+1);

    void calcLcaArr (int n) {
        void dfs (int v, int fr) {
            if (fr != 0) {
                lca[0][v] = fr;
                h[v] = h[fr] + 1;
            }
            foreach (u; g[v]) {
                if (u == fr) { continue; }

                dfs(u, v);
            }
        }

        foreach (lvl; 0 .. MX_LVL) { lca[lvl][] = 0; }
        lca[0][1] = 1;
        h[1] = 0;

        dfs(1, 0);

        foreach (lvl; 1 .. MX_LVL) {
            foreach (v; 1 .. n+1) {
                lca[lvl][v] = lca[lvl-1][lca[lvl-1][v]];
            }
        }
    }

    calcLcaArr(n);

    int getLca (int a, int b) {
        if (h[a] < h[b]) { swap(a, b); }

        foreach_reverse (lvl; 0 .. MX_LVL) {
            if (h[a] - (1 << lvl) < h[b]) { continue; }

            a = lca[lvl][a];
        }

        if (a == b) {
            return a;
        }

        foreach_reverse (lvl; 0 .. MX_LVL) {
            if (lca[lvl][a] != lca[lvl][b]) {
                a = lca[lvl][a];
                b = lca[lvl][b];
            }
        }

        return lca[0][a];
    }

    debug {
        h.writeln;
        lca.each!writeln;

        writeln([getLca (1, 1), getLca(1, 2), getLca(2, 1), getLca(2, 2), getLca(2, 3)]
                .map!(to!string).join(" "));

    }

    int dst(int a, int b) {
        int vlca = getLca(a, b);
        return h[a] - h[vlca] + h[b] - h[vlca] + 1;
    }

    while (q--) {
        auto arr = readln.chomp.split.map!(to!int).array;

        int ans = 0;
        foreach (t; 0 .. 3) {
            int[] starts;
            foreach (i; 0 .. 3) { if (i != t) { starts ~= arr[i]; } }

            int cur = (dst(starts[0], arr[t]) + dst(starts[1], arr[t]) - dst(starts[0], starts[1])) / 2 + 1;

            ans = max(ans, cur);
        }

        ans.writeln;
    }
}

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

void main() {
    auto S = readln.split.map!(to!int);
    auto N = S[0];
    auto Q = S[1];
    auto edges = new int[][](N);
    auto P = readln.split.map!(to!int).array;
    foreach (i; 0..N-1) {
        edges[i + 1] ~= P[i] - 1;
        edges[P[i] - 1] ~= i + 1;
    }

    auto depth = new int[](N);
    auto dp = new int[][](20, N);

    void dfs(int n, int p, int d) {
        dp[0][n] = p;
        depth[n] = d;
        foreach (m; edges[n]) if (m != p) dfs(m, n, d+1);
    }

    dfs(0, -1, 0);

    foreach (k; 0..19)
        foreach (n; 0..N)
            dp[k+1][n] = (dp[k][n] == -1) ? -1 : dp[k][dp[k][n]];

    int[int][int] mem;

    int lca(int a, int b) {
        if (depth[a] < depth[b]) swap(a, b);
        if (a in mem && b in mem[a]) return mem[a][b];

        auto orig_a = a;
        auto orig_b = b;

        while (depth[a] > depth[b]) {
            int K = 19;
            foreach (k; iota(K, -1, -1)) {
                if ((1L << k) <= depth[a] - depth[b]) {
                    a = dp[k][a];
                    K = k;
                }
            }
        }

        if (a == b) return mem[a][b] = a;

        while (dp[0][a] != dp[0][b]) {
            int K = 19;
            foreach (k; iota(K, -1, -1)) {
                if (dp[k][a] != dp[k][b]) {
                    a = dp[k][a];
                    b = dp[k][b];
                    K = k;
                }
            }
        }

        return mem[a][b] = dp[0][a];
    }

    int dist(int a, int b, int ab) {
        return depth[a] + depth[b] - depth[ab] * 2;
    }

    foreach(_; 0..Q) {
        auto q = readln.split.map!(to!int).array;
        q.sort();
        int ans = 0;
        do {
            int s = q[0] - 1, t = q[1] - 1, f = q[2] - 1;
            int st = lca(s, t);
            int tf = lca(t, f);
            int fs = lca(f, s);
            int sft;
            if (depth[st] >= depth[tf] && depth[st] >= depth[fs]) sft = st;
            else if (depth[tf] >= depth[st] && depth[tf] >= depth[fs]) sft = tf;
            else sft = fs;
            ans = max(ans, dist(f, sft, lca(f, sft)) + 1);
        } while (nextPermutation(q));
        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.typecons;

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

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

    auto g = new int[][] (n+1);

    foreach (i, e; p.enumerate(2)) {
        g[i] ~= e;
        g[e] ~= i;
    }

    debug { g.each!writeln; }

    immutable int MX_LVL = log2(n).to!int + 1;
    auto lca = new int[][] (MX_LVL, n+1);
    auto h = new int[] (n+1);

    void calcLcaArr (int n) {
        void dfs (int v, int fr) {
            if (fr != 0) {
                lca[0][v] = fr;
                h[v] = h[fr] + 1;
            }
            foreach (u; g[v]) {
                if (u == fr) { continue; }

                dfs(u, v);
            }
        }

        foreach (lvl; 0 .. MX_LVL) { lca[lvl][] = 0; }
        lca[0][1] = 1;
        h[1] = 0;

        dfs(1, 0);

        foreach (lvl; 1 .. MX_LVL) {
            foreach (v; 1 .. n+1) {
                lca[lvl][v] = lca[lvl-1][lca[lvl-1][v]];
            }
        }
    }

    calcLcaArr(n);

    int getLca (int a, int b) {
        if (h[a] < h[b]) { swap(a, b); }

        foreach_reverse (lvl; 0 .. MX_LVL) {
            if (h[a] - (1 << lvl) < h[b]) { continue; }

            a = lca[lvl][a];
        }

        if (a == b) {
            return a;
        }

        foreach_reverse (lvl; 0 .. MX_LVL) {
            if (lca[lvl][a] != lca[lvl][b]) {
                a = lca[lvl][a];
                b = lca[lvl][b];
            }
        }

        return lca[0][a];
    }

    debug {
        h.writeln;
        lca.each!writeln;

        writeln([getLca (1, 1), getLca(1, 2), getLca(2, 1), getLca(2, 2), getLca(2, 3)]
                .map!(to!string).join(" "));

    }

    while (q--) {
        auto arr = readln.chomp.split.map!(to!int).array;

        int ans = 0;
        foreach (t; 0 .. 3) {
            int[] starts;
            foreach (i; 0 .. 3) { if (i != t) { starts ~= arr[i]; } }

            int lca1 = getLca(starts[0], starts[1]);
            int lca2 = getLca(starts[0], arr[t]);
            int lca3 = getLca(starts[1], arr[t]);

            int cur = 0;
            if (h[lca1] >= h[lca2] && h[lca1] >= h[lca3]) {
                int all = getLca(lca1, arr[t]);
                cur = h[lca1] - h[all] + h[arr[t]] - h[all];
            } else {
                cur = h[arr[t]] - max(h[lca2], h[lca3]);
            }

            ans = max(ans, cur + 1);
        }

        ans.writeln;
    }
}

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

void main() {
    auto S = readln.split.map!(to!int);
    auto N = S[0];
    auto Q = S[1];
    auto edges = new int[][](N);
    auto P = readln.split.map!(to!int).array;
    foreach (i; 0..N-1) {
        edges[i + 1] ~= P[i] - 1;
        edges[P[i] - 1] ~= i + 1;
    }

    auto depth = new int[](N);
    auto dp = new int[][](20, N);

    void dfs(int n, int p, int d) {
        dp[0][n] = p;
        depth[n] = d;
        foreach (m; edges[n]) if (m != p) dfs(m, n, d+1);
    }

    dfs(0, -1, 0);

    foreach (k; 0..19)
        foreach (n; 0..N)
            dp[k+1][n] = (dp[k][n] == -1) ? -1 : dp[k][dp[k][n]];

    int[int][int] mem;

    int lca(int a, int b) {
        if (depth[a] < depth[b]) swap(a, b);
        if (a in mem && b in mem[a]) return mem[a][b];

        auto orig_a = a;
        auto orig_b = b;

        while (depth[a] > depth[b]) {
            int K = 19;
            foreach (k; iota(K, -1, -1)) {
                if ((1L << k) <= depth[a] - depth[b]) {
                    a = dp[k][a];
                    K = k;
                }
            }
        }

        if (a == b) return mem[a][b] = a;

        while (dp[0][a] != dp[0][b]) {
            int K = 19;
            foreach (k; iota(K, -1, -1)) {
                if (dp[k][a] != dp[k][b]) {
                    a = dp[k][a];
                    b = dp[k][b];
                    K = k;
                }
            }
        }

        return mem[a][b] = dp[0][a];
    }

    int dist(int a, int b, int ab) {
        return depth[a] + depth[b] - depth[ab] * 2;
    }

    foreach(_; 0..Q) {
        auto q = readln.split.map!(to!int).array;
        q.sort();
        int ans = 0;
        do {
            int s = q[0] - 1, t = q[1] - 1, f = q[2] - 1;
            int st = lca(s, t);
            int sf = lca(s, f);
            int tf = lca(t, f);
            int d1 = dist(s, t, st);
            int d2 = dist(s, f, sf);
            int d3 = dist(t, f, tf);
            int stf1 = d1 < d2 ? st : sf;
            int d4 = dist(stf1, f, lca(stf1, f));
            int stf2 = d1 < d3 ? st : tf;
            int d5 = dist(stf2, f, lca(stf2, f));
            int d = min(d4, d5) + 1;
            ans = max(ans, d);
        } while (nextPermutation(q));
        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.typecons;

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

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

    auto g = new int[][] (n+1);

    foreach (i, e; p.enumerate(2)) {
        g[i] ~= e;
        g[e] ~= i;
    }

    debug { g.each!writeln; }

    immutable int MX_LVL = log2(n).to!int + 1;
    auto lca = new int[][] (MX_LVL, n+1);
    auto h = new int[] (n+1);

    void calcLcaArr (int n) {
        void dfs (int v, int fr) {
            if (fr != 0) {
                lca[0][v] = fr;
                h[v] = h[fr] + 1;
            }
            foreach (u; g[v]) {
                if (u == fr) { continue; }

                dfs(u, v);
            }
        }

        foreach (lvl; 0 .. MX_LVL) { lca[lvl][] = 0; }
        lca[0][1] = 1;
        h[1] = 0;

        dfs(1, 0);

        foreach (lvl; 1 .. MX_LVL) {
            foreach (v; 1 .. n+1) {
                lca[lvl][v] = lca[lvl-1][lca[lvl-1][v]];
            }
        }
    }

    calcLcaArr(n);

    int getLca (int a, int b) {
        if (h[a] < h[b]) { swap(a, b); }

        foreach_reverse (lvl; 0 .. MX_LVL) {
            if (h[a] - (1 << lvl) < h[b]) { continue; }

            a = lca[lvl][a];
        }

        if (a == b) {
            return a;
        }

        foreach_reverse (lvl; 0 .. MX_LVL) {
            if (lca[lvl][a] != lca[lvl][b]) {
                a = lca[lvl][a];
                b = lca[lvl][b];
            }
        }

        return lca[0][a];
    }

    debug {
        h.writeln;
        lca.each!writeln;

        writeln([getLca (1, 1), getLca(1, 2), getLca(2, 1), getLca(2, 2), getLca(2, 3)]
                .map!(to!string).join(" "));

    }

    while (q--) {
        auto arr = readln.chomp.split.map!(to!int).array;

        int ans = 0;
        foreach (t; 0 .. 3) {
            int[] starts;
            foreach (i; 0 .. 3) { if (i != t) { starts ~= arr[i]; } }

            int lca1 = getLca(starts[0], starts[1]);
            int lca2 = getLca(starts[0], arr[t]);
            int lca3 = getLca(starts[1], arr[t]);

            int cur = 0;
            if (h[lca1] >= h[lca2] && h[lca1] >= h[lca3]) {
                int all = getLca(lca1, arr[t]);
                cur = h[lca1] - h[all] + h[t] - h[all];
            } else {
                cur = h[t] - max(h[lca2], h[lca3]);
            }

            debug { writeln(starts, ' ', lca1, ' ', lca2, ' ', lca3, ' ', cur); }

            ans = max(ans, cur + 1);
        }

        ans.writeln;
    }
}


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

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

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 solve() {
    int n = read!int;
    auto a = readarray!int;
    if (n == 8)
        writeln(6);
    else if (n == 7)
        writeln(3*6);
    else
        writeln(6 * (10 - n)*(9 - n) / 2);
}

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

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) {
    auto n = readln.strip.to!int;
    auto a = readln.splitter.map!(to!int).array;
    bool[int] banned;
    foreach (x ; a)
      banned[x] = true;
    int ans = 0;
    foreach (d1 ; 0 .. 10)
      foreach (d2 ; 0 .. 10)
        foreach (d3 ; 0 .. 10)
          foreach (d4 ; 0 .. 10) {
             int[int] digits;
             int[4] code = [d1, d2, d3, d4];
             bool good = true;
             foreach (x ; code) {
               if (x in banned)
                 good = false;
               digits[x]++;
             }
             if (digits.length != 2 || digits.values != [2, 2])
               good = false;
             if (good)
               ans++;
          }
    writeln(ans);
  }
}

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 = read!int;
    auto A = readarray!int;
    long ans = (10 - N) * (10 - N - 1) / 2 * 6;
    writeln(ans);
}


void main() { runSolver(); }

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

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto M = scan!int;
      auto S = scan!string(N);

      long ans = long.max;
      foreach(i; 0..N - 1) foreach(j; i+1..N) {
        long tans;
        foreach(k; 0..M) {
          tans += (S[i][k] - S[j][k]).abs;
        }

        ans = min(ans, tans);
      }

      return ans;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

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

int[] rtln(T)() { return to!(T[])(strip(readln).split(" ")); }

void main() {
    int t, n, m;
    readf("%d\n", t);
    while(t--) {
		char[][] a;
        readf("%d %d\n", n, m);
		while (n--)
			a ~= strip(readln).dup;
    int min;
    for (int p = 0; p < m; p++) {
      int tempTemp = (a[0][p] + '0') - (a[1][p] + '0');
      if (tempTemp < 0) tempTemp = -tempTemp;
        min += tempTemp;
    }
    if (min < 0)
      min = -min;
		for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a.length; j++) {
        if (j == i)
          continue;
        int temp = 0;
        for (int p = 0; p < m; p++) {
          int tempTemp = (a[i][p] + '0') - (a[j][p] + '0');
          if (tempTemp < 0)
            tempTemp = -tempTemp;
          temp += tempTemp;
        }
        if (temp < min)
          min = temp;
      }
    }
    writeln(min);
  }
}

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

alias dist = (s, t) => zip (s.byChar, t.byChar).map !(q{abs (a[0] - a[1])}).sum;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		string [] s;
		foreach (i; 0..n)
		{
			s ~= readln.strip;
		}
		int res = int.max;
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				res = min (res, dist (s[i], s[j]));
			}
		}
		writeln (res);
	}
}


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

auto separators (string s, char start) {
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
		if (cur != c) {
			res ~= i + 1;
			cur = c;
		}
	return res;
}

auto solveSingle (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front + q.front) {
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

auto solve (string s, string t) {
	auto res = solveSingle (s, t);
	auto p = separators (s, 'a') ~ 0, q = separators (t, 'b') ~ 0;
	reverse (p);
	reverse (q);
	auto balance = (p.length.to!int - q.length.to!int) / 2;
	foreach (i; 0..2) foreach (j; 0..2) {
		int u = abs (balance) + i;
		int v = j;
		if (balance < 0) swap (u, v);
		if (u < 0 || p.length <= u || v < 0 || q.length <= v) continue;
		auto s2 = t[0..q[v]] ~ s[p[u]..$], t2 = s[0..p[u]] ~ t[q[v]..$];
		int [2] [] temp = [[p[u], q[v]]];
		temp ~= solveSingle (s2, t2);
		if (res.length > temp.length) res = temp;
	}
	return res;
}

void main () {
	auto s = readln.strip, t = readln.strip;
	auto res1 = solve (s, t), res2 = solve (t, s);
	if (res1.length > res2.length) {
		swap (res1, res2);
		res1.each !((ref a) => swap (a[0], a[1]));
	}
	writeln (res1.length);
	foreach (line; res1) writefln ("%(%s %)", line);
}

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

auto separators (string s, char start) {
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
		if (cur != c) {
			res ~= i + 1;
			cur = c;
		}
	return res;
}

auto solveSingle (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front + q.front) {
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

auto solve (string s, string t) {
	auto res = solveSingle (s, t);
	auto p = separators (s, 'a') ~ 0, q = separators (t, 'b') ~ 0;
	reverse (p);
	reverse (q);
	auto balance = (p.length.to!int - q.length.to!int) / 2;
	foreach (i; 0..3) foreach (j; 0..2) {
		int u = abs (balance) + i;
		int v = j;
		if (balance < 0) swap (u, v);
		if (u < 0 || p.length <= u || v < 0 || q.length <= v) continue;
		auto s2 = t[0..q[v]] ~ s[p[u]..$], t2 = s[0..p[u]] ~ t[q[v]..$];
		int [2] [] temp = [[p[u], q[v]]];
		temp ~= solveSingle (s2, t2);
		if (res.length > temp.length) res = temp;
	}
	return res;
}

void main () {
	auto s = readln.strip, t = readln.strip;
	auto res1 = solve (s, t), res2 = solve (t, s);
	if (res1.length > res2.length) {
		swap (res1, res2);
		res1.each !((ref a) => swap (a[0], a[1]));
	}
	writeln (res1.length);
	foreach (line; res1) writefln ("%(%s %)", line);
}

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

alias Record = Tuple !(int, q{a}, int, q{b});

int [] separators (string s, char start)
{
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
	{
		if (cur != c)
		{
			res ~= i + 1;
			cur = c;
		}
	}
	debug {writeln (res);}
	return res;
}

Record [] solve (string s, string t)
{
	Record [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front > 0 || q.front > 0)
	{
		res ~= Record (p.front, q.front);
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

Record [] solveStrategyLayer (string s, string t)
{
	Record [] res = solve (s, t);
	auto p = separators (s, 'a').chain (0.only).retro.array;
	auto q = separators (t, 'b').chain (0.only).retro.array;
	int balance = p.length.to!int - q.length.to!int;
	balance /= 2;
	foreach (i; 0..4)
	{
		foreach (j; 0..2)
		{
			int u = abs (balance) - 2 + i;
			int v = j;
			if (balance < 0)
			{
				swap (u, v);
			}
			if (u < 0 || p.length <= u || v < 0 || q.length <= v)
			{
				continue;
			}
			auto s2 = t[0..q[v]] ~ s[p[u]..$];
			auto t2 = s[0..p[u]] ~ t[q[v]..$];
			Record [] temp = [Record (p[u], q[v])];
			temp ~= solve (s2, t2);
			if (res.length > temp.length)
			{
				res = temp;
			}
		}
	}
	return res;
}

void main ()
{
	string s, t;
	while ((s = readln.strip) != "")
	{
		t = readln.strip;
		auto res1 = solveStrategyLayer (s, t);
		auto res2 = solveStrategyLayer (t, s);
		if (res1.length > res2.length)
		{
			swap (res1, res2);
			res1.each !((ref a) => swap (a[0], a[1]));
		}
		writeln (res1.length);
		foreach (ref line; res1)
		{
			writefln ("%s %s", line.a, line.b);
		}
	}
}

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

auto separators (string s, char start) {
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
		if (cur != c) {
			res ~= i + 1;
			cur = c;
		}
	return res;
}

auto solveSingle (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front + q.front) {
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

auto solve (string s, string t) {
	auto res = solveSingle (s, t);
	auto p = separators (s, 'a') ~ 0, q = separators (t, 'b') ~ 0;
	reverse (p);
	reverse (q);
	auto balance = (p.length.to!int - q.length.to!int) / 2;
	foreach (i; 0..3) foreach (j; 0..1) {
		int u = abs (balance) + i;
		int v = j;
		if (balance < 0) swap (u, v);
		if (u < 0 || p.length <= u || v < 0 || q.length <= v) continue;
		auto s2 = t[0..q[v]] ~ s[p[u]..$], t2 = s[0..p[u]] ~ t[q[v]..$];
		int [2] [] temp = [[p[u], q[v]]];
		temp ~= solveSingle (s2, t2);
		if (res.length > temp.length) res = temp;
	}
	return res;
}

void main () {
	auto s = readln.strip, t = readln.strip;
	auto res1 = solve (s, t), res2 = solve (t, s);
	if (res1.length > res2.length) {
		swap (res1, res2);
		res1.each !((ref a) => swap (a[0], a[1]));
	}
	writeln (res1.length);
	foreach (line; res1) writefln ("%(%s %)", line);
}

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

auto separators (string s, char start) {
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
		if (cur != c) {
			res ~= i + 1;
			cur = c;
		}
	return res;
}

auto solveSingle (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front + q.front) {
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

auto solve (string s, string t) {
	auto res = solveSingle (s, t);
	auto p = separators (s, 'a') ~ 0, q = separators (t, 'b') ~ 0;
	reverse (p);
	reverse (q);
	auto balance = (p.length.to!int - q.length.to!int) / 2;
	foreach (i; 0..2) foreach (j; 0..1) {
		int u = abs (balance) - 2 + i;
		int v = j;
		if (balance < 0) swap (u, v);
		if (u < 0 || p.length <= u || v < 0 || q.length <= v) continue;
		auto s2 = t[0..q[v]] ~ s[p[u]..$], t2 = s[0..p[u]] ~ t[q[v]..$];
		int [2] [] temp = [[p[u], q[v]]];
		temp ~= solveSingle (s2, t2);
		if (res.length > temp.length) res = temp;
	}
	return res;
}

void main () {
	auto s = readln.strip, t = readln.strip;
	auto res1 = solve (s, t), res2 = solve (t, s);
	if (res1.length > res2.length) {
		swap (res1, res2);
		res1.each !((ref a) => swap (a[0], a[1]));
	}
	writeln (res1.length);
	foreach (line; res1) writefln ("%(%s %)", line);
}

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

auto separators (string s, char start) {
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
		if (cur != c) {
			res ~= i + 1;
			cur = c;
		}
	return res;
}

auto solveSingle (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front + q.front) {
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

auto solve (string s, string t) {
	auto res = solveSingle (s, t);
	auto p = separators (s, 'a') ~ 0, q = separators (t, 'b') ~ 0;
	reverse (p);
	reverse (q);
	auto balance = (p.length.to!int - q.length.to!int) / 2;
	foreach (i; 0..2) foreach (j; 0..1) {
		int u = abs (balance) + i;
		int v = j;
		if (balance < 0) swap (u, v);
		if (u < 0 || p.length <= u || v < 0 || q.length <= v) continue;
		auto s2 = t[0..q[v]] ~ s[p[u]..$], t2 = s[0..p[u]] ~ t[q[v]..$];
		int [2] [] temp = [[p[u], q[v]]];
		temp ~= solveSingle (s2, t2);
		if (res.length > temp.length) res = temp;
	}
	return res;
}

void main () {
	auto s = readln.strip, t = readln.strip;
	auto res1 = solve (s, t), res2 = solve (t, s);
	if (res1.length > res2.length) {
		swap (res1, res2);
		res1.each !((ref a) => swap (a[0], a[1]));
	}
	writeln (res1.length);
	foreach (line; res1) writefln ("%(%s %)", line);
}

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

int [] separators (string s, char start)
{
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
	{
		if (cur != c)
		{
			res ~= i + 1;
			cur = c;
		}
	}
	debug {writeln (res);}
	return res;
}

int [2] [] solve (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front > 0 || q.front > 0)
	{
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

void main ()
{
	string s, t;
	while ((s = readln.strip) != "")
	{
		t = readln.strip;
		auto res1 = solve (s, t);
		auto res2 = solve (t, s);
		if (res1.length > res2.length)
		{
			swap (res1, res2);
			res1.each !((ref a) => swap (a[0], a[1]));
		}
		writeln (res1.length);
		foreach (ref line; res1)
		{
			writefln ("%(%s %)", line[]);
		}
	}
}

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

alias Record = Tuple !(int, q{a}, int, q{b});

int [] separators (string s, char start)
{
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
	{
		if (cur != c)
		{
			res ~= i + 1;
			cur = c;
		}
	}
	debug {writeln (res);}
	return res;
}

Record [] solve (string s, string t)
{
	Record [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front > 0 || q.front > 0)
	{
		res ~= Record (p.front, q.front);
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

Record [] solveStrategyLayer (string s, string t)
{
	Record [] res = solve (s, t);
	auto p = separators (s, 'a').chain (0.only).retro.array;
	auto q = separators (t, 'b').chain (0.only).retro.array;
	int balance = p.length.to!int - q.length.to!int;
	foreach (i; 0..4)
	{
		foreach (j; 0..3)
		{
			int u = abs (balance) - 2 + i;
			int v = j;
			if (balance < 0)
			{
				swap (u, v);
			}
			if (u < 0 || p.length <= u || v < 0 || q.length <= v)
			{
				continue;
			}
			auto s2 = t[0..q[v]] ~ s[p[u]..$];
			auto t2 = s[0..p[u]] ~ t[q[v]..$];
			Record [] temp = [Record (p[u], q[v])];
			temp ~= solve (s2, t2);
			if (res.length > temp.length)
			{
				res = temp;
			}
		}
	}
	return res;
}

void main ()
{
	string s, t;
	while ((s = readln.strip) != "")
	{
		t = readln.strip;
		auto res1 = solveStrategyLayer (s, t);
		auto res2 = solveStrategyLayer (t, s);
		if (res1.length > res2.length)
		{
			swap (res1, res2);
			res1.each !((ref a) => swap (a[0], a[1]));
		}
		writeln (res1.length);
		foreach (ref line; res1)
		{
			writefln ("%s %s", line.a, line.b);
		}
	}
}

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

int [] separators (string s, char start)
{
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
	{
		if (cur != c)
		{
			res ~= i + 1;
			cur = c;
		}
	}
	debug {writeln (res);}
	return res;
}

int [2] [] solve (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front > 0 || q.front > 0)
	{
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

int [2] [] solveStrategyLayer (string s, string t)
{
	int [2] [] res = solve (s, t);
	auto p = separators (s, 'a').chain (0.only).retro.array;
	auto q = separators (t, 'b').chain (0.only).retro.array;
	int balance = p.length.to!int - q.length.to!int;
	foreach (i; 0..4)
	{
		foreach (j; 0..2)
		{
			int u = abs (balance) - 1 + i;
			int v = j;
			if (balance < 0)
			{
				swap (u, v);
			}
			if (u < 0 || p.length <= u || v < 0 || q.length <= v)
			{
				continue;
			}
			auto s2 = t[0..q[v]] ~ s[p[u]..$];
			auto t2 = s[0..p[u]] ~ t[q[v]..$];
			int [2] [] temp = [[p[u], q[v]]];
			temp ~= solve (s2, t2);
			if (res.length > temp.length)
			{
				res = temp;
			}
		}
	}
	return res;
}

void main ()
{
	string s, t;
	while ((s = readln.strip) != "")
	{
		t = readln.strip;
		auto res1 = solveStrategyLayer (s, t);
		auto res2 = solveStrategyLayer (t, s);
		if (res1.length > res2.length)
		{
			swap (res1, res2);
			res1.each !((ref a) => swap (a[0], a[1]));
		}
		writeln (res1.length);
		foreach (ref line; res1)
		{
			writefln ("%(%s %)", line[]);
		}
	}
}

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

auto separators (string s, char start) {
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
		if (cur != c) {
			res ~= i + 1;
			cur = c;
		}
	return res;
}

auto solveSingle (string s, string t)
{
	int [2] [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front + q.front) {
		res ~= [p.front, q.front];
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

auto solve (string s, string t) {
	auto res = solveSingle (s, t);
	auto p = separators (s, 'a') ~ 0, q = separators (t, 'b') ~ 0;
	reverse (p);
	reverse (q);
	auto balance = (p.length.to!int - q.length.to!int) / 2;
	foreach (i; 0..2) foreach (j; 0..2) {
		int u = abs (balance) - 1 + i;
		int v = j;
		if (balance < 0) swap (u, v);
		if (u < 0 || p.length <= u || v < 0 || q.length <= v) continue;
		auto s2 = t[0..q[v]] ~ s[p[u]..$], t2 = s[0..p[u]] ~ t[q[v]..$];
		int [2] [] temp = [[p[u], q[v]]];
		temp ~= solveSingle (s2, t2);
		if (res.length > temp.length) res = temp;
	}
	return res;
}

void main () {
	auto s = readln.strip, t = readln.strip;
	auto res1 = solve (s, t), res2 = solve (t, s);
	if (res1.length > res2.length) {
		swap (res1, res2);
		res1.each !((ref a) => swap (a[0], a[1]));
	}
	writeln (res1.length);
	foreach (line; res1) writefln ("%(%s %)", line);
}

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

alias Record = Tuple !(int, q{a}, int, q{b});

int [] separators (string s, char start)
{
	int [] res;
	char cur = start;
	foreach_reverse (int i, char c; s)
	{
		if (cur != c)
		{
			res ~= i + 1;
			cur = c;
		}
	}
	debug {writeln (res);}
	return res;
}

Record [] solve (string s, string t)
{
	Record [] res;
	auto p = separators (s, 'a').chain (0.only.cycle);
	auto q = separators (t, 'b').chain (0.only.cycle);
	while (p.front > 0 || q.front > 0)
	{
		res ~= Record (p.front, q.front);
		p.popFront ();
		q.popFront ();
		swap (p, q);
	}
	return res;
}

Record [] solveStrategyLayer (string s, string t)
{
	Record [] res = solve (s, t);
	auto p = separators (s, 'a').chain (0.only).retro.array;
	auto q = separators (t, 'b').chain (0.only).retro.array;
	int balance = p.length.to!int - q.length.to!int;
	foreach (i; 0..6)
	{
		foreach (j; 0..2)
		{
			int u = abs (balance) - 2 + i;
			int v = j;
			if (balance < 0)
			{
				swap (u, v);
			}
			if (u < 0 || p.length <= u || v < 0 || q.length <= v)
			{
				continue;
			}
			auto s2 = t[0..q[v]] ~ s[p[u]..$];
			auto t2 = s[0..p[u]] ~ t[q[v]..$];
			Record [] temp = [Record (p[u], q[v])];
			temp ~= solve (s2, t2);
			if (res.length > temp.length)
			{
				res = temp;
			}
		}
	}
	return res;
}

void main ()
{
	string s, t;
	while ((s = readln.strip) != "")
	{
		t = readln.strip;
		auto res1 = solveStrategyLayer (s, t);
		auto res2 = solveStrategyLayer (t, s);
		if (res1.length > res2.length)
		{
			swap (res1, res2);
			res1.each !((ref a) => swap (a[0], a[1]));
		}
		writeln (res1.length);
		foreach (ref line; res1)
		{
			writefln ("%s %s", line.a, line.b);
		}
	}
}


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

void main ()
{
	foreach (t; 0..readln.strip.to !(int))
	{
		int n, m;
		readf !(" %s %s") (n, m);
		auto adj = new bool [int] [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			adj[v][u] = true;
		}

		auto z = new bool [n];
		foreach (u; 0..n)
		{
			bool [int] temp;
			foreach (v, _; adj[u])
			{
				if (!z[v])
				{
					temp[v] = true;
				}
			}
			adj[u] = temp;
			foreach (v, _; adj[u])
			{
				foreach (w, _; adj[v])
				{
					if (!z[w])
					{
						z[u] = true;
					}
				}
			}
		}

		writeln (z.sum);
		writefln !("%(%s %)")
		    (n.iota.filter !(u => z[u]).map !(q{a + 1}));
	}
}

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

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

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

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

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

        auto graph = new int[][N];
        foreach (i; 0 .. M) {
          graph[B[i]] ~= A[i];
        }

        auto col = new int[N];
        foreach (u; 0 .. N) {
          foreach (v; graph[u]) {
            if (col[v] < 2) {
              chmax(col[u], col[v] + 1);
            }
          }
        }

        const ans = iota(N).filter!(u => (col[u] == 2)).array;
        writeln(ans.length);
        foreach (index, u; ans) {
          if (index > 0) write(" ");
          write(u + 1);
        }
        writeln();
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	foreach (t; 0..readln.strip.to !(int))
	{
		int n, m;
		readf !(" %s %s") (n, m);
		auto adj = new int [] [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			adj[u] ~= v;
		}

		auto z = new bool [n];
		foreach_reverse (u; 0..n)
		{
			foreach (v; adj[u])
			{
				if (!z[v])
				{
					foreach (w; adj[v])
					{
						if (!z[w])
						{
							z[u] = true;
						}
					}
				}
			}
		}

		writeln (z.sum);
		writefln !("%(%s %)")
		    (n.iota.filter !(u => z[u]).map !(q{a + 1}));
	}
}

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

int N, M;
int[] A, B;

bool[] del;
int[][] graph;
int[][] dp;
int[][] prev;

void solve(int u) {
  auto sums = new int[3];
  foreach (v; graph[u]) {
    solve(v);
    sums[] += dp[v][];
  }
  foreach (k; 0 .. 3) {
    // delete
    dp[u][k] = 1 + sums[0];
    // keep
    if (k < 2) {
      if (chmin(dp[u][k], sums[k + 1])) {
        prev[u][k] = 1;
      }
    }
  }
}

void recover(int u, int k) {
  int kk;
  if (prev[u][k] == 0) {
    del[u] = true;
    kk = 0;
  } else {
    kk = k + 1;
  }
  foreach (v; graph[u]) {
    recover(v, kk);
  }
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        N = readInt();
        M = readInt();
        A = new int[M];
        B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        alias Entry = Tuple!(int, "deg", int, "u");
        auto es = new Entry[N];
        foreach (u; 0 .. N) {
          es[u].u = u;
        }
        auto graphIn = new int[][N];
        foreach (i; 0 .. M) {
          --es[A[i]].deg;
          --es[B[i]].deg;
          graphIn[B[i]] ~= A[i];
        }
        es.sort;

        del = new bool[N];
        foreach_reverse (ref e; es) {
          const u = e.u;
          int indeg;
          foreach (v; graphIn[u]) {
            if (!del[v]) {
              ++indeg;
            }
          }
          if (indeg >= 2) {
            del[u] = true;
          }
        }
        debug {
          writeln("del = ", del);
        }

        graph = new int[][N];
        auto isChild = new bool[N];
        foreach (i; 0 .. M) {
          if (!del[A[i]] && !del[B[i]]) {
            graph[A[i]] ~= B[i];
            isChild[B[i]] = true;
          }
        }
        debug {
          writeln("graph = ", graph);
        }

        dp = new int[][](N, 3);
        prev = new int[][](N, 3);
        foreach (u; 0 .. N) {
          if (!del[u]) {
            if (!isChild[u]) {
              solve(u);
              recover(u, 0);
            }
          }
        }
        debug {
          writeln("dp = ", dp);
          writeln("del = ", del);
        }

        int[] ans;
        foreach (u; 0 .. N) {
          if (del[u]) {
            ans ~= u;
          }
        }

if(ans.length>N*7/4)for(;;){}
        writeln(ans.length);
        foreach (index, u; ans) {
          if (index > 0) write(" ");
          write(u + 1);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

int N, M;
int[] A, B;

bool[] del;
int[][] graph;
int[][] dp;
int[][] prev;

void solve(int u) {
  auto sums = new int[3];
  foreach (v; graph[u]) {
    solve(v);
    sums[] += dp[v][];
  }
  foreach (k; 0 .. 3) {
    // delete
    dp[u][k] = 1 + sums[0];
    // keep
    if (k < 2) {
      if (chmin(dp[u][k], sums[k + 1])) {
        prev[u][k] = 1;
      }
    }
  }
}

void recover(int u, int k) {
  int kk;
  if (prev[u][k] == 0) {
    del[u] = true;
    kk = 0;
  } else {
    kk = k + 1;
  }
  foreach (v; graph[u]) {
    recover(v, kk);
  }
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        N = readInt();
        M = readInt();
        A = new int[M];
        B = new int[M];
        foreach (i; 0 .. M) {
          A[i] = readInt() - 1;
          B[i] = readInt() - 1;
        }

        auto graphIn = new int[][N];
        foreach (i; 0 .. M) {
          graphIn[B[i]] ~= A[i];
        }

        del = new bool[N];
        foreach (u; 0 .. N) {
          int indeg;
          foreach (v; graphIn[u]) {
            if (!del[v]) {
              ++indeg;
            }
          }
          if (indeg >= 2) {
            del[u] = true;
          }
        }
        debug {
          writeln("del = ", del);
        }

        graph = new int[][N];
        auto isChild = new bool[N];
        foreach (i; 0 .. M) {
          if (!del[A[i]] && !del[B[i]]) {
            graph[A[i]] ~= B[i];
            isChild[B[i]] = true;
          }
        }
        debug {
          writeln("graph = ", graph);
        }

        dp = new int[][](N, 3);
        prev = new int[][](N, 3);
        foreach (u; 0 .. N) {
          if (!del[u]) {
            if (!isChild[u]) {
              solve(u);
              recover(u, 0);
            }
          }
        }
        debug {
          writeln("dp = ", dp);
          writeln("del = ", del);
        }

        int[] ans;
        foreach (u; 0 .. N) {
          if (del[u]) {
            ans ~= u;
          }
        }

        writeln(ans.length);
        foreach (index, u; ans) {
          if (index > 0) write(" ");
          write(u + 1);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}