Alice has a grid with $$$2$$$ rows and $$$n$$$ columns. She fully covers the grid using $$$n$$$ dominoes of size $$$1 \times 2$$$ — Alice may place them vertically or horizontally, and each cell should be covered by exactly one domino.Now, she decided to show one row of the grid to Bob. Help Bob and figure out what the other row of the grid looks like!

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the width of the grid. The second line of each test case contains a string $$$s$$$ consisting of $$$n$$$ characters, each of which is either L, R, U, or D, representing the left, right, top, or bottom half of a domino, respectively (see notes for better understanding). This string represents one of the rows of the grid.  Additional constraint on the input: each input corresponds to at least one valid tiling.

For each test case, output one string — the other row of the grid, using the same format as the input string. If there are multiple answers, print any.

NoteIn the first test case, Alice shows Bob the top row, the whole grid may look like:   In the second test case, Alice shows Bob the bottom row, the whole grid may look like:   In the third test case, Alice shows Bob the bottom row, the whole grid may look like:   In the fourth test case, Alice shows Bob the top row, the whole grid may look like:

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 string[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto s = RD!string;

		foreach (c; s)
		{
			if (c == 'U')
				ans[ti] ~= "D";
			else if (c == 'L')
				ans[ti] ~= "L";
			else if (c == 'R')
				ans[ti] ~= "R";
			else
				ans[ti] ~= "U";
		}
	}

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

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto grid = readString;
	string ans;
	int i = 0;
	while (i < n)
	{
		switch(grid[i])
		{
		case 'U':
			ans ~= 'D';
			break;
		case 'D':
			ans ~= 'U';
			break;
		case 'L':
			ans ~= "LR";
			i++;
			break;
		default:
			assert(0);
		}
		i++;
	}
	assert(ans.length == grid.length);
	writeln(ans);
}

//{{{
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.format, 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--) {
    long n;
    readf!" %d "(n);
    auto s = readln.strip;
    char[] s2;
    foreach (ch ; s) {
      if (ch == 'R')
        s2 ~= 'R';
      else if (ch == 'L')
        s2 ~= 'L';
      else if (ch == 'U')
        s2 ~= 'D';
      else
        s2 ~= 'U';
    }
    writeln(s2.idup);
  }
}


After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly.However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s.Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response.

In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10).  Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive.

Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any.

Go_straight_along_this_street
5
str
long
tree
biginteger
ellipse

NoteIn the first sample, the solution is traight_alon.In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on.In the third sample, the solution is either nagio or oisii.

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

void main()
{
    auto s = readln.chomp;

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

    string[] tsInput;
    foreach (i; 0 .. n) {
        tsInput ~= readln.chomp;
    }

    string[] ts;
    outer: foreach (i; 0 .. n) {
        foreach (j; 0 .. n) {
            if (i == j) { continue; }

            if ((tsInput[i] == tsInput[j] && i > j)
                || (tsInput[i] != tsInput[j] && tsInput[i].canFind(tsInput[j]))) { continue outer; }
        }

        ts ~= tsInput[i];
    }

    debug { ts.writeln; }

    auto starts = new int[] (s.length);
    auto ends = new int[] (s.length);
    foreach (i; 0 .. s.length) {
        foreach (k, e; ts) {
            if (e.length > s.length - i) { continue; }

            if (s[i .. i + e.length] == e) {
                starts[i] = k.to!int + 1;
                ends[i + e.length - 1] = k.to!int + 1;
            }
        }
    }

    int mxlen = 0, mxpos = 0;
    int curlft = 0, nxtlft = 0;
    auto rbt = make!(RedBlackTree!int);
    foreach (i; 0 .. s.length) {
        debug { writeln(i, ' ', starts[i], ' ', ends[i], ' ', curlft, ' ', nxtlft, ' ', mxlen, ' ', mxpos); }

        if (starts[i] != 0) {
            nxtlft = i.to!int + 1;
            rbt.insert(starts[i]);
        }

        if (ends[i] in rbt) {
            int curln = i.to!int - 1 - curlft + 1;
            if (curln > mxlen) {
                mxlen = curln;
                mxpos = curlft;
            }

            curlft = nxtlft;
            rbt.clear();
        }
    }

    int curln = s.length.to!int - 1 - curlft + 1;
    if (curln > mxlen) {
        mxlen = curln;
        mxpos = curlft;
    }

    writeln(mxlen, ' ', mxpos);
}

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

void main()
{
    auto s = readln.chomp;

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

    string[] tsInput;
    foreach (i; 0 .. n) {
        tsInput ~= readln.chomp;
    }

    string[] ts;
    outer: foreach (i; 0 .. n) {
        foreach (j; 0 .. n) {
            if (i == j) { continue; }

            if ((tsInput[i] == tsInput[j] && i > j)
                || (tsInput[i] != tsInput[j] && tsInput[i].canFind(tsInput[j]))) { continue outer; }
        }

        ts ~= tsInput[i];
    }

    debug { ts.writeln; }

    auto starts = new bool[] (s.length);
    auto ends = new bool[] (s.length);
    foreach (i; 0 .. s.length) {
        foreach (e; ts) {
            if (e.length > s.length - i) { continue; }

            if (s[i .. i + e.length] == e) {
                starts[i] = true;
                ends[i + e.length - 1] = true;
            }
        }
    }

    int mxlen = 0, mxpos = 0;
    int curlft = 0, nxtlft = 0;
    foreach (i; 0 .. s.length) {
        debug { writeln(i, ' ', starts[i], ' ', ends[i], ' ', curlft, ' ', nxtlft, ' ', mxlen, ' ', mxpos); }

        if (starts[i]) {
            nxtlft = i.to!int + 1;
        }

        if (ends[i]) {
            int curln = i.to!int - 1 - curlft + 1;
            if (curln > mxlen) {
                mxlen = curln;
                mxpos = curlft;
            }

            curlft = nxtlft;
        }
    }

    int curln = s.length.to!int - 1 - curlft + 1;
    if (curln > mxlen) {
        mxlen = curln;
        mxpos = curlft;
    }

    writeln(mxlen, ' ', mxpos);
}


You are given a string s, consisting of lowercase English letters, and the integer m.One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.Formally, we choose a subsequence of indices 1 ≤ i1 < i2 < ... < it ≤ |s|. The selected sequence must meet the following condition: for every j such that 1 ≤ j ≤ |s| - m + 1, there must be at least one selected index that belongs to the segment [j,  j + m - 1], i.e. there should exist a k from 1 to t, such that j ≤ ik ≤ j + m - 1.Then we take any permutation p of the selected indices and form a new string sip1sip2... sipt.Find the lexicographically smallest string, that can be obtained using this procedure.

The first line of the input contains a single integer m (1 ≤ m ≤ 100 000). The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.

Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.

NoteIn the first sample, one can choose the subsequence {3} and form a string "a".In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab".

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

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

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

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

enum A = 26;

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const S = readToken();
      const L = cast(int)(S.length);

      auto freq = new int[A];
      foreach (i; 0 .. L) {
        ++freq[S[i] - 'a'];
      }

      string ans;
      foreach (a; 0 .. A) {
        debug {
          writeln("a = ", a);
        }
        bool ok = true;
        int cnt;
        int last = -1, save = -1;
        foreach (i; 0 .. L) {
          if (S[i] - 'a' < a) {
            last = i;
          } else if (S[i] - 'a' == a) {
            save = i;
          }
          if (i - last >= M) {
            if (last >= save) {
              debug {
                writeln("  ng; i = ", i);
              }
              ok = false;
              break;
            }
            ++cnt;
            last = save;
          }
        }
        if (ok) {
          debug {
            writeln("  ok; cnt = ", cnt);
          }
          foreach (b; 0 .. a) {
            foreach (_; 0 .. freq[b]) {
              ans ~= cast(char)('a' + b);
            }
          }
          foreach (_; 0 .. cnt) {
            ans ~= cast(char)('a' + a);
          }
          break;
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


There are three cells on an infinite 2-dimensional grid, labeled $$$A$$$, $$$B$$$, and $$$F$$$. Find the length of the shortest path from $$$A$$$ to $$$B$$$ if:   in one move you can go to any of the four adjacent cells sharing a side;  visiting the cell $$$F$$$ is forbidden (it is an obstacle).

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first one contains two integers $$$x_A, y_A$$$ ($$$1 \le x_A, y_A \le 1000$$$) — coordinates of the start cell $$$A$$$. The second one contains two integers $$$x_B, y_B$$$ ($$$1 \le x_B, y_B \le 1000$$$) — coordinates of the finish cell $$$B$$$. The third one contains two integers $$$x_F, y_F$$$ ($$$1 \le x_F, y_F \le 1000$$$) — coordinates of the forbidden cell $$$F$$$. All cells are distinct. Coordinate $$$x$$$ corresponds to the column number and coordinate $$$y$$$ corresponds to the row number (see the pictures below).

Output $$$t$$$ lines. The $$$i$$$-th line should contain the answer for the $$$i$$$-th test case: the length of the shortest path from the cell $$$A$$$ to the cell $$$B$$$ if the cell $$$F$$$ is not allowed to be visited.

Note    An example of a possible shortest path for the first test case.     An example of a possible shortest path for the second test case.

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

int abs(int x) {
    return x < 0 ? -x : x;
}

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

while(t--) {
    int xa, ya, xb, yb, xf, yf;
    readf("\n%s %s\n%s %s\n%s %s\n", &xa, &ya, &xb, &yb, &xf, &yf);
    int ans = abs(xa - xb) + abs(ya - yb);
    if(xa == xb && xb == xf && yf < max(ya, yb) && yf > min(ya, yb)) {
        ans += 2;
    }
    if(ya == yb && yb == yf && xf < max(xa, xb) && xf > min(xa, xb)) {
        ans += 2;
    }
    ans.writeln;
}
}

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

int abs(int x) {
    return x < 0 ? -x : x;
}

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

while(t--) {
    int xa, ya, xb, yb, xf, yf;
    readf("\n%s %s\n%s %s\n%s %s\n", &xa, &ya, &xb, &yb, &xf, &yf);
    int ans = abs(xa - xb) + abs(ya - yb);
    if(xa == xb && xb == xf && xf < max(xa, xb) && xf > min(xa, xb)) {
        ans += 2;
    }
    if(ya == yb && yb == yf && yf < max(ya, yb) && yf > min(ya, yb)) {
        ans += 2;
    }
    ans.writeln;
}
}


