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

		long m = a[0];
		long x;
		foreach (i; 0..n)
		{
			auto d = m - a[i];
			if (d > 0)
			{
				foreach (j; 0..32)
				{
					auto bit = 1L << j;
					if (d & bit)
					{
						x.chmax(j+1);
					}
				}
			}
			m.chmax(a[i]);
		}
		ans[ti] = x;
	}

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

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto a0 = a.front;
		a[] -= a0;
		int res = 0;
		int prev = 0;
		foreach (ref c; a)
		{
			res = max (res, prev - c);
			prev = max (prev, c);
		}
		res = (res == 0) ? 0 : bsr (res) + 1;
		writeln (res);
	}
}

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(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
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)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
    A: foreach(_; 0 .. scan!int){
        int n = scan!int;
        long[] as = scan!long(n);

        long t = as[0];
        long maxg = 0;
        foreach(a; as){
            if(a < t) maxg.raiseTo(t - a);
            else t = a;
        }
        if(maxg == 0) "0".writeln;
        else foreach(i; 0 .. 60){
            if((1L<<i) <= maxg) continue;
            i.writeln;
            continue A;
        }
    }
}

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;
    long[63] ns, cs;
    foreach (i; 0..63) ns[i] = (1L<<(i+1))-1;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(long[]);
        cs[] = 0;
        foreach (i; 0..N-1) {
            if (as[i+1] >= as[i]) continue;
            foreach (j, n; ns) if (as[i] - as[i+1] <= n) {
                ++cs[j];
                as[i+1] = as[i];
                break;
            }
        }
        foreach_reverse (i, c; cs) if (c > 0) {
            writeln(i+1);
            goto ok;
        }
        writeln(0);
        ok:
    }
}

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

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

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

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

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

        long mx;
        long now = long.min;
        foreach (i; 0 .. N) {
          chmax(now, A[i]);
          chmax(mx, now - A[i]);
        }
        debug {
          writeln("mx = ", mx);
        }
        for (int e = 0; ; ++e) {
          if (mx < 2L^^e) {
            writeln(e);
            break;
          }
        }
      }
    }
  } catch (EOFException e) {
  }
}

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

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

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

  void solve(long tc = -1)
  {
    long maxdiff = long.min;
    long bar = long.min;
    foreach(ai; a)
      {
        bar = max(bar, ai);
        maxdiff = max(maxdiff, bar - ai);
      }
    long bits = 0;
    while(maxdiff)
      {
        maxdiff /= 2;
        bits++;
      }
    writeln(bits);
  }
}

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

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

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

int test (const int n, const long[] a) {
  if (isSorted (a)) return 0;
  long d = 1;
  long x = 0;
  long lastD;
  long maxD;
  foreach (i; 1 .. n) {
    long t = max (0L, (a[i-1] + lastD) - a[i]);
    maxD = max (maxD, t);
    lastD = t;
  }
  debug stderr.writeln ("maxD = ", maxD);
  foreach (l; 1 .. 50) {
    x += d;
    if (x >= maxD) return l;
    d *= 2;
  }
  return -1;
}

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  foreach (tid; 0 .. nt) {
    const n = r.next!uint;
    auto a = r.nextA!long(n);
    writeln (test (n, a));
  }
}

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(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
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)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
    A: foreach(_; 0 .. scan!int){
        int n = scan!int;
        long[] as = scan!long(n);

        long t = as[0];
        long maxg = 0;
        foreach(a; as){
            if(a < t) maxg.raiseTo(t - a);
            else t = a;
        }
        if(maxg == 0) "0".writeln;
        else foreach(i; 0 .. 30){
            if((1L<<i) <= maxg) continue;
            i.writeln;
            continue A;
        }
    }
}

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;
    long[32] ns, cs;
    foreach (i; 0..32) ns[i] = 1L<<i;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(long[]);
        cs[] = 0;
        foreach (i; 0..N-1) {
            foreach (j, n; ns) if (as[i] - as[i+1] > 0) {
                ++cs[j];
                as[i+1] += n;
            }
        }
        foreach_reverse (i, c; cs) if (c > 0) {
            writeln(i+1);
            goto ok;
        }
        writeln(0);
        ok:
    }
}

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;
    long[32] ns, cs;
    foreach (i; 0..32) ns[i] = (1L<<(i+1))-1;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(long[]);
        cs[] = 0;
        long cnt;
        foreach (i; 0..N-1) {
            if (as[i] - as[i+1] <= 0) continue;
            foreach (j, n; ns) if (n >= as[i] - as[i+1]) {
                ++cs[j];
                ++cnt;
                break;
            }
        }
        if (cnt == 0) {
            writeln(0);
            continue;
        }
        long rest, res;
        foreach (i, c; cs) {
            rest += c;
            cnt -= c;
            if (rest) rest -= 1;
            res += 1;
            if (cnt == 0) break;
        }
        writeln(res + rest);
    }
}

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;
    long[32] ns, cs;
    foreach (i; 0..32) ns[i] = (1L<<(i+1))-1;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(long[]);
        cs[] = 0;
        long cnt;
        foreach (i; 0..N-1) {
            if (as[i] - as[i+1] <= 0) continue;
            foreach (j, n; ns) if (n >= as[i] - as[i+1]) {
                ++cs[j];
                as[i+1] += n;
                ++cnt;
                break;
            }
        }
        if (cnt == 0) {
            writeln(0);
            continue;
        }
        foreach_reverse (i, c; cs) if (c > 0) {
            writeln(i+1);
            break;
        }
    }
}

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;
    long[32] ns, cs;
    foreach (i; 0..32) ns[i] = 1L<<i;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(long[]);
        cs[] = 0;
        long cnt;
        foreach (i; 0..N-1) {
            if (as[i] - as[i+1] <= 0) continue;
            foreach (j, n; ns) if (n >= as[i] - as[i+1]) {
                ++cs[j];
                ++cnt;
                break;
            }
        }
        if (cnt == 0) {
            writeln(0);
            continue;
        }
        long rest, res;
        foreach (i, c; cs) {
            rest += c;
            cnt -= c;
            if (rest) --rest;
            res += 1;
            if (cnt == 0) break;
        }
        writeln(res + rest);
    }
}

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;
    long[63] ns, cs;
    foreach (i; 0..63) ns[i] = 1L<<(i+1)-1;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(long[]);
        cs[] = 0;
        foreach (i; 0..N-1) {
            if (as[i+1] >= as[i]) continue;
            foreach (j, n; ns) if (as[i] - as[i+1] <= n) {
                ++cs[j];
                as[i+1] = as[i];
                break;
            }
        }
        foreach_reverse (i, c; cs) if (c > 0) {
            writeln(i+1);
            goto ok;
        }
        writeln(0);
        ok:
    }
}

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

		long m = a[0], dd;
		foreach (i; 0..n)
		{
			auto d = a[i] - m;
			if (d == -2)
			{
				d = -3;
				m.chmax(a[i]+3);
			}
			if (d < 0)
			{
				dd.chmax(-d);
			}
			m.chmax(a[i]);
		}

		long x;
		while (dd > 0)
		{
			++x;
			dd -= x;
		}
		ans[ti] = x;
	}

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

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

		long m, dd;
		foreach (i; 0..n)
		{
			auto d = a[i] - m;
			if (d < 0)
			{
				dd.chmax(-d);
			}
			m.chmax(a[i]);
		}

		long x;
		while (dd > 0)
		{
			++x;
			dd -= x;
		}
		ans[ti] = x;
	}

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

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

		long m = a[0], dd;
		foreach (i; 0..n)
		{
			auto d = a[i] - m;
			if (d < 0)
			{
				dd.chmax(-d);
			}
			m.chmax(a[i]);
		}

		long x;
		while (dd > 0)
		{
			++x;
			dd -= x;
		}
		ans[ti] = x;
	}

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

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

		long m = a[0], dd = long.min;
		foreach (i; 0..n)
		{
			auto d = a[i] - m;
			if (d < 0)
			{
				dd.chmax(-d);
			}
			m.chmax(a[i]);
		}

		long x;
		while (dd > 0)
		{
			++x;
			dd -= x;
		}
		ans[ti] = x;
	}

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

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

