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


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.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
			ans[ti] = c2-1;
	}

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


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

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


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


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

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.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) {
  }
}

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.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

*/


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


// code by cutx64

module main;

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

void main() {

  int tests; readf!"%s\n"(tests);
  immutable(long) mod = 10 ^^ 9 + 7;
  while(tests--) {
    long n; readf!"%s\n"(n);
    long ans, p = 2;
    foreach(d; [2, 3, 2, 5, 1, 7, 2, 3, 1, 11, 1, 13, 1, 1, 2, 17, 1, 19, 1, 1, 1, 23, 1, 5, 1, 3, 1, 29, 1, 31, 2, 1, 1, 1, 1, 37, 1, 1, 1, 41, 1, 43, 1, 1, 1, 47, 1, 7, 1, 1, 1, 53, 1, 1, 1, 1, 1, 59, 1, 61, 1, 1, 2, 1, 1, 67, 1, 1, 1, 71, 1, 73, 1, 1, 1, 77, 1, 79, 1]) {
      long q = n / d;
      ans = (ans + (n - q) * p) % mod;
      n = q;
      ++p;
    }
    ans.writeln;
  }

} // main

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

string FMT_F = "%.10f";

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

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

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

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

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

		long x = 1;
		long last = n;
		foreach (i; 2..n+10)
		{
			x = lcm(x, i);
			auto tmp = last - n / x;
			tmp.modm(i);
			ans[ti].moda(tmp);
			last = n / x;
			if (last == 0) break;
		}
	}

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


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

immutable double maxR = 1_500_000.0 - 1.0;

alias Vector = Tuple !(double, q{x}, double, q{y});

bool less (int a, int b)
{
	auto at = atan2 (v[a].y, v[a].x);
	auto bt = atan2 (v[b].y, v[b].x);
	if (at != bt)
	{
		return at < bt;
	}
	return a < b;
}

__gshared Vector [] v;
__gshared Vector total;
__gshared int [] b;
__gshared RedBlackTree !(int, less) t;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		v = new Vector [n + 1];
		foreach (ref c; v[0..n])
		{
			int x, y;
			readf (" %s %s", &x, &y);
			c = Vector (x, y);
		}

		b = new int [n];
		b[] = 1;
		t = redBlackTree !(less, int) ();
		v[n] = Vector (0, 0);
		foreach (i; 0..n)
		{
			if (uniform (0, 2))
			{
				b[i] *= -1;
				v[i].x *= -1;
				v[i].y *= -1;
			}
			v[n].x += v[i].x;
			v[n].y += v[i].y;
			t.insert (i);
		}

		bool relax (int cur)
		{
			auto next = v[n];
			next.x -= 2 * v[cur].x;
			next.y -= 2 * v[cur].y;
			debug {writefln ("relax %s %.0f %.0f %.0f %.0f " ~
			    "%.10f %.10f", cur, v[cur].x, v[cur].y,
			    next.x, next.y, hypot (next.y, next.x),
			    hypot (v[n].y, v[n].x)); stdout.flush;}
			if (hypot (next.y, next.x) < hypot (v[n].y, v[n].x))
			{
				t.removeKey (cur);
				b[cur] *= -1;
				v[cur].x *= -1;
				v[cur].y *= -1;
				t.insert (cur);
				v[n] = next;
				return true;
			}
			return false;
		}

		while (hypot (v[n].y, v[n].x) > maxR)
		{
			debug {writefln ("%.0f %.0f %.10f", v[n].x, v[n].y,
			    hypot (v[n].y, v[n].x)); stdout.flush;}

			auto lo = t.lowerBound (n);
			debug {writeln (lo.empty);}
			if (relax (!lo.empty ? lo.back : t.back))
			{
				continue;
			}

			auto hi = t.upperBound (n);
			debug {writeln (hi.empty);}
			if (relax (!hi.empty ? hi.front : t.front))
			{
				continue;
			}

			auto me = t.equalRange (n);
			debug {writeln (me.empty);}
			if (relax (!me.empty ? me.front : t.back))
			{
				continue;
			}

			assert (false);
		}

		debug {writefln ("%.0f %.0f %.10f", v[n].x, v[n].y,
		    hypot (v[n].y, v[n].x)); stdout.flush;}
		writefln ("%(%s %)", b);
	}
}

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

