import std.stdio;
import std.string;

void main ()
{
	int divs = 0;
	foreach (p; 2..51)
	{
		bool ok = true;
		for (int d = 2; d * d <= p; d++)
		{
			if (p % d == 0)
			{
				ok = false;
			}
		}
		if (!ok)
		{
			continue;
		}
		writeln (p);
		stdout.flush ();
		bool cur = (readln.strip == "yes");
		divs += cur;
		if (cur && p * p <= 100)
		{
			writeln (p * p);
			stdout.flush ();
			bool next = (readln.strip == "yes");
			divs += next;
		}
	}
	writeln (divs > 1 ? "composite" : "prime");
}

import core.bitop;
import core.checkedint;
import core.simd;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

bool ask(int x) {
    writeln(x);
    stdout.flush();
    static char[4] storage;
    auto s = storage[ ];
    readln(s);
    return s[0] == 'y';
}

void main() {
    bool found;
    foreach (x; [2, 3, 5, 7])
        if (ask(x)) {
            if (found) {
                write("composite\n");
                return;
            }
            found = true;
            foreach (y; [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) {
                if (x * y > 100)
                    break;
                if (ask(x * y)) {
                    write("composite\n");
                    return;
                }
            }
        }
    write("prime\n");
}

import core.bitop;
import core.checkedint;
import core.simd;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

enum primes = [2, 3, 5, 7];

void main() {
    char[3] storage;
    foreach (x; primes) {
        writeln(x);
        stdout.flush();
        auto s = storage[ ];
        readln(s);
        if (s[0] == 'y') {
            write("composite\n");
            return;
        }
    }
    write("prime\n");
}

import core.bitop;
import core.checkedint;
import core.simd;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

enum primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71];

void main() {
    char[4] storage;
    foreach (x; primes) {
        writeln(x);
        stdout.flush();
        auto s = storage[ ];
        readln(s);
        if (s[0] == 'y') {
            write("composite\n");
            return;
        }
    }
    write("prime\n");
}

import core.bitop;
import core.checkedint;
import core.simd;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

char[4] storage;

bool ask(int x) {
    writeln(x);
    stdout.flush();
    auto s = storage[ ];
    readln(s);
    return s[0] == 'y';
}

void main() {
    bool found;
    foreach (x; [2, 3, 5, 7])
        if (ask(x)) {
            if (found) {
                write("composite\n");
                return;
            }
            found = true;
            foreach (y; [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47])
                if (ask(x * y)) {
                    write("composite\n");
                    return;
                }
        }
    write("prime\n");
}

import core.bitop;
import core.checkedint;
import core.simd;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

char[4] storage;

bool ask(int x) {
    writeln(x);
    stdout.flush();
    auto s = storage[ ];
    readln(s);
    return s[0] == 'y';
}

void main() {
    bool found;
    foreach (x; [2, 3, 5, 7])
        if (ask(x)) {
            if (found) {
                write("composite\n");
                return;
            }
            found = true;
            foreach (y; [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]) {
                if (x * y > 100)
                    break;
                if (ask(x * y)) {
                    write("composite\n");
                    return;
                }
            }
        }
    write("prime\n");
}

import core.bitop;
import core.checkedint;
import core.simd;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

enum primes = [2, 3, 5, 7];

void main() {
    char[4] storage;
    foreach (x; primes) {
        writeln(x);
        stdout.flush();
        auto s = storage[ ];
        readln(s);
        if (s[0] == 'y') {
            write("composite\n");
            return;
        }
    }
    write("prime\n");
}

import std.stdio;
import std.string;

void main ()
{
	bool ok = false;
	foreach (p; [2, 3, 5, 7])
	{
		writeln (p);
		stdout.flush ();
		ok |= readln.strip == "yes";
	}
	writeln (ok ? "composite" : "prime");
}


import std.stdio;
import std.math;
import std.array;
import std.algorithm;

bool solve(char[][] c){
  size_t n = c.length;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      int imp = 0;
      int cnt = 0;
      for(int a = i-1; a <= i+1; a++){
        for(int b = j-1;b <= j+1; b++){
          imp++;
          if(a >= 0 && a < n){
            if(b >= 0 && b < n){
              // write(c[a][b]);
              if(c[a][b] == 'o' && imp % 2 == 0){
                cnt++;
              }
            }
          }
        }
      }
      // writeln();
      // writeln(cnt);
      if(cnt % 2 == 1){
        return false;
      }
    }
  }
  return true;
}

void main(){
  int n;
  char c;

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

  char[][] l = new char[][](n,n);
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      char t = '\n';
      while(t == '\n'){
        readf("%c",&t);
      }
      l[i][j] = t;
    }
  }
  if(solve(l)){
    writeln("YES");
  } else {
    writeln("NO");
  }
}

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

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

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

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

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

immutable DX = [ +1, 0, -1, 0, ];
immutable DY = [ 0, +1, 0, -1, ];

int N;
string[] A;

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

		bool ans = true;
		foreach (x; 0 .. N) foreach (y; 0 .. N) {
			int cnt;
			foreach (dir; 0 .. 4) {
				const xx = x + DX[dir];
				const yy = y + DY[dir];
				if (0 <= xx && xx < N && 0 <= yy && yy < N) {
					if (A[xx][yy] == 'o') {
						++cnt;
					}
				}
			}
			ans = ans && (cnt % 2 == 0);
		}
		writeln(ans ? "YES" : "NO");
	}
	} catch (EOFException) {}
}


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

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

void solve() {
    int n = read!int;
    auto a = readarray!long;
    long twos;
    foreach(el; a)
        if (!(el & 1)) {
            while(el % 2 == 0) {
                el >>= 1;
                twos++;
            }
        }
    if (twos >= n)
        writeln(0);
    else {
        long[] oper;
        foreach(el;iota(n,0,-1).array) {
            if (!(el & 1)) {
                long tmp;
                while(el % 2 == 0) {
                    el >>= 1;
                    tmp++;
                }
                oper ~= tmp;
            }
        }
        sort!"a > b"(oper);
        n -= twos;
        foreach(i, el; oper)
            if (el >= n) {
                writeln(i+1);
                return;
            }
            else
                n -= el;

        writeln(-1);
    }
}

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

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

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!long).array;
    auto am = a.length.iota.map!(i => a[i] * (i + 1)).array;
    long lowzeroes = a.map!(x => cast(long)bsf(x)).sum;
    auto clzamd = a.length.iota.map!(i => bsf(am[i]) - bsf(a[i])).array.sort.reverse.array;
    long ans = 0;
    foreach (x ; clzamd) {
      if (lowzeroes >= n)
        break;
      if (x <= 0)
        break;
      lowzeroes += x;
      ans++;
    }
    writeln(lowzeroes >= n ? ans : -1);
  }
}


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

long sum(int left, int right, long[] s)
{
    long res = 0;
    if (right > 0) res += s[right - 1];
    if (left > 0) res -= s[left - 1];
    return res;
}

void solve(int[] a, int n)
{
    auto fdp = new long[n + 1];
    auto bdp = new long[n + 1];
    auto fs = new long[n + 1];
    auto fidx = new int[n + 1];
    auto bidx = new int[n + 1];
    foreach (i; 0 .. n + 1)
    {
        fs[i] = 0;
        if (i < n) fs[i] = a[i];
        if (i > 0) fs[i] += fs[i - 1];
        fdp[i] = sum(0, i, fs) - sum(i, i, fs);
        fidx[i] = i;
        if (i > 0)
        {
            auto val = fdp[i - 1] - a[i - 1];
            if (val > fdp[i])
            {
                fdp[i] = val;
                fidx[i] = fidx[i - 1];
            }
        }
    }
    for (int i = n; i >= 0; -- i)
    {
        bdp[i] = sum(i, i, fs) - sum(i, n, fs);
        bidx[i] = i;
        if (i < n)
        {
            auto val = bdp[i + 1] + a[i];
            if (val > bdp[i])
            {
                bdp[i] = val;
                bidx[i] = bidx[i + 1];
            }
        }
    }
    long ans = long.min;
    int li = -1, mi = -1, ri = -1;
    foreach (i; 0 .. n + 1)
    {
        if (fdp[i] + bdp[i] > ans)
        {
            ans = fdp[i] + bdp[i];
            li = fidx[i];
            ri = bidx[i];
            mi = i;
        }
    }
    writeln(li, " ", mi, " ", ri);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a, n);
    }
    return 0;
}

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

