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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto s = RDA;
		s.sort();
		ans[ti] = long.max;
		foreach (i; 1..n)
		{
			ans[ti].chmin(s[i] - s[i-1]);
		}
	}

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

import std.stdio;
import std.string : split, strip;
import std.algorithm : map, sort, minElement;
import std.conv : to;
import std.array;
import std.range : dropOne, zip;

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

  foreach (i; 0..t) {
    int n;
    readf!"%d\n"(n);
    auto a = readln.strip.split.map!(to!int).array;
    a.sort;

    writeln(minElement(a.zip(a.dropOne).map!"a[1] - a[0]"));
  }
}

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;
  @Dim("n") long[] s;

  void solve(long tc = -1)
  {
    sort(s);
    writeln(iota(0, n - 1).map!(i => abs(s.at(i) - s.at(i + 1))).fold!min(long.max));
  }
}

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;
import std.conv;
import std.string;
import std.algorithm;
import std.algorithm.searching;
import std.algorithm.sorting;
import std.math;

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

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n, m;
        readf!"%d %d\n"(n, m);

        string s;
        readf!"%s\n"(s);
        char[] instructions = s.dup;

        int x = 0;
        int y = 0;
        int min_x = 0;
        int min_y = 0;
        int max_x = 0;
        int max_y = 0;
        int x_size = 1;
        int y_size = 1;
        int offset_x = 0;
        int offset_y = 0;

        for (int i = 0; i < instructions.length; i++) {
            switch (instructions[i]) {
                case 'R':
                    x ++;
                    break;
                case 'L':
                    x --;
                    break;
                case 'D':
                    y ++;
                    break;
                case 'U':
                    y --;
                    break;
                default:
                    break;
            }

            bool xflag = false;
            bool yflag = false;
            if (min_x > x) {
                xflag = true;
                min_x = x;
            }
            if (min_y > y) {
                yflag = true;
                min_y = y;
            }
            if (max_x < x) {
                max_x = x;
            }
            if (max_y < y) {
                max_y = y;
            }

            x_size = max_x - min_x + 1;
            y_size = max_y - min_y + 1;
            // writeln(to!string(instructions[i]) ~ " " ~ to!string(x_size) ~ " " ~ to!string(y_size));
            if (x_size > m) break;
            if (y_size > n) break;

            if (xflag) offset_x += 1;
            if (yflag) offset_y += 1;
        }

        writeln(to!string(offset_y + 1) ~ " " ~ to!string(offset_x + 1));
    }
}

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

immutable int dirs = 4;
immutable int  [dirs] dRow  = [ -1,   0,  +1,   0];
immutable int  [dirs] dCol  = [  0,  -1,   0,  +1];
immutable char [dirs] dName = ['U', 'L', 'D', 'R'];

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int rows, cols;
		readf !(" %s %s") (rows, cols);
		readln;
		auto s = readln.strip;
		int rLo = 0;
		int rHi = 0;
		int cLo = 0;
		int cHi = 0;
		int r = 0;
		int c = 0;
		foreach (ref cur; s)
		{
			auto dir = dName[].countUntil (cur);
			r += dRow[dir];
			c += dCol[dir];
			auto trLo = min (rLo, r);
			auto trHi = max (rHi, r);
			auto tcLo = min (cLo, c);
			auto tcHi = max (cHi, c);
			if (trHi - trLo >= rows)
			{
				break;
			}
			if (tcHi - tcLo >= cols)
			{
				break;
			}
			rLo = trLo;
			rHi = trHi;
			cLo = tcLo;
			cHi = tcHi;
		}
		writeln (1 - rLo, " ", 1 - cLo);
	}
}

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

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

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n, m;
        readf!"%d %d\n"(n, m);

        string s;
        readf!"%s\n"(s);
        char[] instructions = s.dup;

        int x = 0;
        int y = 0;
        int min_x = 0;
        int min_y = 0;
        int max_x = 0;
        int max_y = 0;
        int x_size = 1;
        int y_size = 1;
        int offset_x = 0;
        int offset_y = 0;

        for (int i = 0; i < instructions.length; i++) {
            switch (instructions[i]) {
                case 'R':
                    x ++;
                    break;
                case 'L':
                    x --;
                    break;
                case 'D':
                    y ++;
                    break;
                case 'U':
                    y --;
                    break;
                default:
                    break;
            }

            bool xflag = false;
            bool yflag = false;
            if (min_x > x) {
                xflag = true;
                min_x = x;
            }
            if (min_y > y) {
                yflag = true;
                min_y = y;
            }
            if (max_x < x) {
                max_x = x;
            }
            if (max_y > y) {
                max_y = y;
            }

            x_size = max_x - min_x + 1;
            y_size = max_y - min_y + 1;
            if (x_size > m) break;
            if (y_size > n) break;

            if (xflag) offset_x += 1;
            if (yflag) offset_y += 1;
        }

        writeln(to!string(offset_y + 1) ~ " " ~ to!string(offset_x + 1));
    }
}


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

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

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

    immutable int MX = 22;

    auto cnt = new int[] (MX);
    foreach (e; arr) {

        foreach (b; 0 .. MX) {
            if (e & (1 << b)) { cnt[b] += 1; }
        }
    }

    auto ans = 0L;
    foreach (i; 1 .. n+1) {
        int cur = 0;
        foreach (b; 0 .. MX) {
            if (cnt[b] >= i) { cur += (1 << b); }
        }

        ans += cur.to!long * cur;
    }

    ans.writeln;
}

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

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

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

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

