import std.stdio, std.array, std.range, std.string, std.typecons;
import std.algorithm, std.container, std.math, std.numeric, std.random;
void scan(T...)(ref T args) { foreach (ref arg; args) readf(" %s", &arg); }
void minify(T)(ref T a, in T b) { if (a > b) a = b; }
void maxify(T)(ref T a, in T b) { if (a < b) a = b; }
void ewriteln(T...)(T args) { stderr.writeln("\033[35m", args, "\033[0m"); }
int ilen(T)(const ref T a) { return cast(int)(a.length); }

// ok i'm half asleep this was not a good idea (no pun intended)

struct Edge {
	int v1, v2;
	bool w, u;
}

enum int N = 100008;
enum long OO = 10L ^^ 18;
int n, m;
size_t[][N] graph;
long[N] dist;
size_t[N] parent;

int[N] heap;
size_t[N] invheap;
size_t heaplen;

void hpush(int v) {
	heaplen++;
	heap[heaplen] = v;
	invheap[v] = heaplen;
	percUpIx(heaplen);
}
void hswap(size_t i1, size_t i2) {
	swap(heap[i1], heap[i2]);
	swap(invheap[heap[i1]], invheap[heap[i2]]);
}
void percUpIx(size_t ix) {
	if (ix == 1) return;
	size_t parent = ix / 2;
	if (dist[heap[parent]] > dist[heap[ix]]) {
		hswap(parent, ix);
		percUpIx(parent);
	}
}
void percDownIx(size_t ix) {
	size_t tgt = ix;
	foreach (tix; tuple(2*ix, 2*ix + 1)) {
		if (tix <= heaplen && dist[heap[tix]] < dist[heap[tgt]]) {
			tgt = tix;
		}
	}
	if (tgt != ix) {
		hswap(tgt, ix);
		percDownIx(tgt);
	}
}
void percUp(int v) { percUpIx(invheap[v]); }
int hpop() {
	assert(heaplen > 0);
	int ret = heap[1];
	hswap(1, heaplen);
	heaplen--;
	percDownIx(1);
	return ret;
}

Edge[N] edges;

bool[N] vis;

void printDiff() {
	int v = n;
	while (v != 1) {
		auto ei = parent[v];
		edges[ei].u = true;
		Edge e = edges[ei];
		e.u = true;
		assert(v == e.v1 || v == e.v2);
		v = e.v1 ^ e.v2 ^ v;
	}
	int count = 0;
	foreach (ei; 0..m) {
		Edge e = edges[ei];
		if (e.w ^ e.u) {
			count++;
		}
	}
	writeln(count);
	foreach (ei; 0..m) {
		Edge e = edges[ei];
		if (e.w ^ e.u) {
			writeln(e.v1, " ", e.v2, " ", e.u ? 1 : 0);
		}
	}
}

void main() {
	scan(n, m);
	foreach (i; 0..m) {
		int a, b, w;
		scan(a, b, w);
		edges[i] = Edge(a, b, cast(bool)w, false);
		graph[a] ~= i;
		graph[b] ~= i;
	}
	foreach (v; 2..n+1) {
		dist[v] = OO;
	}
	hpush(1);
	while (true) {
		int nxt = hpop();
		vis[nxt] = true;
		if (nxt == n) {
			printDiff();
			return;
		}
		foreach (ei; graph[nxt]) {
			Edge e = edges[ei];
			assert(nxt == e.v1 || nxt == e.v2);
			int other = e.v1 ^ e.v2 ^ nxt;
			long newdist = dist[nxt] + N + (e.w ? -1 : 1);
			if (newdist < dist[other]) {
				parent[other] = ei;
				if (dist[other] == OO) {
					dist[other] = newdist;
					hpush(other);
				} else {
					dist[other] = newdist;
					percUp(other);
				}
			}
		}
	}
}

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, M;
int[] A, B, C;

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

		auto g = new int[][N];
		foreach (i; 0 .. M) {
			g[A[i]] ~= i;
			g[B[i]] ~= i;
		}

		int[] q;
		int[] d = new int[N];
		d[] = -1;
		int[] dp = new int[N];
		dp[] = int.min;
		int[] prv = new int[N];
		prv[] = -1;

		d[0] = 0;
		q ~= 0;
		dp[0] = 0;
		for (; !q.empty; ) {
			const u = q.front; q.popFront;
			foreach (i; g[u]) {
				const v = A[i] ^ B[i] ^ u;
				if (d[v] == -1) {
					d[v] = d[u] + 1;
					q ~= v;
				}
				if (d[v] == d[u] + 1) {
					if (dp[v] < dp[u] + C[i]) {
						dp[v] = dp[u] + C[i];
						prv[v] = i;
					}
				}
			}
		}
debug{
writeln("d = ",d);
writeln("dp = ",dp);
writeln("prv = ",prv);
}

		bool[] used = new bool[M];
		Pair!(int, Pair!(int, int))[] anss;
		for (int u = N - 1; u != 0; ) {
			const i = prv[u];
			used[i] = true;
			if (C[i] == 0) {
				anss ~= pair(1, pair(A[i], B[i]));
			}
			u = A[i] ^ B[i] ^ u;
		}
		foreach (i; 0 .. M) if (!used[i]) {
			if (C[i] == 1) {
				anss ~= pair(0, pair(A[i], B[i]));
			}
		}
		writeln(anss.length);
		foreach (ans; anss) {
			writeln(ans.y.x + 1, " ", ans.y.y + 1, " ", ans.x);
		}

	}
	} catch (EOFException) {}
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		int balance = 0;
		int lo = 0;
		long res = s.length;
		foreach (i, char c; s)
		{
			balance += (c == '+') ? +1 : -1;
			if (lo > balance)
			{
				lo = balance;
				res += i + 1;
			}
		}
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long last;
		long cnt;
		foreach (i; 0..s.length)
		{
			if (s[i] == '-')
				--cnt;
			else
				++cnt;

			if (cnt < last)
			{
				--last;
				ans[ti] += i+1;
			}
		}
		ans[ti] += s.length;
	}

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

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

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

	while (t--) {
		auto s = stdin.byLine.front;
		int n = s.length;

		int[] first_neg_i = repeat(-1, n + 3).array;

		int cur = 0;
		foreach (i, c; s) {
			if (c == '-') cur--;
			else cur++;

			if (cur < 0 && first_neg_i[-cur] == -1)
				first_neg_i[-cur] = i + 1;
		}

		long res = 0;
		int next = 1;
		while (true) {
			if (first_neg_i[next] != -1) {
				res += first_neg_i[next++];
			} else {
				res += n;
				break;
			}
		}

		res.writeln;
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long last;
		long cnt;
		foreach (i; 0..s.length)
		{
			if (s[i] == '-')
				--cnt;
			else
				++cnt;

			if (cnt < last)
			{
				--last;
				ans[ti] += i+1;
			}
		}
		ans[ti] += s.length;
	}

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


import std.stdio;
import std.algorithm : max;

void main()
{
    int n, m, k;
    readf("%d %d %d\n", &n, &m, &k);
    auto t = new int[n + 1];
    auto s = new long[n + 1];
    auto w = new long[n + 1];
    auto p = new long[n + 1];
    foreach(i; 1 .. n + 1)
    {
        readf(" %d", &t[i]);
        if (i)
            s[i] = s[i - 1] + t[i];
    }

    foreach(i; 0 .. k)
    {
        foreach(j; m .. n + 1)
            w[j] = max(w[j - 1], p[j - m] + s[j] - s[j - m]);
        p = w.dup;
    }
    writeln(w[n]);
}

import std.stdio;
import std.algorithm;