long sum(int left, int right, long[] s)
{
    long res = 0;
    if (right > 0) res += s[right - 1];
    if (left > 0) res -= s[left - 1];
    return res;
}

void solve(int[] a, int n)
{
    auto fdp = new long[n + 1];
    auto bdp = new long[n + 1];
    auto fs = new long[n + 1];
    auto fidx = new int[n + 1];
    auto bidx = new int[n + 1];
    foreach (i; 0 .. n + 1)
    {
        fs[i] = 0;
        if (i < n) fs[i] = a[i];
        if (i > 0) fs[i] += fs[i - 1];
        fdp[i] = long.min;
        foreach (j; 0 .. i + 1)
        {
            auto val = sum(0, j, fs) - sum(j, i, fs);
            if (val > fdp[i])
            {
                fdp[i] = val;
                fidx[i] = j;
            }
        }
    }
    for (int i = n; i >= 0; -- i)
    {
        bdp[i] = long.min;
        foreach (j; i .. n + 1)
        {
            auto val = sum(i, j, fs) - sum(j, n, fs);
            if (val > bdp[i])
            {
                bdp[i] = val;
                bidx[i] = j;
            }
        }
    }
    long ans = long.min;
    int li = -1, mi = -1, ri = -1;
    foreach (i; 0 .. n + 1)
    {
        if (fdp[i] + bdp[i] > ans)
        {
            ans = fdp[i] + bdp[i];
            li = fidx[i];
            ri = bidx[i];
            mi = i;
        }
    }
    writeln(li, " ", mi, " ", ri);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a, n);
    }
    return 0;
}

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

long sum(int left, int right, long[] s)
{
    long res = 0;
    if (right > 0) res += s[right - 1];
    if (left > 0) res -= s[left - 1];
    return res;
}

void solve(int[] a, int n)
{
    auto fdp = new long[n + 1];
    auto bdp = new long[n + 1];
    auto fs = new long[n + 1];
    auto fidx = new int[n + 1];
    auto bidx = new int[n + 1];
    foreach (i; 0 .. n + 1)
    {
        fs[i] = 0;
        if (i < n) fs[i] = a[i];
        if (i > 0) fs[i] += fs[i - 1];
        fdp[i] = sum(0, i, fs) - sum(i, i, fs);
        fidx[i] = i;
        if (i > 0)
        {
            auto val = fdp[i - 1];
            if (i < n) val -= a[i];
            if (val > fdp[i])
            {
                fdp[i] = val;
                fidx[i] = fidx[i - 1];
            }
        }
    }
    for (int i = n; i >= 0; -- i)
    {
        bdp[i] = sum(i, i, fs) - sum(i, n, fs);
        bidx[i] = i;
        if (i < n)
        {
            auto val = bdp[i + 1] + a[i];
            if (val > bdp[i])
            {
                bdp[i] = val;
                bidx[i] = bidx[i + 1];
            }
        }
    }
    long ans = long.min;
    int li = -1, mi = -1, ri = -1;
    foreach (i; 0 .. n + 1)
    {
        if (fdp[i] + bdp[i] > ans)
        {
            ans = fdp[i] + bdp[i];
            li = fidx[i];
            ri = bidx[i];
            mi = i;
        }
    }
    writeln(li, " ", mi, " ", ri);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a, n);
    }
    return 0;
}

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

long sum(int left, int right, long[] s)
{
    long res = 0;
    if (right > 0) res += s[right - 1];
    if (left > 0) res -= s[left - 1];
    return res;
}

void solve(int[] a, int n)
{
    auto fdp = new long[n + 1];
    auto bdp = new long[n + 1];
    auto fs = new long[n + 1];
    auto fidx = new int[n + 1];
    auto bidx = new int[n + 1];
    foreach (i; 0 .. n + 1)
    {
        fs[i] = 0;
        if (i < n) fs[i] = a[i];
        if (i > 0) fs[i] += fs[i - 1];
        fdp[i] = sum(0, i, fs) - sum(i, i, fs);
        fidx[i] = i;
        if (i > 0 && i < n)
        {
            auto val = fdp[i - 1] - a[i];
            if (val > fdp[i])
            {
                fdp[i] = val;
                fidx[i] = fidx[i - 1];
            }
        }
    }
    for (int i = n; i >= 0; -- i)
    {
        bdp[i] = sum(i, i, fs) - sum(i, n, fs);
        bidx[i] = i;
        if (i < n)
        {
            auto val = bdp[i + 1] + a[i];
            if (val > bdp[i])
            {
                bdp[i] = val;
                bidx[i] = bidx[i + 1];
            }
        }
    }
    long ans = long.min;
    int li = -1, mi = -1, ri = -1;
    foreach (i; 0 .. n + 1)
    {
        if (fdp[i] + bdp[i] > ans)
        {
            ans = fdp[i] + bdp[i];
            li = fidx[i];
            ri = bidx[i];
            mi = i;
        }
    }
    writeln(li, " ", mi, " ", ri);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a, n);
    }
    return 0;
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		auto index = w.MAKE_IDX!"a > b";
		long tot;
		foreach (i; index)
		{
			if (w[i] > W) continue;
			if (w[i] >= (W+1) / 2)
			{
				tot += w[i];
				ans[ti] ~= i+1;
				break;
			}
			tot += w[i];
			ans[ti] ~= i+1;
			if (tot >= (W+1) / 2) break;
		}
		if (tot < (W+1) / 2)
			ans[ti].length = 0;
	}

	foreach (e; ans)
	{
		if (e.length == 0)
			writeln(-1);
		else
		{
			writeln(e.length);
			e.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n;
		long w;
		readf !(" %s %s") (n, w);
		readln;
		auto s = readln.splitter.map !(to !(long)).array;
		auto p = n.iota.array;
		p.sort !((i, j) => s[i] > s[j]);
		int [] answer;
		long total = 0;
		foreach (ref i; p)
		{
			if (total + s[i] <= w)
			{
				answer ~= i + 1;
				total += s[i];
			}
		}
		if (total * 2 < w)
		{
			writeln (-1);
		}
		else
		{
			writeln (answer.length);
			writefln !("%(%s %)") (answer);
		}
	}
}


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

void main ()
{
	int n;
	int k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		auto a = new int [2000 + 1];
		foreach (i; 0..n)
		{
			int x;
			readf (" %s", &x);
			a[x]++;
		}
		long res = 0;
		int v = k;
		foreach_reverse (i, c; a)
		{
			foreach (j; 0..c)
			{
				if (v == k)
				{
					res += (i - 1) * 2;
				}
				v--;
				if (v == 0)
				{
					v = k;
				}
			}
		}
		writeln (res);
	}
}

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

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

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

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

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

int N, K;
int[] F;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		K = readInt;
		F = new int[N];
		foreach (i; 0 .. N) {
			F[i] = readInt;
		}

		F.sort();
		int ans;
		for (int i = N - 1; i >= 0; i -= K) {
			ans += (F[i] - 1) * 2;
		}
		writeln(ans);
	}
	} catch (EOFException) {}
}


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