enum E = 25;

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

      auto bs = new long[N];
      foreach (e; 0 .. E) {
        int cnt;
        foreach (i; 0 .. N) {
          if ((A[i] >> e) & 1) {
            ++cnt;
          }
        }
        debug {
          writeln(e, ": ", cnt);
        }
        foreach (i; 0 .. cnt) {
          bs[i] |= 1L << e;
        }
      }

      long ans;
      foreach (i; 0 .. N) {
        ans += bs[i]^^2;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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 n = RD!int;
	auto a = RDA;

	auto cnt = new int[](20);
	foreach (i; 0..n)
	{
		foreach (j; 0..20)
		{
			auto bit = 1L << j;
			if (a[i] & bit)
			{
				++cnt[j];
			}
		}
	}

	long ans;
	foreach (i; 0..n)
	{
		long t;
		foreach (j; 0..20)
		{
			if (cnt[j])
			{
				t += 1L << j;
				--cnt[j];
			}
		}
		ans += t^^2;
	}

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

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

immutable int bits = 20;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int [bits] s;
		foreach (k; 0..bits)
		{
			s[k] = a.count !(x => (x & (1 << k)) != 0).to !(int);
		}

		long res = 0;
		foreach (i; 0..n)
		{
			int cur = 0;
			foreach (k; 0..bits)
			{
				if (s[k] > i)
				{
					cur |= (1 << k);
				}
			}
			res += cur * 1L * cur;
		}
		writeln (res);
	}
}

// Code By H~$~C

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

void _Main() {
  int n;
  readf!" %d\n"(n);
  int[] a = readln.splitter.map!(to!int).array;
  int[][20] b;
  foreach(j; 0 .. 20) {
    b[j] = new int[n];
  }
  foreach(i; 0 .. n) {
    foreach(j; 0 .. 20) {
      b[j][i] = ((a[i] >> j) & 1);
    }
  }
  foreach(j; 0 .. 20) {
    b[j].sort!"a > b";
  }
  long ans = 0;
  foreach(i; 0 .. n) {
    long x = 0;
    foreach(j; 0 .. 20) {
      x += ((b[j][i]) << j);
    }
    ans += x * x;
  }
  writeln(ans);
}

void main(string[] args) {
  int tests = 1;
//  readf!" %d"(tests);
  foreach (test; 0 .. tests) _Main();
}

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

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

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

    foreach (i; 0 .. n-1) {
        auto le = arr[i] & arr[i+1];
        auto r = arr[i] | arr[i+1];

        arr[i] = le;
        arr[i+1] = r;
    }

    auto ans = arr.map!(x => x.to!long * x).sum(0L);

    ans.writeln;
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto a = RDA;

	a.sort!"a > b";
	auto zero = new int[][](20);
	foreach (i; 0..n)
	{
		foreach (j; 0..20)
		{
			auto bit = 1L << j;
			if (!(a[i] & bit))
			{
				zero[j] ~= i;
			}
		}
	}

	foreach_reverse (i; 0..n)
	{
		auto x = a[i];
		foreach_reverse (j; 0..20)
		{
			auto bit = 1L << j;
			if (x & bit)
			{
				int pos = n;
				while (!zero[j].empty)
				{
					if (pos < i) break;
					pos = zero[j].front; zero[j].popFront;
				}
				if (pos == n) continue;

				auto y = a[pos] & a[i];
				a[pos] |= a[i];
				a[i] = y;
			}
		}
	}

	long ans;
	foreach (i; 0..n)
		ans += a[i]^^2;

	writeln(ans);
	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)); }

//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 n = RD!int;
	auto a = RDA;

	a.sort!"a > b";
	auto zero = new int[][](20);
	foreach (i; 0..n)
	{
		foreach (j; 0..20)
		{
			auto bit = 1L << j;
			if (!(a[i] & bit))
			{
				zero[j] ~= i;
			}
		}
	}

	foreach_reverse (i; 0..n)
	{
		auto x = a[i];
		bool[int] used;
		foreach_reverse (j; 0..20)
		{
			auto bit = 1L << j;
			if (x & bit)
			{
				int pos = n;
				while (!zero[j].empty)
				{
					if (pos < i) break;
					pos = zero[j].front; zero[j].popFront;
				}
				if (pos == n) continue;
				if (used.get(pos, false)) continue;
				used[pos] = true;
				auto y = a[pos] & a[i];
				a[pos] |= a[i];
				a[i] = y;
			}
		}
	}

	long ans;
	foreach (i; 0..n)
		ans += a[i]^^2;

	writeln(ans);
	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.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

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

