void main() { runSolver(); }

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

  auto solve() {
    auto subSolve() {
      auto S = scan;
      int n = cast(int)S.length;

      auto a = new int[](n + 2);
      auto b = new int[](n + 2);
      auto c = new int[](n + 2);
      foreach(i; 1..n + 1) a[i] = (S[i - 1] - '0') ^ 1;
      foreach(i; 1..n + 1) b[i] = b[i - 1] + a[i];
      foreach(i; iota(n, 0, -1)) c[i] = c[i + 1] + a[i];

      long ans = b[n];
      foreach(i; 0.. b[n] + 1) ans = min(ans, b[n] - (b[i] + c[n - b[n] + i + 1]));
      return ans;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

struct SegTree(alias pred = "a + b", T = long) {
  alias predFun = binaryFun!pred;
  int size;
  T[] data;
  T monoid;

  this(T[] src, T monoid = T.init) {
    this.monoid = monoid;

    for(int i = 2; i < 2L^^32; i *= 2) {
      if (src.length <= i) {
        size = i;
        break;
      }
    }

    data = new T[](size * 2);
    foreach(i, s; src) data[i + size] = s;
    foreach_reverse(b; 1..size) {
      data[b] = predFun(data[b * 2], data[b * 2 + 1]);
    }
  }

  void update(int index, T value) {
    int i = index + size;
    data[i] = value;
    while(i > 0) {
      i /= 2;
      data[i] = predFun(data[i * 2], data[i * 2 + 1]);
    }
  }

  T get(int index) {
    return data[index + size];
  }

  T sum(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = size;

    if (r <= a || b <= l) return monoid;
    if (a <= l && r <= b) return data[k];

    T leftValue = sum(a, b, 2*k, l, (l + r) / 2);
    T rightValue = sum(a, b, 2*k + 1, (l + r) / 2, r);
    return predFun(leftValue, rightValue);
  }
}

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

auto go (T) (T c)
{
	int [] s = [0];
	foreach (ref x; c)
	{
		s ~= s.back + (x ? +1 : -1);
	}
	return s;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto a = readln.strip.map !(q{a - '0'}).array;
		auto n = a.length.to !(int);
		auto s0 = [0];
		foreach (ref c; a)
		{
			s0 ~= s0.back + !c;
		}
		auto s1 = [0];
		foreach (ref c; a)
		{
			s1 ~= s1.back + c;
		}

		bool go (int limit)
		{
			int j = 0;
			int cur0 = 0;
			foreach (i; 0..n)
			{
				j = max (j, i);
				while (j < n && s0[j + 1] - s0[i] <= limit)
				{
					j += 1;
				}
				if (s1[i] - s1[0] + s1[n] - s1[j] <= limit)
				{
					return true;
                                }
			}
			return false;
		}

		int lo = 0;
		int hi = n;
		while (lo < hi)
		{
			int me = (lo + hi) / 2;
			if (go (me))
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}
		writeln (lo);
	}
}

void main() { runSolver(); }

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

  auto solve() {
    auto subSolve() {
      auto S = scan;
      int n = cast(int)S.length;

      auto pre = new int[](n + 1);
      auto suf = new int[](n + 1);
      foreach(i; 1..n + 1) {
        pre[i] = pre[i - 1] + (S[i - 1] == '0');
        suf[i] = suf[i - 1] + (S[n - i] == '0');
      }
      // pre.deb;
      // suf.deb;

      int a, ans;
      a = ans = pre[n];
      foreach(i; 0..a + 1) {
        ans = min(ans, a - pre[i] - suf[a - i]);
      }

      return ans;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

struct SegTree(alias pred = "a + b", T = long) {
  alias predFun = binaryFun!pred;
  int size;
  T[] data;
  T monoid;

  this(T[] src, T monoid = T.init) {
    this.monoid = monoid;

    for(int i = 2; i < 2L^^32; i *= 2) {
      if (src.length <= i) {
        size = i;
        break;
      }
    }

    data = new T[](size * 2);
    foreach(i, s; src) data[i + size] = s;
    foreach_reverse(b; 1..size) {
      data[b] = predFun(data[b * 2], data[b * 2 + 1]);
    }
  }

  void update(int index, T value) {
    int i = index + size;
    data[i] = value;
    while(i > 0) {
      i /= 2;
      data[i] = predFun(data[i * 2], data[i * 2 + 1]);
    }
  }

  T get(int index) {
    return data[index + size];
  }

  T sum(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r < 0) r = size;

    if (r <= a || b <= l) return monoid;
    if (a <= l && r <= b) return data[k];

    T leftValue = sum(a, b, 2*k, l, (l + r) / 2);
    T rightValue = sum(a, b, 2*k + 1, (l + r) / 2, r);
    return predFun(leftValue, rightValue);
  }
}


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

void main() {
  int n = readln.strip.to!int;
  auto v = readln.splitter.map!(to!long).array;
  v.reverse;
  long md = 5000;
  writeln(n+1);
  //make v[i] = -i mod md.
  foreach(i,x;v) {
    //x + i + delta = k*md
    long target = (x+i+md)/md*md;
    long d = target - (x+i+1);
    v[i..$] += d;
    writefln!"1 %d %d" (n-i, d);
    debug {writeln(v);}
  }
  v.reverse;
  v[] %= md;
  debug {writeln(v);}
  writefln!"2 %d %d" (n, md);
}

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;

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

    auto ans = new long[](N);
    long a = 0;

    foreach_reverse (i; 0..N) {
        long m = (A[i] + a) % N;
        long b = ((i - m) % N + N) % N;
        ans[i] = b;
        a += b;
    }

    writeln(N+1);
    foreach (i; 0..N) {
        writeln(1, " ", N-i, " ", ans[N-i-1]);
    }
    writeln(2, " ", N, " ", N);
}

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;

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

    auto ans = new long[](N);
    long a = 0;

    foreach_reverse (i; 0..N) {
        long m = (A[i] + a) % N;
        long b = ((i - m) % N + N) % N;
        ans[i] = b;
        a += b;
    }

    writeln(N+1);
    foreach (i; 0..N) {
        writeln(1, " ", N-i, " ", ans[N-i-1]);
    }
    writeln(2, " ", 1, " ", N);
}


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

immutable int dirs = 4;
immutable int [dirs] dRow = [-1,  0, +1,  0];
immutable int [dirs] dCol = [ 0, -1,  0, +1];