This is the hard version of this problem. The difference between easy and hard versions is only the constraints on $$$a_i$$$ and on $$$n$$$. You can make hacks only if both versions of the problem are solved.Burenka is the crown princess of Buryatia, and soon she will become the $$$n$$$-th queen of the country. There is an ancient tradition in Buryatia — before the coronation, the ruler must show their strength to the inhabitants. To determine the strength of the $$$n$$$-th ruler, the inhabitants of the country give them an array of $$$a$$$ of exactly $$$n$$$ numbers, after which the ruler must turn all the elements of the array into zeros in the shortest time. The ruler can do the following two-step operation any number of times:   select two indices $$$l$$$ and $$$r$$$, so that $$$1 \le l \le r \le n$$$ and a non-negative integer $$$x$$$, then  for all $$$l \leq i \leq r$$$ assign $$$a_i := a_i \oplus x$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. It takes $$$\left\lceil \frac{r-l+1}{2} \right\rceil$$$ seconds to do this operation, where $$$\lceil y \rceil$$$ denotes $$$y$$$ rounded up to the nearest integer. Help Burenka calculate how much time she will need.

The first line contains a single integer $$$t$$$ $$$(1 \le t \le 500)$$$  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ - the size of the array The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \cdots , a_n$$$ $$$(0 \le a_i < 2^{30})$$$ — elements of the array. It is guaranteed that the sum of $$$n$$$ in all tests does not exceed $$$10^5$$$.

For each test case, output a single number  — the minimum time that Burenka will need.

NoteIn the first test case, Burenka can choose segment $$$l = 1$$$, $$$r = 4$$$, and $$$x=5$$$. so it will fill the array with zeros in $$$2$$$ seconds.In the second test case, Burenka first selects segment $$$l = 1$$$, $$$r = 2$$$, and $$$x = 1$$$, after which $$$a = [0, 2, 2]$$$, and then the segment $$$l = 2$$$, $$$r = 3$$$, and $$$x=2$$$, which fills the array with zeros. In total, Burenka will spend $$$2$$$ seconds.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto s = [0];
		foreach (ref x; a)
		{
			s ~= s.back ^ x;
		}

		bool [int] v;
		int cur = 0;
		v[cur] = true;
		int res = 0;
		foreach (ref x; a)
		{
			if ((x ^ cur) in v)
			{
				v = null;
				cur = 0;
				v[cur] = true;
				continue;
			}
			res += 1;
			cur ^= x;
			v[cur] = true;
		}
		writeln (res);
	}
}


You are given an array $$$a_1, a_2, \dots , a_n$$$, which is sorted in non-decreasing order ($$$a_i \le a_{i + 1})$$$. Find three indices $$$i$$$, $$$j$$$, $$$k$$$ such that $$$1 \le i < j < k \le n$$$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$ (for example it is possible to construct a non-degenerate triangle with sides $$$3$$$, $$$4$$$ and $$$5$$$ but impossible with sides $$$3$$$, $$$4$$$ and $$$7$$$). If it is impossible to find such triple, report it.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le 10^9$$$; $$$a_{i - 1} \le a_i$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case print the answer to it in one line. If there is a triple of indices $$$i$$$, $$$j$$$, $$$k$$$ ($$$i < j < k$$$) such that it is impossible to construct a non-degenerate triangle having sides equal to $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1.

NoteIn the first test case it is impossible with sides $$$6$$$, $$$11$$$ and $$$18$$$. Note, that this is not the only correct answer.In the second test case you always can construct a non-degenerate triangle.

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

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

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

        if(a[$-1] >= a[0] + a[1])
            writefln("1 2 %s", n);
        else
            "-1".writeln;
    }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto p = n.iota.array;
		makeIndex (a, p);
		if (a[p[0]] + a[p[1]] <= a[p[$ - 1]])
		{
			auto q = [p[0], p[1], p[$ - 1]];
			sort (q);
			q[] += 1;
			writefln !("%(%s %)") (q);
		}
		else
		{
			writeln (-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(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;
		auto a = RDA!int;
		if (a[0]+a[1] > a[$-1])
			continue;
		ans[ti] = [1, 2, n];
	}

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

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

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

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

        if(a[$-1] > a[0] + a[1])
            writefln("1 2 %s", n);
        else
            "-1".writeln;
    }
}


Bob is a duck. He wants to get to Alice's nest, so that those two can duck!  Duck is the ultimate animal! (Image courtesy of See Bang) The journey can be represented as a straight line, consisting of $$$n$$$ segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last segment. Each segment has a length in meters, and also terrain type: grass, water or lava. Bob has three movement types: swimming, walking and flying. He can switch between them or change his direction at any point in time (even when he is located at a non-integer coordinate), and doing so doesn't require any extra time. Bob can swim only on the water, walk only on the grass and fly over any terrain. Flying one meter takes $$$1$$$ second, swimming one meter takes $$$3$$$ seconds, and finally walking one meter takes $$$5$$$ seconds.Bob has a finite amount of energy, called stamina. Swimming and walking is relaxing for him, so he gains $$$1$$$ stamina for every meter he walks or swims. On the other hand, flying is quite tiring, and he spends $$$1$$$ stamina for every meter flown. Staying in place does not influence his stamina at all. Of course, his stamina can never become negative. Initially, his stamina is zero.What is the shortest possible time in which he can reach Alice's nest?

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of segments of terrain.  The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \leq l_i \leq 10^{12}$$$). The $$$l_i$$$ represents the length of the $$$i$$$-th terrain segment in meters. The third line contains a string $$$s$$$ consisting of $$$n$$$ characters "G", "W", "L", representing Grass, Water and Lava, respectively.  It is guaranteed that the first segment is not Lava.

Output a single integer $$$t$$$ — the minimum time Bob needs to reach Alice.

