// Vicfred
// https://codeforces.com/contest/1496/problem/B
// math, greedy
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.stdio;
import std.string;

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

    long n, k;
while(t--) {
    readf("%s %s\n", &n, &k);
    long[] a = readln.split.map!(to!long).array;
    a.sort;

    long maxima = a[$-1];
    long mex = maxima + 1;
    for(int i = 0; i < n; ++i) {
        if(i != a[i]) {
            mex = i;
            break;
        }
    }

    auto rbt = redBlackTree(a);

    //a.writeln;
    //writefln("max: %s mex: %s\n", maxima, mex);
    if(mex < maxima) {
        if(k > 0) {
            rbt.insert((maxima + mex - 1)/2 + 1);
        }
        writefln("%s", rbt.length);
        continue;
    }

    writefln("%s", n + k);

}
}

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

void theCode(){
    int n;
    n = scan!int;
    ll k = scan;
    auto arr = scanArray;
    ll maxx = arr.maxElement;
    auto rbt = redBlackTree!ll(arr);
    bool f = 1;
    for(ll i = 0; i < n.to!long; ++i){
        if(!(i in rbt)){
            f = 0;
            ll val = (maxx + i)/2 + (maxx + i) % 2;
            if(k) rbt.insert(val);
            break;
        }
    }
    if(f){
        writeln(n + k);
    }else{
        writeln(rbt.length);
    }
}

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

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (k == 0)
		{
			ans[ti] = n;
			continue;
		}
		int r = n;
		foreach (i; 0..n)
		{
			if (a[i] != i)
			{
				r = i;
				break;
			}
		}
		if (r == n)
			ans[ti] = n + k;
		else
		{
			auto y = (r + a[n-1] + 1) / 2;
			int add = 1;
			foreach (i; 0..n)
			{
				if (a[i] == y)
				{
					add = 0;
					break;
				}
			}
			ans[ti] = n + add;
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (k == 0)
		{
			ans[ti] = n;
			continue;
		}
		int r = n;
		foreach (i; 0..n)
		{
			if (a[i] != i)
			{
				r = i;
				break;
			}
		}
		auto y = (r + a[n-1] + 1) / 2;
		if (y == n)
			ans[ti] = n + k;
		else
		{
			int add = 1;
			foreach (i; 0..n)
			{
				if (a[i] == y)
				{
					add = 0;
					break;
				}
			}
			ans[ti] = n + add;
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

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

		if (k == 0)
		{
			ans[ti] = n;
			continue;
		}
		int r = n;
		foreach (i; 0..n)
		{
			if (a[i] != i)
			{
				r = i;
				break;
			}
		}
		auto y = (r + a[n-1] + 1) / 2;
		if (y == n)
			ans[ti] = n + k;
		else
		{
			bool g(int x)
			{
				return a[x] <= y;
			}
			auto rr = binarySearch!(g)(-1, n);
			if (rr == -1)
				ans[ti] = n + 1;
			else if (a[rr] != y)
				ans[ti] = n + 1;
			else
				ans[ti] = n;
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

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

		if (k == 0)
		{
			ans[ti] = n;
			continue;
		}
		bool f(int x)
		{
			return a[x] == x;
		}
		auto r = binarySearch!(f)(-1, n) + 1;
		auto y = (r + a[n-1] + 1) / 2;
		if (y == n)
			ans[ti] = n + k;
		else
		{
			bool g(int x)
			{
				return a[x] <= y;
			}
			auto rr = binarySearch!(g)(-1, n);
			if (rr == -1)
				ans[ti] = n + 1;
			else if (a[rr] != y)
				ans[ti] = n + 1;
			else
				ans[ti] = n;
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

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

		bool f(int x)
		{
			return a[x] == x;
		}
		auto r = binarySearch!(f)(-1, n) + 1;
		auto y = (r + a[n-1] + 1) / 2;
		if (y == n)
			ans[ti] = n + k;
		else
		{
			bool g(int x)
			{
				return a[x] <= y;
			}
			auto rr = binarySearch!(g)(-1, n);
			if (rr == -1)
				ans[ti] = n + 1;
			else if (a[rr] != y)
				ans[ti] = n + 1;
			else
				ans[ti] = n;
		}
	}

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


import std.stdio;
import std.string;

void main() {

	ubyte n;

	readf("%s", &n);

	int ans = 1;
	int[string] map;
	string name1, name2, tmp;

	map["polycarp"] = 1;

	while (n--) {
		readf(" %s %s %s\n", &name1, &tmp, &name2);
		auto e = map[toLower(name2)] + 1;
		if (e > ans)
			ans = e;
		map[toLower(name1)] = e;
	}

	writeln(ans);
}

import std.stdio;
import std.string;

void main() {

	ubyte n;

	readf("%s", &n);

	int ans = 1;
	int[string] map;
	string name1, name2;

	map["polycarp"] = 1;

	while (n--) {
		name1 = readln(' ').strip;
		readln(' ');
		name2 = readln.strip;
		auto e = map[toLower(name2)] + 1;
		if (e > ans)
			ans = e;
		map[toLower(name1)] = e;
	}

	writeln(ans);
}


import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
alias mp=make_pair;
alias bins=binary_search;
alias orsq=orient_square;
immutable int mod=1000000007;
pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
pure nothrow @property X sqr(X)(in X a_) {return a_*a_;}
pure nothrow @property X cub(X)(in X a_) {return a_*a_*a_;}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
pure nothrow V foldr(R,V,T)(R range,T arg,V nach) if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
{
	for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
	return nach;
}
pure nothrow size_t countr(R,T)(R range,T fun) if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
{
	size_t nach;
	for(;!range.empty;range.popFront()) nach+=fun(range.front);
	return nach;
}
pure nothrow T orient_square(T)(pair!(T,T)[] figure...) if(is(typeof(figure.front*figure.front)==T))
{
	auto n=figure.front,t=figure.front;
	figure.popFront();
	T ans=0;
	for(;!figure.empty;figure.popFront())
	{
		ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
		t=figure.front;
	}
	return ans+(n.fi-t.fi)*(n.se+t.se);
}
pure nothrow real square(T)(pair!(T,T)[] figure...) if(is(typeof(figure.front*figure.front)==T))
{
	return abs(orient_square(figure))/2.000000000000000;
}
struct Tree(T,alias arg="a+b")
{
	private alias fun=binaryFun!arg;
	private static const uint n=int.max;
	private T[uint] t;
	private void upd(uint v,uint vl,uint vr,uint i,T x)
	{
		if (vr - vl == 1)t[v] = x;
		uint vm = (vl + vr) >> 1;
		if(i<vm)upd(2*v,vl,vm,i,x);
		else upd(2*v+1,vm,vr,i,x);
		t[v]=fun(t[v*2],t[v*2+1]);
	}
	void upd(uint i,T x)
	{
		upd(1,0,n,i,x);
	}
	private T cel(uint v,uint vl,uint vr,uint l,uint r)
	{
		if(vl==l && r==vr) return t[v];
		uint vm=(vl+vr) >> 1;
		if(r<=vm) return cel(2*v,vl,vm,l,r);
		else if(l>=vm) return cel(2*v+1,vm,vr,l,r);
		else return fun(cel(2*v,vl,vm,l,vm),cel(2*v+1,vm,vr,vm,r));
	}
	T cel(uint l,uint r){return cel(1,0,n,l,r);}
};
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		auto a=new pii[n];
		foreach_reverse(i;0..n)input(&a[i].fi,&a[i].se);
		int check(int r)
		{
			foreach(i;0..n)
			{
				r-=a[i].fi;
				if(a[i].se==2 && r>1899)return 1;
				else if(a[i].se==1 && r<1900) return -1;
			}
			return 0;
		}
		int l=int.min/2,r=int.max/2;
		while(r-l>1)
		{
			int m=(l+r)>>1;
			auto k=check(m);
			if(k==1) r=m;
			else l=m;
		}
		if(l>=int.max/2-1)writeln("Infinity");
		else if(check(l)!=0)writeln("Impossible");
		else writeln(l);
	}
	debug system("pause");
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int lo = int.min / 2;
		int hi = int.max / 2;
		int delta = 0;
		foreach (i; 0..n)
		{
			int c;
			int d;
			readf (" %s %s", &c, &d);
			if (d == 1)
			{ // rating + delta >= 1900
				lo = max (lo, 1900 - delta);
			}
			else if (d == 2)
			{ // rating + delta <= 1899
				hi = min (hi, 1899 - delta);
			}
			else
			{
				assert (false);
			}
			delta += c;
		}
		debug {writeln (lo, " ", hi);}
		if (hi > int.max / 4)
		{
			writeln ("Infinity");
		}
		else if (lo > hi)
		{
			writeln ("Impossible");
		}
		else
		{
			writeln (hi + delta);
		}
	}
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		auto x = min(n-1, r);
		ans[ti] = x * (x+1) / 2;
		if (r >= n)
			++ans[ti];
	}

	foreach (e; ans)
		writeln(e);

	stdout.flush;
	debug readln;
}