void main ()
{
	int rows, cols;
	while (readf (" %s %s", &rows, &cols) > 0)
	{
		readln;
		string [] a;
		foreach (row; 0..rows)
		{
			a ~= readln.strip;
		}
		int rowS, colS, rowF, colF;
		foreach (row; 0..rows)
		{
			foreach (col; 0..cols)
			{
				if (a[row][col] == 'S')
				{
					rowS = row;
					colS = col;
				}
				if (a[row][col] == 'E')
				{
					rowF = row;
					colF = col;
				}
			}
		}
		auto commands = readln.strip.map !(x => x - '0').array;

		int res = 0;
		auto p = dirs.iota.array;
		do
		{
			int row = rowS;
			int col = colS;
			foreach (dir; commands)
			{
				row += dRow[p[dir]];
				col += dCol[p[dir]];
				if (row < 0 || rows <= row ||
				    col < 0 || cols <= col)
				{
					break;
				}
				if (a[row][col] == '#')
				{
					break;
				}
				if (tuple (row, col) == tuple (rowF, colF))
				{
					break;
				}
			}
			res += (tuple (row, col) == tuple (rowF, colF));
		}
		while (nextPermutation (p));
		writeln (res);
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      auto A = new string[M];
      foreach (x; 0 .. M) {
        A[x] = readToken();
      }
      const S = readToken();

      int sx, sy;
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        if (A[x][y] == 'S') {
          sx = x;
          sy = y;
        }
      }

      int ans;
      auto perm = iota(4).array;
      do {
        bool ok;
        int x = sx, y = sy;
        foreach (s; S) {
          const dir = perm[s - '0'];
          const xx = x + [+1, 0, -1, 0][dir];
          const yy = y + [0, -1, 0, +1][dir];
          if (0 <= xx && xx < M && 0 <= yy && yy < N && A[xx][yy] != '#') {
            x = xx;
            y = yy;
            if (A[x][y] == 'E') {
              ok = true;
              break;
            }
          } else {
            break;
          }
        }
        if (ok) {
          ++ans;
        }
      } while (perm.nextPermutation);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

/+ dub.sdl:
    name "B"
    dependency "dunkelheit" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dkh.foundation, dkh.scanner;

// import dkh.array, dkh.dungeon;

int main() {
    Scanner sc = new Scanner(stdin);
    scope(exit) sc.read!true;

    int h, w;
    sc.read(h, w);
    char[][] g = new char[][](h, w);
    foreach (r; 0..h) sc.read(g[r]);

    string _s;
    sc.read(_s);
    int[] s = _s.map!(c => c-'0').map!(to!int).array;

    int ans = 0;
    foreach (idx; iota(4).permutations) {
        int[2] sv = g.findFirst2D('S');
        int[2] tv = g.findFirst2D('E');
        auto dh = Dungeon(h, w);
        bool f = true;
        foreach (_d; s) {
            int d = idx[_d];
            sv = dh.move4(sv, d);
            if (!dh.isInside(sv)) {
                f = false;
                break;
            }
            if (g.at2D(sv) == '#') {
                f = false;
                break;
            }
            if (sv == tv) break;
        }
        if (!f) continue;
        if (sv != tv) continue;
        ans++;
    }
    writeln(ans);
    return 0;
}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/array.d */
// module dkh.array;

T[N] fixed(T, size_t N)(T[N] a) {return a;}

int[2] findFirst2D(T, U)(in T mp, in U c) {
    import std.conv : to;
    int R = mp.length.to!int;
    int C = mp[0].length.to!int;
    foreach (i; 0..R) {
        foreach (j; 0..C) {
            if (mp[i][j] == c) return [i, j];
        }
    }
    assert(false);
}

auto ref at2D(T, U)(ref T mp, in U idx) {
    return mp[idx[0]][idx[1]];
}

/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/foundation.d */

// module dkh.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/dungeon.d */
// module dkh.dungeon;

immutable static int[2][4] direction4 = [
    [1, 0], [0, 1], [-1, 0], [0, -1],
];

immutable static int[2][8] direction8 = [
    [1, 0],
    [1, 1],
    [0, 1],
    [-1, 1],
    [-1, 0],
    [-1, -1],
    [0, -1],
    [1, -1],
];

static int[2] addInt2(in int[2] a, in int[2] b) {
    int[2] c;
    c[] = a[] + b[];
    return c;
}

struct Dungeon {

    static int[2] move4(int[2] p, int dir) {
        return addInt2(p, direction4[dir]);
    }

    static int[2] move8(int[2] p, int dir) {
        return addInt2(p, direction8[dir]);
    }

    immutable int R, C;

    this(int R, int C) {
        this.R = R;
        this.C = C;
    }

    bool isInside(int[2] p) const {
        int r = p[0], c = p[1];
        return 0 <= r && r < R && 0 <= c && c < C;
    }

    int getID(int[2] p) const {
        int r = p[0], c = p[1];
        return r*R+c;
    }
}

auto neighbors4(int[2] p) {
    static struct Rng {
        int[2] center;
        size_t i;
        bool empty() const { return i == 4;}
        int[2] front() const { return addInt2(center, direction4[i]); }
        void popFront() { i++; }
    }
    return Rng(p, 0);
}

auto neighbors4(int[2] p, in Dungeon dg) {
    static struct Rng {
        int[2] center;
        Dungeon dg;
        size_t i;
        this(in int[2] center, in Dungeon dg) {
            this.center = center;
            this.dg = dg;
            while (!empty() && !dg.isInside(front)) i++;
        }
        bool empty() const { return i == 4;}
        int[2] front() const { return addInt2(center, direction4[i]); }
        void popFront() {
            i++;
            while (!empty() && !dg.isInside(front)) i++;
        }
    }
    return Rng(p, dg);
}

/* IMPORT /Users/yosupo/Program/dunkelheit/source/dkh/scanner.d */
// module dkh.scanner;

// import dkh.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }

    int unsafeRead(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
    void read(bool enforceEOF = false, T, Args...)(ref T x, auto ref Args args) {
        import std.exception;
        enforce(readSingle(x));
        static if (args.length == 0) {
            enforce(enforceEOF == false || !succ());
        } else {
            read!enforceEOF(args);
        }
    }
    void read(bool enforceEOF = false, Args...)(auto ref Args args) {
        import std.exception;
        static if (args.length == 0) {
            enforce(enforceEOF == false || !succ());
        } else {
            enforce(readSingle(args[0]));
            read!enforceEOF(args);
        }
    }
}

/*
This source code generated by dunkelheit and include dunkelheit's source code.
dunkelheit's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dunkelheit)
dunkelheit's License: MIT License(https://github.com/yosupo06/dunkelheit/blob/master/LICENSE.txt)
*/

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

  int h, w; rd(h, w);
  auto c=new char[][](h);
  foreach(i; 0..h) c[i]=readln.chomp.to!(char[]);
  auto t=readln.chomp.to!(char[]);

  import std.typecons;
  alias P=Tuple!(int, "i", int, "j");
  P st, gl;
  foreach(i; 0..h)foreach(j; 0..w){
    if(c[i][j]=='S') st=P(i, j);
    if(c[i][j]=='E') gl=P(i, j);
  }
  auto dir=[[-1, 0], [0, 1], [1, 0], [0, -1]]; // LURD
  auto nums=[0, 1, 2, 3];
  int tot=0;
  do{
    auto cur=st;
    foreach(char ch; t){
      int x=ch-'0';
      auto d=dir[nums[x]];
      auto nex=P(cur.i+d[0], cur.j+d[1]);
      if(nex.i<0 || nex.i>=h || nex.j<0 || nex.j>=w) break;
      if(c[nex.i][nex.j]=='#') break;
      if(c[nex.i][nex.j]=='E'){tot++; break;}
      cur=nex;
    }
  }while(nextPermutation(nums));

  writeln(tot);
}

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

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

immutable int dirs = 4;
immutable int [dirs] dRow = [-1,  0, +1,  0];
immutable int [dirs] dCol = [ 0, -1,  0, +1];

