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

string FMT_F = "%.10f";

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

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

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

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

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

		{
			int x = int.min;
			foreach (i; 0..n)
			{
				x.chmax(a[i]);
			}
			foreach (i; 0..n)
			{
				a[i] = x - a[i];
			}
		}
		if (k % 2 == 0)
		{
			int x = int.min;
			foreach (i; 0..n)
			{
				x.chmax(a[i]);
			}
			foreach (i; 0..n)
			{
				a[i] = x - a[i];
			}
		}
		ans[ti] = a;
	}

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

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;
		long k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		k -= 1;
		auto d = a.maxElement;
		a[] = d - a[];
		k &= 1;
		while (k > 0)
		{
			d = a.maxElement;
			a[] = d - a[];
			k -= 1;
		}
		writefln !("%(%s %)") (a);
	}
}

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

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

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

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

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

        auto ans = new long[N];
        if (K % 2 == 0) {
          const AMin = A.minElement;
          foreach (i; 0 .. N) {
            ans[i] = A[i] - AMin;
          }
        } else {
          const AMax = A.maxElement;
          foreach (i; 0 .. N) {
            ans[i] = AMax - A[i];
          }
        }

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

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

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

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

  void solve(long tc = -1)
  {
    if (k % 2 == 1)
      {
        auto m = a.fold!max;
        foreach(ai; a)
          {
            write(m - ai, " ");
          }
        writeln;
      }
    else
      {
        auto m = a.fold!min;
        foreach(ai; a)
          {
            write(ai - m, " ");
          }
        writeln;
      }
  }
}

struct Interval
{
  long l, h;

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

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

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

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

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

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

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

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

DList!string _words;

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

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

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

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

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

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

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

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

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

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

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

    }
}

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

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

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

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

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

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

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

void main()
{
  type;
}


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

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

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

    int s = a + b;
    int k = 0;
    while (((k+1).to!long * (k+2)) / 2 <= s) ++k;

    int[][2] nrs;
    int cur = k;
    while (cur > 0) {
        if (cur <= a) {
            nrs[0] ~= cur;
            a -= cur;
        } else {
            nrs[1] ~= cur;
            b -= cur;
        }
        --cur;
    }

    foreach (arr; nrs) {
        arr.length.writeln;
        arr.writefln!("%(%s %)");
    }
}

void main() {
    import std.range : dropExactly, retro, sequence, take;
    import std.stdio : readf, writefln;

    int a, b;
    readf!" %d %d"(a, b);

    int nn;
    int ns;
    foreach (n; sequence!"n".dropExactly(1)) {
        ns += n;
        if (ns > a + b)
            break;
        nn++;
    }

    int[] d1;
    d1.reserve(nn);
    int[] d2;
    d2.reserve(nn);

    foreach (n; sequence!"n".dropExactly(1).take(nn).retro) {
        if (n <= a) {
            a -= n;
            d1 ~= n;
        }
        else {
            d2 ~= n;
        }
    }
    writefln("%s\n%(%s %)\n%s\n%(%s %)", d1.length, d1, d2.length, d2);
}


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

const long MOD = 1_000_000_007;

// Modexp
ll modexp(ll base, ll exp){
    ll x = base % MOD;
    ll y = exp;
    ll res = 1;
    while(y > 0){
        if(y & 1){
            res *= x; res %= MOD;
        }
        x *= x; x %= MOD;
        y >>= 1;
    }
    return res;
}

void solve(){
    long n = scan;
    long m = scan;
    long res = 0;
    for(int i = 0; i < m; ++i){
        long l = scan;
        long r = scan;
        long x = scan;
        res |= x;
    }
    res *= modexp(2, n-1);
    res %= MOD;
    writeln(res);
}

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

/**************    *****    That's All Folks!    *****    **************/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm, std.container, std.math;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

immutable multi = true;

immutable long p = 1_000_000_000L + 7;
alias Zp = Z!p;
long[] p2;

static this()
{
	p2 = new long[](2_000_00 + 1);
	p2[0] = 1;
	foreach(i; 1 .. p2.length)
		p2[i] = (p2[i-1] * 2)%p;
}

void solve(int tc)
{
	auto n = readInt!int;
	auto m = readInt!int;
	struct Rel
	{
		int i, acc;
	}
	auto rel = new Rel[][](n + 1);
	auto cum = new int[](n + 1);
	foreach(i; 0 .. m)
	{
		auto l = readInt!int - 1;
		auto r = readInt!int;
		auto x = readInt!int;
		rel[r] ~= Rel(l, x);
		rel[l] ~= Rel(r, x);
	}
	auto filled = new bool[](n + 1);
	void dfs(int v, int fill)
	{
		if (filled[v])
		{
			assert(fill == cum[v]);
			return;
		}
		cum[v] = fill;
		filled[v] = true;
		foreach(r; rel[v])
		{
			auto w = r.i;
			auto x = r.acc;
			dfs(w, x ^ fill);
		}
	}
	foreach(i; 0 .. n + 1)
	{
		if (!filled[i])
			dfs(i, 0);
	}
	auto orig = new int[](n);
	int[30] cnt;
	foreach(i, ref oi; orig)
	{
		oi = cum[i+1] ^ cum[i];
		int b = 0;
		while (oi)
		{
			cnt[b] += int((oi & 1) != 0);
			b++;
			oi >>= 1;
		}
	}
	long ans = 0;
	foreach(i; 0 .. 30)
	{
		if (cnt[i] == 0) continue;
		long term = 1;
		term *= p2[i];
		term *= p2[n - 1];
		term %= p;
		ans += term;
		ans %= p;
	}
	ans.writeln;
}

// modular {{{
struct Z(immutable long m)
{
	long rep;
	static immutable bool primeModulus = isPrime(m);
	static Z!m[] fact;
	static if (primeModulus) { static Z!m[] invFact; }
	static makeFactorials(int n)
	{
		fact = new Z!m[](n + 1);
		fact[0] = Z!m(1);
		foreach(i; 1 .. n + 1) fact[i] = Z!m(i) * fact[i - 1];
		static if (primeModulus)
		{
			invFact = new Z!m[](n + 1);
			invFact[n] = fact[n].inv;
			foreach_reverse(i; 0 .. n) invFact[i] = Z!m(i + 1) * invFact[i + 1];
		}
	}

	this(long num)
	{
		rep = num;
	}

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

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

