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

long solve(int[] a, bool tie)
{
    int even_cnt = cast(int)a.count(0);
    int odd_cnt = cast(int)a.length - even_cnt;

    if (abs(even_cnt - odd_cnt) > 1)
      return -1;

    int odd_i, even_i;
    int[] arr;
    if (odd_cnt == even_cnt) {
      if (tie)
        odd_i = 1;
      else
        even_i = 1;
    } else if (odd_cnt < even_cnt) {
      odd_i = 1;
    } else {
      even_i = 1;
    }

    foreach (x ; a) {
      if (x) {
        arr ~= odd_i;
        odd_i += 2;
      } else {
        arr ~= even_i;
        even_i += 2;
      }
    }
    long ans = 0;
    foreach (i ; 0 .. cast(int)a.length) {
      ans += abs(arr[i] - i);
    }
    assert(ans % 2 == 0);
    return ans / 2;
}

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readln.formattedRead!" %d "(n);
    auto a = readln.strip.split.map!(x => x.to!int % 2).array;
    writeln(min(solve(a, true), solve(a, false)));
  }
}

immutable multi = true;

void solve(int tc)
{
	int n = readInt!int;
	auto a = ma(n, readInt!int%2);
	auto m1 = n/2;
	auto m2 = n - m1;
	int fe = void;
	int[2] c;
	c[0] = cast(int)a.count!(ai => ai == 0);
	c[1] = n - c[0];
	if (c[0] != m2 && c[1] != m2) return writeln(-1);
	long allOps = long.max;
	void tryFe(int fe)
	{
	        if (c[fe] != m2) return;
		int[] lfe = null;
		foreach(i, ai; a)
		{
			if (ai == fe)
			{
				lfe ~= cast(int)i;
			}
		}
		debug writeln("lfe ", lfe);
		long ops = 0;
		foreach(i; 0 .. n)
		{
			if (i%2 == 0)
			{
				assert(lfe.length > 0);
				ops += abs(lfe[0] - i);
				lfe = lfe[1 .. $];
			}
		}
		allOps = min(allOps, ops);
	}
	tryFe(0); tryFe(1);
	allOps.writeln;
}

//{{{
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;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

long solve (int [] a, int pos)
{
	auto n = a.length.to !(int);
	auto half = (n + 1 - pos) / 2;
	auto total = a.sum;
	if (total != half)
	{
		return long.max;
	}
	long res = 0;
	foreach (int i, ref c; a)
	{
		if (c & 1)
		{
			res += abs (i - pos);
			pos += 2;
		}
	}
	return res;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		a[] %= 2;
		auto res = long.max;
		res = min (res, solve (a, 0));
		res = min (res, solve (a, 1));
		if (res == long.max)
		{
			res = -1;
		}
		writeln (res);
	}
}


import core.bitop;
import core.checkedint;
//import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.length;
}

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

int[10^^4] p;

void main() {
    int n, m;
    while (read(n, m)) {
        foreach (ref x; p[0 .. n])
            read(x);
        while (m--) {
            int left, right, pos;
            read(left, right, pos);
            left--;
            const x = p[pos - 1];
            const lte = p[left .. right].filter!(y => y <= x).walkLength();
            writeln(pos == left + lte ? "Yes" : "No");
        }
        debug writeln();
    }
}

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

void main(){
    int N, M, l, r, x;

    readVars(N, M);
    auto p = readln.split.to!(int[]);

    int cnt;

    foreach(i ; 0 .. M){
        readVars(l, r, x);
        l--;
        x--;

        cnt = 0;

        foreach(j ; l .. r){
            cnt += (p[j] < p[x]);
        }

        if (cnt == x - l) {
            writeln("Yes");
        } else {
            writeln("No");
        }
    }
}

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


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 n = readln.strip.to!int;
    Tuple!(long, long)[] a;
    foreach (i ; 0 .. n) {
      long ai, bi;
      readf!" %d %d "(ai, bi);
      a ~= tuple(min(ai, bi), max(ai, bi));
    }
    a.sort!((x, y) => x[1] < y[1]);
    long ans = a[0][0] * 2 + a[0][1];
    foreach (i ; 1 .. a.length) {
      ans += a[i][0] * 2 + (a[i][1] - a[i - 1][1]);
    }
    ans += a.back[1];
    writeln(ans);
  }
}

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

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

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 solve() {
    int n = read!int;
    long a, b, maxV, res;
    foreach(i; 0 .. n) {
        scanf("%lld %lld\n", &a, &b);
        res += min(a,b);
        if (max(a,b) > maxV)
            maxV = max(a,b);
    }
    res <<= 1;
    maxV <<= 1;
    writeln(res + maxV);
}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		long res = 0;
		long hi = 0;
		foreach (i; 0..n)
		{
			long a, b;
			readf !(" %s %s") (a, b);
			if (a > b)
			{
				swap (a, b);
			}
			res += a * 2;
			hi = max (hi, b);
		}
		readln;
		writeln (res + hi * 2);
	}
}


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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    auto s = readln.strip;
    long ans = 0;
    char last = 0;
    foreach (ch ; s) {
      if (last != ch) {
        ans += (ch == '0');
      }
      last = ch;
    }
    writeln(min(2, ans));
  }
}

immutable multi = true;

void solve(int)
{
	auto s = readString;
	int i = 0;
	int[2] cnt;
	while (i < s.length)
	{
		char ch = s[i];
		while (i+1 < s.length && s[i+1] == ch) i++;
		cnt[ch-'0']++;
		i++;
	}
	if (cnt[0] == 0)
	{
		return writeln(0);
	}
	if (cnt[1] == 0)
	{
		return writeln(1);
	}
	if (cnt[0] == 1)
	{
		return writeln(1);
	}
	return writeln(2);
}

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

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

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

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

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

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

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(i; 0..t){
	auto str = readln.strip();
	string result;
	while (str.length > 0)
	{
		result ~= str[0];
		str = str.stripLeft(str[0]);
	}
	if (result.length > 3)
		writeln(2);
	else if (result == "0")
		writeln(1);
	else if (result == "1")
		writeln(0);
	else if (result == "01" || result == "10")
		writeln(1);
	else if (result == "101")
		writeln(1);
	else
		writeln(2);
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long cnt = s[0] == '0' ? 1 : 0;
		foreach (i; 0..s.length-1)
		{
			if (s[i] == '1' && s[i+1] == '0')
				++cnt;
		}
		ans[ti] = min(cnt, 2);
	}

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

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

int solve (string s)
{
	s = s.strip ('1');
	if (s.empty)
	{
		return 0;
	}
	if (s.all !(q{a == '0'}))
	{
		return 1;
	}
	return 2;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		readln.strip.solve.writeln;
	}
}


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

immutable int NA = -1;

void main ()
{
	int n;
	int k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto c = readln.split.map !(to !(int)).array;
		int [] p;
		int [] q;
		int [] r;
		for (int d = 2; d <= k; d++)
		{
			if (k % d == 0)
			{
				p ~= d;
				q ~= 0;
				r ~= 0;
				do
				{
					q[$ - 1]++;
					k /= d;
				}
				while (k % d == 0);
			}
		}
		foreach (cur; c)
		{
			foreach (i; 0..p.length)
			{
				int s = 0;
				while (cur % p[i] == 0)
				{
					cur /= p[i];
					s++;
				}
				r[i] = max (r[i], s);
			}
		}
		writeln (p.length.iota.all
		    !(x => q[x] <= r[x]) ? "Yes" : "No");
	}
}

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

