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

void main() {
	int tc;
	scanf("%d\n", &tc);

	while (tc--) {
		int n, m;
		scanf("%d%d\n", &n, &m);

		string[] arr = new string[n];
		for (int i = 0; i < n; i++)
			arr[i] = stdin.byLine.front.to!string;

		string answer = null;
		check: foreach(s; arr) {
			foreach(i; iota(m)) {
				for (char c = 'a'; c <= 'z'; c++) {
					char[] test_string = s.dup;
					test_string[i] = c;

					// test the string
					bool good = true;
					foreach (t; arr) {
						int d = 0;
						foreach (j, e; t) d += e != test_string[j];
						if (d > 1) {
							good = false;
							break;
						}
					}

					if (good) {
						answer = test_string.dup;
						break check;
					}
				}
			}
		}

		if (answer == null) writeln(-1);
		else answer.writeln;
	}
}

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

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

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

        string[] input;
        foreach (i; 0 .. n) {
            input ~= readln.chomp;
        }

        auto ans = input[0].to!(dchar[]);
        foreach (col; 0 .. m) {

            foreach (row; 0 .. n) {
                ans[col] = input[row][col];

                bool ok = true;
                foreach (compareidx; 0 .. n) {
                    ok &= zip(ans, input[compareidx]).filter!"a[0] != a[1]".array.length <= 1;
                }

                if (ok) {
                    writeln(ans);
                    continue outer;
                }
            }

            ans[col] = input[0][col];
        }

        writeln(-1);
    }
}

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

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

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

  void solve(long tc = -1)
  {
    bool test(string str)
    {
      return iota(0, n)
        .all!(j => iota(0, m).count!(i => a.at(j).at(i) != str.at(i)) <= 1);
    }

    char[] ts = a.at(0).dup;
    foreach(i; 0 .. m)
      {
        foreach(c; 'a' .. cast(char)('z' + 1))
          {
            ts.at(i) = c;
            if (test(cast(string) ts))
              {
                writeln(ts);
                return;
              }
          }
        ts.at(i) = a.at(0).at(i);
      }
    writeln(-1);
  }
}

struct Interval
{
  long l, h;

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

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

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

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

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

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

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

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

DList!string _words;

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

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

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

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

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

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

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

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

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

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

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

    }
}

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

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

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

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

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

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

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

void main()
{
  type;
}