void main ()
{
	int rows, cols;
	while (readf (" %s %s", &rows, &cols) > 0)
	{
		readln;
		string [] a;
		foreach (row; 0..rows)
		{
			a ~= readln.strip;
		}
		int rowS, colS, rowF, colF;
		foreach (row; 0..rows)
		{
			foreach (col; 0..cols)
			{
				if (a[row][col] == 'S')
				{
					rowS = row;
					colS = col;
				}
				if (a[row][col] == 'E')
				{
					rowF = row;
					colF = col;
				}
			}
		}
		auto commands = readln.strip.map !(x => x - '0').array;

		int res = 0;
		auto p = dirs.iota.array;
		do
		{
			int row = rowS;
			int col = colS;
			foreach (dir; commands)
			{
				row += dRow[p[dir]];
				col += dCol[p[dir]];
				if (row < 0 || rows <= row ||
				    col < 0 || cols <= col)
				{
					break;
				}
				if (tuple (row, col) == tuple (rowF, colF))
				{
					break;
				}
			}
			res += (tuple (row, col) == tuple (rowF, colF));
		}
		while (nextPermutation (p));
		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;

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		char [] [] s;
		foreach (i; 0..n)
		{
			s ~= readln.strip.dup;
		}

		bool isValid (int row, int col)
		{
			return 0 <= row && row < n &&
			    0 <= col && col < n;
		}

		auto t = '.'.repeat (n * 2 - 1).array
		    .repeat (n * 2 - 1).map!dup.array;

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				bool canHit = true;
				foreach (row; 0..n)
				{
					foreach (col; 0..n)
					{
						if (s[row][col] == 'o')
						{
							if (isValid (row +
							    drow, col + dcol)
							    && s[row + drow]
							    [col + dcol] ==
							    '.')
							{
								canHit = false;
							}
						}
					}
				}
				if (canHit)
				{
					t[drow + (n - 1)][dcol + (n - 1)] =
					    'x';
				}
			}
		}
		t[n - 1][n - 1] = 'o';
		debug {writefln ("%-(%s\n%)", t);}

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				if (t[drow + (n - 1)][dcol + (n - 1)] == 'x')
				{
					foreach (row; 0..n)
					{
						foreach (col; 0..n)
						{
							if (s[row][col] == 'o'
							    && isValid (row +
							    drow, col + dcol)
							    && s[row + drow]
							    [col + dcol] ==
							    'x')
							{
								s[row + drow]
								    [col +
								    dcol] =
								    '.';
							}
						}
					}
				}
			}
		}
		debug {writefln ("%-(%s\n%)", s);}

		bool ok = true;
		foreach (row; 0..n)
		{
			foreach (col; 0..n)
			{
				ok &= (s[row][col] != 'x');
			}
		}

		if (ok)
		{
			writeln ("YES");
			writefln ("%-(%s\n%)", t);
		}
		else
		{
			writeln ("NO");
		}
	}
}

import std.math;
import std.conv;
import std.array;
import std.stdio;
import std.range;
import std.format;
import std.string;
import std.c.stdio : scanf;
import std.algorithm;

struct S {
	int x, y;
}

void main() {
	auto n = readln.strip.to!int, k = 2 * n - 1;
	S[] ps;
	char[][] arr, r;

	r = uninitializedArray!(typeof(r))(k, k);
	foreach(p; r) p[] = '.';
	r[n - 1][n - 1] = 'o';

	foreach(y; 0..n) {
		arr ~= readln.strip.dup;
		foreach(x, c; arr.back) if(c == 'o') ps ~= S(cast(int)x, cast(int)y);
	}

	foreach(y, a; arr)
	foreach(x, ref c; a)
		if(c == 'x') {
			foreach(s; ps) {
				auto dx = x - s.x, dy = y - s.y;
				bool b;

				foreach(u; ps) {
					auto w = u.x + dx, e = u.y + dy;
					if(w >= 0 && w < n && e >= 0 && e < n && arr[e][w] == '.') b = true;
				}

				if(!b) {
					c = 0;

					foreach(u; ps) {
						auto w = u.x + dx, e = u.y + dy;
						if(w >= 0 && w < n && e >= 0 && e < n) arr[e][w] = 0;
					}

					r[n + dy - 1][n + dx - 1] = 'x';
				}
			}
		}

	if(arr.any!(a => a.canFind('x')))
		write(`NO`);
	else
		writef("YES\n%-(%s\n%)", r);
}

import std.math;
import std.conv;
import std.array;
import std.stdio;
import std.range;
import std.format;
import std.string;
import std.c.stdio : scanf;
import std.algorithm;

struct S {
	int x, y;
}

void main() {
	auto n = readln.strip.to!int, k = 2 * n - 1;
	S[] ps;
	char[][] arr;

	foreach(y; 0..n) {
		arr ~= readln.strip.dup;
		foreach(x, c; arr.back) if(c == 'o') ps ~= S(cast(int)x, cast(int)y);
	}

	auto r = uninitializedArray!(char[][])(k, k);
	foreach(p; r) p[] = '.';
	r[n - 1][n - 1] = 'o';

	foreach(y, a; arr)
	loop: foreach(x, ref c; a)
		if(c == 'x') {
			foreach(s; ps) {
				auto dx = x - s.x, dy = y - s.y;
				bool b;

				foreach(u; ps) {
					auto w = u.x + dx, e = u.y + dy;
					if(w >= 0 && w < n && e >= 0 && e < n && arr[w][e] == '.') b = true;
				}

				if(!b) {
					c = 0;
					r[n + dx - 1][n + dy - 1] = 'x';
					continue loop;
				}
			}
		}

	if(arr.any!(a => a.canFind('x')))
		write(`NO`);
	else
		writef("YES\n%-(%s\n%)", r);
}

import std.math;
import std.conv;
import std.array;
import std.stdio;
import std.range;
import std.format;
import std.string;
import std.c.stdio : scanf;
import std.algorithm;

struct S {
	int x, y;
}

void main() {
	auto n = readln.strip.to!int, k = 2 * n - 1;
	S[] ps;
	string[] arr;

	foreach(y; 0..n) {
		arr ~= readln.strip;
		foreach(x, c; arr.back) if(c == 'o') ps ~= S(cast(int)x, cast(int)y);
	}

	auto r = uninitializedArray!(char[][])(k, k);
	foreach(p; r) p[] = '.';
	r[n - 1][n - 1] = 'o';

	foreach(y, a; arr)
	foreach(x, c; a)
		if(c == 'x') {
			foreach(s; ps) {
				auto dx = x - s.x, dy = y - s.y;

				foreach(u; ps) {
					auto w = u.x + dx, e = u.y + dy;
					if(w >= 0 && w < n && e >= 0 && e < n && arr[w][e] == '.') return write(`NO`);
				}

				r[n + dx - 1][n + dy - 1] = 'x';
			}
		}

	writef("YES\n%-(%s\n%)", r);
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		char [] [] s;
		foreach (i; 0..n)
		{
			s ~= readln.strip.dup;
		}

		bool isValid (int row, int col)
		{
			return 0 <= row && row < n &&
			    0 <= col && col < n;
		}

		auto t = '.'.repeat (n * 2 - 1).array
		    .repeat (n * 2 - 1).map!dup.array;

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				bool canHit = true;
				foreach (row; 0..n)
				{
					foreach (col; 0..n)
					{
						if (s[row][col] == 'o')
						{
							if (isValid (row -
							    drow, col - dcol)
							    && s[row - drow]
							    [col - dcol] ==
							    '.')
							{
								canHit = false;
							}
						}
					}
				}
				if (canHit)
				{
					t[drow + (n - 1)][dcol + (n - 1)] =
					    'x';
				}
			}
		}
		t[n - 1][n - 1] = 'o';
		debug {writefln ("%-(%s\n%)", t);}

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				if (t[drow + (n - 1)][dcol + (n - 1)] == 'x')
				{
					foreach (row; 0..n)
					{
						foreach (col; 0..n)
						{
							if (s[row][col] == 'o'
							    && isValid (row -
							    drow, col - dcol)
							    && s[row - drow]
							    [col - dcol] ==
							    'x')
							{
								s[row - drow]
								    [col -
								    dcol] =
								    '.';
							}
						}
					}
				}
			}
		}
		debug {writefln ("%-(%s\n%)", s);}

		bool ok = true;
		foreach (row; 0..n)
		{
			foreach (col; 0..n)
			{
				ok &= (s[row][col] != 'x');
			}
		}

		if (ok)
		{
			writeln ("YES");
			writefln ("%-(%s\n%)", t);
		}
		else
		{
			writeln ("NO");
		}
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		char [] [] s;
		foreach (i; 0..n)
		{
			s ~= readln.strip.dup;
		}

		bool isValid (int row, int col)
		{
			return 0 <= row && row < n &&
			    0 <= col && col < n;
		}

		auto t = '.'.repeat (n * 2 - 1).array
		    .repeat (n * 2 - 1).map!dup.array;

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				bool canHit = true;
				foreach (row; 0..n)
				{
					foreach (col; 0..n)
					{
						if (s[row][col] == 'o')
						{
							if (isValid (row -
							    drow, col - dcol)
							    && s[row - drow]
							    [col - dcol] ==
							    '.')
							{
								canHit = false;
							}
						}
					}
				}
				if (canHit)
				{
					t[drow + (n - 1)][dcol + (n - 1)] =
					    'x';
				}
			}
		}
		t[n - 1][n - 1] = 'o';
		debug {writefln ("%-(%s\n%)", t);}

		bool ok = true;
		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				if (t[drow + (n - 1)][dcol + (n - 1)] == 'x')
				{
					foreach (row; 0..n)
					{
						foreach (col; 0..n)
						{
							if (s[row][col] == 'o'
							    && isValid (row -
							    drow, col - dcol)
							    && s[row - drow]
							    [col - dcol] ==
							    '.')
							{
								ok = false;
							}
						}
					}
				}
			}
		}

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				if (t[drow + (n - 1)][dcol + (n - 1)] == 'x')
				{
					foreach (row; 0..n)
					{
						foreach (col; 0..n)
						{
							if (s[row][col] == 'o'
							    && isValid (row -
							    drow, col - dcol)
							    && s[row - drow]
							    [col - dcol] ==
							    'x')
							{
								s[row - drow]
								    [col -
								    dcol] =
								    '.';
							}
						}
					}
				}
			}
		}
		debug {writefln ("%-(%s\n%)", s);}

		foreach (row; 0..n)
		{
			foreach (col; 0..n)
			{
				ok &= (s[row][col] != 'x');
			}
		}

		if (ok)
		{
			writeln ("YES");
			writefln ("%-(%s\n%)", t);
		}
		else
		{
			writeln ("NO");
		}
	}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		char [] [] s;
		foreach (i; 0..n)
		{
			s ~= readln.strip.dup;
		}

		bool isValid (int row, int col)
		{
			return 0 <= row && row < n &&
			    0 <= col && col < n;
		}

		auto t = '.'.repeat (n * 2 - 1).array
		    .repeat (n * 2 - 1).map!dup.array;

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				bool canHit = true;
				foreach (row; 0..n)
				{
					foreach (col; 0..n)
					{
						if (s[row][col] == 'o')
						{
							if (isValid (row -
							    drow, col - dcol)
							    && s[row - drow]
							    [col - dcol] ==
							    '.')
							{
								canHit = false;
							}
						}
					}
				}
				if (canHit)
				{
					t[drow + (n - 1)][dcol + (n - 1)] =
					    'x';
				}
			}
		}
		t[n - 1][n - 1] = 'o';

		foreach (drow; -(n - 1)..n)
		{
			foreach (dcol; -(n - 1)..n)
			{
				if (t[drow + (n - 1)][dcol + (n - 1)] == 'x')
				{
					foreach (row; 0..n)
					{
						foreach (col; 0..n)
						{
							if (isValid (row -
							    drow, col - dcol)
							    && s[row - drow]
							    [col - dcol] ==
							    'x')
							{
								s[row - drow]
								    [col -
								    dcol] =
								    '.';
							}
						}
					}
				}
			}
		}

		bool ok = true;
		foreach (row; 0..n)
		{
			foreach (col; 0..n)
			{
				ok &= (s[row][col] != 'x');
			}
		}

		if (ok)
		{
			writeln ("YES");
			writefln ("%-(%s\n%)", t);
		}
		else
		{
			writeln ("NO");
		}
	}
}