import std.container: DList;
import std.algorithm: all, map, count, filter;
import std.range: iota;
import std.array: array;
import std.stdio;
import std.utf;

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, r;
      read(n), read(r);
      // k < n
      // k = 1: 1
      // k = 2: 2
      // k = 3: 3
      // ...
      // k = n - 1: n - 1
      // (n - 1)n / 2
      // k >= n
      // k = n: 1
      // k = n + 1: 1
      // ...
      // k = r: r - n
      // (r - n + 1)(r - n + 2) / 2

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

import std.algorithm, std.conv, std.stdio, std.string;
void main () {
	foreach (i; 0..readln.strip.to!int) {
		int n, r;
		readf !(" %s %s") (n, r);
		int s = min (n, r + 1);
		writeln (s * (s - 1L) / 2 + (n <= r));
	}
}


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

void main()
{
	int t = readInt!int;
	while (t--) solve;
}
void solve()
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = new char[][](n);
	foreach(i; 0 .. n)
	{
		mat[i] = cast(char[]) readString;
	}
	int color = 0;
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
			if (mat[i][j] != '.')
				color = ((i + j + (mat[i][j] == 'R')))%2;
	bool can = true;
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
			if (mat[i][j] != '.')
			{
				if ((mat[i][j] == 'R') != (i + j + color)%2)
				{
					can = false;
				}
			}
	if (!can) return "NO".writeln;
	"YES".writeln;
	foreach(i; 0 .. n)
	{
		foreach(j; 0 .. m)
		{
			if ((i + j + color)%2 == 0) "W".write;
			else "R".write;
		}
		writeln;
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

bool check(string[][] a, int n, int m, bool r11)
{
  foreach (i ; 0 .. n) {
    foreach (j ; 0 .. m) {
      string expected = ((i + j) % 2 == 0) == r11 ? "R" : "W";
      if (a[i][j] != expected && a[i][j] != ".")
        return false;
    }
  }
  return true;
}

void fill(string[][] a, int n, int m, bool r11)
{
  string c1, c2;
  if (r11) {
    c1 = "R";
    c2 = "W";
  } else {
    c1 = "W";
    c2 = "R";
  }
  foreach (i ; 0 .. n) {
    foreach (j ; 0 .. m) {
      a[i][j] = (i + j) % 2 == 0 ? c1 : c2;
    }
  }
}

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n, m;
    readf!" %d %d "(n, m);
    string[][] a;
    foreach (i ; 0 .. n) {
      a ~= readln.strip.split("").array;
    }
    if (check(a, n, m, true)) {
      writeln("YES");
      fill(a, n, m, true);
      foreach (i ; 0 .. n) { writeln(a[i].join("")); }
    } else if (check(a, n, m, false)) {
      writeln("YES");
      fill(a, n, m, false);
      foreach (i ; 0 .. n) { writeln(a[i].join("")); }
    } else {
      writeln("NO");
    }
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
multitest_loop:
	foreach (test; 0..tests)
	{
		int rows, cols;
		readf !(" %s %s") (rows, cols);
		readln;
		char [] [] board;
		foreach (row; 0..rows)
		{
			board ~= readln.strip.dup;
		}
		foreach (k; 0..2)
		{
			auto answer = new char [] [] (rows, cols);
			foreach (row; 0..rows)
			{
				foreach (col; 0..cols)
				{
					answer[row][col] =
					    "RW"[(row ^ col ^ k) & 1];
				}
			}
			bool good = true;
			foreach (row; 0..rows)
			{
				foreach (col; 0..cols)
				{
					if (answer[row][col] !=
					    board[row][col] &&
					    board[row][col] != '.')
					{
						good = false;
					}
				}
			}
			if (good)
			{
				writeln ("YES");
				writefln !("%-(%s\n%)") (answer);
				continue multitest_loop;
			}
		}
		writeln ("NO");
	}
}

// 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 tests; readf!"%s\n"(tests);
multitest_end:
  while(tests--) {
    int n, m;
    readf!"%s %s\n"(n, m);
    char[][] grid;
    foreach(i; 0 .. n) {
      grid ~= readln.strip.dup;
    }
    foreach(k; 0 .. 2) {
      auto answer = new char[][](n, m);
      foreach(i; 0 .. n) {
        foreach(j; 0 .. m) {
          answer[i][j] = "RW"[(i + j + k) % 2];
        }
      }
      bool good = true;
      foreach(i; 0 .. n) {
        foreach(j; 0 .. m) {
          good &= (grid[i][j] == answer[i][j] || grid[i][j] == '.');
        }
      }
      if (good) {
        writefln!"YES\n%-(%s\n%)"(answer);
        continue multitest_end;
      }
    }
    writefln!"NO";
  }

} // 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 tests; readf!"%s\n"(tests);
  while(tests--) {
    int n, m;
    readf!"%s %s\n"(n, m);
    char[][] grid = new char[][n];
    foreach(ref str; grid) {
      str = readln.strip.dup;
    }
    int red = 0, white = 0;
    foreach(i; 0 .. n) {
      foreach(j; 0 .. m) {
        if (grid[i][j] == 'R') {
          red |= (1 << ((i + j) % 2));
        }
        else if (grid[i][j] == 'W') {
          white |= (1 << ((i + j) % 2));
        }
      }
    }
    if ((red & white) != 0) {
      "NO".writeln;
    }
    else {
      "YES".writeln;
      if (red == 0 && white == 0) {
        red = 1, white = 2;
      }
      else if (red == 0) {
        red = 3 - white;
      }
      else if (white == 0) {
        white = 3 - red;
      }
      foreach(i; 0 .. n) {
        foreach(j; 0 .. m) {
          if ((1 << ((i + j) % 2)) == red) {
            grid[i][j] = 'R';
          }
          else {
            grid[i][j] = 'W';
          }
        }
      }
      writefln!"%-(%s\n%)"(grid);
    }
  }

} // main


