Once upon a time Mike and Mike decided to come up with an outstanding problem for some stage of ROI (rare olympiad in informatics). One of them came up with a problem prototype but another stole the idea and proposed that problem for another stage of the same olympiad. Since then the first Mike has been waiting for an opportunity to propose the original idea for some other contest... Mike waited until this moment!You are given an array $$$a$$$ of $$$n$$$ integers. You are also given $$$q$$$ queries of two types:  Replace $$$i$$$-th element in the array with integer $$$x$$$.  Replace each element in the array with integer $$$x$$$. After performing each query you have to calculate the sum of all elements in the array.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \cdot 10^5$$$) — the number of elements in the array and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — elements of the array $$$a$$$. Each of the following $$$q$$$ lines contains a description of the corresponding query. Description begins with integer $$$t$$$ ($$$t \in \{1, 2\}$$$) which denotes a type of the query:   If $$$t = 1$$$, then two integers $$$i$$$ and $$$x$$$ are following ($$$1 \le i \le n$$$, $$$1 \le x \le 10^9$$$) — position of replaced element and it's new value.  If $$$t = 2$$$, then integer $$$x$$$ is following ($$$1 \le x \le 10^9$$$) — new value of each element in the array.

Print $$$q$$$ integers, each on a separate line. In the $$$i$$$-th line print the sum of all elements in the array after performing the first $$$i$$$ queries.

NoteConsider array from the example and the result of performing each query:  Initial array is $$$[1, 2, 3, 4, 5]$$$.  After performing the first query, array equals to $$$[5, 2, 3, 4, 5]$$$. The sum of all elements is $$$19$$$.  After performing the second query, array equals to $$$[10, 10, 10, 10, 10]$$$. The sum of all elements is $$$50$$$.  After performing the third query, array equals to $$$[10, 10, 10, 10, 11$$$]. The sum of all elements is $$$51$$$.  After performing the fourth query, array equals to $$$[10, 10, 10, 1, 11]$$$. The sum of all elements is $$$42$$$.  After performing the fifth query, array equals to $$$[1, 1, 1, 1, 1]$$$. The sum of all elements is $$$5$$$.

void main() { runSolver(); }

void problem() {
  auto QN = 1;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto Q = scan!int;
      auto A = scan!long(N);

      long ans = A.sum;

      alias QV = Tuple!(int, "qi", long, "v");
      auto arr = new QV[](N + 1);
      foreach(i; 0..N) arr[i].v = A[i];

      foreach(qi; 1..Q + 1) {
        auto q = scan!int;
        if (q == 1) {
          auto i = scan!int - 1;
          auto v = scan!long;
          auto currentValue = arr[i].qi >= arr[N].qi ? arr[i].v : arr[N].v;
          ans += v - currentValue;
          arr[i] = QV(qi, v);
        } else {
          auto v = scan!long;
          ans = v * N;
          arr[N] = QV(qi, v);
        }

        ans.writeln;
      }
    }

    foreach(i; 0..QN) {
      subSolve();
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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

import std;

void main()
{
    int n, q;
    scanf("%d %d", &n, &q);
    getchar();
    long[] arr = readln.split.to!(long[]);
    long sum = arr.sum;
    long[long] xch;
    long def_value;
    bool not_rewrited = true;
    outer: foreach(_; 0..q)
    {
        long[] query = readln.split.to!(long[]);
        if (query[0] == 1) {
            if (not_rewrited)
                sum += query[2] - xch.get(query[1], arr[cast(size_t)query[1] - 1]);
            else
                sum += query[2] - xch.get(query[1], def_value);
            xch[query[1]] = query[2];
        }
        else {
            not_rewrited = false;
            sum = n * query[1];
            def_value = query[1];
            xch.clear;
        }
        writeln(sum);
    }
}

void main() { runSolver(); }

void problem() {
  auto QN = 1;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto Q = scan!int;
      auto A = scan!long(N);

      long ans = A.sum;

      alias QV = Tuple!(int, "qi", long, "v");
      auto arr = new QV[](N + 1);
      foreach(i; 0..N) arr[i].v = A[i];

      foreach(qi; 0..Q) {
        auto q = scan!int;
        if (q == 1) {
          auto i = scan!int - 1;
          auto v = scan!long;
          auto currentValue = arr[i].qi >= arr[N].qi ? arr[i].v : arr[N].v;
          ans += v - currentValue;
          arr[i] = QV(qi, v);
        } else {
          auto v = scan!long;
          ans = v * N;
          arr[N] = QV(qi, v);
        }

        ans.writeln;
      }
    }

    foreach(i; 0..QN) {
      subSolve();
    }
  }

  outputForAtCoder(&solve);
}

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

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric, std.traits, std.functional, std.bigint, std.datetime.stopwatch, core.time, core.bitop;
T[][] combinations(T)(T[] s, in long m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(long n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");
Point invert(Point p) { return Point(p.y, p.x); }
long[] divisors(long n) { long[] ret; for (long i = 1; i * i <= n; i++) { if (n % i == 0) { ret ~= i; if (i * i != n) ret ~= n / i; } } return ret.sort.array; }
bool chmin(T)(ref T a, T b) { if (b < a) { a = b; return true; } else return false; }
bool chmax(T)(ref T a, T b) { if (b > a) { a = b; return true; } else return false; }
string charSort(alias S = "a < b")(string s) { return (cast(char[])((cast(byte[])s).sort!S.array)).to!string; }
ulong comb(ulong a, ulong b) { if (b == 0) {return 1;}else{return comb(a - 1, b - 1) * a / b;}}
T[] compress(T)(T[] arr, T origin = T.init) { T[T] indecies; arr.dup.sort.uniq.enumerate(origin).each!((i, t) => indecies[t] = i); return arr.map!(t => indecies[t]).array; }
string toAnswerString(R)(R r) { return r.map!"a.to!string".joiner(" ").array.to!string; }
struct ModInt(uint MD) if (MD < int.max) {ulong v;this(string v) {this(v.to!long);}this(int v) {this(long(v));}this(long v) {this.v = (v%MD+MD)%MD;}void opAssign(long t) {v = (t%MD+MD)%MD;}static auto normS(ulong x) {return (x<MD)?x:x-MD;}static auto make(ulong x) {ModInt m; m.v = x; return m;}auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}auto opBinary(string op:"*")(ModInt r) const {return make((ulong(v)*r.v%MD).to!ulong);}auto opBinary(string op:"^^", T)(T r) const {long x=v;long y=1;while(r){if(r%2==1)y=(y*x)%MD;x=x^^2%MD;r/=2;} return make(y);}auto opBinary(string op:"/")(ModInt r) const {return this*memoize!inv(r);}static ModInt inv(ModInt x) {return x^^(MD-2);}string toString() const {return v.to!string;}auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}}
alias MInt1 = ModInt!(10^^9 + 7);
alias MInt9 = ModInt!(998_244_353);
void outputForAtCoder(T)(T delegate() fn) {
  static if (is(T == float) || is(T == double) || is(T == real)) "%.16f".writefln(fn());
  else static if (is(T == void)) fn();
  else static if (is(T == string)) fn().writeln;
  else static if (isInputRange!T) {
    static if (!is(string == ElementType!T) && isInputRange!(ElementType!T)) foreach(r; fn()) r.toAnswerString.writeln;
    else foreach(r; fn()) r.writeln;
  }
  else fn().writeln;
}
void runSolver() {
  enum BORDER = "==================================";
  debug { BORDER.writeln; while(!stdin.eof) { "<<< Process time: %s >>>".writefln(benchmark!problem(1)); BORDER.writeln; } }
  else problem();
}
enum YESNO = [true: "Yes", false: "No"];

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


Recently a top secret mission to Mars has taken place. As a result, scientists managed to obtain some information about the Martian DNA. Now we know that any Martian DNA contains at most m different nucleotides, numbered from 1 to m. Special characteristics of the Martian DNA prevent some nucleotide pairs from following consecutively in this chain. For example, if the nucleotide 1 and nucleotide 2 can not follow consecutively in the Martian DNA, then the chain of nucleotides [1, 2] is not a valid chain of Martian DNA, but the chain of nucleotides [2, 1] can be a valid chain (if there is no corresponding restriction). The number of nucleotide pairs that can't follow in the DNA chain consecutively, is k. The needs of gene research required information about the quantity of correct n-long chains of the Martian DNA. Your task is to write a program that will calculate this value.

The first line contains three space-separated integers n, m, k (1 ≤ n ≤ 1015, 1 ≤ m ≤ 52, 0 ≤ k ≤ m2). Next k lines contain two characters each, without a space between them, representing a forbidden nucleotide pair. The first character represents the first nucleotide in the forbidden pair, the second character represents the second nucleotide. The nucleotides with assigned numbers from 1 to 26 are represented by English alphabet letters from "a" to "z" (1 is an "a", 2 is a "b", ..., 26 is a "z"). Nucleotides with assigned numbers from 27 to 52 are represented by English alphabet letters from "A" to "Z" (27 is an "A", 28 is a "B", ..., 52 is a "Z"). It is guaranteed that each forbidden pair occurs at most once in the input. It is guaranteed that nucleotide's numbers in all forbidden pairs cannot be more than m. Note that order is important in nucleotide pairs. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Print a single integer — the sought number modulo 1000000007 (109 + 7).