NoteIn the first sample, Bob first walks $$$5$$$ meters in $$$25$$$ seconds. Then he flies the remaining $$$5$$$ meters in $$$5$$$ seconds.In the second sample, Bob first swims $$$10$$$ meters in $$$30$$$ seconds. Then he flies over the patch of lava for $$$10$$$ seconds.In the third sample, the water pond is much smaller. Bob first swims over the water pond, taking him $$$3$$$ seconds. However, he cannot fly over the lava just yet, as he only has one stamina while he needs two. So he swims back for half a meter, and then half a meter forward, taking him $$$3$$$ seconds in total. Now he has $$$2$$$ stamina, so he can spend $$$2$$$ seconds flying over the lava.In the fourth sample, he walks for $$$50$$$ seconds, flies for $$$10$$$ seconds, swims for $$$15$$$ seconds, and finally flies for $$$5$$$ seconds.

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto d = readln.splitter.map !(to !(long)).array;
		auto s = readln.strip;

		auto need = new long [n + 1];
		auto grassAhead = new long [n + 1];
		long cur = 0;
		need[n] = cur;
		grassAhead[n] = 0;
		foreach_reverse (i; 0..n)
		{
			grassAhead[i] = grassAhead[i + 1];
			switch (s[i])
			{
				case 'G': cur -= d[i];
				    grassAhead[i] += d[i] * 2; break;
				case 'W': cur -= d[i]; break;
				case 'L': cur += d[i]; break;
				default: assert (false);
			}
			cur = max (cur, 0);
			need[i] = cur;
		}
		debug {writeln (need);}
		debug {writeln (grassAhead);}

		bool seenWater = false;
		long res = 0;
		long energy = 0;
		long walkDist = 0;
		long swimDist = 0;
		long grassFlown = 0;
		foreach (i; 0..n)
		{
			switch (s[i])
			{
				case 'G': energy += d[i];
				    res += d[i] * 5; break;
				case 'W': energy += d[i];
				    seenWater = true;
				    res += d[i] * 3; break;
				case 'L': energy -= d[i];
				    res += d[i] * 1; break;
				default: assert (false);
			}
			if (energy < 0)
			{
				res += (seenWater ? 3 : 5) * (-energy);
				energy = 0;
			}
			if (energy > need[i + 1])
			{
				auto delta = energy - need[i + 1];
				auto deltaGrass = min (delta,
				    grassAhead[i] - grassFlown);
				grassFlown += deltaGrass;
				res -= 2 * deltaGrass;
				res -= 1 * (delta - deltaGrass);
				energy = need[i + 1];
			}
			if (s[i] == 'G')
			{
				grassFlown = max (grassFlown - d[i] * 2, 0);
			}
			debug {writeln (grassFlown);}
		}

		writeln (res);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto d = readln.splitter.map !(to !(long)).array;
		auto s = readln.strip;

		auto need = new long [n + 1];
		auto grassAhead = new long [n + 1];
		long cur = 0;
		need[n] = cur;
		grassAhead[n] = 0;
		foreach_reverse (i; 0..n)
		{
			grassAhead[i] = grassAhead[i + 1];
			switch (s[i])
			{
				case 'G': cur -= d[i];
				    grassAhead[i] += d[i]; break;
				case 'W': cur -= d[i]; break;
				case 'L': cur += d[i]; break;
				default: assert (false);
			}
			cur = max (cur, 0);
			need[i] = cur;
		}
		debug {writeln (need);}

		bool seenWater = false;
		long res = 0;
		long energy = 0;
		long walkDist = 0;
		long swimDist = 0;
		long grassFlown = 0;
		foreach (i; 0..n)
		{
			switch (s[i])
			{
				case 'G': energy += d[i];
				    res += d[i] * 5; break;
				case 'W': energy += d[i];
				    seenWater = true;
				    res += d[i] * 3; break;
				case 'L': energy -= d[i];
				    res += d[i] * 1; break;
				default: assert (false);
			}
			if (energy < 0)
			{
				res += (seenWater ? 3 : 5) * (-energy);
				energy = 0;
			}
			if (energy > need[i + 1])
			{
				auto delta = energy - need[i + 1];
				auto deltaGrass = min (delta,
				    grassAhead[i] - grassFlown);
				grassFlown += deltaGrass;
				res -= 2 * deltaGrass;
				res -= 1 * (delta - deltaGrass);
				energy = need[i + 1];
			}
			if (s[i] == 'G')
			{
				grassFlown = max (grassFlown - d[i], 0);
			}
		}

		writeln (res);
	}
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto d = readln.splitter.map !(to !(long)).array;
		auto s = readln.strip;

		auto need = new long [n + 1];
		auto grassAhead = new long [n + 1];
		long cur = 0;
		need[n] = cur;
		grassAhead[n] = 0;
		foreach_reverse (i; 0..n)
		{
			grassAhead[i] = grassAhead[i + 1];
			switch (s[i])
			{
				case 'G': cur -= d[i];
				    grassAhead[i] += d[i]; break;
				case 'W': cur -= d[i]; break;
				case 'L': cur += d[i]; break;
				default: assert (false);
			}
			cur = max (cur, 0);
			need[i] = cur;
		}
		debug {writeln (need);}

		bool seenWater = false;
		long res = 0;
		long energy = 0;
		long walkDist = 0;
		long swimDist = 0;
		long grassFlown = 0;
		foreach (i; 0..n)
		{
			switch (s[i])
			{
				case 'G': energy += d[i];
				    grassFlown = max (grassFlown - d[i], 0);
				    res += d[i] * 5; break;
				case 'W': energy += d[i];
				    seenWater = true;
				    res += d[i] * 3; break;
				case 'L': energy -= d[i];
				    res += d[i] * 1; break;
				default: assert (false);
			}
			if (energy < 0)
			{
				res += (seenWater ? 3 : 5) * (-energy);
				energy = 0;
			}
			if (energy > need[i + 1])
			{
				auto delta = energy - need[i + 1];
				auto deltaGrass = min (delta,
				    grassAhead[i] - grassFlown);
				res -= 2 * deltaGrass;
				res -= 1 * (delta - deltaGrass);
				energy = need[i + 1];
			}
		}

		writeln (res);
	}
}


Casimir has a string $$$s$$$ which consists of capital Latin letters 'A', 'B', and 'C' only. Each turn he can choose to do one of the two following actions:  he can either erase exactly one letter 'A' and exactly one letter 'B' from arbitrary places of the string (these letters don't have to be adjacent);  or he can erase exactly one letter 'B' and exactly one letter 'C' from arbitrary places in the string (these letters don't have to be adjacent). Therefore, each turn the length of the string is decreased exactly by $$$2$$$. All turns are independent so for each turn, Casimir can choose any of two possible actions.For example, with $$$s$$$ $$$=$$$ "ABCABC" he can obtain a string $$$s$$$ $$$=$$$ "ACBC" in one turn (by erasing the first occurrence of 'B' and the second occurrence of 'A'). There are also many other options for a turn aside from this particular example.For a given string $$$s$$$ determine whether there is a sequence of actions leading to an empty string. In other words, Casimir's goal is to erase all letters from the string. Is there a way to do this?

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Each test case is described by one string $$$s$$$, for which you need to determine if it can be fully erased by some sequence of turns. The string $$$s$$$ consists of capital letters 'A', 'B', 'C' and has a length from $$$1$$$ to $$$50$$$ letters, inclusive.

Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if there is a way to fully erase the corresponding string and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers).

6
ABACAB
ABBA
AC
ABC
CABCBB
BCBCBCBCBCBCBCBC

immutable multi = true;

void solve(int tc)
{
	// A - o1 = 0
	// B - o2 - o1 = 0
	// C - o2 = 0
	auto s = cast(char[])readString;
	int[256] cnt = 0;
	foreach(c; s)
	{
		cnt[c]++;
	}
	if (cnt['B'] - cnt['A'] - cnt['C'] == 0)
		return writeln("YES");
	return writeln("NO");
}

//{{{
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;
// }}}


Are you going to Scarborough Fair?Parsley, sage, rosemary and thyme.Remember me to one who lives there.He once was the true love of mine.Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.Although the girl wants to help, Willem insists on doing it by himself.Grick gave Willem a string of length n.Willem needs to do m operations, each operation has four parameters l, r, c1, c2, which means that all symbols c1 in range [l, r] (from l-th to r-th, including l and r) are changed into c2. String is 1-indexed.Grick wants to know the final string after all the m operations.

The first line contains two integers n and m (1 ≤ n, m ≤ 100). The second line contains a string s of length n, consisting of lowercase English letters. Each of the next m lines contains four parameters l, r, c1, c2 (1 ≤ l ≤ r ≤ n, c1, c2 are lowercase English letters), separated by space.

Output string s after performing m operations described above.

NoteFor the second example:After the first operation, the string is wxxak.After the second operation, the string is waaak.After the third operation, the string is gaaak.

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

void main() {
    int n, m;
    char[] s;

    scan(n, m);
    scan(s);

    foreach (i ; 0 .. m) {
        int l, r;
        char c1, c2;
        scan(l, r, c1, c2);
        l--, r--;

        foreach (j ; l .. r + 1) {
            if (s[j] == c1) s[j] = c2;
        }
    }

    writeln(s);
}

long revnum(long n) {
    long res;

    while (n > 0) {
        res *= 10;
        res += (n % 10);
        n /= 10;
    }

    return res;
}

unittest {
    assert(revnum(0) == 0);
    assert(revnum(8) == 8);
    assert(revnum(10) == 1);
    assert(revnum(123) == 321);
    assert(revnum(10100) == 101);
    writeln("ok");
}

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);
        }
    }
}


There are $$$n$$$ pieces of cake on a line. The $$$i$$$-th piece of cake has weight $$$a_i$$$ ($$$1 \leq i \leq n$$$).The tastiness of the cake is the maximum total weight of two adjacent pieces of cake (i. e., $$$\max(a_1+a_2,\, a_2+a_3,\, \ldots,\, a_{n-1} + a_{n})$$$).You want to maximize the tastiness of the cake. You are allowed to do the following operation at most once (doing more operations would ruin the cake):   Choose a contiguous subsegment $$$a[l, r]$$$ of pieces of cake ($$$1 \leq l \leq r \leq n$$$), and reverse it. The subsegment $$$a[l, r]$$$ of the array $$$a$$$ is the sequence $$$a_l, a_{l+1}, \dots, a_r$$$.If you reverse it, the array will become $$$a_1, a_2, \dots, a_{l-2}, a_{l-1}, \underline{a_r}, \underline{a_{r-1}}, \underline{\dots}, \underline{a_{l+1}}, \underline{a_l}, a_{r+1}, a_{r+2}, \dots, a_{n-1}, a_n$$$.For example, if the weights are initially $$$[5, 2, 1, 4, 7, 3]$$$, you can reverse the subsegment $$$a[2, 5]$$$, getting $$$[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$$$. The tastiness of the cake is now $$$5 + 7 = 12$$$ (while before the operation the tastiness was $$$4+7=11$$$).Find the maximum tastiness of the cake after doing the operation at most once.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 50$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 1000$$$) — the number of pieces of cake. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the weight of the $$$i$$$-th piece of cake.

For each test case, print a single integer: the maximum tastiness of the cake after doing the operation at most once.

NoteIn the first test case, after reversing the subsegment $$$a[2, 5]$$$, you get a cake with weights $$$[5, \underline{7}, \underline{4}, \underline{1}, \underline{2}, 3]$$$. The tastiness of the cake is now $$$\max(5+7, 7+4, 4+1, 1+2, 2+3) = 12$$$. This is the maximum possible tastiness of the cake one can obtain by performing the operation at most once.In the second test case, it's optimal not to do any operation. The tastiness is $$$78+78 = 156$$$.In the third test case, after reversing the subsegment $$$a[1, 2]$$$, you get a cake with weights $$$[\underline{54}, \underline{69}, 91]$$$. The tastiness of the cake is now $$$\max(54+69, 69+91) = 160$$$. There is no way to make the tastiness of the cake greater than $$$160$$$ by performing at most one operation.

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		a[$ - 2..$].sum.writeln;
	}
}


Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given $$$n$$$ days from Petya's live and you have to determine what happened with his flower in the end.The flower grows as follows:   If the flower isn't watered for two days in a row, it dies.  If the flower is watered in the $$$i$$$-th day, it grows by $$$1$$$ centimeter.  If the flower is watered in the $$$i$$$-th and in the $$$(i-1)$$$-th day ($$$i > 1$$$), then it grows by $$$5$$$ centimeters instead of $$$1$$$.  If the flower is not watered in the $$$i$$$-th day, it does not grow. At the beginning of the $$$1$$$-st day the flower is $$$1$$$ centimeter tall. What is its height after $$$n$$$ days?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains the only integer $$$n$$$ ($$$1 \leq n \leq 100$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$). If $$$a_i = 1$$$, the flower is watered in the $$$i$$$-th day, otherwise it is not watered.