	static if (primeModulus)
	{
		Z!m inv()
		{
			return this^^(m - 2);
		}
		static Z!m C(int n, int k)
		{
			if (k < 0 || k > n) return Z!m(0);
			return fact[n] * invFact[k] * invFact[n - k];
		}
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}
bool isPrime(long n)
{
	for(long d = 2; d * d <= n; d++)
		if (n % d == 0) return false;
	return true;
}
unittest
{
	alias Zp = Z!23;
	static assert(Zp.primeModulus);
	foreach(i; 1 .. 23) assert(Zp(i) * Zp(i).inv == Zp(1));
	Zp.makeFactorials(22);
	foreach(i; 0 .. 23) assert(Zp.fact[i] * Zp.invFact[i] == Zp(1));
	assert(Zp.C(3, 2) == Zp(3));
	assert(Zp.C(4, 2) == Zp(6));
}
// }}}

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

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

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

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

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


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

const int sz = 100005;
bool[] isPrime;
ll[] primes;
void prime_sieve(){
    isPrime = new bool[](sz + 5);
    isPrime[] = 1;
    foreach(p; 2..10000){
        if(isPrime[p]){
            for(ll divi = p*p; divi <= sz; divi += p){
                isPrime[divi.to!int] = 0;
            }
        }
    }
    for(int i = 2; i <= sz; ++i){
        if(isPrime[i]) primes ~= i;
    }
}

void theCode(){
    ll d;
    d = scan;
    int lo = -1, hi = primes.length.to!int-1;
    while(hi - lo > 1){
        int mid = (hi + lo )>> 1;
        ((primes[mid] < d+1) ? lo : hi) = mid;
    }
    long p0 = primes[hi];
    lo = -1, hi = primes.length.to!int-1;
    while(hi - lo > 1){
        int mid = (hi + lo )>> 1;
        ((primes[mid] < d + p0) ? lo : hi) = mid;
    }
    long p1 = primes[hi];
    writeln(p0*p1);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

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

auto sieve (int limit)
{
	auto res = new bool [limit];
	res[2..$] = true;
	for (int d = 2; d * d < limit; d++)
	{
		if (res[d])
		{
			for (int e = d; e * d < limit; e++)
			{
				res[e * d] = false;
			}
		}
	}
	return res;
}

immutable int limit = 30_000;

void main ()
{
	auto s = sieve (limit);

	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto d = readln.strip.to !(int);
		auto x = 1;
		auto res = x;
		foreach (i; 0..2)
		{
			x += d;
			while (!s[x])
			{
				x += 1;
			}
			res *= x;
		}
		writeln (res);
	}
}

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

enum PCNT = 10^^6;

bool[PCNT+1] PS;

void prime_init()
{
    PS[] = true;
    PS[0] = false;
    PS[1] = false;
    foreach (i; 2..PCNT+1) {
        if (PS[i]) {
            auto x = i*2;
            while (x <= PCNT) {
                PS[x] = false;
                x += i;
            }
        }
    }
}

void main()
{
    prime_init();
    int[] ps;
    foreach (i; 1..PCNT + 1) if (PS[i]) ps ~= i;

    int T; get(T);
    while (T--) {
        int d; get(d);
        if (d == 1) {
            writeln(6);
            continue;
        }
        long search(long p) {
            int l, r = ps.length.to!int;
            while (l + 1 < r) {
                auto m = (l + r) / 2;
                if (ps[m] >= p + d) {
                    r = m;
                } else {
                    l = m;
                }
            }
            return ps[r];
        }
        auto p = search(1);
        auto q = search(p);
        writeln(p * q);
    }
}

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

const int sz = 100005;
bool[] isPrime;
void prime_sieve(){
    isPrime = new bool[](sz + 5);
    isPrime[] = 1;
    foreach(p; 2..10000){
        if(isPrime[p]){
            for(ll divi = p*p; divi <= sz; divi += p){
                isPrime[divi.to!int] = 0;
            }
        }
    }
}

void theCode(){
    ll n;
    n = scan;
    ll fp;
    for(ll d = n+1; d < 100000 ; ++d){
        if(isPrime[d.to!int]){
            fp = d;
            ll res = (2*fp - 1) * fp;
            writeln(res);
            return;
        }
    }
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

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

void theCode(){
    ll n;
    n = scan;
    ll res = (2*n + 1) * (n + 1);
    writeln(res);
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");


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

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

void main()
{
  int n, k; readV(n, k);
  string[] s; readC(n, s);

  auto a = new int[][](n, n);

  auto checkH(int r, int c)
  {
    foreach (i; 0..k)
      if (s[r][c+i] == '#') return false;
    return true;
  }

  auto addH(int r, int c)
  {
    foreach (i; 0..k) a[r][c+i] += 1;
  }

  auto checkV(int r, int c)
  {
    foreach (i; 0..k)
      if (s[r+i][c] == '#') return false;
    return true;
  }

  auto addV(int r, int c)
  {
    foreach (i; 0..k) a[r+i][c] += 1;
  }

  foreach (r; 0..n)
    foreach (c; 0..n) {
      if (c <= n-k && checkH(r, c)) addH(r, c);
      if (r <= n-k && checkV(r, c)) addV(r, c);
    }

  auto m = a.map!(ai => ai.maxElement).maxElement;
  foreach (r; 0..n)
    foreach (c; 0..n)
      if (a[r][c] == m) {
        writeln(r+1, " ", c+1);
        return;
      }
}

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

int[] f(string s, int k)
{
    int[] taken = [-1];

    foreach (i, c; s)
    {
        if (c == '#')
        {
            taken ~= cast(int) i;
        }
    }

    taken ~= cast(int) s.length;

    int[] result = new int[s.length];

    int curInTaken = 1;

    foreach (i, ref v; result)
    {
        while (taken[curInTaken] < i)
        {
            curInTaken++;
        }

        if (i == taken[curInTaken] || k >= taken[curInTaken] - taken[curInTaken - 1])
        {
            v = 0;
        }
        else
        {
            v = min(i - taken[curInTaken - 1], taken[curInTaken] - i, k, taken[curInTaken] - taken[curInTaken - 1] - k);
        }
    }

    return result;
}

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

    readln();

    string[] field;
    int[][] a;

    foreach (i; 0 .. n)
    {
        auto s = readln().strip();
        a ~= f(s, k);
        field ~= s;
    }

    foreach (i; 0 .. n)
    {
        string s;

        foreach (j; 0 .. n)
        {
            s ~= field[j][i];
        }

        auto r = f(s, k);

        foreach (j; 0 .. n)
        {
            a[j][i] += r[j];
        }
    }

    int x = 0, y = 0, mx = 0;

    foreach (i; 0 .. n)
    {
        foreach (j; 0 .. n)
        {
            if (a[i][j] > mx)
            {
                mx = a[i][j];
                x = j;
                y = i;
            }
        }
    }

    writeln(y+1, ' ', x+1);
}

import std.algorithm.iteration : map;
import std.algorithm.searching : maxElement;
import std.array;
import std.range : enumerate;
import std.stdio;

void main()
{
  int n, k;
  readf!"%d %d\n"(n, k);
  auto board = stdin.byLineCopy().array();
  int[][] count = new int[][](n, n);

  foreach (r; 0..n) {
    foreach (c; 0..n) {
      auto valid = true;

      // check horizontal
      foreach (i; 0..k) {
	if (c+i >= n || board[r][c+i] == '#') {
	  valid = false;
	  break;
	}
      }
      if (valid) {
	foreach (i; 0..k) {
	  count[r][c+i]++;
	}
      }

      valid = true;
      // check vertical
      foreach (j; 0..k) {
	if (r+j >= n || board[r+j][c] == '#') {
	  valid = false;
	  break;
	}
      }
      if (valid) {
	foreach (j; 0..k) {
	  count[r+j][c]++;
	}
      }
    }
  }

  auto countIdx = count.map!(a => a.enumerate.maxElement!"a.value");
  auto best = countIdx.enumerate.maxElement!"a.value.value";
  writef("%d %d\n", best.index + 1, best.value.index + 1);
}

import std.algorithm.iteration : map;
import std.algorithm.searching : maxElement;
import std.array;
import std.range : enumerate;
import std.stdio;

void main()
{
  int n, k;
  readf!"%d %d\n"(n, k);
  auto board = stdin.byLineCopy().array();
  int[][] count = new int[][](n, n);

  foreach (r; 0..n-k+1) {
    foreach (c; 0..n-k+1) {
      auto valid = true;

      // check horizontal
      foreach (i; 0..k) {
	if (board[r][c+i] == '#') {
	  valid = false;
	  break;
	}
      }
      if (valid) {
	foreach (i; 0..k) {
	  count[r][c+i]++;
	}
      }

      valid = true;
      // check vertical
      foreach (j; 0..k) {
	if (board[r+j][c] == '#') {
	  valid = false;
	  break;
	}
      }
      if (valid) {
	foreach (j; 0..k) {
	  count[r+j][c]++;
	}
      }
    }
  }

  auto countIdx = count.map!(a => a.enumerate.maxElement!"a.value");
  auto best = countIdx.enumerate.maxElement!"a.value.value";
  writef("%d %d\n", best.index + 1, best.value.index + 1);
}


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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const D = readInt();
      const E = readInt();

      int ans = N;
      for (int a = 0; D * a <= N; ++a) {
        const n = N - D * a;
        chmin(ans, n % (E * 5));
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

void main() {
    int n,d,e;
    readf!" %s %s %s"(n,d,e);

    int ans = n;

    for (int k = 0; k <= n / d; k++) {
        int rest = n - k * d;
        ans = min(ans, rest % (e*5));
    }

    writeln(ans);
}

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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

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

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

void main()
{
	auto n = RD!int;
	auto d = RD;
	auto e = RD*5;

	long ans = long.max;
	while (n >= e)
	{
		ans = min(n % d, ans);
		n  -= e;
	}
	ans = min(n % d, ans);

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


import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.searching;
import std.algorithm.mutation;
import std.string;
import std.range;
import std.range.primitives;
import std.conv;

void makeKmpPrefixFunction(string pattern, int[] kmpPreFunc) {
	int m = pattern.length;
	int k = -1;
	int q = 0;
	kmpPreFunc[q] = k;
	while (q < m) {
		while (k >= 0 && pattern[k] != pattern[q])
			k = kmpPreFunc[k];
		++k;
		++q;
		kmpPreFunc[q] = k;
	}
}

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

    string answer = "Just a legend";

    string str;
    readf("%s\n", &str);
    int[] kmpPreFunc = new int[str.length + 1];
    makeKmpPrefixFunction(str, kmpPreFunc);
    int mm = kmpPreFunc[$ - 1];
    if (mm > 0) {
        bool found = false;
        foreach ( i ; 1 .. str.length ) {
            if (kmpPreFunc[i] == mm) {
                found = true;
                answer = str[0 .. mm];
            }
        }
        if (!found) {
            foreach ( i ; 0 .. mm + 1 ) kmpPreFunc[i] = 0;
            makeKmpPrefixFunction(str[0 .. mm], kmpPreFunc);
            if (kmpPreFunc[mm] > 0) answer = str[0 .. kmpPreFunc[mm]];
        }
    }
    writeln(answer);

    return 0;
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.searching;
import std.algorithm.mutation;
import std.string;
import std.range;
import std.range.primitives;
import std.conv;

void makeKmpPrefixFunction(string pattern, int[] kmpPreFunc) {
	int m = pattern.length;
	int k = -1;
	int q = 0;
	kmpPreFunc[q] = k;
	while (q < m) {
		while (k >= 0 && pattern[k] != pattern[q])
			k = kmpPreFunc[k];
		++k;
		++q;
		kmpPreFunc[q] = k;
	}
}

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

    string answer = "Just a legend";

    string str;
    readf("%s\n", &str);
    int[] kmpPreFunc = new int[str.length + 1];
    makeKmpPrefixFunction(str, kmpPreFunc);
    int mm = kmpPreFunc[str.length];
    if (mm > 0) {
        bool found = false;
        foreach ( i ; 1 .. str.length ) {
            if (kmpPreFunc[i] == mm) {
                found = true;
                answer = str[0 .. mm];
                break;
            }
        }
        if (!found) {
            foreach ( i ; 0 .. mm + 1 ) kmpPreFunc[i] = 0;
            makeKmpPrefixFunction(str[0 .. mm], kmpPreFunc);
            if (kmpPreFunc[mm] > 0) answer = str[0 .. kmpPreFunc[mm]];
        }
    }
    writeln(answer);

    return 0;
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.searching;
import std.algorithm.mutation;
import std.string;
import std.range;
import std.range.primitives;
import std.conv;

int matchlen(string str, int pos) {
    pragma(inline, true);
    int res = 0;
    for (int i = 0; i < pos && i*3 < str.length; ++i) {
        if (str[i] == str[i + pos]) ++res;
    }
    return res;
}

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

    string str;
    readf("%s\n", &str);
    if (str.length < 3) {
        writeln("Just a legend");
        return 0;
    }
    foreach_reverse ( i ; 1 .. 1 + str.length / 3 ) {
next:   foreach ( j ; i .. 1 + i + str.length - 3 * i ) {
            foreach ( k ; 0 .. i ) {
                if (str[k] != str[k + j] || str[k + j] != str[$ - i + k])
                    continue next;
            }
            writeln(str[0 .. i]);
            return 0;
        }
    }
    writeln("Just a legend");

    return 0;
}


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 a = readln.splitter.map!(to!long).array;
    long a0 = a[1] - (a[2] - a[1]);
    if (a0 % a[0] == 0 && a0 / a[0] > 0) {
      writeln("YES");
      continue;
    }
    if ((a[0] + a[2]) % 2 == 0 &&
        (((a[0] + a[2]) / 2) % a[1] == 0) &&
        (((a[0] + a[2]) / 2) / a[1] > 0)) {
      writeln("YES");
      continue;
    }
    long a2 = a[1] + (a[1] - a[0]);
    if (a2 % a[2] == 0 && a2 / a[2] > 0) {
      writeln("YES");
      continue;
    }
    writeln("NO");
  }
}

void main() { runSolver(); }

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