immutable int MAXN = cast(int)1e6;

int[MAXN + 5] Pr, nx, cnt;

void sieve() {
	Pr[0] = Pr[1] = 0;
	foreach(i; 2..MAXN) Pr[i] = 1;
	foreach(i; 2..MAXN) if (Pr[i]) {
		for(int j = i; j <= MAXN; j += i) nx[j] = i, Pr[j] = 0;
	}
}

int N, K;

void add(int x, void function(ref int a, in int b) f) {
	while (x > 1) {
		int c = 0, t = nx[x];
		while (x % t == 0) {
			++c; x /= t;
		}
		f(cnt[t], c);
	}
}

void main() {
	sieve();
	readf("%d %d", &N, &K);
	foreach(i; 1..N+1) {
		int inp; readf(" %d", &inp);
		add(inp, function(ref int a, in int b) { a = max(a, b); });
	}
	add(K, function(ref int a, in int b) { a -= b; });
	foreach (i; cnt) if (i < 0) {
		writef("No\n"); return;
	}
	writef("Yes\n");
}


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

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

    if (n % 2 == 0) {
        writeln(-1);
        return;
    }

    foreach (_; 0 .. 2) {
        n.iota.writefln!("%(%s %)");
    }
    auto lst = (2*n).iota.stride(2).map!(x => x % n);
    lst.writefln!("%(%s %)");
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		if (n % 2 == 0)
		{
			writeln (-1);
		}
		else
		{
			auto a = array (iota (n));
			auto c = a.dup;
			c[] *= 2;
			c[] %= n;
			writefln ("%(%s %)", a);
			writefln ("%(%s %)", a);
			writefln ("%(%s %)", c);
		}
	}
}


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

void solve()
{
    readln;
    auto arr = readln.splitter.map!(to!int).array;
    auto pos = arr.count!(x => x > 0);
    auto neg = arr.count!(x => x < 0);
    auto z = arr.length - pos - neg;
    if (pos >= 3 || neg >= 3)
    {
        writeln("NO");
        return;
    }
    if (z)
    {
        arr = [0] ~ arr.filter!(x => x != 0).array;
    }
    foreach (i; 0 .. arr.length)
        foreach (j; i + 1 .. arr.length)
            foreach (k; j + 1 .. arr.length)
            {
                auto sum = arr[i] + arr[j] + arr[k];
                if (!arr.canFind(sum))
                {
                    writeln("NO");
                    return;
                }
            }
    writeln("YES");
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}

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

bool solve (int n, int [] a)
{
	sort (a);
	auto g = a.group;

	a = [];
	foreach (ref x; g)
	{
		foreach (i; 0..min (3, x[1]))
		{
			a ~= x[0];
		}
	}
	if (a.length > 10)
	{
		return false;
	}

	bool [long] b;
	foreach (ref c; a)
	{
		b[c] = true;
	}

	foreach (i; 0..a.length)
	{
		foreach (j; i + 1..a.length)
		{
			foreach (k; j + 1..a.length)
			{
				if (0L + a[i] + a[j] + a[k] !in b)
				{
					return false;
				}
			}
		}
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		writeln (solve (n, a) ? "YES" : "NO");
	}
}


import std.algorithm;
import std.array;
import std.stdio;
import std.typecons;
immutable int N = 2 ^^ 16 + 8;
int[N] deg, s;
int n;
int[] d1;
Tuple!(int, int)[] edges;
void main() {
	scanf("%d", &n);
	foreach (i; 0..n) {
		scanf("%d%d", &deg[i], &s[i]);
		if (deg[i] == 1) d1 ~= i;
	}
	while (!d1.empty()) {
		int i = d1.back(); // this and next are UFCS for std.array
		d1.popBack();
		if (deg[i] == 0) continue;
		assert(deg[i] == 1);
		deg[i] = 0;
		int j = s[i];
		deg[j] -= 1;
		if (deg[j] == 1) d1 ~= j;
		s[j] ^= i;
		edges ~= tuple(i, j);
	}
	writefln("%d", edges.length);
	foreach (t; edges) {
		writefln("%d %d", t[0], t[1]);
	}
}

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 d = new int [n];
		auto s = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &d[i], &s[i]);
		}

		int [] q;
		foreach (i; 0..n)
		{
			if (d[i] == 1)
			{
				q ~= i;
			}
		}

		int [] [] e;
		while (!q.empty)
		{
			int i = q.front;
			q.popFront ();
			if (d[i] != 1)
			{
				continue;
			}
			e ~= [i, s[i]];
			d[i]--;
			d[s[i]]--;
			if (d[s[i]] == 1)
			{
				q ~= s[i];
			}
			s[s[i]] ^= i;
			s[i] ^= s[i];
		}

		writeln (e.length);
		foreach (c; e)
		{
			writeln (c[0], ' ', c[1]);
		}
	}
}

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;

struct P {
    int v;
    int d, s;
}
struct E {
    int from, to;
}

void main() {
    int N; scanf("%d\n", &N);
    BinaryHeap!(Array!P, "a.d > b.d") PQ;
    auto ps = new P[N];
    foreach (i; 0 .. N) {
        int d, s; scanf("%d %d\n", &d, &s);
        int v = i;
        ps[i] = P(v, d, s);
        PQ.insert(P(v, d, s));
    }
    auto done = new bool[N];
    E[] ans;
    while (!PQ.empty) {
        auto p = PQ.front; PQ.removeFront;
        if (done[p.v]) continue;
        done[p.v] = true;
        if (p.d == 0) continue;
        assert(p.d == 1);
        ans ~= E(p.v, p.s);
        ps[ p.s ].d--;
        ps[ p.s ].s = ps[ p.s ].s ^ p.v;
        PQ.insert(ps[ p.s ]);
        done[p.v] = true;
    }
    writeln(ans.length);
    foreach (a; ans) {
        writefln("%d %d", a.from, a.to);
    }
}

// 1. start with a full forest
// 2. take any vertex with the current degree of 1 = V
// 3. its "s" value is the adjacent vertex number = S
// 4. add an edge (V, S)
// 5. remove V from the forest, decrease S degree, XOR s value of S with V
// 6. if vertices with non-zero degree have remained, then goto 2

import std.stdio;
import std.algorithm;

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