module main;

import std.stdio, std.string;

void solve(char[] s)
{
    int[] ans;
    int pos = -1;
    foreach (i; 0 .. s.length)
    {
        if (s[i] == '#')
        {
            ans ~= 1;
            pos = i;
        }
    }
    int cl = 0, cr = 0;
    foreach (i; 0 .. s.length)
    {
        if (s[i] == '(')
        {
            ++ cl;
        }
        else
        {
            ++ cr;
        }
        if (cr > cl)
        {
            writeln(-1);
            return;
        }
    }
    ans[$ - 1] += cl - cr;
    cl = cr = 0;
    int idx = 0;
    foreach (i; 0 .. s.length)
    {
        if (s[i] == '(')
        {
            ++ cl;
        }
        else if (s[i] == ')')
        {
            ++ cr;
        }
        else
        {
            cr += ans[idx];
            ++ idx;
        }
        if (cr > cl)
        {
            writeln(-1);
            return;
        }
    }
    foreach (i, val; ans)
    {
        writeln(val);
    }
}

int main(string[] args)
{
    char[] s;
    while (stdin.readln(s))
    {
        solve(chomp(s));
    }
    return 0;
}

module main;

import std.stdio, std.string;

void solve(char[] s)
{
    int cl = 0, cr = 0, sl = 0, sr = 0, pos = -1, cnt = 0;
    foreach (i; 0 .. s.length)
    {
        if (s[i] == '(')
        {
            ++ sl;
        }
        else if (s[i] == ')')
        {
            ++ sr;
        }
        else
        {
            pos = i;
            ++ cnt;
        }
    }
    int add = 0;
    auto ans = new int[cnt];
    int idx = 0;
    foreach (i; 0 .. s.length)
    {
        if (s[i] == '(')
        {
            ++ cl;
        }
        else if (s[i] == ')')
        {
            ++ cr;
        }
        else
        {
            if (i == pos)
            {
                int need = sl - (sr + add);
                if (need <= 0)
                {
                    writeln(-1);
                    return;
                }
                ans[idx] = need;
                add += need;
            }
            else
            {
                ans[idx] = 1;
                ++ add;
            }
            ++ idx;
        }
        if (cr + add > cl)
        {
            writeln(-1);
            return;
        }
    }
    if (cr + add != cl)
    {
        writeln(-1);
    }
    else
    {
        foreach (i, val; ans)
        {
            writeln(ans[i]);
        }
    }
}

int main(string[] args)
{
    char[] s;
    while (stdin.readln(s))
    {
        solve(chomp(s));
    }
	return 0;
}

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

void main() {
    auto s = readln.chomp;
    int n = cast(int)s.count!"a == '#'";

    int[] ans;

    int x;
    int open, close;
    foreach (i, c; s) {
        if (c == '(') open++;
        else if (c == ')') close++;
        else {
            assert(c == '#');
            if (x == n - 1) {
                int d = open - close;
                foreach (j; i + 1 .. s.length) {
                    d += (s[j] == '(' ? 1 : -1);
                }
                if (d <= 0) {
                    writeln(-1);
                    return;
                } else {
                    ans ~= d;
                    close += d;
                }
            } else {
                ans ~= 1;
                close++;
                x++;
            }
        }
        if (open < close) {
            writeln(-1);
            return;
        }
    }
    writefln("%(%s\n%)", ans);
}

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

void main() {
    auto s = readln.chomp;
    int n = cast(int)s.count!"a == '#'";

    int[] ans;

    int x;
    int open, close;
    foreach (i, c; s) {
        if (c == '(') open++;
        else if (c == ')') close++;
        else {
            assert(c == '#');
            if (x == n - 1) {
                int d = open - close;
                foreach (j; i + 1 .. s.length) {
                    d += (s[j] == '(' ? 1 : -1);
                }
                if (d <= 0) {
                    writeln(-1);
                    return;
                } else {
                    ans ~= d;
                }
            } else {
                ans ~= 1;
                close++;
                x++;
            }
        }
    }

    writefln("%(%s\n%)", ans);
}

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

void main() {
    auto s = readln.chomp;
    int n = cast(int)s.count!"a == '#'";

    int[] ans;

    int x;
    int open, close;
    foreach (i, c; s) {
        if (c == '(') open++;
        else if (c == ')') close++;
        else {
            assert(c == '#');
            if (x == n - 1) {
                int d = open - close;
                foreach (j; i + 1 .. s.length) {
                    d += (s[j] == '(' ? 1 : -1);
                }
                if (d <= 0) {
                    writeln(-1);
                    return;
                } else {
                    ans ~= d;
                }
            } else {
                ans ~= 1;
                close++;
                x++;
            }
        }
    }

    x = 0; open = 0; close = 0;
    foreach (i, c; s) {
        if (c == '(') open++;
        else if (c == ')') close++;
        else {
            assert(c == '#');
            close += ans[x];
        }
        if (open < close) {
            writeln(-1); return;
        }
    }

    writefln("%(%s\n%)", ans);
}

module main;

import std.stdio, std.string;

void solve(char[] s)
{
    int[] ans;
    int pos = -1;
    foreach (i; 0 .. s.length)
    {
        if (s[i] == '#')
        {
            ans ~= 1;
            pos = i;
        }
    }
    int cl = 0, cr = 0;
    foreach (i; 0 .. s.length)
    {
        if (s[i] == '(')
        {
            ++ cl;
        }
        else
        {
            ++ cr;
        }
        if (cr > cl)
        {
            writeln(-1);
            return;
        }
    }
    ans[$ - 1] += cl - cr;
    foreach (i, val; ans)
    {
        writeln(val);
    }
}

int main(string[] args)
{
    char[] s;
    while (stdin.readln(s))
    {
        solve(chomp(s));
    }
    return 0;
}


immutable multi = true;

void solve(int tc)
{
  auto b = ma(7, readInt!int);
  auto bs = redBlackTree!(true, int)(b);
  int[3] a;
  a[0] = b[0];
  a[1] = b[1];
  bs.removeKey(a[0]);
  bs.removeKey(a[1]);
  bs.removeKey(a[0] + a[1]);
  a[2] = bs.front;
  foreach(ai; a) write(ai, " ");
  writeln;
}

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

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

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

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

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

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

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto b = readln.splitter.map!(to!int).array;
    writefln("%d %d %d", b[0], b[1], b.back - b[0] - b[1]);
  }
}

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

void solve(){
    auto arr = scanArray;
    auto summ = arr[6];
    long a = summ - arr[5];
    long b = summ - arr[4];
    long c = summ - a - b;
    writeln(a, " ", b, " ", c);
}

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

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


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

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