NoteIn the second test case all possible three-nucleotide DNAs are permitted. Each nucleotide can take one of three values, thus in total there are 27 distinct three nucleotide DNAs.In the third test sample we cannot make any DNA of two nucleotides — the only possible nucleotide "a" cannot occur two times consecutively.

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

const MOD = 1000000007;

int[][] modMul(in int[][] A, in int[][] B) {
    assert(A[0].length == B.length);
    auto H = A.length, W = B[0].length;
    auto ret = new int[][](H, W);
    foreach (i; 0 .. H) {
        foreach (j; 0 .. W) {
            foreach (k; 0 .. A[0].length) {
                long t = ret[i][j] + cast(long)(A[i][k]) * B[k][j];
                ret[i][j] = cast(int)(t % MOD);
            }
        }
    }
    return ret;
}

int[][] E(in int[][] M) {
    assert(M.length == M[0].length);
    auto N = M.length;
    auto ret = new int[][](N, N);
    foreach (i; 0 .. N) {
        ret[i][i] = 1;
    }
    return ret;
}

int[][] modPow(in int[][] M, long x) {
    if (x == 0) return M.E;
    if (x & 1) {
        return M.modMul(modPow(M, x - 1));
    } else {
        auto M1 = modPow(M, x / 2);
        return M1.modMul(M1);
    }
}

void main() {
    long N; int M, K;
    scanf("%lld %d %d\n", &N, &M, &K);
    auto B = new string[K];
    foreach (k; 0 .. K) {
        B[k] = readln.chomp;
    }

    auto x = new int[][](M, M);
    foreach (ref l; x) l[] = 1;
    foreach (s; B) {
        int begin = cast(int)(s[0].isLower ? s[0] - 'a' : s[0] - 'A' + 26),
            end   = cast(int)(s[1].isLower ? s[1] - 'a' : s[1] - 'A' + 26);
        x[begin][end] = 0;
    }
    auto y = x.modPow(N - 1);
    int ans = 0;
    foreach (i; 0 .. M) {
        foreach (j; 0 .. M) {
            ans = (ans + y[i][j]) % MOD;
        }
    }
    writeln(ans);
}

import std.ascii;
import std.conv;
import std.exception;
import std.stdio;

immutable int MAX_N = 52;
immutable int MOD = 1_000_000_007;

struct Z_P (T, int MOD)
{
private:
	static if (is (T == int))
	{
		alias long T2;
	}
	else static if (is (T == uint))
	{
		alias ulong T2;
	}
	else
	{
		static assert (false);
	}

	T value;

	void conv (T nvalue)
	{
		if (nvalue < 0)
		{
			value = ((nvalue % MOD) + MOD) % MOD;
		}
		else if (nvalue >= MOD)
		{
			value = nvalue % MOD;
		}
		else
		{
			value = nvalue;
		}
	}

public:
	this (T nvalue)
	{
		conv (nvalue);
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "+")
	{
		auto temp = value + other.value;
		if (temp >= MOD)
		{
			temp -= MOD;
		}
		return Z_P (temp);
	}

	ref Z_P opBinary (string op) (T other)
		if (op == "+")
	{
		auto temp = this + Z_P (other);
		this = temp;
		return this;
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "*")
	{
		auto temp = (cast (T2) value * other.value) % MOD;
		return Z_P (cast (T) temp);
	}

	ref Z_P opAssign (Z_P other)
	{
		value = other.value;
		return this;
	}

	ref Z_P opAssign (T nvalue)
	{
		conv (nvalue);
		return this;
	}

	ref opOpAssign (string op) (Z_P other)
		if (op == "+")
	{
		this = this + other;
		return this;
	}

	string toString ()
	{
		return to!string (value);
	}
}

struct Matrix (T)
{
private:
	T [] a;
	int w, h;

public:
	this (this)
	{
		a = a.dup;
	}

	this (int nw, int nh)
	{
		w = nw;
		h = nh;
		a.length = w * h;
	}

	ref Matrix opAssign (Matrix other)
	{
		debug {writeln ("!");}
		a = other.a.dup;
		w = other.w;
		h = other.h;
		return this;
	}

	ref Matrix opOpAssign (string op) (Matrix other)
		if (op == "*")
	{
		enforce (h == other.w);
		debug {writeln (this, " * ", other);}
		auto res = Matrix (w, other.h);
		foreach (i; 0..w)
		{
			foreach (j; 0..other.h)
			{
				foreach (k; 0..h)
				{
					res[i, j] += this[i, k] * other[k, j];
				}
			}
		}
		debug {writeln (this, " * ", other, " = ", res);}
		this = res;
		return this;
	}

	ref T opIndex (int p, int q)
	{
		return a[p * w + q];
	}

	string toString ()
	{
		return "[" ~ to!string (w) ~ " x " ~  to!string (h) ~
		       ": " ~ to!string (a) ~ "]";
	}

	@property Matrix unity ()
	{
		enforce (w == h);
		Matrix res;
		foreach (i; 0..w)
		{
			res[i, i] = 1;
		}
		return res;
	}
}

T pow (T, P) (T a, P b, T UNITY)
{
	auto res = UNITY;
	debug {writeln ("res = ", res);}
	auto temp = a;
	while (b > 0)
	{
		debug {writeln ("b = ", b);}
		if (b & 1)
		{
			res *= temp;
			debug {writeln ("res = ", res);}
		}
		temp *= temp;
		b >>= 1;
	}
	return res;
}

int conv (char c)
{
	if (isLower (c))
	{
		return c - 'a';
	}
	if (isUpper (c))
	{
		return c - 'A' + 26;
	}
	assert (false);
}

void main ()
{
	long n;
	int m, k;
	while (readf (" %s %s %s", &n, &m, &k) > 0)
	{
		alias Z_P !(int, MOD) EType;
		alias Matrix !(EType) MType;
		auto a = MType (m, m);
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				a[i, j] = 1;
			}
		}
		foreach (i; 0..k)
		{
			char u, v;
			readf (" %s %s", &u, &v);
			a[conv (u), conv (v)] = 0;
		}
		debug {writeln ("a: ", a);}
		auto unity = MType (m, m);
		foreach (i; 0..m)
		{
			unity[i, i] = 1;
		}
		debug {writeln ("unity: ", unity);}
		auto b = pow (a, n - 1, unity);
		EType res;
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				res += b[i, j];
			}
		}
		writeln (res);
	}
}

import std.ascii;
import std.conv;
import std.exception;
import std.stdio;

immutable int MAX_N = 52;
immutable int MOD = 1_000_000_007;

struct Z_P (T, int MOD)
{
private:
	static if (is (T == int))
	{
		alias long T2;
	}
	else static if (is (T == uint))
	{
		alias ulong T2;
	}
	else
	{
		static assert (false);
	}

	T value;

	void conv (T nvalue)
	{
		if (nvalue < 0)
		{
			value = ((nvalue % MOD) + MOD) % MOD;
		}
		else if (nvalue >= MOD)
		{
			value = nvalue % MOD;
		}
		else
		{
			value = nvalue;
		}
	}

public:
	this (T nvalue)
	{
		conv (nvalue);
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "+")
	{
		auto temp = value + other.value;
		if (temp >= MOD)
		{
			temp -= MOD;
		}
		return Z_P (temp);
	}

	ref Z_P opBinary (string op) (T other)
		if (op == "+")
	{
		auto temp = this + Z_P (other);
		this = temp;
		return this;
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "*")
	{
		auto temp = (cast (T2) value * other.value) % MOD;
		return Z_P (cast (T) temp);
	}

	ref Z_P opAssign (Z_P other)
	{
		value = other.value;
		return this;
	}

	ref Z_P opAssign (T nvalue)
	{
		conv (nvalue);
		return this;
	}

	ref opOpAssign (string op) (Z_P other)
		if (op == "+")
	{
		this = this + other;
		return this;
	}

	string toString ()
	{
		return to!string (value);
	}
}

struct Matrix (T)
{
private:
	T [] a;
	int w, h;

public:
	this (this)
	{
		a = a.dup;
	}

	this (int nw, int nh)
	{
		w = nw;
		h = nh;
		a.length = w * h;
	}

	ref Matrix opAssign (Matrix other)
	{
		debug {writeln ("!");}
		a = other.a.dup;
		w = other.w;
		h = other.h;
		return this;
	}

	ref Matrix opOpAssign (string op) (Matrix other)
		if (op == "*")
	{
		enforce (h == other.w);
		debug {writeln (this, " * ", other);}
		auto res = Matrix (w, other.h);
		foreach (i; 0..w)
		{
			foreach (j; 0..other.h)
			{
				res[i, j] = 0;
			}
			foreach (k; 0..h)
			{
				foreach (j; 0..other.h)
				{
					res[i, j] += this[i, k] * other[k, j];
				}
			}
		}
		debug {writeln (this, " * ", other, " = ", res);}
		this = res;
		return this;
	}

	ref T opIndex (int p, int q)
	{
		return a[p * w + q];
	}

	string toString ()
	{
		return "[" ~ to!string (w) ~ " x " ~  to!string (h) ~
		       ": " ~ to!string (a) ~ "]";
	}