int main(string[] args)
{
    auto n = read!int;
    int[2][0x10000] desc;
    int[0x10000] queue; // vertices with "current" degree of 1
    int queue_n = 0;
    foreach (i; 0..n)
    {
        auto d = read!int;
        auto s = read!int;

        if (d == 1)
            queue[queue_n++] = i;

        desc[i][0] = d;
        desc[i][1] = s;
    }

    int m = 0;
    int[2][0x10000] ans;
    foreach (i; 0..n)
    {
        debug writeln("queue = ", queue[0..queue_n]);

        if (queue_n == 0)
            break;

        auto p = queue[--queue_n];

        if (desc[p][0] == 0)
            continue;

        auto j = desc[p][1];

        ans[m][0] = p;
        ans[m][1] = j;
        m++;
        desc[j][0]--;
        desc[j][1] ^= p;

        if (desc[j][0] == 1)
            queue[queue_n++] = j;
    }

    writeln(m);
    foreach (i; 0..m)
    {
        writefln("%s %s", ans[i][0], ans[i][1]);
    }

    return 0;
}

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 d = new int [n];
		auto s = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &d[i], &s[i]);
		}

		int [] q;
		foreach (i; 0..n)
		{
			if (d[i] == 1)
			{
				q ~= i;
			}
		}

		int [] [] e;
		while (!q.empty)
		{
			int i = q.front;
			q.popFront ();
			if (d[i] != 1)
			{
				continue;
			}
			e ~= [i, s[i]];
			d[i]--;
			d[s[i]]--;
			s[s[i]] ^= i;
			s[i] ^= s[i];
		}

		writeln (e.length);
		foreach (c; e)
		{
			writeln (c[0], ' ', c[1]);
		}
	}
}

import std.algorithm;
import std.array;
import std.stdio;
import std.typecons;
immutable int N = 2 ^^ 16 + 8;
int[N] deg, s;
int n;
int[] d1;
Tuple!(int, int)[] edges;
void main() {
	scanf("%d", &n);
	foreach (i; 0..n) {
		scanf("%d%d", &deg[i], &s[i]);
		if (deg[i] == 1) d1 ~= i;
	}
	while (!d1.empty()) {
		int i = d1.back(); // this and next are UFCS for std.array
		d1.popBack();
		if (deg[i] == 0) continue;
		assert(deg[i] == 1);
		deg[i] = 0;
		int j = s[i];
		deg[j] -= 1;
		s[j] ^= i;
		edges ~= tuple(i, j);
	}
	writefln("%d", edges.length);
	foreach (t; edges) {
		writefln("%d %d", t[0], t[1]);
	}
}

import std.stdio;
import std.algorithm;

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

int main(string[] args)
{
    auto n = read!int;
    int[3][0x10000] desc;
    foreach (i; 0..n)
    {
        desc[i][0] = read!int;
        desc[i][1] = read!int;
        desc[i][2] = i;
    }

    auto sorted = sort!"a[0] < b[0]"(desc[0..n].dup);
    debug writeln("sorted = ", sorted);

    int m = 0;
    Tuple!(int, int)[0x10000] ans;
    foreach (i; 0..n)
    {
        auto p = sorted[i][2];

        if (desc[p][0] == 0)
            continue;

        debug writeln("i = ", i);
        debug write("desc = ", desc[0..n], " -> ");
        assert(sorted[i][0] == 1);

        auto j = desc[p][1];
        ans[m][0] = p;
        ans[m][1] = j;
        m++;
        desc[j][0]--;
        desc[j][1] ^= p;

        debug writeln(desc[0..n]);
    }

    writeln(m);
    foreach (i; 0..m)
    {
        writefln("%s %s", ans[i][0], ans[i][1]);
    }

    return 0;
}


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

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

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  auto t = r.nextA!uint (nt);
  foreach (n; t) {
    int x = n;
    if (x & 1) {
      write ('7');
      x -= 3;
    }
    while (x > 0) {
      write ('1');
      x -= 2;
    }
    writeln;
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new string[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto digit = n / 2;
		//if (digit <= 9)
		{
			string str;
			if (n % 2 == 0)
				str ~= "1";
			else
				str ~= "7";

			foreach (i; 1..digit)
				str ~= "1";
			ans[ti] = str;
		}
		/*else
		{
			auto arr = [9, 9, 8, 2, 4, 4, 3, 5, 3];
			auto cnt = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
			auto remain = n - 18;
			bool isLimit = true;
			foreach (i; 0..9)
			{
				auto lim = isLimit ? arr[i] : 9;
				long num;
				foreach_reverse (j; 0..lim+1)
				{
					if (remain >= cnt[j] - 2)
					{
						num = j;
						remain -= cnt[j] - 2;
						break;
					}
				}
				if (num < lim)
					isLimit = false;
				ans[ti] += num * 10^^(8-i);
			}
		}*/
	}

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

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

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

    while (T--) {
        auto N = readln.chomp.to!int;
        if (N % 2) write(7), N -= 3;
        foreach (i; 0..N/2) write(1);
        writeln;
    }
}

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

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

    while (T--) {
        auto N = readln.chomp.to!int;
        for (int i = N; N > 3; N -= 2) write(1);
        if (N == 2) write(1);
        else write(7);
        writeln;
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n >= 47)
		{
			ans[ti] = 998244353;
			continue;
		}

		auto digit = n / 2;
		if (digit <= 9)
		{
			string str;
			if (n % 2 == 0)
				str ~= "1";
			else
				str ~= "7";

			foreach (i; 1..digit)
				str ~= "1";
			ans[ti] = str.to!long;
		}
		else
		{
			auto arr = [9, 9, 8, 2, 4, 4, 3, 5, 3];
			auto cnt = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
			auto remain = n - 18;
			bool isLimit = true;
			foreach (i; 0..9)
			{
				auto lim = isLimit ? arr[i] : 9;
				long num;
				foreach_reverse (j; 0..lim+1)
				{
					if (remain >= cnt[j] - 2)
					{
						num = j;
						remain -= cnt[j] - 2;
						break;
					}
				}
				if (num < lim)
					isLimit = false;
				ans[ti] += num * 10^^(8-i);
			}
		}
	}

	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 = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n >= 47)
		{
			ans[ti] = 998244353;
			continue;
		}

		auto digit = n / 2;
		if (digit <= 9)
		{
			string str;
			if (n % 2 == 0)
				str ~= "1";
			else
				str ~= "7";

			foreach (i; 1..digit)
				str ~= "1";
			ans[ti] = str.to!long;
		}
		else
		{
			auto remain = n - 18;
			string str;
			foreach (i; 0..9)
			{
				if (remain >= 4)
				{
					str ~= "9";
					remain -= 4;
				}
				else if (remain >= 1)
				{
					str ~= "7";
					remain -= 1;
				}
				else
					str ~= "1";
			}
			ans[ti] = str.to!long;
		}
	}

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


// cheese-cracker [2022-01-31]

void solve(){
    long n = scan;
    long ten = (n/10) % 10;
    long d = n % 7;
    long u = 7 - d;
    long down = n - d;
    long up = n + u;
    long tend = (down / 10) % 10;
    long tenu = (up / 10) % 10;
    if(tend == ten){
        writeln(down);
    }else{
        writeln(up);
    }
}

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; alias tup = Tuple!(long, long);
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; } }

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        int n;
        scanf("%d", &n);
        getchar();
        auto d = n % 7;
        if (d == 0)
            writeln(n);
        else {
            int m = n % 10;
            if (m >= d)
                writeln(n - d);
            else {
                writeln(n + 7 - (n+7) % 7);
            }
        }
    }
}


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

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n, l, r;
    readf!" %d %d %d "(n, l, r);
    auto a = readln.strip.split.map!(to!int).array.sort;
    long result = 0;
    for (int i = 0; i < a.length; i++) {
      int x = a[i];
      int min_x = l - x;
      int max_x = r - x;
      auto chop_l = a.assumeSorted.lowerBound(min_x).length;
      auto chop_r = a.assumeSorted.upperBound(max_x).length;
      if (a.length > chop_l + chop_r) {
        auto count = a.length - chop_l - chop_r;
        if (x >= min_x && x <= max_x)
          count--;
        result += count;
      }
    }
    writeln(result / 2);
  }
}

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