module cf_78A;

import std.stdio;

bool isVowel(char letter) {
    return letter == 'a' || letter == 'e' || letter == 'i' ||
           letter == 'o' || letter == 'u';
}

void main() {
    immutable MAX_ROWS = 3;
    immutable ROW_SYLLABLES = [ 5, 7, 5 ];

    string row;

    bool haiku = true;
    for (int i = 0; i < MAX_ROWS; ++i) {
        readf("%s\n", &row);

        int syllables = 0;
        for (int j = 0; j < row.length; ++j) {
            if (isVowel(row[j])) {
                ++syllables;
            }
        }
        if (syllables != ROW_SYLLABLES[i]) {
            haiku = false;
        }
    }

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

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.variant,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
immutable long hashmod=10L^^18+3;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (g==a[l])?l:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------
void main()
{
	/*int n;
	loop:while(read(&n))
	{

	}*/
	auto a=new int[3];
	foreach(i;0..3)
	{
		auto s=readln.strip;
		a[i]=count!q{a=='a' || a=='o' || a=='e' || a=='u' || a=='i'}(s);
	}
	if(a==[5,7,5])writeln("YES");
	else writeln("NO");
}


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;

void solve() {
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    int pos = -1;
    foreach (i; 0..N) if (S[i] == '8') {
        pos = i;
        break;
    }
    if (pos == -1 || N - pos < 11) {
        writeln("NO");
    } else {
        writeln("YES");
    }
}

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

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}

	long t = read.to!long;
	foreach(q; 0 .. t){

		long n = read.to!long;
		string s = readln.chomp;

		bool f = 0;
		for(int i = 0; i < n - 10; i ++) if(s[i] == '8') f = 1;

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto T = RD!int;
	string[] ans;
	foreach (i; 0..T)
	{
		auto N = RD!int;
		auto s = RD!string;
		bool a;
		foreach (j; 0..N)
		{
			if (j > N - 11) break;

			if (s[j] == '8')
			{
				a = true;
				break;
			}
		}
		ans ~= a ? "YES" : "NO";
	}

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

module cf1167a;
import std.stdio;
import std.string : indexOf, strip;
import std.conv : to;
void main() {
	int n;
	char[] buf;
	readln(buf);
	n = buf.strip.to!int;
	debug writeln(n);
	foreach(i; 0..n) {
		int x;
		readln(buf);
		x = buf.strip.to!int;
		readln(buf);
		immutable eight = buf.indexOf('8');
		debug writeln(eight, " ", x);
		if (eight >= 0 && eight + 11 <= x) {
			writeln("YES");
		}else {
			writeln("NO");
		}
	}
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
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,t;
    t=readln.chomp.to!int;
    string s;
    while(t--){
        n=readln.chomp.to!int;
        s=readln.strip;
        long k=s.indexOf('8');
        if(k>=0 && n-k>=11)
            writeln("YES");
        else writeln("NO");
    }
}

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

void main() {
  int nt = readln.strip.to!int;
  foreach (tid; 0 .. nt) {
    int n = readln.strip.to!int;
    auto s = readln.take (n).array;
    auto res = "NO";
    foreach (i; 0 .. n) {
      if (s[i] == '8' && (n - 1) - i >= 10) {
        res = "YES";
        break;
      }
    }
    writeln (res);
  }
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
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,t;
    t=readln.chomp.to!int;
    string s;
    while(t--){
        n=readln.chomp.to!int;
        s=readln.strip;
        long k=s.indexOf('8');
        if(n>=11 && k>=0 && n-k+1>=10)
            writeln("YES");
        else writeln("NO");
    }
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
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,t;
    t=readln.chomp.to!int;
    string s;
    while(t--){
        n=readln.chomp.to!int;
        s=readln.strip;
        long k=s.indexOf('8');
        if(n>=11 && k>=0 && n-k>=10)
            writeln("YES");
        else writeln("NO");
    }
}


import std.stdio;

void main()
{
    int curvas, curvasPerigosas;
    int[1001] x;
    int[1001] y;

    readf(" %s ", &curvas);
    for(int i = 0; i <= curvas; ++i)
    {
        readf(" %s %s", &x[i], &y[i]);
    }

    for (int i = 1; i < curvas; ++i)
    {
        if (y[i] - y[i - 1] > 0 && x[i + 1] - x[i] < 0
        || x[i] - x[i - 1] > 0 && y[i + 1] - y[i] > 0
        || y[i] - y[i - 1] < 0 && x[i + 1] - x[i] > 0
        || x[i] - x[i - 1] < 0 && y[i + 1] - y[i] < 0)
            ++curvasPerigosas;
    }

    writeln(curvasPerigosas);
}

import std.stdio;
import std.typecons;

void main()
{
    int n;
    Tuple!(int, int)[] points;

    readf("%d", &n);
    points = new Tuple!(int, int)[n + 2];
    for (int i = 0; i < n + 1; i++) {
        readf(" %d %d", &points[i][0], &points[i][1]);
    }
    points[n + 1] = points[1];

    int result = 0;
    for (int i = 1; i <= n; i++) {
        Tuple!(int, int) d[2];

        d[0][0] = points[i][0] - points[i - 1][0];
        d[0][1] = points[i][1] - points[i - 1][1];
        d[1][0] = points[i + 1][0] - points[i][0];
        d[1][1] = points[i + 1][1] - points[i][1];

        if (d[0][0] * d[1][1] - d[1][0] * d[0][1] > 0) {
            result++;
        }
    }

    writef("%d\n", result);
}

import std.stdio;

void main()
{
    int curvas, curvasPerigosas;
    int[1001] x;
    int[1001] y;

    readf(" %s ", &curvas);
    readf(" %s %s", &x[0], &y[0]);
    for(int i = 1; i <= curvas; ++i)
    {
        readf(" %s %s", &x[i], &y[i]);
        if (y[i] - y[i - 1] > 0 && x[i + 1] - x[i] < 0
        || x[i] - x[i - 1] > 0 && y[i + 1] - y[i] > 0
        || y[i] - y[i - 1] < 0 && x[i + 1] - x[i] > 0
        || x[i] - x[i - 1] < 0 && y[i + 1] - y[i] < 0)
            ++curvasPerigosas;
    }

    writeln(curvasPerigosas);
}


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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long s, n;
    readf!" %d %d "(s, n);
    long[] ans;
    auto numpool = redBlackTree!("a<b", true, long)();
    long mul = 1;
    while (s > 0) {
      long digit = s % 10;
      foreach (i ; 0 .. digit)
        numpool.insert(mul);
      s /= 10;
      mul *= 10;
    }
    while (true) {
      if (n == 1) {
        long final_num = 0;
        while (!numpool.empty) {
          final_num += numpool.front;
          numpool.removeFront;
        }
        ans ~= final_num;
        break;
      }
      if (n <= numpool.length || numpool.front == 1) {
        ans ~= numpool.front;
        numpool.removeFront;
        n--;
        continue;
      }
      long tmp = numpool.front;
      numpool.removeFront;
      assert(tmp > 0 && tmp % 10 == 0);
      foreach (i ; 0 .. 10)
        numpool.insert(tmp / 10);
    }
    writeln(ans.map!text.joiner(" "));
  }
}

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

		auto a = new int[](s.length);
		foreach (i; 0..s.length)
			a[i] = s[i]-'0';

		long cnt = a.sum;
		while (n > cnt)
		{
			int p = cast(int)a.length-2;
			while (a[p] == 0)
				--p;
			--a[p];
			a[p+1] += 10;
			cnt += 9;
		}

		(){
		foreach (i; 0..a.length)
		{
			foreach (j; 0..a[i])
			{
				ans[ti] ~= 10^^(a.length-i-1);
				--a[i];
				if (ans[ti].length == n) return;
			}
		}}();
		foreach (i; 0..a.length)
		{
			foreach (j; 0..a[i])
			{
				ans[ti][j%n] += 10^^(a.length-i-1);
			}
		}
	}

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

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