	@property Matrix unity ()
	{
		enforce (w == h);
		Matrix res;
		foreach (i; 0..w)
		{
			res[i, i] = 1;
		}
		return res;
	}
}

T pow (T, P) (T a, P b, T UNITY)
{
	auto res = UNITY;
	debug {writeln ("res = ", res);}
	auto temp = a;
	while (b > 0)
	{
		debug {writeln ("b = ", b);}
		if (b & 1)
		{
			res *= temp;
			debug {writeln ("res = ", res);}
		}
		temp *= temp;
		b >>= 1;
	}
	return res;
}

int conv (char c)
{
	if (isLower (c))
	{
		return c - 'a';
	}
	if (isUpper (c))
	{
		return c - 'A' + 26;
	}
	assert (false);
}

void main ()
{
	long n;
	int m, k;
	while (readf (" %s %s %s", &n, &m, &k) > 0)
	{
		alias Z_P !(int, MOD) EType;
		alias Matrix !(EType) MType;
		auto a = MType (m, m);
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				a[i, j] = 1;
			}
		}
		foreach (i; 0..k)
		{
			char u, v;
			readf (" %s %s", &u, &v);
			a[conv (u), conv (v)] = 0;
		}
		debug {writeln ("a: ", a);}
		auto unity = MType (m, m);
		foreach (i; 0..m)
		{
			unity[i, i] = 1;
		}
		debug {writeln ("unity: ", unity);}
		auto b = pow (a, n - 1, unity);
		EType res;
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				res += b[i, j];
			}
		}
		writeln (res);
	}
}

import std.ascii;
import std.conv;
import std.exception;
import std.stdio;

immutable int MAX_N = 52;
immutable int MOD = 1_000_000_007;

struct Z_P (T, int MOD)
{
private:
	static if (is (T == int))
	{
		alias long T2;
	}
	else static if (is (T == uint))
	{
		alias ulong T2;
	}
	else
	{
		static assert (false);
	}

	T value;

	void conv (T nvalue)
	{
		if (nvalue < 0)
		{
			value = ((nvalue % MOD) + MOD) % MOD;
		}
		else if (nvalue >= MOD)
		{
			value = nvalue % MOD;
		}
		else
		{
			value = nvalue;
		}
	}

public:
	this (T nvalue)
	{
		conv (nvalue);
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "+")
	{
		auto temp = value + other.value;
		if (temp >= MOD)
		{
			temp -= MOD;
		}
		return Z_P (temp);
	}

	ref Z_P opBinary (string op) (T other)
		if (op == "+")
	{
		auto temp = this + Z_P (other);
		this = temp;
		return this;
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "*")
	{
		auto temp = (cast (T2) value * other.value) % MOD;
		return Z_P (cast (T) temp);
	}

	ref Z_P opAssign (Z_P other)
	{
		value = other.value;
		return this;
	}

	ref Z_P opAssign (T nvalue)
	{
		conv (nvalue);
		return this;
	}

	ref opOpAssign (string op) (Z_P other)
		if (op == "+")
	{
		this = this + other;
		return this;
	}

	string toString ()
	{
		return to!string (value);
	}
}

struct Matrix (T)
{
private:
	T [] a;
	int w, h;

public:
	this (this)
	{
		a = a.dup;
	}

	this (int nw, int nh)
	{
		w = nw;
		h = nh;
		a.length = w * h;
	}

	ref Matrix opAssign (Matrix other)
	{
		debug {writeln ("!");}
		a = other.a.dup;
		w = other.w;
		h = other.h;
		return this;
	}

	ref Matrix opOpAssign (string op) (Matrix other)
		if (op == "*")
	{
		enforce (w == other.h);
		debug {writeln (this, " * ", other);}
		auto res = Matrix (h, other.w);
		foreach (i; 0..h)
		{
			res[i][] = cast (T) 0;
			foreach (k; 0..w)
			{
//				res[i][] += other[k][] * this[i, k];
				foreach (j; 0..other.w)
				{
					res[i][j] += this[i][k] * other[k][j];
				}
			}
		}
		debug {writeln (this, " * ", other, " = ", res);}
		this = res;
		return this;
	}

	T [] opIndex (int p)
	{
		return a[p * w..p * w + w];
	}

	ref T opIndex (int p, int q)
	{
		return a[p * w + q];
	}

	string toString ()
	{
		return "[" ~ to!string (w) ~ " x " ~  to!string (h) ~
		       ": " ~ to!string (a) ~ "]";
	}

	@property Matrix unity ()
	{
		enforce (w == h);
		Matrix res;
		foreach (i; 0..w)
		{
			res[i, i] = 1;
		}
		return res;
	}
}

T pow (T, P) (T a, P b, T UNITY)
{
	auto res = UNITY;
	debug {writeln ("res = ", res);}
	auto temp = a;
	while (b > 0)
	{
		debug {writeln ("b = ", b);}
		if (b & 1)
		{
			res *= temp;
			debug {writeln ("res = ", res);}
		}
		temp *= temp;
		b >>= 1;
	}
	return res;
}

int conv (char c)
{
	if (isLower (c))
	{
		return c - 'a';
	}
	if (isUpper (c))
	{
		return c - 'A' + 26;
	}
	assert (false);
}

void main ()
{
	long n;
	int m, k;
	while (readf (" %s %s %s", &n, &m, &k) > 0)
	{
		alias Z_P !(int, MOD) EType;
		alias Matrix !(EType) MType;
		auto a = MType (m, m);
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				a[i, j] = 1;
			}
		}
		foreach (i; 0..k)
		{
			char u, v;
			readf (" %s %s", &u, &v);
			a[conv (u), conv (v)] = 0;
		}
		debug {writeln ("a: ", a);}
		auto unity = MType (m, m);
		foreach (i; 0..m)
		{
			unity[i, i] = 1;
		}
		debug {writeln ("unity: ", unity);}
		auto b = pow (a, n - 1, unity);
		EType res;
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				res += b[i, j];
			}
		}
		writeln (res);
	}
}

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

const MOD = 1000000007;

int[][] modMul(in int[][] A, in int[][] B) {
    assert(A[0].length == B.length);
    auto H = A.length, W = B[0].length;
    auto ret = new int[][](H, W);
    foreach (i; 0 .. H) {
        foreach (j; 0 .. W) {
            foreach (k; 0 .. A[0].length) {
                ret[i][j] = (ret[i][j] + A[i][k] * B[k][j]) % MOD;
            }
        }
    }
    return ret;
}

int[][] E(in int[][] M) {
    assert(M.length == M[0].length);
    auto N = M.length;
    auto ret = new int[][](N, N);
    foreach (i; 0 .. N) {
        ret[i][i] = 1;
    }
    return ret;
}

int[][] modPow(in int[][] M, long x) {
    if (x == 0) return M.E;
    if (x & 1) {
        return M.modMul(modPow(M, x - 1));
    } else {
        auto M1 = modPow(M, x / 2);
        return M1.modMul(M1);
    }
}

void main() {
    long N; int M, K;
    scanf("%lld %d %d\n", &N, &M, &K);
    auto B = new string[K];
    foreach (k; 0 .. K) {
        B[k] = readln.chomp;
    }

    auto x = new int[][](M, M);
    foreach (ref l; x) l[] = 1;
    foreach (s; B) {
        int begin = cast(int)(s[0].isLower ? s[0] - 'a' : s[0] - 'A'),
            end   = cast(int)(s[1].isLower ? s[1] - 'a' : s[1] - 'A');
        x[begin][end] = 0;
    }
    auto y = x.modPow(N - 1);
    int ans = 0;
    foreach (i; 0 .. M) {
        foreach (j; 0 .. M) {
            ans = (ans + y[i][j]) % MOD;
        }
    }
    writeln(ans);
}

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

const MOD = 1000000007;

int[][] modMul(in int[][] A, in int[][] B) {
    assert(A[0].length == B.length);
    auto H = A.length, W = B[0].length;
    auto ret = new int[][](H, W);
    foreach (i; 0 .. H) {
        foreach (j; 0 .. W) {
            foreach (k; 0 .. A[0].length) {
                long t = ret[i][j] + A[i][k] * B[k][j];
                ret[i][j] = cast(int)(t % MOD);
            }
        }
    }
    return ret;
}

int[][] E(in int[][] M) {
    assert(M.length == M[0].length);
    auto N = M.length;
    auto ret = new int[][](N, N);
    foreach (i; 0 .. N) {
        ret[i][i] = 1;
    }
    return ret;
}

int[][] modPow(in int[][] M, long x) {
    if (x == 0) return M.E;
    if (x & 1) {
        return M.modMul(modPow(M, x - 1));
    } else {
        auto M1 = modPow(M, x / 2);
        return M1.modMul(M1);
    }
}

void main() {
    long N; int M, K;
    scanf("%lld %d %d\n", &N, &M, &K);
    auto B = new string[K];
    foreach (k; 0 .. K) {
        B[k] = readln.chomp;
    }

    auto x = new int[][](M, M);
    foreach (ref l; x) l[] = 1;
    foreach (s; B) {
        int begin = cast(int)(s[0].isLower ? s[0] - 'a' : s[0] - 'A' + 26),
            end   = cast(int)(s[1].isLower ? s[1] - 'a' : s[1] - 'A' + 26);
        x[begin][end] = 0;
    }
    auto y = x.modPow(N - 1);
    int ans = 0;
    foreach (i; 0 .. M) {
        foreach (j; 0 .. M) {
            ans = (ans + y[i][j]) % MOD;
        }
    }
    writeln(ans);
}

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