import std.stdio, 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 string[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto s = new string[](n);
		foreach (i; 0..n)
			s[i] = RD!string;

		if (n == 1)
		{
			ans[ti] = s[0];
			continue;
		}

		auto used = new bool[][](m, 26);
		foreach (i; 0..n)
		{
			foreach (j; 0..m)
			{
				auto num = s[i][j]-'a';
				used[j][num] = true;
			}
		}

		int[][string] dp;
		dp[""] = new int[](n);
		foreach (i; 0..m)
		{
			int[][string] ndp;
			foreach (j; 0..26)
			{
				if (used[i][j] == false) continue;
				auto c = cast(char)('a'+j);
				foreach (key; dp.keys)
				{
					auto str = key ~ c;
					auto cnt = dp[key].dup;
					bool ok = true;
					foreach (k; 0..n)
					{
						if (s[k][i] != c)
						{
							++cnt[k];
							if (cnt[k] >= 2)
							{
								ok = false;
								break;
							}
						}
					}
					if (ok)
					{
						ndp[str] = cnt;
					}
				}
			}
			dp = ndp;
		}
		if (!dp.empty)
		{
			ans[ti] = dp.keys[0];
		}
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
			writeln(e);
	}
	stdout.flush;
	debug readln;
}


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;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [n];
		foreach (ref x; a)
		{
			readf (" %s", &x);
		}
		sort !("a < b", SwapStrategy.stable) (a);

		long res = 0;
		long [] ans;
		if (n == 1)
		{
			res = -1;
		}
		else if (n == 2)
		{
			if (a[0] == a[1])
			{
				res = 1;
				ans ~= a[0];
			}
			else
			{
				int d = a[1] - a[0];
				res = 2 + (d % 2 == 0);
				ans ~= a[0] - d;
				if (d % 2 == 0)
				{
					ans ~= (a[0] + a[1]) >> 1;
				}
				ans ~= a[1] + d;
			}
		}
		else if (a[$ - 1] == a[1])
		{
			res = 1;
			ans ~= a[0];
		}
		else
		{
			bool [int] e;
			e[a[1] - a[0]] = true;
			e[a[2] - a[1]] = true;
			foreach (d, val; e)
			{
				ans = [];
				int holes = 0;
				for (int i = 1; i < n; i++)
				{
					if (a[i] - a[i - 1] == d)
					{
						continue;
					}
					if (a[i] - a[i - 1] == d * 2)
					{
						ans ~= (a[i] + a[i - 1]) >> 1;
						holes++;
					}
					else
					{
						holes += 2;
					}
				}
				if (holes == 0)
				{
					ans ~= a[0] - d;
					ans ~= a[$ - 1] + d;
				}
				res = max (0, 2 - holes);
				if (res > 0)
				{
					break;
				}
			}
		}
		writeln (res);
		if (res > 0)
		{
			writefln ("%(%s %)", ans);
		}
	}
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = readln.chomp.split(" ").map!(to!int).array;
    xs.sort;
    if (xs.length == 1) {
        (-1).writeln;
        return;
    }
    if (xs.length == 2) {
        if ((xs.back - xs.front) % 2 == 0) {
            if (xs.back - xs.front == 0) {
                writeln(1);
                writeln(xs.back);
            } else {
                int d = (xs.back - xs.front) / 2;
                writeln(3);
                writeln(xs.front - 2 * d, " ", xs.front + d, " ", xs.back + 2 * d);
            }
        } else {
            int d = (xs.back - xs.front);
            writeln(2);
            writeln(xs.front - d, " ", xs.back + d);
        }
        return;
    }
    int[] ds;
    foreach (i; 0 .. xs.length - 1) {
        ds ~= xs[i + 1] - xs[i];
    }
    int[] k = ds.sort.uniq.array;
    switch (k.length) {
        case 1:
            if (k[0] == 0) {
                writeln(1);
                writeln(xs.front);
            } else {
                writeln(2);
                writeln(xs.front - k[0], " ", xs.back + k[0]);
            }
            return;
        case 2:
            int[int] c;
            foreach (d; ds) {
                c[d]++;
            }
            if (c.length > 2) {
                writeln(0);
                return;
            }
            if (c.length == 2) {
                int k1 = c.keys.reduce!max;
                int k2 = c.keys.reduce!min;
                if (c[k1] != 1) {
                    writeln(0);
                    return;
                }
            }
            if (k[0] * 2 != k[1]) {
                writeln(0);
                return;
            }
            int d = k[0];
            foreach (i; 0 .. xs.length - 1) {
                if (xs[i + 1] - xs[i] != d) {
                    if (xs[i + 1] - xs[i] == 2 * d) {
                        1.writeln;
                        ((xs[i + 1] + xs[i]) / 2).writeln;
                    } else {
                        0.writeln;
                    }
                    return;
                }
            }
        default:
            writeln(0);
            return;
    }
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = readln.chomp.split(" ").map!(to!int).array;
    xs.sort;
    if (xs.length == 1) {
        (-1).writeln;
        return;
    }
    if (xs.length == 2) {
        if ((xs.back - xs.front) % 2 == 0) {
            if (xs.back - xs.front == 0) {
                writeln(1);
                writeln(xs.back);
            } else {
                int d = (xs.back - xs.front) / 2;
                writeln(3);
                writeln(xs.front - 2 * d, " ", xs.front + d, " ", xs.back + 2 * d);
            }
        } else {
            int d = (xs.back - xs.front);
            writeln(2);
            writeln(xs.front - d, " ", xs.back + d);
        }
        return;
    }
    int[] ds;
    foreach (i; 0 .. xs.length - 1) {
        ds ~= xs[i + 1] - xs[i];
    }
    int[] k = ds.sort.uniq.array;
    switch (k.length) {
        case 1:
            if (k[0] == 0) {
                writeln(1);
                writeln(xs.front);
            } else {
                writeln(2);
                writeln(xs.front - k[0], " ", xs.back + k[0]);
            }
            return;
        case 2:
            int[int] c;
            foreach (d; ds) {
                c[d]++;
            }
            if (c.length > 2) {
                writeln(0);
                return;
            }
            if (c.length == 2) {
                bool f = false;
                foreach (key, value; c) {
                    if (value == 1) {
                        f = true;
                    }
                }
                if (!f) {
                    writeln(0);
                    return;
                }
            }
            if (k[0] * 2 != k[1]) {
                writeln(0);
                return;
            }
            int d = k[0];
            foreach (i; 0 .. xs.length - 1) {
                if (xs[i + 1] - xs[i] != d) {
                    if (xs[i + 1] - xs[i] == 2 * d) {
                        1.writeln;
                        ((xs[i + 1] + xs[i]) / 2).writeln;
                    } else {
                        0.writeln;
                    }
                    return;
                }
            }
        default:
            writeln(0);
            return;
    }
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = readln.chomp.split(" ").map!(to!int).array;
    if (xs.length == 1) {
        (-1).writeln;
        return;
    }
    if (xs.length == 2) {
        if ((xs.back - xs.front) % 2 == 0) {
            int d = (xs.back - xs.front) / 2;
            writeln(3);
            writeln(xs.front - 2 * d, " ", xs.front + d, " ", xs.back + 2 * d);
        } else {
            writeln(2);
            writeln(xs.front, xs.back);
        }
        return;
    }
    xs.sort;
    int[] ds;
    foreach (i; 0 .. xs.length - 1) {
        ds ~= xs[i + 1] - xs[i];
    }
    int[] k = ds.sort.uniq.array;
    switch (k.length) {
        case 1:
            writeln(2);
            writeln(xs.front - k[0], " ", xs.back + k[0]);
            return;
        case 2:
            if (k[0] * 2 != k[1]) {
                writeln(0);
                return;
            }
            int d = k[0];
            foreach (i; 0 .. xs.length - 1) {
                if (xs[i + 1] - xs[i] != d) {
                    if (xs[i + 1] - xs[i] == 2 * d) {
                        1.writeln;
                        ((xs[i + 1] + xs[i]) / 2).writeln;
                    } else {
                        0.writeln;
                    }
                    return;
                }
            }
        default:
            writeln(0);
            return;
    }
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = readln.chomp.split(" ").map!(to!int).array;
    if (xs.length == 1) {
        (-1).writeln;
        return;
    }
    if (xs.length == 2) {
        if ((xs.back - xs.front) % 2 == 0) {
            if (xs.back - xs.front == 0) {
                writeln(1);
                writeln(xs.back);
            } else {
                int d = (xs.back - xs.front) / 2;
                writeln(3);
                writeln(xs.front - 2 * d, " ", xs.front + d, " ", xs.back + 2 * d);
            }
        } else {
            writeln(2);
            writeln(xs.front, xs.back);
        }
        return;
    }
    xs.sort;
    int[] ds;
    foreach (i; 0 .. xs.length - 1) {
        ds ~= xs[i + 1] - xs[i];
    }
    int[] k = ds.sort.uniq.array;
    switch (k.length) {
        case 1:
            if (k[0] == 0) {
                writeln(1);
                writeln(xs.front);
            } else {
                writeln(2);
                writeln(xs.front - k[0], " ", xs.back + k[0]);
            }
            return;
        case 2:
            if (k[0] * 2 != k[1]) {
                writeln(0);
                return;
            }
            int d = k[0];
            foreach (i; 0 .. xs.length - 1) {
                if (xs[i + 1] - xs[i] != d) {
                    if (xs[i + 1] - xs[i] == 2 * d) {
                        1.writeln;
                        ((xs[i + 1] + xs[i]) / 2).writeln;
                    } else {
                        0.writeln;
                    }
                    return;
                }
            }
        default:
            writeln(0);
            return;
    }
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = readln.chomp.split(" ").map!(to!int).array;
    xs.sort;
    if (xs.length == 1) {
        (-1).writeln;
        return;
    }
    if (xs.length == 2) {
        if ((xs.back - xs.front) % 2 == 0) {
            if (xs.back - xs.front == 0) {
                writeln(1);
                writeln(xs.back);
            } else {
                int d = (xs.back - xs.front) / 2;
                writeln(3);
                writeln(xs.front - 2 * d, " ", xs.front + d, " ", xs.back + 2 * d);
            }
        } else {
            int d = (xs.back - xs.front);
            writeln(2);
            writeln(xs.front - d, " ", xs.back + d);
        }
        return;
    }
    int[] ds;
    foreach (i; 0 .. xs.length - 1) {
        ds ~= xs[i + 1] - xs[i];
    }
    int[] k = ds.sort.uniq.array;
    switch (k.length) {
        case 1:
            if (k[0] == 0) {
                writeln(1);
                writeln(xs.front);
            } else {
                writeln(2);
                writeln(xs.front - k[0], " ", xs.back + k[0]);
            }
            return;
        case 2:
            if (k[0] * 2 != k[1]) {
                writeln(0);
                return;
            }
            int d = k[0];
            foreach (i; 0 .. xs.length - 1) {
                if (xs[i + 1] - xs[i] != d) {
                    if (xs[i + 1] - xs[i] == 2 * d) {
                        1.writeln;
                        ((xs[i + 1] + xs[i]) / 2).writeln;
                    } else {
                        0.writeln;
                    }
                    return;
                }
            }
        default:
            writeln(0);
            return;
    }
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = readln.chomp.split(" ").map!(to!int).array;
    if (xs.length == 1) {
        (-1).writeln;
        return;
    }
    if (xs.length == 2) {
        if ((xs.back - xs.front) % 2 == 0) {
            int d = (xs.back - xs.front) / 2;
            writeln(3);
            writeln(xs.front - 2 * d, " ", xs.front + d, " ", xs.back + 2 * d);
        } else {
            writeln(2);
            writeln(xs.front, xs.back);
        }
        return;
    }
    xs.sort;
    int d = xs[1] - xs[0];
    foreach (i; 0 .. xs.length - 1) {
        if (xs[i + 1] - xs[i] != d) {
            if (xs[i + 1] - xs[i] == 2 * d) {
                1.writeln;
                ((xs[i + 1] + xs[i]) / 2).writeln;
            } else {
                0.writeln;
            }
            return;
        }
    }
    writeln(2);
    writeln(xs.front - d, " ", xs.back + d);
}

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