size_t binarySearch(alias pred, T)(in T[] arr, bool isLower = true)
{
	long ok, ng;
	if (isLower) { ok = arr.length; ng = -1; }
	else		 { ok = 0; ng = arr.length; }
	while (abs(ok-ng) > 1)
	{
		size_t mid = cast(size_t)(ok+ng) / 2;
		if (unaryFun!pred(arr[mid]))
			ok = mid;
		else ng = mid;
	}
	return cast(size_t)ok;
}

void main()
{
	auto n = RD!int;
	auto s = RD!string;
	auto cnt = new int[][](s.length+1, 26);
	foreach (i; 0..s.length)
	{
		auto c = cnt[i].dup;
		++c[s[i]-'a'];
		cnt[i+1] = c;
	}
	auto m = RD!int;
	foreach (i; 0..m)
	{
		auto t = RD!string;
		auto cnt2 = new int[](26);
		foreach (c; t)
		{
			++cnt2[c-'a'];
		}
		bool check(in int[] cnt1)
		{
			foreach (j; 0..cnt1.length)
			{
				if (cnt1[j] < cnt2[j])
					return false;
			}
			return true;
		}
		auto pos = binarySearch!(check)(cnt);
		writeln(pos);
	}

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

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

int cvt (dchar c) {
  return (c.to!int - 97);
}

void main() {
  immutable n = readln.strip.to!int;
  string s = readln.strip;
  auto idx = new int[][26];
  foreach (i; 0 .. n) {
    int o = cvt (s[i]);
    idx[o] ~= i + 1;
  }
  immutable m = readln.strip.to!int;
  debug stderr.writeln (m);
  foreach (qid; 0 .. m) {
    string t = readln.strip;
    int[26] c;
    foreach (ch; t) {
      ++c[cvt (ch)];
    }
    int res;
    foreach (i; 0 .. 26) {
      if (c[i] > 0) {
        res = max (res, idx[i][c[i]-1]);
      }
    }
    writeln (res);
  }
}

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

alias T = Tuple!(int, int);

int cvt (dchar c) {
  return 1 << (c.to!int - 48);
}

void main() {
  immutable n = readln.strip.to!int;
  string s = readln.strip;
  T[] h;
  int mask;
  foreach (i; 0 .. n) {
    int o = mask | cvt (s[i]);
    if (o != mask) {
      mask = o;
      h ~= tuple (mask, i + 1);
    }
  }
  immutable m = readln.strip.to!int;
  foreach (qid; 0 .. m) {
    string t = readln.strip;
    mask = t.fold! ( (acc, x) => acc | cvt (x))(0);
    foreach (p; h) {
      if ((p[0] & mask) == mask) {
        writeln (p[1]);
        break;
      }
    }
  }
}

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

alias T = Tuple!(int, int);

int cvt (dchar c) {
  return 1 << (c.to!int - 97);
}

void main() {
  immutable n = readln.strip.to!int;
  string s = readln.strip;
  T[] h;
  int mask;
  foreach (i; 0 .. n) {
    int o = mask | cvt (s[i]);
    if (o != mask) {
      mask = o;
      h ~= tuple (mask, i + 1);
    }
  }
  immutable m = readln.strip.to!int;
  foreach (qid; 0 .. m) {
    string t = readln.strip;
    mask = t.fold! ( (acc, x) => acc | cvt (x))(0);
    foreach (p; h) {
      if ((p[0] & mask) == mask) {
        writeln (p[1]);
        break;
      }
    }
  }
}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto n = RD!int;
	auto s = RD!string;
	auto m = RD!int;
	foreach (i; 0..m)
	{
		auto t = RD!string;
		bool[char] set;
		foreach (c; t)
		{
			set[c] = true;
		}
		auto len = set.keys.length;
		foreach (j, c; s)
		{
			if (set.get(c, false))
			{
				--len;
				set[c] = false;
				if (len == 0)
				{
					writeln(j+1);
					break;
				}
			}
		}
	}

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


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

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

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

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

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

      auto fs = new int[1 << 4];
      foreach (i; 0 .. N) {
        int x;
        foreach (k; 0 .. K) {
          x |= (A[i][k] ^ 1) << k;
        }
        foreach (k; K .. 4) {
          x |= 1 << k;
        }
        ++fs[x];
      }
      debug {
        writeln("fs = ", fs);
      }

      bool ans;
      if (fs[(1 << 4) - 1] > 0) {
        ans = true;
      }
      foreach (p; 0 .. 1 << 4) foreach (q; p + 1 .. 1 << 4) {
        if (fs[p] > 0 && fs[q] > 0) {
          if ((p | q) == (1 << 4) - 1) {
            ans = true;
          }
        }
      }
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}

import std.algorithm, std.conv, std.range, std.stdio;
void main () {
	readln;
	int [int] s;
	string t;
	while ((t = readln.split.join) > "") s[t.to!int (2)]++;
	writeln (s.byKey.any !(x => s.byKey.any !(y => !(x & y))) ? "YES" : "NO");
}