const MOD = 1000000007;

int[][] modMul(in int[][] A, in int[][] B) {
    assert(A[0].length == B.length);
    auto H = A.length, W = B[0].length;
    auto ret = new int[][](H, W);
    foreach (i; 0 .. H) {
        foreach (j; 0 .. W) {
            foreach (k; 0 .. A[0].length) {
                ret[i][j] = (ret[i][j] + A[i][k] * B[k][j]) % MOD;
            }
        }
    }
    return ret;
}

int[][] E(in int[][] M) {
    assert(M.length == M[0].length);
    auto N = M.length;
    auto ret = new int[][](N, N);
    foreach (i; 0 .. N) {
        ret[i][i] = 1;
    }
    return ret;
}

int[][] modPow(in int[][] M, long x) {
    if (x == 0) return M.E;
    if (x & 1) {
        return M.modMul(modPow(M, x - 1));
    } else {
        auto M1 = modPow(M, x / 2);
        return M1.modMul(M1);
    }
}

void main() {
    long N; int M, K;
    scanf("%lld %d %d\n", &N, &M, &K);
    auto B = new string[K];
    foreach (k; 0 .. K) {
        B[k] = readln.chomp;
    }

    auto x = new int[][](M, M);
    foreach (ref l; x) l[] = 1;
    foreach (s; B) {
        int begin = cast(int)(s[0].isLower ? s[0] - 'a' : s[0] - 'A' + 26),
            end   = cast(int)(s[1].isLower ? s[1] - 'a' : s[1] - 'A' + 26);
        x[begin][end] = 0;
    }
    auto y = x.modPow(N - 1);
    int ans = 0;
    foreach (i; 0 .. M) {
        foreach (j; 0 .. M) {
            ans = (ans + y[i][j]) % MOD;
        }
    }
    writeln(ans);
}

import std.ascii;
import std.conv;
import std.exception;
import std.stdio;

immutable int MAX_N = 52;
immutable int MOD = 1_000_000_007;

struct Z_P (T, int MOD)
{
private:
	static if (is (T == int))
	{
		alias long T2;
	}
	else static if (is (T == uint))
	{
		alias ulong T2;
	}
	else
	{
		static assert (false);
	}

	T value;

	void conv (T nvalue)
	{
		if (nvalue < 0)
		{
			value = ((nvalue % MOD) + MOD) % MOD;
		}
		else if (nvalue >= MOD)
		{
			value = nvalue % MOD;
		}
		else
		{
			value = nvalue;
		}
	}

public:
	this (T nvalue)
	{
		conv (nvalue);
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "+")
	{
		auto temp = value + other.value;
		if (temp >= MOD)
		{
			temp -= MOD;
		}
		return Z_P (temp);
	}

	ref Z_P opBinary (string op) (T other)
		if (op == "+")
	{
		auto temp = this + Z_P (other);
		this = temp;
		return this;
	}

	Z_P opBinary (string op) (Z_P other)
		if (op == "*")
	{
		auto temp = (cast (T2) value * other.value) % MOD;
		return Z_P (cast (T) temp);
	}

	ref Z_P opAssign (T nvalue)
	{
		conv (nvalue);
		return this;
	}

	ref opOpAssign (string op) (Z_P other)
		if (op == "+")
	{
//		this = this + other;
		return this + other;
	}

	string toString ()
	{
		return to!string (value);
	}
}

struct Matrix (T)
{
private:
	T [] a;
	int w, h;

public:
	this (this)
	{
		a = a.dup;
	}

	this (int nw, int nh)
	{
		w = nw;
		h = nh;
		a.length = w * h;
	}

	ref Matrix opAssign (Matrix other)
	{
		debug {writeln ("!");}
		a = other.a.dup;
		w = other.w;
		h = other.h;
		return this;
	}

	ref Matrix opOpAssign (string op) (Matrix other)
		if (op == "*")
	{
		enforce (h == other.w);
		debug {writeln (this, " * ", other);}
		auto res = Matrix (w, other.h);
		foreach (i; 0..w)
		{
			foreach (j; 0..other.h)
			{
				foreach (k; 0..h)
				{
					res[i, j] += this[i, k] * other[k, j];
				}
			}
		}
		debug {writeln (this, " * ", other, " = ", res);}
		this = res;
		return this;
	}

	ref T opIndex (int p, int q)
	{
		return a[p * w + q];
	}

	string toString ()
	{
		return "[" ~ to!string (w) ~ " x " ~  to!string (h) ~
		       ": " ~ to!string (a) ~ "]";
	}

	@property Matrix unity ()
	{
		enforce (w == h);
		Matrix res;
		foreach (i; 0..w)
		{
			res[i, i] = 1;
		}
		return res;
	}
}

T pow (T, P) (T a, P b, T UNITY)
{
	auto res = UNITY;
	debug {writeln ("res = ", res);}
	auto temp = a;
	while (b > 0)
	{
		debug {writeln ("b = ", b);}
		if (b & 1)
		{
			res *= temp;
			debug {writeln ("res = ", res);}
		}
		temp *= temp;
		b >>= 1;
	}
	return res;
}

int conv (char c)
{
	if (isLower (c))
	{
		return c - 'a';
	}
	if (isUpper (c))
	{
		return c - 'A' + 26;
	}
	assert (false);
}

void main ()
{
	long n;
	int m, k;
	while (readf (" %s %s %s", &n, &m, &k) > 0)
	{
		alias Z_P !(int, MOD) EType;
		alias Matrix !(EType) MType;
		auto a = MType (m, m);
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				a[i, j] = 1;
			}
		}
		foreach (i; 0..k)
		{
			char u, v;
			readf (" %s %s", &u, &v);
			a[conv (u), conv (v)] = 0;
		}
		debug {writeln ("a: ", a);}
		auto unity = MType (m, m);
		foreach (i; 0..m)
		{
			unity[i, i] = 1;
		}
		debug {writeln ("unity: ", unity);}
		auto b = pow (a, n - 1, unity);
		EType res;
		foreach (i; 0..m)
		{
			foreach (j; 0..m)
			{
				res += b[i, j];
			}
		}
		writeln (res);
	}
}


Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles.

The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles. The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n, 1 ≤ d ≤ u ≤ n), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle.

For each query rectangle output its beauty degree on a separate line.

NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below:  There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture:

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

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

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

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

void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}