bool solve(long s, long n, ref long[] a)
{
  long[] ans;
  foreach (i ; 0 .. n - 1) {
    ans ~= 1;
  }
  if (s - (n - 1) <= 0)
    return false;

  ans ~= s - (n - 1);
  if (ans.length > 1 && ans.back >= 10) {
    ans.front += ans.back % 10;
    ans.back -= ans.back % 10;
  }
  a = ans;
  return true;
}

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long s, n;
    readf!" %d %d "(s, n);
    long[] best_a;
    long best_div = 1;
    solve(s, n, best_a);
    long div = 10;
    while (s / div * div == s) {
      long[] a;
      if (solve(s / div, n, a)) {
        best_a = a;
        best_div = div;
      }
      div *= 10;
    }
    writeln(best_a.map!(x => (x * best_div).text).joiner(" "));
/*
    long[] ans;
    foreach (i ; 0 .. n - 1) {
      ans ~= 1;
    }
    ans ~= s - (n - 1);
    if (ans.length > 1 && ans.back >= 10) {
      ans.front += ans.back % 10;
      ans.back -= ans.back % 10;
    }
    writeln(ans.map!text.joiner(" "));
*/
  }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long s, n;
    readf!" %d %d "(s, n);
    long[] ans;
    foreach (i ; 0 .. n - 1) {
      ans ~= 1;
    }
    ans ~= s - (n - 1);
    if (ans.length > 1 && ans.back >= 10) {
      ans.front += ans.back % 10;
      ans.back -= ans.back % 10;
    }
    writeln(ans.map!text.joiner(" "));
  }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long s, n;
    readf!" %d %d "(s, n);
    long[] ans;
    foreach (i ; 0 .. n - 1) {
      ans ~= 1;
    }
    ans ~= s - (n - 1);
    if (ans.length > 1) {
      ans.front += ans.back % 10;
      ans.back -= ans.back % 10;
    }
    writeln(ans.map!text.joiner(" "));
  }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long s, n;
    readf!" %d %d "(s, n);
    long[] ans;
    foreach (i ; 0 .. n - 1) {
      ans ~= 1;
    }
    ans ~= s - (n - 1);
    ans.front += ans.back % 10;
    ans.back -= ans.back % 10;
    writeln(ans.map!text.joiner(" "));
  }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long s, n;
    readf!" %d %d "(s, n);
    long[] ans;
    foreach (i ; 0 .. n - 1) {
      ans ~= 1;
    }
    ans ~= s - (n - 1);
    writeln(ans.map!text.joiner(" "));
  }
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		long x, y;
		foreach (i; 0..n)
		{
			if (x == a[i])
				++x;
			else if (y == a[i])
				++y;
		}
		ans[ti] = x+y;
	}

	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;
bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1;
if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.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 gen(){
}
void jury(){
}
void solve(){
    foreach(_; 0 .. scan!int){
        int n = scan!int;
        int[] as = scan!int(n);

        int[int] acnt;
        foreach(a; as){
            if(a !in acnt) acnt[a] = 0;
            acnt[a] += 1;
        }

        int ans = 0;
        int i = 0;

        while(i in acnt && acnt[i] >= 2) i ++;
        ans += i;

        while(i in acnt && acnt[i] >= 1) i ++;
        ans += i;

        ans.writeln;
    }
}


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

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto B = N.iota.map!(i => tuple(A[i], i)).array;
    B.sort();

    auto used = new int[](N);
    auto uf = new UnionFind(N);

    used[B[0][1]] = true;
    int max_locs = 1;
    int ans = B[0][0] + 1;
    uf.cnt = 1;
    uf.lens[1] = 1;

    foreach (i; 1..N) {
        int v = B[i][0];
        int n = B[i][1];
        used[n] = true;
        uf.cnt += 1;
        uf.lens[1] += 1;
        int lens = uf.lens[1];
        if (n > 0 && used[n-1]) lens = uf.unite(n, n-1);
        if (n < N-1 && used[n+1]) lens = uf.unite(n, n+1);
        if (lens == uf.cnt) {
            if (uf.cnt > max_locs) {
                max_locs = uf.cnt;
                ans = v + 1;
            }
        }
    }

    ans.writeln;
}

class UnionFind {
    int N;
    int[] table;
    Tuple!(int, int)[] segs;
    int[] lens;
    int cnt;