immutable long MOD = 10^^9 + 7;

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

    long sm(long a, long n) {
        a %= MOD;
        n %= MOD;
        return n * (a + (n - 1)) % MOD;
    }

    long calc(long x, long n, long a, long cnt) {
        long res = 0;
        while (cnt + n + 2 * n <= x) {
            (res += sm(a, n)) %= MOD;
            (a += 2 * n % MOD) %= MOD;
            cnt += n + 2 * n;
            n = n * 4;
        }
        (res += sm(a, min(x - cnt, n))) %= MOD;
        return res;
    }

    long ansl = calc(l-1, 1, 1, 0) + calc(l-1, 2, 2, 1);
    long ansr = calc(r, 1, 1, 0) + calc(r, 2, 2, 1);
    writeln(((ansr - ansl) % MOD + MOD) % MOD);
}

import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;

alias Tree = RBT!int;

long MOD = 1_000_000_007;

long find(long idx) {

    long eve = 1;
    long[] nn = [0, 0]; // 0 even, 1 odd
    uint turn = 1;
    long mul = 1;
    long tt = 0;
    if (idx <= 0)
        return 0;
    while (true) {
        if (tt + mul > idx) {
            nn[turn] += (idx - tt);
            tt += (idx - tt);
            break;
        }

        nn[turn] += mul;
        tt += mul;

        turn = 1 - turn;
        mul = 2 * mul;
    }
    long nod = nn[1];
    long nev = nn[0];

    long tt1 = ((nod % MOD) * (nod % MOD)) % MOD;
    long tt2 = 0;
    if (nev > 0)
        tt2 = (((nev + 1) % MOD) * (nev % MOD) % MOD);

    return (tt1 + tt2) % MOD;
}

void main() {
    GC.disable();

    long l, r;
    readf(" %s %s", l, r);

    writeln((find(r) - find(l - 1) + MOD) % MOD);

}

module c;import std.algorithm.comparison;
import std.algorithm.iteration;
import std.algorithm.searching;
import std.range;
import std.stdio;

const MOD = 1000000007;

private long sum(long n) {
    long[] count = [0, 0];
    long n1 = 0, d = 1, s = 0;
    int odd = 1;
    while (n1 < n) {
        if (n1 + d >= n) {
            count[odd] += n - n1;
            break;
        } else {
            n1 += d;
            count[odd] += d;
            d *= 2;
            odd = 1 - odd;
        }
    }
    count[0] %= MOD;
    count[1] %= MOD;
    return (count[0]*(count[0]+1) + count[1]*count[1]) % MOD;
}

void main() {
    long l, r;
    readf("%s %s", l, r);
    writeln((MOD + sum(r) - sum(l-1)) % MOD);
}


// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm, std.array, std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;

alias ll = long;
int isz(long t){ return to!int(t); }
static string[] tempstr;
T rd(T = long)() { while(!tempstr.length) tempstr = readln.chomp.split; string res = tempstr[0]; tempstr.popFront; return res.to!T; }
T[] rdarr(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void play(){
    int n, m, k;
    n = rd!int;
    m = rd!int;
    k = rd!int;
    int win = min(m, n/k);
    int leftj = (m - win);
    int max2 = leftj/(k-1) + (leftj % (k - 1) != 0);
    writeln(win - max2);
}

int main(){
    ll t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m, k;
		readf !(" %s %s %s") (n, m, k);
		auto win = min (m, n / k);
		auto left = m - win;
		auto others = k - 1;
		auto lose = (left + others - 1) / others;
		writeln (win - lose);
	}
}

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

		auto cnt = n / k;
		auto x = min(cnt, m);
		m -= x;
		ans[ti] = x - (m+k-2) / (k-1);
	}

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

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm, std.array, std.container, std.range, std.typecons, std.bigint, std.numeric, std.math, std.random;

alias ll = long;
int isz(long t){ return to!int(t); }
static string[] tempstr;
T rd(T = long)() { while(!tempstr.length) tempstr = readln.chomp.split; string res = tempstr[0]; tempstr.popFront; return res.to!T; }
T[] rdarr(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void play(){
    int n, m, k;
    n = rd!int;
    m = rd!int;
    k = rd!int;
    int win = min(m, n/k);
    int leftj = (m - win);
    int max2 = leftj/(k-1) + (leftj % (k - 1));
    writeln(win - max2);
}

int main(){
    ll t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}


import std.stdio;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int T = readln.chomp.to!int;
    foreach (t; 0 .. T) {
        int N, K; scanf("%d %d\n", &N, &K);
        int[] xs, ys;
        if (K == 0) {
            for (int i = 0; i < N/2; i++) {
                xs ~= i;
                ys ~= (N - 1) - i;
            }
        } else if (K <= N - 2) {
            for (int i = 1; i < N/2; i++) {
                if (i == K || i == N-1-K) continue;
                xs ~= i;
                ys ~= (N - 1) - i;
            }
            xs ~= N - 1;
            ys ~= K;
            xs ~= 0;
            ys ~= N - 1 - K;
        } else {
            // K == N - 1
            if (N >= 8) {
                xs ~= N-1; ys ~= N-2;
                xs ~= 1;   ys ~= 3;
                xs ~= 0;   ys ~= N-4;
                xs ~= N-3; ys ~= 2;
                for (int i = 4; i < N/2; i++) {
                    xs ~= i;
                    ys ~= (N - 1) - i;
                }
            } else {
                writeln(-1);
                continue;
            }
        }
        DBG!N;
        DBG!K;
        long s = 0;
        for (int i = 0; i < N/2; i++) {
            s += xs[i] & ys[i];
        }
        DBG!s;
        assert(xs.length == N/2 && ys.length == N/2);
        assert(s == K);
        for (int i = 0; i < N/2; i++) {
            writefln("%d %d", xs[i], ys[i]);
        }

    }
}

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)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		int [2] [] a;
		foreach (i; 0..n / 2)
		{
			a ~= [i, n - 1 - i];
		}
		if (n == 4 && k == 3)
		{
			writeln (-1);
			continue;
		}
		int t = min (k, n - 2);
		int pos = (t >= n / 2) ? (n - 1 - t) : (t);
		int qos = (t >= n / 2);
		swap (a[0][0], a[pos][qos]);
		if (k > t)
		{
			swap (a[pos][qos], a[$ - 1][0]);
		}
		writefln !("%(%(%s %)\n%)") (a);
	}
}

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

		if (n == 4 && k == 3) continue;

		foreach (i; 0..n/2)
		{
			ans[ti] ~= [i, n-i-1];
		}

		auto x = k & (n/2-1);
		swap(ans[ti][x][1], ans[ti][0][1]);
		if (k & (n/2))
		{
			if (x != n/2-1)
				swap(ans[ti][n/2-1][1], ans[ti][0][0]);
			else
				swap(ans[ti][n/2-2][1], ans[ti][0][0]);
		}
		debug
		{
			long tot;
			foreach (ee; ans[ti])
				tot += ee[0] & ee[1];
			if (tot != k)
				writeln("wa:n=", n, " k=", k);
		}
	}
	debug writeln("done");
	debug readln;

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

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double 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 k = RD!int;

		if (n == 4 && k == 3) break;

		foreach (i; 0..n/2)
		{
			ans[ti] ~= [i, n-i-1];
		}

		auto x = k & (n/2-1);
		swap(ans[ti][x][1], ans[ti][0][1]);
		if (k & (n/2))
		{
			if (x != n/2-1)
				swap(ans[ti][n/2-1][1], ans[ti][0][0]);
			else
				swap(ans[ti][n/2-2][1], ans[ti][0][0]);
		}
	}

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