void solve(){
	int n = rint;
	int[] as = rint(n).map!(x => x - 1).array;
	int[] bs = rint(n).map!(x => x - 1).array;
	log("as:", as, "bs:", bs);

	int[] xs = new int[](n);
	foreach(int i, a; as) xs[a] = i;
	log("xs:", xs);

	int[] ys = new int[](n);
	foreach(int i, b; bs) ys[i] = xs[b];
	log("ys:", ys);

	int t;
	int ans;
	bool[] us = new bool[](n);
	foreach(y; ys){
		us[y] = 1;
		if(y > t) ans += 1;
		else while(t < n && us[t]) t += 1;
		log("y:", y, "t:", t, "us:", us, "ans:", ans);
	}

	ans.writeln;
}

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

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

		auto ia = new int [n];
		foreach (i; 0..n)
		{
			ia[a[i]] = i;
		}
		auto pb = new int [n];
		foreach (i; 0..n)
		{
			pb[i] = ia[b[i]];
		}
		debug {writeln (pb);}

		int res = 0;
		auto vis = new bool [n + 1];
		int p = 0;
		for (int i = 0; i < n; i++)
		{
			if (p != pb[i])
			{
				res += 1;
			}
			vis[pb[i]] = true;
			while (vis[p])
			{
				p += 1;
			}
		}
		writeln (res);
	}
}

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

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

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

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

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

      auto ordB = new int[N];
      foreach (i; 0 .. N) {
        ordB[B[i]] = i;
      }

      int ans;
      int mx = -1;
      foreach (i; 0 .. N) {
        if (mx > ordB[A[i]]) {
          ++ans;
        }
        chmax(mx, ordB[A[i]]);
      }
      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.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto n = RD!int;
	auto a = RDA;
	auto b = RDA;
	int ans;
	bool[long] set;
	while (true)
	{
		if (b.empty)
		{
			break;
		}
		if (set.get(a.front, false))
		{
			a.popFront;
		}
		else if (a.front != b.front)
		{
			if (set.get(a.front, false) == false)
			{
				++ans;
				set[b.front] = true;
			}
			b.popFront;
		}
		else
		{
			a.popFront;
			set[b.front] = true;
			b.popFront;
		}
	}

	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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

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

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

void main()
{
	auto n = RD!int;
	auto a = RDA;
	auto b = RDA;
	int ans;
	bool[long] set;
	while (true)
	{
		if (b.empty)
		{
			break;
		}
		if (a.front != b.front)
		{
			if (set.get(a.front, false) == false)
			{
				++ans;
				set[b.front] = true;
			}
			b.popFront;
		}
		else
		{
			a.popFront;
			set[b.front] = true;
			b.popFront;
		}
	}

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


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

  int q;
  rd(q);
  bool enough(long k, long n, long a, long b, long m) {
    return a * m + b * (n - m) < k;
  }

  while (q--) {
    long k, n, a, b;
    rd(k, n, a, b);
    if (b * n >= k) {
      writeln(-1);
      continue;
    }
    long ok = 0, ng = n + 1;
    while (ng - ok > 1) {
      auto m = (ok + ng) / 2;
      if (enough(k, n, a, b, m)) {
        ok = m;
      } else {
        ng = m;
      }
    }
    writeln(ok);
  }

}

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

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 INF = 1L << 59;

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        auto s = readln.split.map!(to!long);
        auto K = s[0];
        auto N = s[1];
        auto A = s[2];
        auto B = s[3];
        long hi = N + 1;
        long lo = -1;
        while (hi - lo > 1) {
            long mid = (hi + lo) / 2;
            long k = K - mid * A;
            if (k + A <= A) {
                hi = mid;
                continue;
            }
            k = k - (N - mid) * B;
            if (k + B <= B) {
                hi = mid;
                continue;
            }
            lo = mid;
        }
        lo.writeln;
    }
}


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

void main() {
    auto N = readln.chomp.to!int;
    auto S = N.iota.map!(_ => readln.chomp).array;
    S.sort!((a, b) => a.length < b.length)();

    foreach (i; 1..N) {
        if (!canFind(S[i], S[i-1])) {
            writeln("NO");
            return;
        }
    }

    writeln("YES");
    S.each!writeln;
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n=readln.chomp.to!int;
    auto s=new string[n];
    foreach(i;0..n)
        s[i]=readln.strip;
    sort(s);
    sort!"a.length<b.length"(s);
    foreach(i;1..n){
        if(s[i].indexOf(s[i-1])<0){
            writeln("NO");
            return;
        }
    }
    writeln("YES");
    foreach(ref x;s)
        writeln(x);
}

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

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

    string[] a;
    foreach (_; 0..n) a ~= readln.chomp;

    debug { a.writeln; }

    a.sort!((x, y) => x.length < y.length);

    bool ok =  zip(a, a.dropOne).array.all!(t => t[1].canFind(t[0]));

    if (! ok) {
        writeln("NO");
        return;
    }

    writeln("YES");
    a.each!writeln;
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