void main() {
  int n, m, k;
  readf(" %s %s %s", &n, &m, &k);
  auto a = new int [n];
  auto sum = new long [n];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
    sum[i] = a[i];
    if (i > 0) {
      sum[i] += sum[i-1];
    }
  }
  auto f = new long [][] (n, k);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - 1 >= 0) {
        f[i][j] = f[i - 1][j];
      }
      long q = sum[i] - (i - m >= 0 ? sum[i - m] : 0);
      f[i][j] = max(f[i][j], (i - m >= 0 && j - 1 >= 0 ? f[i - m][j - 1] : 0) + q);
      if (i - 1 >= 0) {
        f[i][j] = max(f[i - 1][j], f[i][j]);
      }
    }
  }
  writeln(f[n - 1][k - 1]);
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto A = readln.split.map!(to!long).array;;

    auto B = new long[](N-M+1);
    B[0] = 0;
    foreach (i; 0..M)
        B[0] += A[i];
    foreach (i; M..N) {
        B[i-M+1] = B[i-M] - A[i-M] + A[i];
    }

    auto dp = new long[][](N-M+1, K);
    foreach (i; 0..N-M+1)
        fill(dp[i], 0);
    dp[0][0] = B[0];

    foreach (i; 1..N-M+1) {
        dp[i][0] = B[i];
        foreach (j; 0..K) {
            dp[i][j] = max(dp[i][j], dp[i-1][j]);
            if (i >= M && j > 0) {
                dp[i][j] = max(dp[i][j], dp[i-M][j-1]+B[i]);
                dp[i][j] = max(dp[i][j], dp[i-M][j]);
            }
        }
    }

    //B.writeln;
    //dp.each!(writeln);
    dp[N-M][K-1].writeln;
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto K = s[2];
    auto A = readln.split.map!(to!long).array;;

    auto B = new long[](N-M+1);
    B[0] = 0;
    foreach (i; 0..M)
        B[0] += A[i];
    foreach (i; M..N) {
        B[i-M+1] = B[i-M] - A[i-M] + A[i];
    }

    auto dp = new long[][](N-M+1, K);
    foreach (i; 0..N-M+1)
        fill(dp[i], 0);
    dp[0][0] = B[0];

    foreach (i; 1..N-M+1) {
        dp[i][0] = max(dp[i-1][0], B[i]);
        foreach (j; 1..K) {
            if (i >= M)
                dp[i][j] = max(dp[i-1][j-1]+B[i], dp[i-1][j]);
        }
    }

    dp[N-M][K-1].writeln;
}

import std.stdio;
import std.algorithm : max;

void main()
{
    int n, m, k;
    readf("%d %d %d\n", &n, &m, &k);
    auto t = new int[n + 1];
    auto s = new long[n + 1];
    auto w = new long[n + 1];
    auto p = new long[n + 1];
    foreach(i; 1 .. n + 1)
    {
        readf(" %d", &t[i]);
        if (i)
            s[i] = s[i - 1] + t[i];
    }

    foreach(i; 0 .. k)
    {
        writeln(s);
        writeln(w);
        foreach(j; m .. n + 1)
            w[j] = max(w[j - 1], p[j - m] + s[j] - s[j - m]);
        p = w.dup;
    }
    writeln(w[n]);
}


// code by cutx64

module main;

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

void main() {

  int n; readf!"%s\n"(n);
  alias NodeType = Tuple!(int, "v", int, "x");
  NodeType[] a;
  foreach(i; 0 .. n) {
    int x, y; readf!"%s %s\n"(x, y);
    a ~= NodeType(x + y, x);
  }
  a.sort;
  RedBlackTree!(long, "a > b", true) hl = make!(RedBlackTree!(long, "a > b", true))();
  RedBlackTree!(long, "a < b", true) hr = make!(RedBlackTree!(long, "a < b", true))();
  foreach(i; 0 .. n + 1) {
    hl.insert(0);
    hr.insert(0);
  }
  int last;
  long tag, ans;
  foreach(i; 0 .. n) {
    int d = a[i].v - last;
    last = a[i].v;
    tag += d;
    int x = a[i].x;
    if (x <= hl.front) {
      ans += hl.front - x;
      hl.insert(x);
      hl.insert(x);
      hr.insert(hl.front - tag);
      hl.removeFront;
    }
    else if (x >= hr.front + tag) {
      ans += x - (hr.front + tag);
      hr.insert(x - tag);
      hr.insert(x - tag);
      hl.insert(hr.front + tag);
      hr.removeFront;
    }
    else {
      hl.insert(x);
      hr.insert(x - tag);
    }
  }
  ans.writeln;

} // main

// code by cutx64

module main;

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

void main() {

  int n; readf!"%s\n"(n);
  alias NodeType = Tuple!(int, "v", int, "x");
  NodeType[] a;
  foreach(i; 0 .. n) {
    int x, y; readf!"%s %s\n"(x, y);
    a ~= NodeType(x + y, x);
  }
  a.sort;
  BinaryHeap!(Array!long) hl;
  BinaryHeap!(Array!long, "a > b") hr;
  foreach(i; 0 .. n + 1) {
    hl.insert(0);
    hr.insert(0);
  }
  int last;
  long tag, ans;
  foreach(i; 0 .. n) {
    int d = a[i].v - last;
    last = a[i].v;
    tag += d;
    int x = a[i].x;
    if (x <= hl.front) {
      ans += hl.front - x;
      hl.insert(x);
      hl.insert(x);
      hr.insert(hl.front() - tag);
      hl.popFront;
    }
    else if (x >= hr.front + tag) {
      ans += x - (hr.front + tag);
      hr.insert(x - tag);
      hr.insert(x - tag);
      hl.insert(hr.front + tag);
      hr.popFront;
    }
    else {
      hl.insert(x);
      hr.insert(x - tag);
    }
  }
  ans.writeln;

} // main


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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998244353;
//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 a = RD;
		auto b = RD;
		if (a == b)
			ans[ti] = 0;
		else if (b > a)
			ans[ti] = (b - a) % 2 == 0 ? 2 : 1;
		else
			ans[ti] = (a - b) % 2 == 0 ? 1 : 2;
	}

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

import std.stdio : readln, writeln;

struct IO {
  import std.conv : to;
  import std.range : empty, front, popFront, split;

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

  int readInt() {
    return readToken.to!int;
  }

  string[] tokens;
}

int solve(int a, int b) {
  if (a == b) {
    return 0;
  }
  if (a < b) {
    return (b & 1) == (a & 1) ? 2 : 1;
  }
  return (b & 1) == (a & 1) ? 1 : 2;
}

void main() {
  IO io;
  int T = io.readInt;
  while (T--) {
    int a = io.readInt;
    int b = io.readInt;
    writeln(solve(a, b));
  }
}

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

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

void solve(){
    foreach(_; 0 .. rint){
        int a = rint, b = rint;
        if(a < b){
            if((b - a) % 2) 1.writeln;
            else 2.writeln;
        }
        else if(a == b) 0.writeln;
        else{
            if((a - b) % 2) 2.writeln;
            else 1.writeln;
        }
    }
}

import std.stdio;