import std.stdio;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int T = readln.chomp.to!int;
    foreach (t; 0 .. T) {
        int N, K; scanf("%d %d\n", &N, &K);
        int[] xs, ys;
        if (K == 0) {
            for (int i = 0; i < N/2; i++) {
                xs ~= i;
                ys ~= (N - 1) - i;
            }
        } else if (K <= N - 2) {
            for (int i = 1; i < N/2; i++) {
                if (i == K || i == N-1-K) continue;
                xs ~= i;
                ys ~= (N - 1) - i;
            }
            xs ~= N - 1;
            ys ~= K;
            xs ~= 0;
            ys ~= N - 1 - K;
        } else {
            // K == N - 1
            if (N >= 8) {
                xs ~= N-1; ys ~= N-2;
                xs ~= 1;   ys ~= 3;
                xs ~= 0;   ys ~= N-4;
                xs ~= N-3; ys ~= 2;
                for (int i = 4; i < N/2; i++) {
                    xs ~= i;
                    ys ~= (N - 1) - i;
                }
            } else {
                writeln(-1);
                return;
            }
        }
        DBG!N;
        DBG!K;
        long s = 0;
        for (int i = 0; i < N/2; i++) {
            s += xs[i] & ys[i];
        }
        DBG!s;
        assert(xs.length == N/2 && ys.length == N/2);
        assert(s == K);
        for (int i = 0; i < N/2; i++) {
            writefln("%d %d", xs[i], ys[i]);
        }

    }
}

import std.stdio;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int T = readln.chomp.to!int;
    foreach (t; 0 .. T) {
        int N, K; scanf("%d %d\n", &N, &K);
        int[] xs, ys;
        if (K == 0) {
            for (int i = 0; i < N/2; i++) {
                xs ~= i;
                ys ~= (N - 1) - i;
            }
        } else if (K <= N - 2) {
            for (int i = 1; i < N/2; i++) {
                if (i == K || i == N-1-K) continue;
                xs ~= i;
                ys ~= (N - 1) - i;
            }
            xs ~= N - 1;
            ys ~= K;
            xs ~= 0;
            ys ~= N - 1 - K;
        } else {
            // K == N - 1
            if (N >= 8) {
                xs ~= N-1;
                ys ~= N-2;
                xs ~= 1;
                ys ~= 3;
                xs ~= 0;
                ys ~= N-4;
                xs ~= N - 3;
                ys ~= 2;
                for (int i = 5; i < N/2; i++) {
                    xs ~= i;
                    ys ~= (N - 1) - i;
                }
            } else {
                writeln(-1);
                return;
            }
        }
        assert(xs.length == N/2 && ys.length == N/2);
        long s = 0;
        for (int i = 0; i < N/2; i++) {
            s += xs[i] & ys[i];
        }
        assert(s == K);
        for (int i = 0; i < N/2; i++) {
            writefln("%d %d", xs[i], ys[i]);
        }

    }
}


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 S = readToken;

        bool ans = true;
        int now;
        foreach (s; S) {
          (s == 'A') ? ++now : --now;
          ans = ans && (now >= 0);
        }
        ans = ans && (S[$ - 1] == 'B');
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

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

bool solve (string s)
{
	if (s.back != 'B')
	{
		return false;
	}
	int b = 0;
	foreach (ref c; s)
	{
		b += (c == 'A') ? +1 : -1;
		if (b < 0)
		{
			return false;
		}
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		writeln (solve (s) ? "YES" : "NO");
	}
}

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

bool solve (string s)
{
	if (!s.canFind ('B'))
	{
		return false;
	}
	int b = 0;
	foreach (ref c; s)
	{
		b += (c == 'A') ? +1 : -1;
		if (b < 0)
		{
			return false;
		}
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		writeln (solve (s) ? "YES" : "NO");
	}
}


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

enum int INF = 1<<29;

void solve() {
    readln;
    int N, K; readf("%d %d\n", &N, &K);
    int X, Y; readf("%d %d\n", &X, &Y); X--; Y--;
    auto _A = readln.chomp.split(" ").map!(to!int).array;
    auto A = new bool[N];
    foreach (a; _A) {
        a--;
        A[a] = true;
    }
    auto G = new int[][N];
    foreach (i; 0 .. (N-1)) {
        int u, v; readf("%d %d\n", &u, &v);
        u--; v--;
        G[u] ~= v;
        G[v] ~= u;
    }

    alias P = Tuple!(bool,"g", int,"v", int,"p");
    alias S = Tuple!(int,bool);
    S[P] cache;
    S f(bool g, int v, int p) {
        auto key = P(g, v, p);
        if (key in cache) return cache[key];
        int ans = 0;
        bool a_exist = A[v];
        foreach (nv; G[v]) {
            if (nv == p) continue;
            auto c = f(g, nv, v);
            if (c[1]) {
                ans += c[0] + (g ? 1 : 2);
                a_exist = true;
            }
        }
        return cache[key] = S(ans, a_exist);
    }

    auto dx = new int[N]; dx[] = INF;
    auto dy = new int[N]; dy[] = INF;
    void filld(int v, int p, int d, int[] ds) {
        ds[v] = d;
        foreach (nv; G[v]) {
            if (nv == p) continue;
            filld(nv, v, d+1, ds);
        }
    }
    filld(X, -1, 0, dx);
    filld(Y, -1, 0, dy);

    int total = f(false, X, -1)[0];
    int ans = INF;
    for (int z = 0; z < N; z++) {
        if (A[z]) {
            ans = min(ans, total - dx[z] + dy[z]);
        }
    }
    writeln(ans);
}

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

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

immutable int NA = -1;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		int x, y;
		readf !(" %s %s") (x, y);
		x -= 1;
		y -= 1;
		readln;

		auto a = readln.splitter.map !(to !(int)).array;
		a[] -= 1;
		auto imp = new bool [n];
		foreach (ref c; a)
		{
			imp[c] = true;
		}
		imp[y] = true;

		auto adj = new int [] [n];
		foreach (i; 0..n - 1)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			adj[u] ~= v;
			adj[v] ~= u;
		}
		readln;

		auto d = new int [n];

		void recur (int v, int p, int depth)
		{
			d[v] = depth;
			foreach (ref u; adj[v])
			{
				if (u != p)
				{
					recur (u, v, depth + 1);
					imp[v] |= imp[u];
				}
			}
		}

		recur (x, NA, 0);

		writeln ((imp.sum - 1) * 2 - d[y]);
	}
}


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

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

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

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

int N;
long[][] M;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      M = new long[][](N, N);
      foreach (x; 0 .. N) foreach (y; 0 .. N) {
        M[x][y] = readLong();
      }

      auto ans = new long[N];
      foreach (i; 0 .. N) {
        const j = (i + 1) % N;
        const k = (i + 2) % N;
        const b = M[i][j] * M[i][k] / M[j][k];
        long lo = 0, hi = 2 * cast(long)(sqrt(cast(real)(b))) + 10;
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          (mid * mid >= b) ? (hi = mid) : (lo = mid);
        }
        ans[i] = hi;
      }
      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(ans[i]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}

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

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

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

    int[][] a = new int[][](n,n);

    foreach (i; 0 .. n) {
        foreach (j; 0 .. n) {
            readf!" %s"(a[i][j]);
        }
    }

    int g = 0;
    foreach(j; 0 .. n) {
        g = gcd(g,a[0][j]);
    }

    int ans;
    for (long i = 1; i*i <= g; i++) {
        if (g % i == 0) {
            if ((a[0][1]/i) * (a[0][2]/i) == a[1][2]){
                ans = to!int(i);
                break;
            }
            if ((a[0][1]/(g/i)) * (a[0][2]/(g/i)) == a[1][2]) {
                ans = g/i;
                break;
            }
        }
    }

    writef!"%d "(ans);
    foreach (i; 1 .. n) {
        writef!"%d "(a[0][i]/ans);
    }
    writeln();
}

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

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

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

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

int N;
long[][] M;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      M = new long[][](N, N);
      foreach (x; 0 .. N) foreach (y; 0 .. N) {
        M[x][y] = readLong();
      }

      auto ans = new long[N];
      foreach (i; 0 .. N) {
        const j = (i + 1) % N;
        const k = (i + 2) % N;
        const b = M[i][j] * M[i][k] / M[j][k];
        long lo = 0, hi = b;
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          (mid * mid >= b) ? (hi = mid) : (lo = mid);
        }
        ans[i] = hi;
      }
      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(ans[i]);
      }
      writeln();
    }
  } catch (EOFException 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.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)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