void solve() {
    int N, K; readf("%d %d\n", &N, &K);
    auto A = readarray!int;

    int[] C = new int[N + 2];
    foreach (a; A) C[a]++;

    int[] S = new int[N + 3];
    for (int i = 0; i <= N+1; i++) {
        S[i + 1] = S[i] + C[i];
    }

    int cd = int.max;
    int[] range = [-1, -1];
    for (int x = 0; x <= N; x++) {
        int lb = x, ub = N + 1;
        bool check(int m) {
            return S[m] - S[x] >= K + (N - K + 1) / 2;
        }
        if (! check(ub)) continue;
        while (lb + 1 < ub) {
            int mid = (lb + ub) / 2;
            (check(mid) ? ub : lb) = mid;
        }
        int y = ub - 1;
        if (y - x < cd) {
            cd = y - x;
            range = [x, y];
        }
    }

    int x = range[0], y = range[1];
    assert(x >= 0 && y >= 0 && x <= y);

    Array!int ins, outs;
    int[] ans;
    for (int i = 0; i < N; i++) {
        int a = A[i];
        if (x <= a && a <= y) {
            if (outs.empty) {
                ins ~= i;
            } else {
                outs.removeBack;
            }
        } else {
            if (ins.empty) {
                outs ~= i;
            } else {
                ins.removeBack;
            }
        }
        if (ins.length == 1) {
            ins.removeBack;
            ans ~= i;
            if (ans.length == K) break;
        }
    }
    writefln("%s %s", x, y);
    //writeln(ans);

    if (K == 1) {
        writefln("%s %s", 1, N);
    } else {
        for (int k = 0; k < K; k++) {
            int L, R;
            if (k == 0) {
                L = 0;
                R = ans[k];
            } else if (k == K - 1) {
                L = ans[k - 1] + 1;
                R = N - 1;
            } else {
                L = ans[k - 1] + 1;
                R = ans[k];
            }
            writefln("%s %s", L+1, R+1);
        }
    }

}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = a.dup;
		sort (b);

		int best = int.max;
		int bestX = -1;
		int bestY = -1;
		int num = 0;
		while (num < n - num + k)
		{
			num += 1;
		}
		foreach (val; num..n + 1)
		{
			auto x = b[val - num];
			auto y = b[val - 1];
			if (best > y - x)
			{
				best = y - x;
				bestX = x;
				bestY = y;
			}
		}

		int [] [] answer;
		int pos = 0;
		while (answer.length.to !(int) < k - 1)
		{
			answer ~= [pos + 1];
			int balance = 0;
			while (balance <= 0)
			{
				balance += (bestX <= a[pos] &&
				    a[pos] <= bestY) ? +1 : -1;
				pos += 1;
			}
			answer.back ~= pos;
		}
		answer ~= [pos + 1, n];

		writeln (bestX, " ", bestY);
		foreach (const ref line; answer)
		{
			writefln !("%(%s %)") (line);
		}
	}
}

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

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

void solve() {
    int N, K; readf("%d %d\n", &N, &K);
    auto A = readarray!int;

    int[] C = new int[N + 2];
    foreach (a; A) C[a]++;

    int[] S = new int[N + 3];
    for (int i = 0; i <= N+1; i++) {
        S[i + 1] = S[i] + C[i];
    }

    int cd = int.max;
    int[] range = [-1, -1];
    for (int x = 0; x <= N; x++) {
        int lb = x, ub = N + 1;
        bool check(int m) {
            return S[m] - S[x] >= K + (N - K + 1) / 2;
        }
        if (! check(ub)) continue;
        while (lb + 1 < ub) {
            int mid = (lb + ub) / 2;
            (check(mid) ? ub : lb) = mid;
        }
        int y = ub - 1;
        if (y - x < cd) {
            cd = y - x;
            range = [x, y];
        }
    }

    int x = range[0], y = range[1];
    assert(x >= 0 && y >= 0 && x <= y);

    Array!int ins, outs;
    int[] ans;
    for (int i = 0; i < N; i++) {
        int a = A[i];
        if (x <= a && a <= y) {
            if (outs.empty) {
                ins ~= i;
            } else {
                outs.removeBack;
            }
        } else {
            if (ins.empty) {
                outs ~= i;
            } else {
                ins.removeBack;
            }
        }
        if (ins.length == 1) {
            ins.removeBack;
            ans ~= i;
            if (ans.length == K) break;
        }
    }
    writefln("%s %s", x, y);
    //writeln(ans);

    for (int k = 0; k < K; k++) {
        int L, R;
        if (k == 0) {
            L = 0;
            R = ans[k];
        } else if (k == K - 1) {
            L = ans[k - 1] + 1;
            R = N - 1;
        } else {
            L = ans[k - 1] + 1;
            R = ans[k];
        }
        writefln("%s %s", L+1, R+1);
    }

}

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


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

alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void solve(){
    int n;
    n = rd!int;
    ll[] arr = rdarr;
    arr.sort();
    ll[] res = new ll[](n);
    foreach(i; 0..n/2){
        res[2*i + 1] = arr[i];
    }
    foreach(i;(n/2)..n){
        res[2*(i - n/2)] = arr[i];
    }
    ll num = 0;
    foreach(i; 1..n-1){
        if(res[i] < res[i-1] && res[i] < res[i+1]){
            ++num;
        }
    }
    writeln(num);
    foreach(el; res){
        write(el, " ");
    }
    writeln;
}

void main(){
    long t = 1;
    /* t = rd; */
    while(t--) solve();
    stdout.flush;
}

// Vicfred
// https://codeforces.com/contest/1419/problem/D1
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.container;
import std.stdio;
import std.string;

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

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

    auto dlist = DList!long(a);

    long[] solution;

    for(int i = 0; i < n; i++) {
        if(i % 2 == 0) {
            solution ~= dlist.back;
            dlist.removeBack;
        } else {
            solution ~= dlist.front;
            dlist.removeFront;
        }
    }

    int ans = 0;
    for(int i = 1; i + 1 < n; i++) {
        if(solution[i] < solution[i - 1] &&
           solution[i] < solution[i + 1])
            ans += 1;
    }
    ans.writeln;
    foreach(item; solution)
        writef("%s ", item);
    "".writeln;
}

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

void main()
{
    auto N = readln.chomp.to!int;
    auto AS = readln.split.to!(int[]);
    sort!"a > b"(AS);

    auto BS = new int[](N);
    int i, j;
    while (j < N) {
        BS[j] = AS[i];
        ++i;
        j += 2;
    }
    foreach (k; 0..N) if (BS[k] == 0) BS[k] = AS[i++];
    if (N%2 == 0) {
        int k = 1;
        while (k < N-1) {
            if (BS[k-1] > BS[k] && BS[k+1] > BS[k]) {
                k += 2;
                continue;
            }
            BS[$-1] = BS[k];
            BS[k] = AS[i-1];
            goto end;
        }
    }
    end:

    int r;
    foreach (k; 1..N-1) if (BS[k-1] > BS[k] && BS[k+1] > BS[k]) ++r;
    writeln(r);
    writeln(BS.to!(string[]).join(" "));
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto n = RD!int;
	auto a = RDA;
	a.sort();

	long[] ans;
	debug writeln(a);
	foreach (i; 0..n)
	{
		if (i % 2 == 0)
		{
			ans ~= a.back; a.popBack;
		}
		else
		{
			ans ~= a.front; a.popFront;
		}
		debug writeln(a);
	}

	writeln((ans.length-1) / 2);
	ans.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}