  auto solve() {
    void subSolve(long[] nums) {
      bool ans;

      long[] candidates = [
        nums[1] - (nums[2] - nums[1]),
        (nums[0] + nums[2]) % 2 == 0 ? (nums[0] + nums[2]) / 2 : -1,
        nums[1] + (nums[1] - nums[0])
      ];

      // [nums, candidates].deb;
      foreach(i, cand; candidates) {
        if (cand <= 0) continue;

        ans |= (cand % nums[i] == 0);
      }

      writeln(ans ? "YES" : "NO");
    }

    foreach(i; 0..QN) {
      subSolve(scan!long(3));
    }
  }

  outputForAtCoder(&solve);
}

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

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

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

K binarySearch(K)(bool delegate(K) cond, K l, K r) { return binarySearch((K k) => k, cond, l, r); }
T binarySearch(T, K)(K delegate(T) fn, bool delegate(K) cond, T l, T r) {
  auto ok = l;
  auto ng = r;
  const T TWO = 2;

  bool again() {
    static if (is(T == float) || is(T == double) || is(T == real)) {
      return !ng.approxEqual(ok, 1e-08, 1e-08);
    } else {
      return abs(ng - ok) > 1;
    }
  }

  while(again()) {
    const half = (ng + ok) / TWO;
    const halfValue = fn(half);

    if (cond(halfValue)) {
      ok = half;
    } else {
      ng = half;
    }
  }

  return ok;
}