int M, K;
string S;
int[] C;
int[][] X;

int[][] uss;
int[][] graph;
int[] colors;

void dfs(int u) {
  foreach (i; graph[u]) {
    const v = uss[i][0] ^ uss[i][1] ^ u;
    if (colors[v] == -1) {
      colors[v] = (colors[u] ^ ('1' - S[i]));
      dfs(v);
    }
    assert(colors[v] == (colors[u] ^ ('1' - S[i])));
  }
}

void main() {
  try {
    for (; ; ) {
      M = readInt();
      K = readInt();
      S = readToken();
      C = new int[K];
      X = new int[][K];
      foreach (u; 0 .. K) {
        C[u] = readInt();
        X[u] = new int[C[u]];
        foreach (j; 0 .. C[u]) {
          X[u][j] = readInt() - 1;
        }
      }

      uss = new int[][M];
      foreach (u; 0 .. K) {
        foreach (j; 0 .. C[u]) {
          uss[X[u][j]] ~= u;
        }
      }
      graph = new int[][K + 1];
      foreach (i; 0 .. M) {
        switch (uss[i].length) {
          case 0: {
            // no edge
          } break;
          case 1: {
            uss[i] ~= K;
            graph[uss[i][0]] ~= i;
            graph[uss[i][1]] ~= i;
          } break;
          case 2: {
            graph[uss[i][0]] ~= i;
            graph[uss[i][1]] ~= i;
          } break;
          default: assert(false);
        }
      }
      debug {
        writeln("uss = ", uss);
      }
      colors = new int[K + 1];
      colors[] = -1;
      foreach_reverse (u; 0 .. K + 1) {
        if (colors[u] == -1) {
          colors[u] = 0;
          dfs(u);
        }
      }
      debug {
        writeln("colors = ", colors);
      }

      int ans;
      auto nums = new int[][](K + 1, 2);
      foreach (u; 0 .. K + 1) {
        ++nums[u][colors[u]];
      }
      auto uf = new int[K + 1];
      uf[] = -1;
      int get(int r) {
        if (uf.root(K) == r) {
          return nums[r][1];
        } else {
          return min(nums[r][0], nums[r][1]);
        }
      }

      foreach (i; 0 .. M) {
        if (uss[i]) {
          int ru = uf.root(uss[i][0]);
          int rv = uf.root(uss[i][1]);
          if (ru != rv) {
            ans -= get(ru);
            ans -= get(rv);
            uf.connect(ru, rv);
            if (ru != uf.root(ru)) {
              swap(ru, rv);
            }
            nums[ru][] += nums[rv][];
            ans += get(ru);
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto toDo = readln.strip.map !(x => x == '0').array;
		auto a = new int [] [n];
		auto s = new int [] [k];
		foreach (j; 0..k)
		{
			int len;
			readf !(" %s") (len);
			foreach (i; 0..len)
			{
				int elem;
				readf !(" %s") (elem);
				elem -= 1;
				a[elem] ~= j;
				s[j] ~= elem;
			}
		}

		auto p = k.iota.array;
		auto comp = new int [] [k];
		auto cost = new int [k];
		auto state = new int [k];
		auto stateRoot = new int [k];
		auto isFixed = new bool [k];

		foreach (j; 0..k)
		{
			comp[j] ~= j;
		}

		int root (int v)
		{
			if (p[v] != v)
			{
				p[v] = root (p[v]);
			}
			return p[v];
		}

		int getState (int u)
		{
			return state[u] ^ stateRoot[root (u)];
		}

		int totalCost = 0;

		void switchComp (int u)
		{
			auto x = root (u);
			debug {writeln ("switchComp ", u, " ", x);}
			stateRoot[x] ^= 1;
			auto newCostX = comp[x].length.to !(int) - cost[x];
			debug {writeln (cost[x], " -> ", newCostX);}
			totalCost -= cost[x];
			cost[x] = newCostX;
			totalCost += cost[x];
		}

		bool unite (int u, int v, bool toDoHere)
		{
			auto x = root (u);
			auto y = root (v);
			debug {writeln ("unite ", u, " ", v, " ",
			    toDoHere, " ", x, " ", y);}
			auto hasWork = toDoHere ^ getState (u) ^ getState (v);
			debug {writeln (toDoHere, " ", getState (u), " ",
			    getState (v));}
			if (hasWork)
			{
				debug {writeln ("hasWork ", x, " ", y);}
				if (x == y)
				{
					assert (false);
				}
				auto newCostX =
				    comp[x].length.to !(int) - cost[x];
				auto newCostY =
				    comp[y].length.to !(int) - cost[y];
				if (!isFixed[x] && (isFixed[y] ||
				    cost[x] + newCostY > newCostX + cost[y]))
				{
					swap (u, v);
					swap (x, y);
				}
				if (isFixed[y])
				{
					assert (false);
				}
				switchComp (y);
			}

			if (x == y)
			{
				return false;
			}
			if (comp[y].length < comp[x].length)
			{
				swap (x, y);
			}
			if (stateRoot[x] != stateRoot[y])
			{
				foreach (w; comp[x])
				{
					state[w] ^= 1;
				}
			}
			p[x] = y;
			comp[y] ~= comp[x];
			cost[y] += cost[x];
			isFixed[y] |= isFixed[x];
			return true;
		}

		foreach (i; 0..n)
		{
			debug {writeln (i + 1, ": type ", a[i].length);}
			debug {writeln ("state: ", state);}
			debug {writeln ("stateRoot: ", stateRoot);}
			debug {writeln ("getState: ", k.iota.map !(getState));}
			debug {writeln ("isFixed: ", isFixed);}
			if (a[i].length == 0)
			{
			}
			else if (a[i].length == 1)
			{
				auto u = a[i][0];
				auto x = root (u);
				auto hasWork = toDo[i] ^ getState (u);
				if (hasWork)
				{
					if (isFixed[x])
					{
						assert (false);
					}
					switchComp (x);
				}
				isFixed[x] = true;
			}
			else if (a[i].length == 2)
			{
				unite (a[i][0], a[i][1], toDo[i]);
			}
			else
			{
				assert (false);
			}

			writeln (totalCost);
		}
	}
}


import std.stdio;

int len, p1, p2;
int[] ans;
char[] s;

int main() {
  readln(s);
  ans.length = s.length;
  len = s.length - 1;
  p1 = 0, p2 = len - 1;
  for (int i = 0; i < len; ++ i) {
    if (s[i] == 'l') ans[p2 --] = i + 1;
    else ans[p1 ++] = i + 1;
  }
  for (int i = 0; i < len; ++ i) writeln(ans[i]);
  return 0;
}

import std.array;
import std.stdio;
import std.string;

void main() {
	string commands = strip(stdin.readln());

	auto array = appender!(int[])();

	foreach (i, c; commands) {
		if (c == 'r') {
			stdout.writeln(i + 1);
		} else {
			array.put(i + 1);
		}
	}

	auto end = array.data;

	for (int i = end.length - 1; i >= 0; --i) {
		stdout.writeln(end[i]);
	}
}

import std.stdio;
import std.c.stdio;
import std.typecons;

void output (Tuple !(int, int, int) v [], int i)
{
	if (i == -1)
	{
		return;
	}
	output (v, v[i][0]);
	printf ("%d\n", v[i][2]);
	output (v, v[i][1]);
}

int main ()
{
	string s;
	while ((s = readln ()) != null)
	{
		auto n = s.length - 1;
		Tuple !(int, int, int) v [];
		v ~= tuple (-1, -1, 1);
		foreach (int i; 0..n - 1)
		{
			if (s[i] == 'l')
			{
				v[$ - 1][0] = i + 1;
			}
			else
			{
				v[$ - 1][1] = i + 1;
			}
			v ~= tuple (-1, -1, i + 2);
		}

		output (v, 0);
	}
	return 0;
}

import std.stdio;
import std.c.stdio;
import std.typecons;

int main ()
{
	string s;
	while ((s = readln ()) != null)
	{
		auto n = s.length - 1;
		Tuple !(int, int, int) v [];
		v ~= tuple (-1, -1, 1);
		foreach (int i; 0..n - 1)
		{
			if (s[i] == 'l')
			{
				v[$ - 1][0] = i + 1;
			}
			else
			{
				v[$ - 1][1] = i + 1;
			}
			v ~= tuple (-1, -1, i + 2);
		}

		void output (int i)
		{
			if (i == -1)
			{
				return;
			}
			output (v[i][0]);
			printf ("%d\n", v[i][2]);
			output (v[i][1]);
		}

		output (0);
	}
	return 0;
}

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.stdio;

void main() {
    auto S = readln.chomp;
    auto N = S.length.to!int;
    auto A = new int[](N);
    int l = 0, r = N - 1;

    foreach (i; 0..N) {
        if (S[i] == 'l') {
            A[i] = r;
            r--;
        } else {
            A[i] = l;
            l++;
        }
    }

    auto B = new int[](N);
    foreach (i; 0..N) B[A[i]] = i;
    B.each!(b => (b + 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;

int solve(int c00, int c01, int c10, int c11, int depth, bool flag)
{
  if (depth > 1000001)
    return int.max;

  if (c01 + c10 == 0)
    return 0;

  int ans1 = int.max, ans2 = int.max;

  if (flag) {
    if (c11 > 0) {
      int x = solve(c10, c11 - 1, c00, c01 + 1, depth + 1, !flag);
      if (x != int.max)
        ans1 = 1 + x;
    }
  }

  if (!flag) {
    if (c10 > 0) {
      int x = solve(c10 - 1, c11, c00 + 1, c01, depth + 1, !flag);
      if (x != int.max)
        ans2 = 1 + x;
    }
  }

  return min(ans1, ans2);
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip;
    auto b = readln.strip;
    int c00, c01, c10, c11;
    foreach (i ; 0 .. n) {
      if (a[i] == '0' && b[i] == '0')
        c00++;
      if (a[i] == '0' && b[i] == '1')
        c01++;
      if (a[i] == '1' && b[i] == '0')
        c10++;
      if (a[i] == '1' && b[i] == '1')
        c11++;
    }
    int ans1 = solve(c00, c01, c10, c11, 0, true);
    int ans2 = solve(c00, c01, c10, c11, 0, false);
    int ans = min(ans1, ans2);
    writeln(ans == int.max ? -1 : ans);
  }
}

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

		long x0, x1, y0, y1;
		foreach (i; 0..n)
		{
			if (a[i] == b[i])
			{
				if (a[i] == '0')
					++x0;
				else
					++x1;
			}
			else
			{
				if (a[i] == '0')
					++y0;
				else
					++y1;
			}
		}
		ans[ti] = long.max;
		if ((x0+x1) % 2 == 1 && (x0+x1+1)/2 == x1)
			ans[ti].chmin(x0+x1);
		if ((y0+y1) % 2 == 0 && (y0+y1+1)/2 == y1)
			ans[ti].chmin(y0+y1);
		if (ans[ti] == long.max)
			ans[ti] = -1;
	}

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

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

int solve (int n, bool [] a, bool [] b)
{
	auto aOnes = a.sum.to !(int);
	auto bOnes = b.sum.to !(int);
	auto same = n.iota.count !(i => a[i] == b[i]).to !(int);
	auto diff = n - same;
	int res = int.max;
	if (aOnes == bOnes)
	{
		res = min (res, diff);
	}
	if (aOnes + bOnes == n + 1)
	{
		res = min (res, same);
	}
	if (res == int.max)
	{
		res = -1;
	}
	return res;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.strip.map !(q{a == '1'}).array;
		auto b = readln.strip.map !(q{a == '1'}).array;
		writeln (solve (n, a, b));
	}
}

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

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

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

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

enum INF = 10^^9;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        const A = readToken();
        const B = readToken();

        int ans = INF;
        if (A == B) {
          ans = 0;
        } else {
          const a = cast(int)(A.count('1'));
          const b = cast(int)(B.count('1'));
          if (a >= 1 && b >= 1 && a == b) {
            int d;
            foreach (i; 0 .. N) {
              if (A[i] != B[i]) {
                ++d;
              }
            }
            chmin(ans, d);
          }
          if (a >= 1 && b >= 1 && (a - 1) + (b - 1) == N - 1) {
            int d;
            foreach (i; 0 .. N) {
              if (A[i] == B[i]) {
                ++d;
              }
            }
            chmin(ans, d);
          }
        }

        writeln((ans >= INF) ? -1 : ans);
      }
    }
  } catch (EOFException e) {
  }
}

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto s1 = cast(byte[]) readString;
  auto s2 = cast(byte[]) readString;
  int[2][2] cnt;
  foreach(i; 0 .. n)
    {
      cnt[s1[i]-'0'][s2[i]-'0']++;
    }
  debug {
    foreach(r; cnt) writeln(r);
  }
  int minops = int.max;
  int k = cnt[1][0];
  if (cnt[0][1] == k) minops = min(minops, 2 * k);
  k = cnt[0][0];
  if (cnt[1][1] - k - 1 == 0) minops = min(minops, 2 * k + 1);
  if (minops != int.max) return writeln(minops);
  return writeln(-1);
}