void main(){
    int n=readln.chomp.to!int;
    auto s=new string[n];
    foreach(i;0..n)
        s[i]=readln.strip;
    sort!"a.length<b.length"(s);
    foreach(i;1..n){
        if(s[i].indexOf(s[i-1])<0){
            writeln("NO");
            return;
        }
    }
    writeln("YES");
    foreach(ref x;s)
        writeln(x);
}


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;

bool can_add (string start, char e)
{
	if (start.length >= 1 && start[$ - 1] == e)
	{
		return false;
	}
	if (start.length >= 2 && start[$ - 2] == e)
	{
		return false;
	}
	return true;
}

string build (string start, int n, int p)
{
main_loop:
	while (start.length < n)
	{
		foreach (d; 0..p)
		{
			char e = cast (char) (d + 'a');
			if (!can_add (start, e))
			{
				continue;
			}
			start ~= e;
			continue main_loop;
		}
		return null;
	}
	if (start.length == n)
	{
		return start;
	}
	return null;
}

string solve (int n, int p, string s)
{
	foreach_reverse (i, c; s)
	{
		foreach (d; c - 'a' + 1..p)
		{
			char e = cast (char) (d + 'a');
			string t = s[0..i];
			if (!can_add (t, e))
			{
				continue;
			}
			t = build (t ~ e, n, p);
			if (t !is null)
			{
				return t;
			}
		}
	}
	return "NO";
}

void main ()
{
	int n;
	int p;
	while (readf (" %s %s ", &n, &p) > 0)
	{
		string s = readln ().strip ();
		writeln (solve (n, p, s));
	}
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.range;
import std.range.primitives;
import std.conv;

bool nextstr(char[] carr, int idx, int p) {
    if (idx < 0) return false;
    if (carr[idx] - 'a' + 1 == p) {
        carr[idx] = 'a';
        return nextstr(carr, idx - 1, p);
    }
    else {
        carr[idx] += 1;
        return true;
    }
}

void setabc(char[] carr, int idx, int n) {
    char ch = 'a';
    while (idx < n) {
        carr[idx++] = ch;
        ch += 1;
        if (ch == 'd') ch = 'a';
    }
}

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

    int n, p;
    readf(" %s %s\n", &n, &p);
    char[] mstr;
    readf(" %s\n", &mstr);

    if (p < 3) {
        if (n == 1 && p == 2 && mstr[0] == 'a') writeln("b");
        else if (n == 2 && mstr[0] == 'a' && mstr[1] == 'b')  writeln("ba");
        else writeln("NO");
        return 0;
    }

    int idx = n - 1;
    while (nextstr(mstr, idx, p)) {
        foreach (i; 0 .. n - 2) {
            if (mstr[i] == mstr[i + 1]) {
                idx = i + 1;
                goto cont;
            }
            if (mstr[i] == mstr[i + 2]) {
                idx = i + 2;
                goto cont;
            }
        }
        if (n > 1 && mstr[n - 2] == mstr[n - 1]) {
            idx = n - 1;
            goto cont;
        }
        writeln(mstr);
        return 0;
cont:
        setabc(mstr, idx + 1, n);
    }
    writeln("NO");

    return 0;
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int N, P;
string S;

string solve() {
	if (P == 1) {
		if (N == 1) {
			if (S < "a") return "a";
			return "";
		} else {
			return "";
		}
	} else if (P == 2) {
		if (N == 1) {
			if (S < "a") return "a";
			if (S < "b") return "b";
			return "";
		} else if (N == 2) {
			if (S < "ab") return "ab";
			if (S < "ba") return "ba";
			return "";
		} else {
			return "";
		}
	} else {
		foreach_reverse (k; 0 .. N) {
			string ret = S[0 .. k];
			bool ok = true;
			foreach (i; k .. N) {
				bool found;
				foreach (x; 0 .. P) {
					if (i == k && x <= S[k] - 'a') {
						continue;
					}
					if (i >= 1 && ret[i - 1] - 'a' == x) {
						continue;
					}
					if (i >= 2 && ret[i - 2] - 'a' == x) {
						continue;
					}
					found = true;
					ret ~= cast(char)('a' + x);
					break;
				}
				if (!found) {
					ok = false;
					break;
				}
			}
			if (ok) {
				return ret;
			}
		}
		return "";
	}
}

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		P = readInt;
		S = readToken;

		string res = solve;
		if (res == "") {
			writeln("NO");
		} else {
			writeln(res);
		}
	}
	} catch (EOFException) {}
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.range;
import std.range.primitives;
import std.conv;

bool nextstr(char[] carr, int idx, int p) {
    if (idx < 0) return false;
    if (carr[idx] - 'a' + 1 == p) {
        carr[idx] = 'a';
        return nextstr(carr, idx - 1, p);
    }
    else {
        carr[idx] += 1;
        return true;
    }
}

bool ispalin(char[] carr, int bi, int ei) {
    foreach (i; 0 .. (ei - bi + 1) / 2) {
        if (carr[bi + i] != carr[ei - i]) return false;
    }
    return true;
}

void setabc(char[] carr, int idx, int n) {
    char ch = 'a';
    while (idx < n) {
        carr[idx++] = ch;
        ch += 1;
        if (ch == 'd') ch = 'a';
    }

}

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

    int n, p;
    readf(" %s %s\n", &n, &p);
    char[] mstr;
    readf(" %s\n", &mstr);

    if (n < 4 || p < 3) {
        while (nextstr(mstr, n - 1, p)) {
            bool palinfree = true;
        outer:
            foreach (i; 0 .. n - 1) {
                foreach (j; i + 1 .. n) {
                    if (ispalin(mstr, i, j)) {
                        palinfree = false;
                        break outer;
                    }
                }
            }
            if (palinfree) {
                writeln(mstr);
                return 0;
            }
        }
        writeln("NO");
        return 0;
    }

    int idx = n - 1;
    while (nextstr(mstr, idx, p)) {
        foreach (i; 0 .. n - 2) {
            if (mstr[i] == mstr[i + 1]) {
                idx = i + 1;
                goto cont;
            }
            if (mstr[i] == mstr[i + 2]) {
                idx = i + 2;
                goto cont;
            }
        }
        if (mstr[n - 2] == mstr[n - 1]) {
            idx = n - 1;
            goto cont;
        }
        writeln(mstr);
        return 0;
cont:
        setabc(mstr, idx + 1, n);
    }
    writeln("NO");

    return 0;
}