void main() {
    int n; readf("%d\n", &n);
    int[] xs = readln.chomp.split(" ").map!(to!int).array;
    xs.sort;
    if (xs.length == 1) {
        (-1).writeln;
        return;
    }
    if (xs.length == 2) {
        if ((xs.back - xs.front) % 2 == 0) {
            if (xs.back - xs.front == 0) {
                writeln(1);
                writeln(xs.back);
            } else {
                int d = (xs.back - xs.front) / 2;
                writeln(3);
                writeln(xs.front - 2 * d, " ", xs.front + d, " ", xs.back + 2 * d);
            }
        } else {
            int d = (xs.back - xs.front);
            writeln(2);
            writeln(xs.front - d, " ", xs.back + d);
        }
        return;
    }
    int[] ds;
    foreach (i; 0 .. xs.length - 1) {
        ds ~= xs[i + 1] - xs[i];
    }
    int[] k = ds.sort.uniq.array;
    switch (k.length) {
        case 1:
            if (k[0] == 0) {
                writeln(1);
                writeln(xs.front);
            } else {
                writeln(2);
                writeln(xs.front - k[0], " ", xs.back + k[0]);
            }
            return;
        case 2:
            int[int] c;
            foreach (d; ds) {
                c[d]++;
            }
            if (c.length > 2) {
                writeln(0);
                return;
            }
            if (c.length == 2) {
                int k1 = c.keys.reduce!min;
                int k2 = c.keys.reduce!max;
                if (c[k1] != 1) {
                    writeln(0);
                    return;
                }
            }
            if (k[0] * 2 != k[1]) {
                writeln(0);
                return;
            }
            int d = k[0];
            foreach (i; 0 .. xs.length - 1) {
                if (xs[i + 1] - xs[i] != d) {
                    if (xs[i + 1] - xs[i] == 2 * d) {
                        1.writeln;
                        ((xs[i + 1] + xs[i]) / 2).writeln;
                    } else {
                        0.writeln;
                    }
                    return;
                }
            }
        default:
            writeln(0);
            return;
    }
}


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

void main(){

	int[200010] a;
	int n;

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

	int ss = 0;

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

	readf("%s", &a[n]);
	ss += a[n];
	readln;

	ss = (ss + 1) / 2;
	int tt = 0;
	for (int i = 1; i <= n; ++i){
		tt += a[i];
		if (tt >= ss){
			writeln(i);
			return;
		}
	}
}

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

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

  auto a = readln.stripRight.split.map!(a => a.parse!int);
  auto s = cumulativeFold!"a + b"(a, 0).array;
  // find the first value >= sum/2
  writeln(s.assumeSorted.lowerBound((s[$-1] + 1) / 2).length + 1);
}


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

void main()
{
    auto t = readln().strip().to!int;
    foreach (i; 0..t)
    {
        auto testCase = readln().strip();
        auto numbers = testCase.split().map!(s=>s.to!int).array.sort;
        writefln("%d", numbers[1]);
    }
}

import std.stdio;
import std.algorithm;
import std.math;
//import std.conv;
//import std.numeric;
//import std.range;
import std.array;
import std.bigint;
import std.string;

void sol(){

    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    if(a>b){
        swap(a,b);
    }
    if(b>c){
        swap(b,c);
    }
    if(a>b){
        swap(a,b);
    }

    writeln(b);
}

void main(){

    int t;
    readf!"%d"(t);
    while (t--){
        sol();
    }
}

import std;

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto a = readln.splitter.map!(to!long).array.sort.array;
    writeln(a[1]);
  }
}


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

string FMT_F = "%.10f";

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

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

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

		ans[ti] = long.min;
		foreach_reverse (i; 0..n)
		{
			auto x = k * a[i];
			foreach_reverse (j; 0..i)
			{
				long y = cast(long)(i+1) * (j+1);
				if (y - x <= ans[ti]) break;
				auto z = y - k * (a[i]|a[j]);
				ans[ti].chmax(z);
			}
		}
	}

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

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

void main()
{
	int t = readInt!int;
	while (t--) solve;
}