import std;

void main () {
    immutable tests = (){ uint t; readf!"%s\n"(t); return t; }();

    foreach (test_index; 0 .. tests) {
        immutable a = (){ int x; readf!"%s"(x); return x; }();
        immutable b = (){ int x; readf!" %s"(x); return x; }();
        immutable c = (){ int x; readf!" %s\n"(x); return x; }();

        if ((b + b - a) % c == 0 && (b + b - a) / c > 0)
            writeln("YES");
        else if ((a + c) % 2 == 0 && (a + c) / 2 % b == 0 && (a + c) / 2 / b > 0)
            writeln("YES");
        else if ((b - (c - b)) % a == 0 && (b - (c - b)) / a > 0)
            writeln("YES");
        else
            writeln("NO");
    }
}
// ""


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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

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

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

void main()
{
	auto n = RD!int;
	auto edges = new int[][](n);
	foreach (i; 0..n)
	{
		auto s = RD!string;
		foreach (j; 0..n)
		{
			if (s[j] == '1')
			{
				edges[i] ~= j;
			}
		}
	}
	auto m = RD!int;
	auto p = RDA!int(-1);

	long[int] memo;
	long search(int start, int goal)
	{
		auto key = start * 1000 + goal;
		if (memo.get(key, -1) != -1) return memo[key];

		int[] open = [start];
		auto dist = new long[](n);
		dist[] = long.max;
		dist[start] = 0;
		while (!open.empty)
		{
			auto from = open.front; open.popFront;
			foreach (to; edges[from])
			{
				auto d = dist[from] + 1;
				if (d < dist[to])
				{
					dist[to] = d;
					if (to == goal)
					{
						memo[key] = dist[to];
						return dist[to];
					}
					open ~= to;
				}
			}
		}
		return -1;
	}

	long[] ans = [p[0]+1];
	int i, j = 1;
	long last = search(p[0], p[1]);
	while (j < m-1)
	{
		auto d1 = last + search(p[j], p[j+1]);
		auto d2 = search(p[i], p[j+1]);
		if (d2 >= d1) // 省略可能な場合
		{
			++j;
			last = d1;
		}
		else
		{
			ans ~= [p[j]+1];
			i = j;
			++j;
			last = d1 - last;
		}
	}
	ans ~= p[$-1]+1;

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

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

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

    auto g = new string[] (n+1);
    foreach (i; 1 .. n+1) {
        g[i] = "0" ~ readln.chomp;
    }

    debug { g.writeln; }

    immutable int INF = 10 ^^ 9 + 7;
    auto fw = new int[][] (n+1, n+1);
    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. n+1) {
            fw[i][j] = g[i][j] == '1' ? 1 : INF;
        }

        fw[i][i] = 0;
    }

    foreach (k; 1 .. n+1) {
        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. n+1) {
                fw[i][j] = min(fw[i][j], fw[i][k] + fw[k][j]);
            }
        }
    }

    debug { fw.each!writeln; }

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

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

    int[] ans;
    ans ~= a.front;

    foreach (p; 1 .. a.length-1) {
        if (fw[ans.back][a[p+1]] < fw[ans.back][a[p]] + fw[a[p]][a[p+1]]) {
            ans ~= a[p];
        }
    }

    ans ~= a.back;

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

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

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

    auto g = new string[] (n+1);
    foreach (i; 1 .. n+1) {
        g[i] = "0" ~ readln.chomp;
    }

    debug { g.writeln; }

    immutable int INF = 10 ^^ 9 + 7;
    auto fw = new int[][] (n+1, n+1);
    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. n+1) {
            fw[i][j] = g[i][j] == '1' ? 1 : INF;
        }

        fw[i][i] = 0;
    }

    foreach (k; 1 .. n+1) {
        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. n+1) {
                fw[i][j] = min(fw[i][j], fw[i][k] + fw[k][j]);
            }
        }
    }

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

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

    int[] ans;
    ans ~= a.front;

    foreach (p; 1 .. a.length-1) {
        if (fw[a[p-1]][a[p+1]] < fw[a[p-1]][a[p]] + fw[a[p]][a[p+1]]) {
            ans ~= a[p];
        }
    }

    ans ~= a.back;

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

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

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

    auto g = new string[] (n+1);
    foreach (i; 1 .. n+1) {
        g[i] = "0" ~ readln.chomp;
    }

    debug { g.writeln; }

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

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

    int[] ans;
    ans ~= a.front;

    foreach (p; 1 .. a.length-1) {
        if (a[p-1] == a[p+1] || g[a[p-1]][a[p+1]] == '1') {
            ans ~= a[p];
        }
    }

    ans ~= a.back;

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