// Vicfred
// https://codeforces.com/contest/1419/problem/D1
// greedy
import std.algorithm;
import std.array;
import std.conv;
import std.container;
import std.stdio;
import std.string;

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

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

    auto dlist = DList!long(a);

    long[] solution;

    for(int i = 0; i < n; i++) {
        if(i % 2 == 0) {
            solution ~= dlist.back;
            dlist.removeBack;
        } else {
            solution ~= dlist.front;
            dlist.removeFront;
        }
    }

    (n/2).writeln;
    foreach(item; solution) {
        writef("%s ", item);
    } "".writeln;
}


import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;
import std.algorithm, std.array, std.ascii, std.base64, std.bigint, std.bitmanip, std.compiler, std.complex, std.concurrency, std.container, std.conv, std.csv, std.datetime, std.demangle, std.digest, std.encoding, std.exception, std.file, std.format, std.functional, std.getopt, std.json, std.math, std.mathspecial, std.meta, std.mmfile, std.numeric, std.parallelism, std.path, std.process, std.random, std.range, std.regex, std.signals, std.stdint, std.stdio, std.string, std.system, std.traits, std.typecons, std.uni, std.uri, std.utf, std.uuid, std.variant, std.zip, std.zlib;

const long MAXN = 200000;
const long m = 1000000007;

void main()
{
  long[] factorial;
  factorial ~= 1;
  factorial ~= 1;
  factorial ~= 1;
  for (int i = 3; i <= MAXN; i++)
    factorial ~= factorial[i - 1] * i % m;

  long t;
  readf(" %d ", &t);
  while (t--) {
    int n;
    readf(" %d ", &n);
    writeln(factorial[2 * n]);
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti] = 1;
		foreach (i; 2..n*2)
			ans[ti].modm(i+1);
	}

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


import std.stdio;
import std.algorithm.comparison;

__gshared int[100_100] lastone;
__gshared long[100_100] cumsum;
__gshared int[100_100] arr;

long solvit(int idx) {
    if (lastone[idx] == -1) return 0;
    if (lastone[idx] == 0) return cumsum[0];
    return max(cumsum[lastone[idx] - 1],
               arr[lastone[idx]] + solvit(lastone[idx] - 1));
}

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

    char[100_100] bin;
    long binrep = 0, aml = 0;
    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        readf(" %s", &arr[i]);
    }
    char[] pbin = bin;
    string dummy = readln;
    readln(pbin);
    cumsum[0] = arr[0];
    if (bin[0] == '1') lastone[0] = 0;
    else lastone[0] = -1;
    foreach (i; 1 .. n) {
        cumsum[i] = arr[i] + cumsum[i-1];
        if (bin[i] == '1') lastone[i] = i;
        else lastone[i] = lastone[i-1];
    }
    writeln(solvit(n - 1));

    return 0;
}

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

void solve(int[] a, int[] s, int n)
{
    auto acc = new int[n];
    acc[0] = a[0];
    foreach (i; 1 .. n)
    {
        acc[i] = acc[i - 1] + a[i];
    }
    auto ans = 0, sum = 0;
    foreach (i; 0 .. n)
    {
        if (s[i] == 1)
        {
            if (n - i - 2 >= 0)
            {
                auto val = sum + acc[n - i - 2];
                ans = max(ans, val);
            }
            sum += a[n - i - 1];
        }
    }
    ans = max(ans, sum);
    writeln(ans);
}

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

import std.stdio;
import std.algorithm.comparison;

__gshared int[100_100] lastone;
__gshared long[100_100] cumsum;
__gshared int[100_100] arr;

long solvit(int idx) {
    if (lastone[idx] == -1) return 0;
    if (lastone[idx] == 0) return cumsum[0];
    return max(cumsum[lastone[idx] - 1],
               arr[lastone[idx]] + solvit(lastone[idx] - 1));
}

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

    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        readf(" %s", &arr[i]);
    }
    string dummy = readln;
    string bin = readln();
    cumsum[0] = arr[0];
    if (bin[0] == '1') lastone[0] = 0;
    else lastone[0] = -1;
    foreach (i; 1 .. n) {
        cumsum[i] = arr[i] + cumsum[i-1];
        if (bin[i] == '1') lastone[i] = i;
        else lastone[i] = lastone[i-1];
    }
    writeln(solvit(n - 1));

    return 0;
}

import std.stdio;
import std.algorithm.comparison;

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

    int[100_100] arr;
    char[100_100] bin;
    long binrep = 0, aml = 0;
    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        readf(" %s", &arr[i]);
    }
    char[] pbin = bin;
    string dummy = readln;
    readln(pbin);
    int lastone = -1;
    foreach (i; 0 .. n) {
        if (bin[i] == '1') lastone = i;
    }
    if (lastone == -1) {
        writeln("0");
        return 0;
    }
    foreach (i; 0 .. lastone) {
        if (bin[i] == '1') binrep += arr[i];
        aml += arr[i];
    }
    binrep += arr[lastone];
    writeln(max(binrep, aml));

    return 0;
}


import std.algorithm;
import std.stdio;
import std.bigint;
import std.container;
import std.functional;
import std.math;
import std.complex;
import std.range;
import std.string;

alias RedBlackTree!(int) IntSet;

void main() {
    int n, m;
    int [] a, amt;
    while (readf(" %d", &n)) {
        a = new int[](n);
        amt = new int[](n);
        amt[0..n] = 0;
        IntSet S = new IntSet(-1, -2);
        foreach(i; 0..n) {
            readf(" %d", &a[i]);
            S.insert(i);
        }
        readf(" %d", &m);
        foreach(i ; 0..m) {
            int t;
            readf(" %d", &t);
            if (t == 1) {
                int p, x;
                readf(" %d %d", &p, &x);
                --p;
                int f = min(x, a[p] - amt[p]);
                x -= f;
                amt[p] += f;
                if (amt[p] == a[p]) S.removeKey(p);
                if (x > 0) {
                    auto next = S.upperBound(p);
                    while (!next.empty() && x > 0) {
                        f = min(x, a[next.front()] - amt[next.front()]);
                        x -= f;
                        amt[next.front()] += f;
                        if (amt[next.front()] == a[next.front()]) S.removeKey(next.front());
                        next = S.upperBound(next.front());
                    }
                }
            } else {
                int k;
                readf(" %d", &k);
                writeln(amt[--k]);
            }
        }
    }
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s", &a[i]);
		}
		auto next = n.iota.map !(a => a + 1).array;
		a ~= 0;
		next ~= n;
		auto b = new int [n + 1];
		b[] = 0;
		int m;
		readf (" %s", &m);
		foreach (j; 0..m)
		{
			int t;
			readf (" %s", &t);
			if (t == 1)
			{
				int p, x;
				readf (" %s %s", &p, &x);
				p--;
				b[p] += x;
				while (p < n && b[p] > a[p])
				{
					b[next[p]] += b[p] - a[p];
					int q = next[p];
					if (b[next[p]] >= a[next[p]])
					{
						next[p] = next[next[p]];
					}
					b[p] = a[p];
					p = q;
				}
			}
			else if (t == 2)
			{
				int k;
				readf (" %s", &k);
				k--;
				writeln (b[k]);
			}
			else
			{
				enforce (false);
			}
		}
	}
}