void solve()
{
	int n = readInt!int;
	int k = readInt!int;
	auto a = new int[](n); foreach(ref ai; a) ai = readInt!int;
	long mx = long.min;
	foreach(j; n - 100 .. n)
	{
		foreach(i; 0 .. j)
		{
			mx = max(mx, cast(long)(i+1)*cast(long)(j+1) - cast(long)k * (a[i]|a[j]));
		}
	}
	mx.writeln;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

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

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

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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(string[ ] args) {
    int t;
    readf!"%s"(t);
    readln;

    while (t--) {
        int n, k;
        readf!"%s %s"(n, k);
        readln;

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

        immutable int reach = 110;

        long ans = long.min;
        for (int i = n-1; i >= 0 && i >= n - reach; --i) {
            for (int j = i-1; j >= 0 && j >= n - reach; --j) {
                auto cur = (i+1).to!long * (j+1).to!long - k * (a[i] | a[j]);
                ans = max(cur, ans);
            }
        }

        ans.writeln;
    }
}

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

alias P = Tuple!(long, "i", long, "j");

void solve() {
    int N; long K; readf("%d %d\n", &N, &K);
    auto A = readarray!int;

    long xmax = -(1L<<56);
    for (int j = N; j >= 1; j--) {
        for (int i = j-1; i >= 1; i--) {
            if (xmax >= cast(long)(i) * j) break;
            long x = cast(long)(i) * j - K * (A[i-1] | A[j-1]);
            xmax.chmax(x);
        }
    }
    writeln(xmax);
}

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

alias P = Tuple!(long, "i", long, "j");

void solve() {
    int N; long K; readf("%d %d\n", &N, &K);
    auto A = readarray!int;

    if (N >= 500) {
        bool[P] used;
        auto Q = heapify!((a, b) => a.i*a.j < b.i*b.j)(new P[0], 0);
        Q.insert(P(N-1, N));
        long xmax = -(1L<<56);
        while (! Q.empty) {
            auto c = Q.front; Q.removeFront;
            long i = c.i, j = c.j;
            if (i * j < xmax) break;
            long x = i * j - K * (A[cast(int)(i-1)] | A[cast(int)(j-1)]);
            xmax.chmax(x);
            if (i + 1 < j) {
                auto n = P(i, j-1);
                if (n !in used) {
                    used[n] = true;
                    Q.insert(n);
                }
            }
            if (i > 1) {
                auto n = P(i-1, j);
                if (n !in used) {
                    used[n] = true;
                    Q.insert(n);
                }
            }
        }
        writeln(xmax);
    } else {
        long xmax = -(1L<<56);
        for (int j = N; j >= 1; j--) {
            for (int i = j-1; i >= 1; i--) {
                if (xmax >= i * j) break;
                long x = i * j - K * (A[i-1] | A[j-1]);
                xmax.chmax(x);
            }
        }
        writeln(xmax);
    }
}

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

alias P = Tuple!(long, "i", long, "j");

void solve() {
    int N; long K; readf("%d %d\n", &N, &K);
    auto A = readarray!int;

    long xmax = -(1L<<56);
    for (int j = N; j >= 1; j--) {
        for (int i = j-1; i >= 1; i--) {
            if (xmax >= i * j) break;
            long x = i * j - K * (A[i-1] | A[j-1]);
            xmax.chmax(x);
        }
    }
    writeln(xmax);
}

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

alias P = Tuple!(long, "i", long, "j");

void solve() {
    int N; long K; readf("%d %d\n", &N, &K);
    auto A = readarray!int;
    bool[P] used;
    DList!P Q;
    Q.insert(P(N-1, N));
    long xmax = -(1L<<56);
    while (! Q.empty) {
        auto c = Q.front; Q.removeFront;
        long i = c.i, j = c.j;
        if (i * j < xmax) break;
        long x = i * j - K * (A[cast(int)(i-1)] | A[cast(int)(j-1)]);
        xmax.chmax(x);
        if (i + 1 < j) {
            auto n = P(i, j-1);
            if (n !in used) {
                used[n] = true;
                Q.insert(n);
            }
        }
        if (i > 1) {
            auto n = P(i-1, j);
            if (n !in used) {
                used[n] = true;
                Q.insert(n);
            }
        }
    }
    writeln(xmax);
}

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(string[ ] args) {
    int t;
    readf!"%s"(t);
    readln;

    while (t--) {
        int n, k;
        readf!"%s %s"(n, k);
        readln;

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

        immutable int reach = 100;

        long ans = long.min;
        for (int i = n-1; i >= 0 && i >= n - reach; --i) {
            for (int j = i-1; j >= 0 && j >= n - reach; --j) {
                auto cur = (i+1).to!long * (j+1).to!long - k * (a[i] | a[j]);
                ans = max(cur, ans);
            }
        }

        ans.writeln;
    }
}

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

void main(string[ ] args) {
    int t;
    readf!"%s"(t);
    readln;

    while (t--) {
        int n, k;
        readf!"%s %s"(n, k);
        readln;

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

        long ans = long.min;
        for (int i = n-1; i >= 0 && i >= n - 10; --i) {
            for (int j = i-1; j >= 0 && j >= n - 10; --j) {
                auto cur = (i+1).to!long * (j+1).to!long - k * (a[i] | a[j]);
                ans = max(cur, ans);
            }
        }

        ans.writeln;
    }
}

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

void main()
{
	int t = readInt!int;
	while (t--) solve;
}

void solve()
{
	int n = readInt!int;
	int k = readInt!int;
	auto a = new int[](n); foreach(ref ai; a) ai = readInt!int;
	if (n <= 1000)
	{
		long mx = long.min;
		foreach(i; 0 .. n)
			foreach(j; i + 1 .. n)
				mx = max(mx, cast(long)(i+1)*cast(long)(j+1) - cast(long)k * (a[i]|a[j]));
		mx.writeln;
	}
	else
	{
		long mx = long.min;
		foreach(j; n - 3 .. n)
		{
			foreach(i; 0 .. j)
			{
				mx = max(mx, cast(long)(i+1)*cast(long)(j+1) - cast(long)k * (a[i]|a[j]));
			}
		}
		mx.writeln;
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

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

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

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

		ans[ti] = long.min;
		foreach_reverse (i; 0..n)
		{
			auto x = k * a[i];
			foreach_reverse (j; 0..i)
			{
				long y = (i+1) * (j+1);
				if (y - x <= ans[ti]) break;
				auto z = y - k * (a[i]|a[j]);
				ans[ti].chmax(z);
			}
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

		ans[ti] = long.min;
		foreach_reverse (i; 0..n)
		{
			auto x = k * a[i];
			foreach_reverse (j; 0..i)
			{
				auto y = (i+1) * (j+1);
				if (y - x <= ans[ti]) break;
				auto z = y - k * (a[i]|a[j]);
				ans[ti].chmax(z);
			}
		}
	}

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


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 (_t; 0..T) {
        auto N = readln.chomp.to!long;
        long[] as;
        long b = 1;
        N -= 1;
        while (N) {
            if (N <= b*2) {
                as ~= N-b;
                break;
            }
            if (N <= b*4) {
                as ~= N/2-b;
                b = N/2;
            } else {
                as ~= b;
                b *= 2;
            }
            N -= b;
        }
        writeln(as.length);
        writeln(as.to!(string[]).join(" "));
    }
}

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

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

        int mx = 0;
        while (n >= (1 << mx)) {
            n -= (1 << mx);
            ++mx;
        }

        int[] arr;
        foreach (b; 0 .. mx) {
            arr ~= (1 << b);
            if (n >= (1 << b) && n < (1 << (b+1))) { arr ~= n; }
        }

        int[] ans;
        foreach (prv, nxt; lockstep(arr, arr.dropOne)) {
            ans ~= nxt - prv;
        }

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

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

void main ()
{
	readln;
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto steps = bsr (n);
		int cur = 1;
		int total = cur;
		int [] ans;
		foreach (step; 0..steps)
		{
			int next = (n - total) / (steps - step);
			next = min (next, cur * 2);
			ans ~= next - cur;
			cur = next;
			total += next;
		}
		writeln (steps);
		writefln !("%(%s %)") (ans);
	}
}

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 (_t; 0..T) {
        auto N = readln.chomp.to!long;
        long[] as;
        long b = 1;
        if (N%2 == 0) {
            as ~= 0;
            N -= 2;
        } else {
            N -= 1;
        }
        while (N) {
            if (N == b) {
                as ~= 0;
                break;
            } else if (N == b*2) {
                as ~= b;
                break;
            }
            if (N/2 <= b*2) {
                as ~= N/2-b;
                b = N/2;
                N /= 2;
            } else {
                as ~= b;
                b *= 2;
                N -= b;
            }
        }
        writeln(as.length);
        writeln(as.to!(string[]).join(" "));
    }
}


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()
{
    immutable int MD = 998244353;
    immutable int MXN = 3 * 10 ^^ 5;

    auto pw = new int[] (MXN + 1);
    pw[0] = 1;
    foreach (i; 1 .. MXN+1) { pw[i] = pw[i-1] * 2 % MD; }

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

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

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

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

        auto clr = new int[] (n+1);
        clr[] = 0;
        int ans = 1;
        foreach (i; 1 .. n+1) {
            if (clr[i] != 0) { continue; }

            auto res = new int[] (3);
            res[] = 0;

            bool dfs(int v, int c) {
                clr[v] = c;
                res[c] += 1;
                auto ok = true;
                foreach (u; g[v]) {
                    if (clr[u] == 0) {
                        ok &= dfs(u, 3 - c);
                    }
                    else if (clr[u] == 3 - c) { continue; }
                    else { return false; }
                }

                return ok;
            }

            if (!dfs(i, 1)) {
                writeln(0);
                continue outer;
            }

            debug { writeln(i, ' ', res); }

            int cur = (pw[res[1]] + pw[res[2]]) % MD;
            ans = ans.to!long * cur % MD;
        }

        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, core.stdc.string;

immutable long MOD = 998244353;

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

void solve() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }

    long ans = 1;
    auto cnt = new long[](2);
    auto C = new int[](N);
    fill(C, -1);

    bool dfs(int n, int c) {
        if (C[n] != -1)
            return C[n] == c;
        C[n] = c;
        cnt[c] += 1;
        foreach (m; G[n]) {
            if (C[m] != -1) {
                if (C[m] == (c^1))
                    continue;
                else
                    return false;
            }
            if (!dfs(m, c^1))
                return false;
        }
        return true;
    }

    foreach (i; 0..N) {
        if (C[i] != -1) continue;
        cnt[0] = cnt[1] = 0;
        if (dfs(i, 0)) {
            long tmp = powmod(2, cnt[0], MOD) + powmod(2, cnt[1], MOD);
            tmp %= MOD;
            ans = ans * tmp % MOD;
        } else {
            writeln(0);
            return;
        }
    }

    ans.writeln;
}