immutable multi = false;

void solve(int tc)
{
	auto n = readInt!int;
	auto q = readInt!int;
	debug writeln(n, " ", q);
	auto a = ma(n, readInt!int);
	auto b = ma(n, readInt!int);
	auto d = new int[](n);
	foreach(i, ref di; d) di = a[i] - b[i];
	auto ssum = new long[](n + 1);
	ssum[n] = 0;
	foreach_reverse(i; 0 .. n)
	{
		ssum[i] = ssum[i+1] + d[i];
	}
	debug writeln("ssum ", ssum);
	auto ssumMin = Sparse!(long, min, "min")(ssum);
	auto ssumMax = Sparse!(long, max, "max")(ssum);
	debug writeln("entering queries ", q);
	foreach(qi; 0 .. q)
	{
		debug writeln(qi);
		auto l = readInt!int - 1;
		auto r = readInt!int - 1;
		debug writeln("query ", qi + 1, " ", l, " ", r);
		auto sumlr = ssum[l] - ssum[r+1];
		if (sumlr != 0)
		{
			writeln(-1);
			continue;
		}
		auto lsum = ssumMin[l .. r + 1].min - ssum[r+1];
		if (lsum < 0)
		{
			writeln(-1);
			continue;
		}
		auto hsum = ssumMax[l .. r + 1].max - ssum[r+1];
		hsum.writeln;
	}
}

// sparse table V, f, fName {{{
struct Sparse(V, alias f, string fName = "value")
{
	import std.math : log2;
	V[][] _table;
	int[] maxPow;
	this(V[] values)
	{
		auto n = values.length;
		maxPow = new int[](n + 1);
		maxPow[0] = int.min;
		maxPow[1] = 0;
		foreach(i; 2 .. n + 1)
		{
			int prev = maxPow[i - 1];
			maxPow[i] = prev;
			prev = (1 << prev);
			if (prev * 2 <= i) maxPow[i]++;
		}
		_table = new V[][](maxPow[n] + 1, n);
		_table[0][] = values[];

		debug writeln("constructing table ", maxPow[n]);
		foreach(p; 1 .. maxPow[n] + 1)
		foreach(i; 0 .. n - (1<<p) + 1)
			_table[p][i] = f(_table[p-1][i], _table[p-1][i+(1<<(p-1))]);
		debug writeln("done ");
	}
	int[2] opSlice(size_t i)(size_t start, size_t end) if (i == 0)
	{
		return [cast(int)start, cast(int)end];
	}
	auto opIndex(int[2] slice)
	{
		int sliceLen = slice[1] - slice[0];
		auto x = maxPow[sliceLen];
		auto twoX = (1 << x);
		auto p1 = _table[x][slice[0]];
		auto p2 = _table[x][slice[1] - twoX];
		struct Ans
		{
			mixin(q{V }, fName, q{;});
		}
		return Ans(f(p1, p2));
	}
}

unittest
{
	import std.algorithm;
	import std.stdio;
	alias MinQ = Sparse!(int, min, "min");
	writeln("testing");
	int[] array = [1, 2, 3, 4, -2, -2, -3, 0, 10];
	auto minQ = MinQ(array);
	foreach(i; 0 .. array.length)
	{
		foreach(j; i + 1 .. array.length + 1)
		{
			assert(minQ[i .. j].min == array[i .. j].fold!min);
		}
	}
}
// }}}

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

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

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

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

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

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

struct Record
{
	long diff;
	long lo;
	long hi;

	Record opBinary (string op) (const auto ref Record that) const
	    if (op == "+")
	{
		return Record (this.diff + that.diff,
		    min (this.lo, that.lo + this.diff),
		    max (this.hi, that.hi + this.diff));
	}
}

immutable int logHalf = 17;
immutable int half = 1 << logHalf;
immutable int limit = half << 1;

void main ()
{
	int n, q;
	while (readf !(" %s %s") (n, q) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;

		auto t = new Record [limit];
		foreach (i; 0..n)
		{
			t[half + i].diff = b[i] - a[i];
			t[half + i].lo = min (0, t[half + i].diff);
			t[half + i].hi = max (0, t[half + i].diff);
			debug {writeln (t[half + i]);}
		}
		foreach_reverse (i; 1..half)
		{
			t[i] = t[i * 2 + 0] + t[i * 2 + 1];
		}

		Record tGet (int lo, int hi)
		{
			int [] nums1;
			int [] nums2;
			for (lo += half, hi += half; lo < hi;
			    lo >>= 1, hi >>= 1)
			{
				if (lo & 1)
				{
					nums1 ~= lo;
					lo += 1;
				}
				if (hi & 1)
				{
					hi -= 1;
					nums2 ~= hi;
				}
			}
			Record res;
			foreach (i; nums1 ~ nums2.retro.array)
			{
				res = res + t[i];
			}
			debug {writeln (res);}
			return res;
		}

		foreach (j; 0..q)
		{
			int l, r;
			readf !(" %s %s") (l, r);
			l -= 1;
			r -= 0;
			auto cur = tGet (l, r);
			writeln ((cur.diff == 0 && cur.lo >= 0) ? cur.hi : -1);
		}
	}
}


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