alias Pt = Tuple!(long, "x", long, "y", int, "id");

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto V = new Pt[N];
      foreach (i; 0 .. N) {
        V[i].x = readLong();
        V[i].y = readLong();
        V[i].id = i;
      }
      V.sort!((a, b) => (a.x^^2 + a.y^^2 < b.x^^2 + b.y^^2));
      debug {
        writeln("V = ", V);
      }

      auto ans = new int[N];

      ans[V[0].id] = +1;
      long x = V[0].x, y = V[0].y;
      if (N % 2 == 0) {
        foreach (s; [+1, -1]) {
          const xx = x + s * V[1].x;
          const yy = y + s * V[1].y;
          if (xx^^2 + yy^^2 <= 2 * (V[1].x^^2 + V[1].y^^2)) {
            debug {
              writeln(s, " ", V[1]);
              writeln(xx, " ", yy);
            }
            ans[V[1].id] = s;
            x = xx;
            y = yy;
            goto found2;
          }
        }
        assert(false);
       found2:
      }
      for (int i = (N % 2 == 0) ? 2 : 1; i < N; i += 2) {
        foreach (s; [+1, -1]) foreach (t; [+1, -1]) {
          const xx = x + s * V[i].x + t * V[i + 1].x;
          const yy = y + s * V[i].y + t * V[i + 1].y;
          if (xx^^2 + yy^^2 <= 2 * (V[i + 1].x^^2 + V[i + 1].y^^2)) {
            debug {
              writeln(s, " ", V[i], ", ", t, " ", V[i + 1]);
              writeln(xx, " ", yy);
            }
            ans[V[i].id] = s;
            ans[V[i + 1].id] = t;
            x = xx;
            y = yy;
            goto found;
          }
        }
        assert(false);
       found:
      }

      foreach (i; 0 .. N) {
        if (i > 0) write(" ");
        write(ans[i]);
      }
      writeln();
    }
  } catch (EOFException e) {
  }
}


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

string FMT_F = "%.10f";

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

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

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

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

	auto b = new long[](n+m);
	foreach (i; 0..n)
	{
		b[m+i] = b[i] + a[i];
	}
	auto ans = new long[](n+1);
	foreach (i; 0..n)
	{
		ans[i+1] = ans[i] + a[i];
		ans[i+1] += b[i];
	}

	ans[1..$].map!(to!string).join(" ").writeln;
	stdout.flush();
	debug readln();
}

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

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

    long ans = 0;

    foreach (i; 0..N) {
        ans += B[i % M] + A[i];
        B[i % M] += A[i];
        write(ans, " ");
    }

    writeln;
}

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

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

void solve(){
	int n = rint, m = rint;
	long[] as = rlong(n);
	as.sort();

	long[] sums = new long[](n);
	long an;
	long[] ans;
	foreach(k; 0 .. n){
		sums[k % m] += as[k];
		an += sums[k % m];
		ans ~= an;
	}

	ans.map!(to!string).array.join(" ").writeln;
}

/*
(k - 1 => k)

some sweets move from day d to day d + 1.
they are that of ( ~= k % m)-th order.

us[k % m] = sum of ...

*/


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

void main()
{
  long t;
  readf!" %d "(t);
  foreach (casenum ; 1 .. t + 1) {
    auto s = readln.strip;
    if ((s[$ - 1] - '0') % 2 == 0)
      writeln(0);
    else if ((s[0] - '0') % 2 == 0)
      writeln(1);
    else {
      bool good = false;
      foreach (ch ; s) {
        if ((ch - '0') % 2 == 0) {
          good = true;
          break;
        }
      }
      writeln(good ? 2 : -1);
    }
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = readln.strip;
		writeln (s.back % 2 == 0 ? 0 :
		    s.front % 2 == 0 ? 1 :
		    s.canFind !(x => x % 2 == 0) ? 2 :
		    -1);
	}
}