import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.range;
import std.range.primitives;
import std.conv;

bool nextstr(char[] carr, int idx, int p) {
    if (idx < 0) return false;
    if (carr[idx] - 'a' + 1 == p) {
        carr[idx] = 'a';
        return nextstr(carr, idx - 1, p);
    }
    else {
        carr[idx] += 1;
        return true;
    }
}

void setabc(char[] carr, int idx, int n) {
    char ch = 'a';
    while (idx < n) {
        carr[idx++] = ch;
        ch += 1;
        if (ch == 'd') ch = 'a';
    }
}

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

    int n, p;
    readf(" %s %s\n", &n, &p);
    char[] mstr;
    readf(" %s\n", &mstr);

    if (p < 3) {
        if (p == 1 || n > 1 || mstr[0] != 'a') {
            writeln("NO");
            return 0;
        }
        else {
            writeln("b");
            return 0;
        }
    }

    int idx = n - 1;
    while (nextstr(mstr, idx, p)) {
        foreach (i; 0 .. n - 2) {
            if (mstr[i] == mstr[i + 1]) {
                idx = i + 1;
                goto cont;
            }
            if (mstr[i] == mstr[i + 2]) {
                idx = i + 2;
                goto cont;
            }
        }
        if (n > 1 && mstr[n - 2] == mstr[n - 1]) {
            idx = n - 1;
            goto cont;
        }
        writeln(mstr);
        return 0;
cont:
        setabc(mstr, idx + 1, n);
    }
    writeln("NO");

    return 0;
}


import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;
import core.checkedint;

void main()
{
	string[string] dic = [
		"purple" : "Power",
		"green" : "Time",
		"blue" : "Space",
		"orange" : "Soul",
		"red" : "Reality",
		"yellow" : "Mind"
	];
	int m = readln.chomp.to!int;
	foreach (i; 0..m) {
		string s = readln.chomp;
		dic.remove(s);
	}
	dic.length.writeln;
	foreach (k, v; dic) {
		v.writeln;
	}
}

void tie(R, Args...)(R arr, ref Args args)
		if (isRandomAccessRange!R || isArray!R)
in
{
	assert (arr.length == args.length);
}
body
{
	foreach (i, ref v; args) {
		alias T = typeof(v);
		v = arr[i].to!T;
	}
}

void verbose(Args...)(in Args args)
{
	stderr.write("[");
	foreach (i, ref v; args) {
		if (i) stderr.write(", ");
		stderr.write(v);
	}
	stderr.writeln("]");
}

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

void main()
{
    string [string] mp = [
        "purple": "Power",
        "green": "Time",
        "blue": "Space",
        "orange": "Soul",
        "red": "Reality",
        "yellow": "Mind"
    ];

    readln;
    string s;
    while ((s = readln.chomp) != "") {
        debug { s.writeln; }
        mp.remove(s);
    }

    auto ans = mp.values;
    ans.length.writeln;
    ans.each!writeln;
}


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

