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

struct point {
    int x, y;
}
void main() {
    int t = readln.chomp.to!int;

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

    int sum_a = a.sum;
    int sum_b = b.sum;

    if(sum_a != sum_b) {
        "-1".writeln;
        continue;
    }

    point[] idx;

    int i, j;
    while(i < n && j < n) {
        while(a[i] <= b[i]) {
            i += 1;
            if(i >= n || j >= n)
                break;
        }
        while(a[j] >= b[j]) {
            j += 1;
            if(i >= n || j >= n)
                break;
        }
        if(i >= n || j >= n)
            break;
        idx ~= point(i, j);
        a[i] -= 1;
        a[j] += 1;
    }

    if(a == b) {
        idx.length.writeln;
        idx.each!(it => "%s %s".writefln(it.x + 1, it.y + 1));
    } else {
        "-1".writeln;
    }
}
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!long).array;
    auto b = readln.strip.split.map!(to!long).array;
    long suma = a.sum;
    long sumb = b.sum;
    if (suma != sumb) {
      writeln(-1);
      continue;
    }
    long m = 0;
    foreach (i ; 0 .. n) {
      if (b[i] > a[i])
        m += b[i] - a[i];
    }
    writeln(m);
    int i = 0, j = 0;
    while (m--) {
      while (a[i] <= b[i])
        i++;
      while (a[j] >= b[j])
        j++;
      a[i]--;
      a[j]++;
      writefln("%d %d", i + 1, j + 1);
    }
  }
}

// code by cutx64

module main;

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

void main() {

  int tests; readf!"%s\n"(tests);
  while(tests--) {
    int n; readf!"%s\n"(n);
    auto a = readln.splitter.map!(to!int).array;
    auto b = readln.splitter.map!(to!int).array;
    if (a.sum != b.sum) {
      "-1".writeln;
    }
    else {
      alias Pair = Tuple!(int, "i", int, "j");
      Pair[] ans;
      void add_pair(int x, int y) {
        a[x]--; a[y]++;
        ans ~= Pair(x + 1, y + 1);
      }
      int[] s, g;
      foreach(i; 0 .. n) {
        if (a[i] < b[i]) s ~= i;
        else g ~= i;
      }
      int i, j;
      for (i = 0; i < s.length; ) {
        if (a[s[i]] != b[s[i]]) {
          while (a[g[j]] == b[g[j]]) ++j;
          add_pair(g[j], s[i]);
        }
        else {
          ++i;
        }
      }
      writefln!"%s"(ans.length);
      foreach(p; ans) {
        writefln!"%s %s"(p.i, p.j);
      }
    }
  }

} // main

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

		if (a.sum != b.sum) continue;

		ok[ti] = true;
		long[] x, y;
		foreach (i; 0..n)
		{
			auto d = b[i] - a[i];
			if (d >= 0)
			{
				foreach (_; 0..d)
					x ~= i;
			}
			else
			{
				foreach (_; 0..-d)
					y ~= i;
			}
		}

		foreach (i; 0..x.length)
		{
			auto xx = x.front; x.popFront;
			auto yy = y.front; y.popFront;
			ans[ti] ~= [yy+1, xx+1];
		}
	}

	foreach (ti, e; ans)
	{
		if (e.empty)
			writeln(ok[ti] ? 0 : -1);
		else
		{
			writeln(e.length);
			foreach (ee; e)
				writeln(ee[0], " ", ee[1]);
		}
	}
	stdout.flush;
	debug readln;
}

// code by cutx64

module main;

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

void main() {

  int tests; readf!"%s\n"(tests);
  while(tests--) {
    int n; readf!"%s\n"(n);
    auto a = readln.splitter.map!(to!int).array;
    auto b = readln.splitter.map!(to!int).array;
    if (a.sum != b.sum) {
      "-1".writeln;
    }
    else {
      alias Pair = Tuple!(int, "i", int, "j");
      Pair[] ans;
      foreach(i; 0 .. n) {
        while (a[i] != b[i]) {
          if (a[i] > b[i]) {
            ans ~= Pair(i + 1, i + 2);
            a[i]--;
            a[i + 1]++;
          }
          else {
            a[i]++;
            a[i + 1]--;
            ans ~= Pair(i + 2, i + 1);
          }
        }
      }
      writefln!"%s"(ans.length);
      foreach(p; ans) {
        writefln!"%s %s"(p.i, p.j);
      }
    }
  }

} // main


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto x = readln.strip.to !(int);
		int step = 0;
		int pos = 0;
		while (pos < x)
		{
			step += 1;
			pos += step;
		}
		writeln (step + (pos - x == 1));
	}
}

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

string FMT_F = "%.10f";

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

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

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

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(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); }

T binarySearch(alias pred, T)(T ok, T ng)
{
	while (abs(ok-ng) > 1)
	{
		auto mid = (ok+ng)/2;
		if (unaryFun!pred(mid)) ok = mid;
		else ng = mid;
	}
	return ok;
}

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

		bool f(long y)
		{
			return y * (y+1) / 2 < x;
		}
		auto r = binarySearch!(f)(0, x) + 1;
		if (r * (r+1) / 2 == x+1)
		{
			ans[ti] = r+1;
		}
		else
		{
			ans[ti] = r;
		}
	}

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


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

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

  int q;
  rd(q);
  while (q--) {
    long n, a, b;
    rd(n, a, b);
    auto ans = n * a;
    if (n & 1) {
      ans = min(ans, n / 2 * b + a);
    } else {
      ans = min(ans, n / 2 * b);
    }
    writeln(ans);
  }
}

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

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


import std.stdio, std.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 int[][](t, 3);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto p = RDA!int;
		int[int] cnt;
		foreach (j; 0..n)
		{
			++cnt[p[j]];
		}
		auto keys = cnt.keys;
		keys.sort!"a > b"();
		size_t pos;
		int x;
		foreach (j, key; keys)
		{
			x += cnt[key];
			if (x > n/2)
				break;
			pos = j;
		}

		size_t l;
		ans[i][0] = cnt[keys[0]];
		foreach (j, key; keys[0..pos+1])
		{
			if (j == 0) continue;
			ans[i][1] += cnt[key];
			l = j;
			if (ans[i][1] > ans[i][0])
				break;
		}
		foreach (key; keys[l+1..pos+1])
		{
			ans[i][2] += cnt[key];
		}
		if (!(ans[i][1] > ans[i][0] && ans[i][2] > ans[i][0]))
		{
			ans[i] = [0, 0, 0];
		}
	}

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

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

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