void main() {
    auto Q = readln.chomp.to!int;
    while (Q--) solve;
}

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

immutable long MOD = 998244353;

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

void solve() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);
    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }

    long ans = 0;
    auto cnt = new long[](2);
    auto C = new int[](N);
    fill(C, -1);

    bool dfs(int n, int c) {
        if (C[n] != -1)
            return C[n] == c;
        C[n] = c;
        cnt[c] += 1;
        foreach (m; G[n]) {
            if (C[m] != -1) {
                if (C[m] != (c^1))
                    return false;
                else
                    continue;
            }
            if (!dfs(m, c^1))
                return false;
        }
        return true;
    }

    foreach (i; 0..N) {
        if (C[i] != -1) continue;
        cnt[0] = cnt[1] = 0;
        if (dfs(i, 0)) {
            ans += powmod(2, cnt[0], MOD) + powmod(2, cnt[1], MOD);
            ans %= MOD;
        } else {
            writeln(0);
            return;
        }
    }

    ans.writeln;
}

void main() {
    auto Q = readln.chomp.to!int;
    while (Q--) solve;
}

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()
{
    immutable int MD = 998244353;
    immutable int MXN = 3 * 10 ^^ 5;

    auto pw = new int[] (MXN + 1);
    pw[0] = 1;
    foreach (i; 1 .. MXN+1) { pw[i] = pw[i-1] * 2 % MD; }

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

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

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

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

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

        bool dfs(int v, int c) {
            clr[v] = c;
            bool ok = true;
            foreach (u; g[v]) {
                if (clr[u] == 0) {
                    ok &= dfs(u, 3 - c);
                }
                else if (clr[u] == 3 - c) { continue; }
                else { return false; }
            }

            return ok;
        }

        if (!dfs(1, 1)) {
            writeln(0);
            continue;
        }

        debug { writeln(clr); }

        auto side = clr.count!(x => x == 1).to!int;
        int ans = (pw[side] + pw[n - side]) % MD;
        ans.writeln;
    }
}


import std.stdio;
int main()
{
	int t=0;
	readf(" %d", &t);
	for (int i=0; i<t; i++)
	{
		long a=0;
		long b=0;
		long c=0;
		readf(" %d", &a);
		readf(" %d", &b);
		readf(" %d", &c);
		long d=c/2;
		writeln(a*(c-d)-b*d);
	}
	return 0;
}

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

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

    while (t--)
    {
        long a, b, k;
        readf!" %s %s %s "(a, b, k);

        long ans = k / 2 * (a - b);
        if (k % 2)
            ans += a;

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

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

    while (t--) {
        int a, b, k;
        readf("%s %s %s", &a, &b, &k);
        readln;

        auto diff = a - b;
        auto ans = cast(long)diff * (k/2);

        if (k % 2 == 1) ans += a;

        ans.writeln;
    }
}


import std;

void main () {
	auto n = readln.strip.to !(int);
	auto a = readln.splitter.map !(to !(long)).array;
	rndGen.seed (123);
	long first = a.front;
	a.popFront ();
	randomShuffle (a);
	for (long u = 1; ; u++) {
		auto lo = max (u ^^ 2 - first, 0);
		auto hi = u ^^ 2 + u - first;
		foreach (i, ref c; a) {
			auto d = c + lo;
			auto v = cast (long) (sqrt (cast (double) (d)));
			if (d > v ^^ 2 + v) lo += (v + 1) ^^ 2 - d;
			else hi = min (hi, lo + v ^^ 2 + v - d);
			if (lo > hi) {
				a.swapAt (i, uniform (0, i + 1));
				break;
			}
		}
		if (lo <= hi) {
			writeln (lo);
			break;
		}
	}
}

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

immutable long limit = 2 * 10 ^^ 6 + 5;

void main ()
{

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto used = new bool [limit];
		auto a = readln.splitter.map !(to !(long)).array;

		rndGen.seed (263_473_470);
		long sel = a.front;
		long cur = sel;
		a.popFront ();
		randomShuffle (a);
		while (true)
		{
			long base = cast (long) (sqrt (cur * 1.0));
			long lo = cast (long) (cur - base * 1L * base);
			long hi = base;
			long delta = cur - sel;
			foreach (i, ref c; a)
			{
				long d = c + delta;
				long v = cast (long) (sqrt (d * 1.0));
				long w = v * 1L * v;
				if (d > w + v)
				{
					long e = w + v * 2 + 1;
					long add = cast (long) (e - d);
					cur += add;
					delta += add;
					lo += add;
				}
				else
				{
					hi = min (hi,
					    lo + cast (long) (w + v - d));
				}
				if (lo > hi)
				{
					a.swapAt (i, uniform (0, i + 1));
					break;
				}
			}
			if (lo <= hi)
			{
				break;
			}
			cur = (base + 1L) ^^ 2;
		}
		writeln (cur - sel);
	}
}

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