void read(S...)(ref S args) {
  auto input = readln.split;
  assert(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

char[] favor(string s, bool first) {
  char[] t;
  foreach (i; 0 .. 10) {
    if (s[i] == '?') {
      bool isFirstTeam = i % 2 == 0;
      t ~= (isFirstTeam == first) ? '1' : '0';
    } else {
      t ~= s[i];
    }
  }
  return t;
}

int solve(char[] s) {
  auto score = [0, 0];
  auto rem   = [5, 5];
  foreach (i; 0 .. 10) {
    if (s[i] == '1') {
      score[i % 2]++;
    }
    rem[i % 2]--;

    if (score[0] - score[1] > rem[1] || score[1] - score[0] > rem[0]) return i + 1;
  }

  return 10;
}

void main() {
  int T;
  read(T);

  while (T--) {
    string s;
    s = readln.strip;

    writeln(min(solve(s.favor(false)), solve(s.favor(true))));
  }
}

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

void main()
{
	int t = readInt!int;
	while (t--)
	{
		char[] str = cast(char[])readString;
		int minkicks = int.max;
		foreach(p; 0 .. 2)
		{
			int[2] pos = [5, 5];
			int[2] scr = [0, 0];
			int kicks = 0;
			foreach(i; 0 .. 10)
			{
				kicks++;
				int s = i & 1;
				int o = s ^ 1;
				if (str[i] == '1') scr[s]++;
				else if (str[i] == '?' && s == p) scr[s]++;
				pos[s]--;
				if (scr[o]+pos[o] < scr[s] || scr[s]+pos[s] < scr[o]) break;
			}
			minkicks = min(kicks, minkicks);
		}
		minkicks.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.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

long solve(int[] a)
{
  long odd  = 0;
  long even = 0;
  foreach (round ; 0 .. 5) {
    int remrounds = 4 - round;
    even += a[round * 2 + 0];
    if ((even > odd && even - odd > remrounds + 1) || (odd > even && odd - even > remrounds))
      return round * 2 + 1;
    odd += a[round * 2 + 1];
    if ((even > odd && even - odd > remrounds) || (odd > even && odd - even > remrounds))
      return round * 2 + 2;
  }
  return 10;
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    string s = readln.strip;
    int[] a;
    int[] b;
    foreach (i; 0 .. s.length) {
      if (s[i] == '?') {
        if (i % 2 == 0) {
          a ~= 0;
          b ~= 1;
        } else {
          a ~= 1;
          b ~= 0;
        }
      } else {
        a ~= s[i] - '0';
        b ~= s[i] - '0';
      }
    }
/*
    writeln(s);
    writeln(a);
    writeln(solve(a));
    writeln(b);
    writeln(solve(b));
*/
    writeln(min(solve(a), solve(b)));
  }
}

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

immutable int steps = 10;

int solve (string s, int team)
{
	int [2] goals;
	foreach (step; 0..steps)
	{
		auto cur = (s[step] == '0') ? 0 :
		    (s[step] == '1') ? 1 : (step % 2 == team);
		goals[step % 2] += cur;
		if (goals[0] + (steps - 1 - step + 0) / 2 < goals[1] ||
		    goals[1] + (steps - 1 - step + 1) / 2 < goals[0])
		{
			return step + 1;
		}
	}
	return steps;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		writeln (min (solve (s, 0), solve (s, 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 s = RD!string;

		ans[ti] = s.length;
		{
			long cnt;
			foreach (i; 0..s.length)
			{
				if (s[i] == '?')
				{
					if (i % 2 == 0)
						++cnt;
				}
				else if (s[i] == '1')
				{
					if (i % 2)
						--cnt;
					else
						++cnt;
				}
				{
					long rem1 = (s.length - i) / 2 - (i % 2 == 0 ? 1 : 0);
					long rem2 = (s.length - i) / 2;
					if (cnt > rem2 || cnt + rem1 < 0)
					{
						ans[ti].chmin(i+1);
						break;
					}
				}
			}
		}
		{
			long cnt;
			foreach (i; 0..s.length)
			{
				if (s[i] == '?')
				{
					if (i % 2)
						++cnt;
				}
				else if (s[i] == '1')
				{
					if (i % 2)
						++cnt;
					else
						--cnt;
				}
				{
					long rem1 = (s.length - i) / 2 - (i % 2 == 0 ? 1 : 0);
					long rem2 = (s.length - i) / 2;
					if (cnt > rem1 || cnt + rem2 < 0)
					{
						ans[ti].chmin(i+1);
						break;
					}
				}
			}
		}
	}

	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 s = RD!string;

		ans[ti] = s.length;
		{
			long cnt;
			foreach (i; 0..s.length)
			{
				if (s[i] == '?')
				{
					if (i % 2 == 0)
						++cnt;
				}
				else if (s[i] == '1')
				{
					if (i % 2)
						--cnt;
					else
						++cnt;
				}
				{
					long rem1 = (s.length - i) / 2 - (i % 2 == 0 ? 1 : 0);
					long rem2 = (s.length - i) / 2;
					if (cnt > rem2 || cnt + rem1 < 0)
					{
						ans[ti].chmin(i+1);
						break;
					}
				}
			}
		}
		{
			long cnt;
			foreach (i; 0..s.length)
			{
				if (s[i] == '?')
				{
					if (i % 2)
						++cnt;
				}
				else if (s[i] == '1')
				{
					if (i % 2)
						++cnt;
					else
						--cnt;
				}
				if (i % 2)
				{
					long rem1 = (s.length - i) / 2 - (i % 2 == 0 ? 1 : 0);
					long rem2 = (s.length - i) / 2;
					if (cnt > rem1 || cnt + rem2 < 0)
					{
						ans[ti].chmin(i+1);
						break;
					}
				}
			}
		}
	}

	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 s = RD!string;

		ans[ti] = s.length;
		{
			long cnt;
			foreach (i; 0..s.length)
			{
				if (s[i] == '?')
				{
					if (i % 2 == 0)
						++cnt;
				}
				else if (s[i] == '1')
				{
					if (i % 2)
						--cnt;
					else
						++cnt;
				}
				if (i % 2 == 0)
				{
					long rem = (s.length - i) / 2;
					debug writeln("i:", i, " cnt:", cnt, " rem:", rem);
					if (cnt > rem || -cnt > rem-1)
					{
						ans[ti].chmin(i+1);
						break;
					}
				}
			}
		}
		{
			long cnt;
			foreach (i; 0..s.length)
			{
				if (s[i] == '?')
				{
					if (i % 2)
						++cnt;
				}
				else if (s[i] == '1')
				{
					if (i % 2)
						++cnt;
					else
						--cnt;
				}
				if (i % 2)
				{
					long rem = (s.length - i) / 2;
					if (cnt > rem || -cnt > rem-1)
					{
						ans[ti].chmin(i+1);
						break;
					}
				}
			}
		}
	}

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


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

// Main Logic Here
void play(){
    int n, q;
    n = rd!int; q = rd!int;
    dchar[] arr = rd!(dchar[]);
    auto before = new ll[2][n+1];
    auto after = new ll[2][n+1];
    bool is1 = 0, is0 = 0;

    foreach(i; 0..n){
        before[i][0] = is0;
        before[i][1] = is1;
        is0 = (arr[i] == '0' || is0);
        is1 = (arr[i] == '1' || is1);
    }
    is1 = 0; is0 = 0;
    foreach_reverse(i; 0..n){
        after[i][0] = is0;
        after[i][1] = is1;
        is0 = (arr[i] == '0' || is0);
        is1 = (arr[i] == '1' || is1);
    }
    show(before);
    show(after);
    while(q--){
        int l = rd!int;
        int r = rd!int;
        show(l, r);
        if(before[l-1][to!int(arr[l-1] - '0')] || after[r-1][to!int(arr[r-1] - '0')]){
            writeln("YES");
        }else{
            writeln("NO");
        }
    }
}

int main(){
    long t = 1;
    t = rd;        // Toggle for testcases!
    while(t--) play();  // Let's play!
    return 0;
}

/**********That's All Folks!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
static string[] inp;    // Read input
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }
alias ll = long;
alias tup = Tuple!(long, "x",  long, "y");
void show(A...)(A a) { debug{ foreach(t; a){ stderr.write(t, "| "); } stderr.writeln; } } // Debug
/* Add if TLE, T max(T = long)(T a, T b){ return (a > b) ? a : 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(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 bool[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto q = RD!int;
		auto s = RD!string;
		ans[ti].length = q;
		foreach (i; 0..q)
		{
			auto l = RD!int-1;
			auto r = RD!int-1;
			bool ok;
			foreach (j; 0..l)
			{
				if (s[j] == s[l])
				{
					ok = true;
					break;
				}
			}
			foreach (j; r+1..n)
			{
				if (s[j] == s[r])
				{
					ok = true;
					break;
				}
			}
			ans[ti][i] = ok;
		}
	}

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

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

// Main Logic Here
void play(){
    int n, q;
    n = rd!int; q = rd!int;
    dchar[] arr = rd!(dchar[]);
    auto before = new ll[2][n+1];
    auto after = new ll[2][n+1];
    bool is1 = 0, is0 = 0;

    foreach(i; 0..n){
        before[i][0] = is0;
        before[i][1] = is1;
        is0 = (arr[i] == '0' || is0);
        is1 = (arr[i] == '1' || is1);
    }
    is1 = 0; is0 = 1;
    foreach_reverse(i; 0..n){
        after[i][0] = is0;
        after[i][1] = is1;
        is0 = (arr[i] == '0' || is0);
        is1 = (arr[i] == '1' || is1);
    }
    show(before);
    show(after);
    while(q--){
        int l = rd!int;
        int r = rd!int;
        show(l, r);
        if(before[l-1][to!int(arr[l-1] - '0')] || after[r-1][to!int(arr[r-1] - '0')]){
            writeln("YES");
        }else{
            writeln("NO");
        }
    }
}

int main(){
    long t = 1;
    t = rd;        // Toggle for testcases!
    while(t--) play();  // Let's play!
    return 0;
}

/**********That's All Folks!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
static string[] inp;    // Read input
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }
alias ll = long;
alias tup = Tuple!(long, "x",  long, "y");
void show(A...)(A a) { debug{ foreach(t; a){ stderr.write(t, "| "); } stderr.writeln; } } // Debug
/* Add if TLE, T max(T = long)(T a, T b){ return (a > b) ? a : b; } */


import std.stdio;

int n, m;

int[] col;
int[][] edge, child;
int[2][][] query;

int size[];
void root(int u) {
    size[u] = 1;
    foreach(v; edge[u]) {
        if (size[v] == 0) {
            child[u] ~= v;
            root(v);
            size[u] += size[v];
        }
    }
}

int[][] least, num, cols;
void add(int lv, int c, bool rt) {
    ++least[lv][++num[lv][c]];
    cols[lv].assumeSafeAppend;
    cols[lv] ~= c;
    if (!rt) --least[lv+1][num[lv+1][c]--];
}

int[] ans;
void dfs(int u, int lv) {
    int maxv = -1;
    foreach(v; child[u]) {
        if (maxv == -1 || size[v] > size[maxv]) {
            maxv = v;
        }
    }

    if (maxv != -1) {
        dfs(maxv, lv);
        foreach(v; child[u]) {
            if (v == maxv) continue;
            dfs(v, lv+1);
            foreach(c; cols[lv+1]) {
                add(lv, c, false);
            }
            cols[lv+1].length = 0;
        }
    }
    add(lv, col[u], true);

    foreach(q; query[u]) {
        ans[q[0]] = q[1] > n ? 0 : least[lv][q[1]];
    }
}

void main() {
    readf(" %d %d", &n, &m);

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

    edge = new int[][](n, 0);
    foreach(i; 0..n-1) {
        int a, b;
        readf(" %d %d", &a, &b);
        --a, --b;

        edge[a] ~= b;
        edge[b] ~= a;
    }

    query = new int[2][][](n, 0);
    foreach(i; 0..m) {
        int v, k;
        readf(" %d %d", &v, &k);
        --v;

        query[v] ~= [i, k];
    }

    child = new int[][](n, 0);
    size = new int[](n);
    root(0);

    int log = 1;
    while (1 << log < n) { ++log; }

    least = new int[][](log, n+1);
    num = new int[][](log, 10^^5);
    cols = new int[][](log, 0);

    ans = new int[](m);
    dfs(0, 0);

    foreach(a; ans) {
        writeln(a);
    }
}

import std.stdio;

int n, m;

int[] col;
int[][] edge, child;
int[2][][] query;

int size[];
void root(int u) {
    size[u] = 1;
    foreach(v; edge[u]) {
        if (size[v] == 0) {
            child[u] ~= v;
            root(v);
            size[u] += size[v];
        }
    }
}

int[][] least, num, cols;
void add(int lv, int c, bool rt) {
    ++least[lv][++num[lv][c]];
    cols[lv] ~= c;
    if (!rt) --least[lv+1][num[lv+1][c]--];
}

int[] ans;
void dfs(int u, int lv) {
    int maxv = -1;
    foreach(v; child[u]) {
        if (maxv == -1 || size[v] > size[maxv]) {
            maxv = v;
        }
    }

    if (maxv != -1) {
        dfs(maxv, lv);
        foreach(v; child[u]) {
            if (v == maxv) continue;
            dfs(v, lv+1);
            foreach(c; cols[lv+1]) {
                add(lv, c, false);
            }
            cols[lv+1].length = 0;
        }
    }
    add(lv, col[u], true);

    foreach(q; query[u]) {
        ans[q[0]] = q[1] > n ? 0 : least[lv][q[1]];
    }
}

void main() {
    readf(" %d %d", &n, &m);

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

    edge = new int[][](n, 0);
    foreach(i; 0..n-1) {
        int a, b;
        readf(" %d %d", &a, &b);
        --a, --b;

        edge[a] ~= b;
        edge[b] ~= a;
    }

    query = new int[2][][](n, 0);
    foreach(i; 0..m) {
        int v, k;
        readf(" %d %d", &v, &k);
        --v;

        query[v] ~= [i, k];
    }

    child = new int[][](n, 0);
    size = new int[](n);
    root(0);

    int log = 1;
    while (1 << log < n) { ++log; }

    least = new int[][](log, n+1);
    num = new int[][](log, 10^^5);
    cols = new int[][](log, 0);

    ans = new int[](m);
    dfs(0, 0);

    foreach(a; ans) {
        writeln(a);
    }
}

import std.stdio;

int n, m;

int[] col;
int[][] edge, child;
int[2][][] query;

int size[];
void root(int u) {
    size[u] = 1;
    foreach(v; edge[u]) {
        if (size[v] == 0) {
            child[u] ~= v;
            root(v);
            size[u] += size[v];
        }
    }
}

int[][] least, num, cols;
void add(int lv, int c, bool rt) {
    ++least[lv][++num[lv][c]];
    cols[lv] ~= c;
    if (!rt) --least[lv+1][num[lv+1][c]--];
}

int[] ans;
void dfs(int u, int lv) {
    int maxv = -1;
    foreach(v; child[u]) {
        if (maxv == -1 || size[v] > size[maxv]) {
            maxv = v;
        }
    }

    if (maxv != -1) {
        dfs(maxv, lv);
        foreach(v; child[u]) {
            if (v == maxv) continue;
            dfs(v, lv+1);
            foreach(c; cols[lv+1]) {
                add(lv, c, false);
            }
            cols[lv+1].length = 0;
        }
    }
    add(lv, col[u], true);

    foreach(q; query[u]) {
        ans[q[0]] = q[1] > n ? 0 : least[lv][q[1]];
    }
}

void main() {
    readf(" %d %d", &n, &m);

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

    edge = new int[][](n, 0);
    foreach(i; 0..n-1) {
        int a, b;
        readf(" %d %d", &a, &b);
        --a, --b;

        edge[a] ~= b;
        edge[b] ~= a;
    }

    query = new int[2][][](n, 0);
    foreach(i; 0..m) {
        int v, k;
        readf(" %d %d", &v, &k);
        --v;

        query[v] ~= [i, k];
    }

    child = new int[][](n, 0);
    size = new int[](n);
    root(0);

    int lg = 0;
    while (1 << lg <= n) ++lg;

    least = new int[][](lg, n+1);
    num = new int[][](lg, 10^^5);
    cols = new int[][](lg, 0);

    ans = new int[](m);
    dfs(0, 0);

    foreach(a; ans) {
        writeln(a);
    }
}

import std.stdio;

int n, m;

int[] col;
int[][] edge, child;
int[2][][] query;

int size[];
void root(int u) {
    size[u] = 1;
    foreach(v; edge[u]) {
        if (size[v] == 0) {
            child[u] ~= v;
            root(v);
            size[u] += size[v];
        }
    }
}

int[][] least, num, cols;
void add(int lv, int c, bool rt) {
    ++least[lv][++num[lv][c]];
    cols[lv].assumeSafeAppend;
    cols[lv] ~= c;
    if (!rt) --least[lv+1][num[lv+1][c]--];
}

int[] ans;
void dfs(int u, int lv) {
    if (child[u] !is null) {
        int maxv = -1;
        foreach(v; child[u]) {
            if (maxv == -1 || size[v] > size[maxv]) {
                maxv = v;
            }
        }

        dfs(maxv, lv);
        foreach(v; child[u]) {
            if (v == maxv) continue;
            dfs(v, lv+1);
            foreach(c; cols[lv+1]) {
                add(lv, c, false);
            }
            cols[lv+1].length = 0;
        }
    }
    add(lv, col[u], true);

    foreach(q; query[u]) {
        ans[q[0]] = least[lv][q[1]];
    }
}

void main() {
    readf(" %d %d", &n, &m);

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

    edge = new int[][](n, 0);
    foreach(i; 0..n-1) {
        int a, b;
        readf(" %d %d", &a, &b);
        --a, --b;

        edge[a] ~= b;
        edge[b] ~= a;
    }

    query = new int[2][][](n, 0);
    foreach(i; 0..m) {
        int v, k;
        readf(" %d %d", &v, &k);
        --v;

        query[v] ~= [i, k];
    }

    child = new int[][](n, 0);
    size = new int[](n);
    root(0);

    int log = 1;
    while (1 << log < n) { ++log; }

    least = new int[][](log, n+1);
    num = new int[][](log, 10^^5);
    cols = new int[][](log, 0);

    ans = new int[](m);
    dfs(0, 0);

    foreach(a; ans) {
        writeln(a);
    }
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int x1, y1, x2, y2;
		readf !(" %s %s %s %s") (x1, y1, x2, y2);
		writeln (abs (x1 - x2) + abs (y1 - y2) +
		    2 * (x1 != x2 && y1 != y2));
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto x1 = RD!int;
		auto y1 = RD!int;
		auto x2 = RD!int;
		auto y2 = RD!int;

		ans[ti] = abs(x1-x2) + abs(y1-y2);
		if (x1 != x2 && y1 != y2)
			ans[ti] += 2;
	}

	foreach (e; ans)
		writeln(e);

	stdout.flush;
	debug readln;
}


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

immutable int mod = 10 ^^ 9 + 7;

void main ()
{
	int n, m, q;
	while (readf !(" %s %s %s") (n, m, q) > 0)
	{
		auto d = new long [n];
		alias Edge = Tuple !(int, q{u}, int, q{v}, int, q{w});
		auto e = new Edge [m];
		foreach (j; 0..m)
		{
			int u, v, w;
			readf !(" %s %s %s") (u, v, w);
			u -= 1;
			v -= 1;
			e ~= Edge (u, v, w);
			d[u] = max (d[u], w);
			d[v] = max (d[v], w);
		}

		long res = 0;
		auto cur = new long [] [] (2, n);
		int b = 0;
		cur[b][] = long.min;
		cur[b][0] = 0;
		int oldQ = q;
		while (q > 0 && oldQ - q <= 2000)
		{
			b ^= 1;
			cur[b][] = long.min;
			foreach (ref edge; e)
			{
				cur[b][edge.v] = max (cur[b][edge.v],
				    cur[!b][edge.u] + edge.w);
				cur[b][edge.u] = max (cur[b][edge.u],
				    cur[!b][edge.v] + edge.w);
			}
			res = (res + cur[b].maxElement) % mod;
			q -= 1;
		}

		auto p = n.iota.array;
		while (q > 0)
		{
			makeIndex (cur[b], p);
			b ^= 1;
			int lo = 1;
			int hi = q;
			while (lo < hi)
			{
				int me = (lo + hi) / 2;
				cur[b][] = cur[!b][] + d[] * me;
				if (cur[b].countUntil (cur[b].maxElement) ==
				    cur[!b].countUntil (cur[!b].maxElement))
				{
					lo = me + 1;
				}
				else
				{
					hi = me;
				}
			}
			debug {writeln ("q = ", q, ", lo = ", lo);}
			long top = cur[!b].maxElement;
			long add = top % mod;
			int pos = cur[!b].countUntil (top).to !(int);
			long arithm = ((lo - 0L) * (lo - 1L) / 2) % mod;
			res = (res + add * (lo - 1L)) % mod;
			res = (res + d[pos] * arithm) % mod;
			cur[b][] = cur[!b][] + d[] * lo;
			res = (res + cur[b].maxElement) % mod;
			q -= lo;
		}
		writeln (res);
	}
}


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;

bool solve (int [] p)
{
	static immutable int MAX_LIMIT = 60;

	int n = to !(int) (p.length);

	auto q = new int [n];
	foreach (i; 0..n)
	{
		q[p[i]] = i;
	}

	foreach (i; 0..n)
	{
		int k = 0;
		int lim = min (i + 1, n - i);
		if (lim <= 2 * MAX_LIMIT)
		{
			foreach (j; 1..lim)
			{
				if ((q[i - j] < q[i]) != (q[i + j] < q[i]))
				{
					return true;
				}
			}
		}
		else
		{
			foreach (j; 1..MAX_LIMIT)
			{
				if ((q[i - j] < q[i]) != (q[i + j] < q[i]))
				{
					return true;
				}
			}
			foreach (j; lim - MAX_LIMIT..lim)
			{
				if ((q[i - j] < q[i]) != (q[i + j] < q[i]))
				{
					return true;
				}
			}
		}
	}

	foreach (i; 0..n)
	{
		int lo = max (0, i - MAX_LIMIT);
		foreach_reverse (j; lo..i)
		{
			int k = p[i] * 2 - p[j];
			if (k < 0 || n <= k)
			{
				continue;
			}
			if (q[k] > i)
			{
				return true;
			}
		}

		int hi = min (n - 1, i + MAX_LIMIT);
		foreach (j; i + 1..hi + 1)
		{
			int k = p[i] * 2 - p[j];
			if (k < 0 || n <= k)
			{
				continue;
			}
			if (q[k] < i)
			{
				return true;
			}
		}
	}

	return false;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto p = new int [n];
		foreach (ref x; p)
		{
			readf (" %s", &x);
		}
		p[] -= 1;
		writeln (solve (p) ? "YES" : "NO");
	}
}

import std.algorithm;
import std.stdio;

bool solve (int [] p)
{
	static immutable int MAX_LIMIT = 60;

	int n = p.length;

	auto q = new int [n];
	foreach (i; 0..n)
	{
		q[p[i]] = i;
	}

	foreach (i; 0..n)
	{
		int lo = max (0, i - MAX_LIMIT);
		foreach_reverse (j; lo..i)
		{
			int k = p[i] * 2 - p[j];
			if (k < 0 || n <= k)
			{
				continue;
			}
			if (q[k] > i)
			{
				return true;
			}
		}

		int hi = min (n - 1, i + MAX_LIMIT);
		foreach (j; i + 1..hi + 1)
		{
			int k = p[i] * 2 - p[j];
			if (k < 0 || n <= k)
			{
				continue;
			}
			if (q[k] < i)
			{
				return true;
			}
		}
	}

	return false;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto p = new int [n];
		foreach (ref x; p)
		{
			readf (" %s", &x);
		}
		p[] -= 1;
		writeln (solve (p) ? "YES" : "NO");
	}
}

import std.algorithm;
import std.stdio;

bool solve (int [] p)
{
	static immutable int MAX_LIMIT = 60;

	int n = p.length;

	auto q = new int [n];
	foreach (i; 0..n)
	{
		q[p[i]] = i;
	}

	foreach (i; 0..n)
	{
		int k = 0;
		int lim = min (i + 1, n - i);
		if (lim <= 2 * MAX_LIMIT)
		{
			foreach (j; 1..lim)
			{
				if ((q[i - j] < q[i]) != (q[i + j] < q[i]))
				{
					return true;
				}
			}
		}
		else
		{
			foreach (j; 1..MAX_LIMIT)
			{
				if ((q[i - j] < q[i]) != (q[i + j] < q[i]))
				{
					return true;
				}
			}
			foreach (j; lim - MAX_LIMIT..lim)
			{
				if ((q[i - j] < q[i]) != (q[i + j] < q[i]))
				{
					return true;
				}
			}
		}
	}

	return false;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto p = new int [n];
		foreach (ref x; p)
		{
			readf (" %s", &x);
		}
		p[] -= 1;
		writeln (solve (p) ? "YES" : "NO");
	}
}