import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
//import core.stdc.stdio;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias long lint;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
int sz(T)(T a) if(hasLength!T)
{
	return cast(int)a.length;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter
//------------------------------------------program beginning--------------------------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n;
	//writeln("hello");
loop:while(read(n))
	{
		auto a=arread!int;
		sort(a);
		int pos=sz(a)-1;
		long s=0,m=0;
		while(pos>=0 && a[pos]>0)
		{
			s+=a[pos];
			if(a[pos]&1)m=a[pos];
			pos--;
		}
		while(pos>=0 && a[pos]%2==0)pos--;
		if(pos<0)
		{
			if(s&1)writeln(s);
			else writeln(s-m);
		}
		else
		{
			if(s&1)writeln(s);
			else writeln(max(s-(m==0?int.max:m),s+a[pos]));
		}
	}
	//debug system("pause");
}

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

immutable int inf = 10^^9 + 7;
int n;
int[] a;

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

    auto dp = new int[][](n + 1, 2);
    dp[0][0] = 0, dp[0][1] = -inf;

    foreach (i ; 0 .. n) {
        if (a[i] & 1) {
            dp[i + 1][0] = max(dp[i][0], dp[i][1] + a[i]);
            dp[i + 1][1] = max(dp[i][1], dp[i][0] + a[i]);
        }
        else {
            dp[i + 1][0] = max(dp[i][0], dp[i][0] + a[i]);
            dp[i + 1][1] = max(dp[i][1], dp[i][1] + a[i]);
        }
    }

    writeln(dp[n][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.math, std.typecons, std.functional;

immutable int inf = 10^^9 + 7;
immutable int c = 10^^4;

int n;
int[] a;

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

    int e_max = 0, o_max = -inf;

    foreach (i ; 0 .. n) {
        int t_emax = e_max, t_omax = o_max;

        if ((a[i] + c) % 2 == 0) {
            e_max = max(e_max, e_max + a[i]);
            o_max = max(o_max, o_max + a[i]);
        }
        else {
            e_max = max(e_max, t_omax + a[i]);
            o_max = max(o_max, t_emax + a[i]);
        }

        debug {
            writeln("e_max:", e_max);
            writeln("o_max:", o_max);
        }
    }

    writeln(o_max);
}

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.math, std.typecons, std.functional;

immutable int inf = 10^^9 + 7;
immutable int c = 10^^4;

int n;
int[] a;

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

    int e_max = 0, o_max = -inf;

    foreach (i ; 0 .. n) {
        if ((a[i] + c) % 2 == 0) {
            e_max = max(e_max, e_max + a[i]);
            o_max = max(o_max, o_max + a[i]);
        }
        else {
            e_max = max(e_max, o_max + a[i]);
            o_max = max(o_max, e_max + a[i]);
        }

        debug {
            writeln("e_max:", e_max);
            writeln("o_max:", o_max);
        }
    }

    writeln(o_max);
}

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

struct Info {
  int sz;
  int[2][2] a;
  Info opBinary(string op)(Info f) const if (op == "*") {
    Info g;
    g.sz = sz + f.sz;
    foreach (x; 0 .. 2) foreach (y; 0 .. 2) {
      foreach (xx; 0 .. 2 - x) foreach (yy; 0 .. 2 - y) {
        g.a[x + xx][y + yy] += a[x][y] * f.a[xx][yy];
      }
    }
    return g;
  }
  Info up() const {
    Info f;
    f.sz = 1 + sz;
    f.a[0][1] += a[0][0];
    f.a[1][0] += a[0][0];
    f.a[1][1] += a[0][1];
    f.a[0][0] += a[1][0];
    f.a[0][1] += a[1][1];
    return f;
  }
}
Info identity() {
  Info f;
  f.a[0][0] = 1;
  return f;
}

int N;
int[] A, B;

int[][] G;
int[] par;
Info[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  Info f = identity;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      f = f * dp[v];
    }
  }
  dp[u] = f.up;
}