immutable long limit = 2 * 10 ^^ 6 + 5;

void main ()
{

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto used = new bool [limit];
		auto a = readln.splitter.map !(to !(long)).array;

		rndGen.seed (263_473_470);
		long sel = a.front;
		long cur = sel;
		a.popFront ();
		randomShuffle (a);
		while (true)
		{
			long base = cast (long) (sqrt (cur * 1.0));
			long lo = cast (long) (cur - base * 1L * base);
			long hi = base;
			long delta = cur - sel;
			foreach (i, ref c; a)
			{
				long d = c + delta;
				long v = cast (long) (sqrt (d * 1.0));
				long w = v * 1L * v;
				if (d > w + v)
				{
					long e = w + v * 2 + 1;
					long add = cast (long) (e - d);
					cur += add;
					delta += add;
					lo += add;
				}
				else
				{
					hi = min (hi,
					    lo + cast (long) (w + v - d));
				}
				if (lo > hi)
				{
					swap (a[0], a[i]);
					break;
				}
			}
			if (lo <= hi)
			{
				break;
			}
			cur = (base + 1L) ^^ 2;
		}
		writeln (cur - sel);
	}
}

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

immutable long limit = 2 * 10 ^^ 6 + 5;

void main ()
{

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto used = new bool [limit];
		auto a = readln.splitter.map !(to !(long)).array;

		rndGen.seed (263_473_470);
		long sel = a.front;
		long cur = sel;
		a.popFront ();
		randomShuffle (a);
		while (true)
		{
			long base = cast (long) (sqrt (cur * 1.0));
			long lo = cast (long) (cur - base * 1L * base);
			long hi = base;
			long delta = cur - sel;
			foreach (ref c; a)
			{
				long d = c + delta;
				long v = cast (long) (sqrt (d * 1.0));
				long w = v * 1L * v;
				if (d > w + v)
				{
					long e = w + v * 2 + 1;
					long add = cast (long) (e - d);
					cur += add;
					delta += add;
					lo += add;
				}
				else
				{
					hi = min (hi, cast (long) (w + v - d));
				}
				if (lo > hi)
				{
					break;
				}
			}
			if (lo <= hi)
			{
				break;
			}
			cur = (base + 1L) ^^ 2;
		}
		writeln (cur - sel);
	}
}

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

immutable int limit = 2 * 10 ^^ 6 + 5;

void main ()
{

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto used = new bool [limit];
		auto a = readln.splitter.map !(to !(int)).array;

		rndGen.seed (263_473_470);
		long sel = a.front;
		long cur = sel;
		a.popFront ();
		randomShuffle (a);
		while (true)
		{
			int base = cast (int) (sqrt (cur * 1.0));
			int lo = cast (int) (cur - base * 1L * base);
			int hi = base;
			long delta = cur - sel;
			debug {writefln !("considering %s [%s..%s]")
			    (cur, lo, hi);}
			foreach (ref c; a)
			{
				long d = c + delta;
				int v = cast (int) (sqrt (d * 1.0));
				long w = v * 1L * v;
				debug {writefln
				    !("lo = %s, %s -> %s in [%s..%s]")
				    (lo, c, d, w, w + v);}
				if (d > w + v)
				{
					long e = w + v * 2 + 1;
					int add = cast (int) (e - d);
					debug {writeln ("add ", add);}
					cur += add;
					debug {writeln ("cur = ", cur);}
					delta += add;
					lo += add;
				}
				else
				{
					hi = min (hi, cast (int) (w + v - d));
				}
				if (lo > hi)
				{
					break;
				}
			}
			if (lo <= hi)
			{
				break;
			}
			cur = (base + 1L) ^^ 2;
			debug {writeln ("cur = ", cur);}
		}
		writeln (cur - sel);
	}
}


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

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

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

    int [int] cnt;

    arr.each!(x => ++cnt[x]);

    int[] ans;
    foreach (i; 0 .. n) {
        auto cur = cnt.keys.maxElement();
        ans ~= cur;
        cnt[cur] -= 1;
        if (cnt[cur] == 0) cnt.remove(cur);

        foreach (j; 0 .. i) {
            auto now = gcd(ans[i], ans[j]);
            cnt[now] -= 2;
            if (cnt[now] == 0) cnt.remove(now);
        }
    }

    ans.sort();
    //debug { ans.writeln; }
    ans.writefln!("%(%s %)");
}

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;

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		sort (a);
		int [] r;
		foreach (i; 0..n)
		{
			auto cur = a.minPos !(q{a > b}).front;
			int [] b = [cur];
			foreach (p; r)
			{
				b ~= gcd (p, cur);
			}
			b ~= b;
			b ~= int.max;
			sort (b);
			for (int j = 0, p = 0; j < a.length; j++)
			{
				if (b.front == a[j])
				{
					b.popFront ();
				}
				else
				{
					a[p] = a[j];
					p++;
				}
			}
			a.length -= r.length * 2 + 1;
			r ~= cur;
		}
		writefln ("%(%s %)", r);
	}
}

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

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

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

    int [int] cnt;

    arr.each!(x => ++cnt[x]);

    int[] ans;
    foreach (i; 0 .. n) {
        auto cur = cnt.keys.maxElement();
        ans ~= cur;
        cnt[cur] -= 1;
        if (cnt[cur] == 0) cnt.remove(cur);

        foreach (j; 0 .. i) {
            auto now = gcd(ans[i], ans[j]);
            cnt[now] -= 2;
            if (cnt[now] == 0) cnt.remove(cur);
        }
    }

    ans.sort();
    //debug { ans.writeln; }
    ans.writefln!("%(%s %)");
}

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

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

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

    int [int] cnt;

    arr.each!(x => ++cnt[x]);

    int[] ans;
    foreach (i; 0 .. n) {
        auto cur = cnt.keys.maxElement();
        ans ~= cur;
        cnt[cur] -= 1;
        if (cnt[cur] == 0) cnt.remove(cur);

        foreach (j; 0 .. i) {
            auto now = gcd(ans[i], ans[j]);
            cnt[now] -= 2;
            if (cnt[now] == 0) cnt.remove(cur);
        }
    }

    //debug { ans.writeln; }
    ans.writefln!("%(%s %)");
}


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 solve() {
    int N = read!int;
    auto A = readarray!int;
    sort(A);
    bool f() {
        for (int i = 0; i + 1 < N; i++) {
            if (A[i] == A[i + 1]) return false;
        }
        return true;
    }
    writeln(f() ? "YES" : "NO");
}

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) {
    long n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!long).array.sort.array;
    long prev = 0;
    bool good = true;
    foreach (x ; a) {
      if (x == prev) {
        good = false;
        break;
      }
      prev = x;
    }
    writeln(good ? "YES" : "NO");
  }
}