int find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1, ret = -1;
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret = mid;
	    left = mid + 1;
	    //right = mid - 1;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    if (ret == -1) ret = left;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    //auto pos = find(a[0] - 1, b);
    //long ansa = a.length * 3;
    //long ansb = pos * 2 + (b.length - pos) * 3;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	//if (i < a.length - 1 && a[i] == a[i + 1]) continue;
	if (i > 0 && a[i] == a[i - 1]) continue;
	auto pos = find(a[i] - 1, b);
	//long scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	long scorea = i * 2 + (a.length - i) * 3;
	long scoreb;
	if (pos < b.length && b[pos] == a[i] - 1)
	{
	    scoreb = (pos + 1) * 2 + (b.length - pos - 1) * 3;
	}
	else
	{
	    scoreb = pos * 2 + (b.length - pos) * 3;
	}
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1, ret = -1;
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret = mid;
	    left = mid + 1;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    if (ret == -1) ret = left;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	if (i > 0 && a[i] == a[i - 1]) continue;
	auto pos = find(a[i] - 1, b);
	long scorea = i * 2 + (a.length - i) * 3;
	long scoreb;
	if (pos < b.length && b[pos] == a[i] - 1)
	{
	    scoreb = (pos + 1) * 2 + (b.length - pos - 1) * 3;
	}
	else
	{
	    scoreb = pos * 2 + (b.length - pos) * 3;
	}
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int[] find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1;
    auto ret = new int[2];
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret[0] = mid;
	    ret[1] = 1;
	    return ret;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    ret[0] = left;
    ret[1] = 0;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	auto res = find(a[i], b);
	long scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	long scoreb;
	if (res[1] == 1)
	{
	    scoreb = (res[0] + 1) * 2 + (b.length - res[0] - 1) * 3;
	}
	else
	{
	    if (res[0] == 0)
	    {
		scoreb = b.length * 3;
	    }
	    else
	    {
		scoreb = res[0] * 2 + (b.length - res[0]) * 3;
	    }
	}
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1, ret = -1;
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret = mid;
	    //left = mid + 1;
	    right = mid - 1;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    if (ret == -1) ret = left;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    //auto pos = find(a[0] - 1, b);
    //long ansa = a.length * 3;
    //long ansb = pos * 2 + (b.length - pos) * 3;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	//if (i < a.length - 1 && a[i] == a[i + 1]) continue;
	if (i > 0 && a[i] == a[i - 1]) continue;
	auto pos = find(a[i] - 1, b);
	//long scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	long scorea = i * 2 + (a.length - i) * 3;
	long scoreb;
	if (pos < b.length && b[pos] == a[i] - 1)
	{
	    scoreb = (pos + 1) * 2 + (b.length - pos - 1) * 3;
	}
	else
	{
	    scoreb = pos * 2 + (b.length - pos) * 3;
	}
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1, ret = -1;
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret = mid;
	    //left = mid + 1;
	    right = mid - 1;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    if (ret == -1) ret = left;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    //auto pos = find(a[0] - 1, b);
    //long ansa = a.length * 3;
    //long ansb = pos * 2 + (b.length - pos) * 3;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	if (i < a.length - 1 && a[i] == a[i + 1]) continue;
	//if (i > 0 && a[i] == a[i - 1]) continue;
	auto pos = find(a[i] - 1, b);
	long scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	long scoreb = pos * 2 + (b.length - pos) * 3;
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1, ret = -1;
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret = mid;
	    left = mid + 1;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    if (ret == -1) ret = left;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	if (i < a.length - 1 && a[i] == a[i + 1]) continue;
	auto pos = find(a[i], b);
	long scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	long scoreb = pos * 2 + (b.length - pos) * 3;
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1, ret = -1;
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret = mid;
	    //left = mid + 1;
	    right = mid - 1;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    if (ret == -1) ret = left;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    int ansa = a.length << 1, ansb = b.length << 1;
    //auto pos = find(a[0] - 1, b);
    //int ansa = a.length * 3;
    //int ansb = pos * 2 + (b.length - pos) * 3;
    int dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	//if (i < a.length - 1 && a[i] == a[i + 1]) continue;
	if (i > 0 && a[i] == a[i - 1]) continue;
	auto pos = find(a[i] - 1, b);
	//int scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	int scorea = i * 2 + (a.length - i) * 3;
	int scoreb = pos * 2 + (b.length - pos) * 3;
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int[] find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1;
    auto ret = new int[2];
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    ret[0] = mid;
	    ret[1] = 1;
	    return ret;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    ret[0] = left;
    ret[1] = 0;
    return ret;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	auto res = find(a[i], b);
	long scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	long scoreb;
	if (res[1] == 1)
	{
	    scoreb = res[0] * 2 + (b.length - res[0]) * 3;
	}
	else
	{
	    if (res[0] == 0)
	    {
		scoreb = b.length * 3;
	    }
	    else
	    {
		scoreb = res[0] * 2 + (b.length - res[0]) * 3;
	    }
	}
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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

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

int find(int tar, int[] arr)
{
    int left = 0, right = to!int(arr.length) - 1;
    while (left <= right)
    {
	int mid = (left + right) >> 1;
	if (arr[mid] == tar)
	{
	    return mid;
	}
	else if (arr[mid] < tar)
	{
	    left = mid + 1;
	}
	else
	{
	    right = mid - 1;
	}
    }
    return left;
}

void solve(int[] a, int[] b)
{
    sort(a);
    sort(b);
    long ansa = a.length << 1, ansb = b.length << 1;
    long dist = ansa - ansb;
    foreach (i; 0 .. a.length)
    {
	auto pos = find(a[i], b);
	long scorea = (i + 1) * 3 + (a.length - i - 1) * 2;
	long scoreb = pos * 2 + (b.length - pos) * 3;
	if (scorea - scoreb > dist || scorea - scoreb == dist && scorea > ansa)
	{
	    dist = scorea - scoreb;
	    ansa = scorea;
	    ansb = scoreb;
	}
    }
    writefln("%d:%d", ansa, ansb);
}

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


// Vicfred
// https://codeforces.com/contest/1536/problem/A
//
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.stdio;
import std.string;

void main() {
    int t = readln.chomp.to!int;

while(t--) {
    int n = readln.chomp.to!int;
    int[] a = readln.split.map!(to!int).array;

    if(a.count!(x => x < 0) > 0) {
        "NO".writeln;
    } else {
        "YES".writeln;
        "101".writeln;
        for(int i = 0; i < 101; ++i)
            writef("%s ", i);
        "".writeln;
    }
}
}

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

int[] solve(ref int[] a, size_t bbase, ref int[int] freq)
{
  size_t n = a.length;
  foreach (i ; bbase .. n)
    freq[a[i]] = 1;

  int[int] b;
  foreach (i ; bbase .. n) {
    foreach (j ; 0 .. i) {
      int diff = abs(a[i] - a[j]);
      if (!freq.get(diff, 0)) {
        b[diff] = 1;
      }
    }
  }

  if (b.length == 0)
    return a;

  if (a.length + b.length > 300)
    return [];

  bbase = a.length;
  a ~= b.keys;
  return solve(a, bbase, freq);
}

void main()
{
  int t;
  readf!" %d "(t);
  foreach (casenum ; 0 .. t) {
    int n;
    readf!" %d "(n);
    auto a = readln.chomp.split.map!(to!int).array;
//    writeln(a);
    int[int] freq;

    auto result = solve(a, 0, freq);
    if (result.length == 0) {
      writeln("NO");
    } else {
      writeln("YES");
      writeln(result.length);
      writeln(result.map!text.joiner(" "));
    }
  }
}

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