For each test case print a single integer $$$k$$$ — the flower's height after $$$n$$$ days, or $$$-1$$$, if the flower dies.

import std;
void main() {
	auto t = readln.strip.to!int;
	Attempt: foreach(tt; 0 .. t) {
		uint cm = 1;
		readln;
		auto ai = readln.split.to!(int[]);
		if(ai[0] == 1) {
			cm++;
		}
		if(ai.length >= 2) {
			foreach(i, a; ai[1 .. $]) {
				if(a == 0 && ai[i] == 0) {
					writeln("-1");
					continue Attempt;
				} else if(a == 1 && ai[i] == 1) {
					cm += 5;
				} else if(a == 1) {
					cm++;
				}
			}
		}
		writeln(cm);
	}
}

immutable multi = true;

void solve(int tc)
{
  auto n = readInt!int;
  auto a = ma(n, readInt!int);
  int height = 1;
  foreach(i, ai; a)
    {
      final switch(ai)
	{
	case 0:
	  if (i > 0 && a[i-1] == 0) return writeln(-1);
	  break;
	case 1:
	  if (i > 0 && a[i-1] == 1) height += 5;
	  else height += 1;
	  break;
	}
    }
  height.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;
void main() {
	writeln("started");
	auto t = readln.strip.to!int;
	Attempt: foreach(tt; 0 .. t) {
		uint cm = 1;
		readln;
		auto ai = readln.split.to!(int[]);
		if(ai[0] == 1) {
			cm++;
		}
		if(ai.length >= 2) {
			foreach(i, a; ai[1 .. $]) {
				if(a == 0 && ai[i] == 0) {
					writeln("-1");
					continue Attempt;
				} else if(a == 1 && ai[i] == 1) {
					cm += 5;
				} else if(a == 1) {
					cm++;
				}
			}
		}
		writeln(cm);
	}
}


Little Petya likes arrays that consist of non-negative integers a lot. Recently his mom has presented him one such array consisting of n elements. Petya immediately decided to find there a segment of consecutive elements, such that the xor of all numbers from this segment was maximal possible. Help him with that.The xor operation is the bitwise exclusive "OR", that is denoted as "xor" in Pascal and "^" in C/C++/Java.

The first line contains integer n (1 ≤ n ≤ 100) — the number of elements in the array. The second line contains the space-separated integers from the array. All numbers are non-negative integers strictly less than 230.

Print a single integer — the required maximal xor of a segment of consecutive elements.

NoteIn the first sample one of the optimal segments is the segment that consists of the first and the second array elements, if we consider the array elements indexed starting from one.The second sample contains only one optimal segment, which contains exactly one array element (element with index three).

import std.stdio : writeln, writefln;
import std.array;
import std.range;
import std.algorithm : filter, max;
import std.math : sqrt;

void main(){
	int n = next!int();
	int max_;
	int[] list = new int[n];
	foreach(ref v; list){
		v = next!int();
	}

	for(int i=0; i<n; ++i){
		int r = 0;
		for(int j=0; i+j<n; ++j){
			r ^= list[i+j];
			max_ = max(r, max_);
		}
	}

	max_.writeln;
}

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

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

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

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

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

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

	return v_;
}


In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Write n lines, each line should contain a cell coordinates in the other numeration system.

import std.stdio;
import std.algorithm;
import std.regex;

void tr1(in string s) {
    int r, c;
    s.ptr.sscanf("R%dC%d".ptr, &r, &c);
    char[] cs;
    while (c > 0) {
        if (c % 26 == 0) {
            cs ~= 'Z';
            c = (c - 26) / 26;
        } else {
            cs ~= ('A' + c % 26 - 1);
            c /= 26;
        }
    }
    cs.reverse();
    writef("%s%d\n", cs, r);
}

void tr2(in string s) {
    auto m = s.match(regex("([A-Z]+)([0-9]+)")).captures;
    auto cs = m[1], rs = m[2];
    int t = 0;
    foreach (i, c; cs) {
        t = t * 26 + c - 'A' + 1;
    }
    writef("R%sC%d\n", rs, t);
}

void tr(in string s) {
    if (!s.match(regex("R[0-9]+C[0-9]+")).empty()) {
    //if (s[0] == 'R') {
        tr1(s);
    } else {
        tr2(s);
    }
}

void main() {
    int n; readf("%d\n", &n);
    foreach (_; 0 .. n) {
        tr(stdin.readln());
    }
}

import std.stdio, std.regex, std.conv;

immutable rx1 = r"^([A-Z]+)(\d+)$";
immutable rx2 = r"^R(\d+)C(\d+)$";

void main() {
	long n;
	readf("%s\n", &n);
	foreach (i; 0 .. n) {
		string s;
		readf("%s\n", &s);
		auto m = match(s, rx1);
		if (m) {
			long col = 0;
			foreach(c; m.captures[1]) {
				col *= 26;
				col += c - 'A' + 1;
			}
			writefln("R%sC%d", m.captures[2], col);
		} else {
			m = match(s, rx2);
			long col = to!long(m.captures[2]);
			string res = "";
			while(col) {
				if (col % 26) {
					res = ((col % 26) - 1 + 'A') ~ res;
				} else {
					res = 'Z' ~ res;
					col -= 26;
				}
				col /= 26;
			}
			writefln("%s%s", res, m.captures[1]);
		}
	}
}

import std.stdio, std.regex, std.conv;

static immutable string excelMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static immutable int base = excelMap.length;

ulong excelToDec(string excel) {
    ulong dec = 0, pow = 1;

    for (int i = excel.length-1; i >= 0; --i) {
        dec += (excel[i]-'A'+1) * pow;
        pow *= base;
    }

    return dec;
}

string decToExcel(ulong dec) {
    string s;

    while (dec) {
        s = cast(char)(excelMap[--dec%base]) ~ s;
        dec /= base;
    }

    return s;
}

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

    for (int i = 0; i < n; ++i) {
        auto s = readln();
        auto m = match(s, r"^R([0-9]+)C([0-9]+)");
        if (m) {
            writefln("%s%s", decToExcel(to!int(m.captures[2])), m.captures[1]);
        } else {
            m = match(s, r"^([A-Z]+)([0-9]+)");
            writefln("R%sC%s", m.captures[2], excelToDec(m.captures[1]));
        }
    }
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

struct Cell
{
	public int column;
	public int row;

	public int from_string(string col)
	{
		int res = 0;
		for (int i = col.length - 1; i >= 0; i--)
		{
			res += ((col[i] - 'A') + 1) * pow(26, col.length - i - 1);
		}
		return res;
	}

	public string toMode1()
	{
		string s = "R" ~ to!string(row) ~ "C" ~ to!string(column);
		return s;
	}

	public string toMode2()
	{
		int c = column;
		string s = "";
		while (c > 0)
		{
			c--;
			int lc = c % 26;
			if (lc < 0) lc += 26;
			s ~= cast(char)(lc + 'A');
			c = c / 26;
		}
		char[] ms = s.dup;
		reverse(ms);
		return to!string(ms) ~ to!string(row);
	}
}

int main(string[] argv)
{
	int n;
	scanf("%d\n", &n);
	auto mode1 = r"^R([0-9]+)C([0-9]+$)".regex;
	auto mode2 = r"^([A-Z]+)([0-9]+)$".regex;
	foreach (int i; 0..n)
	{
		string expression = readline();
		Cell c;
		auto m = matchFirst(expression, mode1);
		if (m.empty)
		{
			m = matchFirst(expression, mode2);
			c.column = c.from_string(m[1]);
			c.row = to!int(m[2]);
			write(c.toMode1(), "\n");
		} else {
			c.row = to!int(m[1]);
			c.column = to!int(m[2]);
			write(c.toMode2(), "\n");
		}
	}
	return 0;
}

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

void main() {
  int n = readln.chomp.to!int;
  auto rc = regex(r"^R(\d+)C(\d+)$");
  auto alnum = regex(r"^([A-Z]+)(\d+)$");

  foreach (i; 0..n) {
    string coord = readln.chomp;
    auto rccap = coord.matchFirst(rc);
    if (!rccap.empty) {
      int row = rccap[1].to!int;
      int col = rccap[2].to!int;
      auto newcol = numToAl(col);
      writeln(newcol ~ rccap[1]);
    } else {
      auto alnumcap = coord.matchFirst(alnum);
      writeln("R" ~ alnumcap[2] ~ "C" ~ alToNum(alnumcap[1]).to!string);
    }
  }
}

int alToNum(string row) {
  int ans;

  foreach (int i, c; (cast(char[])row).reverse) {
    int al = c - 'A' + 1;
    ans += al * pow(26, i);
  }

  return ans;
}

char[] numToAl(int row) {
  char[] ans;
  while (row > 0) {
    ans ~= ((row - 1) % 26) + 'A';
    row = (row - 1) / 26;
  }
  return ans.reverse;
}

import std.stdio;
import std.algorithm;
import std.regex;