int main()
{
	int t=0;
	readf(" %d", &t);
	for (int i=0; i<t; i++)
	{
		int a=0;
		int b=0;
		readf(" %d", &a);
		readf(" %d", &b);
		if (a==b)
		{
			writeln(0);
		}
		else
		{
			if (((b-a)%2==1 && b>a) || ((b-a)%2==0 && b<a))
			{
				writeln(1);
			}
			else
			{
				writeln(2);
			}
		}
	}
	return 0;
}


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

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

    immutable int MAX = 2 * 10 ^^ 5 + 1;

    auto starts = new int[][] (MAX);
    auto end = new int[] (n+1);

    foreach (i; 0 .. n) {
        int le, r;
        readf("%s %s", &le, &r);
        readln;

        starts[le] ~= i+1;
        end[i+1] = r;
    }

    alias seg = Tuple!(int, int);
    auto rbt = make!(RedBlackTree!seg);

    int[] ans;
    foreach (p; 0 .. MAX) {
        foreach (e; starts[p]) {
            rbt.insert(seg(end[e], e));
        }

        while (rbt.length > k) {
            ans ~= rbt.back[1];
            rbt.removeBack();
        }

        while (!rbt.empty() && rbt.front[0] == p) {
            rbt.removeFront();
        }
    }

    ans.length.writeln;
    ans.map!(to!string).join(' ').writeln;
}

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

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

    immutable int MAX = 200 + 1;

    auto starts = new int[][] (MAX);
    auto ends = new int[] (n+1);

    foreach (i; 0 .. n) {
        int le, r;
        readf("%s %s", &le, &r);
        readln;

        starts[le] ~= i+1;
        ends[i+1] = r;
    }

    auto rbt = make!(RedBlackTree!(Tuple!(int, int)));

    int cur = 0;
    int[] ans;
    foreach (p; 0 .. MAX) {
        foreach (e; starts[p]) {
            ++cur;
            rbt.insert(tuple(ends[e], e));
        }

        while (cur > k) {
            ans ~= rbt.back[1];
            rbt.removeBack();
            --cur;
        }

        while (!rbt.empty() && rbt.front[0] == p) {
            rbt.removeFront();
            --cur;
        }
    }

    ans.length.writeln;
    ans.map!(to!string).join(' ').writeln;
}

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

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

    immutable int MAX = 2 * 10 ^^ 5 + 1;

    auto starts = new int[][] (MAX);
    auto ends = new int[] (n+1);

    foreach (i; 0 .. n) {
        int le, r;
        readf("%s %s", &le, &r);
        readln;

        starts[le] ~= i+1;
        ends[i+1] = r;
    }

    auto rbt = make!(RedBlackTree!(Tuple!(int, int)));

    int[] ans;
    foreach (p; 0 .. MAX) {
        foreach (e; starts[p]) {
            rbt.insert(tuple(ends[e], e));
        }

        while (rbt.length > k) {
            ans ~= rbt.back[1];
            rbt.removeBack();
        }

        while (!rbt.empty() && rbt.front[0] == p) {
            rbt.removeFront();
        }
    }

    ans.length.writeln;
    ans.map!(to!string).join(' ').writeln;
}

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

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

    immutable int MAX = 2 * 10 ^^ 5 + 1;

    auto starts = new int[][] (MAX);
    auto end = new int[] (n+1);

    foreach (i; 0 .. n) {
        int le, r;
        readf("%s %s", &le, &r);
        readln;

        starts[le] ~= i+1;
        end[i+1] = r;
    }

    auto rbt = make!(RedBlackTree!(Tuple!(int, int)));

    int[] ans;
    foreach (p; 0 .. MAX) {
        foreach (e; starts[p]) {
            rbt.insert(tuple(end[e], e));
        }

        while (rbt.length > k) {
            ans ~= rbt.back[1];
            rbt.removeBack();
        }

        while (!rbt.empty() && rbt.front[0] == p) {
            rbt.removeFront();
        }
    }

    ans.length.writeln;
    ans.map!(to!string).join(' ').writeln;
}

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

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

    immutable int MAX = 2 * 10 ^^ 5 + 1;

    auto starts = new int[][] (MAX);
    auto ends = new int[] (n+1);

    foreach (i; 0 .. n) {
        int le, r;
        readf("%s %s", &le, &r);
        readln;

        starts[le] ~= i+1;
        ends[i+1] = r;
    }

    auto rbt = make!(RedBlackTree!(Tuple!(int, int)));

    int cur = 0;
    int[] ans;
    foreach (p; 0 .. MAX) {
        foreach (e; starts[p]) {
            ++cur;
            rbt.insert(tuple(ends[e], e));
        }

        while (cur > k) {
            ans ~= rbt.back[1];
            rbt.removeBack();
            --cur;
        }

        while (!rbt.empty() && rbt.front[0] == p) {
            rbt.removeFront();
            --cur;
        }
    }

    ans.length.writeln;
    ans.map!(to!string).join(' ').writeln;
}


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

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

		auto c = new int [n];

		void recur (int v, int cur)
		{
			debug {writeln (v, " ", cur);}
			if ((c[v] & cur) == cur)
			{
				return;
			}
			c[v] |= cur;
			foreach (u; a[v])
			{
				recur (u, cur ^ 3);
			}
		}

		foreach (i; 0..n)
		{
			if (c[i] == 0)
			{
				recur (i, 1);
			}
		}

		if (n.iota.any !(x => c[x] == 3))
		{
			writeln (-1);
		}
		else
		{
			foreach (i; 1..3)
			{
				auto ans =
				    n.iota.filter !(x => c[x] == i).array;
				writefln ("%s\n%(%s %)",
				    ans.length, ans.map !(x => x + 1));
			}
		}
	}
}

import std.stdio;

immutable int MAXN = cast(int)5e5;

int N, M;
int[][MAXN] G;
int[MAXN] d;
int[] A, B;
bool correct = 1;

void dfs(int v, int col) {
	d[v] = col;
	foreach(u; G[v]) {
		if (!d[u]) dfs(u, -col);
		else if (d[u] == d[v]) correct = 0;
	}
}

void print(int[] arr) {
	printf("%d\n", arr.length);
	foreach(i; arr) printf("%d ", i);
	printf("\n");
}

void main() {
	readf("%d %d\n", &N, &M);
	for (int i = 0; i < M; ++i) {
		int u, v; readf("%d %d\n", &u, &v);
		G[u] ~= v; G[v] ~= u;
	}
	for (int i = 1; i <= N; ++i) if (!d[i]) dfs(i, 1);
	if (!correct) {
		writef("-1\n"); return;
	}
	for (int i = 1; i <= N; ++i) (d[i] == 1 ? A : B) ~= i;
	print(A); print(B);
}

import std.stdio;
import std.container: DList;

int opposite_color(int c) {
    if (c == 1) return 2;
    return 1;
}

void main() {
    int n, m;
    readf("%d %d\n", &n, &m);
    int[][] graph;
    int[] colors;
    graph.length = n;
    colors.length = n;
    int first_colors = 0;
    int second_colors = 0;

    for(int i = 0; i < m; ++i) {
        int u, v;
        readf("%d %d\n", &u, &v);
        --u;
        --v;
        graph[u] ~= v;
        graph[v] ~= u;
    }
    bool possible = true;
outerLoop:
    foreach (int i, int color; colors) {
        if (color == 0  && graph[i].length != 0) {
            auto queue = DList!int();
            queue.insertBack(i);
            while (!queue.empty()) {
                int u = queue.front;
                queue.removeFront(1);
                foreach (int j, int v; graph[u]) {
                    if (colors[v] != 0 && colors[v] == colors[u]) {
                        possible = false;
                        break outerLoop;
                    } else if (colors[v] == 0) {
                        int opColor = opposite_color(colors[u]);
                        colors[v] = opColor;
                        if (opColor == 1) first_colors++;
                        else second_colors++;
                        queue.insertBack(v);
                    }
                }
            }
        }
    }

    if (possible) {
        writeln(first_colors);
        foreach(int i, int c; colors) {
            if(c == 1) {
                write(i+1, " ");
            }
        }
        writeln();
        writeln(second_colors);
        foreach(int i, int c; colors) {
            if(c == 2) {
                write(i+1, " ");
            }
        }
        writeln();
    } else {
        writeln(-1);
    }
}


import std.stdio : File, writeln, writefln;
import std.array;
import std.range;
import std.algorithm : min;

int n;
int[] arr;

void swap(T)(ref T a, ref T b){
	T tmp = a;
	a = b;
	b = tmp;
}

File ifp;
File ofp;

void main(){
	ifp = File("input.txt", "r");
	ofp = File("output.txt", "w");

	char[] l = ['B', 'G'];

	int n = next!int();
	int m = next!int();

	auto str = cycle(l[]).take(min(n, m)*2).array;

	if(n>m){
		ofp.write = str;
		ofp.write = repeat('B').take(n-m).array;
	}else{
		ofp.write = repeat('G').take(m-n).array;
		ofp.write = str;
	}

	ofp.writeln;
}

import std.stdio : readln, chomp;
import std.conv : to;
import std.string : split;
shared string[] input;
shared string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

void next(T)(ref T v){
	v = next!T();
}