void main() {
  int[][] efs;
  foreach (e0; 0 .. 3) foreach (f0; 0 .. 3) {
    foreach (e1; 0 .. 3) foreach (f1; 0 .. 3) {
      if (e0 * 3 + f0 < e1 * 3 + f1) {
        if (min(e0, e1) <= 1 && 1 <= max(e0, e1) && min(f0, f1) <= 1 && 1 <= max(f0, f1)) {
          efs ~= [e0, f0, e1, f1];
        }
      }
    }
  }
  debug {
    writeln("|efs| = ", efs.length);
    writeln("efs = ", efs);
  }

  try {
    for (; ; ) {
      const N = readInt();
      const Q = readInt();
      auto Y = new int[N];
      foreach (x; 0 .. N) {
        Y[x] = readInt() - 1;
      }
      auto X0 = new int[Q];
      auto Y0 = new int[Q];
      auto X1 = new int[Q];
      auto Y1 = new int[Q];
      foreach (q; 0 .. Q) {
        X0[q] = readInt() - 1;
        Y0[q] = readInt() - 1;
        X1[q] = readInt();
        Y1[q] = readInt();
      }

      auto z = new long[][][](Q, 3, 3);

      auto qss = new int[][N + 1];
      foreach (q; 0 .. Q) {
        qss[X0[q]] ~= q;
        qss[X1[q]] ~= q;
      }
      auto bit = new int[N];
      foreach (x; 0 .. N + 1) {
        foreach (q; qss[x]) {
          const e = (x == X0[q]) ? 0 : 1;
          z[q][e][0] = bit.bSum(Y0[q]);
          z[q][e][1] = bit.bSum(Y1[q]);
          z[q][e][2] = x;
        }
        if (x < N) {
          bit.bAdd(Y[x], +1);
        }
      }

      foreach (q; 0 .. Q) {
        z[q][2][0] = Y0[q];
        z[q][2][1] = Y1[q];
        z[q][2][2] = N;
        foreach_reverse (e; 0 .. 3 - 1) foreach (f; 0 .. 3) {
          z[q][e + 1][f] -= z[q][e][f];
        }
        foreach (e; 0 .. 3) foreach_reverse (f; 0 .. 3 - 1) {
          z[q][e][f + 1] -= z[q][e][f];
        }
        debug {
          writeln("z[q] = ", z[q]);
        }
        long ans = z[q][1][1] * (z[q][1][1] - 1) / 2;
        foreach (ef; efs) {
          ans += z[q][ef[0]][ef[1]] * z[q][ef[2]][ef[3]];
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}


You have $$$n$$$ students under your control and you have to compose exactly two teams consisting of some subset of your students. Each student had his own skill, the $$$i$$$-th student skill is denoted by an integer $$$a_i$$$ (different students can have the same skills).So, about the teams. Firstly, these two teams should have the same size. Two more constraints:  The first team should consist of students with distinct skills (i.e. all skills in the first team are unique).  The second team should consist of students with the same skills (i.e. all skills in the second team are equal). Note that it is permissible that some student of the first team has the same skill as a student of the second team.Consider some examples (skills are given):  $$$[1, 2, 3]$$$, $$$[4, 4]$$$ is not a good pair of teams because sizes should be the same;  $$$[1, 1, 2]$$$, $$$[3, 3, 3]$$$ is not a good pair of teams because the first team should not contain students with the same skills;  $$$[1, 2, 3]$$$, $$$[3, 4, 4]$$$ is not a good pair of teams because the second team should contain students with the same skills;  $$$[1, 2, 3]$$$, $$$[3, 3, 3]$$$ is a good pair of teams;  $$$[5]$$$, $$$[6]$$$ is a good pair of teams. Your task is to find the maximum possible size $$$x$$$ for which it is possible to compose a valid pair of teams, where each team size is $$$x$$$ (skills in the first team needed to be unique, skills in the second team should be the same between them). A student cannot be part of more than one team.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of students. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th student. Different students can have the same skills. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$).

For each test case, print the answer — the maximum possible size $$$x$$$ for which it is possible to compose a valid pair of teams, where each team size is $$$x$$$.

NoteIn the first test case of the example, it is possible to construct two teams of size $$$3$$$: the first team is $$$[1, 2, 4]$$$ and the second team is $$$[4, 4, 4]$$$. Note, that there are some other ways to construct two valid teams of size $$$3$$$.

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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;
        auto ns = new int[](N);
        foreach (a; readln.split.to!(int[])) ++ns[a-1];
        int cnt, max_n;
        foreach (n; ns) if (n > 0) {
            ++cnt;
            max_n = max(max_n, n);
        }
        if (cnt < max_n) {
            writeln(cnt);
        } else if (max_n < cnt) {
            writeln(max_n);
        } else {
            writeln(cnt-1);
        }
    }
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n;
		readf !(" %s") (n);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int [int] d;
		foreach (ref c; a)
		{
			d[c] += 1;
		}
		auto p = d.byValue.array;
		sort (p);
		int one = p.back;
		int two = p.length.to !(int);
		int res = min (one, two) - (one == two);
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto cnt = new long[](n);
		foreach (i; 0..n)
		{
			++cnt[a[i]];
		}

		long c1, c2;
		foreach (i; 0..n)
		{
			if (cnt[i] > 0)
				++c1;
			c2.chmax(cnt[i]);
		}

		if (c1 > c2)
			ans[ti] = c2;
		else if (c1 == c2)
			ans[ti] = c2-1;
		else
			ans[ti] = c1;
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto cnt = new long[](n);
		foreach (i; 0..n)
		{
			++cnt[a[i]];
		}

		long c1, c2;
		foreach (i; 0..n)
		{
			if (cnt[i] > 0)
				++c1;
			c2.chmax(cnt[i]);
		}

		if (c1 > c2)
			ans[ti] = c2;
		else
			ans[ti] = c2-1;
	}

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


On the way to school, Karen became fixated on the puzzle game on her phone!  The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to gi, j.Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!

The first line of input contains two integers, n and m (1 ≤ n, m ≤ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains gi, j (0 ≤ gi, j ≤ 500).

If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done:   row x, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".  col x, (1 ≤ x ≤ m) describing a move of the form "choose the x-th column".  If there are multiple optimal solutions, output any one of them.

NoteIn the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level:  In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center.In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level:  Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.

import std.stdio, std.algorithm;

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

    int[][] a = new int[][n];

    foreach (ref aa; a)
    {
        aa = new int[m];
        foreach (ref t; aa) readf(" %d", &t);
    }

    struct Result {bool row; int num; int count;}

    Result[] r = [];

    void f(bool row)
    {
        if (row)
        {
            for (int i = 0; i < n; i++)
            {
                int mn = int.max;

                for (int j = 0; j < m; j++) mn = min(a[i][j], mn);

                for (int j = 0; j < m; j++) a[i][j] -= mn;

                r ~= Result(row, i, mn);
            }
        }
        else
        {
            for (int i = 0; i < m; i++)
            {
                int mn = int.max;

                for (int j = 0; j < n; j++) mn = min(a[j][i], mn);

                for (int j = 0; j < n; j++) a[j][i] -= mn;

                r ~= Result(row, i, mn);
            }
        }
    }

    f(n <= m);
    f(!(n <= m));

    bool happened = true;

    loop: foreach (aa; a)
    {
        foreach (t; aa)
        {
            if (t != 0)
            {
                happened = false;
                break loop;
            }
        }
    }

    if (happened)
    {
        int count = 0;

        foreach (rr; r) count += rr.count;

        writeln(count);

        foreach (rr; r)
        {
            for (int i = 0; i < rr.count; i++)
            {
                writeln(rr.row ? "row" : "col", " ", rr.num+1);
            }
        }
    }
    else
    {
        writeln(-1);
    }
}

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

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto B = H.iota.map!(_ => readln.split.map!(to!int).array).array;
    auto C = new int[][](W, H);
    foreach (j; 0..W) foreach (i; 0..H) C[j][i] = B[i][j];

    int ans1 = 0;
    string[] ans1s;
    foreach (i; 0..H) {
        int m = B[i].reduce!min;
        ans1 += m;
        foreach (j; 0..W) B[i][j] -= m;
        foreach (_; 0..m) ans1s ~= "row " ~ (i + 1).to!string;
    }
    foreach (j; 0..W) {
        int m = 1 << 29;
        foreach (i; 0..H) m = min(m, B[i][j]);
        ans1 += m;
        foreach (i; 0..H) B[i][j] -= m;
        foreach (_; 0..m) ans1s ~= "col " ~ (j + 1).to!string;
    }

    foreach (i; 0..H) foreach (j; 0..W) if (B[i][j] != 0) ans1 = 1 << 29;

    swap(H, W);
    int ans2 = 0;
    string[] ans2s;
    foreach (i; 0..H) {
        int m = C[i].reduce!min;
        ans2 += m;
        foreach (j; 0..W) C[i][j] -= m;
        foreach (_; 0..m) ans2s ~= "col " ~ (i + 1).to!string;
    }
    foreach (j; 0..W) {
        int m = 1 << 29;
        foreach (i; 0..H) m = min(m, C[i][j]);
        ans2 += m;
        foreach (i; 0..H) C[i][j] -= m;
        foreach (_; 0..m) ans2s ~= "row " ~ (j + 1).to!string;
    }
    foreach (i; 0..H) foreach (j; 0..W) if (C[i][j] != 0) ans2 = 1 << 29;

    int ans;
    string[] anss;
    if (ans1 <= ans2) ans = ans1, anss = ans1s;
    else ans = ans2, anss = ans2s;

    if (ans == 1 << 29) {
        writeln(-1);
        return;
    }

    ans.writeln;
    foreach (i; 0..ans) anss[i].writeln;
}

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

immutable int inf = 10^^9 + 7;

int n, m;
int[][] g;

void main() {
    scan(n, m);

    g = new int[][](n, m);
    iota(n).each!(i => g[i][] = readln.split.to!(int[]));

    int itrmax = g[0].reduce!min;

    int minm = inf;
    int[] rmmin = new int[](n), cmmin = new int[](m);

    foreach (k ; 0 .. itrmax + 1) {
        int move;
        int[] rm = new int[](n), cm = new int[](m);

        move = k;
        rm[0] = k;

        foreach (j ; 0 .. m) {
            move += g[0][j] - k;
            cm[j] = g[0][j] - k;
        }

        foreach (i ; 1 .. n) {
            int v = g[i][0] - cm[0];

            if (v < 0) {
                move = inf;
                break;
            }

            bool flag;

            foreach (j ; 1 .. m) {
                if (v != g[i][j] - cm[j]) {
                    move = inf;
                    flag = 1;
                    break;
                }
            }

            if (flag) break;

            move += v;
            rm[i] = v;
        }

        debug {
            writeln("k:", k);
            writeln("move:", move);
        }

        if (move < minm) {
            minm = move;
            rmmin = rm.dup;
            cmmin = cm.dup;
        }
    }

    debug {
    }

    if (minm == inf) {
        writeln(-1);
        return;
    }

    writeln(minm);

    foreach (i ; 0 .. n) {
        foreach (k ; 0 .. rmmin[i]) {
            writefln("row %s", i + 1);
        }
    }

    foreach (j ; 0 .. m) {
        foreach (k ; 0 .. cmmin[j]) {
            writefln("col %s", j + 1);
        }
    }
}

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

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

immutable int max = 2 * 10^^5 + 10;

int n, m;
int[][] g;