import std;

void main()
{
    int t;
    scanf("%d\n", &t);
    foreach(_; 0..t) {
        int n;
        scanf("%d\n", &n);
        auto arr = readln.split.to!(long[]);
        if (arr.length == 1)
            writeln("YES");
        else {
            if (arr.length == uniq(arr.sort).array.length)
                writeln("YES");
            else
                writeln("NO");
        }
    }
}

import std;

void main()
{
    int t;
    scanf("%d\n", &t);
    foreach(_; 0..t) {
        int n;
        scanf("%d\n", &n);
        auto arr = readln.split.to!(long[]);
        if (arr.length == 1)
            writeln("YES");
        else {
            if (arr.length == uniq(arr).array.length)
                writeln("YES");
            else
                writeln("NO");
        }
    }
}


import std.algorithm.searching : maxElement;
import std.range : generate, takeExactly;
import std.stdio : readf, readln, writeln;
import std.string : strip;

void main()
{
  int n, m; // number of students, number of questions
  readf!"%d %d\n"(n, m);
  auto s = new string[n];
  foreach (i; 0..n) s[i] = readln.strip;

  auto total = 0;

  // read points for each question
  foreach (i; 0..m) {
    int a;
    char c;
    readf!"%d%c"(a, c);

    auto counts = new int[26];

    foreach (j; 0..n) {
      counts[s[j][i] - 'A']++;
    }

    total += counts.maxElement * a;
  }

  writeln(total);
}

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

void main() {
  int n,m;
  readln.strip.formattedRead(" %s %s", n, m);

  string[] ans;

  foreach (i; 0 .. n) {
    ans ~= readln.strip;
  }

  int[] maxAns;
  foreach (i; 0 .. m) {
    int[26] cnt = 0;
    foreach (j; 0 .. n) {
      cnt[ans[j][i]-'A']++;
    }
    maxAns ~= cnt[].maxElement;
  }

  auto point = readln.split.map!(a => parse!int(a));

  int sum = 0;
  foreach (i; 0 .. m) {
    sum += maxAns[i] * point[i];
  }

  writeln(sum);
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto S = N.iota.map!(_ => readln.chomp).array;
    auto A = readln.split.map!(to!long).array;
    auto cnt = new long[](5);
    long ans = 0;

    foreach (i; 0..M) {
        cnt[] = 0;
        foreach (j; 0..N) cnt[S[j][i]-'A'] += 1;
        ans += cnt.reduce!max * A[i];
    }

    ans.writeln;
}


#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;

void main()
{
    for(string S; (S=readln().chomp()).length; )
    {
        auto nm = S.split().map!(to!int)();
        auto n = nm[0], m = nm[1];
        auto d = new int[3][m];
        foreach(ref a;d)
            a[] = array(readln().split().map!(to!int)().map!("a-1")());
        auto c = new int[n];
        foreach(ref a;d)
        {
            auto o = 0;
            foreach(i;0..3)
                if(c[a[i]]!=0)
                    o=c[a[i]]+2-i;
            foreach(i;0..3)
                c[a[i]]=(i+o)%3+1;
        }
        writeln(c.map!(to!string)().join(" "));
    }
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto F = new int[][M];
    foreach (i; 0 .. M) {
        auto s = readln.chomp.split(" ").map!(to!int).array;
        foreach (ref e; s) e--;
        F[i] = s;
    }

    auto X = new int[N];
    foreach (i; 0 .. M) {
        auto used = new bool[4];
        foreach (j; 0 .. 3) {
            int p = F[i][j];
            if (X[p] != 0) {
                used[ X[p] ] = true;
            }
        }
        foreach (j; 0 .. 3) {
            int p = F[i][j];
            if (X[p] != 0) continue;
            foreach (int k; 1 .. 4) {
                if (!used[k]) {
                    X[p] = k;
                    used[k] = true;
                    break;
                }
            }
        }
    }
    writefln("%(%s %)", X);
}


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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto q = RD!int;
	auto ans = new long[](q);
	foreach (i; 0..q)
	{
		auto n = RD!int;
		if (n == 2)
		{
			ans[i] = 2;
		}
		else
			ans[i] = n % 2 == 0 ? 0 : 1;
	}

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

import std.stdio;
int main()
{
	int t=0;
	readf(" %d", &t);
	for (int i=0; i<t; i++)
	{
		int a=0;
		readf(" %d", &a);
		if (a<4)
		{
			writeln(4-a);
		}
		else
		{
			if (a%2==0)
			{
				writeln(0);
			}
			else
			{
				writeln(1);
			}
		}
	}
	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.ascii, std.typecons;

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

    while (t--) {
        int r, b, k;
        readf("%s %s %s", &r, &b, &k);
        readln;

        if (r > b) { swap(r, b); }

        auto g = gcd(r, b);

        r /= g;
        b /= g;

        auto m = (b-1+r-1) / r;

        debug { writeln(m, ' ', g); }

        writeln(m < k ? "OBEY" : "REBEL");
    }
}

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

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

void solve(){
	int t = rint;
	foreach(_; 0 .. t){
		string ans = "OBEY";
		long r = rlong, b = rlong, k = rlong;
		long g = gcd(r, b);
		r /= g, b /= g;
		if(r > b && (r - 2) / b + 1 >= k) ans = "REBEL";
		if(b > r && (b - 2) / r + 1 >= k) ans = "REBEL";
		ans.writeln;
	}

}
long gcd(long a, long b){
	if(a < 0) a = -a;
	if(b == 0) return a;
	if(b < 0) return gcd(a, -b);
	if(a % b == 0) return b;
	if(a < b) return gcd(b, a);
	else return gcd(b, a % b);
}

import std.stdio;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]))
      foreach(ref e; args[i])
	read(e);
    else
      readf!" %s"(args[i]);
}
alias r = read;
alias w = writeln;

int main()
{
  int t;
  r(t);
  foreach(tc; 0 .. t)
    {
      long red, b, k;
      r(red, b, k);
      import std.numeric: gcd;
      long g = gcd(red, b);
      red /= g;
      b /= g;
      if (red * (k - 1) < (b - 1) || b * (k - 1) < (red - 1))
	{
	  w("REBEL");
	}
      else
	{
	  w("OBEY");
	}
    }
  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.ascii, std.typecons;

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

    while (t--) {
        int r, b, k;
        readf("%s %s %s", &r, &b, &k);
        readln;

        if (r > b) { swap(r, b); }

        auto g = gcd(r, b);

        auto m = b / r;

        if (g == r && r != b) { --m; }

        debug { writeln(m, ' ', g); }

        writeln(m < k ? "OBEY" : "REBEL");
    }
}

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

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

    while (t--) {
        int r, b, k;
        readf("%s %s %s", &r, &b, &k);
        readln;

        if (r > b) { swap(r, b); }

        auto g = gcd(r, b);

        auto m = b / r;

        debug { writeln(m, ' ', g); }

        writeln(m < k ? "OBEY" : "REBEL");
    }
}

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

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