// main {{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
// }}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}


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 a = RDA;
		ans[ti] = a.sum == m;
	}

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

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

void play(){
    int n, m;
    n = rd!int;
    m = rd!int;
    int summ = 0, el;
    foreach(i; 0..n){
        el = rd!int;
        summ += el;
    }
    if(summ == m){
        writeln("YES");
    }else{
        writeln("NO");
    }
}

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

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(){ auto r = readln.chomp.split.to!(T[]); return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }
alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, "x",  long, "y");
T max(T = long)(T a, T b){ return (a > b) ? a : b; }
T min(T = long)(T a, T b){ return (a < b) ? a : b; }


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.stdio;

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

    while (T--) {
        auto s = readln.split.map!(to!long);
        auto A = s[0];
        auto B = s[1];
        auto D = abs(A - B);
        long tmp = 0;
        for (long i = 0; ; ++i) {
            tmp += i;
            if (tmp >= D && tmp % 2 == D % 2) {
                i.writeln;
                break;
            }
        }
    }

}

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 long[](t);
	foreach (ti; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto d = max(a, b) - min(a, b);
		long x, j;
		foreach (i; 1..10^^5)
		{
			if (x >= d && (x - d) % 2 == 0) break;
			x = cast(long)i * (i+1) / 2;
			j = i;
		}
		ans[ti] = j;
	}

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

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 long[](t);
	foreach (ti; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto d = max(a, b) - min(a, b);
		long x, j;
		foreach (i; 1..10^^5)
		{
			if (x >= d) break;
			x = i * (i+1) / 2;
			j = i;
		}
		if (x == d)
			ans[ti] = j;
		else
			ans[ti] = j+1;
	}

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


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

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

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

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

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

        auto a = new int[][](M, N);
        foreach (x; 0 .. M) foreach (y; 0 .. N) {
          a[x][y] = A[x][y] - '0';
        }

        int[][] ans;
        void oper(int x0, int y0, int x1, int y1, int x2, int y2) {
          ans ~= [x0, y0, x1, y1, x2, y2];
          a[x0][y0] ^= 1;
          a[x1][y1] ^= 1;
          a[x2][y2] ^= 1;
        }

        foreach (x; 0 .. M - 2) foreach (y; 0 .. N - 2) {
          if (a[x][y]) {
            oper(x, y, x, y + 1, x + 1, y);
          }
        }
        foreach (x; 0 .. M - 2) {
          const y = N - 2;
          if (a[x][y]) {
            if (a[x][y + 1]) {
              oper(x, y, x, y + 1, x + 1, y);
            } else {
              oper(x, y, x + 1, y, x + 1, y + 1);
            }
          } else {
            if (a[x][y + 1]) {
              oper(x, y + 1, x + 1, y, x + 1, y + 1);
            } else {
              oper(x, y, x, y + 1, x + 1, y);
              oper(x, y, x, y + 1, x + 1, y);
            }
          }
        }
        foreach (y; 0 .. N - 2) {
          const x = M - 2;
          if (a[x][y]) {
            if (a[x + 1][y]) {
              oper(x, y, x + 1, y, x, y + 1);
            } else {
              oper(x, y, x, y + 1, x + 1, y + 1);
            }
          } else {
            if (a[x + 1][y]) {
              oper(x + 1, y, x, y + 1, x + 1, y + 1);
            } else {
              oper(x, y, x + 1, y, x, y + 1);
              oper(x, y, x + 1, y, x, y + 1);
            }
          }
        }
        {
          const x = M - 2, y = N - 2;
          foreach (p; 0 .. 1 << 4) {
            auto bs = new int[4];
            foreach (i; 0 .. 4) {
              if (p & 1 << i) {
                foreach (j; 0 .. 4) {
                  if (i != j) {
                    bs[j] ^= 1;
                  }
                }
              }
            }
            debug {
              writeln(p, ": ", bs);
            }
            bool ok = true;
            foreach (j; 0 .. 4) {
              ok = ok && (a[x + j / 2][y + j % 2] == bs[j]);
            }
            if (ok) {
              if (p & 1 << 0) oper(x, y + 1, x + 1, y, x + 1, y + 1);
              if (p & 1 << 1) oper(x, y, x + 1, y, x + 1, y + 1);
              if (p & 1 << 2) oper(x, y, x, y + 1, x + 1, y + 1);
              if (p & 1 << 3) oper(x, y, x, y + 1, x + 1, y);
            }
          }
        }

        writeln(ans.length);
        foreach (row; ans) {
          foreach (i; 0 .. 6) {
            if (i > 0) write(" ");
            write(row[i] + 1);
          }
          writeln;
        }
        debug {
          writeln("a = ", a);
        }
      }
    }
  } catch (EOFException e) {
  }
}

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)
	{
		int rows, cols;
		readf !(" %s %s") (rows, cols);
		readln;
		uint [] [] board;
		foreach (row; 0..rows)
		{
			board ~= readln.strip.map !(q{a - '0'}).array;
		}

		int [] [] answer;

		void go (int row, int col,
		    int row1, int col1, int row2, int col2)
		{
			answer ~= [row, col, row1, col1, row2, col2];
			board[row][col] ^= 1;
			board[row1][col1] ^= 1;
			board[row2][col2] ^= 1;
		}

		foreach_reverse (row; 0..rows)
		{
			foreach_reverse (col; 0..cols)
			{
				if ((row == rows - 1 && rows % 2 == 1) ||
				    (col == cols - 1 && cols % 2 == 1))
				{
					if (board[row][col])
					{
						auto row1 = max (row - 1, 0);
						auto col1 = max (col - 1, 0);
						auto row2 = row1 + 1;
						auto col2 = col1;
						if (row2 == row &&
						    col2 == col)
						{
							row2 = row1;
							col2 = col1 + 1;
						}
						go (row, col,
						    row1, col1, row2, col2);
					}
				}
			}
		}

		void doSquare (int row, int col)
		{
			while (true)
			{
				auto total = board[row + 0][col + 0] +
				    board[row + 1][col + 0] +
				    board[row + 0][col + 1] +
				    board[row + 1][col + 1];
				if (total == 0)
				{
					break;
				}
				int [] seqRow;
				int [] seqCol;
				foreach_reverse (value; 0..2)
				{
					foreach (rowT; row..row + 2)
					{
						foreach (colT; col..col + 2)
						{
							if (board[rowT][colT]
							    == value)
							{
								seqRow ~= rowT;
								seqCol ~= colT;
							}
						}
					}
				}
				if (total == 2)
				{
					seqRow.popFront ();
					seqCol.popFront ();
				}
				go (seqRow[0], seqCol[0],
				    seqRow[1], seqCol[1],
				    seqRow[2], seqCol[2]);
			}
		}

		for (int row = 0; row + 1 < rows; row += 2)
		{
			for (int col = 0; col + 1 < cols; col += 2)
			{
				doSquare (row, col);
			}
		}

		foreach (row; 0..rows)
		{
			foreach (col; 0..cols)
			{
				if (board[row][col])
				{
					assert (false);
				}
			}
		}

		writeln (answer.length);
		foreach (ref line; answer)
		{
			writefln !("%(%s %)") (line.map !(q{a + 1}));
		}
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

		foreach (y; 0..n-1)
		{
			foreach (x; 0..m-1)
			{
				debug writeln("y:", y, " x:", x);
				void draw(long[] pos)
				{
					ans[ti] ~= [pos[0]+1, pos[1]+1, pos[2]+1, pos[3]+1, pos[4]+1, pos[5]+1];
					foreach (i; 0..3)
					{
						auto yy = cast(int)pos[i*2];
						auto xx = cast(int)pos[i*2+1];
						s[yy][xx] = s[yy][xx] == '0' ? '1' : '0';
					}
				}
				void fin()
				{
					long[] tmp;
					foreach (i; 0..2)
					{
						foreach (j; 0..2)
						{
							if (s[y+i][x+j] == '1')
								tmp ~= [y+i, x+j];
						}
					}
					draw(tmp);
				}
				void fin2()
				{
					long[] tmp;
					bool mode;
					foreach (i; 0..2)
					{
						foreach (j; 0..2)
						{
							if (s[y+i][x+j] == '1')
							{
								if (mode) continue;
								mode = true;
								tmp ~= [y+i, x+j];
							}
							else
								tmp ~= [y+i, x+j];
						}
					}
					draw(tmp);
					fin();
				}
				if (y == n-2 && x == m-2)
				{
					long cnt;
					foreach (i; 0..2)
					{
						foreach (j; 0..2)
						{
							if (s[y+i][x+j] == '1')
								++cnt;
						}
					}
					if (cnt == 4)
					{
						draw([y, x, y, x+1, y+1, x+1]);
						draw([y, x, y+1, x, y+1, x+1]);
						draw([y+1, x, y, x+1, y+1, x+1]);
						fin();
					}
					else if (cnt == 3)
					{
						fin();
					}
					else if (cnt == 2)
					{
						fin2();
					}
					else if (cnt == 1)
					{
						long[] tmp;
						int mode;
						foreach (i; 0..2)
						{
							foreach (j; 0..2)
							{
								if (s[y+i][x+j] == '0')
								{
									if (mode == 2) continue;
									++mode;
									tmp ~= [y+i, x+j];
								}
								else
									tmp ~= [y+i, x+j];
							}
						}
						draw(tmp);
						fin2();
					}
				}
				else if (y == n-2)
				{
					if (s[y][x] == '1' && s[y+1][x] == '1')
						draw([y, x, y+1, x, y, x+1]);
					else if (s[y][x] == '1')
						draw([y, x, y, x+1, y+1, x+1]);
					else if (s[y+1][x] == '1')
						draw([y+1, x, y, x+1, y+1, x+1]);
				}
				else if (x == m-2)
				{
					if (s[y][x] == '1' && s[y][x+1] == '1')
						draw([y, x, y, x+1, y+1, x+1]);
					else if (s[y][x] == '1')
						draw([y, x, y+1, x, y+1, x+1]);
					else if (s[y][x+1] == '1')
						draw([y+1, x, y, x+1, y+1, x+1]);
				}
				else
				{
					if (s[y][x] == '0') continue;
					draw([y, x, y, x+1, y+1, x+1]);
				}
			}
		}
		debug
		{
			foreach (i; 0..n)
				writeln(s[i]);
		}
	}

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

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