void theCode(){
    ll n = scan;
    auto arr = scanArray;
    ll maxx = 0;
    foreach(el; arr){
        if(el < 0){
            writeln("NO");
            return;
        }
        maxx = max(el, maxx);
    }
    writeln("YES");
    writeln(maxx+1);
    foreach(el; 0..maxx+1){
        write(el, " ");
    }
    writeln;
}

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

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

import std.stdio;
import std.algorithm;
import std.numeric;
void solve (){
  int n;
  readf!"%d\n"(n);
  int [] x;
  for (int i = 0; i < n-1; i ++){
    int u;
    readf!"%d "(u);
    x ~= u;
  }
  int u;
  readf!"%d\n"(u);
  x ~= u;
  x.sort!"a < b"();
  if (x[0] < 0){
    writeln("nO");
    return;
  }
  int max = x[n-1];
  writeln("yEs\n", max+1);
  for(int i = 0; i <= max; i ++){
    write(i, " ");
  }
  writeln();
}

void main (){
  int t;
  readf!"%d\n"(t);
  while(t--){
    solve();
  }
}

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

void main() {
    foreach(t; 0 .. to!int(readln.strip)) {
        int n = to!int(readln.strip);
        auto nums = readln.split.map!(to!int).array.sort;
        if(nums[0] < 0) {
            writeln("NO");
        } else {
            writeln("YES");
            writeln(nums[$-1]+1);
            writeln(iota(nums[$-1]+1).map!(to!string).join(" "));
        }
    }
}

// Vicfred
// https://codeforces.com/contest/1536/problem/A
//
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.stdio;
import std.string;

long abs(long n) {
    return n < 0 ? -n : n;
}

bool contains(long[] a, long item) {
    foreach(x; a)
        if(x == item)
            return true;
    return false;
}

void main() {
    int t = readln.chomp.to!int;

while(t--) {
    long n = readln.chomp.to!long;
    long[] a = readln.split.map!(to!long).array;

    while(n < 301) {
        bool found = false;
        n = cast(long)a.length;
        for(int i = 0; i < n; ++i) {
            for(int j = i + 1; j < n; ++j) {
                long x = abs(a[i] - a[j]);
                if(!a.contains(x)) {
                    //writefln("%s no esta", x);
                    a ~= x;
                    found = true;
                }
            }
        }
        if(!found) {
            break;
        }
    }
    if(n >= 301) {
        "NO".writeln;
    } else {
        "YES".writeln;
        foreach(item; a) {
            writef("%s ", item);
        } "".writeln;
    }
}
}

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

void main() {
    foreach(t; 0 .. to!int(readln.strip)) {
        int n = to!int(readln.strip);
        auto nums = readln.split.map!(to!int).array.sort;
        if(nums[0] < 0) {
            writeln("NO");
        } else {
            writeln("YES");
            writeln(nums[$-1]-nums[0]+1);
            writeln(iota(nums[$-1]+1).map!(to!string).join(" "));
        }
    }
}


import core.stdc.stdio;
int n;
int [333333] a;
int [333333] nxt;
int [333333] prv;
void suka(int pos) {
    a[pos] += 1;
    nxt[pos] = nxt[nxt[pos]];
    if(nxt[pos] < n)
        prv[nxt[pos]] = pos;
}
void main()
{
	scanf("%d",&n);
	int len = n;
	for (int i=0;i<n;i++) {
	    scanf("%d",&a[i]);
	    nxt[i] = i + 1;
	    prv[i] = i - 1;
    }
	for (int i;i<n;) {
	    int nx = nxt[i];
	    if(i + 1 < n) {
	        if(a[i] == a[nx]) {
	            suka(i);
	            len--;
	            i = prv[i];
	            continue;
            }
	    }
	    i = nx;
	}
	printf("%d\n", len);
	for (int i;i<n;) {
	    int nx = nxt[i];
	    printf("%d ", a[i]);
	    i = nx;
	}
	printf("\n");
}

import std.stdio;

void main()
{
    int[900100] a;
    int n;
    int it = 0, t;
    readf("%d\n", &n);
    for(int i = 0; i < n; i++, it++)
    {
        readf("%d ", &t);
        a[it] = t;
        if(it == 0)
            continue;
        while(a[it] == a[it - 1])
        {
            it--;
            // writef("add to %d\n", it);
            // writef("%d\n", a[it]);
            a[it] = a[it] + 1;
            // writef("%d\n", a[it]);
        }
    }

    // writef("her: %d\n", a[0]);
    writef("%d\n", it);

    for(int k = 0; k < it; k++)
        writef("%d ", a[k]);
}

import std.stdio, std.bigint, std.string;

void main() {
    int n;
    scanf("%d", &n);
    int[] a = new int[n];
    for(int i = 0; i<n; i++) {
		scanf("%d", &a[i]);
	}
	int [] b= new int[n];
	for (int i =0; i < n; ++i) {
	    b[i] = -1;
	}
	b[0] = a[0];
	int c = 1;
	int i = 1;
	while (i < n) {
		c +=1;
		b[c-1] = a[i];

		while (c > 1 && b[c-1] == b[c-2]) {
		    b[c-2] = b[c-2] +1;
		    c-=1;
		}
		i += 1;
	}
    writeln(c);
    for (int j = 0; j < c; ++j) {
        write(b[j]);
        write(" ");
    }
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	int l=0;
	for(int i = 0; i<n; i++)
	{
		scanf("%d", &arr[l]);
		if(l>0)
		{
			while(1)
			{
				if(arr[l]==arr[l-1])
				{
					arr[l-1]++;
					l--;
				}
				else
				{
					break;
				}
			}
		}
		l++;
	}
	printf("%d\n", l);
	for(int i=0;i<l;i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

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

  int n; rd(n);
  auto x=readln.split.to!(int[]);
    int[200005] val,pre,nxt;
  for(int i=1;i<=n;i++) {
    val[i]=x[i-1];
    pre[i]=i-1;
    nxt[i]=i+1;
  }
  nxt[0]=1;
  pre[n+1]=n;
  int cnt=n;
  for(int i=nxt[1];i<=n;i=nxt[i]) {
    while(val[i]==val[pre[i]]) {
        val[i]++;
        cnt--;
        nxt[pre[pre[i]]]=nxt[pre[i]];
        pre[nxt[pre[i]]]=pre[pre[i]];
    }
  }
  writeln(cnt);
  for(int i=nxt[0];i<=n;i=nxt[i])
    write(val[i]),write(' ');
}

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

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = stdin;

  auto n = next!int;
  auto a = next!int(n);
  auto finalA = DList!int();
  foreach(ai; a)
    {
      auto toInsert = ai;
      while (!finalA.empty && finalA.front == toInsert)
	{
	  finalA.removeFront;
	  toInsert++;
	}
      finalA.insertFront(toInsert);
    }
  auto finalAArray = finalA[].array;
  finalAArray.length.writeln;
  foreach_reverse(fai; finalAArray)
    write(fai, " ");
  writeln;
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  string toString()
  {
    return to!string(rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  pow = pow * pow;
	  exp >>= 1;
	}
      return res;
    }
}