void main() {
    foreach(_; 0 .. to!int(readln.strip)) {
        int n, l, r; readf("%d %d %d\n", n, l, r);
        auto ar = readln.strip.split.map!(to!int).array.sort;
        long ans = 0;
        foreach(e; ar) {
            int up = ar.upperBound(l-e-1).length;
            int dw = ar.upperBound(r-e).length;
            ans += up - dw;
            if(l <= 2*e && 2*e <= r) ans--;
        }
        writeln(ans/2);
    }
}

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

void main() {
    foreach(_; 0 .. to!int(readln.strip)) {
        int n, l, r; readf("%d %d %d\n", n, l, r);
        auto ar = readln.strip.split.map!(to!int).array.sort;
        int ans = 0;
        foreach(e; ar) {
            int up = ar.upperBound(l-e-1).length;
            int dw = ar.upperBound(r-e).length;
            ans += up - dw;
            if(l <= 2*e && 2*e <= r) ans--;
        }
        writeln(ans/2);
    }
}


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

struct IntM {
  enum q = 1_000_000_007;
  int v;
  this (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  IntM opAssign (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
    return this;
  }
  IntM opUnary (string op : "-")() const pure nothrow @nogc {
    return IntM ((q - v) % q);
  }
  ref IntM opUnary (string op : "++")() pure nothrow @nogc {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  ref IntM opUnary (string op : "--")() pure nothrow @nogc {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  ref IntM opOpAssign (string op : "+")(in IntM rhs) pure nothrow @nogc {
    v += rhs.v;
    v %= q;
    return this;
  }
  ref IntM opOpAssign (string op : "-")(in IntM rhs) pure nothrow @nogc {
    v -= rhs.v;
    v %= q;
    return this;
  }
  ref IntM opOpAssign (string op : "*")(in IntM rhs) pure nothrow @nogc {
    v = ((v.to!(long)) * rhs.v.to!(long)) % q;
    return this;
  }
  IntM opBinary (string op : "+")(in IntM rhs) const pure nothrow @nogc {
    return IntM ( (v + rhs.v) % q);
  }
  IntM opBinary (string op : "-")(in IntM rhs) const pure nothrow @nogc {
    return IntM ( (v - rhs.v) % q);
  }
  IntM opBinary (string op : "*")(in IntM rhs) const pure nothrow @nogc {
    return IntM (((v.to!(long)) * rhs.v.to!(long)) % q);
  }
  IntM opBinary (string op : "^^")(in long rhs) const pure nothrow @nogc {
    IntM a = 1, b = this;
    long p = rhs;
    while (p > 0) {
      //a * (b ^ p) == x ^ rhs
      if (p & 1) {
        a *= b;
      }
      b *= b;
      p >>>= 1;
    }
    return a;
  }
  IntM opBinary (string op)(in int v) const pure nothrow @nogc if (op == "+" || op == "-" || op == "*") {
    mixin ("return this " ~ op ~ " IntM(v);");
  }
  int opCast(T : int)() const pure nothrow @nogc { return v; }
  int opCmp (const IntM rhs) const pure nothrow @nogc {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  bool opEquals (const IntM rhs) const pure nothrow @nogc { return v == rhs.v; }
  string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; }
  //a ^ x = this (mod q)
  int discreteLogarithm (in IntM a) const {
    enum int n = ceil (sqrt (q.to!double)).to!int;
    IntM an = a ^^ n;
    int[int] h;
    IntM cur = an;
    foreach (p; 1 .. n + 1) {
      auto ptr = cur.v !in h;
      if (cur.v !in h) {
        h[cur.v] = p;
      }
      cur *= an;
    }
    cur = this;
    int res = int.max;
    foreach (q; 0 .. n + 1) {
      auto ptr = cur.v in h;
      if (ptr) {
        res = min (res, *ptr * n - q);
      }
      cur *= a;
    }
    return res != int.max ? res : -1;
  }
}

struct IntM2 {
  enum q = 1_000_000_006;
  int v;
  this (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  IntM2 opAssign (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
    return this;
  }
  IntM2 opUnary (string op : "-")() const pure nothrow @nogc {
    return IntM2 ((q - v) % q);
  }
  ref IntM2 opUnary (string op : "++")() pure nothrow @nogc {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  ref IntM2 opUnary (string op : "--")() pure nothrow @nogc {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  ref IntM2 opOpAssign (string op : "+")(in IntM2 rhs) pure nothrow @nogc {
    v += rhs.v;
    v %= q;
    return this;
  }
  ref IntM2 opOpAssign (string op : "-")(in IntM2 rhs) pure nothrow @nogc {
    v -= rhs.v;
    v %= q;
    return this;
  }
  ref IntM2 opOpAssign (string op : "*")(in IntM2 rhs) pure nothrow @nogc {
    v = ((v.to!(long)) * rhs.v.to!(long)) % q;
    return this;
  }
  IntM2 opBinary (string op : "+")(in IntM2 rhs) const pure nothrow @nogc {
    return IntM2 ( (v + rhs.v) % q);
  }
  IntM2 opBinary (string op : "-")(in IntM2 rhs) const pure nothrow @nogc {
    return IntM2 ( (v - rhs.v) % q);
  }
  IntM2 opBinary (string op : "*")(in IntM2 rhs) const pure nothrow @nogc {
    return IntM2 (((v.to!(long)) * rhs.v.to!(long)) % q);
  }
  IntM2 opBinary (string op : "^^")(in long rhs) const pure nothrow @nogc {
    IntM2 a = 1, b = this;
    long p = rhs;
    while (p > 0) {
      //a * (b ^ p) == x ^ rhs
      if (p & 1) {
        a *= b;
      }
      b *= b;
      p >>>= 1;
    }
    return a;
  }
  IntM2 opBinary (string op)(in int v) const pure nothrow @nogc if (op == "+" || op == "-" || op == "*") {
    mixin ("return this " ~ op ~ " IntM2(v);");
  }
  int opCast(T : int)() const pure nothrow @nogc { return v; }
  int opCmp (const IntM2 rhs) const pure nothrow @nogc {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  bool opEquals (const IntM2 rhs) const pure nothrow @nogc { return v == rhs.v; }
  string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; }
}

alias Matrix = IntM2[3][3];
Matrix mul (in Matrix a, in Matrix b) {
  Matrix c;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) foreach (k; 0 .. 3) {
    c[i][j] += a[i][k] * b[k][j];
  }
  return c;
}

Matrix power (Matrix x, long rhs) {
  Matrix a;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) a[i][j] = IntM2 (i == j ? 1 : 0);
  Matrix b;
  foreach (i; 0 .. 3) b[i] = x[i].dup;
  long p = rhs;
  while (p > 0) {
    //a * (b ^ p) == x ^ rhs
    if (p & 1) {
      a = mul (a, b);
    }
    b = mul (b, b);
    p >>>= 1;
  }
  return a;
}

void main() {
  long n;
  int[4] f;
  int[4] h;
  int c;
  readf! " %d %d %d %d %d" (n, f[1], f[2], f[3], c);
  IntM[4] g;
  foreach (i; 1 .. 4) {
    g[i] = IntM (f[i]);
    foreach (j; 0 .. i) {
      g[i] *= IntM (c);
    }
    h[i] = g[i].to!int;
  }
  foreach (p; [2, 3, 5, 7, 11, 13, 17, 23, 27, 31]) {
    bool ok = true;
    foreach (i; 1 .. 4) {
      f[i] = IntM(h[i]).discreteLogarithm (IntM(p));
      if (f[i] < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      debug stderr.writeln ("p = ", p);
      Matrix q;
      foreach (i; 0 .. 3) foreach (j; 0 .. 3) q[i][j] = IntM2 (0);
      q[0][0] = q[0][1] = q[0][2] = q[1][0] = q[2][1] = IntM2 (1);
      Matrix w = power (q, n - 3);
      debug stderr.writeln (w);
      IntM2 res2 = 0;
      foreach (i; 0 .. 3) {
        res2 += IntM2 (f[3 - i]) * w[0][i];
      }
      debug stderr.writeln (res2);
      IntM res = IntM (p) ^^ res2.v;
      res *= (IntM (c) ^^ n) ^^ (IntM.q - 2);
      writeln (res);
      return;
    }
  }
}

unittest {
  enum DL_TESTS = 10;
  foreach (p; [2, 3, 5, 7, 11, 17]) {
    stderr.writeln (p);
    IntM a = p;
    IntM x = 1;
    foreach (j; 0 .. DL_TESTS) {
      assert (x.discreteLogarithm (a) == j);
      x *= a;
    }
    foreach (j; iota (100, 10007 * DL_TESTS, 10007)) {
      IntM y = IntM (p) ^^ j;
      assert (y.discreteLogarithm (a) == j);
    }
  }
}

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

T gcdext(T) (T a, T b, ref T x, ref T y) pure nothrow @nogc {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T res = gcdext (b, a % b, y, x);
  y -= x * (a / b);
  return res;
}

struct IntM(int q = 1_000_000_007) {
  alias N = IntM!q;
  int v;
  private void fromInt (int m) pure nothrow @nogc {
    v = m % q;
    if (v < 0) {
      v += q;
    }
  }
  this (int m) pure nothrow @nogc {
    fromInt (m);
  }
  N opAssign (int m) pure nothrow @nogc {
    fromInt (m);
    return this;
  }
  N opUnary (string op : "-")() const pure nothrow @nogc {
    return N ((q - v) % q);
  }
  ref N opUnary (string op : "++")() pure nothrow @nogc {
    if (++v >= q) {
      v -= q;
    }
    return this;
  }
  ref N opUnary (string op : "--")() pure nothrow @nogc {
    if (--v < 0) {
      v += q;
    }
    return this;
  }
  ref N opOpAssign (string op : "+")(in N rhs) pure nothrow @nogc {
    v += rhs.v;
    v %= q;
    return this;
  }
  ref N opOpAssign (string op : "-")(in N rhs) pure nothrow @nogc {
    v -= rhs.v;
    v %= q;
    return this;
  }
  ref N opOpAssign (string op : "*")(in N rhs) pure nothrow @nogc {
    v = ((v.to!(long)) * rhs.v.to!(long)) % q;
    return this;
  }
  N opBinary (string op : "+")(in N rhs) const pure nothrow @nogc {
    return N ( (v + rhs.v) % q);
  }
  N opBinary (string op : "-")(in N rhs) const pure nothrow @nogc {
    return N ( (v - rhs.v) % q);
  }
  N opBinary (string op : "*")(in N rhs) const pure nothrow @nogc {
    return N (((v.to!(long)) * rhs.v.to!(long)) % q);
  }
  N opBinary (string op : "/")(in N rhs) const pure nothrow @nogc {
    return this * N.inversion ();
  }
  N inversion () const pure @nogc {
    int x, y;
    immutable g = gcdext!int (v, q, x, y);
    assert (g == 1);
    return N (x);
  }
  N opBinary (string op : "^^")(in long rhs) const pure nothrow @nogc {
    N a = 1, b = this;
    long p = rhs;
    while (p > 0) {
      //a * (b ^ p) == x ^ rhs
      if (p & 1) {
        a *= b;
      }
      b *= b;
      p >>>= 1;
    }
    return a;
  }
  N opBinary (string op)(in int v) const pure nothrow @nogc if (op == "+" || op == "-" || op == "*") {
    mixin ("return this " ~ op ~ " N(v);");
  }
  int opCast(T : int)() const pure nothrow @nogc { return v; }
  int opCmp (const N rhs) const pure nothrow @nogc {
    if (v < rhs.v) {
      return -1;
    }
    if (v > rhs.v) {
      return 1;
    }
    return 0;
  }
  bool opEquals (const N rhs) const pure nothrow @nogc { return v == rhs.v; }
  string toString() const pure nothrow { return ((v < 0) ? v + q : v).text; }
  //a ^ x = this (mod q)
  int discreteLogarithm (in N a) const {
    enum int n = ceil (sqrt (q.to!double)).to!int;
    N an = a ^^ n;
    int[int] h;
    N cur = an;
    foreach (p; 1 .. n + 1) {
      auto ptr = cur.v !in h;
      if (cur.v !in h) {
        h[cur.v] = p;
      }
      cur *= an;
    }
    cur = this;
    int res = int.max;
    foreach (q; 0 .. n + 1) {
      auto ptr = cur.v in h;
      if (ptr) {
        res = min (res, *ptr * n - q);
      }
      cur *= a;
    }
    return res != int.max ? res : -1;
  }
}

alias Matrix = IntM2[3][3];
Matrix mul (in Matrix a, in Matrix b) {
  Matrix c;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) foreach (k; 0 .. 3) {
    c[i][j] += a[i][k] * b[k][j];
  }
  return c;
}

Matrix power (Matrix x, long rhs) {
  Matrix a;
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) a[i][j] = IntM2 (i == j ? 1 : 0);
  Matrix b;
  foreach (i; 0 .. 3) b[i] = x[i].dup;
  long p = rhs;
  while (p > 0) {
    //a * (b ^ p) == x ^ rhs
    if (p & 1) {
      a = mul (a, b);
    }
    b = mul (b, b);
    p >>>= 1;
  }
  return a;
}

alias IntM1 = IntM!();
alias IntM2 = IntM!(1_000_000_006);

void main() {
  long n;
  int[4] f;
  int[4] h;
  int c;
  readf! " %d %d %d %d %d" (n, f[1], f[2], f[3], c);
  IntM1[4] g;
  foreach (i; 1 .. 4) {
    g[i] = IntM1 (f[i]);
    foreach (j; 0 .. i) {
      g[i] *= IntM1 (c);
    }
    h[i] = g[i].to!int;
  }
  foreach (p; [2, 3, 5, 7, 11, 13, 17, 23, 27, 31]) {
    bool ok = true;
    foreach (i; 1 .. 4) {
      f[i] = IntM1(h[i]).discreteLogarithm (IntM1(p));
      if (f[i] < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      debug stderr.writeln ("p = ", p);
      Matrix q;
      foreach (i; 0 .. 3) foreach (j; 0 .. 3) q[i][j] = IntM2 (0);
      q[0][0] = q[0][1] = q[0][2] = q[1][0] = q[2][1] = IntM2 (1);
      Matrix w = power (q, n - 3);
      debug stderr.writeln (w);
      IntM2 res2 = 0;
      foreach (i; 0 .. 3) {
        res2 += IntM2 (f[3 - i]) * w[0][i];
      }
      debug stderr.writeln (res2);
      IntM1 res = IntM1 (p) ^^ res2.v;
      res *= (IntM1 (c) ^^ n).inversion ();
      writeln (res);
      return;
    }
  }
}


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

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

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        write(arr.length - arr.find(arr[i] + arr[j]).length + 1, " ");
                                        writeln(i + 1, " ", j + 1);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}

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

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

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        write(countUntil(arr, arr.find(arr[i] + arr[j])) + 1, " ");
                                        writeln(i + 1, " ", j + 1);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}

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

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

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        writeln(arr[i] + arr[j], " ", arr[i],  " ", arr[j]);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}

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

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

void main() {
        IO cin;
        int t = 1;
        // t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int[] arr = new int[n];
                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_int;
                }
                for (int i = 0; i < n; i++) {
                        for (int j = i + 1; j < n; j++) {
                                if (arr.canFind(arr[i] + arr[j])) {
                                        writeln(arr[i], " ", arr[j],  " ", arr[i] + arr[j]);
                                        return;
                                }
                        }
                }
                writeln(-1);
        }
}


immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int - 1);
  auto sa = a.dup; sort(sa);
  auto sPosS = new int[][](n);
  foreach(i, ai; sa) sPosS[ai] ~= cast(int)i;
  bool excedent = false;
  foreach(i; 0 .. n) if (sPosS[i].length > 1) excedent = true;
  debug writeln("sPosS ", sPosS);
  auto adj = new int[](n);
  foreach(i; 0 .. n)
    {
      assert(sPosS[a[i]].length > 0);
      adj[i] = sPosS[a[i]].front;
      sPosS[a[i]].popFront;
    }
  debug writeln("adj ", adj);
  auto visited = new bool[](n);
  int getPar(int i)
  {
    assert(!visited[i]);
    visited[i] = true;
    int ans = 1;
    if (!visited[adj[i]]) ans ^= getPar(adj[i]);
    return ans;
  }
  auto par = int(0);
  foreach(i; 0 .. n) if (!visited[i]) par ^= 1^getPar(i);
  if (excedent) return writeln("YES");
  if (par) return writeln("NO");
  return writeln("YES");
}

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

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

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

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

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

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

void solve() {
    int N = read!int;
    auto A = readarray!int;

    bool f() {
        if (N == 1) return true;
        if (N == 2) return (A[0] <= A[1]);

        auto B = A.dup; B.sort;
        for (int i = 0; i+1 < N; i++) {
            if (B[i] == B[i+1]) return true;
        }

        long c = count_inversion(A);
        return c % 2 == 0;
    }
    writeln(f() ? "YES" : "NO");
}