    this(int n) {
        N = n;
        table = new int[](N);
        fill(table, -1);
        segs = N.iota.map!(i => tuple(i, i)).array;
        lens = new int[](N+10);
        cnt = 0;
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    int unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return -1;
        if (table[x] > table[y]) swap(x, y);
        cnt -= 1;
        table[x] += table[y];
        lens[segs[x][1] - segs[x][0] + 1] -= 1;
        lens[segs[y][1] - segs[y][0] + 1] -= 1;
        segs[x] = tuple(min(segs[x][0], segs[y][0]), max(segs[x][1], segs[y][1]));
        lens[segs[x][1] - segs[x][0] + 1] += 1;
        table[y] = x;
        return lens[segs[x][1] - segs[x][0] + 1];
    }
}

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

auto biggestBit(T)(T t)
{
  debug assert(t != 0);
  int bitpos = -1;
  while(t)
    {
      t >>= 1;
      bitpos++;
    }
  return bitpos;
}

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

  auto n = next!int;
  auto a = next!int(n);
  auto numRows = n.biggestBit;
  auto maxIn = new int[][](numRows + 1, n);
  foreach(r; 0 .. numRows + 1)
    maxIn[r][] = -1;
  foreach(i; 0 .. n)
    maxIn[0][i] = i;
  auto maxInd(int i, int j)
  {
    return a[i] > a[j]? i : j;
  }
  foreach(p; 1 .. numRows + 1)
    {
      foreach(i; 0 .. n)
        {
          if ((i + (1 << (p)) - 1) >= n)
            continue;
          maxIn[p][i] = maxInd(maxIn[p - 1][i], maxIn[p - 1][i + (1 << (p - 1))]);
        }
    }
  debug writeln(maxIn);
  auto maxInRange(int s, int e)
  {
    auto len = e - s + 1;
    auto p = len.biggestBit;
    auto lenwithoutbb = len - (1 << p);
    return maxInd(maxIn[p][s], maxIn[p][s + lenwithoutbb]);
  }

  auto poolSize = new int[](n);
  void doPoolSize(int s, int e)
  {
    if (s > e)
      return;
    auto len = e - s + 1;
    auto mxi = maxInRange(s, e);
    debug writeln("in range ", s, " - ", e, " max is ", a[mxi], " with len ", len);
    assert(mxi >= s && mxi <= e);
    poolSize[mxi] = len;
    doPoolSize(s, mxi - 1);
    doPoolSize(mxi + 1, e);
  }
  doPoolSize(0, n - 1);
  debug writeln(poolSize);
  auto sortedIndexes = iota(0, n).array.sort!((i, j) => a[i] < a[j]).array;
  auto lastInd = new int[](n + 1);
  auto sizeCnt = new int[](n + 1);
  auto bestBySize = new int[](n + 1);
  int bestSize = 0;
  lastInd[] = -1;
  debug foreach(i, ind; sortedIndexes)
    {
      write(poolSize[ind], " ");
    }
  debug writeln;
  foreach(i, ind; sortedIndexes)
    {
      auto s = poolSize[ind];
      sizeCnt[s]++;
      auto l = lastInd[s];
      debug if (s == 3) {writeln("sizeCnt ", s, " ", sizeCnt[s], " i ", i);}
      if (sizeCnt[s] * s - 1 == i)
        {
          bestBySize[s] = sizeCnt[s];
          bestSize = max(bestBySize[s], bestSize);
          lastInd[s] = cast(int)i;
        }
    }
  debug writeln("bestBySize: ", bestBySize);
  int minimalUp = int.max;
  foreach(s, b; bestBySize)
    {
      if (s != 0 && b == bestSize)
        {
          minimalUp = min(minimalUp, s * b - 1);
        }
    }
  auto minV = a[sortedIndexes[minimalUp]];
  (minV + 1).writeln;
}

// INPUT

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

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

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

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

auto biggestBit(T)(T t)
{
  debug assert(t != 0);
  int bitpos = -1;
  while(t)
    {
      t >>= 1;
      bitpos++;
    }
  return bitpos;
}

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

  auto n = next!int;
  auto a = next!int(n);
  auto numRows = n.biggestBit;
  auto maxIn = new int[][](numRows + 1, n);
  foreach(r; 0 .. numRows + 1)
    maxIn[r][] = -1;
  foreach(i; 0 .. n)
    maxIn[0][i] = i;
  foreach(p; 1 .. numRows + 1)
    {
      foreach(i; 0 .. n)
        {
          if ((i + (1 << (p)) - 1) >= n)
            continue;
          auto v0 = a[maxIn[p - 1][i]];
          auto v1 = a[maxIn[p - 1][i + (1 << (p - 1))]];
          if (v0 > v1)
            maxIn[p][i] = maxIn[p - 1][i];
          else
            maxIn[p][i] = maxIn[p - 1][i + (1 << (p - 1))];
        }
    }
  debug writeln(maxIn);
  auto maxInRange(int s, int e)
  {
    auto len = e - s + 1;
    auto p = len.biggestBit;
    auto lenwithoutbb = len - (1 << p);
    auto v0 = a[maxIn[p][s]];
    auto v1 = a[maxIn[p][s + lenwithoutbb]];
    if (v0 > v1)
      return maxIn[p][s];
    return maxIn[p][s + lenwithoutbb];
  }

  auto poolSize = new int[](n);
  void doPoolSize(int s, int e)
  {
    if (s > e)
      return;
    auto len = e - s + 1;
    auto mxi = maxInRange(s, e);
    debug writeln("in range ", s, " - ", e, " max is ", a[mxi], " with len ", len);
    assert(mxi >= s && mxi <= e);
    poolSize[mxi] = len;
    doPoolSize(s, mxi - 1);
    doPoolSize(mxi + 1, e);
  }
  doPoolSize(0, n - 1);
  debug writeln(poolSize);
  auto sortedIndexes = iota(0, n).array.sort!((i, j) => a[i] < a[j]).array;
  auto lastInd = new int[](n + 1);
  auto bestBySize = new int[](n + 1);
  int bestSize = 0;
  lastInd[] = -1;
  debug foreach(i, ind; sortedIndexes)
    {
      write(poolSize[ind], " ");
    }
  debug writeln;
  foreach(i, ind; sortedIndexes)
    {
      auto s = poolSize[ind];
      auto l = lastInd[s];
      if (i - l == s)
        {
          bestBySize[s]++;
          bestSize = max(bestBySize[s], bestSize);
          lastInd[s] = cast(int)i;
        }
    }
  debug writeln(bestBySize);
  int minimalUp = int.max;
  foreach(s, b; bestBySize)
    {
      if (s != 0 && b == bestSize)
        {
          minimalUp = min(minimalUp, s * b - 1);
        }
    }
  auto minV = a[sortedIndexes[minimalUp]];
  (minV + 1).writeln;
}

// INPUT

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

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

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

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