void tr1(in string s) {
    int r, c;
    s.ptr.sscanf("R%dC%d".ptr, &r, &c);
    char[] cs;
    while (c > 0) {
        if (c % 26 == 0) {
            cs ~= 'Z';
            c = (c - 26) % 26;
        } else {
            cs ~= ('A' + c % 26 - 1);
            c /= 26;
        }
    }
    cs.reverse();
    writef("%s%d\n", cs, r);
}

void tr2(in string s) {
    auto m = s.match(regex("([A-Z]+)([0-9]+)")).captures;
    auto cs = m[1], rs = m[2];
    int t = 0;
    foreach (i, c; cs) {
        t = t * 26 + c - 'A' + 1;
    }
    writef("R%sC%d\n", rs, t);
}

void tr(in string s) {
    if (s[0] == 'R') {
        tr1(s);
    } else {
        tr2(s);
    }
}

void main() {
    int n; readf("%d\n", &n);
    foreach (_; 0 .. n) {
        tr(stdin.readln());
    }
}

import std.stdio;
import std.algorithm;
import std.regex;

void tr1(in string s) {
    int r, c;
    s.ptr.sscanf("R%dC%d".ptr, &r, &c);
    char[] cs;
    while (c > 0) {
        if (c % 26 == 0) {
            cs ~= 'Z';
            c = (c - 26) % 26;
        } else {
            cs ~= ('A' + c % 26 - 1);
            c /= 26;
        }
    }
    cs.reverse();
    writef("%s%d\n", cs, r);
}

void tr2(in string s) {
    auto m = s.match(regex("([A-Z]+)([0-9]+)")).captures;
    auto cs = m[1], rs = m[2];
    int t = 0;
    foreach (i, c; cs) {
        t = t * 26 + c - 'A' + 1;
    }
    writef("R%sC%d\n", rs, t);
}

void tr(in string s) {
    if (!s.match(regex("R[0-9]+C[0-9]+")).empty()) {
    //if (s[0] == 'R') {
        tr1(s);
    } else {
        tr2(s);
    }
}

void main() {
    int n; readf("%d\n", &n);
    foreach (_; 0 .. n) {
        tr(stdin.readln());
    }
}

import std.stdio, std.regex, std.conv;

static immutable string excelMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static immutable int base = excelMap.length;

ulong excelToDec(string excel) {
    ulong dec = 0, pow = 1;

    for (int i = excel.length-1; i >= 0; --i) {
        dec += (excel[i]-'A'+1) * pow;
        pow *= base;
    }

    return dec;
}

string decToExcel(ulong dec) {
    string s;

    while (dec) {
        s = cast(char)(excelMap[--dec%base]) ~ s;
        dec /= base;
    }

    return s;
}

void main() {
    writeln(decToExcel(494));

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

    for (int i = 0; i < n; ++i) {
        auto s = readln();
        auto m = match(s, r"^R([0-9]+)C([0-9]+)");
        if (m) {
            writefln("%s%s", decToExcel(to!int(m.captures[2])), m.captures[1]);
        } else {
            m = match(s, r"^([A-Z]+)([0-9]+)");
            writefln("R%sC%s", m.captures[2], excelToDec(m.captures[1]));
        }
    }
}

import std.stdio, std.regex, std.conv;

static immutable string excelMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static immutable int base = excelMap.length;

ulong excelToDec(string excel) {
    ulong dec = 0, pow = 1;

    for (int i = excel.length-1; i >= 0; --i) {
        dec += (excel[i]-'A'+1) * pow;
        pow *= base;
    }

    return dec;
}

string decToExcel(ulong dec) {
    string s;

    while (dec) {
        s = cast(char)(excelMap[dec%base]-1) ~ s;
        dec /= base;
    }

    return s;
}

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

    for (int i = 0; i < n; ++i) {
        auto s = readln();
        auto m = match(s, r"^R([0-9]+)C([0-9]+)");
        if (m) {
            writefln("%s%s", decToExcel(to!int(m.captures[2])), m.captures[1]);
        }
        else {
            m = match(s, r"^([A-Z]+)([0-9]+)");
            writefln("R%sC%s", m.captures[2], excelToDec(m.captures[1]));
        }
    }
}

import std.stdio, std.regex, std.conv;

static immutable string excelMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static immutable int base = excelMap.length;

ulong excelToDec(string excel) {
    ulong dec = 0, pow = 1;

    for (int i = excel.length-1; i >= 0; --i) {
        dec += (excel[i]-'A'+1) * pow;
        pow *= base;
    }

    return dec;
}

string decToExcel(ulong dec) {
    string s;

    while (dec) {
        s = cast(char)(excelMap[dec%base]-1) ~ s;
        dec /= base;
    }

    return s;
}

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

    for (int i = 0; i < n; ++i) {
        auto s = readln();
        writeln(s);
        auto m = match(s, r"^R([0-9]+)C([0-9]+)");
        if (m)
            writefln("%s%s", decToExcel(to!int(m.captures[2])), m.captures[1]);
        else
            m = match(s, r"^([A-Z]+)([0-9]+)");
            writefln("R%sC%s", m.captures[2], excelToDec(m.captures[1]));
    }
}

import std.stdio, std.regex, std.conv;

static immutable string excelMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static immutable int base = excelMap.length;

ulong excelToDec(string excel) {
    ulong dec = 0, pow = 1;

    for (int i = excel.length-1; i >= 0; --i) {
        dec += (excel[i]-'A'+1) * pow;
        pow *= base;
    }

    return dec;
}

string decToExcel(ulong dec) {
    string s;

    while (dec) {
        s = cast(char)(excelMap[dec%base]-1) ~ s;
        dec /= base;
    }

    return s;
}

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

    for (int i = 0; i < n; ++i) {
        auto s = readln();
        auto m = match(s, r"^R([0-9]+)C([0-9]+)");
        if (m)
            writefln("%s%s", decToExcel(to!int(m.captures[2])), m.captures[1]);
        else
            m = match(s, r"^([A-Z]+)([0-9]+)");
            writefln("R%sC%s", m.captures[2], excelToDec(m.captures[1]));
    }
}

import std.stdio;
import std.algorithm;
import std.regex;
import std.math;
import std.conv;

string readline()
{
	string l = readln();
	if (l[$-1] == '\n') l.length--;
	return l;
}

struct Cell
{
	public int column;
	public int row;

	public int from_string(string col)
	{
		int res = 0;
		for (int i = col.length - 1; i >= 0; i--)
		{
			res += ((col[i] - 'A') + 1) * pow(26, col.length - i - 1);
		}
		return res;
	}

	public string toMode1()
	{
		string s = "R" ~ to!string(row) ~ "C" ~ to!string(column);
		return s;
	}

	public string toMode2()
	{
		int c = column;
		string s = "";
		while (c > 0)
		{
			s ~= cast(char)((c % 26) + 'A' - 1);
			c = c / 26;
		}
		char[] ms = s.dup;
		reverse(ms);
		return to!string(ms) ~ to!string(row);
	}
}

int main(string[] argv)
{
	int n;
	scanf("%d\n", &n);
	auto mode1 = r"^R([0-9]+)C([0-9]+$)".regex;
	auto mode2 = r"^([A-Z]+)([0-9]+)$".regex;
	foreach (int i; 0..n)
	{
		string expression = readline();
		Cell c;
		auto m = matchFirst(expression, mode1);
		if (m.empty)
		{
			m = matchFirst(expression, mode2);
			c.column = c.from_string(m[1]);
			c.row = to!int(m[2]);
			write(c.toMode1(), "\n");
		} else {
			c.row = to!int(m[1]);
			c.column = to!int(m[2]);
			write(c.toMode2(), "\n");
		}
	}
	return 0;
}

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

void main() {
  int n = readln.chomp.to!int;
  auto rc = regex(r"^R(\d+)C(\d+)$");
  auto alnum = regex(r"^([A-Z]+)(\d+)$");

  foreach (i; 0..n) {
    string coord = readln.chomp;
    auto rccap = coord.matchFirst(rc);
    if (!rccap.empty) {
      int row = rccap[1].to!int;
      int col = rccap[2].to!int;
      auto newcol = numToAl(col);
      writeln(newcol ~ rccap[1]);
    } else {
      auto alnumcap = coord.matchFirst(alnum);
      writeln("R" ~ alnumcap[2] ~ "C" ~ alToNum(alnumcap[1]).to!string);
    }
  }
}

int alToNum(string row) {
  int ans;

  foreach (int i, c; (cast(char[])row).reverse) {
    int al = c - 'A' + 1;
    ans += al * pow(26, i);
  }

  return ans;
}

char[] numToAl(int row) {
  char[] ans;
  while (row > 25) {
    ans ~= (row % 26) + 'A' - 1;
    row /= 26;
  }

  ans ~= (row + 'A' - 1);
  return ans.reverse;
}


You are given an undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Your task is to find the number of connected components which are cycles.Here are some definitions of graph theory.An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex $$$a$$$ is connected with a vertex $$$b$$$, a vertex $$$b$$$ is also connected with a vertex $$$a$$$). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices.Two vertices $$$u$$$ and $$$v$$$ belong to the same connected component if and only if there is at least one path along edges connecting $$$u$$$ and $$$v$$$.A connected component is a cycle if and only if its vertices can be reordered in such a way that:  the first vertex is connected with the second vertex by an edge,  the second vertex is connected with the third vertex by an edge,  ...  the last vertex is connected with the first vertex by an edge,  all the described edges of a cycle are distinct. A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices.    There are $$$6$$$ connected components, $$$2$$$ of them are cycles: $$$[7, 10, 16]$$$ and $$$[5, 11, 9, 15]$$$.