void solve(){
	int t = rint;
	foreach(_; 0 .. t){
		int n = rint;
		long[] ps = rlong(n);

		X[] xs;
		long lastp = -1;
		int cnt = 0;
		foreach(p; ps){
			if(lastp >= 0 && lastp != p){
				xs ~= X(lastp, cnt);
				cnt = 0;
			}
			lastp = p;
			cnt += 1;
		}
		xs ~= X(lastp, cnt);

		int total;
		foreach(x; xs) total += x.cnt;

		int g, s, b;
		int sum = g = xs[0].cnt;
		int i = 1;
		for(; i < xs.length; i ++){
			sum += xs[i].cnt;
			if(sum > g * 2){
				s = sum - g;
				break;
			}
		}
		for(i += 1; i < xs.length; i ++){
			sum += xs[i].cnt;
			if(sum * 2 > total){
				b = sum - xs[i].cnt - g - s;
				break;
			}
		}
		if(b <= g) g = 0, s = 0, b = 0;

		writeln(g, " ", s, " ", b);
	}
}
struct X{
	long p;
	int cnt;
}

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

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

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

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

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

        const grp = P.group.array;
        const grpLen = cast(int)(grp.length);
        debug {
          writeln("grp = ", grp);
        }
        auto sum = new int[grpLen + 1];
        foreach (j; 0 .. grpLen) {
          sum[j + 1] = sum[j] + grp[j][1];
        }

        auto ans = new int[3];
        foreach (j; 2 .. grpLen) {
          if (sum[1] - sum[0] < sum[j] - sum[1]) {
            foreach (k; j + 1 .. grpLen + 1) {
              if (sum[1] - sum[0] < sum[k] - sum[j] && 2 * sum[k] <= N) {
                if (ans.sum < sum[k]) {
                  ans = [sum[1] - sum[0], sum[j] - sum[1], sum[k] - sum[j]];
                }
              }
            }
            break;
          }
        }
        writeln(ans[0], " ", ans[1], " ", ans[2]);
      }
    }
  } catch (EOFException e) {
  }
}


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

dchar[] stringify(ll num){
    dchar[] res;
    while(num > 0){
        res ~= to!dchar('0' + (num % 10));
        num /= 10;
    }
    res.reverse;
    return res;
}

dchar[][] pow2;
void preProcess(){
    ll num = 1;
    for(int i = 0; i <= 62; ++i){
        pow2 ~= stringify(num);
        num *= 2;
    }
}

ll common(dchar[] w, dchar[] p2){
    int ptr = 0;
    foreach(c; w){
        if(p2[ptr] == c){
            ++ptr;
        }
        if(ptr == p2.length.to!long){
            return ptr.to!long;
        }
    }
    return ptr.to!long;
}

void theCode(){
    auto w = scan!(dchar[]);
    ll mindist = 32;
    for(int i = 0; i < 62; ++i){
        ll com = common(w, pow2[i]);
        ll dist = w.length.to!long + pow2[i].length.to!long - 2 * com;
        mindist = min(dist, mindist);
    }
    writeln(mindist);
}

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

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

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

size_t solve(string a, string t)
{
  size_t i, j, matched;
  while (i < a.length && j < t.length) {
    if (a[i] == t[j]) {
      i++;
      j++;
      matched++;
    } else {
      i++;
    }
  }
  return a.length - matched + (t.length - j);
}

void main()
{
  string[] pow2list;
  long x = 0;
  while (x < 60) {
    pow2list ~= to!string(1L << x);
    x++;
  }
  long t;
  readf!" %d "(t);
  while (t--) {
    auto a = readln.strip;
    writeln(pow2list.map!(t => solve(a, t)).minElement);
  }
}

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

dchar[] stringify(ll num){
    dchar[] res;
    while(num > 0){
        res ~= to!dchar('0' + (num % 10));
        num /= 10;
    }
    res.reverse;
    return res;
}

dchar[][] pow2;
void preProcess(){
    ll num = 1;
    for(int i = 0; i <= 50; ++i){
        pow2 ~= stringify(num);
        num *= 2;
    }
}

ll common(dchar[] w, dchar[] p2){
    int ptr = 0;
    foreach(c; w){
        if(p2[ptr] == c){
            ++ptr;
        }
        if(ptr == p2.length.to!long){
            return ptr.to!long;
        }
    }
    return ptr.to!long;
}

void theCode(){
    auto w = scan!(dchar[]);
    ll mindist = 32;
    for(int i = 0; i <= 50; ++i){
        ll com = common(w, pow2[i]);
        ll dist = w.length.to!long + pow2[i].length.to!long - 2 * com;
        mindist = min(dist, mindist);
    }
    writeln(mindist);
}

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

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


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

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

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  foreach (tid; 0 .. nt) {
    const n = r.next!uint;
    auto a = r.nextA!int(n);
    sort(a);
    auto b = new int[n];
    int i = (n - 1) / 2, j = i + 1;
    b[0] = a[i];
    foreach (k; 1 .. n) {
      debug stderr.writefln ("[%d, %d)", i, j);
      if ((k & 1)) {
        b[k] = a[j++];
      } else {
        b[k] = a[--i];
      }
    }
    writefln!("%(%s %)")(b);
  }
}

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 (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(int[]);
        sort(as);
        int[] bs;
        int i = (as.length.to!int-1)/2, j = (as.length.to!int-1)/2+1;
        for (;;) {
            if (i < 0) break;
            bs ~= as[i];
            if (j >= N) break;
            bs ~= as[j];
            --i;
            ++j;
        }
        writeln(bs.to!(string[]).join(" "));
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		a.sort();
		auto pos = n / 2;
		ans[ti] ~= a[pos];
		int l = pos-1;
		int r = pos+1;
		while (true)
		{
			if (l < 0) break;
			ans[ti] ~= a[l];
			if (r == n) break;
			ans[ti] ~= a[r];
			--l; ++r;
		}
	}

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


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

void main()
{
  int k;
  readf!" %d "(k);
  while (k--) {
    long n, x, t;
    readf!" %d %d %d "(n, x, t);
    long tmp = t / x;
    long result = 0;

    if (n >= tmp + 1) {
      result += tmp * (n - tmp - 1);
      result += (tmp + 1) * (tmp) / 2;
    } else {
      result = n * (n - 1) / 2;
    }
    writeln(result);
  }
}

// Vicfred
// https://codeforces.com/contest/1539/problem/A
// math
import std.algorithm;
import std.stdio;

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