void solve(){
	int t = rint;
	foreach(_; 0 .. t){
		string ans = "OBAY";
		long r = rlong, b = rlong, k = rlong;
		long g = gcd(r, b);
		r /= g, b /= g;
		if(r > b && (r - 2) / b + 1 >= k) ans = "REBEL";
		if(b > r && (b - 2) / r + 1 >= k) ans = "REBEL";
		ans.writeln;
	}

}
long gcd(long a, long b){
	if(a < 0) a = -a;
	if(b == 0) return a;
	if(b < 0) return gcd(a, -b);
	if(a % b == 0) return b;
	if(a < b) return gcd(b, a);
	else return gcd(b, a % b);
}


void main() { runSolver(); }

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

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto A = scan!int(N);

      auto mi = A.minElement;
      return A.sum - mi*N;
    }

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

  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"];

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

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

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

void main() {
    int t, n, m;
    readf("%d\n", t);
    while(t--) {
		readln;
		int sum;
        auto b = rtln!int;
		int min = b.minElement;
		foreach (x; b)
				sum += (x - min);
		writeln(sum);
    }
}

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 n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (a.sum - a.minElement * n);
	}
}


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 n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		long [] [] s = [[0], [0], [0]];
		foreach (i, c; a)
		{
			s[0] ~= s[0][$ - 1] + !(i & 1) * c;
			s[1] ~= s[1][$ - 1] + (i & 1) * c;
			s[2] ~= s[1][$ - 1] - s[0][$ - 1];
		}
		long res = 0;
		long [2] lo = [long.max / 2, 0];
		foreach (i; 0..n)
		{
			lo[i & 1] = min (lo[i & 1], s[2][i + 1]);
			res = max (res, s[2][i + 1] - lo[i & 1]);
		}
		writeln (res + s[0].back);
	}
}

import std.stdio;
import std.container;
import std.range;
import std.algorithm;

long calculate_max_sum(long[] arr) {
	long current = 0;
	long best_res = 0;
	int left = 0;

	for (int i = 0; i < arr.length; i++) {
		current += arr[i];
		while (left <= i && arr[left] < 0) {
			current -= arr[left];
			left++;
		}

		if (current < 0) {
			current = 0;
			left = i + 1;
		}

		best_res = max(best_res, current);
	}

	return best_res;
}

void main() {
	int t;
	scanf("%d", &t);

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

		long[] arr = new long[n];
		foreach (i; iota(n)) scanf("%lld", &arr[i]);

		// calculate the sum for even elems
		long sum = 0;
		foreach (i, e; arr) sum += ((i + 1) % 2) * e;

		// sum-diff-array starting at first element
		long[] a = new long[n / 2];
		for (int i = 0; i + 1 < n; i += 2) {
			a[i / 2] = arr[i + 1] - arr[i];
		}

		// starting at second
		int n_odd = (n - 1) / 2;
		long[] b = new long[n_odd];
		for (int i = 1; i + 1 < n; i += 2) {
			b[i / 2] = arr[i] - arr[i + 1];
		}

		writeln(sum + max(calculate_max_sum(a), calculate_max_sum(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)); }

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

		auto b = new long[](n+1);
		foreach (i; 0..n)
		{
			if (i % 2)
			{
				b[i+1] = b[i] - a[i];
			}
			else
			{
				b[i+1] = b[i] + a[i];
			}
		}

		auto c0 = new long[](n+1);
		auto c1 = new long[](n+1);
		c0[] = long.max;
		c1[] = long.max;
		long tot;
		foreach_reverse (i; 0..n)
		{
			if (i % 2)
			{
				c1[i] = min(c1[i+1], b[i+1]);
				c0[i] = c0[i+1];
			}
			else
			{
				c0[i] = min(c0[i+1], b[i+1]);
				c1[i] = c1[i+1];
				tot += a[i];
			}
		}
		debug writeln(b);
		debug writeln(c0);
		debug writeln(c1);

		ans[ti] = tot;
		foreach (i; 0..n-1)
		{
			long x;
			if (i % 2)
			{
				x = -(c0[i+1] - b[i]);
				debug writeln("i:", i, " ", c0[i+1], " ", b[i]);
			}
			else
			{
				x = -(c1[i+1] - b[i]);
				debug writeln("i:", i, " ", c1[i+1], " ", b[i]);
			}

			ans[ti].chmax(tot+x);
			debug writeln("i:", i, " ", ans[ti]);
		}
	}

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


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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		int [] [] a;
		foreach (i; 0..n)
		{
			a ~= readln.split.map !(to !(int)).array;
		}

		auto p = new int [n];
main_loop:
		foreach (i; 0..n)
		{
			foreach (j; 0..n)
			{
				foreach (k; j + 1..n)
				{
					if (a[i][j] == a[i][k])
					{
						p[i] = a[i][j];
						continue main_loop;
					}
				}
			}
		}

		int v = n - 1;
		foreach (i; 0..n)
		{
			if (p[i] == 0)
			{
				p[i] = v;
				v++;
			}
		}

		writefln ("%(%s %)", p);
	}
}

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

void main() {
    int n = readln.chomp.to!int;
    int[][] t;
    for (int i = 0; i < n; i++) {
        t ~= readln.chomp.split.map!(to!int).array;
    }

    int[] ans;
    foreach (e; t) {
        ans ~= e.minPos!("a > b")[0];
    }

    ans[ans.length - ans.minPos!("a > b").length] = ans.length.to!int;

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

import std.stdio;
int n,i,j,k;
int[50][50] v;
int[50] ans;
void main()
{
	scanf(" %d",&n);
	for(i=0;i<n;++i)
	{
		for(j=0;j<n;++j)
		{
			scanf(" %d",&v[i][j]);
		}
	}
	for(k=1;k<=n;++k)
	{
		int cnt;
		for(i=0;i<n;++i)
		{
			if(ans[i]) continue;
			cnt=0;
			for(j=0;j<n;++j) if(v[i][j]==k) ++cnt;
			if(cnt==n-1) break;
		}
		ans[i]=k;
		for(i=0;i<n;++i) for(j=0;j<n;++j) if(v[i][j]==k) v[i][j]=k+1;
	}
	for(i=0;i<n;++i) writef("%d ",ans[i]);
	writeln();
}