The first line contains two integer numbers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le m \le 2 \cdot 10^5$$$) — number of vertices and edges. The following $$$m$$$ lines contains edges: edge $$$i$$$ is given as a pair of vertices $$$v_i$$$, $$$u_i$$$ ($$$1 \le v_i, u_i \le n$$$, $$$u_i \ne v_i$$$). There is no multiple edges in the given graph, i.e. for each pair ($$$v_i, u_i$$$) there no other pairs ($$$v_i, u_i$$$) and ($$$u_i, v_i$$$) in the list of edges.

Print one integer — the number of connected components which are also cycles.

NoteIn the first example only component $$$[3, 4, 5]$$$ is also a cycle.The illustration above corresponds to the second example.

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

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

    auto g = new int [][] (n+1);
    foreach (_; 0..m) {
        auto a = readln.split.map!(to!int).array;
        (2).iota.each!(i => g[a[i]] ~= a[1-i]);
    }

    auto vis = false.repeat(n+1).array;

    int[] bfs (int s) {
        auto r = [s];
        auto q = SList!int(s);
        while (!q.empty()) {
            auto v = q.front();
            q.removeFront();
            if (vis[v]) continue;
            vis[v] = true;
            r ~= v;
            foreach (u; g[v]) if (!vis[u]) q.insertFront(u);
        }
        return r;
    }

    auto isCycle = (int[] comp) => comp.length > 2 && comp.all!(v => g[v].length == 2);
    int ans = 0;
    foreach (i; 1..n+1) {
        debug { writeln(g[i]); }
        if (vis[i]) continue;
        ans += isCycle(bfs(i).array);
    }

    writeln(ans);
}

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

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

    auto g = new int [][] (n+1);
    foreach (_; 0..m) {
        auto a = readln.split.map!(to!int).array;
        (2).iota.each!(i => g[a[i]] ~= a[1-i]);
    }

    auto vis = false.repeat(n+1).array;

    void dfs (int v, ref int[] cur) {
        vis[v] = true;
        cur ~= v;

        foreach (u; g[v]) {
            if (!vis[u]) dfs(u, cur);
        }
    }

    auto isCycle = (int[] comp) => comp.length > 2 && comp.all!(v => g[v].length == 2);
    int ans = 0;
    foreach (i; 1..n+1) {
        debug { writeln(g[i]); }
        if (vis[i]) continue;
        int[] comp;
        dfs(i, comp);
        ans += isCycle(comp);
    }

    writeln(ans);
}

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

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

    auto g = new int [][] (n+1);
    foreach (_; 0..m) {
        auto a = readln.split.map!(to!int).array;
        (2).iota.each!(i => g[a[i]] ~= a[1-i]);
    }

    auto vis = false.repeat(n+1).array;

    SList!int bfs (int s) {
        auto r = SList!int(s);
        auto q = SList!int(s);
        while (!q.empty()) {
            auto v = q.front();
            q.removeFront();
            if (vis[v]) continue;
            vis[v] = true;
            r.insertFront(v);
            foreach (u; g[v]) if (!vis[u]) q.insertFront(u);
        }
        return r;
    }

    auto isCycle = (int[] comp) => comp.length > 2 && comp.all!(v => g[v].length == 2);
    int ans = 0;
    foreach (i; 1..n+1) {
        debug { writeln(g[i]); }
        if (vis[i]) continue;
        ans += isCycle(bfs(i).array);
    }

    writeln(ans);
}

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

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

    auto g = new int [][] (n+1);
    foreach (_; 0..m) {
        auto a = readln.split.map!(to!int).array;
        (2).iota.each!(i => g[a[i]] ~= a[1-i]);
    }

    auto vis = false.repeat(n+1).array;

    int[] bfs (int s) {
        auto r = Array!int(s);
        auto q = SList!int(s);
        while (!q.empty()) {
            auto v = q.front();
            q.removeFront();
            if (vis[v]) continue;
            vis[v] = true;
            r ~= v;
            foreach (u; g[v]) if (!vis[u]) q.insertFront(u);
        }
        return r.array;
    }

    int[][] comps;
    foreach (i; 1..n+1) {
        debug { writeln(g[i]); }
        if (vis[i]) continue;
        comps ~= bfs(i);
    }

    auto isCycle = (int[] comp) => comp.length > 2 && comp.all!(v => g[v].length == 2);
    auto ans = comps.count!(isCycle);

    writeln(ans);
}

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

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

    auto g = new int [][] (n+1);
    foreach (_; 0..m) {
        auto a = readln.split.map!(to!int).array;
        (2).iota.each!(i => g[a[i]] ~= a[1-i]);
    }

    auto vis = false.repeat(n+1).array;

    bool dfs (int v, int fr, int fin) {
        vis[v] = true;
        if (g[v].length != 2) return false;

        foreach (u; g[v]) {
            if (u == fr) continue;
            if (vis[u]) return u == fin;
            return dfs(u, v, fin);
        }

        assert(false);
    }

    int ans = 0;
    foreach (i; 1..n+1) {
        debug { writeln(g[i]); }
        if (vis[i]) continue;
        ans += dfs(i, -1, i);
    }

    writeln(ans);
}

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

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

    auto g = new int [][] (n+1);
    foreach (_; 0..m) {
        auto a = readln.split.map!(to!int).array;
        (2).iota.each!(i => g[a[i]] ~= a[1-i]);
    }

    auto vis = false.repeat(n+1).array;

    void dfs (int v, ref int[] cur) {
        vis[v] = true;
        cur ~= v;

        foreach (u; g[v]) {
            if (!vis[u]) dfs(u, cur);
        }
    }

    int[][] comps;
    foreach (i; 1..n+1) {
        debug { writeln(g[i]); }
        if (vis[i]) continue;
        int[] comp;
        dfs(i, comp);
        comps ~= comp;
    }

    auto isCycle = (int[] comp) => comp.length > 2 && comp.all!(v => g[v].length == 2);
    auto ans = comps.count!(isCycle);

    writeln(ans);
}

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

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int n, m; readV(n, m);

  auto g = Graph!int(n), uf = UnionFind!int(n);
  foreach (i; 0..m) {
    int a, b; readV(a, b); --a; --b;
    g.addEdgeB(a, b);
    uf.unite(a, b);
  }

  writeln(uf.groups.count!(gi => gi.all!(vi => g[vi].length == 2)));
}

struct Graph(N = int)
{
  alias Node = N;
  Node n;
  Node[][] g;
  alias g this;
  this(Node n) { this.n = n; g = new Node[][](n); }
  void addEdge(Node u, Node v) { g[u] ~= v; }
  void addEdgeB(Node u, Node v) { g[u] ~= v; g[v] ~= u; }
}

struct UnionFind(T)
{
  import std.algorithm, std.range;

  T[] p; // parent
  const T s; // sentinel
  const T n;
  T countForests; // number of forests
  T[] countNodes; // number of nodes in forests

  this(T n)
  {
    this.n = n;
    s = n;
    p = new T[](n);
    p[] = s;
    countForests = n;
    countNodes = new T[](n);
    countNodes[] = 1;
  }

  T opIndex(T i)
  {
    if (p[i] == s) {
      return i;
    } else {
      p[i] = this[p[i]];
      return p[i];
    }
  }

  bool unite(T i, T j)
  {
    auto pi = this[i], pj = this[j];
    if (pi != pj) {
      p[pj] = pi;
      --countForests;
      countNodes[pi] += countNodes[pj];
      return true;
    } else {
      return false;
    }
  }

  auto countNodesOf(T i) { return countNodes[this[i]]; }
  bool isSame(T i, T j) { return this[i] == this[j]; }

  auto groups()
  {
    auto g = new T[][](n);
    foreach (i; 0..n) g[this[i]] ~= i;
    return g.filter!(l => !l.empty);
  }
}

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

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

    auto g = new int [][] (n+1);
    foreach (_; 0..m) {
        auto a = readln.split.map!(to!int).array;
        (2).iota.each!(i => g[a[i]] ~= a[1-i]);
    }

    auto vis = false.repeat(n+1).array;

    void dfs (int v, ref int[] cur) {
        vis[v] = true;
        cur ~= v;

        foreach (u; g[v]) {
            if (vis[u]) continue;
            return dfs(u, cur);
        }
    }

    int[][] comps;
    foreach (i; 1..n+1) {
        debug { writeln(g[i]); }
        if (vis[i]) continue;
        int[] comp;
        dfs(i, comp);
        comps ~= comp;
    }

    auto isCycle = (int[] comp) => comp.length > 2 && comp.all!(v => g[v].length == 2);
    auto ans = comps.count!(isCycle);

    writeln(ans);
}


This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.There are $$$n$$$ players labelled from $$$1$$$ to $$$n$$$. It is guaranteed that $$$n$$$ is a multiple of $$$3$$$.Among them, there are $$$k$$$ impostors and $$$n-k$$$ crewmates. The number of impostors, $$$k$$$, is not given to you. It is guaranteed that $$$\frac{n}{3} < k < \frac{2n}{3}$$$.In each question, you can choose three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \le a, b, c \le n$$$) and ask: "Among the players labelled $$$a$$$, $$$b$$$ and $$$c$$$, are there more impostors or more crewmates?" You will be given the integer $$$0$$$ if there are more impostors than crewmates, and $$$1$$$ otherwise.Find the number of impostors $$$k$$$ and the indices of players that are impostors after asking at most $$$2n$$$ questions.The jury is adaptive, which means the indices of impostors may not be fixed beforehand and can depend on your questions. It is guaranteed that there is at least one set of impostors which fulfills the constraints and the answers to your questions at any time.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first and only line of each test case contains a single integer $$$n$$$ ($$$6 \le n < 10^4$$$, $$$n$$$ is a multiple of $$$3$$$) — the number of players. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^4$$$.