while(k--) {
    long n, x, t;
    readf("%s %s %s\n", &n, &x, &t);
    long ans = t/x*max(0, n-t/x) + min(n - 1, t/x - 1)*min(n, t/x)/2;
    ans.writeln;
}
}

// Vicfred
// https://codeforces.com/contest/1539/problem/A
//
import std.stdio;

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

while(k--) {
    long n, x, t;
    readf("%s %s %s\n", &n, &x, &t);
    long ans = t/x*(n-t/x) + (t/x - 1)*(t/x)/2;
    ans.writeln;
}
}


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

void main(){

  int n; rd(n);
  long a, b; rd(a, b);
  long[] cand;
  for(int i=2; i*i<=a; i++){
    if(a%i==0){
      cand~=i;
      while(a%i==0) a/=i;
    }
  }
  if(a>1) cand~=a;
  for(int i=2; i*i<=b; i++){
    if(b%i==0){
      cand~=i;
      while(b%i==0) b/=i;
    }
  }
  if(b>1) cand~=b;
  foreach(i; 0..(n-1)){
    long s, t; rd(s, t);
    long[] nex;
    foreach(x; cand){
      if(s%x==0 || t%x==0) nex~=x;
    }
    cand.swap(nex);
  }

  if(cand.length>0){
    writeln(cand[0]);
  }else{
    writeln(-1);
  }
}

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

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

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

int n;

void main() {
  string s = readln.strip;
  n = parse!int(s);

  long[] a,b;
  a.length = n;
  b.length = n;

  long x = 0;
  foreach (i; 0 .. n) {
    readln.strip.formattedRead(" %s %s", a[i], b[i]);
    x = gcd(a[i]*b[i],x);
  }

  foreach(i; 0 .. n) {
    long y = gcd(x,a[i]);
    if (y != 1) {
      x = y;
    } else {
      x = gcd(x,b[i]);
    }
  }

  if (x == 1)
    writeln(-1);
  else
    writeln(x);
}

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

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

    int[] calcFor2Elms(int a, int b) {
        int[] calc(int x) {
            int[] ans;
            auto sq = sqrt(x.to!real).floor();
            auto v = x, k = 2;
            while (v > 1) {
                if (v % k == 0) {
                    ans ~= k;
                    while (v % k == 0) v /= k;
                }
                ++k;
                if (k > sq) break;
            }
            if (v > 1) ans ~= v;
            return ans;
        }
        return multiwayUnion([calc(a), calc(b)]).array;
    }

    int a, b;
    readf("%s %s", &a, &b);
    readln;
    auto elms = calcFor2Elms(a, b);

    debug { elms.writeln; }

    foreach (_; 1 .. n) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;

        int[] cur;
        foreach (e; elms) {
            if (x % e == 0 || y % e == 0) cur ~= e;
        }

        elms = cur;

        debug { elms.writeln; }
    }

    writeln(elms.empty() ? -1 : elms.front);
}

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

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

    int[] calcFor2Elms(int a, int b) {
        int[] calc(int x) {
            int[] ans;
            auto sq = sqrt(x.to!real).floor();
            auto v = x, k = 2;
            while (v > 1 && k <= sq) {
                if (v % k == 0) {
                    ans ~= k;
                    while (v % k == 0) v /= k;
                }
                ++k;
            }
            if (v > 1) ans ~= v;
            return ans;
        }
        return multiwayUnion([calc(a), calc(b)]).array;
    }

    int a, b;
    readf("%s %s", &a, &b);
    readln;
    auto elms = calcFor2Elms(a, b);

    debug { elms.writeln; }

    foreach (_; 1 .. n) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;

        int[] cur;
        foreach (e; elms) {
            if (x % e == 0 || y % e == 0) cur ~= e;
        }

        elms = cur;

        debug { elms.writeln; }
    }

    writeln(elms.empty() ? -1 : elms.front);
}

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

bool isPrime (int v)
{
	if (v < 2)
	{
		return false;
	}
	for (int d = 2; d * d <= v; d++)
	{
		if (v % d == 0)
		{
			return false;
		}
	}
	return true;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [n];
		auto b = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &a[i], &b[i]);
		}

		bool [int] cand;
		for (int d = 1; d * d <= a[0]; d += 1)
		{
			if (a[0] % d == 0)
			{
				cand[d] = true;
				cand[a[0] / d] = true;
			}
		}
		for (int d = 1; d * d <= b[0]; d += 1)
		{
			if (b[0] % d == 0)
			{
				cand[d] = true;
				cand[b[0] / d] = true;
			}
		}

		int [] divs;
		foreach (k, v; cand)
		{
			if (isPrime (k))
			{
				divs ~= k;
			}
		}

		int res = -1;
		foreach (d; divs)
		{
			bool ok = true;
			foreach (i; 0..n)
			{
				if (a[i] % d != 0 && b[i] % d != 0)
				{
					ok = false;
				}
			}
			if (ok)
			{
				res = d;
				break;
			}
		}

		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.numeric, std.regex, std.typecons;