long count_inversion(T)(in T[] xs_) {
    long inversion = 0;
    auto xs = xs_.dup;
    auto buf = new T[xs.length];
    void f(int s, int t) {
        if (s + 1 == t) return;
        int mid = (s + t) / 2;
        int i = s;
        f(s, mid);
        f(mid, t);
        int li = s, ri = mid;
        while (li < mid || ri < t) {
            if (li == mid) { buf[i++] = xs[ri++]; continue; }
            if (ri == t) { buf[i++] = xs[li++]; continue; }
            if (xs[li] < xs[ri]) {
                buf[i++] = xs[li++];
            } else if (xs[li] > xs[ri]) {
                buf[i++] = xs[ri++];
                inversion += mid - li;
            } else {
                writefln("s = %d, t = %d", s, t);
                writefln("li = %d, ri = %d, i = %d", li, ri, i);
                assert(false);
            }
        }
        for (int j = s; j < t; j++) xs[j] = buf[j];
        assert(i == t);
    }
    f(0, xs.length);
    return inversion;
}

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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

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

void solve() {
    int N = read!int;
    auto A = readarray!int;

    bool f() {
        if (N == 1) return true;
        if (N == 2) return (A[0] <= A[1]);
        long c = count_inversion(A);
        return c % 2 == 0;
    }
    writeln(f() ? "YES" : "NO");
}

long count_inversion(T)(in T[] xs_) {
    long inversion = 0;
    auto xs = xs_.dup;
    auto buf = new T[xs.length];
    void f(int s, int t) {
        if (s + 1 == t) return;
        int mid = (s + t) / 2;
        int i = s;
        f(s, mid);
        f(mid, t);
        int li = s, ri = mid;
        while (li < mid || ri < t) {
            if (li == mid) { buf[i++] = xs[ri++]; continue; }
            if (ri == t) { buf[i++] = xs[li++]; continue; }
            if (xs[li] <= xs[ri]) {
                buf[i++] = xs[li++];
            } else if (xs[li] > xs[ri]) {
                buf[i++] = xs[ri++];
                inversion += mid - li;
            } else {
                writefln("s = %d, t = %d", s, t);
                writefln("li = %d, ri = %d, i = %d", li, ri, i);
                assert(false);
            }
        }
        for (int j = s; j < t; j++) xs[j] = buf[j];
        assert(i == t);
    }
    f(0, xs.length);
    return inversion;
}

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int - 1);
  auto sa = a.dup; sort(sa);
  auto sPosS = new int[][](n);
  foreach(i, ai; sa) sPosS[ai] ~= cast(int)i;
  debug writeln("sPosS ", sPosS);
  auto adj = new int[](n);
  foreach(i; 0 .. n)
    {
      assert(sPosS[a[i]].length > 0);
      adj[i] = sPosS[a[i]].front;
      sPosS[a[i]].popFront;
    }
  auto visited = new bool[](n);
  int getPar(int i)
  {
    assert(!visited[i]);
    visited[i] = true;
    int ans = 1;
    if (!visited[adj[i]]) ans ^= getPar(adj[i]);
    return ans;
  }
  auto par = int(0);
  foreach(i; 0 .. n) if (!visited[i]) par ^= 1^getPar(i);
  if (par) return writeln("NO");
  return writeln("YES");
}

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

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

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

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

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


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

int id(char c) {
    return cast(int)(c - 'a');
}

int diff(int mod, char c, in int[][] hist) {
    int ret = 0;
    auto N = hist.length;
    foreach (j; 0 .. 26) {
        if (j == c.id) continue;
        ret += hist[mod][j].abs;
    }
    return ret;
}