import std.algorithm, std.conv, std.range, std.stdio;
void main () {
	int n, k;
	readf (" %s %s ", &n, &k);
	auto m = 1 << k, d = new int [m];
	foreach (i; 0..n) d[readln.split.join.to!int (2)]++;
	writeln (m.iota.any !(x => m.iota.any !(y => d[x] && d[y] && !(x & y))) ? "YES" : "NO");
}

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

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto k2 = 1 << k;
		auto d = new int [k2];
		foreach (i; 0..n)
		{
			auto a = readln.split.join.to !(int) (2);
			d[a] += 1;
		}
		debug {writeln (d);}

		bool res = false;
		foreach (i; 0..k2)
		{
			foreach (j; 0..k2)
			{
				if (d[i] && d[j] && !(i & j))
				{
					res = true;
				}
			}
		}
		writeln (res ? "YES" : "NO");
	}
}

import std.algorithm, std.conv, std.range, std.stdio;
void main () {
	readln;
	int [int] s;
	string t;
	while ((t = readln.split.join) > "") s[t.to!int]++;
	writeln (s.byKey.any !(x => s.byKey.any !(y => !(x & y))) ? "YES" : "NO");
}


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

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

void solve() {
    int n;
    char r;
    readf!" %d %c "(n, r);
    //scanf("%d %c\n", &n, &r);
    auto s = readln.strip;
    if (r == 'g') {
        writeln(0);
        return;
    }
    long fromStart;
    bool foundG, countR;
    long tmp, maxD;
    foreach(i, el; s) {
        if (el == 'g') {
            foundG = true;
            if (tmp > maxD)
                maxD = tmp;
            countR = false;
            tmp = 0;
        }
        if (el == r)
            countR = true;
        if (countR)
            tmp++;
        if (!foundG)
            fromStart++;
    }
    if (countR)
        writeln(max(maxD, tmp+fromStart));
    else
        writeln(maxD);

}

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

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

// Lengths of sequences satisfying the condition (moving to the right)
int[] seq_length_right(alias less = "a < b", T)(T[] a)
{
  if (a.length == 0)
    return new int[](0);
  auto tmp = new int[](a.length);
  int count = 1;
  tmp[$ - 1] = count;
  foreach_reverse (i ; 0 .. a.length - 1) {
    if (binaryFun!less(a[i], a[i + 1]))
      count++;
    else
      count = 1;
    tmp[i] = count;
  }
  return tmp;
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto tmp = readln.split.array;
    auto n = tmp[0].to!long;
    auto ch = tmp[1].strip[0];
    auto s = readln.strip;
    auto ss = s ~ s;
    if (ch == 'g') {
      writeln(0);
      continue;
    }
    auto a = ss.seq_length_right!((a, b) => a != 'g')();
    long ans = 0;
    foreach (i ; 0 .. a.length) {
      if (ss[i] == ch)
        ans = max(a[i], ans);
    }
    writeln(ans - 1);
  }
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti] = 1;
		auto dp = new int[][](n, 21);
		foreach (i; 0..n)
			dp[i][] = int.max;
		foreach (i; 0..n/2)
		{
			dp[i][0] = s[i];
		}
		foreach (int to; 1..n+1)
		{
			int[] index;
			for (long from = 1; from*from <= to; ++from)
			{
				if (to % from) continue;
				index ~= cast(int)from;
				index ~= cast(int)(to / from);
			}
			foreach (from; index)
			{
				foreach (k; 0..20)
				{
					if (dp[from-1][k] < s[to-1])
					{
						dp[to-1][k+1].chmin(s[to-1]);
					}
				}
			}
		}
		(){
		foreach_reverse (i; 0..21)
		{
			foreach (j; 0..n)
			{
				if (dp[j][i] != int.max)
				{
					ans[ti] = i+1;
					return;
				}
			}
		}}();
	}

	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!int;
        auto ss = readln.split.to!(int[]);
        auto DP = new int[](N+1);
        foreach_reverse (i; 1..N+1) {
            int x = i * 2, n = 1;
            while (x <= N) {
                if (ss[x-1] > ss[i-1]) n = max(n, DP[x] + 1);
                x += i;
            }
            DP[i] = n;
        }
        int r;
        foreach (ref e; DP) r = max(r, e);
        writeln(r);
    }
}


import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.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, m, k;
        readf("%s %s %s", &n, &m, &k);
        readln;

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

        bool ok = true;
        foreach (a, b; lockstep(hs, hs.dropOne)) {
            if (a + m < b - k) {
                ok = false;
                break;
            }

            if (a > b - k) { m += a - max(b-k, 0); }
            else { m -= b-k - a; }
        }

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

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto m = RD;
		auto k = RD;
		auto h = RDA;
		bool tmp = true;
		foreach (j; 1..n)
		{
			auto d = k - (h[j] - h[j-1]);
			m += min(d, h[j-1]);
			if (m < 0)
			{
				tmp = false;
				break;
			}
		}
		ans[i] = tmp;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush();
	debug readln();
}

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