// INPUT
File inputFile;
DList!string _words;
void _wordsFill()
{
  while (_words.empty) _words.insertBack(inputFile.readln.split);
}
string popWord()
{
  _wordsFill;
  auto word = _words.front;
  _words.removeFront;
  return word;
}
string peekWord()
{
  _wordsFill;
  return _words.front;
}
T next(T)()
{
  return to!T(popWord);
}
T peek(T)()
{
  return to!T(peekWord);
}
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;
    }
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	int [] bk = new int[n];
	int tk = -1;
	for(int i = 0; i<n; i++)
		scanf("%d", &arr[i]);
	for(int e = 0; e < n; e++){
        if(tk == -1){
            bk[++tk] = arr[e];
        } else {
            while(tk >= 0 && bk[tk] == arr[e]){
                tk--;
                arr[e]++;
            }
            bk[++tk] = arr[e];
        }
    }
    printf("%d\n", tk + 1);
    for(int e = 0; e <= tk; e++)
        printf("%d ", bk[e]);
    printf("\n");
	return 0;
}

import std.stdio;

/* Hello World Program in D Programming */
void main(string[ ] args)
{
    int n;
    scanf("%d", &n);
    int[211111] a;
    int a_len = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; ++i) {
        a[a_len] = a[i];
        ++a_len;
        while (a_len > 1 && a[a_len - 1] == a[a_len - 2]) {
            --a_len;
            ++a[a_len - 1];
        }
    }
    printf("%d\n", a_len);
    for (int i = 0; i < a_len; ++i) {
        printf("%d ", a[i]);
    }
    printf("\n");

    return;
}

import std.stdio;

void main() {
  int n; readf(" %s", &n);
  auto ok = new int[n];
  int top=0;
  for (int i = 0; i < n; i++) {
    int k; readf(" %s", &k);
      if(top==0){ok[top]=k; top++;}
    else
    {
        if(ok[top-1]!=k){ok[top]=k; top++;}
      else
      {while(ok[top-1]==k && top>0)
      {
        ok[top-1]=k+1;
        k++;
        top--;
      }
      top++;
      }
    }

  }
  writeln(top);
  writeln("\n");
   int i=0;
  while(i<top) {
    writeln(ok[i]);
      i++;
    writeln(" ");
  }

}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto p = [0] ~ readln.splitter.map !(to !(int)).array;
		auto a = new int [n + 1];
		auto cp = 0;
		foreach (i; 1..n + 1)
		{
		    auto x = p[i];
		    while (cp > 0 && a[cp] == x) {
		        x += 1;
		        cp -= 1;
		    }
		    cp += 1;
		    a[cp] = x;
		}
		writeln(cp);
		foreach (i; 1..cp + 1)
		{
		    write(a[i]);
		    write(" ");
		}
	}
}

module main;

import std.stdio;
import std.string;

int main(string[] argv)
{
	int n, j = 0;
	scanf("%d", &n);
    int [] a = new int[n];
    for(int i = 0; i < n; i++)
    {
    scanf("%d", &a[j]);
    while(j > 0 && a[j] == a[j - 1])
    {
        a[j - 1]++;
         a[j] = 0;
        j--;
     }
    j++;
    }
    writeln(j);
    for(int i = 0; i < j; i++)
       write(a[i]," ");

	return 0;
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto p = [0] ~ readln.splitter.map !(to !(int)).array;
		auto a = new int [n + 1];
		auto cp = 0;
		foreach (i; 1..n + 1)
		{
		    auto x = p[i];
		    while (cp > 0 && a[cp] == x) {
		        x += 1;
		        cp -= 1;
		    }
		    cp += 1;
		    a[cp] = x;
		}
		foreach (i; 1..cp + 1)
		{
		    write(a[i]);
		    write(" ");
		}
	}
}

import std.stdio;

void main()
{
    int[101000] a;
    int n;
    int it = 0, t;
    readf("%d\n", &n);
    for(int i = 0; i < n; i++, it++)
    {
        readf("%d ", &t);
        a[it] = t;
        if(it == 0)
            continue;
        if(a[it] == a[it - 1])
        {
            it--;
            // writef("add to %d\n", it);
            // writef("%d\n", a[it]);
            a[it] = a[it] + 1;
            // writef("%d\n", a[it]);
        }
    }

    // writef("her: %d\n", a[0]);

    for(int k = 0; k < it; k++)
        writef("%d ", a[k]);
}

import std.stdio;

void main()
{
    int[101000] a;
    int n;
    int it = 0, t;
    readf("%d\n", &n);
    for(int i = 0; i < n; i++, it++)
    {
        readf("%d ", &t);
        a[it] = t;
        if(it == 0)
            continue;
        if(a[it] == a[it - 1])
        {
            it--;
            // writef("add to %d\n", it);
            // writef("%d\n", a[it]);
            a[it] = a[it] + 1;
            // writef("%d\n", a[it]);
        }
    }

    // writef("her: %d\n", a[0]);
    writef("%d\n", it);

    for(int k = 0; k < it; k++)
        writef("%d ", a[k]);
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	int l=0;
	for(int i = 0; i<n; i++)
	{
		scanf("%d", &arr[l]);
		if(l>0)
		{
			while(1)
			{
				if(arr[l]==arr[l-1])
				{
					arr[l-1]++;
					l--;
				}
				else
				{
					break;
				}
			}
		}
		l++;
	}
	printf("%d", l);
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	int l=0;
	for(int i = 0; i<n; i++)
	{
		scanf("%d", &arr[l]);
		if(l>0)
		{
			while(1)
			{
				if(arr[l]==arr[l-1])
				{
					arr[l-1]++;
					l--;
				}
				else
				{
					break;
				}
			}
		}
		l++;
	}
	printf("%d", l);
	for(int i=0;i<l;i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}