void main() {
    scan(n, m);

    bool swapped;

    if (n <= m) {
        g = new int[][](n, m);
        iota(n).each!(i => g[i][] = readln.split.to!(int[]));
    }
    else {
        swap(n, m);
        swapped = 1;

        g = new int[][](n, m);

        foreach (i ; 0 .. m) {
            auto line = readln.split.to!(int[]);

            foreach (j, e ; line) {
                g[j][i] = e;
            }
        }
    }

    debug {
        writeln(n, " ", m);
        stderr.writefln("%(%(%s %)\n%)", g);
    }

    string[] ans;
    string i_s, con;

    foreach (i ; 0 .. n) {
        int x = g[i].reduce!min;
        g[i][] -= x;
        i_s = (i + 1).to!string;

        if (!swapped) con = "row " ~ i_s;
        else con = "col " ~ i_s;

        ans ~= repeat(con, x).array;
    }

    debug {
        writefln("%(%(%s %)\n%)", g);
        writeln("ans", ans);
    }

    int x;

    foreach (j ; 0 .. m) {
        x = g[0][j];

        foreach (i ; 1 .. n) {
            if (x != g[i][j]) {
                writeln(-1);
                return;
            }
        }

        if (!swapped) con = "col " ~ (j + 1).to!string;
        else con = "row " ~ (j + 1).to!string;

        ans ~= repeat(con, x).array;
    }

    int k = ans.length.to!int;

    writeln(k);

    foreach (move ; ans) {
        writeln(move);
    }

}

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

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

int n, m;
int[][] g;

void main() {
    scan(n, m);

    bool swapped;

    if (n <= m) {
        g = new int[][](n, m);
        iota(n).each!(i => g[i][] = readln.split.to!(int[]));
    }
    else {
        swap(n, m);
        swapped = 1;

        int[] line;

        g = new int[][](n, m);

        foreach (j ; 0 .. m) {
            line = readln.split.to!(int[]);

            foreach (i, e; line) {
                g[i][j] = e;
            }
        }
    }

    debug {
        writefln("n:%d, m:%d", n, m);
        writefln("%(%(%s %)\n%)", g);
    }

    string[] ans;
    string rc;

    foreach (i ; 0 .. n) {
        int x = g[i].reduce!min;
        g[i][] -= x;

        if (!swapped) rc = "row ";
        else rc = "col ";

        ans ~= repeat(rc ~ (i + 1).to!string, x).array;
    }

    foreach (j ; 0 .. m) {
        int x = g[0][j];

        if (!(iota(n).map!(i => g[i][j] == x).reduce!"a && b")) {
            writeln(-1);
            return;
        }

        if (!swapped) rc = "col ";
        else rc = "row ";

        ans ~= repeat(rc ~ (j + 1).to!string, x).array;
    }

    writeln(ans.length);

    foreach (move ; ans) {
        writeln(move);
    }
}

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

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

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

    int[][] arr;
    foreach (_; 0 .. n) arr ~= readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    Tuple!(string, int)[] ans;

    void go(string descr) {
        foreach (i; 0 .. arr.length) {
            int ops = 500;
            foreach (j; 0 .. arr[i].length) {
                ops = min(ops, arr[i][j]);
            }
            foreach (_; 0 .. ops) ans ~= tuple(descr, cast(int)i+1);
            foreach (j; 0 .. arr[i].length) arr[i][j] -= ops;
        }
    }

    auto transposer = (int[][] arr) => arr.transposed().map!(t => t.array).array;

    if (n < m) {
        go("row");
        arr = transposer(arr);
        go("col");
    } else {
        arr = transposer(arr);
        go("col");
        arr = transposer(arr);
        go("row");
    }

    bool ok = arr.all!(r => r.all!(x => x == 0));
    if (!ok) {
        writeln("-1");
        return;
    }

    ans.length.writeln;
    foreach (t; ans) {
        writeln(t[0], ' ', t[1]);
    }
}

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

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

    int[][] arr;
    foreach (_; 0 .. n) arr ~= readln.chomp.split.map!(to!int).array;

    debug { arr.writeln; }

    Tuple!(string, int)[] ans;

    void go(string descr) {
        foreach (i; 0 .. arr.length) {
            int ops = arr[i].minElement;
            ans ~= tuple(descr, cast(int)i+1).repeat(ops).array;
            arr[i][] -= ops;
        }
    }

    auto transposer = (int[][] arr) => arr.transposed().map!(t => t.array).array;

    if (n < m) {
        go("row");
        arr = transposer(arr);
        go("col");
    } else {
        arr = transposer(arr);
        go("col");
        arr = transposer(arr);
        go("row");
    }

    bool ok = arr.all!(r => r.all!(x => x == 0));
    if (!ok) {
        writeln("-1");
        return;
    }

    ans.length.writeln;
    foreach (t; ans) {
        writeln(t[0], ' ', t[1]);
    }
}

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 rows, cols;
	while (readf (" %s %s", &rows, &cols) > 0)
	{
		readln;
		int [] [] a;
		foreach (row; 0..rows)
		{
			a ~= readln.splitter.map !(to !(int)).array;
		}
		int w = a.map !(line => line.reduce !(min)).reduce !(min);
		foreach (ref line; a)
		{
			line[] -= w;
		}

		int [] rAdd;
		int [] cAdd;
solution_loop:
		foreach (row; 0..rows)
		{
			foreach (col; 0..cols)
			{
				if (a[row][col] == 0)
				{
					rAdd = a.transversal (col).array;
					cAdd = a[row].dup;
					break solution_loop;
				}
			}
		}
		assert (rAdd.length == rows);
		assert (cAdd.length == cols);

		bool ok = true;
		foreach (row; 0..rows)
		{
			foreach (col; 0..cols)
			{
				ok &= (a[row][col] == rAdd[row] + cAdd[col]);
			}
		}

		if (ok)
		{
			writeln (w * min (rows, cols) + rAdd.sum + cAdd.sum);
			if (rows < cols)
			{
				rAdd[] += w;
			}
			else
			{
				cAdd[] += w;
			}
			foreach (row; 0..rows)
			{
				foreach (i; 0..rAdd[row])
				{
					writeln ("row ", row + 1);
				}
			}
			foreach (col; 0..cols)
			{
				foreach (i; 0..cAdd[col])
				{
					writeln ("col ", col + 1);
				}
			}
		}
		else
		{
			writeln (-1);
		}
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      auto G = new int[][](M, N);
      foreach (x; 0 .. M) foreach (y; 0 .. N) {
        G[x][y] = readInt();
      }

      int ans = int.max;
      int am = -1;

      foreach (a; 0 .. G[0][0] + 1) {
        auto rows = new int[M];
        auto cols = new int[N];
        rows[0] = a;
        cols[0] = G[0][0] - a;
        foreach (x; 1 .. M) {
          rows[x] = G[x][0] - cols[0];
        }
        foreach (y; 1 .. N) {
          cols[y] = G[0][y] - rows[0];
        }
        if (rows.all!"a >= 0" && cols.all!"a >= 0") {
          bool ok = true;
          foreach (x; 0 .. M) foreach (y; 0 .. N) {
            ok = ok && (G[x][y] == rows[x] + cols[y]);
          }
          if (ok) {
            const cost = rows.sum + cols.sum;
            if (chmin(ans, cost)) {
              am = a;
            }
          }
        }
      }

      if (am == -1) {
        writeln(-1);
      } else {
        auto rows = new int[M];
        auto cols = new int[N];
        rows[0] = am;
        cols[0] = G[0][0] - am;
        foreach (x; 1 .. M) {
          rows[x] = G[x][0] - cols[0];
        }
        foreach (y; 1 .. N) {
          cols[y] = G[0][y] - rows[0];
        }
        writeln(ans);
        foreach (x; 0 .. M) {
          foreach (_; 0 .. rows[x]) {
            writeln("row ", x + 1);
          }
        }
        foreach (y; 0 .. N) {
          foreach (_; 0 .. cols[y]) {
            writeln("col ", y + 1);
          }
        }
      }
    }
  } catch (EOFException e) {
  }
}


User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds.As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1.Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.

The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

NoteIn the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).   A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

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

class UnionFind {
    int[] parent;
    this(int N) {
        parent = new int[N];
        parent[] = -1;
    }
    int root(int x) {
        if (parent[x] == -1) return x;
        return parent[x] = root(parent[x]);
    }
    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        parent[y] = x;
    }
    bool query(int x, int y) {
        return root(x) == root(y);
    }
}

void main() {
    int N; scanf("%d\n", &N);
    auto P = readln.chomp.split(" ").map!(to!int).array;
    auto A = new string[N];
    foreach (i; 0 .. N) {
        A[i] = readln.chomp;
    }

    auto uf = new UnionFind(N);
    foreach (int i; 0 .. N) {
        foreach (int j; 0 .. N) {
            if (A[i][j] == '1') {
                uf.merge(i, j);
            }
        }
    }

    auto used = new bool[N];
    auto groups = new int[][N];
    auto elems = new int[][N];
    foreach (int i; 0 .. N) {
        auto p = uf.root(i);
        groups[p] ~= i;
        elems[p] ~= P[i];
    }

    foreach (ref e; elems) {
        e.sort;
    }

    auto ans = new int[N];
    foreach (int i; 0 .. N) {
        foreach (int j, id; groups[i]) {
            ans[ id ] = elems[i][j];
        }
    }
    writefln("%(%s %)", ans);
}