void solve(){
	int t = read.to!int;

A:
	foreach(_; 0 .. t){
		int n = read.to!int;
		long m = read.to!long;
		long k = read.to!long;
		long[] hs;
		foreach(i; 0 .. n) hs ~= read.to!long;
		log("t:", t, "n:", n, "m:", m, "k:", k, "hs:", hs);

		foreach(i; 0 .. n - 1){
			log("i:", i, "m:", m, "hs[i]:", hs[i], "hs[i + 1]:", hs[i + 1]);
			if(hs[i] + m < hs[i + 1] - k){
				"NO".writeln;
				continue A;
			}
			else{
				long h = hs[i + 1] - k;
				if(h < 0) h = 0;
				if(hs[i] > h) m += hs[i] - h;
				else m -= h - hs[i];
				log("h:", h, "m:", m);
			}
		}
		"YES".writeln;
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.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, m, k;
        readf("%s %s %s", &n, &m, &k);
        readln;

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

        bool ok = true;
        foreach (a, b; lockstep(hs, hs.dropOne)) {
            if (a + m < b - k) {
                ok = false;
                break;
            }

            if (a > b - k) { m += a - (b-k); }
            else { m -= b-k - a; }
        }

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

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto k = RD!int;
		auto h = RDA;
		bool tmp = true;
		foreach (j; 1..n)
		{
			auto d = k - (h[j] - h[j-1]);
			m += d;
			if (m < 0)
			{
				tmp = false;
				break;
			}
		}
		ans[i] = tmp;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	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.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 t = RD!int;
	auto ans = new bool[](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto m = RD;
		auto k = RD;
		auto h = RDA;
		bool tmp = true;
		foreach (j; 1..n)
		{
			auto d = k - (h[j] - h[j-1]);
			m += d;
			if (m < 0)
			{
				tmp = false;
				break;
			}
		}
		ans[i] = tmp;
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush();
	debug readln();
}


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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		auto q = readln.splitter.map !(to !(int)).array;
		p[] -= 1;
		q[] -= 1;
		auto rp = new int [n];
		auto rq = new int [n];
		foreach (i; 0..n)
		{
			rp[p[i]] = i;
			rq[q[i]] = i;
		}
		int [int] delta;
		foreach (j; 0..n)
		{
			delta[(n + rp[j] - rq[j]) % n] += 1;
		}
		delta.byValue.maxElement.writeln;
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto a = RDA;
	auto b = RDA;

	auto index1 = a.MAKE_IDX;
	auto index2 = b.MAKE_IDX;

	auto cnt = new long[](n);
	foreach (i; 0..n)
	{
		auto d = cast(long)index1[i] - cast(long)index2[i];
		if (d < 0)
			d = n + d;
		++cnt[cast(int)d];
	}
	auto ans = cnt[cnt.MIN_POS!"a > b"];

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

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

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


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

void main(string[] args)
{
  auto n = next!int;
  auto a = next!long(n);
  long mx = long.min;
  foreach(i; 0 .. n)
    {
      auto ai = a[i];
      foreach(j; i .. n)
	{
	  auto aij = ai | a[j];
	  foreach(k; j .. n)
	    mx = max(mx, aij | a[k]);
	}
    }
  mx.writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		long res = 0;
		foreach (i; 0..n)
		{
			res = max (res, a[i]);
			foreach (j; 0..n)
			{
				res = max (res, a[i] | a[j]);
				foreach (k; 0..n)
				{
					res = max (res, a[i] | a[j] | a[k]);
				}
			}
		}
		writeln (res);
	}
}


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

int q;

void main() {
    scan(q);

    int n;

    while (q--) {
        scan(n);
        solve(n).writeln;
    }
}

long solve(int n) {
    if (n <= 5) {
        return n - 1;
    }

    long btm = 2, top = n - 1, mid;

    while (top - btm > 1) {
        mid = (btm + top) / 2;

        if (n + (mid - 1) * (mid - 2) / 2L <= 2L * (n - 1 - mid)) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    return min(n - 1 + btm * (btm - 1) / 2L, 2L * (n - 1 - btm));
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

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

int q;

void main() {
    scan(q);

    int n;

    while (q--) {
        scan(n);
        solve(n).writeln;
    }
}

long solve(int n) {
    int btm = 2, top = n, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;
        long e = 1L * mid * (mid - 1) / 2L;

        (e <= n - mid ? btm : top) = mid;
    }

    long ans = max(n - btm + 1L * btm * (btm - 1) / 2L, 2L * (n - btm - 1));

    return ans;
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

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

int q;

void main() {
    scan(q);

    long n;

    while (q--) {
        scan(n);
        solve(n);
    }
}

void solve(long n) {
    long btm = 0, top = n*n, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;

        if (check(n, mid)) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    writeln(n - 1 + btm);
}

bool check(long n, long K) {
    long btm = 0, top = K*K, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;

        if (K <= mid * (mid + 1) / 2) {
            top = mid;
        }
        else {
            btm = mid;
        }
    }

    long tot = n - 1 + K;
    long bri = n - 1 - (top + 1);

    return 2*bri >= tot;
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}


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

  int[] n;
  auto memo = new long[][][][](11, 64, 1 << 10, 2);
  afill(memo, -1L);
  long f(const int base, int i, bool less, int used, bool zero) {
    if (i < 0) {
      return used == 0 && !zero;
    } else {
      if (less && memo[base][i][used][zero] >= 0) {
        return memo[base][i][used][zero];
      }
      long ret = 0;
      auto digit = less ? base - 1 : n[i];
      for (int d = 0; d <= digit; d++) {
        auto l = less || (d < digit);
        auto z = zero && (d == 0);
        auto u = used ^ (z ? 0 : (1 << d));
        ret += f(base, i - 1, l, u, z);
      }
      if (less) {
        return memo[base][i][used][zero] = ret;
      } else {
        return ret;
      }
    }
  }

  int q;
  rd(q);
  while (q--) {
    int b;
    long l, r;
    rd(b, l, r);
    long res = 0;
    n = digit(r, b).dup;
    res += f(b, (n.length - 1).to!(int), 0, 0, true);
    n = digit(l - 1, b).dup;
    res -= f(b, (n.length - 1).to!(int), 0, 0, true);
    writeln(res);
  }
}

int[] digit(long n, int base) {
  int[] ret;
  do {
    ret ~= n % base;
    n /= base;
  }
  while (n);
  return ret;
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

void afill(Range, Type)(Range r, Type value) {
  static if (is(typeof(r) == Type[])) {
    foreach (ref elem; r)
      elem = value;
  } else {
    foreach (ref arr; r)
      afill(arr, value);
  }
}

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

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

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

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

enum LIM_B = 10;
enum LIM_L = 100;

void main() {
  auto dp = new long[][][LIM_B + 1];
  foreach (B; 2 .. LIM_B + 1) {
    dp[B] = new long[][](LIM_L + 1, 1 << B);
    dp[B][0][0] = 1;
    foreach (l; 0 .. LIM_L) {
      foreach (p; 0 .. 1 << B) {
        foreach (x; 0 .. B) {
          dp[B][l + 1][p ^ 1 << x] += dp[B][l][p];
        }
      }
    }
  }

  long solve(const(int) B, const(long) N) {
    int[] as;
    for (long n = N; n; n /= B) {
      as ~= cast(int)(n % B);
    }
    as.reverse;
    const asLen = cast(int)(as.length);
    long ans;
    // short
    foreach (l; 1 .. asLen) {
      foreach (x; 1 .. B) {
        ans += dp[B][l - 1][1 << x];
      }
    }
    // as long
    int p;
    foreach (i; 0 .. asLen) {
      foreach (x; 0 .. as[i]) {
        if (i > 0 || x > 0) {
          debug {
            // writeln("  ", B, " ", asLen - 1 - i, " ", p ^ 1 << x, ": ", dp[B][asLen - 1 - i][p ^ 1 << x]);
          }
          ans += dp[B][asLen - 1 - i][p ^ 1 << x];
        }
      }
      p ^= 1 << as[i];
    }
    debug {
      writeln("solve ", B, " ", as, " = ", ans);
    }
    debug {
      long brt;
      foreach (n; 1 .. N) {
        int q;
        for (long nn = n; nn; nn /= B) {
          q ^= 1 << cast(int)(nn % B);
        }
        if (q == 0) {
          ++brt;
        }
      }
      writeln("brt = ", brt);
      assert(brt == ans);
    }
    return ans;
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const B = readInt();
        const L = readLong();
        const R = readLong();

        long ans;
        ans -= solve(B, L);
        ans += solve(B, R + 1);
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

  int[] n;
  auto memo = new long[][][][](11, 64, 1 << 10, 2);
  afill(memo, -1L);
  long f(const int base, size_t i, bool less, int used, bool zero) {
    if (i == n.length) {
      return used == 0 && zero;
    } else {
      if (less && zero && memo[base][n.length-i-1][used][zero] >= 0) {
        return memo[base][n.length-i-1][used][zero];
      }
      long ret = 0;
      auto digit = less ? base - 1 : n[i];
      for (int d = 0; d <= digit; d++) {
        auto l = less || (d < digit);
        auto z = zero || (d > 0);
        auto u = !z ? used : (used ^ (1 << d));
        ret += f(base, i + 1, l, u, z);
      }
      if (less && zero) {
        memo[base][n.length-i-1][used][zero] = ret;
      }
      return ret;
    }
  }

  int q;
  rd(q);
  while (q--) {
    int b;
    long l, r;
    rd(b, l, r);
    long res = 0;
    n = digit(r, b).dup;
    res += f(b, 0, 0, 0, 0);
    n = digit(l - 1, b).dup;
    res -= f(b, 0, 0, 0, 0);
    writeln(res);
  }
}

int[] digit(long n, int base) {
  int[] ret;
  do {
    ret = n % base ~ ret;
    n /= base;
  }
  while (n);
  return ret;
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

void afill(Range, Type)(Range r, Type value) {
  static if (is(typeof(r) == Type[])) {
    foreach (ref elem; r)
      elem = value;
  } else {
    foreach (ref arr; r)
      afill(arr, value);
  }
}

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

  int[] n;
  auto memo = new long[][][][](11, 64, 1 << 10, 2);
  afill(memo, -1L);
  long f(const int base, int i, bool less, int used, bool zero) {
    if (i < 0) {
      return used == 0 && !zero;
    } else {
      if (less && memo[base][i][used][zero] >= 0) {
        return memo[base][i][used][zero];
      }
      long ret = 0;
      auto digit = less ? base - 1 : n[i];
      for (int d = 0; d <= digit; d++) {
        auto l = less || (d < digit);
        auto z = zero && (d == 0);
        auto u = used ^ (z ? 0 : (1 << d));
        ret += f(base, i - 1, l, u, z);
      }
      return memo[base][i][used][zero] = ret;
    }
  }

  int q;
  rd(q);
  while (q--) {
    int b;
    long l, r;
    rd(b, l, r);
    long res = 0;
    n = digit(r, b).dup;
    res += f(b, (n.length - 1).to!(int), 0, 0, true);
    n = digit(l - 1, b).dup;
    res -= f(b, (n.length - 1).to!(int), 0, 0, true);
    writeln(res);
  }
}

int[] digit(long n, int base) {
  int[] ret;
  do {
    ret ~= n % base;
    n /= base;
  }
  while (n);
  return ret;
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

void afill(Range, Type)(Range r, Type value) {
  static if (is(typeof(r) == Type[])) {
    foreach (ref elem; r)
      elem = value;
  } else {
    foreach (ref arr; r)
      afill(arr, value);
  }
}

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

  int[] n;
  auto memo = new long[][][][](11, 64, 1 << 10, 2);
  afill(memo, -1L);
  long f(const int base, size_t i, bool less, int used, bool zero) {
    if (i == n.length) {
      return used == 0;
    } else {
      if (less && zero && memo[base][i][used][zero] >= 0) {
        return memo[base][i][used][zero];
      }
      long ret = 0;
      auto digit = less ? base - 1 : n[i];
      for (int d = 0; d <= digit; d++) {
        auto l = less || (d < digit);
        auto z = zero || (d > 0) || (i + 1 == n.length);
        auto u = !z ? used : (used ^ (1 << d));
        auto tmp = ret;
        ret += f(base, i + 1, l, u, z);
      }
      if (less && zero) { // leading zeros抜けるまでは分からないので
        memo[base][i][used][zero] = ret;
      }
      return ret;
    }
  }

  int q;
  rd(q);
  while (q--) {
    int b;
    long l, r;
    rd(b, l, r);
    long res = 0;
    n = digit(r, b).dup;
    res += f(b, 0, 0, 0, 0);
    n = digit(l - 1, b).dup;
    res -= f(b, 0, 0, 0, 0);
    writeln(res);
  }
}

int[] digit(long n, int base) {
  int[] ret;
  do {
    ret = n % base ~ ret;
    n /= base;
  }
  while (n);
  return ret;
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

void afill(Range, Type)(Range r, Type value) {
  static if (is(typeof(r) == Type[])) {
    foreach (ref elem; r)
      elem = value;
  } else {
    foreach (ref arr; r)
      afill(arr, value);
  }
}

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

  int[] n;
  auto memo = new long[][][][](11, 64, 1 << 10, 2);
  afill(memo, -1L);
  long f(const int base, int i, bool less, int used, bool zero) {
    if (i < 0) {
      return used == 0;
    } else {
      if (less && memo[base][i][used][zero] >= 0) {
        return memo[base][i][used][zero];
      }
      long ret = 0;
      auto digit = less ? base - 1 : n[i];
      for (int d = 0; d <= digit; d++) {
        auto l = less || (d < digit);
        auto z = zero && (d == 0);
        auto u = used ^ (z ? 0 : (1 << d));
        ret += f(base, i - 1, l, u, z);
      }
      return memo[base][i][used][zero] = ret;
    }
  }

  int q;
  rd(q);
  while (q--) {
    int b;
    long l, r;
    rd(b, l, r);
    long res = 0;
    n = digit(r, b).dup;
    res += f(b, (n.length - 1).to!(int), 0, 0, true);
    n = digit(l - 1, b).dup;
    res -= f(b, (n.length - 1).to!(int), 0, 0, true);
    writeln(res);
  }
}

int[] digit(long n, int base) {
  int[] ret;
  do {
    ret ~= n % base;
    n /= base;
  }
  while (n);
  return ret;
}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

void afill(Range, Type)(Range r, Type value) {
  static if (is(typeof(r) == Type[])) {
    foreach (ref elem; r)
      elem = value;
  } else {
    foreach (ref arr; r)
      afill(arr, value);
  }
}

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

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

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

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

enum LIM_B = 10;
enum LIM_L = 100;

void main() {
  auto dp = new long[][][LIM_B + 1];
  foreach (B; 2 .. LIM_B + 1) {
    dp[B] = new long[][](LIM_L + 1, 1 << B);
    dp[B][0][0] = 1;
    foreach (l; 0 .. LIM_L) {
      foreach (p; 0 .. 1 << B) {
        foreach (x; 0 .. B) {
          dp[B][l + 1][p ^ 1 << x] += dp[B][l][p];
        }
      }
    }
  }

  long solve(const(int) B, const(long) N) {
    int[] as;
    for (long n = N; n; n /= B) {
      as ~= cast(int)(n % B);
    }
    as.reverse;
    const asLen = cast(int)(as.length);
    long ans;
    // short
    foreach (l; 1 .. asLen) {
      foreach (x; 1 .. B) {
        ans += dp[B][l - 1][1 << x];
      }
    }
    // as long
    int p;
    foreach (i; 0 .. asLen) {
      foreach (x; 0 .. as[i]) {
        if (i > 0 || x > 0) {
          debug {
            writeln("  ", B, " ", asLen - 1 - i, " ", p ^ 1 << x, ": ", dp[B][asLen - 1 - i][p ^ 1 << x]);
          }
          ans += dp[B][asLen - 1 - i][p ^ 1 << x];
        }
      }
      p ^= 1 << as[i];
    }
    debug {
      writeln("solve ", B, " ", as, " = ", ans);
    }
    return ans;
  }

  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const B = readInt();
        const L = readLong();
        const R = readLong();

        long ans;
        ans -= solve(B, L + 1);
        ans += solve(B, R + 1);
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


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 euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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 a = RDA;

		bool[long] set;
		foreach (i; 0..n)
			set[a[i]] = true;

		auto cnt = cast(int)set.length;
		ans[ti].length = n;
		foreach (i; 0..n)
		{
			auto k = i + 1;
			ans[ti][i] = k + max(0, cnt - k);
		}
	}

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

void main() { runSolver(); }

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

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

      auto uniqSize = A.sort.uniq.array.length;
      return N.iota.map!(k => max(k + 1, uniqSize)).toAnswerString;
    }

    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.string;
import std.array;
import std.functional;
import std.algorithm;
import std.container;
import std.conv;
import std.random;

const int maxn = 100;
int n, a, b, k, fm; // a -- even, b -- odd, fm -- final turn player
int data[maxn][maxn];

void input() {
	readf("%s %s", &n, &k);
	for (int i = 0; i < n; ++i) {
		int t;
		readf(" %s", &t);
		if (t % 2) ++b; else ++a;
	}
	fm = (n - k) % 2;
}

int solve() {
	for (int chet = 0; chet < maxn; ++chet)
		for (int nech = 0; nech < maxn; ++nech) {
			if (chet + nech <= k)
				data[chet][nech] = nech % 2;
			else {
				int cp = fm ^ 1 ^ ((chet + nech - k) % 2);
				if (cp == 0) {
					data[chet][nech] = 1;
					if (chet) data[chet][nech] &= data[chet - 1][nech];
					if (nech) data[chet][nech] &= data[chet][nech - 1];
				} else {
					data[chet][nech] = 0;
					if (chet) data[chet][nech] |= data[chet - 1][nech];
					if (nech) data[chet][nech] |= data[chet][nech - 1];
				}
			}
		}
	return data[a][b];
}

int megasolve() {
	if (a < maxn && b < maxn && k < maxn)
		return solve();
	if (n == k)
		return b % 2;
	if (k % 2 == 0 && fm == 0)
		return 0;
	if (k % 2 == 1 && fm == 0)
		return b - a >= k;
	if (k % 2 == 0 && fm == 1)
		return (b - a < k) && (a - b < k);
	return a - b < k;
}

void main() {
	input();
	writeln(megasolve() ? "Stannis" : "Daenerys");
}

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

bool solve (int n, int k, int even, int odd)
{
	int moves = n - k;
	int dmoves = moves / 2;
	int smoves = moves - dmoves;
	if (n == k)
	{
		return odd % 2 == 0;
	}
	if (odd <= dmoves)
	{
		return true;
	}
	if (even <= smoves && k % 2 == 1)
	{
		return false;
	}
	if (even <= dmoves && k % 2 == 0)
	{
		return true;
	}
	if (even == 0)
	{
		return k % 2 == 0;
	}
	return dmoves >= smoves;
}

void main ()
{
	int n, k;
	while (readf (" %s %s ", &n, &k) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		int even = a.count !(q{a % 2 == 0});
		int odd = a.count !(q{a % 2 == 1});
		debug {writeln (a, ' ', even, ' ', odd);}
		writeln (solve (n, k, even, odd) ? "Daenerys" : "Stannis");
	}
}

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

bool solve (int n, int k, int even, int odd)
{
	int moves = n - k;
	int dmoves = moves / 2;
	int smoves = moves - dmoves;
	if (odd <= dmoves)
	{
		return true;
	}
	if (even <= smoves && k % 2 == 1)
	{
		return false;
	}
	if (even <= dmoves && k % 2 == 0)
	{
		return true;
	}
	if (even == 0)
	{
		return k % 2 == 0;
	}
	return dmoves >= smoves;
}

void main ()
{
	int n, k;
	while (readf (" %s %s ", &n, &k) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		int even = a.count !(q{a % 2 == 0});
		int odd = a.count !(q{a % 2 == 1});
		debug {writeln (a, ' ', even, ' ', odd);}
		writeln (solve (n, k, even, odd) ? "Daenerys" : "Stannis");
	}
}