import std.algorithm;
import std.stdio;
import std.bigint;
import std.container;
import std.functional;
import std.math;
import core.memory;
import std.complex;
import std.range;
import std.string;

alias RedBlackTree!(int) IntSet;

void main() {
    int n, m;
    int [] a, amt;
    while (readf(" %d", &n)) {
        a = new int[](n);
        amt = new int[](n);
        amt[0..n] = 0;
        IntSet S = new IntSet(-1, -2);
        foreach(i; 0..n) {
            readf(" %d", &a[i]);
            S.insert(i);
        }
        readf(" %d", &m);
        foreach(i ; 0..m) {
            int t;
            readf(" %d", &t);
            if (t == 1) {
                int p, x;
                readf(" %d %d", &p, &x);
                --p;
                int f = min(x, a[p] - amt[p]);
                x -= f;
                amt[p] += f;
                if (amt[p] == a[p]) S.removeKey(p);
                if (x > 0) {
                    auto next = S.upperBound(p);
                    foreach (j; next) {
                        if (x == 0) break;
                        f = min(x, a[j] - amt[j]);
                        x -= f;
                        amt[j] += f;
                        if (amt[j] == a[j]) S.removeKey(j);
                    }
                }
            } else {
                int k;
                readf(" %d", &k);
                --k;
                writeln(amt[k]);
            }
        }
    }
}

import std.algorithm;
import std.stdio;
import std.bigint;
import std.container;
import std.functional;
import std.math;
import std.complex;
import std.range;
import std.string;

alias RedBlackTree!(int) IntSet;

void main() {
    int n, m;
    int [] a, amt;
    while (readf(" %d", &n)) {
        a = new int[](n);
        amt = new int[](n);
        IntSet S = new IntSet;
        S.insert(-1);
        S.insert(-2);
        foreach(i; 0..n) {
            readf(" %d", &a[i]);
            S.insert(i);
        }
        readf(" %d", &m);
        foreach(i ; 0..m) {
            int t;
            readf(" %d", &t);
            if (t == 1) {
                int p, x;
                readf(" %d %d", &p, &x);
                --p;
                auto next = S.upperBound(p);
                int f = min(x, a[p] - amt[p]);
                x -= f;
                amt[p] += f;
                if (amt[p] == a[p] && !S.empty()) S.removeKey(p);
                if (x > 0) {
                    foreach (j; next) {
                        if (x == 0) break;
                        f = min(x, a[j] - amt[j]);
                        x -= f;
                        amt[j] += f;
                        if (amt[j] == a[j]) S.removeKey(j);
                    }
                }
            } else {
                int p;
                readf(" %d", &p);
                --p;
                writeln(amt[p]);
            }
        }
    }
}


module main;
__gshared:
import core.bitop, std.algorithm, std.bigint, std.bitmanip, std.complex,
	std.container, std.conv, std.datetime, std.functional, std.math, std.meta;
import std.numeric : Fft, fft, gcd, inverseFft;
import std.parallelism, std.random, std.range, std.regex, std.stdio, std.string,
	std.traits, std.typecons, std.uni, std.variant;

alias pair(X, Y) = Tuple!(X, "fi", Y, "se");
alias mp = make_pair;
alias pii = pair!(int, int);
alias pll = pair!(long, long);
alias gcd = std.numeric.gcd;
alias lint = long;
alias rbt = RedBlackTree;
///modulo
enum mod = 10 ^^ 9 + 7, mod2 = mod + 2;
public
{
	pure nothrow
	{
		///lcm
		T lcm(T)(auto ref T a, auto ref T b)
		{
			return a / gcd(a, b) * b;
		}
		///make_pair
		pair!(X, Y) make_pair(X, Y)(in X x_, in Y y_)
		{
			return tuple!(X, "fi", Y, "se")(x_, y_);
		}
		///BigInt greatest common divizor
		BigInt gcd(BigInt a, BigInt b)
		{
			while (b)
			{
				a %= b;
				swap(a, b);
			}
			return a;
		}
		///square
		@property X sqr(X)(in X a_)
		{
			return a_ * a_;
		}
		///cube
		@property X cub(X)(in X a_)
		{
			return a_ * a_ * a_;
		}
		///posicion lower bound
		size_t lowb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			size_t l, r = a.length;
			while (r - l > 1)
			{
				auto m = (l + r) >> 1;
				if (!(a[m] < g))
					r = m;
				else
					l = m;
			}
			return (!(a[l] < g)) ? l : r;
		}
		///posicion upper bound
		size_t upb(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			size_t l, r = a.length;
			while (r - l > 1)
			{
				auto m = (l + r) >> 1;
				if (g < a[m])
					r = m;
				else
					l = m;
			}
			return (g < a[l]) ? l : r;
		}
		///posicion binary search
		size_t binf(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			auto pos = lowb(a, g);
			return (g == a[pos]) ? pos : a.length;
		}
		///binary search
		bool binary_search(T, X)(T a, auto ref X g)
				if (isRandomAccessRange!T && hasLength!T && is(typeof(g < a[0]) == bool))
		{
			return binf(a, g) != a.length;
		}
	}
	///write an array
	void putarr(X)(in X a, in char ch = ' ') if (isInputRange!X)
	{
		writefln("%(%s" ~ ch ~ "%)", a);
	}
	///write a matrix
	void putarr(X)(in X a)
			if (isInputRange!X && isInputRange!(ElementType!X) && !isSomeString!(ElementType!X))
	{
		writefln("%(%(%s %)\n%)", a);
	}
	///get an array
	bool getarr(X)(X a, in size_t n)
	{
		bool b = 1;
		foreach (ref i; a[0 .. n])
			b = input(&i);
		return b;
	}
	///read without format
	bool input(T...)(ref T ptrs) if (ptrs.length > 0)
	{
		size_t s;
		foreach (ref e; ptrs)
		{
			if (isSomeChar!(Unqual!(typeof(e))))
				s += readf(" %c", &e);
			else
				s += readf(" %s", &e);
		}
		return s == ptrs.length;
	}
	///readln without format
	bool read(T...)(ref T ptrs) if (ptrs.length > 0)
	{
		size_t s;
		foreach (ref e; ptrs)
		{
			if (isSomeChar!(Unqual!(typeof(e))))
				s += readf(" %c", &e);
			else
				s += readf(" %s", &e);
		}
		readln;
		return s == ptrs.length;
	}
	///opening file
	nothrow auto inf(in char* name)
	{
		return freopen(name, "r", core.stdc.stdio.stdin);
	}
	///closing file
	nothrow auto ouf(in char* name)
	{
		return freopen(name, "w", core.stdc.stdio.stdout);
	}
	///parsing array from line
	@property auto arread(T)()
	{
		return readln.splitter.map!(to!(T)).array;
	}
	///outbuffer
	void enter(T...)(T args)
	{
		writeln(args);
		stdout.flush;
	}
}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	int n, p, q, r;
	loop: while (read(n, p, q, r))
	{
		auto a = arread!int;
		auto mapr = a.cumulativeFold!(max).array, masu = a.retro.cumulativeFold!(max).array;
		auto mipr = a.cumulativeFold!(min).array, misu = a.retro.cumulativeFold!(min).array;
		reverse(misu);
		reverse(masu);
		debug
		{
			writeln(a);
			writeln(mipr);
			writeln(mapr);
			writeln(misu);
			writeln(masu);
		}
		long ans = long.min;
		foreach (i; 0 .. n)
		{
			long x = q * 1L * a[i];
			if (p < 0)
				x += mipr[i] * 1L * p;
			else
				x += mapr[i] * 1L * p;
			if (r < 0)
				x += misu[i] * 1L * r;
			else
				x += masu[i] * 1L * r;
			ans = max(ans, x);
		}
		writeln(ans);
	}
}

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();
      const P = readLong();
      const Q = readLong();
      const R = readLong();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }

      auto ls = new long[N + 1];
      auto rs = new long[N + 1];
      ls[0] = long.min;
      foreach (i; 0 .. N) {
        ls[i + 1] = max(ls[i], P * A[i]);
      }
      rs[N] = long.min;
      foreach_reverse (i; 0 .. N) {
        rs[i] = max(R * A[i], rs[i + 1]);
      }

      long ans = long.min;
      foreach (i; 0 .. N) {
        chmax(ans, ls[i + 1] + Q * A[i] + rs[i]);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        int n;
        scanf("%d", &n);
        getchar();
        long res;
        auto s = readln.strip.to!(char[]);
        bool[char] solved;
        foreach(p; s) {
            auto tmp = (p in solved);
            if (tmp is null) {
                res += 2;
                solved[p] = true;
            }
            else {
                res += 1;
            }
        }
        writeln(res);
    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;
		auto t = s.dup;
		writeln (s.length + t.byChar.sort.uniq.walkLength);
	}
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		int k = (n + 1) / 2;
		int [] ans;
		foreach (i; 0..n)
		{
			foreach (j; i + 1..n)
			{
				int d = j - i;
				ans ~= (d < k ? 1 : n - d < k ? -1 : 0);
			}
		}
		writefln !("%(%s %)") (ans);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		if (n % 2)
		{
			foreach (i; 0..n*(n-1)/2)
			{
				if (i % 2 == 0)
					ans[ti] ~= 1;
				else
					ans[ti] ~= -1;
			}
		}
		else
		{
			auto draw = n-1;
			foreach (i; 0..n)
			{
				foreach (j; i+1..n)
				{
					auto x = i + j;
					if (j > draw)
						++x;
					if (j == draw)
						ans[ti] ~= 0;
					else if (x % 2)
						ans[ti] ~= 1;
					else
						ans[ti] ~= -1;

				}
				--draw;
			}
		}
	}

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


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto b = iota (n * m).map !(i => tuple (a[i], i)).array;
		sort (b);
		foreach (row; 0..n)
		{
			b[row * m..row * m + m]
			    .schwartzSort !(c => tuple (c[0], -c[1]));
		}