When he's not training for IOI, Little Alawn enjoys playing with puzzles of various types to stimulate his brain. Today, he's playing with a puzzle that consists of a $$$2 \times n$$$ grid where each row is a permutation of the numbers $$$1,2,3,\ldots,n$$$.The goal of Little Alawn's puzzle is to make sure no numbers on the same column or row are the same (we'll call this state of the puzzle as solved), and to achieve this he is able to swap the numbers in any column. However, after solving the puzzle many times, Little Alawn got bored and began wondering about the number of possible solved configurations of the puzzle he could achieve from an initial solved configuration only by swapping numbers in a column.Unfortunately, Little Alawn got stuck while trying to solve this harder problem, so he was wondering if you could help him with it. Find the answer modulo $$$10^9+7$$$.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 4 \cdot 10^5$$$). The next two lines of each test case describe the initial state of the puzzle grid. Each line will be a permutation of the numbers $$$1,2,3,\ldots,n$$$ and the numbers in each column and row will be pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4 \cdot 10^5$$$.

For each test case output a single integer, the number of possible solved configurations of the puzzle Little Alawn can achieve from an initial solved configuration only by swapping numbers in a column. As the answer can be very large, please output it modulo $$$10^9+7$$$. The answer for each test case should be on a separate line.

NoteThe two possible puzzle configurations for example $$$1$$$ are:  $$$[1,4,2,3]$$$ in the first row and $$$[3,2,1,4]$$$ in the second;  $$$[3,2,1,4]$$$ in the first row and $$$[1,4,2,3]$$$ in the second.

// code by cutx64

module main;

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

int[] inv(int[] p) {
  auto inv_p = new int[p.length];
  foreach(i; 0 .. p.length) {
    inv_p[p[i]] = i;
  }
  return inv_p;
}

void main() {

  immutable mod = 10 ^^ 9 + 7;

  int tests; readf!"%s\n"(tests);
multitest_end:
  while(tests--) {
    int n; readf!"%s\n"(n);
    int[] a = readln.splitter.map!(to!int).map!(x => x - 1).array;
    int[] b = readln.splitter.map!(to!int).map!(x => x - 1).array;
    int[] ra = inv(a), rb = inv(b);
    int[] r = new int[n];
    foreach(i; 0 .. n) {
			r[i] = ra[b[i]];
		}
		bool[] vis = new bool[n];
		int ans = 1;
		foreach(i; 0 .. n) {
			if(!vis[i]) {
				int j = i;
				do {
					vis[j] = true;
					j = r[j];
				} while (!vis[j]);
				ans = (ans << 1) % mod;
			}
		}
    ans.writeln;
  }

} // main

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

ulong p = 1000000007;

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readf!" %d "(n);
    auto a = readln.chomp.split.map!(x => x.to!int - 1).array;
    auto b = readln.chomp.split.map!(x => x.to!int - 1).array;
    auto c = zip(a, b).sort!((x, y) => x[0] < y[0]);
    bool[] flag = new bool[](c.length);
    ulong result = 0;

    foreach (i ; 0 .. c.length) {
      if (!flag[i])
        result += 1;
      size_t j = i;
      while (!flag[j]) {
        flag[j] = true;
        j = c[j][1];
      }
    }
    writeln(powmod(2UL, result, p));
  }
}

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

immutable int mod = 10 ^^ 9 + 7;

int [] inv (int [] p)
{
	auto res = new int [p.length];
	foreach (i; 0..p.length)
	{
		res[p[i]] = i;
	}
	return res;
}

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

		auto p = readln.splitter.map !(to !(int)).array;
		p[] -= 1;
		auto rp = inv (p);

		auto q = readln.splitter.map !(to !(int)).array;
		q[] -= 1;
		auto rq = inv (q);

		auto r = new int [n];
		foreach (i; 0..n)
		{
			r[i] = rp[q[i]];
		}

		auto vis = new bool [n];
		int res = 1;
		foreach (i; 0..n)
		{
			if (!vis[i])
			{
				int j = i;
				do
				{
					vis[j] = true;
					j = r[j];
				}
				while (!vis[j]);
				res = (res << 1) % mod;
			}
		}

		writeln (res);
	}
}


Ela likes Chess a lot. During breaks, she usually challenges her co-worker in DTL to some chess games. She's not an expert at classic chess, but she's very interested in Chess variants, where she has to adapt to new rules and test her tactical mindset to win the game.The problem, which involves a non-standard chess pieces type that is described below, reads: given $$$3$$$ white crickets on a $$$n \cdot n$$$ board, arranged in an "L" shape next to each other, there are no other pieces on the board. Ela wants to know with a finite number of moves, can she put any white cricket on the square on row $$$x$$$, column $$$y$$$?An "L"-shape piece arrangement can only be one of the below:    For simplicity, we describe the rules for crickets on the board where only three white crickets are. It can move horizontally, vertically, or diagonally, but only to a square in some direction that is immediately after another cricket piece (so that it must jump over it). If the square immediately behind the piece is unoccupied, the cricket will occupy the square. Otherwise (when the square is occupied by another cricket, or does not exist), the cricket isn't allowed to make such a move.See an example of valid crickets' moves on the pictures in the Note section.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows. The first line of each test case contains $$$n$$$ ($$$4 \le n \le 10^5$$$) — denotes the size of the chessboard. The second line of each test case contains 6 numbers: $$$r_1$$$, $$$c_1$$$, $$$r_2$$$, $$$c_2$$$, $$$r_3$$$, $$$c_3$$$ ($$$1 \le r_1, c_1, r_2, c_2, r_3, c_3 \le n$$$) — coordinates of the crickets. The input ensures that the three crickets are arranged in an "L" shape that the legend stated. The third line of each test case contains 2 numbers: $$$x$$$, $$$y$$$ ($$$1 \le x, y \le n$$$) — coordinates of the target square.

For each test case, print "YES" or "NO" to denotes whether Ela can put a cricket on the target square.

NoteHere's the solution for the first test case. The red square denotes where the crickets need to reach. Note that in chess horizontals are counted from bottom to top, as well as on this picture.

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

void solve() {
    int N = readln.chomp.to!int;
    auto cs = readln.chomp.split(" ").map!(to!int).array;
    int[] ys = [cs[0], cs[2], cs[4]];
    int[] xs = [cs[1], cs[3], cs[5]];

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

    int find_d(int[] ys) {
        int[int] cy;
        foreach (y; ys) {
            cy[y] = cy.get(y, 0) + 1;
        }
        foreach (k, v; cy) {
            if (v == 2) return k;
        }
        assert(false);
    }
    int y = find_d(ys);
    int x = find_d(xs);

    if ( (y == 1 || y == N) && (x == 1 || x == N) ) {
        // L-corner is at the corner of the board
        writeln(Y == y || X == x ? "YES" : "NO");
    } else {
        writeln( abs(Y - y) % 2  == 0 || abs(X - x) % 2 == 0 ? "YES" : "NO" );
    }
}

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

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

alias Coord = Tuple !(int, q{r}, int, q{c});

bool solve (int n, Coord a, Coord b, Coord c, Coord z)
{
	if (a == z || b == z || c == z)
	{
		return true;
	}
	auto d = Coord (a.r ^ b.r ^ c.r, a.c ^ b.c ^ c.c);
	if (d.r % 2 == z.r % 2 && d.c % 2 == z.c % 2)
	{
		return false;
	}
	Coord e;
	e.r = (a.r == b.r) ? a.r : (b.r == c.r) ? b.r : c.r;
	e.c = (a.c == b.c) ? a.c : (b.c == c.c) ? b.c : c.c;
	bool corner = (e.r == 1 || e.r == n) && (e.c == 1 || e.c == n);
	if (corner)
	{
		return (z.r == e.r || z.c == e.c);
	}
	return true;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n;
		Coord a, b, c, z;
		readf !(" %s") (n);
		readf !(" %s %s") (a.r, a.c);
		readf !(" %s %s") (b.r, b.c);
		readf !(" %s %s") (c.r, c.c);
		readf !(" %s %s") (z.r, z.c);
		writeln (solve (n, a, b, c, z) ? "YES" : "NO");
	}
}


Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem.Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many times as possible.He has already composed major part of the anthem and now just needs to fill in some letters. King asked you to help him with this work.The anthem is the string s of no more than 105 small Latin letters and question marks. The most glorious victory is the string t of no more than 105 small Latin letters. You should replace all the question marks with small Latin letters in such a way that the number of occurrences of string t in string s is maximal.Note that the occurrences of string t in s can overlap. Check the third example for clarification.

The first line contains string of small Latin letters and question marks s (1 ≤ |s| ≤ 105). The second line contains string of small Latin letters t (1 ≤ |t| ≤ 105). Product of lengths of strings |s|·|t| won't exceed 107.

Output the maximum number of occurrences of string t you can achieve by replacing all the question marks in string s with small Latin letters.

NoteIn the first example the resulting string s is "winlosewinwinlwinwin"In the second example the resulting string s is "glorytoreorand". The last letter of the string can be arbitrary.In the third example occurrences of string t are overlapping. String s with maximal number of occurrences of t is "abcabcab".

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