bool hasNext(){
	if(input.length > 0){
		return true;
	}

//	string str = readln;
	string str = ifp.readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return true;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

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

string b="B",g="G";
void main(){
    stdin=File("input.txt","r");
    stdout=File("output.txt","w");
    int n,m;
    readf!"%d %d"(n,m);
    readln;
    if(m<n){
        swap(b,g);
        swap(n,m);
    }
    writeln((g~b).replicate(n)~g.replicate(m-n));
}

module cf_253A;

import std.stdio, std.algorithm;

void main() {
    File fin = File("input.txt", "r");
    File fout = File("output.txt", "w");
    int n, m;

    fin.readf("%d %d", &n, &m);

    string pair = (n > m)? "BG": "GB";
    for (int i = min(n, m); i > 0; --i) {
        fout.writef("%s", pair);
    }
    for (int i = n - min(n, m); i > 0; --i) {
        fout.writef("B");
    }
    for (int i = m - min(n, m); i > 0; --i) {
        fout.writef("G");
    }
    fout.writeln();
}

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

string b="B",g="G";
void main(){
    int n,m;
    scanf("%d %d",&n,&m);
    if(m<n){
        swap(b,g);
        swap(n,m);
    }
    writeln((g~b).replicate(n)~g.replicate(m-n));
}

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

string b="B",g="G";
void main(){
    stdin=File("input.txt","r");
    stdout=File("output.txt","w");
    int n,m;
    scanf("%d %d",&n,&m);
    if(m<n){
        swap(b,g);
        swap(n,m);
    }
    writeln((g~b).replicate(n)~g.replicate(m-n));
}

module cf_253A;

import std.stdio, std.algorithm;

void main() {
    int n, m;

    readf("%d %d", &n, &m);

    string pair = (n > m)? "BG": "GB";
    for (int i = min(n, m); i > 0; --i) {
        writef("%s", pair);
    }
    for (int i = n - min(n, m); i > 0; --i) {
        writef("B");
    }
    for (int i = m - min(n, m); i > 0; --i) {
        writef("G");
    }
    writeln();
}

module cf_253A;

import std.stdio, std.algorithm;

void main() {
    int n, m;

    readf("%d %d", &n, &m);

    string pair = (n > m)? "BG": "GB";
    for (int i = min(n, m); i > 0; --i) {
        writef("%s", pair);
    }
    for (int i = n - min(n, m); i > 0; --i) {
        writef("B");
    }
    for (int i = m - min(n, m); i > 0; --i) {
        writef("G");
    }
}


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

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

void solve(){
	int n = rint;
	S[] as = rlong(n).map!(v => S(v, 1)).array;

	auto sta = new Stack!S;
	foreach(a; as){
		while(sta.length > 0 && sta.peek.gt(a)){
			S b = sta.pop;
			a = S(b.sum + a.sum, b.count + a.count);
		}
		sta.push(a);
	}

	S[] ans = sta.array;
	foreach(an; ans){
		foreach(i; 0 .. an.count) writefln("%10.10f", an.sum.to!real / an.count.to!real);
	}

}

struct S{
	long sum;
	int count;
	bool lt(S b){ return sum * b.count < b.sum * count; }
	bool gt(S b){ return sum * b.count > b.sum * count; }
	bool le(S b){ return !gt(b); }
	bool ge(S b){ return !lt(b); }
}

// ----- スタック -----
class Stack(T){
	private T[] xs;
	private uint j; // j : 次に書き込む位置
	this(){	}
	this(T[] xs){ this.xs = xs; j = xs.length.to!uint; }
	uint length(){ return j; }
	bool isEmpty(){ return j == 0; }
	void push(T x){
		while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1;
		xs[j ++] = x;
	}
	T pop(){ assert(j > 0); return xs[-- j]; }
	T peek(){ assert(j > 0); return xs[j - 1]; }
	T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; }
	static Stack!T opCall(){ return new Stack!T; }
	static Stack!T opCall(T[] xs){ return new Stack!T(xs); }
	T[] array(){ return xs[0 .. j]; }
	override string toString(){ return array.to!string; }
}

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

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

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

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

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

      int len;
      auto numers = new long[N];
      auto denoms = new long[N];
      foreach_reverse (i; 0 .. N) {
        long p = A[i], q = 1;
        for (; len >= 1 && numers[len - 1] * q <= p * denoms[len - 1]; ) {
          p += numers[len - 1];
          q += denoms[len - 1];
          --len;
        }
        numers[len] = p;
        denoms[len] = q;
        ++len;
      }
      foreach_reverse (j; 0 .. len) {
        const ans = cast(real)(numers[j]) / denoms[j];
        foreach (_; 0 .. denoms[j]) {
          writefln("%.10f", ans);
        }
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto s = [0L];
		s.reserve (n + 1);
		foreach (ref c; a)
		{
			s ~= s.back + c;
		}

		auto p = [0, 1];
		foreach (i; 2..n + 1)
		{
			while (p.length > 1 &&
			    (s[i] - s[p[$ - 1]]) * 1L * (i - p[$ - 2]) <=
			    (s[i] - s[p[$ - 2]]) * 1L * (i - p[$ - 1]))
			{
				p.popBack ();
				p.assumeSafeAppend ();
			}
			p ~= i;
		}

		auto answer = new real [n];
		foreach (j; 1..p.length)
		{
			real cur = s[p[j]] - s[p[j - 1]];
			cur /= p[j] - p[j - 1];
			foreach (i; p[j - 1]..p[j])
			{
				answer[i] = cur;
			}
		}
		foreach (const ref c; answer)
		{
			writefln ("%.10f", c);
		}
	}
}


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

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

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

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

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

        sort(P);
        reverse(P);

        bool check(int l) {
          auto zs = new long[l];
          foreach (i; 0 .. l) {
            if ((i + 1) % A == 0) {
              zs[i] += X;
            }
            if ((i + 1) % B == 0) {
              zs[i] += Y;
            }
          }
          sort(zs);
          reverse(zs);
          long sum;
          foreach (i; 0 .. l) {
            sum += P[i] / 100 * zs[i];
          }
          return (sum >= K);
        }

        int lo = -1, hi = N + 1;
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          check(mid) ? (hi = mid) : (lo = mid);
        }
        writeln((hi <= N) ? hi : -1);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	int tests;
	readf (" %s", &tests);
	foreach (test; 0..tests)
	{
		int n;
		readf (" %s", &n);
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		sort (p);
		reverse (p);
		p[] /= 100;
		int x, a;
		readf (" %s %s", &x, &a);
		int y, b;
		readf (" %s %s", &y, &b);
		long k;
		readf (" %s", &k);

		int solve ()
		{
			int lo = 1;
			int hi = n + 1;
			while (lo < hi)
			{
				int me = (lo + hi) / 2;
				auto q = p;
				long total = 0;
				for (int i = 1; i <= me; i++)
				{
					if (i % a == 0 && i % b == 0)
					{
						total += q.front * (x + y);
						q.popFront ();
					}
				}
				for (int i = 1; i <= me; i++)
				{
					if (i % a == 0 && i % b != 0)
					{
						total += q.front * (x + 0);
						q.popFront ();
					}
				}
				for (int i = 1; i <= me; i++)
				{
					if (i % a != 0 && i % b == 0)
					{
						total += q.front * (0 + y);
						q.popFront ();
					}
				}
				if (total >= k)
				{
					hi = me;
				}
				else
				{
					lo = me + 1;
				}
			}
			return lo;
		}

		auto res = solve ();
		swap (x, y);
		swap (a, b);
		res = min (res, solve ());
		if (res == n + 1)
		{
			res = -1;
		}
		writeln (res);
	}
}


import std.container, std.range, std.stdio, std.typecons;

alias Piece = Tuple !(int, q{a}, int, q{b}, int, q{l}, int, q{u});

void main ()
{
	int n, q;
	while (readf (" %s %s", &n, &q) > 0)
	{
		auto s = redBlackTree !(Piece) ();
		s.insert (Piece (1, n, 0, 0));
		foreach (k; 0..q)
		{
			int x, y;
			char t;
			readf (" %s %s %s", &x, &y, &t);
			auto cur = Piece (x, n + 1, 0, 0);
			auto r = s.lowerBound (cur);
			auto prev = r.empty ? Piece.init : r.back;
			int c = x;
			with (prev)
			{
				if (c < a || b < c)
				{
					writeln (0);
				}
				else if (t == 'L')
				{
					s.removeKey (prev);
					writeln (x - l);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, y));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    l, u));
					}
				}
				else if (t == 'U')
				{
					s.removeKey (prev);
					writeln (y - u);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, u));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    x, u));
					}
				}
				else
				{
					assert (false);
				}
			}
		}
	}
}

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

alias Piece = Tuple !(int, q{a}, int, q{b}, int, q{l}, int, q{u});

void main ()
{
	int n;
	int q;
	while (readf (" %s %s", &n, &q) > 0)
	{
		auto s = redBlackTree !(Piece) ();
		s.insert (Piece (1, n, 0, 0));
		foreach (k; 0..q)
		{
			int x;
			int y;
			char t;
			readf (" %s %s %s", &x, &y, &t);
			auto cur = Piece (x, n + 1, 0, 0);
			auto r = s.lowerBound (cur);
			auto prev = r.empty ? Piece.init : r.back;
			int c = x;
			with (prev)
			{
				if (c < a || b < c)
				{
					writeln (0);
				}
				else if (t == 'L')
				{
					s.removeKey (prev);
					writeln (x - l);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, y));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    l, u));
					}
				}
				else if (t == 'U')
				{
					s.removeKey (prev);
					writeln (y - u);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, u));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    x, u));
					}
				}
				else
				{
					assert (false);
				}
			}
			debug {writeln (s[].map !(text).join ("\n"));}
		}
	}
}


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 main() {
    int N, M; readf("%d %d\n", &N, &M);
    auto A = readarray!int;
    long ans = 0;
    for (long i = 0; i <= N - 1; i++) {
        ans += (i + N - 1) * (N - i) / 2L - i * (N - i) + (N - i);
    }

    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            long l = i + 1;
            long r = N - i - 1;
            ans -= l * r;
        }
    }

    foreach (q; 0 .. M) {
        int i, x; readf("%d %d\n", &i, &x);
        i--;
        if (i - 1 >= 0) {
            if (A[i - 1] != A[i] && A[i - 1] == x) {
                long lc = i;
                long rc = N - i;
                ans -= lc * rc;
            }
            if (A[i - 1] == A[i] && A[i - 1] != x) {
                long lc = i;
                long rc = N - i;
                ans += lc * rc;
            }
        }
        if (i + 1 < N) {
            if (A[i + 1] != A[i] && A[i + 1] == x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans -= lc * rc;
            }
            if (A[i + 1] == A[i] && A[i + 1] != x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans += lc * rc;
            }
        }
        A[i] = x;
        writeln(ans);
    }
}