//		b.schwartzSort !(c => tuple (c[0], c[1]));
		debug {writeln (b);}

		long res = 0;
		foreach (row; 0..n)
		{
			auto d = b[row * m..row * m + m];
			foreach (i; 0..m)
			{
				foreach (j; i + 1..m)
				{
					res += (d[i][1] < d[j][1]);
				}
			}
		}
		writeln (res);
	}
}

immutable multi = true;

void solve(int)
{
	auto n = readInt!int;
	auto m = readInt!int;
	auto a = ma(n*m, readInt!int);
	int[2][][int] poss;
	int[int] cnt;
	foreach(i, ai; a) cnt[ai]++;
	auto sa = a.dup; sort(sa);
	auto ua = sa.uniq.array;
	foreach(i; 0 .. n)
	{
		foreach(j; 0 .. m)
		{
			assert(cnt[ua[0]] > 0);
			require(poss, ua[0], null) ~= [i, -j];
			cnt[ua[0]]--;
			if (cnt[ua[0]] == 0) ua = ua[1 .. $];
		}
	}
	auto used = new FenwickTree[](n);
	foreach(ref usedi; used)
		usedi = FenwickTree(m);
	long ans = 0;

	foreach(ai, ref pos; poss)
		sort(pos);
	debug writeln(poss);
	foreach(i, ai; a)
	{
		auto pos = poss[ai][0];
		poss[ai] = poss[ai][1 .. $];
		auto pi = pos[0];
		auto pj = -pos[1];
		ans += used[pi].sum(pj-1);
		used[pi].add(pj, 1);
	}
	ans.writeln;
}
struct FenwickTree {
	int[] bit;
	int n;

	this(int n) {
		this.n = n;
		bit = new int[](n);
	}

	int sum(int r) {
		int ret = 0;
		for (; r >= 0; r = (r & (r + 1)) - 1)
			ret += bit[r];
		return ret;
	}

	int sum(int l, int r) {
		return sum(r) - sum(l - 1);
	}

	void add(int idx, int delta) {
		for (; idx < n; idx = idx | (idx + 1))
			bit[idx] += delta;
	}
}
//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

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

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

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

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


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

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

int main() {
    Scanner sc = new Scanner(stdin);
    scope(exit) assert(!sc.hasNext);

    int n; long S;
    sc.read(n, S);

    long[3][] av, bv;
    long ma = 0;
    long ssm, sasm, sbsm;
    foreach (i; 0..n) {
        long s, a, b;
        sc.read(s, a, b);
        ssm += s;
        ma += s * max(a, b);
        if (a >= b) {
            sasm += s;
            av ~= [s, a, b];
        } else {
            sbsm += s;
            bv ~= [s, a, b];
        }
    }

    if (sasm % S + sbsm % S > S) {
        writeln(ma);
        return 0;
    }

    sasm %= S; sbsm %= S;
    av.sort!"a[1]-a[2]<b[1]-b[2]";
    bv.sort!"a[2]-a[1]<b[2]-b[1]";

    long[3][] mid;
    foreach (i; 0..sasm) {
        if (av[0][0] == 0) {
            av = av[1..$];
        }
        ma -= av[0][1];
        mid ~= [1L, av[0][1], av[0][2]];
        av[0][0]--;
    }

    foreach (i; 0..sbsm) {
        if (bv[0][0] == 0) {
            bv = bv[1..$];
        }
        ma -= bv[0][2];
        mid ~= [1L, bv[0][1], bv[0][2]];
        bv[0][0]--;
    }

    long m1, m2;
    foreach (p; mid) {
        m1 += p[1];
        m2 += p[2];
    }

    writeln(ma + max(m1, m2));
    return 0;
}
/* IMPORT /home/yosupo/Program/dunkelheit/source/dkh/container/stackpayload.d */
// module dkh.container.stackpayload;

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

    private T* _data;
    private uint len, cap;

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

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

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

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

    void clear() {
        len = 0;
    }

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

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

// module dkh.foundation;

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

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