void DFS(int u, int p) {
  int[] vs;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      vs ~= v;
    }
  }
  const len = cast(int)(vs.length);
  auto ls = new Info[len + 1];
  auto rs = new Info[len + 1];
  ls[0] = rs[len] = identity;
  if (p != -1) {
    rs[len] = DP[u];
  }
  foreach (j; 0 .. len) {
    ls[j + 1] = ls[j] * dp[vs[j]];
  }
  foreach_reverse (j; 0 .. len) {
    rs[j] = dp[vs[j]] * rs[j + 1];
  }
  foreach (j; 0 .. len) {
    DP[vs[j]] = (ls[j] * rs[j + 1]).up;
  }
  foreach (j; 0 .. len) {
    DFS(vs[j], u);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }

      par = new int[N];
      dp = new Info[N];
      DP = new Info[N];
      const rt = 0;
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      long ans;
      foreach (u; 0 .. N) {
        if (u != rt) {
          if (dp[u].a[0][0] && DP[u].a[0][0]) {
            ans += 1L * dp[u].sz * DP[u].sz;
          } else {
            ans += 1L * dp[u].a[0][1] * DP[u].a[0][1];
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

struct Info {
  int sz;
  int[2][2] a;
  Info opBinary(string op)(Info f) const if (op == "*") {
    Info g;
    g.sz = sz + f.sz;
    foreach (x; 0 .. 2) foreach (y; 0 .. 2) {
      foreach (xx; 0 .. 2 - x) foreach (yy; 0 .. 2 - y) {
        g.a[x + xx][y + yy] += a[x][y] * f.a[xx][yy];
      }
    }
    return g;
  }
  Info up() const {
    Info f;
    f.sz = 1 + sz;
    f.a[0][1] += a[0][0];
    f.a[1][0] += a[0][0];
    f.a[1][1] += a[0][1];
    f.a[0][0] += a[1][0];
    f.a[0][1] += a[1][1];
    return f;
  }
}
Info identity() {
  Info f;
  f.a[0][0] = 1;
  return f;
}

int N;
int[] A, B;

int[][] G;
int[] par;
Info[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  Info f = identity;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      f = f * dp[v];
    }
  }
  dp[u] = f.up;
}

Info[] ls, rs;
void DFS(int u, int p) {
  int[] vs;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      vs ~= v;
    }
  }
  const len = cast(int)(vs.length);
  ls[0] = rs[len] = identity;
  if (p != -1) {
    rs[len] = DP[u];
  }
  foreach (j; 0 .. len) {
    ls[j + 1] = ls[j] * dp[vs[j]];
  }
  foreach_reverse (j; 0 .. len) {
    rs[j] = dp[vs[j]] * rs[j + 1];
  }
  foreach (j; 0 .. len) {
    DP[vs[j]] = (ls[j] * rs[j + 1]).up;
  }
  foreach (j; 0 .. len) {
    DFS(vs[j], u);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }

      par = new int[N];
      dp = new Info[N];
      DP = new Info[N];
      const rt = 0;
      ls = new Info[N + 1];
      rs = new Info[N + 1];
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      long ans;
      foreach (u; 0 .. N) {
        if (u != rt) {
          if (dp[u].a[0][0] && DP[u].a[0][0]) {
            ans += 1L * dp[u].sz * DP[u].sz;
          } else {
            ans += 1L * dp[u].a[0][1] * DP[u].a[0][1];
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

struct Info {
  int sz;
  // int[2][2] a;
  int a00, a01, a10, a11;
  Info opBinary(string op)(Info f) const if (op == "*") {
    Info g;
    g.sz = sz + f.sz;
    /*
    foreach (x; 0 .. 2) foreach (y; 0 .. 2) {
      foreach (xx; 0 .. 2 - x) foreach (yy; 0 .. 2 - y) {
        g.a[x + xx][y + yy] += a[x][y] * f.a[xx][yy];
      }
    }
    */
    g.a00 += a00 * f.a00;
    g.a01 += a00 * f.a01;
    g.a10 += a00 * f.a10;
    g.a11 += a00 * f.a11;
    g.a01 += a01 * f.a00;
    g.a11 += a01 * f.a10;
    g.a10 += a10 * f.a00;
    g.a11 += a10 * f.a01;
    g.a11 += a11 * f.a00;
    return g;
  }
  Info up() const {
    Info f;
    f.sz = 1 + sz;
    // f.a[0][1] += a[0][0];
    // f.a[1][0] += a[0][0];
    // f.a[1][1] += a[0][1];
    // f.a[0][0] += a[1][0];
    // f.a[0][1] += a[1][1];
    f.a01 += a00;
    f.a10 += a00;
    f.a11 += a01;
    f.a00 += a10;
    f.a01 += a11;
    return f;
  }
}
Info identity() {
  Info f;
  // f.a[0][0] = 1;
  f.a00 = 1;
  return f;
}

int N;
int[] A, B;

int[][] G;
int[] par;
Info[] dp, DP;

void dfs(int u, int p) {
  par[u] = p;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      dfs(v, u);
    }
  }
  Info f = identity;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      f = f * dp[v];
    }
  }
  dp[u] = f.up;
}