import core.bitop;

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

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

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

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

      long g = 0;
      foreach (i; 0 .. N) {
        const l = A[i] / gcd(A[i], B[i]) * B[i];
        g = gcd(g, l);
      }
      if (g == 1) {
        writeln(-1);
      } else {
        long ans;
        foreach (a; [A[0], B[0]]) {
          const m = gcd(g, a);
          for (long p = 2; p * p <= m; ++p) {
            if (m % p == 0) {
              ans = p;
              goto found;
            }
          }
          if (m > 1) {
            ans = m;
            goto found;
          }
        }
        assert(false);
       found:
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main(){

  int n; rd(n);
  long a, b; rd(a, b);
  long[] cand;
  for(int i=2; i*i<=a; i++){
    if(a%i==0){
      cand~=i;
      while(a%i) a/=i;
    }
  }
  if(a>1) cand~=a;
  for(int i=2; i*i<=b; i++){
    if(b%i==0){
      cand~=i;
      while(b%i) b/=i;
    }
  }
  if(b>1) cand~=b;
  foreach(i; 0..(n-1)){
    long s, t; rd(s, t);
    long[] nex;
    foreach(x; cand){
      if(s%x==0 || t%x==0) nex~=x;
    }
    cand.swap(nex);
  }

  if(cand.length>0){
    writeln(cand[0]);
  }else{
    writeln(-1);
  }
}

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

import std.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.numeric, std.regex, std.typecons;
import core.bitop;

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

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

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

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

      long g = 0;
      foreach (i; 0 .. N) {
        const l = A[i] / gcd(A[i], B[i]) * B[i];
        g = gcd(g, l);
      }
      writeln((g == 1) ? -1 : g);
    }
  } catch (EOFException e) {
  }
}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto s = new char[][](n);
		foreach (i; 0..n)
		{
			s[i] = RD!(char[]);
		}

		bool visit(int y, int x)
		{
			if (!inside(y, 0, n) || !inside(x, 0, m)) return true;
			if (s[y][x] == 'G') return false;
			if (s[y][x] == '.')
			{
				s[y][x] = '#';
			}
			return true;
		}

		bool ok = true;
		int[][] good;
		(){
		foreach (int y; 0..n)
		{
			foreach (int x; 0..m)
			{
				if (s[y][x] == 'B')
				{

					ok &= visit(y-1, x);
					ok &= visit(y+1, x);
					ok &= visit(y, x-1);
					ok &= visit(y, x+1);
					if (!ok) return;
				}
				else if (s[y][x] == 'G')
				{
					good ~= [y, x];
				}
			}
		}}();
		if (!ok) continue;

		int[][] open;
		if (s[n-1][m-1] != '#')
		{
			open ~= [[n-1, m-1]];
		}
		auto dist = new int[][](n, m);
		foreach (i; 0..n)
		{
			dist[i][] = int.max;
		}
		dist[n-1][m-1] = 0;
		while (!open.empty)
		{
			auto nd = open.front; open.popFront;
			auto y = nd[0];
			auto x = nd[1];
			if (y != 0)
			{
				auto ny = y-1;
				auto nx = x;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
			if (y != n-1)
			{
				auto ny = y+1;
				auto nx = x;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
			if (x != 0)
			{
				auto ny = y;
				auto nx = x-1;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
			if (x != m-1)
			{
				auto ny = y;
				auto nx = x+1;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
		}

		foreach (e; good)
		{
			auto y = e[0];
			auto x = e[1];
			if (dist[y][x] == int.max)
			{
				ok = false;
				break;
			}
		}
		ans[ti] = ok;
	}

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

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

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

void main ()
{
	foreach (test; 0..readln.strip.to !(int))
	{
		int rows, cols;
		readf !(" %s %s") (rows, cols);
		readln;
		char [] [] board;
		board ~= '#'.repeat (cols + 2).array;
		foreach (row; 1..rows + 1)
		{
			board ~= '#' ~ readln.strip.dup ~ '#';
		}
		board ~= '#'.repeat (cols + 2).array;

		int goods = 0;
		foreach (row; 1..rows + 1)
		{
			goods += board[row].count ('G');
		}

		foreach (row; 1..rows + 1)
		{
			foreach (col; 1..cols + 1)
			{
				if (board[row][col] != 'B')
				{
					continue;
				}
				foreach (dir; 0..dirs)
				{
					auto nRow = row + dRow[dir];
					auto nCol = col + dCol[dir];
					if (board[nRow][nCol] == '.')
					{
						board[nRow][nCol] = '#';
					}
				}
			}
		}

		bool recur (int row, int col)
		{
			if (board[row][col] == '#')
			{
				return true;
			}
			if (board[row][col] == 'B')
			{
				return false;
			}
			goods -= (board[row][col] == 'G');
			board[row][col] = '#';
			foreach (dir; 0..dirs)
			{
				auto nRow = row + dRow[dir];
				auto nCol = col + dCol[dir];
				if (!recur (nRow, nCol))
				{
					return false;
				}
			}
			return true;
		}

		bool ok = recur (rows, cols);
		ok &= (goods == 0);
		writeln (ok ? "Yes" : "No");
	}
}

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

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

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

        auto arr = new dchar[][] (n + 2, m + 2);
        foreach (i; 0 .. n+2) {
            foreach (j; 0 .. m+2) {
                arr[i][j] = '#';
            }
        }

        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. m+1) {
                readf("%s", &arr[i][j]);
            }

            readln;
        }

        debug { arr.writeln; }

        bool isOkCell(int i, int j) {
            if (arr[i][j] != 'B') { return true; }

            foreach (x; -1 .. 2) {
                foreach (y; -1 .. 2) {
                    if (abs(x) + abs(y) != 1) { continue; }
                    if (arr[i+x][y+j] == 'G') { return false; }
                }
            }

            return true;
        }

        void mark(int i, int j) {
            if (arr[i][j] != 'B') { return; }

            foreach (x; -1 .. 2) {
                foreach (y; -1 .. 2) {
                    if (abs(x) + abs(y) != 1) { continue; }
                    if (arr[i+x][j+y] == '.') { arr[i+x][j+y] = '#'; }
                }
            }
        }

        bool ans = true;
        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. m+1) {
                ans &= isOkCell(i, j);
                mark(i, j);
            }
        }

        if (!ans) {
            writeln("No");
            continue;
        }

        debug { arr.writeln; }

        auto vis = new bool[][] (n+2, m+2);
        foreach (i; 0 .. n+2) {
            vis[i][] = false;
        }

        void dfs(int i, int j) {
            if (arr[i][j] == '#') { return; }

            vis[i][j] = true;
            foreach (x; -1 .. 2) {
                foreach (y; -1 .. 2) {
                    if (abs(x) + abs(y) != 1) { continue; }
                    if (vis[i+x][j+y]) { continue; }

                    dfs(i+x, y+j);
                }
            }
        }

        dfs(n, m);

        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. m+1) {
                if (arr[i][j] == 'G' && !vis[i][j]) { ans = false; }
            }
        }

        writeln(ans ? "Yes" : "No");
    }
}

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

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

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

        auto arr = new dchar[][] (n + 2, m + 2);
        foreach (i; 0 .. n+2) {
            foreach (j; 0 .. m+2) {
                arr[i][j] = '#';
            }
        }

        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. m+1) {
                readf("%s", &arr[i][j]);
            }

            readln;
        }

        debug { arr.writeln; }

        bool isOkCell(int i, int j) {
            if (arr[i][j] != 'B') { return true; }

            foreach (x; -1 .. 2) {
                foreach (y; -1 .. 2) {
                    if (abs(x) + abs(y) != 1) { continue; }
                    if (arr[i+x][y+j] == 'G') { return false; }
                }
            }

            return true;
        }

        void mark(int i, int j) {
            if (arr[i][j] != 'B') { return; }

            foreach (x; -1 .. 2) {
                foreach (y; -1 .. 2) {
                    if (abs(x) + abs(y) != 1) { continue; }
                    if (arr[i+x][j+y] != 'G') { arr[i+x][j+y] = '#'; }
                }
            }
        }

        bool ans = true;
        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. m+1) {
                ans &= isOkCell(i, j);
                mark(i, j);
            }
        }

        if (!ans) {
            writeln("No");
            continue;
        }

        debug { arr.writeln; }

        auto vis = new bool[][] (n+2, m+2);
        foreach (i; 0 .. n+2) {
            vis[i][] = false;
        }

        void dfs(int i, int j) {
            if (arr[i][j] == '#') { return; }

            vis[i][j] = true;
            foreach (x; -1 .. 2) {
                foreach (y; -1 .. 2) {
                    if (abs(x) + abs(y) != 1) { continue; }
                    if (vis[i+x][j+y]) { continue; }

                    dfs(i+x, y+j);
                }
            }
        }

        dfs(n, m);

        foreach (i; 1 .. n+1) {
            foreach (j; 1 .. m+1) {
                if (arr[i][j] == 'G' && !vis[i][j]) { ans = false; }
            }
        }

        writeln(ans ? "Yes" : "No");
    }
}

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 bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto s = new char[][](n);
		foreach (i; 0..n)
		{
			s[i] = RD!(char[]);
		}

		bool visit(int y, int x)
		{
			if (!inside(y, 0, n) || !inside(x, 0, m)) return true;
			if (s[y][x] == 'G') return false;
			if (s[y][x] == '.')
			{
				s[y][x] = '#';
			}
			return true;
		}

		bool ok = true;
		int[][] good;
		(){
		foreach (int y; 0..n)
		{
			foreach (int x; 0..m)
			{
				if (s[y][x] == 'B')
				{

					ok = visit(y-1, x);
					ok = visit(y+1, x);
					ok = visit(y, x-1);
					ok = visit(y, x+1);
					if (!ok) return;
				}
				else if (s[y][x] == 'G')
				{
					good ~= [y, x];
				}
			}
		}}();
		if (!ok) continue;

		int[][] open;
		if (s[n-1][m-1] != '#')
		{
			open ~= [[n-1, m-1]];
		}
		auto dist = new int[][](n, m);
		foreach (i; 0..n)
		{
			dist[i][] = int.max;
		}
		dist[n-1][m-1] = 0;
		while (!open.empty)
		{
			auto nd = open.front; open.popFront;
			auto y = nd[0];
			auto x = nd[1];
			if (y != 0)
			{
				auto ny = y-1;
				auto nx = x;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
			if (y != n-1)
			{
				auto ny = y+1;
				auto nx = x;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
			if (x != 0)
			{
				auto ny = y;
				auto nx = x-1;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
			if (x != m-1)
			{
				auto ny = y;
				auto nx = x+1;
				if (s[ny][nx] != '#')
				{
					if (dist[y][x]+1 < dist[ny][nx])
					{
						dist[ny][nx] = dist[y][x]+1;
						open ~= [ny, nx];
					}
				}
			}
		}

		foreach (e; good)
		{
			auto y = e[0];
			auto x = e[1];
			if (dist[y][x] == int.max)
			{
				ok = false;
				break;
			}
		}
		ans[ti] = ok;
	}

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


void main() { runSolver(); }

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

  auto solve() {
    long subSolve(long N, int[] A) {
      int[][int] indecies;
      foreach(int i, a; A) {
        indecies[a] ~= i;
      }
      // indecies.deb;

      long ans = -1;
      foreach(key, inds; indecies) {
        if (inds.length == 1) continue;

        long minDistance = int.max;
        foreach(i; 0..inds.length - 1) {
          minDistance = min(minDistance, inds[i + 1] - inds[i]);
        }

        ans = ans.max(N - minDistance);
      }

      return ans;
    }

    foreach(i; 0..QN) {
      const n = scan!int;
      subSolve(n, scan!int(n)).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"];

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

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

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

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

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

  return ok;
}

import std;

T
read (alias T, string end = "") () {
    T x;
    readf!("%s" ~ end)(x);
    return x;
}

void main () {
    immutable tests = read!(uint, "\n");
    TEST_LOOP: foreach (test_index; 0 .. tests) {
        immutable n = read!(uint, "\n");
        immutable vec =
            readln.chomp.split(' ')
            .to!(uint[]);

        auto indexes = (){
            static assert(true);
            size_t[][uint] ret;
            foreach (ind, val; vec)
                ret[val] ~= ind;
            return ret;
        }();

        (cast(int)(
              -
                indexes
                .byValue
                .filter!"a.length > 1"
                .map!(x => zip(x, x.dropOne)
                        .minElement!"a[1] - a[0]")
                .map!(x => x[1] - x[0])
                .minElement(n + 1)
              + n

        ))
            .writeln;
    }
}
// ""

import std;

T
read (alias T, string end = "") () {
    T x;
    readf!("%s" ~ end)(x);
    return x;
}

void main () {
    immutable tests = read!(uint, "\n");
    TEST_LOOP: foreach (test_index; 0 .. tests) {
        immutable n = read!(uint, "\n");
        immutable vec =
            readln.chomp.split(' ')
            .to!(uint[]);

        auto indexes = (){
            static assert(true);
            size_t[][uint] ret;
            foreach (ind, val; vec)
                ret[val] ~= ind;
            return ret;
        }();

        (cast(int)(
              -
                indexes
                .byValue
                .filter!"a.length > 1"
                .map!(x => zip(x, x.dropOne)
                        .minElement!"a[1] - a[0]")
                .cache.map!(x => x[1] - x[0])
                .minElement(n + 1)
              + n

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

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

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

		auto pos = new int[][](150000);
		foreach (i; 0..n)
		{
			pos[a[i]] ~= i;
		}

		foreach (i; 0..pos.length)
		{
			if (pos[i].length < 2) continue;
			foreach (j; 0..pos[i].length-1)
			{
				ans[ti].chmax(pos[i][j] + n - pos[i][j+1]);
			}
		}
		if (ans[ti] == 0)
			ans[ti] = -1;
	}

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


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

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

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

    auto dp = new int[][] (n+2, 2);
    dp[0][1] = c;

    int[] ans;
    ans ~= 0;
    foreach (i; 0 .. n-1) {
        dp[i+1][0] = min(dp[i][0], dp[i][1]) + a[i];
        dp[i+1][1] = min(dp[i][0] + c, dp[i][1]) + b[i];
        debug { writeln(i, dp[i]); }
        ans ~= min(dp[i+1][0], dp[i+1][1]);
    }

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

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

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

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

	auto dp = new long[][](n+1, 2);
	dp[0][1] = c;

	foreach (i; 0..n-1)
	{
		dp[i+1][0] = min(dp[i][0]+a[i], dp[i][1]+a[i]);
		dp[i+1][1] = min(dp[i][0]+b[i]+c, dp[i][1]+b[i]);
	}

	auto ans = new long[](n);
	foreach (i; 0..n-1)
	{
		ans[i+1] = min(dp[i+1][0], dp[i+1][1]);
	}

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

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


void main() { runSolver(); }

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

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

      auto used = new bool[](N + 1);
      auto queue = iota(1, N+1).array;
      auto ans = Array!int();
      int output;
      void dfs(int size = 0) {
        if (output == N) return;
        if (size == N) {
          output++;
          ans.array.toAnswerString.writeln;
          return;
        }

        foreach(p; queue) {
          if (used[p]) continue;

          if (ans.length < 2 || ans[$ - 1] + ans[$ - 2] != p) {
            ans.insertBack(p);
            used[p] = true;
            dfs(size + 1);
            ans.removeBack;
            used[p] = false;
          }
        }
      }

      dfs();

    }

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

  outputForAtCoder(&solve);
}

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

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

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

import std;

T
read (alias T, string end = "") () {
    T x;
    readf!("%s" ~ end)(x);
    return x;
}

void main () {
    immutable tests = read!(uint, "\n");
    TEST_LOOP: foreach (test_index; 0 .. tests) {
        immutable n = readln.chomp.to!uint;

        foreach (onePosition; 0 .. n) {
            chain(
                iota(n, n - onePosition, -1),
                [1u],
                iota(n - onePosition, 1 , -1)
            ).writefln!"%(%s %)";
        }
    }
}
// ""


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

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

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

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

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

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

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

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

void main()
{
    int N; get(N);
    writefln!"? %d %d"(1, N); stdout.flush();
    int p; get(p);
    int q;
    if (p == 1) {
        q = -1;
    } else if (p == N) {
        q = N;
    } else {
        writefln!"? %d %d"(1, p); stdout.flush();
        get(q);
    }
    if (p == q) {
        int l = 1, r = p;
        while (l + 1 < r) {
            auto m = (l + r) / 2;
            writefln!"? %d %d"(m, p); stdout.flush();
            get(q);
            if (p == q) {
                l = m;
            } else {
                r = m;
            }
        }
        writeln("! ", l); stdout.flush();
    } else {
        int l = p, r = N;
        while (l + 1 < r) {
            auto m = (l + r) / 2;
            writefln!"? %d %d"(p, m); stdout.flush();
            get(q);
            if (p == q) {
                r = m;
            } else {
                l = m;
            }
        }
        writeln("! ", r); stdout.flush();
    }
}

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

	int l = 1, r = n;
	writeln("? ", l, " ", r);
	stdout.flush;
	auto m = RD!int;
	while (r - l > 1)
	{
		auto m2 = (l+r)/2;
		if (m <= m2)
		{
			writeln("? ", l, " ", m2);
			stdout.flush;
			auto a = RD!int;
			if (a == m)
				r = m2;
			else
			{
				l = m2;
				writeln("? ", l, " ", r);
				stdout.flush;
				m = RD!int;
			}
		}
		else
		{
			writeln("? ", m2, " ", r);
			stdout.flush;
			auto a = RD!int;
			if (a == m)
				l = m2;
			else
			{
				r = m2;
				writeln("? ", l, " ", r);
				stdout.flush;
				m = RD!int;
			}
		}
	}

	if (m == l)
		writeln("! ", r);
	else
		writeln("! ", l);
	stdout.flush;
	debug readln;
}

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

int main(string[] args)
{
    auto n = to!int(readln.strip);
    writeln("? 1 ", n);
    stdout.flush;
    auto pidx = to!int(readln.strip);
    auto left = 1, right = n;
    while (left < right)
    {
        auto mid = (left + right) >> 1;
        if (left == mid && right == mid + 1)
        {
            if (pidx == left) left = mid + 1;
            else right = mid;
            continue;
        }
        int lidx, ridx;
        if (left < mid)
        {
            writeln("? ", left, " ", mid);
            stdout.flush;
            lidx = to!int(readln.strip);
        }
        if (mid + 1 < right)
        {
            writeln("? ", mid + 1, " ", right);
            stdout.flush;
            ridx = to!int(readln.strip);
        }
        if (left == mid)
        {
            if (pidx == left || pidx == ridx) left = mid + 1, pidx = ridx;
            else right = mid;
            continue;
        }
        if (right == mid + 1)
        {
            if (pidx == right || pidx == lidx) right = mid, pidx = lidx;
            else left = mid + 1;
            continue;
        }
        if (pidx <= mid)
        {
            if (pidx == lidx) right = mid;
            else left = mid + 1;
        }
        else
        {
            if (pidx == ridx) left = mid + 1;
            else right = mid;
        }
        pidx = (left > mid ? ridx : lidx);
    }
    writeln("! ", left);
    return 0;
}

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

int main(string[] args)
{
    auto n = to!int(readln.strip);
    writeln("? 1 ", n);
    stdout.flush;
    auto pidx = to!int(readln.strip);
    auto left = 1, right = n;
    while (left < right)
    {
        auto mid = (left + right) >> 1;
        if (left == mid && right == mid + 1)
        {
            if (pidx == left) left = mid + 1;
            else right = mid;
            continue;
        }
        int lidx, ridx;
        if (left < mid)
        {
            writeln("? ", left, " ", mid);
            stdout.flush;
            lidx = to!int(readln.strip);
        }
        if (mid + 1 < right)
        {
            writeln("? ", mid + 1, " ", right);
            stdout.flush;
            ridx = to!int(readln.strip);
        }
        if (left == mid)
        {
            if (pidx == left) left = mid + 1, pidx = ridx;
            else right = mid;
            continue;
        }
        if (right == mid + 1)
        {
            if (pidx == right) right = mid, pidx = lidx;
            else left = mid + 1;
            continue;
        }
        if (pidx <= mid)
        {
            if (pidx == lidx) right = mid;
            else left = mid + 1;
        }
        else
        {
            if (pidx == ridx) left = mid + 1;
            else right = mid;
        }
        if (left > mid) pidx = ridx;
        else pidx = lidx;
    }
    writeln("! ", left);
    return 0;
}

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

int main(string[] args)
{
    auto n = to!int(readln.strip);
    writeln("? 1 ", n);
    stdout.flush;
    auto pidx = to!int(readln.strip);
    auto left = 1, right = n;
    while (left < right)
    {
        auto mid = (left + right) >> 1;
        if (left == mid && right == mid + 1)
        {
            if (pidx == left) left = mid + 1;
            else right = mid;
            continue;
        }
        int lidx, ridx;
        if (left < mid)
        {
            writeln("? ", left, " ", mid);
            stdout.flush;
            lidx = to!int(readln.strip);
        }
        if (mid + 1 < right)
        {
            writeln("? ", mid + 1, " ", right);
            stdout.flush;
            ridx = to!int(readln.strip);
        }
        if (left == mid)
        {
            if (pidx == left) left = mid + 1, pidx = ridx;
            else right = mid;
            continue;
        }
        if (right == mid + 1)
        {
            if (pidx == right) right = mid, pidx = lidx;
            else left = mid + 1;
            continue;
        }
        if (pidx < mid)
        {
            if (pidx == lidx) right = mid;
            else left = mid + 1;
        }
        else
        {
            if (pidx == ridx) left = mid + 1;
            else right = mid;
        }
        if (left > mid) pidx = ridx;
        else pidx = lidx;
    }
    writeln("! ", left);
    return 0;
}

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

int main(string[] args)
{
    auto n = to!int(readln.strip);
    writeln("? 1 ", n);
    stdout.flush;
    auto pidx = to!int(readln.strip);
    auto left = 1, right = n;
    while (left < right)
    {
        auto mid = (left + right) >> 1;
        if (left == mid)
        {
            if (pidx == left) left = mid + 1;
            else right = mid - 1;
            continue;
        }
        if (right == mid + 1)
        {
            if (pidx == right) right = mid - 1;
            else left = mid + 1;
            continue;
        }
        writeln("? ", left, " ", mid);
        stdout.flush;
        auto lidx = to!int(readln.strip);
        writeln("? ", mid + 1, " ", right);
        stdout.flush;
        auto ridx = to!int(readln.strip);
        if (pidx < mid)
        {
            if (pidx == lidx) right = mid - 1;
            else left = mid + 1;
        }
        else
        {
            if (pidx == ridx) left = mid + 1;
            else right = mid - 1;
        }
        if (left > mid) pidx = ridx;
        else pidx = lidx;
    }
    writeln("! ", left);
    return 0;
}


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

void main() {
    foreach(i; 0 .. to!int(readln.strip)) {
        int n = to!int(readln.strip);
        int r = bsr(n);
        writeln(pow(2, r)-1);
    }
}

import std.stdio;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n;
        readf!"%d\n"(n);
        int res = 0;
        while (n)
        {
            res++;
            n /= 2;
        }
        writeln((1 << (res - 1)) - 1);
    }
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

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

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

		long x;
		foreach (i; 0..33)
		{
			auto bit = 1L << i;
			if (n & bit)
				x = bit;
		}
		ans[ti] = x - 1;
	}

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


import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;
import std.math : abs;

alias Tree = RBT!int;

void main() {
  GC.disable();

  long n, k;
  readf(" %s %s\n", n, k);
  auto s = readln().strip.dup;

  int ans = -1;
  foreach(c; iota(0,27)) {
      auto cc = 'a' + c;
      int count= 0;
      int tt = 0;

    foreach(e; s){
        if (e == cc) {
        count++;
      }  else {
      tt += count / k;
      count = 0;
      }
    }

    tt += count/k;

    ans = max(tt, ans);
  }

  writeln(ans);

}

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 K = s[1];
    auto S = readln.chomp;
    int ans = 0;

    for (char c = 'a'; c <= 'z'; ++c) {
        int tmp = 0;
        int v = 0;
        foreach (i; 0..N) {
            if (S[i] == c) {
                tmp += 1;
                if (tmp == K) {
                    v += 1;
                    tmp = 0;
                }
            } else {
                tmp = 0;
            }
        }
        ans = max(ans, v);
    }

    ans.writeln;
}


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