NoteExplanation for example interaction (note that this example only exists to demonstrate the interaction procedure and does not provide any hint for the solution):For the first test case:Question "? 1 2 3" returns $$$0$$$, so there are more impostors than crewmates among players $$$1$$$, $$$2$$$ and $$$3$$$.Question "? 3 4 5" returns $$$1$$$, so there are more crewmates than impostors among players $$$3$$$, $$$4$$$ and $$$5$$$.Outputting "! 3 4 1 2" means that one has found all the impostors, by some miracle. There are $$$k = 3$$$ impostors. The players who are impostors are players $$$4$$$, $$$1$$$ and $$$2$$$.For the second test case:Question "? 7 1 9" returns $$$1$$$, so there are more crewmates than impostors among players $$$7$$$, $$$1$$$ and $$$9$$$.Outputting "! 4 2 3 6 8" means that one has found all the impostors, by some miracle. There are $$$k = 4$$$ impostors. The players who are impostors are players $$$2$$$, $$$3$$$, $$$6$$$ and $$$8$$$.

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

void solve (int n)
{
	assert (n % 3 == 0);

	int ask (int a, int b, int c)
	{
		writeln ("? ", a, " ", b, " ", c);
		stdout.flush ();
		return readln.strip.to !(int);
	}

	int [] q;
	for (int i = 0; i < n; i += 3)
	{
		q ~= ask (i + 1, i + 2, i + 3);
	}

	auto t0 = q.countUntil (0) * 3;
	auto t1 = q.countUntil (1) * 3;
	auto six = [t0 + 1, t0 + 2, t0 + 3, t1 + 1, t1 + 2, t1 + 3];
	int pos = 1;
	while (ask (six[pos + 0], six[pos + 1], six[pos + 2]) == 0)
	{
		pos += 1;
	}

	int p0 = six[pos - 1];
	int p1 = six[pos + 2];

	auto v = new bool [n + 1];
	for (int i = 0; i < n; i += 3)
	{
		auto three = [i + 1, i + 2, i + 3];
		if (q[i / 3] == 0)
		{
			int x = ask (three[0], three[1], p1);
			int y = ask (three[0], three[2], p1);
			int z = (x == 1 && y == 1) ? 0 :
			        (x == 0 && y == 0) ? 0 : 1;
			v[three[0]] = 0;
			v[three[1]] = 0;
			v[three[2]] = 0;
			if (x == 1 && y == 1) v[three[0]] = 1;
			if (x == 1 && z == 1) v[three[1]] = 1;
			if (y == 1 && z == 1) v[three[2]] = 1;
		}
		else
		{
			int x = ask (three[0], three[1], p0);
			int y = ask (three[0], three[2], p0);
			int z = (x == 0 && y == 0) ? 1 :
			        (x == 1 && y == 1) ? 1 : 0;
			v[three[0]] = 1;
			v[three[1]] = 1;
			v[three[2]] = 1;
			if (x == 0 && y == 0) v[three[0]] = 0;
			if (x == 0 && z == 0) v[three[1]] = 0;
			if (y == 0 && z == 0) v[three[2]] = 0;
		}
	}

	auto answer = iota (1, n + 1).filter !(i => v[i] == 0).array;
	writefln ("! %s %(%s %)", answer.length, answer);
	stdout.flush ();
}

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

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

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

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

void solve() {
    int N = read!int;
    int M = N / 3;
    int[] G;
    for (int i = 0; i < M; i++) {
        int x = ask(3*i+1, 3*i+2, 3*i+3);
        G ~= x;
    }
    int find_ic() {
        for (int i = 0; i+1 < M; i++) {
            if (G[i] != G[i + 1]) {
                // 3*i+1 ~ 3*i+6
                int[] p;
                for (int k = 3*i+1; k <= 3*i+4; k++) {
                    int x = ask(k, k+1, k+2);
                    if (!p.empty && p.back != x) {
                        // (k, k+1) is (i,c) or (c,i)
                        return k;
                    } else {
                        p ~= x;
                    }
                }
            }
        }
        assert(false);
    }

    int k = find_ic();
    int[] Is, Cs;
    for (int i = 1; i <= N; i++) {
        if (i == k || i == k+1) continue;
        int x = ask(k, k+1, i);
        if (x == 0) {
            Is ~= i;
        } else {
            Cs ~= i;
        }
    }
    int x = ask(Is.back, Cs.back, k);
    if (x == 0) {
        Is ~= k;
        Cs ~= k + 1;
    } else {
        Is ~= k + 1;
        Cs ~= k;
    }
    writefln("! %s %(%s %)", Is.length, Is);
    stdout.flush;
}

int ask(int a, int b, int c) {
    writefln("? %s %s %s", a, b, c);
    stdout.flush;
    return read!int;
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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

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

		auto ans = new long[](n);
		ans[] = -1;
		auto r = new long[](n);
		foreach (i; 1..n-1)
		{
			writeln("? ", i, " ", i+1, " ", i+2);
			stdout.flush;
			r[i] = RD;
		}
		long x, y;
		foreach (i; 1..n-2)
		{
			if (r[i] != r[i+1])
			{
				ans[i-1] = r[i];
				ans[i+2] = r[i+1];
				x = i-1;
				y = i+2;
			}
		}
		foreach (i; 0..n)
		{
			if (ans[i] != -1) continue;
			writeln("? ", x+1, " ", y+1, " ", i+1);
			stdout.flush;
			ans[i] = RD;
		}
		long[] arr;
		foreach (i; 0..n)
		{
			if (ans[i] == 0)
				arr ~= i+1;
		}
		writeln("! ", arr.length, " ", arr.map!(to!string).join(" "));
		stdout.flush;
	}

	debug readln;
}

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

void solve (int n)
{
	assert (n % 3 == 0);

	int ask (int a, int b, int c)
	{
		writeln ("? ", a, " ", b, " ", c);
		stdout.flush ();
		return readln.strip.to !(int);
	}

	int [] q;
	for (int i = 0; i < n; i += 3)
	{
		q ~= ask (i + 1, i + 2, i + 3);
	}

	auto t0 = q.countUntil (0) * 3;
	auto t1 = q.countUntil (1) * 3;
	auto six = [t0 + 1, t0 + 2, t0 + 3, t1 + 1, t1 + 2, t1 + 3];
	int pos = 1;
	while (ask (six[pos + 0], six[pos + 1], six[pos + 2]) == 0)
	{
		pos += 1;
	}

	int p0 = six[pos - 1];
	int p1 = six[pos + 2];

	auto v = new bool [n + 1];
	for (int i = 0; i < n; i += 3)
	{
		auto three = [i + 1, i + 2, i + 3];
		if (q[i / 3] == 0)
		{
			int x = ask (three[0], three[1], p1);
			int y = ask (three[0], three[2], p1);
			int z = (x == 0 && y == 0) ? 0 : 1;
			v[three[0]] = 0;
			v[three[1]] = 0;
			v[three[2]] = 0;
			if (x == 1 && y == 1) v[three[0]] = 1;
			if (x == 1 && z == 1) v[three[1]] = 1;
			if (y == 1 && z == 1) v[three[2]] = 1;
		}
		else
		{
			int x = ask (three[0], three[1], p0);
			int y = ask (three[0], three[2], p0);
			int z = (x == 1 && y == 1) ? 1 : 0;
			v[three[0]] = 1;
			v[three[1]] = 1;
			v[three[2]] = 1;
			if (x == 0 && y == 0) v[three[0]] = 0;
			if (x == 0 && z == 0) v[three[1]] = 0;
			if (y == 0 && z == 0) v[three[2]] = 0;
		}
	}

	auto answer = iota (1, n + 1).filter !(i => v[i] == 0).array;
	writefln ("! %s %(%s %)", answer.length, answer);
	stdout.flush ();
}

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

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

void solve (int n)
{
	assert (n % 3 == 0);

	int askImpl () (int a, int b, int c)
	{
		writeln ("? ", a, " ", b, " ", c);
		stdout.flush ();
		return readln.strip.to !(int);
	}

	alias ask = memoize !(askImpl !());

	int [] q;
	for (int i = 0; i < n; i += 3)
	{
		q ~= ask (i + 1, i + 2, i + 3);
	}

	auto t0 = q.countUntil (0) * 3;
	auto t1 = q.countUntil (1) * 3;
	auto six = [t0 + 1, t0 + 2, t0 + 3, t1 + 1, t1 + 2, t1 + 3];
	int pos = 1;
	while (ask (six[pos + 0], six[pos + 1], six[pos + 2]) == 0)
	{
		pos += 1;
	}

	int p0 = six[pos - 1];
	int p1 = six[pos + 2];

	auto v = new bool [n + 1];
	for (int i = 0; i < n; i += 3)
	{
		auto three = [i + 1, i + 2, i + 3];
		if (q[i / 3] == 0)
		{
			int x = ask (three[0], three[1], p1);
			int y = ask (three[0], three[2], p1);
			int z = (x == 0 && y == 0) ? 0 : 1;
			v[three[0]] = 0;
			v[three[1]] = 0;
			v[three[2]] = 0;
			if (x == 1 && y == 1) v[three[0]] = 1;
			if (x == 1 && z == 1) v[three[1]] = 1;
			if (y == 1 && z == 1) v[three[2]] = 1;
		}
		else
		{
			int x = ask (three[0], three[1], p0);
			int y = ask (three[0], three[2], p0);
			int z = (x == 1 && y == 1) ? 1 : 0;
			v[three[0]] = 1;
			v[three[1]] = 1;
			v[three[2]] = 1;
			if (x == 0 && y == 0) v[three[0]] = 0;
			if (x == 0 && z == 0) v[three[1]] = 0;
			if (y == 0 && z == 0) v[three[2]] = 0;
		}
	}

	auto answer = iota (1, n + 1).filter !(i => v[i] == 0).array;
	writefln ("! %s %(%s %)", answer.length, answer);
	stdout.flush ();
}

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

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