// import dkh.container.stackpayload;

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

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

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

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

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

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();
      const R = readLong();
      auto S = new long[N];
      auto A = new long[N];
      auto B = new long[N];
      foreach (i; 0 .. N) {
        S[i] = readLong();
        A[i] = readLong();
        B[i] = readLong();
      }

      long sumS, sumS0, sumS1;
      long ansBase;
      alias Entry = Tuple!(long, "d", long, "s");
      Entry[] es0, es1;
      foreach (i; 0 .. N) {
        sumS += S[i];
        if (A[i] >= B[i]) {
          sumS0 += S[i];
          ansBase += S[i] * A[i];
          es0 ~= Entry(A[i] - B[i], S[i]);
        } else {
          sumS1 += S[i];
          ansBase += S[i] * B[i];
          es1 ~= Entry(B[i] - A[i], S[i]);
        }
      }
      es0.sort;
      es1.sort;

      const k = (sumS + R - 1) / R;
      long ans;
      foreach (l; sumS0 / R - 2 .. sumS0 / R + 2 + 1) {
        if (0 <= l && l <= k) {
          long score = ansBase;
          {
            long lot = max(sumS0 - R * l, 0);
            foreach (ref e; es0) {
              const tmp = min(e.s, lot);
              score -= e.d * tmp;
              lot -= tmp;
            }
          }
          {
            long lot = max(sumS1 - R * (k - l), 0);
            foreach (ref e; es1) {
              const tmp = min(e.s, lot);
              score -= e.d * tmp;
              lot -= tmp;
            }
          }
          debug {
            writeln(l, " ", k - l, ": ", score);
          }
          chmax(ans, score);
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


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

void main()
{
  int n, q;
  readf!" %d %d "(n, q);
  string s = readln.strip;
  ulong[] a;
  ulong sum = 0;
  a ~= 0;
  foreach (ch ; s) {
    sum += ch - 'a' + 1;
    a ~= sum;
  }
  while (q--) {
    int l, r;
    readf!" %d %d "(l, r);
    writeln(a[r] - a[l - 1]);
  }
}

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

void main() {
    int n, q;
    readf("%s %s\n", &n, &q);
    string s = readln.strip;

    int[][] index = new int[][](26, n + 1);
    foreach(ch; 0 .. 26) {
        int count = 0;
        foreach(i; 0 .. n) {
            if(s[i] == 'a' + ch) {
                count += 1;
            }
            index[ch][i + 1] = count;
        }
    }

    for(int i = 0; i < q; ++i) {
        int l, r;
        readf("%s %s\n", &l, &r);
        int ans = 0;
        foreach(ch; 0 .. 26) {
            int letter = index[ch][r] - index[ch][l - 1];
            ans += letter * (ch + 1);
        }
        ans.writeln;
    }
}

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

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

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

void main() {
  int n, q;
  read(n, q);

  string s = readln;

  auto table = new int[][](26, n + 1);

  foreach (c; 0 .. 26) {
    int t = 0;
    foreach (i; 0 .. n) {
      if (s[i] == 'a' + c) t++;
      table[c][i + 1] = t;
    }
  }

  foreach (i; 0 .. q) {
    int l, r;
    read(l, r);

    long total = 0;

    foreach (c; 0 .. 26) {
      auto rep = table[c][r] - table[c][l - 1];
      total += rep * (c + 1);
    }

    writeln(total);
  }
}


import std.typecons;
import std.stdio;
T gcd(T)(T a, T b) {
	if (a % b == 0) return b;
	return gcd(b, a % b);
}
struct Rat(T) {
	import std.math : abs;
	T n, d;
	this(T n_, T d_) {
		if (d_ == 0) {
			n = 1;
			d = 0;
		} else if (n_ == 0) {
			d = 1;
			n = 0;
		} else {
			auto c = gcd(abs(n_), abs(d_));
			bool neg = (n_ < 0) ^ (d_ < 0);
			n = abs(n_ / c);
			d = abs(d_ / c);
			if (neg) {
				n = -n;
			}
		}
	}
	Rat!T opBinary(string op)(T o) if (op == "*"){
		if (d == 0) {
			return this;
		}
		return Rat!T(n * o, d);
	}
	Rat!T opBinary(string op)(T o) if (op == "/"){
		if (d == 0) {
			return this;
		}
		return Rat!T(n, d * o);
	}
	Rat!T opBinary(string op)(T o) if (op == "+"){
		if (d == 0) {
			return this;
		}
		return Rat!T(n + d * o, d);
	}
	bool opEqual(Rat!T o) {
		return n == o.n && d == o.d;
	}
}
int[2][50] points;
void main() {
	bool[Tuple!(Rat!long, Rat!long)] lines;
	int n;
	scanf("%d ", &n);

	foreach(i; 0..n) {
		scanf("%d %d ", &points[i][0], &points[i][1]);
	}

	debug writeln(points);

	foreach(i; 0..n) {
		foreach(j; 0..i) {
			auto t = Rat!long(points[i][1]-points[j][1], points[i][0]-points[j][0]);
			auto d = t.d ? (t * -points[i][0]) + points[i][1] : Rat!long(points[i][0], 1);
			debug writeln(t, d, t == d);
			lines[tuple(t, d)] = true;
		}
	}

	debug writeln(lines);
	int[Rat!long] tgroup;
	foreach(t, d; lines.byKey) {
		tgroup[t]++;
	}
	long ans = lines.length * (lines.length - 1) / 2;
	foreach(t; tgroup.byKey) {
		ans -= tgroup[t] * (tgroup[t]-1) / 2;
	}
	writeln(ans);
}

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

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

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

	int n = read.to!int;
	long[] xs, ys;
	foreach(i; 0 .. n) xs ~= read.to!int, ys ~= read.to!int;

	print!1("xs:", xs);
	print!1("ys:", ys);

	long[long] kcnt;
	bool[long] mset;
	foreach(i; 0 .. n) foreach(j; 0 .. i){
		long dx = xs[i] - xs[j];
		long dy = ys[i] - ys[j];
		long c = dx * ys[i] - dy * xs[i];
		print!1("dx:", dx, " dy:", dy, " c:", c);

		if(dx == 0){
			if(dy < 0) dy = -dy, c = -c;
			long g = gcd(dy, c);
			dy /= g, c /= g;
		}
		else if(dy == 0){
			if(dx < 0) dx = -dx, c = -c;
			long g = gcd(dx, c);
			dx /= g, c /= g;
		}
		else if(c == 0){
			if(dx < 0) dx = -dx, dy = -dy;
			long g = gcd(dx, dy);
			dx /= g, dy /= g;
		}
		else{
			if(dx < 0) dx = -dx, dy = -dy, c = -c;
			long g = gcd(gcd(dx, dy), c);
			dx /= g, dy /= g, c /= g;
		}

		print!1(" -> dx:", dx, " dy:", dy, " c:", c);

		long k = 100000 * dy + dx;
		long m = k * 10000000 + c;
		if(m !in mset){
			if(k !in kcnt) kcnt[k] = 0;
			mset[m] = 1;
			kcnt[k] += 1;
		}
	}
	print!1("kcnt:", kcnt);
	print!1("mset:", mset);

	long[] ks = kcnt.keys;
	long[] ms = mset.keys;
	long ans = ms.length * (ms.length - 1) / 2;
	foreach(k; ks) ans -= kcnt[k] * (kcnt[k] - 1) / 2;
	ans.writeln;

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

/*

"How many different direction vectors can we have?"

(x1, y1), (x2, y2) -> (x1 - x2, y1 - y2).
here x1 - x2 and y1 - y2 are relatively prime and x1 - x2 > 0,
or (1, 0), or (0, 1).

O(N^2) = 1000 x 1000.

(y1 - y2)(x - x1) - (x1 - x2)(y - y1) = 0
dy x - dx y + dx y1 - dy x1 = 0

*/

void main() {
import std.stdio;
writeln(long.sizeof);
}