void main() {
  string s = readln.strip, t = readln.strip;
  int n = cast(int)s.length, m = cast(int)t.length, x = 0, c;
  auto pi = new int[m], dp = new int[m+1], dp1 = new int[m+1];
  foreach (i; 1..m) {
    while (x && t[x] != t[i]) x = pi[x-1];
    pi[i] = x += t[x] == t[i];
  }
  dp[] = int.min;
  dp[0] = 0;
  foreach (i; 0..n) {
    dp1[0] = dp[0];
    foreach (j; 0..m)
      dp1[j+1] = s[i] == '?' || t[j] == s[i] ? dp[j] : int.min;
    dp1[m]++;
    foreach_reverse (j; 1..m+1)
      dp1[pi[j-1]] = max(dp1[pi[j-1]], dp1[j]);
    dp[] = dp1[];
  }
  writeln(dp[0]);
}

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

void main() {
  string s = readln.strip, t = readln.strip;
  int n = cast(int)s.length, m = cast(int)t.length, x = 0, c;
  auto pi = new int[m], dp = new int[m], dp1 = new int[m];
  foreach (i; 1..m) {
    while (x && t[x] != t[i]) x = pi[x-1];
    if (t[x] == t[i]) x++;
    pi[i] = x;
  }
  dp[] = -1;
  dp[0] = 0;
  foreach (i; 0..n) {
    dp1[] = int.min;
    foreach (j; 0..m)
      foreach (ch; (s[i] == '?' ? 'a' : s[i]) .. (s[i] == '?' ? 'z' : s[i])+1) {
        x = j;
        while (x && t[x] != ch) x = pi[x-1];
        c = 0;
        if (t[x] == ch && ++x == m) {
          c = 1;
          x = pi[m-1];
        }
        dp1[x] = max(dp1[x], dp[j]+c);
      }
    dp[] = dp1[];
  }
  writeln(dp.fold!max);
}

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

void main() {
  string s = readln.strip, t = readln.strip;
  int n = cast(int)s.length, m = cast(int)t.length, x = 0, c;
  auto pi = new int[m], dp = new int[m+1], dp1 = new int[m+1];
  foreach (i; 1..m) {
    while (x && t[x] != t[i]) x = pi[x-1];
    pi[i] = x += t[x] == t[i];
  }
  dp[] = int.min;
  dp[0] = 0;
  foreach (i; 0..n) {
    dp1[] = int.min;
    foreach (j; 0..m)
      dp1[j+1] = s[i] == '?' || t[j] == s[i] ? dp[j] : int.min;
    dp1[m]++;
    foreach_reverse (j; 1..m+1)
      dp1[pi[j-1]] = max(dp1[pi[j-1]], dp1[j]);
    dp[] = dp1[];
  }
  writeln(dp[0]);
}


YouKn0wWho has an integer sequence $$$a_1, a_2, \ldots, a_n$$$. He will perform the following operation until the sequence becomes empty: select an index $$$i$$$ such that $$$1 \le i \le |a|$$$ and $$$a_i$$$ is not divisible by $$$(i + 1)$$$, and erase this element from the sequence. Here $$$|a|$$$ is the length of sequence $$$a$$$ at the moment of operation. Note that the sequence $$$a$$$ changes and the next operation is performed on this changed sequence.For example, if $$$a=[3,5,4,5]$$$, then he can select $$$i = 2$$$, because $$$a_2 = 5$$$ is not divisible by $$$i+1 = 3$$$. After this operation the sequence is $$$[3,4,5]$$$.Help YouKn0wWho determine if it is possible to erase the whole sequence using the aforementioned operation.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$)  — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 \cdot 10^5$$$.

For each test case, print "YES" (without quotes) if it is possible to erase the whole sequence using the aforementioned operation, print "NO" (without quotes) otherwise. You can print each letter in any register (upper or lower).

NoteIn the first test case, YouKn0wWho can perform the following operations (the erased elements are underlined): $$$[1, \underline{2}, 3] \rightarrow [\underline{1}, 3] \rightarrow [\underline{3}] \rightarrow [\,].$$$In the second test case, it is impossible to erase the sequence as $$$i$$$ can only be $$$1$$$, and when $$$i=1$$$, $$$a_1 = 2$$$ is divisible by $$$i + 1 = 2$$$.

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

void main() {
    int cases = to!int(strip(readln()));

    // per testcase
    for (int t = 0; t < cases; t++) {
        int n;
        readf!"%d\n"(n);

        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            if (i == n-1) readf!"%d\n"(a[i]);
                else readf!"%d "(a[i]);
        }

        if (n == 1) {
            // writeln(a);
            if (a[0] % 2 != 0) writeln("YES");
            else writeln("NO");
            continue;
        }

        int[] m = new int[n];
        for (int i = 0; i < n; i++) {
            m[i] = a[i] % (i + 2);
        }

        bool possible = true;
        while (a.length > 0) {
            bool marked = false;
            // writeln(a);
            for (int i = cast(int) (a.length-1); i >= 0; i--) {
                if (a[i] % (i + 2) != 0) {
                    a = a.remove(i);
                    marked = true;
                    break;
                }
            }
            if (!marked) {
                possible = false;
                break;
            }
        }

        if (possible) writeln("YES");
        else writeln("NO");
    }
}

/*
there exists a combination where

*/

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto a = ma(n, readInt!int);
	long m = 1;
	bool can = true;
	foreach(i, ai; a)
	{
		if (m <= 1_000_000_000L) m = m * (cast(long)i + 2) / gcd(m, cast(long)i + 2);
		can = can && (ai % m != 0);
	}
	if (can) writeln("YES"); else 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;
// }}}

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

bool solve (int n, int [] a)
{
	n = min (n, 30);
	a = a[0..n];
remove_loop:
	while (!a.empty)
	{
		foreach_reverse (i; 0..n)
		{
			if (a[i] % (i + 2) != 0)
			{
				a = a[0..i] ~ a[i + 1..$];
				n -= 1;
				continue remove_loop;
			}
		}
		return false;
	}
	return true;
}

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

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

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

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

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

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

        bool ans = true;
        foreach (i; 0 .. N) {
          bool ok;
          for (long x = 2; x <= 2 + i && x <= 30; ++x) {
            ok = ok || (A[i] % x != 0);
          }
          ans = ans && ok;
        }
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}


A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:  Employee A is the immediate manager of employee B  Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.What is the minimum number of groups that must be formed?

The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.  It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.

Print a single integer denoting the minimum number of groups that will be formed in the party.

NoteFor the first example, three groups are sufficient, for example:   Employee 1  Employees 2 and 4  Employees 3 and 5

// https://codeforces.com/problemset/problem/115/A
import std.algorithm;
import std.stdio;
import std.conv;
import std.string;

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

    int[] graph = new int[n+1];

    foreach(i; 1..n+1)
        graph[i] = readln.chomp.to!int;

    int[] depths = new int[n+1];

    foreach(i; 1..n+1) {
        int parent = graph[i];
        int depth = 1;

        while(parent != -1) {
            parent = graph[parent];
            depth += 1;
        }

        depths[i] = depth;
    }

    int maxima = -1;
    foreach(item; depths)
        maxima = max(maxima,item);
    maxima.writeln;
}

// https://codeforces.com/problemset/problem/115/A
import std.algorithm;
import std.stdio;
import std.conv;
import std.string;

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

    int[] graph = new int[n+1];

    foreach(i; 1..n+1)
        graph[i] = readln.chomp.to!int;

    int[] depths = new int[n+1];

    foreach(i; 1..n+1) {
        int parent = graph[i];
        int depth = 1;

        if(parent != -1 && depths[parent] != 0) {
            depths[i] = depths[parent]+1;
            continue;
        }

        while(parent != -1) {
            parent = graph[parent];
            depth += 1;
        }

        depths[i] = depth;
    }

    int maxima = maxElement(depths);
    maxima.writeln;
}

// https://codeforces.com/problemset/problem/115/A
import std.algorithm;
import std.stdio;
import std.conv;
import std.string;

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

    int[] graph = new int[n+1];

    foreach(i; 1..n+1)
        graph[i] = readln.chomp.to!int;

    int[] depths = new int[n+1];

    foreach(i; 1..n+1) {
        int parent = graph[i];
        int depth = 1;

        if(parent != -1 && depths[parent] != 0) {
            depths[i] = depths[parent]+1;
            continue;
        }

        while(parent != -1) {
            parent = graph[parent];
            depth += 1;
        }

        depths[i] = depth;
    }

    int maxima = -1;
    foreach(item; depths)
        maxima = max(maxima,item);
    maxima.writeln;
}

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

immutable int mod = 10^^9 + 7;

void main()
{
    auto n = readln.chomp.to!int;
    auto child = new int[][](n);
    auto roots = new int[](0);

    foreach(i ; 0 .. n){
        int pi = readln.chomp.to!int;

        if (pi == -1) roots ~= i;
        else child[pi - 1] ~= i;
    }

    int ans;

    foreach (root ; roots) {
        ans = max(ans, dfs(n, child, root));
    }

    writeln(ans + 1);
}

int dfs(int n, int[][] child, int u) {
    if (child[u].empty) {
        return 0;
    }

    int res;

    foreach(v ; child[u]) {
        res = max(res, dfs(n, child, v));
    }

    return res + 1;
}

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