void main() {
    long N, M; scanf("%lld %lld\n", &N, &M);
    auto x = readln.chomp,
         y = readln.chomp;
    int  X = cast(int)x.length,
         Y = cast(int)y.length;
    int  gcd = gcd(X, Y);
    long lcm = cast(long)(X) / gcd * Y;
    auto histogram = new int[][](gcd, 26);
    foreach (int i, c; y) {
        histogram[i % gcd][c.id]++;
    }
    long ans = 0;
    foreach (int i, c; x) {
        ans += diff(i % gcd, c, histogram);
    }
    writeln(ans * (N * X / lcm));
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!long;
  auto m = next!long;
  auto x = next!string;
  auto y = next!string;
  auto g = gcd(cast(long)x.length, cast(long)y.length);
  auto lx = x.length / g;
  auto ly = y.length / g;
  debug writeln(g, " ", ly);
  long matches = 0;
  foreach(cl; 0 .. g)
    {
      long[char] chrCnt;
      foreach(inst; 0 .. ly)
	chrCnt.require(y[cast(int)(inst * g + cl)], 0)++;
      foreach(inst; 0 .. lx)
	matches += chrCnt.require(x[cast(int)(inst * g + cl)], 0);
    }
  assert(n * x.length == m * y.length);
  auto l = lx * y.length;
  auto lres = l - matches;
  auto res = (n * x.length) / l * lres;
  res.writeln;
}

// INPUT

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

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

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

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto n = next!long;
  auto m = next!long;
  auto x = next!string;
  auto y = next!string;
  auto g = gcd(x.length, y.length);
  auto lx = x.length / g;
  auto ly = y.length / g;
  debug writeln(g, " ", ly);
  long matches = 0;
  foreach(cl; 0 .. g)
    {
      long[char] chrCnt;
      foreach(inst; 0 .. ly)
	chrCnt.require(y[inst * g + cl], 0)++;
      foreach(inst; 0 .. lx)
	matches += chrCnt.require(x[inst * g + cl], 0);
    }
  assert(n * x.length == m * y.length);
  auto l = lx * y.length;
  auto lres = l - matches;
  auto res = (n * x.length) / l * lres;
  res.writeln;
}

// INPUT

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

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

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

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

int id(char c) {
    return cast(int)(c - 'a');
}

int diff(int mod, char c, in int[][] hist) {
    int ret = 0;
    auto N = hist.length;
    foreach (j; 0 .. 26) {
        if (j == c.id) continue;
        ret += hist[mod][j].abs;
    }
    return ret;
}

void main() {
    long N, M; scanf("%lld %lld\n", &N, &M);
    auto x = readln.chomp,
         y = readln.chomp;
    int  X = cast(int)x.length,
         Y = cast(int)y.length;
    int  gcd = gcd(X, Y);
    long lcm = X / gcd * Y;
    auto histogram = new int[][](gcd, 26);
    foreach (int i, c; y) {
        histogram[i % gcd][c.id]++;
    }
    long ans = 0;
    foreach (int i, c; x) {
        ans += diff(i % gcd, c, histogram);
    }
    writeln(ans * (N * X / lcm));
}


// Codeforces My Practice
// author: Leonardone @ NEETSDKASU
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.range;

// no my think

void main() {
    auto nk = to!(int[])(split(chomp(readln())));
    int n = nk[0], k = nk[1];
    auto xs = to!(long[])(split(chomp(readln())));
    int[5300][201] tb;
    for (int i = 0; i <= k; i++) {
        for (int j = 0; j < 5300; j++) {
            tb[i][j] = -1;
        }
    }
    tb[0][0] = 0;
    foreach (x ; xs) {
        int c2, c5;
        while (x % 2 == 0) {
            x /= 2;
            c2++;
        }
        while (x % 5 == 0) {
            x /= 5;
            c5++;
        }
        for (int i = k - 1; i > 0; i--) {
            for (int j = 5299; j >= 0; j--) {
                if (tb[i][j] < 0) { continue; }
                tb[i+1][j+c5] = max(tb[i+1][j+c5], tb[i][j]+c2);
            }
        }
        tb[1][c5] = max(tb[1][c5], c2);
    }
    int ans = 0;
    for (int i = 1; i < 5300; i++) {
        if (tb[k][i] < 0) { continue; }
        ans = max(ans, min(tb[k][i], i));
    }
    writeln(ans);
}

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.datetime, std.bigint;

int n, k;
long[] a;

void main() {
    scan(n, k);
    a = readln.split.to!(long[]);

    auto two = new int[](n);
    auto five = new int[](n);

    foreach (i ; 0 .. n) {
        while (a[i] % 2 == 0) {
            a[i] /= 2;
            two[i]++;
        }

        while (a[i] % 5 == 0) {
            a[i] /= 5;
            five[i]++;
        }
    }

    int lim = n * 25;

    auto dp = new int[][][](2, k + 1, lim + 1);
    fillAll(dp, -1);
    dp[0][0][0] = 0;

    foreach (i ; 0 .. n) {
        foreach (j ; 0 .. k + 1) {
            foreach (f ; 0 .. lim + 1) {
                if (dp[i & 1][j][f] == -1) continue;
                dp[(i & 1) ^ 1][j][f] = max(dp[(i & 1) ^ 1][j][f], dp[i & 1][j][f]);

                if (j + 1 <= k) {
                    dp[(i & 1) ^ 1][j + 1][f + five[i]] = max(dp[(i & 1) ^ 1][j + 1][f + five[i]], dp[i & 1][j][f] + two[i]);
                }
            }
        }
    }

    int ans;

    foreach (f ; 0 .. lim + 1) {
        ans = max(ans, min(f, dp[n & 1][k][f]));
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

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

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

    auto two = new int[](N);
    auto five = new int[](N);
    foreach (i, a; A) {
        while (a % 2 == 0) a /= 2, two[i] += 1;
        while (a % 5 == 0) a /= 5, five[i] += 1;
    }

    auto dp = new int[][][](2, M+1, 25*N+1);
    auto ok = new bool[][][](2, M+1, 25*N+1);
    ok[0][0][0] = true;
    int cur = 0, tar = 1;

    foreach (i; 0..N) {
        foreach (j; 0..M+1) {
            dp[tar][j][] = 0;
            ok[tar][j][] = 0;
        }
        foreach (j; 0..M+1) foreach (k; 0..25*i+1) if (ok[cur][j][k]) {
            dp[tar][j][k] = max(dp[tar][j][k], dp[cur][j][k]);
            ok[tar][j][k] = true;
            if (j < M) {
                dp[tar][j+1][k+five[i]] = max(dp[tar][j+1][k+five[i]], dp[cur][j][k] + two[i]);
                ok[tar][j+1][k+five[i]] = true;
            }
        }
        swap(cur, tar);
    }

    int ans = 0;
    foreach (j; 0..M+1) foreach (k; 0..25*N+1) {
        ans = max(ans, min(k, dp[cur][j][k]));
    }
    ans.writeln;
}

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

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

    foreach (i; 0..N) {
        while (A[i] % 5 == 0) {
            TF[i][1] += 1;
            A[i] /= 5;
        }
        while (A[i] % 2 == 0) {
            TF[i][0] += 1;
            A[i] /= 2;
        }
    }

    auto dp = new int[][](N + 1, 4000);
    auto dp2 = new bool[][](N + 1, 4000);
    dp2[0][0] = true;

    foreach (i; 0..N) {
        for (int j = i + 1; j >= 1; j--) {
            foreach (k; 0..3800) {
                if (k - TF[i][1] >= 0 && dp2[j - 1][k - TF[i][1]]) {
                    dp[j][k] = max(dp[j][k], dp[j - 1][k - TF[i][1]] + TF[i][0]);
                    dp2[j][k] = true;
                }
            }
        }
        dp[1][TF[i][1]] = max(dp[1][TF[i][1]], TF[i][0]);
        dp2[1][TF[i][1]] = true;
    }

    int ans = 0;
    foreach (i; 0..3800) {
        if (dp2[K][i]) ans = max(ans, min(i, dp[K][i]));
    }
    ans.writeln;
}

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

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

    foreach (i; 0..N) {
        while (A[i] % 5 == 0) {
            TF[i][1] += 1;
            A[i] /= 5;
        }
        while (A[i] % 2 == 0) {
            TF[i][0] += 1;
            A[i] /= 2;
        }
    }

    auto dp = new int[][](N + 1, 2000);
    auto dp2 = new bool[][](N + 1, 2000);
    dp2[0][0] = true;

    foreach (i; 0..N) {
        for (int j = i + 1; j >= 1; j--) {
            foreach (k; 0..1900) {
                if (k - TF[i][1] >= 0 && dp2[j - 1][k - TF[i][1]]) {
                    dp[j][k] = max(dp[j][k], dp[j - 1][k - TF[i][1]] + TF[i][0]);
                    dp2[j][k] = true;
                }
            }
        }
        dp[1][TF[i][1]] = max(dp[1][TF[i][1]], TF[i][0]);
        dp2[1][TF[i][1]] = true;
    }

    int ans = 0;
    foreach (i; 0..1900) {
        if (dp2[K][i]) ans = max(ans, min(i, dp[K][i]));
    }
    ans.writeln;
}