import std.stdio;
import std.typecons;
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 N, M; scanf("%d %d\n", &N, &M);
    auto A = readln.chomp.split(" ").map!(to!long).array;

    long f(long N) {
        long x = N * (N + 1) / 2;
        long y = N * (N + 1) * (2 * N + 1) / 6;
        return N * x + x - y;
    }

    long T = f(N);
    long D = 0;
    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            D += cast(long)(i + 1) * cast(long)(N - (i + 1));
        }
    }
    long Y = T - D; // initial

    long prod(long i) {
        return i * (cast(long)N - i);
    }
    long diff(long x, long y) {
        if (x == y) return 0L;
        return 1L;
    }

    for (int q = 0; q < M; q++) {
        long d = 0;
        int I, X; scanf("%d %d\n", &I, &X);

        if (I - 2 >= 0) {
            d += (diff(A[I-2], X) - diff(A[I-2], A[I-1])) * prod(I-1);
        }
        if (I < N) {
            d += (diff(X, A[I]) - diff(A[I-1], A[I])) * prod(I);
        }

        Y += d;
        writeln(Y);

        A[I - 1] = X;
    }
}

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 main() {
    int N, M; readf("%d %d\n", &N, &M);
    auto A = readarray!int;
    long ans = 0;
    for (long i = 0; i <= N - 1; i++) {
        ans += (i + N - 1) * (N - i) / 2L - i * (N - i) + (N - i);
    }

    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            long l = i + 1;
            long r = N - i - 1;
            ans -= l * r;
        }
    }

    foreach (q; 0 .. M) {
        int i, x; readf("%d %d\n", &i, &x);
        i--;
        if (A[i] == x) continue;
        if (i - 1 >= 0) {
            if (A[i - 1] != A[i] && A[i - 1] == x) {
                long lc = i;
                long rc = N - i;
                ans -= lc * rc;
            }
            if (A[i - 1] == A[i] && A[i - 1] != x) {
                long lc = i;
                long rc = N - i;
                ans += lc * rc;
            }
        }
        if (i + 1 < N) {
            if (A[i + 1] != A[i] && A[i + 1] == x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans -= lc * rc;
            }
            if (A[i + 1] == A[i] && A[i + 1] != x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans += lc * rc;
            }
        }
        A[i] = x;
        writeln(ans);
    }
}

import std.stdio;
import std.typecons;
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 N, M; scanf("%d %d\n", &N, &M);
    auto A = readln.chomp.split(" ").map!(to!long).array;

    long f(long N) {
        long x = N * (N + 1) / 2;
        long y = N * (N + 1) * (2 * N + 1) / 6;
        return N * x + x - y;
    }

    long T = f(N);
    long D = 0;
    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            D += cast(long)(i + 1) * cast(long)(N - (i + 1));
        }
    }
    long Y = T - D; // initial

    long prod(long i) {
        return i * (N - i);
    }
    long diff(long x, long y) {
        if (x == y) return 0;
        return 1;
    }

    for (int q = 0; q < M; q++) {
        long d = 0;
        int I, X; scanf("%d %d\n", &I, &X);
        if (A[I - 1] == X) continue; // nothing changed

        if (I - 2 >= 0) {
            d += (diff(A[I-2], X) - diff(A[I-2], A[I-1])) * prod(I-1);
        }
        if (I < N) {
            d += (diff(X, A[I]) - diff(A[I-1], A[I])) * prod(I);
        }

        Y += d;
        writeln(Y);

        A[I - 1] = X;
    }
}

import std.stdio;
import std.typecons;
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 N, M; scanf("%d %d\n", &N, &M);
    auto A = readln.chomp.split(" ").map!(to!long).array;

    long f(long N) {
        long x = N * (N + 1) / 2;
        long y = N * (N + 1) * (2 * N + 1) / 6;
        return N * x + x - y;
    }

    long T = f(N);
    long D = 0;
    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            D += (i + 1) * (N - (i + 1));
        }
    }
    long Y = T - D; // initial

    long prod(long i) {
        return i * (N - i);
    }
    long diff(long x, long y) {
        if (x == y) return 0;
        return 1;
    }

    for (int q = 0; q < M; q++) {
        long d = 0;
        int I, X; scanf("%d %d\n", &I, &X);
        if (A[I - 1] == X) continue; // nothing changed

        if (I - 2 >= 0) {
            d += (diff(A[I-2], X) - diff(A[I-2], A[I-1])) * prod(I-1);
        }
        if (I < N) {
            d += (diff(X, A[I]) - diff(A[I-1], A[I])) * prod(I);
        }

        Y += d;
        writeln(Y);

        A[I - 1] = X;
    }
}


import std.stdio;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    auto n = read!int;
    bool a[100_001];
    a[] = false;
    int curr = n;

    for (int i = 0; i < n; i++)
    {
        auto q = read!int;
        a[q] = true;
        if (q == curr)
        {
            while (a[curr])
            {
                write(curr, " ");
                curr--;
            }
        }
        writeln;
    }

    return 0;
}

import std.stdio;
import std.container : BinaryHeap;

int main(string[] args)
{
	int n; readf(" %d", n);

	int[] h = new int[n];
	auto bh = BinaryHeap!(int[])(h, 0);

	int cur_expected = n;

	foreach (i; 0..n){
		int num; readf(" %d", num);
		bh.insert(num);

		while(!bh.empty() && bh.front() == cur_expected){
			writef("%d ", bh.front());
			bh.popFront();
			cur_expected--;
		}

		writeln();
	}

	return 0;
}


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

struct node {
    int seqlen;
    int unleft;
    int unright;
    this(int sl, int ul, int ur) {
        seqlen = sl;
        unleft = ul;
        unright = ur;
    }
}

__gshared {
    node[] segtree;
    string parens;
}

node SegTreeConstruct(int sb, int se, int si) {
    if (sb == se) {
        if (parens[sb] == '(') segtree[si] = node(0, 1, 0);
        if (parens[sb] == ')') segtree[si] = node(0, 0, 1);
        return segtree[si];
    }

    int mid = (sb + se) / 2;
    node nl = SegTreeConstruct(sb, mid, si * 2 + 1);
    node nr = SegTreeConstruct(mid + 1, se, si * 2 + 2);
    int m = min(nl.unleft, nr.unright);
    segtree[si] = node(nl.seqlen + nr.seqlen + 2 * m,
                       nl.unleft + nr.unleft - m,
                       nl.unright + nr.unright - m);
    return segtree[si];
}

node SegTreeRead(int sb, int se, int qb, int qe, int si) {
    if (qb <= sb && qe >= se)
        return segtree[si];

    if (se < qb || sb > qe)
        return node(0, 0, 0);

    int mid = (sb + se) / 2;
    node nl = SegTreeRead(sb, mid, qb, qe, 2 * si + 1);
    node nr = SegTreeRead(mid + 1, se, qb, qe, 2 * si + 2);
    int m = min(nl.unleft, nr.unright);
    return node(nl.seqlen + nr.seqlen + 2 * m,
                nl.unleft + nr.unleft - m,
                nl.unright + nr.unright - m);
}

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

    readf(" %s\n", &parens);
    segtree = new node[parens.length * 4];
    SegTreeConstruct(0, parens.length - 1, 0);

    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        int left, right;
        readf(" %s %s\n", &left, &right);
        node nd = SegTreeRead(0, parens.length - 1, left - 1, right - 1, 0);
        writeln(nd.seqlen);
    }

    return 0;
}

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

struct node {
    int seqlen;
    int unleft;
    int unright;
    this(int sl, int ul, int ur) {
        seqlen = sl;
        unleft = ul;
        unright = ur;
    }
}

node[] segtree;
string parens;

node SegTreeConstruct(int sb, int se, int si) {
    if (sb == se) {
        if (parens[sb] == '(') segtree[si] = node(0, 1, 0);
        if (parens[sb] == ')') segtree[si] = node(0, 0, 1);
        return segtree[si];
    }

    int mid = (sb + se) / 2;
    node nl = SegTreeConstruct(sb, mid, si * 2 + 1);
    node nr = SegTreeConstruct(mid + 1, se, si * 2 + 2);
    int m = min(nl.unleft, nr.unright);
    segtree[si] = node(nl.seqlen + nr.seqlen + 2 * m,
                       nl.unleft + nr.unleft - m,
                       nl.unright + nr.unright - m);
    return segtree[si];
}