void DFS(int u, int p) {
  int[] vs;
  foreach (i; G[u]) {
    const v = A[i] ^ B[i] ^ u;
    if (v != p) {
      vs ~= v;
    }
  }
  const len = cast(int)(vs.length);
  auto ls = new Info[len + 1];
  auto rs = new Info[len + 1];
  ls[0] = rs[len] = identity;
  if (p != -1) {
    rs[len] = DP[u];
  }
  foreach (j; 0 .. len) {
    ls[j + 1] = ls[j] * dp[vs[j]];
  }
  foreach_reverse (j; 0 .. len) {
    rs[j] = dp[vs[j]] * rs[j + 1];
  }
  foreach (j; 0 .. len) {
    DP[vs[j]] = (ls[j] * rs[j + 1]).up;
  }
  foreach (j; 0 .. len) {
    DFS(vs[j], u);
  }
}

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

      G = new int[][N];
      foreach (i; 0 .. N - 1) {
        G[A[i]] ~= i;
        G[B[i]] ~= i;
      }

      par = new int[N];
      dp = new Info[N];
      DP = new Info[N];
      const rt = 0;
      dfs(rt, -1);
      DFS(rt, -1);
      debug {
        writeln("rt = ", rt);
        writeln("dp = ", dp);
        writeln("DP = ", DP);
      }
      long ans;
      foreach (u; 0 .. N) {
        if (u != rt) {
          // if (dp[u].a[0][0] && DP[u].a[0][0]) {
          if (dp[u].a00 && DP[u].a00) {
            ans += 1L * dp[u].sz * DP[u].sz;
          } else {
            ans += 1L * dp[u].a01 * DP[u].a01;
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


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

alias KV = Tuple!(int, "key", int, "val");
KV[] history;

int root(int[] uf, int u) {
  // return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
  if (uf[u] < 0) {
    return u;
  } else {
    const r = uf.root(uf[u]);
    if (uf[u] != r) {
      history ~= KV(u, uf[u]);
      uf[u] = r;
    }
    return r;
  }
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  history ~= KV(u, uf[u]);
  history ~= KV(v, uf[v]);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto U = new int[M];
      auto V = new int[M];
      auto W = new int[M];
      foreach (i; 0 .. M) {
        U[i] = readInt() - 1;
        V[i] = readInt() - 1;
        W[i] = readInt();
      }
      const Q = readInt();
      auto K = new int[Q];
      auto E = new int[][Q];
      foreach (q; 0 .. Q) {
        K[q] = readInt();
        E[q] = new int[K[q]];
        foreach (k; 0 .. K[q]) {
          E[q][k] = readInt() - 1;
        }
      }

      const limW = W.maxElement + 1;
      auto edgess = new int[][limW];
      foreach (i; 0 .. M) {
        edgess[W[i]] ~= i;
      }
      alias QK = Tuple!(int, "q", int, "k");
      auto qkss = new QK[][limW];
      foreach (q; 0 .. Q) {
        foreach (k; 0 .. K[q]) {
          qkss[W[E[q][k]]] ~= QK(q, k);
        }
      }

      auto ans = new bool[Q];
      ans[] = true;
      auto uf = new int[N];
      uf[] = -1;
      foreach (w; 0 .. limW) {
        auto qks = qkss[w];
        const len = cast(int)(qks.length);
        for (int j = 0, k; j < len; j = k) {
          for (k = j; k < len && qks[j].q == qks[k].q; ++k) {}
          history = [];
          foreach (l; j .. k) {
            const e = E[qks[l].q][qks[l].k];
            if (!uf.connect(U[e], V[e])) {
              ans[qks[l].q] = false;
            }
          }
          foreach_reverse (kv; history) {
            uf[kv.key] = kv.val;
          }
        }
        foreach (i; edgess[w]) {
          uf.connect(U[i], V[i]);
        }
      }

      foreach (q; 0 .. Q) {
        writeln(ans[q] ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable int maxW = 500_009;

alias Edge = Tuple !(int, q{u}, int, q{v}, int, q{w});
alias Record = Tuple !(int, q{v}, int, q{p}, int, q{r});

void solve (int n, int m, Edge [] e, int qn, int [] [] q)
{
	auto order = m.iota.array;
	makeIndex !((a, b) => a.w < b.w) (e, order);

	foreach (ref query; q)
	{
		schwartzSort !(a => e[a].w) (query);
	}

	auto hasW = new int [] [maxW];
	foreach (k, ref query; q)
	{
		int [] d;
		foreach (c; query)
		{
			d ~= e[c].w;
		}
//		sort (d);
		foreach (w; uniq (d))
		{
			hasW[w] ~= k;
		}
	}
	debug
	{
		foreach (w; 0..maxW)
		{
			if (!hasW[w].empty)
			{
				writeln (w, ": ", hasW[w]);
			}
		}
	}

	auto p = n.iota.array;
	auto r = new int [n];

	int root (int v)
	{
		while (v != p[v])
		{
			v = p[v];
		}
		return v;
	}

	Record [] backups;

	bool unite (int u, int v, bool doBackup)
	{
		u = root (u);
		v = root (v);
		if (u == v)
		{
			return false;
		}
		if (r[u] > r[v])
		{
			swap (u, v);
		}
		if (doBackup)
		{
			backups ~= Record (u, p[u], r[u]);
			backups ~= Record (v, p[v], r[v]);
		}
		p[u] = v;
		if (r[u] == r[v])
		{
			r[v] += 1;
		}
		return true;
	}

	void restoreBackups ()
	{
		foreach_reverse (backup; backups)
		{
			p[backup.v] = backup.p;
			r[backup.v] = backup.r;
		}
		backups.length = 0;
	}

	auto res = new bool [qn];
	res[] = true;

	int prevW = -1;
	foreach (j; 0..m)
	{
		int y = order[j];
		int curW = e[y].w;
		if (prevW != curW)
		{
			foreach (k; hasW[curW])
			{
				while (!q[k].empty && e[q[k].front].w == curW)
				{
					int x = q[k].front;
					q[k].popFront ();
					if (!unite (e[x].u, e[x].v, true))
					{
						res[k] = false;
					}
				}
				restoreBackups ();
			}
			prevW = curW;
		}
		unite (e[y].u, e[y].v, false);
	}

	foreach (c; res)
	{
		writeln (c ? "YES" : "NO");
	}
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto e = new Edge [m];
		foreach (ref edge; e)
		{
			readf (" %s %s %s", &edge.u, &edge.v, &edge.w);
			edge.u -= 1;
			edge.v -= 1;
		}

		int qn;
		readf (" %s", &qn);
		readln;
		auto q = new int [] [qn];
		foreach (ref query; q)
		{
			query = readln.splitter.drop (1)
			    .map !(to !(int)).array;
			query[] -= 1;
		}

		solve (n, m, e, qn, q);
	}
}

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

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto U = new int[M];
      auto V = new int[M];
      auto W = new int[M];
      foreach (i; 0 .. M) {
        U[i] = readInt() - 1;
        V[i] = readInt() - 1;
        W[i] = readInt();
      }
      const Q = readInt();
      auto K = new int[Q];
      auto E = new int[][Q];
      foreach (q; 0 .. Q) {
        K[q] = readInt();
        E[q] = new int[K[q]];
        foreach (k; 0 .. K[q]) {
          E[q][k] = readInt() - 1;
        }
      }

      const limW = W.maxElement + 1;
      auto edgess = new int[][limW];
      foreach (i; 0 .. M) {
        edgess[W[i]] ~= i;
      }
      alias QK = Tuple!(int, "q", int, "k");
      auto qkss = new QK[][limW];
      foreach (q; 0 .. Q) {
        foreach (k; 0 .. K[q]) {
          qkss[W[E[q][k]]] ~= QK(q, k);
        }
      }

      auto ans = new bool[Q];
      ans[] = true;
      auto uf = new int[N];
      uf[] = -1;
      auto saves = new int[N];
      foreach (w; 0 .. limW) {
        auto qks = qkss[w];
        const len = cast(int)(qks.length);
        for (int j = 0, k; j < len; j = k) {
          for (k = j; k < len && qks[j].q == qks[k].q; ++k) {}
          foreach (l; j .. k) {
            const e = E[qks[l].q][qks[l].k];
            saves[U[e]] = uf[U[e]];
            saves[V[e]] = uf[V[e]];
          }
          foreach (l; j .. k) {
            const e = E[qks[l].q][qks[l].k];
            if (!uf.connect(U[e], V[e])) {
              ans[qks[l].q] = false;
            }
          }
          foreach (l; j .. k) {
            const e = E[qks[l].q][qks[l].k];
            uf[U[e]] = saves[U[e]];
            uf[V[e]] = saves[V[e]];
          }
        }
        foreach (i; edgess[w]) {
          uf.connect(U[i], V[i]);
        }
      }

      foreach (q; 0 .. Q) {
        writeln(ans[q] ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

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

alias KV = Tuple!(int, "key", int, "val");
KV[] history;

int root(int[] uf, int u) {
  // return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
  if (uf[u] < 0) {
    return u;
  } else {
    const r = uf.root(uf[u]);
    if (uf[u] != r) {
      history ~= KV(u, r);
      uf[u] = r;
    }
    return r;
  }
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  history ~= KV(u, uf[u]);
  history ~= KV(v, uf[v]);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto U = new int[M];
      auto V = new int[M];
      auto W = new int[M];
      foreach (i; 0 .. M) {
        U[i] = readInt() - 1;
        V[i] = readInt() - 1;
        W[i] = readInt();
      }
      const Q = readInt();
      auto K = new int[Q];
      auto E = new int[][Q];
      foreach (q; 0 .. Q) {
        K[q] = readInt();
        E[q] = new int[K[q]];
        foreach (k; 0 .. K[q]) {
          E[q][k] = readInt() - 1;
        }
      }

      const limW = W.maxElement + 1;
      auto edgess = new int[][limW];
      foreach (i; 0 .. M) {
        edgess[W[i]] ~= i;
      }
      alias QK = Tuple!(int, "q", int, "k");
      auto qkss = new QK[][limW];
      foreach (q; 0 .. Q) {
        foreach (k; 0 .. K[q]) {
          qkss[W[E[q][k]]] ~= QK(q, k);
        }
      }

      auto ans = new bool[Q];
      ans[] = true;
      auto uf = new int[N];
      uf[] = -1;
      foreach (w; 0 .. limW) {
        auto qks = qkss[w];
        const len = cast(int)(qks.length);
        for (int j = 0, k; j < len; j = k) {
          for (k = j; k < len && qks[j].q == qks[k].q; ++k) {}
          history = [];
          foreach (l; j .. k) {
            const e = E[qks[l].q][qks[l].k];
            if (!uf.connect(U[e], V[e])) {
              ans[qks[l].q] = false;
            }
          }
          foreach_reverse (kv; history) {
            uf[kv.key] = kv.val;
          }
        }
        foreach (i; edgess[w]) {
          uf.connect(U[i], V[i]);
        }
      }

      foreach (q; 0 .. Q) {
        writeln(ans[q] ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}


import std.algorithm;
import std.stdio;

void main() {
    int M;
    readf(" %s", &M);
    int[] Q = new int[M];
    foreach (ref q; Q)
        readf(" %s", &q);
    int use = reduce!(min)(Q);

    int N;
    readf(" %s", &N);
    int[] A = new int[N];
    foreach (ref a; A)
        readf(" %s", &a);
    sort!("a > b")(A);

    long ans = 0;
    foreach (i, a; A) {
        auto m = i % (use + 2);
        if (m < use)
            ans += a;
    }
    writeln(ans);
}

import std.stdio : write, writeln, writefln, stdin;
import std.array;
import std.range;
import std.typecons;
import std.algorithm : max, min;

int m;
int[] q;
int n;
int[] a;

T sum(T)(T[] l ...){
	T s;
	foreach(v; l){
		s += v;
	}
	return s;
}

void main(){
	m.next;
	q = q.init;
	foreach(i; m.iota){
		q ~= next!int();
	}
	n.next;
	a = a.init;
	foreach(i; n.iota){
		a ~= next!int();
	}

	q = q.sort;
	a = a.sort.retro.array;

	int cnt;
	int sum;
	for(int i=0; i<n; ++i){
		if( cnt >= q[0] ){
			++i;
			cnt = 0;
		}else{
			sum += a[i];
			++cnt;
		}
	}

	sum.writeln;
}

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

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

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

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

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

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

	return v_;
}