void main () {
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests) {
		int rows, cols;
		readf !(" %s %s ") (rows, cols);
		uint [] [] board;
		foreach (row; 0..rows)
			board ~= readln.strip.map !(q{a - '0'}).array;

		int [] [] answer;

		void go (int row, int col, int row1, int col1, int row2, int col2) {
			answer ~= [row, col, row1, col1, row2, col2];
			board[row][col] ^= 1;
			board[row1][col1] ^= 1;
			board[row2][col2] ^= 1;
		}

		foreach_reverse (row; 0..rows)
			foreach_reverse (col; 0..cols)
				if ((row == rows - 1 && rows % 2 == 1) || (col == cols - 1 && cols % 2 == 1))
					if (board[row][col]) {
						auto row1 = max (row - 1, 0);
						auto col1 = max (col - 1, 0);
						auto row2 = row1 + 1;
						auto col2 = col1;
						if (row2 == row && col2 == col) {
							row2 = row1;
							col2 = col1 + 1;
						}
						go (row, col, row1, col1, row2, col2);
					}

		void doSquare (int row, int col) {
			while (true) {
				auto total = board[row + 0][col + 0] +
				    board[row + 1][col + 0] +
				    board[row + 0][col + 1] +
				    board[row + 1][col + 1];
				if (total == 0)
					break;
				int [] seqRow, seqCol;
				foreach_reverse (value; 0..2)
					foreach (rowT; row..row + 2)
						foreach (colT; col..col + 2)
							if (board[rowT][colT] == value) {
								seqRow ~= rowT;
								seqCol ~= colT;
							}
				if (total == 2) {
					seqRow.popFront ();
					seqCol.popFront ();
				}
				go (seqRow[0], seqCol[0],
				    seqRow[1], seqCol[1],
				    seqRow[2], seqCol[2]);
			}
		}

		for (int row = 0; row + 1 < rows; row += 2)
			for (int col = 0; col + 1 < cols; col += 2)
				doSquare (row, col);

		writeln (answer.length);
		foreach (ref line; answer)
			writefln !("%(%s %)") (line.map !(q{a + 1}));
	}
}


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

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		bool have0 = false;
		bool have1 = false;
		foreach (ref c; s)
		{
			if (c == '0')
			{
				writeln (have0 ? 1 : 3, " ", 1);
				have0 ^= true;
			}
			else
			{
				writeln (1, " ", have1 ? 1 : 3);
				have1 ^= true;
			}
		}
	}
}

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