node SegTreeRead(int sb, int se, int qb, int qe, int si) {
    if (qb <= sb && qe >= se)
        return segtree[si];

    if (se < qb || sb > qe)
        return node(0, 0, 0);

    int mid = (sb + se) / 2;
    node nl = SegTreeRead(sb, mid, qb, qe, 2 * si + 1);
    node nr = SegTreeRead(mid + 1, se, qb, qe, 2 * si + 2);
    int m = min(nl.unleft, nr.unright);
    return node(nl.seqlen + nr.seqlen + 2 * m,
                nl.unleft + nr.unleft - m,
                nl.unright + nr.unright - m);
}

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

    readf(" %s\n", &parens);
    segtree = new node[parens.length * 4];
    SegTreeConstruct(0, parens.length - 1, 0);

    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        int left, right;
        readf(" %s %s\n", &left, &right);
        node nd = SegTreeRead(0, parens.length - 1, left - 1, right - 1, 0);
        writeln(nd.seqlen);
    }

    return 0;
}

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

struct node {
    int seqlen;
    int unleft;
    int unright;
    this(int sl, int ul, int ur) {
        seqlen = sl;
        unleft = ul;
        unright = ur;
    }
}

__gshared {
    node[] segtree;
    string parens;
}

node SegTreeConstruct(int sb, int se, int si) {
    if (sb == se) {
        if (parens[sb] == '(') segtree[si] = node(0, 1, 0);
        if (parens[sb] == ')') segtree[si] = node(0, 0, 1);
        return segtree[si];
    }

    int mid = (sb + se) / 2;
    node nl = SegTreeConstruct(sb, mid, si * 2);
    node nr = SegTreeConstruct(mid + 1, se, si * 2 + 1);
    int m = min(nl.unleft, nr.unright);
    segtree[si] = node(nl.seqlen + nr.seqlen + 2 * m,
                       nl.unleft + nr.unleft - m,
                       nl.unright + nr.unright - m);
    return segtree[si];
}

node SegTreeRead(int sb, int se, int qb, int qe, int si) {
    if (qb <= sb && qe >= se)
        return segtree[si];

    if (se < qb || sb > qe)
        return node(0, 0, 0);

    int mid = (sb + se) / 2;
    node nl = SegTreeRead(sb, mid, qb, qe, 2 * si);
    node nr = SegTreeRead(mid + 1, se, qb, qe, 2 * si + 1);
    int m = min(nl.unleft, nr.unright);
    return node(nl.seqlen + nr.seqlen + 2 * m,
                nl.unleft + nr.unleft - m,
                nl.unright + nr.unright - m);
}

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

    readf(" %s\n", &parens);
    segtree = new node[parens.length * 4];
    SegTreeConstruct(0, parens.length - 1, 1);

    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        int left, right;
        readf(" %s %s\n", &left, &right);
        node nd = SegTreeRead(0, parens.length - 1, left - 1, right - 1, 1);
        writeln(nd.seqlen);
    }

    return 0;
}

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

struct query {
    int index;
    int l;
    int r;
    int answer;
}

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

    string paren;
    readf(" %s\n", &paren);
    int n;
    readf(" %s\n", &n);
    query[] qarr = new query[n];
    foreach (i; 0 .. n) {
        query q;
        readf(" %s %s\n", &q.l, &q.r);
        --q.l;
        --q.r;
        q.index = i;
        qarr[i] = q;
    }
    int[1000000] posarr = -1;
    int[] pstack;
    foreach (int i, char c; paren) {
        if (c == '(') pstack ~= i;
        else if (!pstack.empty) {
            posarr[i] = pstack.back;
            posarr[pstack.back] = i;
            pstack.popBack();
        }
    }
    qarr.sort!"a.r < b.r";
    int pl = 0, pr = 0, answer = 0;
    foreach (ref q; qarr) {
        for ( ; pr < q.r + 1; ++pr) {
            if (posarr[pr] >=0 && posarr[pr] < pr && pl <= posarr[pr]) answer += 2;
        }
        if (pl < q.l) {
            for ( ; pl < q.l; ++pl) {
                if (posarr[pl] > pl && pr >= posarr[pl]) answer -= 2;
            }
        }
        else while (pl > q.l) {
            --pl;
            if (posarr[pl] > pl && pr >= posarr[pl]) answer += 2;
        }
        q.answer = answer;
    }
    qarr.sort!"a.index < b.index";
    foreach (q; qarr) writeln(q.answer);

    return 0;
}


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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new char[][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto d = RD!string;
		auto e = new long[](n);
		foreach (j, c; d)
		{
			e[j] = [c].to!long;
		}
		auto index = new size_t[](e.length);
		makeIndex!("a < b", SwapStrategy.stable)(e, index);
		size_t[] a1 = [index[0]], a2;
		bool ok = true;
		foreach (j; index[1..$])
		{
			if (a2.empty)
			{
				if (j >= a1.back)
					a1 ~= j;
				else
					a2 ~= j;
			}
			else
			{
				if (j >= a1.back && e[j] == e[a2.front])
					a1 ~= j;
				else if (j >= a2.back)
					a2 ~= j;
				else
				{
					ok = false;
					break;
				}
			}
		}
		debug writeln(a1, a2);
		if (ok)
		{
			ans[i] = new char[](n);
			if (a2.empty)
			{
				foreach (j; 0..n) ans[i][j] = '1';
			}
			else
			{
				foreach (j; a1)
				{
					ans[i][j] = '1';
				}
				foreach (j; a2)
				{
					ans[i][j] = '2';
				}
			}
		}
		else
			ans[i] = "-".dup;
	}

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

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

	A:
	foreach(_; 0 .. t){

		int n = rint;
		char[] cs = read.to!(char[]);

		int[] as;
		foreach(c; cs) as ~= ("" ~ c).to!int;

		string ans = "-";
		foreach(k; 0 .. 9){
			int u = 0, v = k;
			int isOK = 1;
			string tempans = "";
			foreach(a; as){
				if(a >= v){
					tempans ~= "2";
					if(a > v) v = a;
				}
				else if(a <= k && a >= u){
					tempans ~= "1";
					if(a > u) u = a;
				}
				else{
					isOK = 0;
					break;
				}
				log("cs:", cs, "k:", k, "a:", a, "u:", u, "v:", v, "tempans:", tempans);
			}
			if(isOK){
				ans = tempans;
				break;
			}
		}
		ans.writeln;
	}
}

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

	A:
	foreach(_; 0 .. t){

		int n = rint;
		char[] cs = read.to!(char[]);

		int a = 0, b = 0;
		string ans = "";
		foreach(c; cs){
			int x = ("" ~ c).to!int;
			if(b <= c) b = c, ans ~= "2";
			else if(a <= c) a = c, ans ~= "1";
			else{
				"-".writeln;
				continue A;
			}
		}
		ans.writeln;
	}
}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new char[][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto d = RD!string;
		auto e = new long[](n);
		foreach (j, c; d)
		{
			e[j] = [c].to!long;
		}
		auto index = new size_t[](e.length);
		makeIndex!("a < b", SwapStrategy.stable)(e, index);
		size_t[] a1 = [index[0]], a2;
		bool ok = true;
		foreach (j; index[1..$])
		{
			if (j >= a1.back)
				a1 ~= j;
			else if (a2.empty)
				a2 ~= j;
			else if (j >= a2.back)
				a2 ~= j;
			else
			{
				ok = false;
				break;
			}
		}
		debug writeln(a1, a2);
		if (ok)
		{
			ans[i] = new char[](n);
			if (a2.empty)
			{
				foreach (j; 0..n) ans[i][j] = '1';
			}
			else
			{
				foreach (j; a1)
				{
					ans[i][j] = '1';
				}
				foreach (j; a2)
				{
					ans[i][j] = '2';
				}
			}
		}
		else
			ans[i] = "-".dup;
	}

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


import std.stdio;
int main()
{
	int t=0;
	readf(" %d", &t);
	for (int i=0; i<t; i++)
	{
		int a=0;
		readf(" %d", &a);
		int[] m = new int[a];
		for (int j=0; j<a; j++)
		{
			readf(" %d", &m[j]);
		}
		if (a==1 && m[0]%2==1)
		{
			writeln(-1);
		}
		else
		{
			int count=0;
			for (int j=0; j<a; j++)
			{
				if (m[j]%2==0)
				{
					count=count+1;
					writeln(1);
					writeln(j+1);
					break;
				}
			}
			if (count==0)
			{
				writeln(2);
				writeln(1);
				writeln(2);
			}
		}
	}
	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 = 998244353;
//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 int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		foreach (i; 0..n)
		{
			if (a[i] % 2 == 0)
			{
				ans[ti] = [i+1];
				break;
			}
		}
		if (!ans[ti].empty) continue;
		foreach (i; 0..n)
		{
			if (a[i] % 2)
			{
				ans[ti] ~= i+1;
			}
		}
		if (ans[ti].length == 1)
			ans[ti].length = 0;
		else
			ans[ti] = ans[ti][0..2];
	}

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