void solve (int n)
{
	assert (n % 3 == 0);

	int askImpl () (int a, int b, int c)
	{
		writeln ("? ", a, " ", b, " ", c);
		stdout.flush ();
		return readln.strip.to !(int);
	}

	alias ask = memoize !(askImpl !());

	int [] q;
	for (int i = 0; i < n; i += 3)
	{
		q ~= ask (i + 1, i + 2, i + 3);
	}

	auto t0 = q.countUntil (0) * 3;
	auto t1 = q.countUntil (1) * 3;
	auto six = [t0 + 1, t0 + 2, t0 + 3, t1 + 1, t1 + 2, t1 + 3];
	int pos = 1;
	while (ask (six[pos + 0], six[pos + 1], six[pos + 2]) == 0)
	{
		pos += 1;
	}

	int p0 = six[pos - 1];
	int p1 = six[pos + 2];

	auto v = new bool [n + 1];
	for (int i = 0; i < n; i += 3)
	{
		auto three = [i + 1, i + 2, i + 3];
		if (q[i / 3] == 0)
		{
			int x = ask (three[0], three[1], p1);
			int y = ask (three[0], three[2], p1);
			int z = (x == 0 && y == 0) ? 0 : 1;
			v[three[0]] = 0;
			v[three[1]] = 0;
			v[three[2]] = 0;
			if (x == 1 && y == 1) v[three[0]] = 1;
			if (x == 1 && z == 1) v[three[1]] = 1;
			if (y == 1 && z == 1) v[three[2]] = 1;
		}
		else
		{
			int x = ask (three[0], three[1], p0);
			int y = ask (three[0], three[2], p0);
			int z = (x == 1 && y == 1) ? 1 : 0;
			v[three[0]] = 1;
			v[three[1]] = 1;
			v[three[2]] = 1;
			if (x == 0 && y == 0) v[three[0]] = 0;
			if (x == 0 && z == 0) v[three[1]] = 0;
			if (y == 0 && z == 0) v[three[2]] = 0;
		}
	}

	writefln ("! %(%s %)", iota (1, n + 1).filter !(i => v[i] == 0));
	stdout.flush ();
}

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

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

void solve (int n)
{
	assert (n % 3 == 0);

	int askImpl () (int a, int b, int c)
	{
		writeln ("? ", a, " ", b, " ", c);
		stdout.flush ();
		return readln.strip.to !(int);
	}

	alias ask = memoize !(askImpl !());

	int [] q;
	for (int i = 0; i < n; i += 3)
	{
		q ~= ask (i + 1, i + 2, i + 3);
	}

	auto t0 = q.countUntil (0) * 3;
	auto t1 = q.countUntil (1) * 3;
	auto six = [t0 + 1, t0 + 2, t0 + 3, t1 + 1, t1 + 2, t1 + 3];
	int pos = 1;
	while (ask (six[pos + 0], six[pos + 1], six[pos + 2]) == 0)
	{
		pos += 1;
	}

	int p0 = six[pos - 1];
	int p1 = six[pos + 2];

	auto v = new bool [n + 1];
	for (int i = 0; i < n; i += 3)
	{
		auto three = [i + 1, i + 2, i + 3];
		if (q[i / 3] == 0)
		{
			int x = ask (three[0], three[1], p1);
			int y = ask (three[0], three[2], p1);
			int z = (x == 0 && y == 0) ? 0 : 1;
			v[three[0]] = 0;
			v[three[1]] = 0;
			v[three[2]] = 0;
			if (x == 1 && y == 1) v[three[0]] = 1;
			if (x == 1 && z == 1) v[three[1]] = 1;
			if (y == 1 && z == 1) v[three[2]] = 1;
		}
		else
		{
			int x = ask (three[0], three[1], p0);
			int y = ask (three[0], three[2], p0);
			int z = (x == 1 && y == 1) ? 1 : 0;
			v[three[0]] = 1;
			v[three[1]] = 1;
			v[three[2]] = 1;
			if (x == 0 && y == 0) v[three[0]] = 0;
			if (x == 0 && z == 0) v[three[1]] = 0;
			if (y == 0 && z == 0) v[three[2]] = 0;
		}
	}

	writefln ("! %(%s %)", iota (1, n + 1).filter !(i => v[i]));
	stdout.flush ();
}

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


Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water.The only dry land there is an archipelago of n narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight line: island i has coordinates [li, ri], besides, ri < li + 1 for 1 ≤ i ≤ n - 1.To reach the goal, Andrewid needs to place a bridge between each pair of adjacent islands. A bridge of length a can be placed between the i-th and the (i + 1)-th islads, if there are such coordinates of x and y, that li ≤ x ≤ ri, li + 1 ≤ y ≤ ri + 1 and y - x = a. The detective was supplied with m bridges, each bridge can be used at most once. Help him determine whether the bridges he got are enough to connect each pair of adjacent islands.

The first line contains integers n (2 ≤ n ≤ 2·105) and m (1 ≤ m ≤ 2·105) — the number of islands and bridges. Next n lines each contain two integers li and ri (1 ≤ li ≤ ri ≤ 1018) — the coordinates of the island endpoints. The last line contains m integer numbers a1, a2, ..., am (1 ≤ ai ≤ 1018) — the lengths of the bridges that Andrewid got.

If it is impossible to place a bridge between each pair of adjacent islands in the required manner, print on a single line "No" (without the quotes), otherwise print in the first line "Yes" (without the quotes), and in the second line print n - 1 numbers b1, b2, ..., bn - 1, which mean that between islands i and i + 1 there must be used a bridge number bi.  If there are multiple correct answers, print any of them. Note that in this problem it is necessary to print "Yes" and "No" in correct case.

NoteIn the first sample test you can, for example, place the second bridge between points 3 and 8, place the third bridge between points 7 and 10 and place the first bridge between points 10 and 14.In the second sample test the first bridge is too short and the second bridge is too long, so the solution doesn't exist.

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

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		alias Island = Tuple !(long, q{lo}, long, q{hi});
		auto s = new Island [n];
		foreach (ref p; s)
		{
			readf (" %s %s", &p.lo, &p.hi);
		}

		alias Segment = Tuple !(long, q{hi}, long, q{lo}, int, q{num});
		auto t = new Segment [n - 1];
		foreach (i; 0..n - 1)
		{
			t[i] = Segment (s[i + 1].hi - s[i].lo,
			    s[i + 1].lo - s[i].hi, i);
		}
		sort (t);

		alias Bridge = Tuple !(long, q{len}, int, q{num});
		auto b = redBlackTree !(Bridge);
		foreach (j; 0..m)
		{
			long d;
			readf (" %s", &d);
			b.insert (Bridge (d, j));
		}

		auto ans = new int [n - 1];
		foreach (i; 0..n - 1)
		{
			auto temp = Bridge (t[i].lo, -1);
			auto r = b.upperBound (temp);
			if (r.empty)
			{
				ans = null;
				break;
			}
			auto cur = r.front;
			if (cur.len > t[i].hi)
			{
				ans = null;
				break;
			}
			ans[t[i].num] = cur.num + 1;
			b.removeKey (cur);
		}

		if (ans is null)
		{
			writeln ("No");
		}
		else
		{
			writeln ("Yes");
			writefln ("%(%s %)", ans);
		}
	}
}

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

void main ()
{
	int n;
	int m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		alias Island = Tuple !(long, q{lo}, long, q{hi});
		auto s = new Island [n];
		foreach (ref p; s)
		{
			readf (" %s %s", &p.lo, &p.hi);
		}

		alias Segment = Tuple !(long, q{hi}, long, q{lo}, int, q{num});
		auto t = new Segment [n - 1];
		foreach (i; 0..n - 1)
		{
			t[i] = Segment (s[i + 1].hi - s[i].lo,
			    s[i + 1].lo - s[i].hi, i);
		}
		sort !(q{a < b}, SwapStrategy.stable) (t);

		alias Bridge = Tuple !(long, q{len}, int, q{num});
		auto b = redBlackTree !(Bridge);
		foreach (j; 0..m)
		{
			long d;
			readf (" %s", &d);
			b.insert (Bridge (d, j));
		}
		debug {writeln (b[].map !(text).join (" "));}

		auto ans = new int [n - 1];
		foreach (i; 0..n - 1)
		{
			auto temp = Bridge (t[i].lo, -1);
			auto r = b.upperBound (temp);
			if (r.empty)
			{
				ans = null;
				break;
			}
			auto cur = r.front;
			if (cur.len > t[i].hi)
			{
				ans = null;
				break;
			}
			ans[t[i].num] = cur.num + 1;
			b.removeKey (cur);
		}

		if (ans is null)
		{
			writeln ("No");
		}
		else
		{
			writeln ("Yes");
			writefln ("%(%s %)", ans);
		}
	}
}