auto biggestBit(T)(T t)
{
  debug assert(t != 0);
  int bitpos = -1;
  while(t)
    {
      t >>= 1;
      bitpos++;
    }
  return bitpos;
}

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

  auto n = next!int;
  auto a = next!int(n);
  auto numRows = n.biggestBit;
  auto maxIn = new int[][](numRows + 1, n);
  foreach(r; 0 .. numRows + 1)
    maxIn[r][] = -1;
  foreach(i; 0 .. n)
    maxIn[0][i] = i;
  auto maxInd(int i, int j)
  {
    return a[i] > a[j]? i : j;
  }
  foreach(p; 1 .. numRows + 1)
    {
      foreach(i; 0 .. n)
        {
          if ((i + (1 << (p)) - 1) >= n)
            continue;
          maxIn[p][i] = maxInd(maxIn[p - 1][i], maxIn[p - 1][i + (1 << (p - 1))]);
        }
    }
  debug writeln(maxIn);
  auto maxInRange(int s, int e)
  {
    auto len = e - s + 1;
    auto p = len.biggestBit;
    auto lenwithoutbb = len - (1 << p);
    return maxInd(maxIn[p][s], maxIn[p][s + lenwithoutbb]);
  }

  auto poolSize = new int[](n);
  void doPoolSize(int s, int e)
  {
    if (s > e)
      return;
    auto len = e - s + 1;
    auto mxi = maxInRange(s, e);
    debug writeln("in range ", s, " - ", e, " max is ", a[mxi], " with len ", len);
    assert(mxi >= s && mxi <= e);
    poolSize[mxi] = len;
    doPoolSize(s, mxi - 1);
    doPoolSize(mxi + 1, e);
  }
  doPoolSize(0, n - 1);
  debug writeln(poolSize);
  auto sortedIndexes = iota(0, n).array.sort!((i, j) => a[i] < a[j]).array;
  auto sizeCnt = new int[](n + 1);
  auto bestBySize = new int[](n + 1);
  int bestSize = 0;
  debug foreach(i, ind; sortedIndexes)
    {
      write(poolSize[ind], " ");
    }
  debug writeln;
  foreach(i, ind; sortedIndexes)
    {
      auto s = poolSize[ind];
      sizeCnt[s]++;
      debug if (s == 3) {writeln("sizeCnt ", s, " ", sizeCnt[s], " i ", i);}
      if (sizeCnt[s] * s - 1 == i)
        {
          bestBySize[s] = sizeCnt[s];
          bestSize = max(bestBySize[s], bestSize);
        }
    }
  debug writeln("bestBySize: ", bestBySize);
  int minimalUp = int.max;
  foreach(s, b; bestBySize)
    {
      if (s != 0 && b == bestSize)
        {
          minimalUp = min(minimalUp, s * b - 1);
        }
    }
  auto minV = a[sortedIndexes[minimalUp]];
  (minV + 1).writeln;
}

// INPUT

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

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

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

import 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, std.regex;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto B = N.iota.map!(i => tuple(A[i], i)).array;
    B.sort();

    auto used = new int[](N);
    auto uf = new UnionFind(N);

    used[0] = true;
    int max_locs = 1;
    int ans = B[0][1] + 1;
    uf.cnt = 1;

    foreach (i; 1..N) {
        int v = B[i][0];
        int n = B[i][1];
        used[n] = true;
        uf.cnt += 1;
        int lens = -1;
        if (n > 0 && used[n-1]) lens = uf.unite(n, n-1);
        if (n < N-1 && used[n+1]) lens = uf.unite(n, n+1);
        if (lens > 0 && lens == uf.cnt) {
            if (uf.cnt > max_locs) {
                max_locs = uf.cnt;
                ans = v + 1;
            }
        }
    }

    ans.writeln;
}

class UnionFind {
    int N;
    int[] table;
    Tuple!(int, int)[] segs;
    int[] lens;
    int cnt;

    this(int n) {
        N = n;
        table = new int[](N);
        fill(table, -1);
        segs = N.iota.map!(i => tuple(i, i)).array;
        lens = new int[](N+1);
        cnt = 0;
    }

    int find(int x) {
        return table[x] < 0 ? x : (table[x] = find(table[x]));
    }

    int unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return -1;
        if (table[x] > table[y]) swap(x, y);
        cnt -= 1;
        table[x] += table[y];
        lens[segs[x][1] - segs[x][0] + 1] -= 1;
        lens[segs[y][1] - segs[y][0] + 1] -= 1;
        segs[x] = tuple(min(segs[x][0], segs[y][0]), max(segs[x][1], segs[y][1]));
        lens[segs[x][1] - segs[x][0] + 1] += 1;
        table[y] = x;
        return lens[segs[x][1] - segs[x][0] + 1];
    }
}


import std.stdio, std.string;
T x(T)(T a) {
	if (a.length&1) return a;
	auto b=a[0..$/2].x, c=a[$/2..$].x;
	return b<c ? b~c : c~b;
}
void main() {writeln (readln.strip.x==readln.strip.x ? "YES" : "NO");}

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

string canonize (string a)
{
	if (a.length % 2 == 0)
	{
		string b = canonize (a[0..$ / 2]);
		string c = canonize (a[$ / 2..$]);
		return min (b, c) ~ max (b, c);
	}
	return a;
}

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		string t = readln.strip;
		writeln ((canonize (s) == canonize (t)) ? "YES" : "NO");
	}
}

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

T x (T) (T a)
{
	if (a.length & 1) return a;
	auto b = x (a[0..$ / 2]), c = x (a[$ / 2..$]);
	return min (b, c) ~ max (b, c);
}

void main ()
{
	writeln (readln.strip.x == readln.strip.x ? "YES" : "NO");
}

T x(T)(T a){if(a.length&1)return a;auto b=a[0..$/2].x,c=a[$/2..$].x;return b<c?b~c:c~b;}
void main(){import std.stdio;writeln(readln[0..$-1].x==readln[0..$-1].x?"YES":"NO");}

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

bool ok (string a, string b)
{
	if (a.length % 2 == 0)
	{
		if ((a[0..$ / 2] < a[$ / 2..$]) ==
		    (b[0..$ / 2] < b[$ / 2..$]))
		{
			return ok (a[0..$ / 2], b[0..$ / 2]) &&
			    ok (a[$ / 2..$], b[$ / 2..$]);
		}
		else
		{
			return ok (a[0..$ / 2], b[$ / 2..$]) &&
			    ok (a[$ / 2..$], b[0..$ / 2]);
		}
	}
	else
	{
		return a == b;
	}
}

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		string t = readln.strip;
		writeln (ok (s, t) ? "YES" : "NO");
	}
}


import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

const long mod = 1_000_000_007;

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}

	int n = read.to!int;
	long t = read.to!long;

	long[] ss, ds;
	foreach(i; 0 .. n){
		ss ~= read.to!long;
		ds ~= read.to!long;
	}

	long bestx = 10_000_000, ans = -1;
	foreach(i; 0 .. n){
		long s = ss[i], d = ds[i];
		long x;
		if(s >= t) x = s;
		else x = s + (t - s + d - 1) / d * d;

		if(x < bestx){
			bestx = x;
			ans = i + 1;
		}
	}

	ans.writeln;

}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto T = s[1].to!long;

    long idx = -1;
    long ans = 1L << 59;

    foreach (i; 0..N) {
        s = readln.split.map!(to!int);
        auto a = s[0].to!long;
        auto b = s[1].to!long;
        long tmp;
        if (a >= T) {
            tmp = a;
        } else {
            long m1 = T % b;
            long m2 = a % b;
            if (m1 <= m2) {
                tmp = T + m2 - m1;
            } else {
                tmp = T + b - m1 + m2;
            }
        }
        if (tmp < ans) {
            ans = tmp;
            idx = i + 1;
        }
    }

    idx.writeln;
}

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

string FMT_F = "%.10f";

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