import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;

struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}

Input input;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
struct ModNum(T)
{
  T representative, modulus;
  this(T representative, T modulus)
  {
    this.representative = representative;
    this.modulus = modulus;
  }
  T minimalPositiveRepresentative()
  {
    return ((representative % modulus) + modulus) % modulus;
  }
  T minimalNegativeRepresentative()
  {
    return ((representative % modulus) - modulus) % modulus;
  }
  T maxRepresentative(T upperBound)
  {
    return upperBound - pmod(upperBound - representative, modulus);
  }
  T minRepresentative(T lowerBound)
  {
    return lowerBound + pmod(representative - lowerBound, modulus);
  }
}
// minimum possible representative of x modulo m.
T pmod(T)(T x, T m)
{
  return (x % m + m) % m;
}
// solves ax + by = gcd(a, b) and returns gcd(a, b)
T extendedEuclideanAlgorithm(T)(T a, T b, out T x, out T y)
{
  if (a == 0)
    {
      x = 0, y = 1;
      return b;
    }
  T x1, y1;
  auto d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1, y = x1;
  return d;
}
alias egcd = extendedEuclideanAlgorithm;
// solves ax = b (mod m) giving a solution as a modulus class.
bool solveLinearCongruence(T)(T a, T b, T m, out ModNum!T res)
{
  T x, p;
  b = pmod(b, m);
  T y;
  T g = egcd(a, m, x, y);
  if (b % g != 0)
    return false;
  p = m / g;
  x = pmod(x * b / g, p);
  res.representative = x;
  res.modulus = p;
  return true;
}

void main()
{
  // a - x >= 0
  // b - y - 2x >= 0
  // b - 2y >= 0

  alias ln = int;
  ln t; get(t);
  while(t--)
    {
      ln a, b, c; get(a, b, c);
      ln mx = 0;
      for(ln x = 0; x <= a; x++)
	{
	  for(ln y = 0; c - 2 * y >= 0; y++)
	    {
	      if ( b - y - 2 * x >= 0)
		{
		  mx = max(mx, x + y + 2 * x + 2 * y);
		}
	    }
	}
      wr(mx);    }
}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (i; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto c = RD;
		auto cnt1 = min(c/2, b);
		auto cnt2 = min((b-cnt1)/2, a);
		ans[i] = (cnt1+cnt2) * 3;
	}

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

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;

struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}

Input input;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
struct ModNum(T)
{
  T representative, modulus;
  this(T representative, T modulus)
  {
    this.representative = representative;
    this.modulus = modulus;
  }
  T minimalPositiveRepresentative()
  {
    return ((representative % modulus) + modulus) % modulus;
  }
  T minimalNegativeRepresentative()
  {
    return ((representative % modulus) - modulus) % modulus;
  }
  T maxRepresentative(T upperBound)
  {
    return upperBound - pmod(upperBound - representative, modulus);
  }
  T minRepresentative(T lowerBound)
  {
    return lowerBound + pmod(representative - lowerBound, modulus);
  }
}
// minimum possible representative of x modulo m.
T pmod(T)(T x, T m)
{
  return (x % m + m) % m;
}
// solves ax + by = gcd(a, b) and returns gcd(a, b)
T extendedEuclideanAlgorithm(T)(T a, T b, out T x, out T y)
{
  if (a == 0)
    {
      x = 0, y = 1;
      return b;
    }
  T x1, y1;
  auto d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1, y = x1;
  return d;
}
alias egcd = extendedEuclideanAlgorithm;
// solves ax = b (mod m) giving a solution as a modulus class.
bool solveLinearCongruence(T)(T a, T b, T m, out ModNum!T res)
{
  T x, p;
  b = pmod(b, m);
  T y;
  T g = egcd(a, m, x, y);
  if (b % g != 0)
    return false;
  p = m / g;
  x = pmod(x * b / g, p);
  res.representative = x;
  res.modulus = p;
  return true;
}

void main()
{
  // a - x >= 0
  // b - y - 2x >= 0
  // b - 2y >= 0

  alias ln = int;
  ln t; get(t);
  while(t--)
    {
      ln a, b, c; get(a, b, c);
      ln mx = 0;
      for(ln x = 0; x <= a; x++)
	{
	  for(ln y = 0; 2 * y <= b; y++)
	    {
	      if ( b - y - 2 * x >= 0 && c - 2 * y >= 0)
		{
		  mx = max(mx, x + y + 2 * x + 2 * y);
		}
	    }
	}
      wr(mx);    }
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;

struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}

Input input;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
struct ModNum(T)
{
  T representative, modulus;
  this(T representative, T modulus)
  {
    this.representative = representative;
    this.modulus = modulus;
  }
  T minimalPositiveRepresentative()
  {
    return ((representative % modulus) + modulus) % modulus;
  }
  T minimalNegativeRepresentative()
  {
    return ((representative % modulus) - modulus) % modulus;
  }
  T maxRepresentative(T upperBound)
  {
    return upperBound - pmod(upperBound - representative, modulus);
  }
  T minRepresentative(T lowerBound)
  {
    return lowerBound + pmod(representative - lowerBound, modulus);
  }
}
// minimum possible representative of x modulo m.
T pmod(T)(T x, T m)
{
  return (x % m + m) % m;
}
// solves ax + by = gcd(a, b) and returns gcd(a, b)
T extendedEuclideanAlgorithm(T)(T a, T b, out T x, out T y)
{
  if (a == 0)
    {
      x = 0, y = 1;
      return b;
    }
  T x1, y1;
  auto d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1, y = x1;
  return d;
}
alias egcd = extendedEuclideanAlgorithm;
// solves ax = b (mod m) giving a solution as a modulus class.
bool solveLinearCongruence(T)(T a, T b, T m, out ModNum!T res)
{
  T x, p;
  b = pmod(b, m);
  T y;
  T g = egcd(a, m, x, y);
  if (b % g != 0)
    return false;
  p = m / g;
  x = pmod(x * b / g, p);
  res.representative = x;
  res.modulus = p;
  return true;
}