void main(){
  auto n = readln.chomp.to!int;
  int[][] matrix;
  foreach(i; 0..n){ matrix ~= readln.chomp.split(" ").map!(to!int).array; }
  matrix.map!(sum).filter!((e) => e >= 2).array.length.writeln;
}

import std.stdio;
import std.format;

void main()
{
    readln; //discard first line

    uint numChallenges;

    byte a, b, c;

    foreach(line; stdin.byLine)
    {
        formattedRead(line, "%s %s %s", &a, &b, &c);

        if ((a + b + c) > 1) ++numChallenges;
    }

    numChallenges.writeln;
}

import std.stdio;

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

    int implement = 0;
    for (int i = 0; i < n; ++i) {
        int p, v, t;
        readf("%d %d %d\n", &p, &v, &t);

        if (p+v+t >= 2) {
            implement++;
        }
    }

    writefln("%d", implement);
}

import std.stdio;
import core.stdc.stdio;

int main(string[] argv)
{
	int n;
	int a, b, c;
	scanf("%d", &n);
	int noproblems = 0;
	for (int i = 0; i < n; i++)
	{
		scanf("%d %d %d", &a, &b, &c);
		noproblems += (a + b + c) > 1 ? 1 : 0;
	}
	printf("%d", noproblems);
    return 0;
}

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

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

	for(int i=0; i<n; i++) {
		if(readln.chomp.split(" ").map!(to!int).filter!(x => x==1).count >= 2) {
			problems++;
		}
	}

	problems.writeln;
}

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