import std.numeric;
import std.stdio;

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

		int d;
		foreach (i; 0..n)
			d = gcd (d, a[i]);
		while (d % 2 == 0)
			d /= 2;

		long res;

		void add (int x)
		{
			while (x < m)
				res += m - x,
				x *= 2;
		}

		for (int w = 1; w * w <= d; w += 2)
			if (d % w == 0)
			{
				add (w);
				if (w * w < d)
					add (d / w);
			}
		writeln (res);
	}
}

import std.numeric;
import std.stdio;

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

		int d = 0;
		foreach (i; 0..n)
			d = gcd (d, a[i]);
		while (d % 2 == 0)
			d /= 2;

		long res = 0;

		void add (int x)
		{
			while (x <= m)
			{
				res += m - x + 1;
				x *= 2;
			}
		}

		for (int w = 1; w * w <= d; w += 2)
			if (d % w == 0)
			{
				add (w);
				if (w * w < d)
					add (d / w);
			}
		writefln ("%s", res);
	}
	return 0;
}

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

int main ()
{
	int n;
	long m;
	while ((readf (" %s %s", &n, &m) >= 0) && !stdin.eof ())
	{
		m--;
		auto a = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s", &a[i]);
			a[i]--;
		}

		int d = 0;
		foreach (i; 0..n)
		{
			d = gcd (d, a[i]);
		}
		while (d % 2 == 0)
		{
			d /= 2;
		}

		long res = 0;

		void add (int x)
		{
			while (x <= m)
			{
				res += m - x + 1;
				x *= 2;
			}
		}

		for (int w = 1; w * w <= d; w += 2)
		{
			if (d % w == 0)
			{
				add (w);
				if (w * w < d)
				{
					add (d / w);
				}
			}
		}
		writefln ("%s", res);
	}
	return 0;
}


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)
	{
		auto n = readln.strip.to !(int);
		foreach (a; [7 * 2, 5 * 3])
		{
			auto p = [2 * 3, 2 * 5, a];
			p ~= n - p.sum;
			sort (p);
			if (p[0] > 0 && isStrictlyMonotonic (p))
			{
				writeln ("YES");
				writefln !("%(%s %)") (p);
				continue multitest_loop;
			}
		}
		writeln ("NO");
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n <= 30) continue;
		if (n-30 == 6 || n-30 == 10 || n-30 == 14)
			ans[ti] = [6, 10, 15, n-31];
		else
			ans[ti] = [6, 10, 14, n-30];
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln("NO");
		else
		{
			writeln("YES");
			e.map!(to!string).join(" ").writeln;
		}
	}
	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(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		if (n <= 30) continue;
		ans[ti] = [6, 10, 14, n-30];
	}

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


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

void play(){
    int a, b, c, d;
    a = rd!int;
    b = rd!int;
    c = rd!int;
    d = rd!int;
    writeln(max(a+b, c+d));
}

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

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

import std.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 a, b, c, d;
		readf !(" %s %s %s %s") (a, b, c, d);
		writeln (max (a + b, c + d));
	}
}


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

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

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

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

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

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

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

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

void main()
{
    int T; get(T);
    while (T--) {
        char[] s, t; get(s); get(t);
        char[] r;
        if (s.length > t.length) swap(s, t);
        foreach (l; 1..s.length + 1) if (s.length % l == 0 && t.length % l == 0) {
            auto x = s[0..l];
            auto n = (s.length / l) * (t.length / l) / gcd(s.length / l, t.length / l);
            foreach (i; 0..s.length / l) if (s[i * l..i * l + l] != x) goto next;
            foreach (i; 0..t.length / l) if (t[i * l..i * l + l] != x) goto next;
            if (r.empty || r.length > l * n) {
                r = [];
                foreach (_; 0..n) r ~= x;
            }
            next:
        }
        if (r.empty) {
            writeln(-1);
        } else {
            writeln(r);
        }
    }
}

import std.stdio, std.string, std.numeric;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        string s = readln.strip, t = readln.strip;
        auto n = s.length, m = t.length, g = gcd(n, m);
        string res = s[0 .. g];
        while (res.length % n || res.length % m)
            res ~= s[0 .. g];
        writeln(res[0 .. n] == s && res[0 .. m] == t ? res : "-1");
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto len = lcm(s1.length, s2.length);
		while (ans[ti].length < len)
		{
			ans[ti] ~= s1;
		}
		string s3;
		while (s3.length < len)
		{
			s3 ~= s2;
		}
		foreach (i; 0..s3.length)
		{
			if (ans[ti][i] != s3[i])
			{
				ans[ti].length = 0;
				break;
			}
		}
	}

	foreach (e; ans)
	{
		writeln(e.empty ? "-1" : e);
	}
	stdout.flush;
	debug readln;
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		auto t = readln.strip;
		auto m = s.length;
		auto n = t.length;
		auto g = gcd (m, n);
		auto x = s[0..g];
		auto r = x;
		while (r.length % m != 0 || r.length % n != 0)
		{
			r ~= x;
		}
		writeln ((r[0..m] == s && r[0..n] == t) ? r : "-1");
	}
}