long mod = 10^^9 + 7;
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;
	auto T = RD;

	long best = long.max;
	long ans;
	foreach (i; 0..N)
	{
		auto s = RD;
		auto d = RD;
		auto x = s - T;
		auto y = abs(x / d);
		if (x < 0)
		{
			x += (y+1) * d;
			x %= d;
		}
		if (x < best)
		{
			best = x;
			ans = i + 1;
		}
		debug writeln(i, " ", best, " ", ans);
	}

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


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

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

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

    int[] a = readln.split.map!(to!int).array;

    int idx = 0;
    while(k > 0 && idx < n) {
        while(a[idx] <= 0) {
            idx += 1;
        }
        if (idx >= n - 1) break;
        a[idx]--;
        a[$ - 1]++;
        k -= 1;
    }

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

		foreach (i; 0..n-1)
		{
			auto x = min(k, a[i]);
			a[i] -= x;
			a[$-1] += x;
			k -= x;
		}
		ans[ti] = a;
	}

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

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp.splitter(" ").array;
        int k = to!int(param[1]);
        auto arr = readln().chomp.split(" ").map!(to!int).array;
        int e = arr.length-1;
        int b = 0;

        while(b < e && k > 0)
        {
            if(arr[b] > 0 && arr[e] < int.max)
            {
                arr[b]--;
                arr[e]++;
                k--;
            }
            else if(arr[b] <= 0)
            {
                b++;
            }
            else
            {
                e--;
            }
        }

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

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

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

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

    int[] a = readln.split.map!(to!int).array;

    int idx = 0;
    while(k > 0 && idx < n - 1) {
        while(a[idx] <= 0) {
            idx += 1;
        }
        a[idx]--;
        a[$ - 1]++;
        k -= 1;
    }

    foreach(item; a)
        writef("%s ", item);
    "".writeln;
}
}

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

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

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

    int[] a = readln.split.map!(to!int).array;

    int idx = 0;
    while(k > 0 && idx <= n - 1) {
        while(a[idx] <= 0) {
            idx += 1;
        }
        a[idx]--;
        a[$ - 1]++;
        k -= 1;
    }

    foreach(item; a)
        writef("%s ", item);
    "".writeln;
}
}

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

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

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

    int[] a = readln.split.map!(to!int).array;

    int idx = 0;
    while(k-- && idx <= n - 1) {
        while(a[idx] <= 0) {
            idx += 1;
        }
        a[idx]--;
        a[$ - 1]++;
    }

    foreach(item; a)
        writef("%s ", item);
    "".writeln;
}
}

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

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

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

    int[] a = readln.split.map!(to!int).array;

    int idx = 0;
    while(k-- && idx <= n - 1) {
        if(a[idx] <= 0) {
            idx += 1;
        }
        a[idx]--;
        a[$ - 1]++;
    }

    foreach(item; a)
        writef("%s ", item);
    "".writeln;
}
}

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp.splitter(" ").array;
        int k = to!int(param[1]);
        auto arr = readln().chomp.split(" ").map!(to!int).array;
        int e = arr.length-1;
        int b = 0;

        while(b < e && k > 0)
        {
            if(arr[b] > 0)
            {
                arr[b]--;
                b++;
            }
            else
            {
                b++;
            }

            if(arr[e] < 10)
            {
                arr[e]++;
            }
            else
            {
                e--;
            }

            k--;
        }

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

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp.splitter(" ").array;
        int k = to!int(param[1]);
        auto arr = readln().chomp.split(" ").map!(to!int).array;
        int e = arr.length-1;
        int b = 0;

        while(b < e && k > 0)
        {
            if(arr[b] > 0)
            {
                arr[b]--;
                arr[e]++;
                e--;
                k--;
            }
            b++;
        }

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

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp.splitter(" ").array;
        int k = to!int(param[1]);
        auto arr = readln().chomp.split(" ").map!(to!int).array;
        int e = arr.length-1;
        int b = 0;

        while(b < e && k > 0)
        {
            while(b < arr.length && arr[b] == 0) b++;
            if(b == arr.length) continue;

            arr[b]--;
            arr[e]++;
            b++;
            e--;
            k--;
        }

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

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp.splitter(" ").array;
        int k = to!int(param[1]);
        auto arr = readln().chomp.split(" ").map!(to!int).array;
        int e = arr.length-1;
        int b = 0;

        while(b < e && k > 0)
        {
            while(b < arr.length && arr[b] <= k) b++;
            if(b == arr.length) continue;

            arr[b]--;
            arr[e]++;
            b++;
            e--;
            k--;
        }

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

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.uni;
import std.math;
import std.container.rbtree;
import std.range;

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp.splitter(" ").array;
        int k = to!int(param[1]);
        auto arr = readln().chomp.split(" ").map!(to!int).array;
        int e = arr.length-1;
        int b = 0;

        while(b < e && k > 0)
        {
            while(b < arr.length && arr[b] <= k) b++;
            if(b == arr.length) continue;

            arr[b]--;
            arr[e]++;
            b++;
            e--;
            k--;
        }

        writeln(arr[]);
    }
}


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; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

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

void main()
{
	auto t = RD!int;
	auto ans = new string[](t);
	foreach (ti; 0..t)
	{
		auto s = RD!string;
		if (s.length >= 2)
		{
			if (s[$-2..$] == "po")
			{
				ans[ti] = "FILIPINO";
				continue;
			}
			if (s.length >= 4)
			{
				if (s[$-4..$] == "desu" || s[$-4..$] == "masu")
				{
					ans[ti] = "JAPANESE";
					continue;
				}
				if (s.length >= 5)
				{
					if (s[$-5..$] == "mnida")
					{
						ans[ti] = "KOREAN";
						continue;
					}
				}
			}
		}
	}
	foreach (e; ans)
		writeln(e);

	stdout.flush;
	debug readln;
}

module _template;
import std.stdio;
import std.string;
import std.algorithm;
import std.container;
import std.range;
import std.math;
import std.numeric;
import std.conv;
import std.typecons;
import std.format;

struct IO {
        string read_string() {
                while (tokens.empty) {
                tokens = readln.split;
        }
        auto token = tokens.front;
                tokens.popFront;
                return token;
        }

        int read_int() {
                return read_string.to!int;
        }

        double read_double() {
                return read_string.to!double;
        }

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        t = cin.read_int;
        while (t--) {
                string s = cin.read_string;
                if (endsWith(s, "po")) writeln("FILIPINO");
                else if (endsWith(s, "desu") || endsWith(s, "masu")) writeln("JAPANESE");
                else writeln("KOREAN");
        }
}


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

void main ()
{
	readln;
	int n;
	while (readf !(" %s") (n) > 0)
	{
		int res = 0;
		foreach (i; 0..n)
		{
			auto add = (i + 1 == n || i + 1 < n / 2);
			res += (2 << i) * (add ? +1 : -1);
		}
		writeln (res);
	}
}

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;
		long x, y;
		foreach (i; 0..n-1)
		{
			if (i < n/2-1)
			{
				x += 2^^(i+1);
			}
			else
			{
				y += 2^^(i+1);
			}
		}
		x += 2^^n;
		ans[ti] = x - y;
	}

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

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_t; 0..T) {
        auto N = readln.chomp.to!long;
        long a = 2L^^N, b;
        foreach (i; 1..N/2) {
            a += 2L^^i;
        }
        foreach (i; N/2..N) {
            b += 2L^^i;
        }
        writeln(a - b);
    }
}


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

void main() {
    int N, X; scanf("%d %d\n", &N, &X);
    auto A = readln.chomp.split(" ").map!(to!int).array;
    auto B = readln.chomp.split(" ").map!(to!int).array;

    A.sort;
    B.sort;

    void first() {
        auto a = A.dup,
             b = B.dup;
        int L = a.back + b.back;
        a.popBack; b.popBack;
        int bi = 0;
        int ans = 1;
        for (int ai = N - 2; ai >= 0; ai--) {
            if (a[ai] + b[bi] <= L) {
                bi++;
            } else {
                ans++;
            }
        }
        write(ans);
    }
    void second() {
        auto a = A.dup,
             b = B.dup;
        auto set = redBlackTree!(true)(b);
        int ans = 0;
        for (int ai = 0; ai < N; ai++) {
            auto r = set.upperBound(X - a[ai] - 1);
            if (r.empty) continue;
            set.removeKey(r.front);
            ans++;
        }
        write(ans);
    }
    first(); write(" "); second();
    writeln;
}

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

immutable int MX = 2 ^^ 14 - 1;

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

    auto arr = new int[][] (2);
    foreach (i; 0 .. 2) {
        arr[i] = readln.chomp.split.map!(to!int).array;
        arr[i].sort().reverse();
    }

    debug { arr[0].writeln; }

    int chk(int idx1) {
        int ans = 0, mv = n-1;
        foreach (i; 0 .. n) {
            while (mv >= 0 && arr[idx1][i] + arr[1-idx1][mv] < x) { --mv; }

            if (mv >= 0) {
                ++ans;
                --mv;
            }
        }

        return ans;
    }

    int ans = max(chk(0), chk(1));
    writeln("1 ", ans);
}