void main() {
	int n = readln.chomp.to!int;
	int problems = 0;

	for(int i=0; i<n; i++) {
		int[3] a = readln.chomp.split(" ").map!(to!int).array;
		int sure = 0;

		foreach(item; a) {
			if(item) {
				sure++;
			}
		}

		if(sure>=2) {
			problems++;
		}
	}

	problems.writeln;
}

// https://codeforces.com/problemset/problem/231/A
// implementation, simulation
import std.conv;
import std.range;
import std.stdio;
import std.algorithm;

void main() {
    int answer = 0;
    foreach(line; stdin.byLine.dropOne)
        answer += (line.split.map!(to!int).sum > 1);
    answer.writeln;
}

// https://codeforces.com/problemset/problem/231/A
import std.conv;
import std.range;
import std.stdio;
import std.algorithm;

void main() {
    int answer = 0;
    foreach(line; stdin.byLine.dropOne) {
        if(line.split.map!(to!int).count!(x => x == 1) >= 2)
            answer += 1;
    }
    answer.writeln;
}

import std.stdio;
void main()
{
    int n;
    readf("%s\n", &n);
    int ans = 0;
    for(int i = 0; i < n; ++i){
        int a,b,c;
        readf("%s %s %s\n",&a,&b,&c);
        ans += (a+b+c)>=2;
    }
    writeln(ans);
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char rc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    do
    {
        res = res * 10 + cast (int) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    do
    {
        res = res * 10 + cast (long) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] ns ()
{
    sb ();
    char[] res = [];
    do
    {
        res ~= to!char(curc);
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

void CF143A()
{
    int cnt, n = ni();
    for (int i = 0; i < n; ++i)
        if (ni() + ni() + ni() >= 2) ++cnt;
    writeln(cnt);
}

void main()
{
    CF143A();
}

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

void main() {
  readln;

  int ans = 0;
  foreach (string s; stdin.lines) {
    if (s.countchars("1") >= 2) ans++;
  }

  ans.writeln;
}

import std.stdio, std.string, std.conv, std.range, std.array, std.algorithm;
import std.uni, std.math, std.container, std.typecons, std.typetuple;
import core.bitop, std.datetime;

immutable int mod = 10^^9 + 7;

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

    int ans;

    foreach(i ; 0 .. n){
        auto a = readln.split.to!(int[]).sum;
        ans += (a > 1);
    }

    writeln(ans);
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        writeln(line);
        throw new Exception("args num < input num");
    }
}

import std.stdio;

void main()
{
    readln; //discard first line

    uint numChallenges;

    byte a, b, c;

    while(!stdin.eof)
    {
        readf("%s %s %s\n", &a, &b, &c);
        if (stdin.eof) break;
        writeln(a,b,c);

        if ((a + b + c) > 1) ++numChallenges;

    }

    numChallenges.writeln;
}

import std.stdio;

void main()
{
    readln; //discard first line

    uint numChallenges;

    byte a, b, c;

    while(!stdin.eof)
    {
        readf("%s %s %s\n", &a, &b, &c);

        if ((a + b + c) > 1) ++numChallenges;
    }

    numChallenges.writeln;
}

import std.stdio;

void main()
{
    readln; //discard first line

    uint numChallenges;

    byte a, b, c;

    while(!stdin.eof)
    {
        readf("%s %s %s\n", &a, &b, &c);

        if (a + b + c > 1) ++numChallenges;
    }

    numChallenges.writeln;
}

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

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

	for(int i=0; i<n; i++) {
		if(readln.chomp.split(" ").map!(to!int).filter!(x => x==1).count >= 2) {
			problems++;
		}
	}
}

import std.algorithm;
import std.c.stdio;
import std.stdio;
import std.conv;
import std.math;
import std.array;
import std.container;

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char rc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    do
    {
        res = res * 10 + cast (int) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    do
    {
        res = res * 10 + cast (long) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

double nd()
{
    sb();
    double res = 0;
    while ((curc != '.') & (32 < curc))
    {
        res = res * 10 + cast(int)(curc - '0');
        curc = getchar();
    }
    if (curc == '.')
    {
        curc = getchar();
        double exp = 10;
        while (32 < curc)
        {
            res += cast(double)(curc - '0') / exp;
            exp *= 10;
            curc = getchar();
        }
    }
    return res;
}

char[] ns ()
{
    sb ();
    char[] res = [];
    do
    {
        res ~= to!char(curc);
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

void CF143A()
{
    int cnt;
    for (int i = 0; i < ni(); ++i)
        if (ni() + ni() + ni() >= 2) ++cnt;
    writeln(cnt);
}

void main()
{
    CF143A();
}


import std.stdio;

void main() {
  int n, s, t;
  scanf("%d %d %d", &n, &s, &t);
  int[] p = new int[n+1];
  for (int i = 1 ; i <= n ; ++i) {
    scanf("%d", &p[i]);
  }
  int ans = 0, flg = 1;
  bool[int] set;
  for ( ; s != t ; ++ans) {
    if (s in set) {flg = 0; break;}
    set[s] = true;
    s = p[s];
  }
  if (!flg) writeln("-1");
  else writeln(ans);
}

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

int[] p;
bool[] e;

int next(int s) {
    if (e[s - 1]) {
        return -1;
    }
    return p[s - 1];
}

void main() {
    int n, s, t; readf("%d %d %d\n", &n, &s, &t);
    p = stdin.readln.chomp.split.map!(to!int).array;
    e = new bool[n];
    int x = s;
    int i = 0;
    while (true) {
        if (x == t) {
            writeln(i);
            break;
        }
        i++;
        x = next(x);
        if (i == n) {
            writeln(-1);
            break;
        }
    }
}

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

int[] p;

int next(int s) {
    return p[s - 1];
}

void main() {
    int n, s, t; readf("%d %d %d\n", &n, &s, &t);
    p = stdin.readln.chomp.split.map!(to!int).array;
    int x = s;
    int i = 0;
    while (true) {
        if (x == t) {
            writeln(i);
            break;
        }
        i++;
        x = next(x);
        if (i == n) {
            writeln(-1);
            break;
        }
    }
}

import std.stdio;

void main ()
{
	int n, s, t;
	while (readf (" %s %s %s", &n, &s, &t) > 0)
	{
		s--;
		t--;
		auto p = new int [n + 1];
		foreach (i; 0..n)
		{
			readf (" %s", &p[i]);
		}
		int res = -1;
		p[] -= 1;
		foreach (i; 0..n)
		{
			if (s == t)
			{
				res = i;
				break;
			}
			s = p[s];
		}
		writeln (res);
	}
}

import std.algorithm;
import std.container;
import std.exception;
import std.math;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n, s, t;
main_loop:
	while (readf (" %s %s %s", &n, &s, &t) > 0)
	{
		s--;
		t--;
		auto p = new int [n + 1];
		foreach (i; 0..n)
		{
			readf (" %s", &p[i]);
		}
		p[] -= 1;
		foreach (i; 0..n)
		{
			if (s == t)
			{
				writeln (i);
				continue main_loop;
			}
			s = p[s];
		}
		writeln (-1);
	}
}

module sigod.codeforces.p285B;

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

void main()
{
	int n, s, t;
	stdin.readf("%s %s %s", &n, &s, &t);

	if (s == t) {
		stdout.write("0");
		return;
	}

	stdin.readln();
	int[] p = read_array!(int)();

	--s;
	--t;

	int result = 0;
	int position = s;

	while (position != t && p[position] != -1) {
		auto tmp = position;
		position = p[position] - 1;

		p[tmp] = -1;

		++result;
	}

	if (position != t) result = -1;

	stdout.write(result);
}

private
T[] read_array(T)() {
	auto input = stdin.readln().strip().split();

	T[] result = new T[input.length];

	foreach (index, ref element; input) {
		result[index] = element.to!T();
	}

	return result;
}