void main() {
    auto S = readln.chomp;
    auto N = S.length.to!int;

    int v_count = 0;
    int h_count = 0;

    foreach (s; S) {
        if (s == '1') {
            writeln(1, " ", h_count + 1);
            h_count += 2;
            if (h_count == 4) h_count = 0;
        } else {
            writeln(3, " ", v_count + 1);
            v_count += 1;
            if (v_count == 4) v_count = 0;
        }
    }
}


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

void main() {
	long tt = readln().chomp.to!long;
	foreach (t; 0 .. tt) {
		readln();
		long[] piles = readln().chomp.split(" ").map!(to!long).array;
		bool winner = true;
		bool didBreak = false;
		foreach (i; piles) {
			if (i == 1) {
				winner = !winner;
			}
			else {
				didBreak = true;
				break;
			}
		}
		if (!didBreak) {
			winner = !winner;
		}
		writeln(winner ? "First" : "Second");
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto p = readln.splitter.countUntil !(q{a != "1"});
		writeln (((p < 0 ? ~n : p) & 1) ? "Second" : "First");
	}
}


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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		foreach (k; 2..4)
		{
			foreach (i; 0..n)
			{
				while (a[i] % k == 0)
				{
					a[i] /= k;
				}
			}
		}
		writeln (a.all !(x => x == a[0]) ? "Yes" : "No");
	}
}

import std.stdio, std.string;

void solve(int[] a)
{
    auto n = cast(int)a.length;
    foreach (i; 0 .. n)
    {
        while (a[i] % 2 == 0)
        {
            a[i] >>= 1;
        }
        while (a[i] % 3 == 0)
        {
            a[i] /= 3;
        }
        if (i > 0 && a[i] != a[i - 1])
        {
            writeln("No");
            return;
        }
    }
    writeln("Yes");
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a);
    }
    return 0;
}


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

immutable long MOD = 10^^9+7;

void main() {
    auto s = readln.split.map!(to!int);
    auto A = s[0];
    auto B = s[1];
    auto C = s[2];
    auto D = s[3];
    --A, --B, --C, --D;

    const int H = 50;
    const int W = 50;
    auto ans = new int[][](H, W);

    foreach (i; 0..H)
        foreach (j; 0..W) {
            if (i < H/2 && j < W/2)
                ans[i][j] = 1;
            else if (i < H/2 && j >= W/2)
                ans[i][j] = 2;
            else if (i >= H/2 && j < W/2)
                ans[i][j] = 3;
            else
                ans[i][j] = 4;
        }

    for (int i = 0; i < H/2-1; i += 2) {
        for (int j = 0; j < W/2-1; ++j) {
            if (j % 3 == 0 && B > 0)
                ans[i][j] = 2, --B;
            else if (j % 3 == 1 && C > 0)
                ans[i][j] = 3, --C;
            else if (j % 3 == 2 && D > 0)
                ans[i][j] = 4, --D;
        }
    }

    for (int i = 0; i < H/2-1; i += 2) {
        for (int j = W/2+1; j < W; ++j) {
            if (j % 3 == 0 && A > 0)
                ans[i][j] = 1, --A;
            else if (j % 3 == 1 && C > 0)
                ans[i][j] = 3, --C;
            else if (j % 3 == 2 && D > 0)
                ans[i][j] = 4, --D;
        }
    }

    for (int i = H/2+1; i < H; i += 2) {
        for (int j = 0; j < W/2-1; ++j) {
            if (j % 3 == 0 && A > 0)
                ans[i][j] = 1, --A;
            else if (j % 3 == 1 && B > 0)
                ans[i][j] = 2, --B;
            else if (j % 3 == 2 && D > 0)
                ans[i][j] = 4, --D;
        }
    }

    for (int i = H/2+1; i < H; i += 2) {
        for (int j = W/2+1; j < W; ++j) {
            if (j % 3 == 0 && A > 0)
                ans[i][j] = 1, --A;
            else if (j % 3 == 1 && B > 0)
                ans[i][j] = 2, --B;
            else if (j % 3 == 2 && C > 0)
                ans[i][j] = 3, --C;
        }
    }

    writeln(H, " ", W);
    ans.map!(a => a.map!(b => ('A' + b - 1).to!char)).each!writeln;
}

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

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

    char[50][44] ans;
    debug { ans.length.writeln; }

    foreach (ref e; v) e -= 1;
    debug { v.writeln; }

    auto clrs = ['a', 'b', 'c', 'd'];

    foreach (i; 0..44) ans[i].fill(clrs[i / 11]);

    void go(char c, int x, int st) {
        if (x == 0) return;

        outer: foreach (ref r; ans[st..st+11].dropOne.dropBackOne.stride(2)) {
            foreach (ref p; r.length.iota.dropOne.dropBackOne.stride(2)) {
                r[p] = c;
                --x;
                if (x == 0) {
                    debug { r.writeln; }
                    break outer;
                }
            }
        }
    }

    foreach (i; (44).iota.stride(11)) {
        auto nxt = ((i/11) + 1) % 4;
        debug { i.writeln; nxt.writeln; clrs[nxt].writeln; }
        go(clrs[nxt], v[nxt], i);
    }

    writeln(ans.length, ' ', ans[0].length);
    ans.each!writeln;
}