void main()
{
  // a - x >= 0
  // b - y - 2x >= 0
  // b - 2y >= 0

  alias ln = int;
  ln t; get(t);
  while(t--)
    {
      ln a, b, c; get(a, b, c);
      ln mx = 0;
      for(ln x = 0; x <= a; x++)
	{
	  for(ln y = 0; 2 * y <= b; y++)
	    {
	      if ( b - y - 2 * x >= 0 && b - 2 * y >= 0)
		{
		  mx = max(mx, x + y + 2 * x + 2 * y);
		}
	    }
	}
      wr(mx);
    }
}


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 A = 2050L;

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

        long ans;
        if (N % A == 0) {
          for (long m = N / A; m > 0; m /= 10) {
            ans += m % 10;
          }
        } else {
          ans = -1;
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable int base = 2050;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(long);
		if (n % base != 0)
		{
			writeln (-1);
		}
		else
		{
			n /= base;
			writeln (n.text.map !(q{a - '0'}).sum);
		}
	}
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);

        if(d % 2050 != 0)
        {
            writeln(-1);
        }
        else
        {
            long m = d / 2050;
            long sum = 0;
            while(m  > 0)
            {
                sum += (m%10);
                m /= 10;
            }

            writeln(sum);
        }
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        real d = to!real(param);
        if(d < 2050)
        {
         writeln(-1);
         continue;
        }
        real m = 2050 * pow(10, ceil(log10(d))-4);
        //writeln(m);
        int c = 0;

        while(d > m)
        {
            d -= m;
            c++;

            if(m > d)
                m /= 10;
        }

        if(d < m)
            writeln(-1);
        else
            writeln(c+1);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    outer : foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        while(m*10 <= d)
        {
            m = m*10;
        }

        int c = 0;
        while(d >= m)
        {
            d -= m;
            c++;

             if(m > d && m >= 10)
                m /= 10;
        }

        if(d != 0)
        writeln(-1);
        else writeln(c);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    outer : foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        while(m*10 <= d)
        {
            m = m*10;
        }

        int c = 0;
        while(d >= m)
        {
            d -= m;
            c++;

             if(m > d && m > 10)
                m /= 10;
        }

        if(d != 0)
        writeln(-1);
        else writeln(c);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        if(d < m || d % m != 0)
        {
            writeln(-1);
            continue;
        }

        long q = d / m;

        while(m*10 <= d)
        {
            m = m*10;
        }

        int c = 0;
        while(d > 0)
        {
            d -= m;
            c++;
             if(m > d)
                m /= 10;

        }

        writeln(c);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        if(d < m || d % m != 0)
        {
            writeln(-1);
            continue;
        }

        long q = d / m;

        while(m*10 < d)
        {
            m = m*10;
        }

        int c = 0;
        while(d > 0)
        {
            d -= m;
            c++;
             if(m > d)
                m /= 10;

        }

        writeln(c);
    }
}


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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int [] [] moves;
		foreach (i; 0..2)
		{
			int [] line;
			int k;
			readf (" %s", &k);
			foreach (j; 0..k)
			{
				int v;
				readf (" %s", &v);
				line ~= n - v;
			}
			moves ~= line;
		}

		auto g = new int [] [] (2, n);
		auto c = new int [] [] (2, n);

		alias Pair = Tuple !(int, q{who}, int, q{pos});
		Pair [] q;

		void add (Pair p, int v)
		{
			g[p.who][p.pos] = v;
			c[p.who][p.pos] = n;
			q ~= p;
		}

		add (Pair (0, 0), -1);
		add (Pair (1, 0), -1);

		while (!q.empty)
		{
			auto who = q.front.who;
			auto pos = q.front.pos;
			auto res = g[who][pos];
			q.popFront ();
			q.assumeSafeAppend ();

			foreach (d; moves[!who])
			{
				int next = (pos + d) % n;
				if (g[!who][next] != 0)
				{
					continue;
				}
				if (res == -1)
				{
					add (Pair (!who, next), +1);
				}
				else
				{
					c[!who][next] += 1;
					if (c[!who][next] >=
					    moves[!who].length)
					{
						add (Pair (!who, next), -1);
					}
				}
			}
		}

		foreach (line; g)
		{
			line.drop (1).map !(x =>
			    ["Lose", "Loop", "Win"][x + 1]).join (" ").writeln;
		}
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto K = new int[2];
      auto S = new int[][2];
      foreach (i; 0 .. 2) {
        K[i] = readInt();
        S[i] = new int[K[i]];
        foreach (j; 0 .. K[i]) {
          S[i][j] = readInt();
        }
      }

      auto dp = new int[][](2, N);
      auto cnt = new int[][](2, N);
      foreach (t; 0 .. 2) foreach (u; 0 .. N) {
        dp[t][u] = -1;
        cnt[t][u] = K[t];
      }

      auto que = new int[2 * N * 2];
      int qb, qe;
      foreach (t; 0 .. 2) {
        dp[t][0] = 0;
        que[qe++] = t;
        que[qe++] = 0;
      }

      for (; qb != qe; ) {
        const t = que[qb++];
        const x = que[qb++];
        foreach (s; S[t ^ 1]) {
          const tt = t ^ 1;
          const xx = (x - s < 0) ? (x - s + N) : (x - s);
          if (dp[tt][xx] == -1) {
            if (dp[t][x] & 1) {
              if (--cnt[tt][xx] > 0) {
                continue;
              }
            }
            dp[tt][xx] = dp[t][x] + 1;
            que[qe++] = tt;
            que[qe++] = xx;
          }
        }
      }

      foreach (t; 0 .. 2) {
        foreach (x; 1 .. N) {
          if (x > 1) write(" ");
          write((dp[t][x] == -1) ? "Loop" : (dp[t][x] & 1) ? "Win" : "Lose");
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}


module cf_159b;

import std.stdio;
import std.algorithm;

void main() {
  immutable MAX_VALUE = 1000;

  int n, m, a, b;
  int[MAX_VALUE + 1][MAX_VALUE + 1] pairCount;
  int[MAX_VALUE + 1] diamCount;
  int closed = 0, nicelyClosed = 0;

  readf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    readf(" %d %d", &a, &b);
    ++pairCount[a][b];
    ++diamCount[b];
  }
  for (int i = 0; i < m; ++i) {
    readf(" %d %d", &a, &b);

    if (pairCount[a][b] > 0) {
      ++nicelyClosed;
      --pairCount[a][b];
    }
    if (diamCount[b] > 0) {
      ++closed;
      --diamCount[b];
    }
  }

  writeln(closed, " ", nicelyClosed);
}

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

immutable int INF = 10L ^^ 9 + 23;

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

    auto vals = new int[][][] (2, 1001, 1001);
    foreach (i; 0 .. 2) {
        foreach (_; 0 .. ns[i]) {
            int x, y;
            readf("%s %s", &x, &y);
            readln;
            vals[i][y][x] += 1;
        }
    }

    int u = 0, v = 0;
    foreach (d; 1 .. 1001) {
        int mleft = 0, cleft = 0;
        int ok = 0;
        foreach (c; 1 .. 1001) {
            ok += min(vals[0][d][c], vals[1][d][c]);
            if (vals[0][d][c] > vals[1][d][c]) mleft += vals[0][d][c] - vals[1][d][c];
            else cleft += vals[1][d][c] - vals[0][d][c];
        }

        u += ok;
        v += ok;

        u += min(mleft, cleft);
    }

    writeln(u, ' ', v);
}

module cf_159b;

import std.stdio;
import std.algorithm;

void main() {
  immutable MAX_VALUE = 1000;

  int n, m, a, b;
  int[MAX_VALUE + 1][MAX_VALUE + 1] pairCount;
  int[MAX_VALUE + 1] diamCount;
  int closed = 0, nicelyClosed = 0;

  readf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    readf(" %d %d", &a, &b);
    ++pairCount[a][b];
    ++diamCount[b];
  }
  for (int i = 0; i < m; ++i) {
    readf(" %d %d", &a, &b);

    if (pairCount[a][b] > 0) {
      ++nicelyClosed;
    }
    if (diamCount[b] > 0) {
      ++closed;
    }
  }

  writeln(closed, " ", nicelyClosed);
}

module cf_159b;

import std.stdio;
import std.algorithm;

void main() {
  int n, m;
  int[2][] markers, caps;

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

  sort!((a, b) { return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]; })(markers);
  sort!((a, b) { return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]; })(caps);

  int nicelyClosed = 0;
  for (int i = 0; i < n; ++i) {
    int l = 0, r = caps.length - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      bool leftCondition = markers[i][0] > caps[mid][0] ||
    markers[i][0] == caps[mid][0] && markers[i][1] > caps[mid][1];
      if (leftCondition) {
    l = mid + 1;
      } else {
    r = mid;
      }
    }

    bool cond = markers[i][0] == caps[r][0] && markers[i][1] == caps[r][1];
    if (cond) {
      ++nicelyClosed;
    }
  }

  sort!((a, b) { return a[1] < b[1]; })(markers);
  sort!((a, b) { return a[1] < b[1]; })(caps);

  int closed = 0;
  for (int i = 0; i < n; ++i) {
    int l = 0, r = caps.length - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      if (markers[i][1] > caps[mid][1]) {
    l = mid + 1;
      } else {
    r = mid;
      }
    }

    bool cond = markers[i][1] == caps[r][1];
    if (cond) {
      ++closed;
    }
  }

  writeln(closed, " ", nicelyClosed);
}


import std.stdio;

void main ()
{
	int tests;
	readf (" %s", &tests);
	foreach (test; 0..tests)
	{
		int n;
		readf (" %s", &n);
		long res = n * 1L * (n + 1) / 2;
		for (int p = 1; p <= n; p <<= 1)
		{
			res -= p * 2;
		}
		writeln (res);
	}
}

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

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        t = cin.readInt;
        while (t--) {
                long n = cin.readString.to!long;
                long sum = (n * (n + 1)) / 2;
                int num = 0;
                for (int i = 1; num <= n; i++) {
                        num = 1 << i;
                        sum -= num;
                }
                writeln(sum);
        }
}

module main;

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

immutable int MAX_LOG = 31;
int[MAX_LOG] powers;
long[MAX_LOG] prefixes;

long specialSum( int n ) {

//    debug {writeln((1L + n) * n / 2);
//            writeln(cast (int)(trunc(log2(cast (double)(n)))));
//            writeln(prefixes[cast (int)(trunc(log2(cast (double)(n))))]);};
    return (1L + n) * n / 2 - 2 *
            prefixes[cast (int)(trunc(log2(cast (double)(n))))];
}

int main() {

    auto t = readln().strip().to! (int);
    long answers[];

    powers[0] = 1;
    foreach (int i; 1..powers.length)
        powers[i] = powers[i - 1] * 2;
    prefixes[0] = 1;
    foreach (int i; 1..prefixes.length)
        prefixes[i] = prefixes[i - 1] + powers[i];

    // debug {writeln(powers, '\n', prefixes);};

    foreach (int i; 0..t) {
        auto n = readln().strip().to! (int);

        answers ~= specialSum(n);
    }

    foreach (i; answers)
        writeln(i);

	return 0;
}