// Vicfred
// https://codeforces.com/contest/1494/problem/B
// brute force
import std.stdio;

void main() {
    long t;
    readf("%s\n", &t);

while(t--) {
    long n, U, R, D, L;
    readf("%s %s %s %s %s\n", &n, &U, &R, &D, &L);
    bool found = false;
    for(int mask = 0; mask < (1 << 4); mask++) {
        long up     = U;
        long right  = R;
        long down   = D;
        long left   = L;
        if(mask & (1 << 0)) {
            up -= 1;
            left -= 1;
        }
        if(mask & (1 << 1)) {
            left -= 1;
            down -= 1;
        }
        if(mask & (1 << 2)) {
            down -= 1;
            right -= 1;
        }
        if(mask & (1 << 3)) {
            right -= 1;
            up -= 1;
        }
        if((up >= 0 && right >= 0 && down >= 0 && left >= 0) &&
          (up <= n - 2 && right <= n - 2 && down <= n - 2 && left <= n - 2)) {
            found = true;
            break;
        }
    }
    if(found)
        "YES".writeln;
    else
        "NO".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(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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD;
		auto U = RD;
		auto R = RD;
		auto D = RD;
		auto L = RD;

		foreach (i; 0..2^^4)
		{
			auto arr = new int[](4);
			arr[0] = i & 1;
			arr[1] = (i>>1) & 1;
			arr[2] = (i>>2) & 1;
			arr[3] = (i>>3) & 1;
			debug writeln(arr);

			auto u = n - arr[0] - arr[1];
			auto r = n - arr[1] - arr[2];
			auto d = n - arr[2] - arr[3];
			auto l = n - arr[3] - arr[0];
			if (inside(U, 2 - arr[0] - arr[1], u+1) && inside(R, 2 - arr[1] - arr[2], r+1) &&
				inside(D, 2 - arr[2] - arr[3], d+1) && inside(L, 2 - arr[3] - arr[0], l+1))
			{
				ans[ti] = true;
				break;
			}
		}
	}

	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;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto a = readln.splitter.map !(to !(int)).array;
		auto n = a[0] - 2;
		a = a[1..$];
		bool ok = false;
		foreach (i; 0..16)
		{
			auto corners = 4.iota.map !(k => (i >> k) & 1).array;
			auto b = a.dup;
			foreach (k; 0..4)
			{
				b[k] -= corners[k];
				b[(k + 1) & 3] -= corners[k];
			}
			ok |= b.all !(v => 0 <= v && v <= n);
		}
		writeln (ok ? "YES" : "NO");
	}
}