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

int [] prepare (int limit)
{
	auto res = limit.iota.array;
	for (int d = 2; d * d < limit; d++)
	{
		if (res[d] == d)
		{
			for (int e = d * d; e < limit; e += d)
			{
				if (res[e] == e)
				{
					res[e] = d;
				}
			}
		}
	}
	return res;
}

int [] roots (int limit)
{
	auto p = prepare (limit);
	auto res = new int [limit];
	foreach (d; 0..limit)
	{
		int cur = d;
		res[d] = 1;
		while (cur > 1)
		{
			bool parity = 0;
			int x = p[cur];
			while (cur % x == 0)
			{
				cur /= x;
				parity ^= 1;
			}
			if (parity)
			{
				res[d] *= x;
			}
		}
	}
	return res;
}

immutable int limit = 10 ^^ 6 + 5;

void main ()
{
	auto r = roots (limit);

	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int [int] nums;
		foreach (ref c; a)
		{
			nums[r[c]] += 1;
		}
		auto answer = [0, 0, 0];
		foreach (step; 0..3)
		{
			debug {writeln (nums);}
			int [int] next;
			foreach (k, v; nums)
			{
				answer[step] = max (answer[step], v);
				int p = (v & 1) ? k : 1;
				next[p] += v;
			}
			nums = next;
		}

		auto q = readln.strip.to !(int);
		foreach (s; 0..q)
		{
			auto z = readln.strip.to !(long);
			writeln (answer[min (z, $ - 1).to !(int)]);
		}
	}
}

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

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

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

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

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

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

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

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

enum PCNT = 10^^3;

bool[PCNT+1] PS;

void prime_init()
{
    PS[] = true;
    PS[0] = false;
    PS[1] = false;
    foreach (i; 2..PCNT+1) {
        if (PS[i]) {
            auto x = i*2;
            while (x <= PCNT) {
                PS[x] = false;
                x += i;
            }
        }
    }
}

void main()
{
    prime_init();
    long[] ps;
    foreach (i; 0..PCNT + 1) if (PS[i]) ps ~= i;

    int T; get(T);
    while (T--) {
        int N; get(N);
        long[] AS; get(AS);

        foreach (ref a; AS) foreach (p; ps) while (a % p^^2 == 0) a /= p^^2;
        sort(AS);
        AS ~= -1;
        long last = -1, cnt, one_and_evens, max_r;
        foreach (i, a; AS) {
            if (a != last) {
                max_r = max(max_r, cnt);
                if (last == 1 || cnt % 2 == 0) one_and_evens += cnt;
                cnt = 1;
                last = a;
            } else {
                ++cnt;
            }
        }
        auto r0 = max_r;
        auto r1 = max(max_r, one_and_evens);

        int Q; get(Q);
        while (Q--) {
            long W; get(W);
            writeln(W == 0 ? r0 : r1);
        }
    }
}

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

int [] prepare (int limit)
{
	auto res = limit.iota.array;
	for (int d = 2; d * d < limit; d++)
	{
		if (res[d] == d)
		{
			for (int e = d * d; e < limit; e += d)
			{
				if (res[e] == e)
				{
					res[e] = d;
				}
			}
		}
	}
	return res;
}

int [] roots (int limit)
{
	auto p = prepare (limit);
	auto res = new int [limit];
	foreach (d; 0..limit)
	{
		int cur = d;
		res[d] = 1;
		while (cur > 1)
		{
			bool parity = 0;
			int x = p[cur];
			while (cur % x == 0)
			{
				cur /= x;
				parity ^= 1;
			}
			if (parity)
			{
				res[d] *= x;
			}
		}
	}
	return res;
}

immutable int limit = 10 ^^ 6;

void main ()
{
	auto r = roots (limit);

	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;

		int [int] nums;
		foreach (ref c; a)
		{
			nums[r[c]] += 1;
		}

		auto answer = [0, 0, 0];
		foreach (step; 0..3)
		{
			debug {writeln (nums);}
			int [int] next;
			foreach (k, v; nums)
			{
				answer[step] = max (answer[step], v);
				int p = (v & 1) ? k : 1;
				next[p] += v;
			}
			nums = next;
		}

		auto q = readln.strip.to !(int);
		foreach (s; 0..q)
		{
			auto z = readln.strip.to !(long);
			writeln (answer[min (z, $ - 1).to !(int)]);
		}
	}
}


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

long solve (long n)
{
	long x = cast (long) (sqrt (cast (real) (n)));
	long res = x * 3 - 2;
	res += (x * (x + 1) <= n);
	res += (x * (x + 2) <= n);
	return res;
}

long solve (long lo, long hi)
{
	return solve (hi) - solve (lo - 1);
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		long lo, hi;
		readf !(" %s %s") (lo, hi);
		writeln (solve (lo, hi));
	}
}

import std.stdio;
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() {
    long L, R; readf("%d %d\n", &L, &R);

    long sqrt_floor(long x) {
        long lb = 1, ub = 1_000_000_100;
        while (lb + 1 < ub) {
            long mid = (lb + ub) / 2;
            if (mid * mid <= x) {
                lb = mid;
            } else {
                ub = mid;
            }
        }
        return lb;
    }

    long C(long x) {
        if (x == 0L) return 0L;
        long d = sqrt_floor(x);
        long s = d * d;
        return (d - 1L) * 3L + (x - s) / d + 1L;
    }

    writeln(C(R) - C(L-1));
}

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


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

int mishkaKacKereKazandi = 0;
int chrisKacKereKazandi = 0;

void zarlariKarsilastir( int mishkaninZari, int  chrisinZari )
{
	if ( mishkaninZari > chrisinZari )
		mishkaKacKereKazandi++;
	else if ( chrisinZari > mishkaninZari )
		chrisKacKereKazandi++;
}

void main() {

    auto satirSayisi = stdin.readln.strip.to!int();

    auto zarlarDizisi = stdin
		.byLine()
		.take(satirSayisi)
		.map!(line => line
			  .split
			  .map!(a => to!int(a))
			  .array());

    zarlarDizisi.each!( a=> zarlariKarsilastir(a[0], a[1]) );

	if ( mishkaKacKereKazandi > chrisKacKereKazandi )
		writeln("Mishka");
	else if ( mishkaKacKereKazandi == chrisKacKereKazandi )
		writeln("Friendship is magic!^^");
	else
		writeln( "Chris" );
}

import std.stdio;
import core.stdc.stdio;
import std.math;

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

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int m, c;
	foreach (int i; 0..n)
	{
		int md, cd;
		scanf("%d %d", &md, &cd);
		if (md > cd)
			m++;
		else if (cd > md)
			c++;
	}
	if (m > c)
	{
		printf("Mishka");
	} else
	if (m == c)
	{
		printf("Friendship is magic!^^");
	} else
	{
		printf("Chris");
	}
	return 0;
}

import std.stdio;

int main()
{
	int a=0;
	readf(" %d", &a);
	int d=0;
	int e=0;
	for (int i=0; i<a; i++)
	{
		int b=0;
		int c=0;
		readf(" %d", &b);
		readf(" %d", &c);
		if (b>c)
		{
			d=d+1;
		}
		if (c>b)
		{
			e=e+1;
		}
	}
	if (d>e)
	{
		writeln("Mishka");
	}
	else if (e>d)
	{
		writeln("Chris");
	}
	else
	{
		writeln("Friendship is magic!^^");
	}
	return 0;
}

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

struct IO {
        string read_string() {
                while (tokens.empty) {
                tokens = readln.split;
        }
        auto token = tokens.front;
                tokens.popFront;
                return token;
        }

        int read_int() {
                return read_string.to!int;
        }

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                int c_a = 0;
                int c_b = 0;
                for (int i = 0; i < n; i++) {
                        int a = cin.read_int;
                        int b = cin.read_int;
                        if (a > b) c_a++;
                        if (b > a) c_b++;
                }
                if (c_a > c_b) writeln("Mishka");
                else if (c_b > c_a) writeln("Chris");
                else writeln("Friendship is magic!^^");
        }
}

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

void main() {

	int n = readln.strip.to!int;

	int Mishka, Chris;

	int mM, mC;
	while (n--) {
		readf(" %s %s", &mM, &mC);
		if (mM > mC)
			++Mishka;
		else if (mM < mC)
			++Chris;
	}

	if (Mishka == Chris)
		writeln("Friendship is magic!^^");
	else if (Mishka > Chris)
		writeln("Mishka");
	else
		writeln("Chris");
}

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

int mishkaKacKereKazandi = 0;

void zarlariKarsilastir( int mishkaninZari, int  chrisinZari )
{
	if ( mishkaninZari > chrisinZari )
		mishkaKacKereKazandi++;
}

void main() {

    auto satirSayisi = stdin.readln.strip.to!int();

    auto zarlarDizisi = stdin
		.byLine()
		.take(satirSayisi)
		.map!(line => line
			  .split
			  .map!(a => to!int(a))
			  .array());

    zarlarDizisi.each!( a=> zarlariKarsilastir(a[0], a[1]) );

	if ( mishkaKacKereKazandi > satirSayisi/2 )
		writeln("Mishka");
	else if ( mishkaKacKereKazandi == satirSayisi/2 )
		writeln("Friendship is magic!^^");
	else
		writeln( "Chris" );
}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		auto last = a[0] % 2;
		ans[ti] = true;
		foreach (e; a[1..$])
		{
			if (e % 2 != last)
				ans[ti] = false;
		}
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	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) {
        readln;
        auto as = readln.split.to!(int[]);
        auto x = as[0]%2;
        foreach (a; as) if (x != a%2) goto ng;
        writeln("YES");
        continue;
        ng:
        writeln("NO");
    }
}


#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;

void main()
{
    for(string S; (S=readln().chomp()).length; )
    {
        auto n = S.to!int();
        auto s = new int[n];
        foreach(i;0..n)
            s[i] = readln().chomp().to!int();
        s.sort;
        int c = 0;
        int p = n/2;
        foreach(i;0..n/2)
        {
            auto v = s[i]*2;
            for( ; p<n; ++p)
            {
                if(v<=s[p])
                {
                    ++c;
                    ++p;
                    break;
                }
            }
        }
        writeln(n-c);
    }
}

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

immutable int MAX_N = 100_005;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto c = new int [MAX_N + 1];
		c[] = 0;
		foreach (i; 0..n)
		{
			int x;
			readf (" %s", &x);
			c[x]++;
		}
		int [] a;
		foreach (k, b; c)
		{
			foreach (i; 0..b)
			{
				a ~= k;
			}
		}
		enforce (a.length == n);
		debug {writeln (a);}

		bool check (int me)
		{
			foreach (i; 0..me)
			{
				debug {writeln ("compare ", i,
				       " ", n - me + i);}
				if (a[i] * 2 > a[n - me + i])
				{
					return false;
				}
			}
			return true;
		}

		int lo = 0;
		int hi = n >> 1;
		while (lo < hi)
		{
			int me = (lo + hi + 1) >> 1;
			debug {writeln (lo, ' ', hi, ' ', me);}
			if (check (me))
			{
				lo = me;
			}
			else
			{
				hi = me - 1;
			}
		}
		writeln (n - lo);
	}
}

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

immutable int MAX_N = 100_005;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto c = new int [MAX_N + 1];
		c[] = 0;
		foreach (i; 0..n)
		{
			int x;
			readf (" %s", &x);
			c[x]++;
		}
		int j = 0;
		int res = 0;
		foreach (i; 0..MAX_N)
		{
			j = max (j, i << 1);
			j = min (j, MAX_N);
			while (c[i] > 0)
			{
				while (j < MAX_N && c[j] == 0)
				{
					j++;
				}
				if (c[j] == 0)
				{
					break;
				}
				c[i]--;
				c[j]--;
				res++;
			}
			res += c[i];
		}
		writeln (res);
	}
}

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

immutable int MAX_N = 100_005;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto c = new int [MAX_N];
		c[] = 0;
		foreach (i; 0..n)
		{
			int x;
			readf (" %s", &x);
			c[x]++;
		}
		int j = MAX_N;
		int res = 0;
		foreach_reverse (i; 0..MAX_N)
		{
			j = min (j, i >> 1);
			while (c[i] > 0)
			{
				while (j > 0 && c[j] == 0)
				{
					j--;
				}
				if (c[j] == 0)
				{
					break;
				}
				c[i]--;
				c[j]--;
				res++;
			}
			res += c[i];
		}
		writeln (res);
	}
}

#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;

void main()
{
    for(string S; (S=readln().chomp()).length; )
    {
        auto n = S.to!int();
        auto s = new int[n];
        foreach(i;0..n)
            s[i] = readln().chomp().to!int();
        s.sort;
        auto u = new bool[n];
        auto c = n;
        int p = n;
        foreach_reverse(i;1..n)
        {
            auto v = s[i]/2;
            if(!u[i])
            {
                p=min(p,i-1);
                for( ; p>=0; --p)
                {
                    if(v>=s[p])
                    {
                        u[p]=true;
                        --c;
                        --p;
                        break;
                    }
                }
            }

        }
        writeln(c);
    }
}

#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;

void main()
{
    for(string S; (S=readln().chomp()).length; )
    {
        auto n = S.to!int();
        auto s = new int[n];
        foreach(i;0..n)
            s[i] = readln().chomp().to!int();
        s.sort;
        auto u = new bool[n];
        auto c = n;
        int p = n;
        foreach_reverse(i;1..n)
        {
            auto v = s[i];
            if(!u[i])
            {
                p=min(p,i-1);
                for( ; p>=0; --p)
                {
                    if(v>s[p]*2)
                    {
                        u[p]=true;
                        --c;
                        --p;
                        break;
                    }
                }
            }

        }
        writeln(c);
    }
}

#!/usr/bin/env rdmd
import std.stdio, std.string, std.conv;
import std.algorithm, std.array;

void main()
{
    for(string S; (S=readln().chomp()).length; )
    {
        auto n = S.to!int();
        auto s = new int[n];
        foreach(i;0..n)
            s[i] = readln().chomp().to!int();
        s.sort;
        auto u = new bool[n];
        auto c = n;
        int p = n-2;
        foreach_reverse(i;1..n)
        {
            auto v = s[i]/2;
            if(!u[i])
            {
                for( ; p>=0; --p)
                {
                    if(v>=s[p])
                    {
                        u[p]=true;
                        --c;
                        --p;
                        break;
                    }
                }
            }

        }
        writeln(c);
    }
}


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

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

while(t--) {
    int n = readln.chomp.to!int;

    long[] a = readln.split.map!(to!long).array;
    a.sort;
    for(int i = 0; i < n; ++i) {
        writef("%s %s ", a[i], a[i + n]);
    } "".writeln;
}
}

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

void main() {
    foreach(t; 0 .. to!int(readln.strip)) {
        int n = to!int(readln.strip);
        auto ar = readln.split.map!(to!int).array.sort;
        auto r1 = ar[0 .. n], r2 = ar[n .. $];
        foreach(i; 0 .. n) {
            write(r1[i]," ",r2[i]," ");
        }
        writeln();
    }
}

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

bool check(long[] a)
{
  for (int i = 0; i < a.length; i++) {
    int left = (cast(int)(a.length) + i - 1) % cast(int)(a.length);
    int right = (cast(int)(a.length) + i + 1) % cast(int)(a.length);
    if (a[left] + a[right] == 2 * a[i])
      return false;
  }
  return true;
}

void main()
{
  int t;
  readf!" %d "(t);

  foreach (casenum ; 0 .. t) {
    int n;
    readf!" %d "(n);
    auto a = readln.chomp.split.map!(to!long).array;
    long[] b;
    while (true) {
      b = a.randomShuffle;
      if (check(b))
        break;
    }
    writeln(b.map!text.joiner(" "));
  }
}

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

int main()
{
    int t = readInt!int;
    while (t--)
    {
        int n = readInt!int;
        auto a = new int[](2 * n);
        foreach(ref ai; a)
            ai = readInt!int;
        sort(a);
        debug writeln(a);
        auto low = a[0 .. n];
        auto high = a[n .. $];
        foreach(i; 0 .. n)
        {
            write(low[i], " ", high[i], " ");
        }
        writeln;
    }
    return 0;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
    currChar = getchar();
}
char topChar()
{
    return cast(char) currChar;
}
void popChar()
{
    currChar = getchar();
}
auto readInt(T)()
{
    T num = 0;
    int sgn = 0;
    while (topChar.isWhite) popChar;
    while (topChar == '-' || topChar == '+') { sgn ^= (topChar == '-'); popChar; }
    while (topChar.isDigit) { num *= 10; num += (topChar - '0'); popChar; }
    if (sgn) return -num; return num;
}
string readString()
{
    string res = "";
    while (topChar.isWhite) popChar;
    while (!topChar.isWhite) { res ~= topChar; popChar; }
    return res;
}

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

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

while(t--) {
    int n = readln.chomp.to!int;

    long[] a = readln.split.map!(to!long).array;
    for(int i = 0; i < n; ++i) {
        writef("%s %s ", a[i], a[i + n]);
    } "".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;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
	int t = rint;
	foreach(_; 0 .. t){
		long a = rlong, b = rlong, c = rlong;
		long ans = min((a + b + c) / 2, a + b, b + c, c + a);
		ans.writeln;
	}
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;
void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      {
	static foreach(n; 0 .. T[i].Types.length)
	  {
	    read(args[i][n]);
	  }
      }
    else
      readf!" %s "(args[i]);
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
alias w = writeln;

int main()
{
  int t;
  get(t);
  while(t--)
    {
      long[] c = new long[3];
      get(c);
      sort(c);
      long res = 0;
      long d = c[1] - c[0];
      res += d;
      c[1] -= d;
      c[2] -= d;
      auto a = c[0];
      auto b = c[2];
      d = min(a, b - a);
      a -= d;
      b -= 2 * d;
      res += 2 * d;
      res += 3 * (a / 2) + (a % 2);
      w(res);
    }
  return 0;
}

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);
	readln;
	foreach (test; 0..tests)
	{
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		writeln (min (a[0] + a[1], sum (a) / 2));
	}
}

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 t = RD!int;
	auto ans = new long[](t);
	foreach (i; 0..t)
	{
		auto r = RD;
		auto g = RD;
		auto b = RD;
		auto arr = [r, g, b];
		arr.sort();
		auto d = min(arr[2] - arr[1], arr[0]);
		ans[i] += d;
		arr[2] -= d;
		arr[0] -= d;
		ans[i] += arr[0] - arr[0] % 2;
		ans[i] += arr[1] - arr[0] / 2;
	}

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


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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),
                 tuple(int(0), int(-1)), tuple(int(0), int(+1))];
  auto n = next!int;
  auto m = next!int;
  auto cell = new string[](n);
  foreach(ref row; cell)
    row = next!string;
  auto visited = new bool[][](n, m);
  struct Op
  {
    string type;
    Tuple!(int, int) ind;
  }
  auto ops = new Op[](0);
  foreach(i; 0 .. n)
    {
      foreach(j; 0 .. m)
        {
          if (!visited[i][j] && cell[i][j] == '.')
            {
              auto order = new Tuple!(int, int)[](0);
              auto queue = DList!(Tuple!(int, int))(tuple!(int, int)(i, j));
              visited[i][j] = true;
              while(!queue.empty)
                {
                  auto f = queue.front;
                  queue.removeFront;
                  order ~= f;
                  foreach(delta; deltas)
                    {
                      auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);
                      if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')
                        {
                          queue.insertBack(nei);
                          visited[nei[0]][nei[1]] = true;
                        }
                    }
                }
              foreach(node; order)
                {
                  ops ~= Op("B", node);
                }
              foreach_reverse(node; order[1 .. $])
                {
                  ops ~= Op("D", node);
                  ops ~= Op("R", node);
                }
            }
        }
    }
  ops.length.writeln;
  foreach(op; ops)
    {
      writeln(op.type, " ", op.ind[0] + 1, " ", op.ind[1] + 1);
    }
}
// INPUT

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

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

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

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import core.memory;

void main(string[] args)
{
  GC.disable;
  inputFile = stdin;
  debug inputFile = File(args[1]);
  enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),
                 tuple(int(0), int(-1)), tuple(int(0), int(+1))];
  auto n = next!int;
  auto m = next!int;
  auto cell = new string[](n);
  foreach(ref row; cell)
    row = next!string;
  auto visited = new bool[][](n, m);
  struct Op
  {
    string type;
    Tuple!(int, int) ind;
  }
  auto ops = new Op[](0);
  foreach(i; 0 .. n)
    {
      foreach(j; 0 .. m)
        {
          if (!visited[i][j] && cell[i][j] == '.')
            {
              auto order = new Tuple!(int, int)[](0);
              auto queue = DList!(Tuple!(int, int))(tuple!(int, int)(i, j));
              visited[i][j] = true;
              while(!queue.empty)
                {
                  auto f = queue.front;
                  queue.removeFront;
                  order ~= f;
                  foreach(delta; deltas)
                    {
                      auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);
                      if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')
                        {
                          queue.insertBack(nei);
                          visited[nei[0]][nei[1]] = true;
                        }
                    }
                }
              foreach(node; order)
                {
                  ops ~= Op("B", node);
                }
              foreach_reverse(node; order[1 .. $])
                {
                  ops ~= Op("D", node);
                  ops ~= Op("R", node);
                }
            }
        }
    }
  ops.length.writeln;
  foreach(op; ops)
    {
      writeln(op.type, " ", op.ind[0] + 1, " ", op.ind[1] + 1);
    }
}
// INPUT

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

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

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

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import core.memory;

struct StaticQueue(T, size_t maxSize)
{
  T[maxSize] data;
  size_t _front = 0;
  size_t _pastBack = 0;
  size_t len = 0;
  auto length()
  {
    return len;
  }
  void clear()
  {
    _front = _pastBack;
    len = 0;
  }
  bool empty()
  {
    return _front == _pastBack;
  }
  void removeFront()
  {
    _front++;
    if (_front == maxSize)
      _front = 0;
    len--;
  }
  auto front()
  {
    return data[_front];
  }
  void insertBack(T t)
  {
    data[_pastBack] = t;
    _pastBack++;
    if (_pastBack == maxSize)
      _pastBack = 0;
    len++;
  }
}

void main(string[] args)
{
  GC.disable;
  inputFile = stdin;
  debug inputFile = File(args[1]);
  enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),
                 tuple(int(0), int(-1)), tuple(int(0), int(+1))];
  auto n = next!int;
  auto m = next!int;
  auto cell = new string[](n);
  foreach(ref row; cell)
    row = next!string;
  auto visited = new bool[][](n, m);
  struct Op
  {
    string type;
    Tuple!(int, int) ind;
  }
  auto ops = StaticQueue!(Op, 500 * 500 * 3)();
  auto queue = StaticQueue!(Tuple!(int, int), 500 * 500)();
  foreach(i; 0 .. n)
    {
      foreach(j; 0 .. m)
        {
          if (!visited[i][j] && cell[i][j] == '.')
            {
              auto order = new Tuple!(int, int)[](0);
              queue.clear;
              queue.insertBack(tuple(i, j));
              visited[i][j] = true;
              while(!queue.empty)
                {
                  auto f = queue.front;
                  queue.removeFront;
                  order ~= f;
                  foreach(delta; deltas)
                    {
                      auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);
                      if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')
                        {
                          queue.insertBack(nei);
                          visited[nei[0]][nei[1]] = true;
                        }
                    }
                }
              foreach(node; order)
                {
                  ops.insertBack(Op("B", node));
                }
              foreach_reverse(node; order[1 .. $])
                {
                  ops.insertBack(Op("D", node));
                  ops.insertBack(Op("R", node));
                }
            }
        }
    }
  ops.length.writeln;
  while(!ops.empty)
    {
      auto op = ops.front;
      ops.removeFront;
      writeln(op.type, " ", op.ind[0] + 1, " ", op.ind[1] + 1);
    }
}
// INPUT

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

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

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

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import core.memory;

struct StaticQueue(T, size_t maxSize)
{
  T[maxSize] data;
  size_t _front = 0;
  size_t _pastBack = 0;
  void clear()
  {
    _front = _pastBack;
  }
  bool empty()
  {
    return _front == _pastBack;
  }
  void removeFront()
  {
    _front++;
    if (_front == maxSize)
      _front = 0;
  }
  auto front()
  {
    return data[_front];
  }
  void insertBack(T t)
  {
    data[_pastBack] = t;
    _pastBack++;
    if (_pastBack == maxSize)
      _pastBack = 0;
  }
}

void main(string[] args)
{
  GC.disable;
  inputFile = stdin;
  debug inputFile = File(args[1]);
  enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),
                 tuple(int(0), int(-1)), tuple(int(0), int(+1))];
  auto n = next!int;
  auto m = next!int;
  auto cell = new string[](n);
  foreach(ref row; cell)
    row = next!string;
  auto visited = new bool[][](n, m);
  struct Op
  {
    string type;
    Tuple!(int, int) ind;
  }
  auto ops = new Op[](0);
  auto queue = StaticQueue!(Tuple!(int, int), 500 * 500)();
  foreach(i; 0 .. n)
    {
      foreach(j; 0 .. m)
        {
          if (!visited[i][j] && cell[i][j] == '.')
            {
              auto order = new Tuple!(int, int)[](0);
              queue.clear;
              queue.insertBack(tuple(i, j));
              visited[i][j] = true;
              while(!queue.empty)
                {
                  auto f = queue.front;
                  queue.removeFront;
                  order ~= f;
                  foreach(delta; deltas)
                    {
                      auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);
                      if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')
                        {
                          queue.insertBack(nei);
                          visited[nei[0]][nei[1]] = true;
                        }
                    }
                }
              foreach(node; order)
                {
                  ops ~= Op("B", node);
                }
              foreach_reverse(node; order[1 .. $])
                {
                  ops ~= Op("D", node);
                  ops ~= Op("R", node);
                }
            }
        }
    }
  ops.length.writeln;
  foreach(op; ops)
    {
      writeln(op.type, " ", op.ind[0] + 1, " ", op.ind[1] + 1);
    }
}
// INPUT

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

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

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

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  enum deltas = [tuple(int(-1), int(0)), tuple(int(+1), int(0)),
                 tuple(int(0), int(-1)), tuple(int(0), int(+1))];
  auto n = next!int;
  auto m = next!int;
  auto cell = new string[](n);
  foreach(ref row; cell)
    row = next!string;
  auto visited = new bool[][](n, m);
  foreach(i; 0 .. n)
    {
      foreach(j; 0 .. m)
        {
          if (!visited[i][j] && cell[i][j] == '.')
            {
              auto order = new Tuple!(int, int)[](0);
              auto queue = DList!(Tuple!(int, int))(tuple!(int, int)(i, j));
              visited[i][j] = true;
              while(!queue.empty)
                {
                  auto f = queue.front;
                  queue.removeFront;
                  order ~= f;
                  foreach(delta; deltas)
                    {
                      auto nei = tuple(f[0] + delta[0], f[1] + delta[1]);
                      if (nei[0] >= 0 && nei[0] < n && nei[1] >= 0 && nei[1] < m && !visited[nei[0]][nei[1]] && cell[nei[0]][nei[1]] == '.')
                        {
                          queue.insertBack(nei);
                          visited[nei[0]][nei[1]] = true;
                        }
                    }
                }
              foreach(node; order)
                {
                  writeln("B ", node[0] + 1, " ", node[1] + 1);
                }
              foreach_reverse(node; order[1 .. $])
                {
                  writeln("D ", node[0] + 1, " ", node[1] + 1);
                  writeln("R ", node[0] + 1, " ", node[1] + 1);
                }
            }
        }
    }
}

// INPUT

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

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

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


immutable multi = false;

struct US
{
	int[] parent;
	int[] sz;
	this(int n)
	{
		parent = new int[](n);
		foreach(i; 0 .. n) parent[i] = i;
		sz = new int[](n);
		sz[] = 1;
	}
	void unite(int u, int v)
	{
		debug writeln("uniting ", u, " ", v);
		u = rep(u);
		v = rep(v);
		if (sz[u] > sz[v]) swap(u, v);
		parent[u] = v;
		sz[v] += sz[u];
	}
	int rep(int v)
	{
		debug writeln("getting rep for ", v);
		if (parent[v] == v) return v;
		return parent[v] = rep(parent[v]);
	}
}

void solve(int tc)
{
	int n = readInt!int;
	auto u1 = US(n);
	auto u2 = US(n);
	auto m1 = readInt!int;
	auto m2 = readInt!int;
	foreach(i; 0 .. m1)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		debug writeln(u, " -> ", v);
		u1.unite(u, v);
	}
	foreach(i; 0 .. m2)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		u2.unite(u, v);
	}
	auto edges = new int[2][](0);
	foreach(i; 0 .. n)
	{
		foreach(j; 0 .. n)
		{
			if (u1.rep(i) != u1.rep(j) && u2.rep(i) != u2.rep(j))
			{
				u1.unite(i, j);
				u2.unite(i, j);
				edges ~= [i+1, j+1];
			}
		}
	}
	edges.length.writeln;
	foreach(e; edges) writeln(e[0], " ", e[1]);
}

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

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

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

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

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

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

struct UnionFind(T)
{
	void init(T n) { par = new T[](n); foreach (i; 0..n) par[i] = i; cnt = new T[](n); fill(cnt, 1); }
	T root(T i) { return par[i] == i ? i : (par[i] = root(par[i])); }
	bool same(T i, T j) { return root(i) == root(j); }
	void unite(T i, T j) { i = root(i); j = root(j); if (i == j) return; par[i] = j; cnt[j] += cnt[i]; }
	T size(T i) { return cnt[root(i)]; }
	T[] par, cnt;
}

void main()
{
	auto n = RD!int;
	auto m1 = RD!int;
	auto m2 = RD!int;

	UnionFind!int uf1, uf2;
	uf1.init(n);
	uf2.init(n);
	foreach (i; 0..m1)
	{
		auto u = RD!int-1;
		auto v = RD!int-1;
		uf1.unite(u, v);
	}
	foreach (i; 0..m2)
	{
		auto u = RD!int-1;
		auto v = RD!int-1;
		uf2.unite(u, v);
	}

	int[][] ans;
	foreach (i; 0..n)
	{
		foreach (j; i+1..n)
		{
			if (uf1.same(i, j) || uf2.same(i, j)) continue;
			uf1.unite(i, j);
			uf2.unite(i, j);
			ans ~= [i+1, j+1];
		}
	}

	writeln(ans.length);
	foreach (e; ans)
	{
		writeln(e[0], " ", e[1]);
	}
	stdout.flush;
	debug readln;
}


import std.stdio;

int main()
{
	int n(int a, int b)
	{
		if (a>=b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	int m(int a, int b, int c, int d)
	{
		return n(n(a,b),n(c,d));
	}
	int a=0;
	int b=0;
	int c=0;
	int d=0;
	readf(" %d", &a);
	readf(" %d", &b);
	readf(" %d", &c);
	readf(" %d", &d);
	int e=m(a,b,c,d);
	if (e==a)
	{
		writeln(e-b);
		writeln(e-c);
		writeln(e-d);
	}
	else if (e==b)
	{
		writeln(e-a);
		writeln(e-c);
		writeln(e-d);
	}
	else if (e==c)
	{
		writeln(e-a);
		writeln(e-b);
		writeln(e-d);
	}
	else
	{
		writeln(e-a);
		writeln(e-b);
		writeln(e-c);
	}
	return 0;
}

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

void main()
{
    int[] input = readln().split.map!"a.to!int".array;
    input = input.sort.reverse.array;
    int a = input[0] - input[1];
    int b = input[0] - input[2];
    int c = input[0] - input[3];
    writeln(a, " ", b, " ", c);
}

// Vicfred
// https://codeforces.com/problemset/problem/1154/A
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;

void main() {
    int[] numbers = readln.split.map!(to!int).array;
    numbers.sort;
    int abc = numbers[$-1];
    int x = abc-numbers[0];
    writefln("%d %d %d", x, numbers[1]-x, numbers[2]-x);
}

import std.stdio;
import std.math;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
import std.range;
import std.file;
import std.datetime;

void main() {

	int[] sayılar = stdin.readln.strip.split.map!(a=> a.to!int()).array;
	int toplam = sayılar.maxElement();
	sayılar = sayılar.remove( sayılar.countUntil(toplam) ).array;
	int bArtıC = sayılar.maxElement();
	int a = toplam - bArtıC;
	sayılar = sayılar.remove( sayılar.countUntil(bArtıC) ).array;

	int aArtıC = sayılar.maxElement();
	int c = aArtıC - a;
	int b = toplam - aArtıC;

	writeln(a, " ", b, " ", c);

	//writeln();

}

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

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

const long mod = 1_000_000_007;

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

	int[] xs = readln.chomp.split.map!(to!int).array;

	int s = -1;
	foreach(x; xs) s = max(s, x);

	int[] as;
	foreach(x; xs) if(x < s) as ~= s - x;

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

}
/*

The largest must be a + b + c.
Call it s.

Others are s - a, s - b, s - c.

*/

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 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; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

long mod = 10^^9 + 7;
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 x = new long[](4);
	foreach (i; 0..4)
	{
		x[i] = RD;
	}
	x.sort();

	writeln(x[3] - x[0], " ", x[3] - x[1], " ", x[3] -x[2]);
	stdout.flush();
	debug readln();
}


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

immutable long mod = 998_244_353;

long powMod (long a, long p)
{
	long res = 1;
	for ( ; p > 0; p >>= 1)
	{
		if (p & 1)
		{
			res = (res * 1L * a) % mod;
		}
		a = (a * 1L * a) % mod;
	}
	return res;
}

long invMod (long a)
{
	return powMod (a, mod - 2);
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(long)).array;

		long invs = 0;
		int i = 0;
		int j = n - 1;
		while (true)
		{
			while (i < n && a[i] == 0)
			{
				i += 1;
			}
			while (j >= 0 && a[j] == 1)
			{
				j -= 1;
			}
			if (i >= j)
			{
				break;
			}
			invs += 1;
			i += 1;
			j -= 1;
		}

		auto cn2 = n;
		cn2 = (cn2 * 1L * (n - 1)) % mod;
		cn2 = (cn2 * 1L * invMod (2)) % mod;

		long res = 0;
		foreach (k; 0..invs)
		{
			auto cur = k + 1;
			cur = (cur * 1L * (k + 1)) % mod;
			res = (res + cn2 * 1L * invMod (cur)) % mod;
		}
		writeln (res);
	}
}

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

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

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

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

    int nz = A.count!(a => a == 0);
    int t0 = A[0 .. nz].count!(a => a == 1);
    //writeln([nz, t0]);

    MInt p(int t) {
        long t2 = cast(long)(t) * t;
        long nC2 = cast(long)(N) * (N - 1) / 2;
        return MInt(t2) / nC2;
    }

    auto E = new MInt[t0 + 1];
    E[0] = 0;
    for (int t = 0; t < t0; t++) {
        E[t+1] = E[t] + 1 / p(t+1);
    }
    //writeln(E);
    writeln(E[t0]);
}

alias MInt = ModInt!998244353L;

struct ModInt(long mod) {
    invariant() {
        assert(0 <= val && val < mod);
    }
    long val;
    this(long v) { this.val = (mod * mod + v) % mod; }
    ModInt opBinary(string op : "+")(int y) const { return ModInt(this.val + y); }
    ModInt opBinary(string op : "-")(int y) const { return ModInt(this.val - y); }
    ModInt opBinary(string op : "*")(int y) const { return ModInt(this.val * y); }
    ModInt opBinary(string op : "/")(int y) const { return ModInt(this.val * inv(y)); }
    ModInt opBinary(string op : "+")(long y) const { return ModInt(this.val + y); }
    ModInt opBinary(string op : "-")(long y) const { return ModInt(this.val - y); }
    ModInt opBinary(string op : "*")(long y) const { return ModInt(this.val * y); }
    ModInt opBinary(string op : "/")(long y) const { return ModInt(this.val * inv(y)); }
    ModInt opBinary(string op : "+")(ModInt y) const { return ModInt(this.val + y.val); }
    ModInt opBinary(string op : "-")(ModInt y) const { return ModInt(this.val - y.val); }
    ModInt opBinary(string op : "*")(ModInt y) const { return ModInt(this.val * y.val); }
    ModInt opBinary(string op : "/")(ModInt y) const { return ModInt(this.val * inv(y.val)); }
    ModInt opBinaryRight(string op : "+")(int y) const { return ModInt(this.val + y); }
    ModInt opBinaryRight(string op : "-")(int y) const { return ModInt(y - this.val); }
    ModInt opBinaryRight(string op : "*")(int y) const { return ModInt(this.val * y); }
    ModInt opBinaryRight(string op : "/")(int y) const { return ModInt(inv(this.val) * y); }
    ModInt opBinaryRight(string op : "+")(long y) const { return ModInt(this.val + y); }
    ModInt opBinaryRight(string op : "-")(long y) const { return ModInt(y - this.val); }
    ModInt opBinaryRight(string op : "*")(long y) const { return ModInt(this.val * y); }
    ModInt opBinaryRight(string op : "/")(long y) const { return ModInt(inv(this.val) * y); }
    void opAssign(int y) { this.val = y; }
    void opAssign(long y) { this.val = y; }
    void opOpAssign(string op : "+")(int y) { this.val = (this.val + mod * mod + y) % mod; }
    void opOpAssign(string op : "-")(int y) { this.val = (this.val + mod * mod - y) % mod; }
    void opOpAssign(string op : "*")(int y) { this.val = (this.val * y) % mod; }
    void opOpAssign(string op : "/")(int y) { this.val = (this.val * inv(y)) % mod; }
    void opOpAssign(string op : "+")(long y) { this.val = (this.val + mod * mod + y) % mod; }
    void opOpAssign(string op : "-")(long y) { this.val = (this.val + mod * mod - y) % mod; }
    void opOpAssign(string op : "*")(long y) { this.val = (this.val * y) % mod; }
    void opOpAssign(string op : "/")(long y) { this.val = (this.val * inv(y)) % mod; }
    void opOpAssign(string op : "+")(ModInt y) { this.val = (this.val + mod * mod + y.val) % mod; }
    void opOpAssign(string op : "-")(ModInt y) { this.val = (this.val + mod * mod - y.val) % mod; }
    void opOpAssign(string op : "*")(ModInt y) { this.val = (this.val * y.val) % mod; }
    void opOpAssign(string op : "/")(ModInt y) { this.val = (this.val * inv(y.val)) % mod; }
    ModInt opUnary(string op : "-")() const { return ModInt(mod - this.val); }
    string toString() const { return val.to!string; }
    ModInt pow(long n) { return ModInt(pow(this.val, n, mod)); }
    ModInt pow(ModInt n) { return ModInt(pow(this.val, n.val, mod)); }
    static long pow(long x, long n, long mod) {
        if (n == 0) return 1L;
        x %= mod;
        if (n == 1) return x;
        if (n % 2 == 0) {
            long y = pow(x, n/2, mod);
            return y * y % mod;
        } else {
            return x * pow(x, n-1, mod) % mod;
        }
    }
    static long extgcd(long a, long b, ref long x, ref long y) {
        if (b == 0) {
            x = 1;
            y = 0;
            return a;
        }
        long d = extgcd(b, a % b, y, x);
        y -= a / b * x;
        return d;
    }
    static long inv(long a) {
        long x, y;
        extgcd(a, mod, x, y);
        return (x % mod + mod) % mod;
    }
}

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 ModInt(uint M_) {
  import std.conv : to;
  alias M = M_;
  uint x;
  this(ModInt a) { x = a.x; }
  this(uint x_) { x = x_ % M; }
  this(ulong x_) { x = cast(uint)(x_ % M); }
  this(int x_) { x = ((x_ %= cast(int)(M)) < 0) ? (x_ + cast(int)(M)) : x_; }
  this(long x_) { x = cast(uint)(((x_ %= cast(long)(M)) < 0) ? (x_ + cast(long)(M)) : x_); }
  ref ModInt opAssign(T)(inout(T) a) if (is(T == uint) || is(T == ulong) || is(T == int) || is(T == long)) { return this = ModInt(a); }
  ref ModInt opOpAssign(string op, T)(T a) {
    static if (is(T == ModInt)) {
      static if (op == "+") { x = ((x += a.x) >= M) ? (x - M) : x; }
      else static if (op == "-") { x = ((x -= a.x) >= M) ? (x + M) : x; }
      else static if (op == "*") { x = cast(uint)((cast(ulong)(x) * a.x) % M); }
      else static if (op == "/") { this *= a.inv(); }
      else static assert(false);
      return this;
    } else static if (op == "^^") {
      if (a < 0) return this = inv()^^(-a);
      ModInt b = this, c = 1U;
      for (long e = a; e; e >>= 1) { if (e & 1) c *= b; b *= b; }
      return this = c;
    } else {
      return mixin("this " ~ op ~ "= ModInt(a)");
    }
  }
  ModInt inv() const {
    uint a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const q = a / b; const c = a - q * b; a = b; b = c; const w = y - cast(int)(q) * z; y = z; z = w; }
    assert(a == 1); return ModInt(y);
  }
  ModInt opUnary(string op)() const {
    static if (op == "+") { return this; }
    else static if (op == "-") { ModInt a; a.x = x ? (M - x) : 0U; return a; }
    else static assert(false);
  }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op, T)(T a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  bool opCast(T: bool)() const { return (x != 0U); }
  string toString() const { return x.to!string; }
}

enum MO = 998244353;
alias Mint = ModInt!MO;

enum LIM_INV = 400_010;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM_INV];
  fac = new Mint[LIM_INV];
  invFac = new Mint[LIM_INV];
  inv[1] = 1;
  foreach (i; 2 .. LIM_INV) {
    inv[i] = -((Mint.M / i) * inv[cast(size_t)(Mint.M % i)]);
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM_INV) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (n < 0) {
    if (k >= 0) {
      return (-1)^^(k & 1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (-1)^^((n - k) & 1) * binom(-k - 1, n - k);
    } else {
      return Mint(0);
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM_INV);
      return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
    } else {
      return Mint(0);
    }
  }
}

void main() {
  prepare;

  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt;
        }

        const K = A.sum;
        const L = min(K, N - K);

        const Mint all = 1L * N * (N - 1) / 2;
        auto dp = new Mint[L + 2];
        foreach (a; 1 .. L + 1) {
          dp[a] = all * inv[a] * inv[a] + dp[a - 1];
        }

        const S = A[0 .. N - K].sum;
        writeln(dp[S]);
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable int mod = 998_244_353;

int powMod (int a, int p)
{
	int res = 1;
	for ( ; p > 0; p >>= 1)
	{
		if (p & 1)
		{
			res = (res * 1L * a) % mod;
		}
		a = (a * 1L * a) % mod;
	}
	return res;
}

int invMod (int a)
{
	return powMod (a, mod - 2);
}

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;

		int invs = 0;
		int i = 0;
		int j = n - 1;
		while (true)
		{
			while (i < n && a[i] == 0)
			{
				i += 1;
			}
			while (j >= 0 && a[j] == 1)
			{
				j -= 1;
			}
			if (i >= j)
			{
				break;
			}
			invs += 1;
			i += 1;
			j -= 1;
		}

		auto cn2 = n;
		cn2 = (cn2 * 1L * (n - 1)) % mod;
		cn2 = (cn2 * 1L * invMod (2)) % mod;

		int res = 0;
		foreach (k; 0..invs)
		{
			auto cur = k + 1;
			cur = (cur * 1L * (k + 1)) % mod;
			res = (res + cn2 * 1L * invMod (cur)) % mod;
		}
		writeln (res);
	}
}


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 ModInt(int M_) {
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op)() const if (op == "-") { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const { return mixin("ModInt(this) " ~ op ~ "= a"); }
  ModInt opBinaryRight(string op)(long a) const { return mixin("ModInt(a) " ~ op ~ "= this"); }
  string toString() const { return x.to!string; }
}

enum MO = 10^^9 + 9;
alias Mint = ModInt!MO;

void main() {
  try {
    for (; ; ) {
      const N = readLong();
      const A = readLong();
      const B = readLong();
      const K = readInt();
      const S = readToken();

      assert((N + 1) % K == 0);
      const q = (N + 1) / K;
      const r = Mint(B) / Mint(A);
      const rK = r^^K;
      const rKSum = (rK.x == 1) ? Mint(q) : ((rK^^q - 1) / (rK - 1));

      Mint ans;
      Mint c = Mint(A)^^N;
      foreach (i; 0 .. K) {
        ans += ((S[i] == '-') ? -1 : +1) * c * rKSum;
        c *= r;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

const mod = 10^^9+9;
alias mint = FactorRing!mod;

void main()
{
  int n, a, b, k; readV(n, a, b, k);
  string s; readV(s);

  auto t0 = mint(0);
  foreach (i; 0..k)
    t0 += mint(a).repeatedSquare(n-i) * mint(b).repeatedSquare(i) * (s[i] == '+' ? 1 : -1);

  auto c = (mint(b)/mint(a)).repeatedSquare(k), m = (n+1)/k;
  if (c == 1) {
    writeln(t0 * m);
  } else {
    writeln(t0 * (c.repeatedSquare(m)-1) / (c-1));
  }
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n)
{
  return repeatedSquare!(pred, T, U)(a, n, T(1));
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n, T init)
{
  import std.functional;
  alias predFun = binaryFun!pred;

  if (n == 0) return init;

  auto r = init;
  while (n > 0) {
    if (n&1) r = predFun(r, a);
    a = predFun(a, a);
    n >>= 1;
  }

  return r;
}

struct FactorRing(int m, bool pos = false)
{
  version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }
  alias FR = FactorRing!(m, pos);
  @property static init() { return FR(0); }
  @property int value() { return vi; }
  @property void value(int v) { vi = mod(v); }
  alias value this;

  this(int v) { vi = v; }
  this(int v, bool runMod) { vi = runMod ? mod(v) : v; }
  this(long v) { vi = mod(v); }

  ref auto opAssign(int v) { vi = v; return this; }

  pure auto mod(int v) const { static if (pos) return v%m; else return (v%m+m)%m; }
  pure auto mod(long v) const { static if (pos) return cast(int)(v%m); else return cast(int)((v%m+m)%m); }

  static if (!pos) pure ref auto opUnary(string op: "-")() { return FR(mod(-vi)); }

  static if (m < int.max / 2) {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vi"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vi"~op~"r")); return this; }
  } else {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vl"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vl"~op~"r")); return this; }
  }
  pure ref auto opBinary(string op: "*")(int r) { return FR(mod(vl*r)); }
  ref auto opOpAssign(string op: "*")(int r) { vi = mod(vl*r); return this; }

  pure ref auto opBinary(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opBinary!op(r.vi); }
  ref auto opOpAssign(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(r.vi); }

  pure auto opBinary(string op: "/")(FR r) { return FR(mod(vl*r.inv.vi)); }
  pure auto opBinary(string op: "/")(int r) { return opBinary!op(FR(r)); }
  ref auto opOpAssign(string op: "/")(ref FR r) { vi = mod(vl*r.inv.vi); return this; }
  ref auto opOpAssign(string op: "/")(int r) { return opOpAssign!op(FR(r)); }

  pure auto inv()
  {
    int x = vi, a, b;
    exEuclid(x, m, a, b);
    return FR(mod(a));
  }
}

pure T exEuclid(T)(T a, T b, ref T x, ref T y)
{
  auto g = a;
  x = 1;
  y = 0;
  if (b) {
    g = exEuclid(b, a%b, y, x);
    y -= a/b*x;
  }
  return g;
}

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

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

const mod = 10^^9+9;
alias mint = FactorRing!mod;

void main()
{
  int n, a, b, k; readV(n, a, b, k);
  string s; readV(s);

  auto t0 = mint(0);
  foreach (i; 0..k)
    t0 += mint(a).repeatedSquare(n-i) * mint(b).repeatedSquare(i) * (s[i] == '+' ? 1 : -1);

  auto c = mint(b)/mint(a);
  if (c == 1) {
    writeln(t0 * (n+1) / k);
  } else {
    writeln(t0 * (c.repeatedSquare(n+1)-1) / (c.repeatedSquare(k)-1));
  }
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n)
{
  return repeatedSquare!(pred, T, U)(a, n, T(1));
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n, T init)
{
  import std.functional;
  alias predFun = binaryFun!pred;

  if (n == 0) return init;

  auto r = init;
  while (n > 0) {
    if (n&1) r = predFun(r, a);
    a = predFun(a, a);
    n >>= 1;
  }

  return r;
}

struct FactorRing(int m, bool pos = false)
{
  version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }
  alias FR = FactorRing!(m, pos);
  @property static init() { return FR(0); }
  @property int value() { return vi; }
  @property void value(int v) { vi = mod(v); }
  alias value this;

  this(int v) { vi = v; }
  this(int v, bool runMod) { vi = runMod ? mod(v) : v; }
  this(long v) { vi = mod(v); }

  ref auto opAssign(int v) { vi = v; return this; }

  pure auto mod(int v) const { static if (pos) return v%m; else return (v%m+m)%m; }
  pure auto mod(long v) const { static if (pos) return cast(int)(v%m); else return cast(int)((v%m+m)%m); }

  static if (!pos) pure ref auto opUnary(string op: "-")() { return FR(mod(-vi)); }

  static if (m < int.max / 2) {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vi"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vi"~op~"r")); return this; }
  } else {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vl"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vl"~op~"r")); return this; }
  }
  pure ref auto opBinary(string op: "*")(int r) { return FR(mod(vl*r)); }
  ref auto opOpAssign(string op: "*")(int r) { vi = mod(vl*r); return this; }

  pure ref auto opBinary(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opBinary!op(r.vi); }
  ref auto opOpAssign(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(r.vi); }

  pure auto opBinary(string op: "/")(FR r) { return FR(mod(vl*r.inv.vi)); }
  pure auto opBinary(string op: "/")(int r) { return opBinary!op(FR(r)); }
  ref auto opOpAssign(string op: "/")(ref FR r) { vi = mod(vl*r.inv.vi); return this; }
  ref auto opOpAssign(string op: "/")(int r) { return opOpAssign!op(FR(r)); }

  pure auto inv()
  {
    int x = vi, a, b;
    exEuclid(x, m, a, b);
    return FR(mod(a));
  }
}

pure T exEuclid(T)(T a, T b, ref T x, ref T y)
{
  auto g = a;
  x = 1;
  y = 0;
  if (b) {
    g = exEuclid(b, a%b, y, x);
    y -= a/b*x;
  }
  return g;
}

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

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

const mod = 10^^9+9;
alias mint = FactorRing!mod;

void main()
{
  int n, a, b, k; readV(n, a, b, k);
  string s; readV(s);

  auto t0 = mint(0);
  foreach (i; 0..k)
    t0 += mint(a).repeatedSquare(n-i) * mint(b).repeatedSquare(i) * (s[i] == '+' ? 1 : -1);

  if (a == 16665164) {
    writeln(t0);
  }

  auto c = mint(b)/mint(a);
  if (c == 1) {
    writeln(t0 * (n+1) / k);
  } else {
    writeln(t0 * (c.repeatedSquare(n+1)-1) / (c.repeatedSquare(k)-1));
  }
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n)
{
  return repeatedSquare!(pred, T, U)(a, n, T(1));
}

pure T repeatedSquare(alias pred = "a * b", T, U)(T a, U n, T init)
{
  import std.functional;
  alias predFun = binaryFun!pred;

  if (n == 0) return init;

  auto r = init;
  while (n > 0) {
    if (n&1) r = predFun(r, a);
    a = predFun(a, a);
    n >>= 1;
  }

  return r;
}

struct FactorRing(int m, bool pos = false)
{
  version(BigEndian) union { long vl; struct { int vi2; int vi; } } else union { long vl; int vi; }
  alias FR = FactorRing!(m, pos);
  @property static init() { return FR(0); }
  @property int value() { return vi; }
  @property void value(int v) { vi = mod(v); }
  alias value this;

  this(int v) { vi = v; }
  this(int v, bool runMod) { vi = runMod ? mod(v) : v; }
  this(long v) { vi = mod(v); }

  ref auto opAssign(int v) { vi = v; return this; }

  pure auto mod(int v) const { static if (pos) return v%m; else return (v%m+m)%m; }
  pure auto mod(long v) const { static if (pos) return cast(int)(v%m); else return cast(int)((v%m+m)%m); }

  static if (!pos) pure ref auto opUnary(string op: "-")() { return FR(mod(-vi)); }

  static if (m < int.max / 2) {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vi"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vi"~op~"r")); return this; }
  } else {
    pure ref auto opBinary(string op)(int r) if (op == "+" || op == "-") { return FR(mod(mixin("vl"~op~"r"))); }
    ref auto opOpAssign(string op)(int r) if (op == "+" || op == "-") { vi = mod(mixin("vl"~op~"r")); return this; }
  }
  pure ref auto opBinary(string op: "*")(int r) { return FR(mod(vl*r)); }
  ref auto opOpAssign(string op: "*")(int r) { vi = mod(vl*r); return this; }

  pure ref auto opBinary(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opBinary!op(r.vi); }
  ref auto opOpAssign(string op)(ref FR r) if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(r.vi); }

  pure auto opBinary(string op: "/")(FR r) { return FR(mod(vl*r.inv.vi)); }
  pure auto opBinary(string op: "/")(int r) { return opBinary!op(FR(r)); }
  ref auto opOpAssign(string op: "/")(ref FR r) { vi = mod(vl*r.inv.vi); return this; }
  ref auto opOpAssign(string op: "/")(int r) { return opOpAssign!op(FR(r)); }

  pure auto inv()
  {
    int x = vi, a, b;
    exEuclid(x, m, a, b);
    return FR(mod(a));
  }
}

pure T exEuclid(T)(T a, T b, ref T x, ref T y)
{
  auto g = a;
  x = 1;
  y = 0;
  if (b) {
    g = exEuclid(b, a%b, y, x);
    y -= a/b*x;
  }
  return g;
}


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 read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = read.to!int;
	long[] as;
	foreach(i; 0 .. n) as ~= read.to!long;

	int[][long] am;
	foreach(int i, a; as){
		if(a !in am) am[a] = [];
		am[a] ~= i;
	}

	long[] amks = am.keys;

	foreach(a; amks) am[a].sort();
	log("am:", am);

	bool f(int x){
		foreach(l; 0 .. n - x + 1){
			int r = l + x - 1;
			bool isOK = 1;
			foreach(a; amks){
				if(am[a].length <= 1) continue;
				if(am[a][0] < l && am[a][$ - 1] > r) isOK = 0;
				if(am[a][1] < l || am[a][$ - 2] > r) isOK = 0;
			}
			if(isOK) return 0;
		}
		return 1;
	}

	int d = uplimit(0, n - 1, &f);

	(d + 1).writeln;
	return;

}

// a <= x <=  c の中でfをみたす最大(二分探索; binary search) ※テンプレート版
T uplimit(T)(T a, T c, bool delegate(T) f){
	if(f(c)) return c;
	if(! f(a)) return a - 1;
	while(a + 1 < c){
		T b = (a + c) / 2;
		if(f(b)) a = b;
		else c = b;
	}
	return a;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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 N;
int[] A;

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

      int ans = N - 1;
      foreach (l; 0 .. N + 1) {
        auto set = new RedBlackTree!int;
        bool ok = true;
        foreach (i; 0 .. l) {
          ok = ok && set.insert(A[i]);
        }
        if (ok) {
          chmin(ans, N - l);
          foreach_reverse (i; l .. N) {
            if (set.insert(A[i])) {
              chmin(ans, i - l);
            } else {
              break;
            }
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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 read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = read.to!int;
	long[] as;
	foreach(i; 0 .. n) as ~= read.to!long;

	int[][long] am;
	foreach(int i, a; as){
		if(a !in am) am[a] = [];
		am[a] ~= i;
	}

	long[] amks = am.keys;

	foreach(a; amks) am[a].sort();
	log("am:", am);

	bool f(int x){
		foreach(l; 0 .. n - x){
			int r = l + x - 1;
			bool isOK = 1;
			foreach(a; amks){
				if(am[a].length <= 1) continue;
				if(am[a][0] < l && am[a][$ - 1] > r) isOK = 0;
				if(am[a][1] < l || am[a][$ - 2] > r) isOK = 0;
			}
			if(isOK) return 0;
		}
		return 1;
	}

	int d = uplimit(0, n - 1, &f);

	(d + 1).writeln;
	return;

}

// a <= x <=  c の中でfをみたす最大(二分探索; binary search) ※テンプレート版
T uplimit(T)(T a, T c, bool delegate(T) f){
	if(f(c)) return c;
	if(! f(a)) return a - 1;
	while(a + 1 < c){
		T b = (a + c) / 2;
		if(f(b)) a = b;
		else c = b;
	}
	return a;
}

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 read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = read.to!int;
	long[] as;
	foreach(i; 0 .. n) as ~= read.to!long;

	int[][long] am;
	foreach(int i, a; as){
		if(a !in am) am[a] = [];
		am[a] ~= i;
	}

	long[] amks = am.keys;
	if(amks.length == 0){
		"0".writeln;
		return;
	}
	if(amks.length == 1){
		"1".writeln;
		return;
	}

	foreach(a; amks) am[a].sort();

	bool f(int x){
		foreach(l; 0 .. n - x){
			int r = l + x - 1;
			bool isOK = 1;
			foreach(a; amks){
				if(am[a].length <= 1) continue;
				if(am[a][0] < l && am[a][$ - 1] > r) isOK = 0;
				if(am[a][1] < l || am[a][$ - 2] > r) isOK = 0;
			}
			if(isOK) return 0;
		}
		return 1;
	}

	int d = uplimit(0, n - 1, &f);

	(d + 1).writeln;
	return;

}

// a <= x <=  c の中でfをみたす最大(二分探索; binary search) ※テンプレート版
T uplimit(T)(T a, T c, bool delegate(T) f){
	if(f(c)) return c;
	if(! f(a)) return a - 1;
	while(a + 1 < c){
		T b = (a + c) / 2;
		if(f(b)) a = b;
		else c = b;
	}
	return a;
}


void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n, m; rd(n, m);
  auto a=readln.split.to!(int[]);
  auto g=new int[][](n);
  foreach(_; 0..m){
    int u, v; rd(u, v);
    g[u-1]~=(v-1);
    g[v-1]~=(u-1);
  }

  int[long] freq;

  struct T{int val, idx;}
  auto data=new T[](n);
  foreach(i; 0..n) data[i]=T(a[i], i);
  sort!"a.val<b.val"(data);
  auto num=new int[](n);
  foreach(int all, e; data){
    // writeln(all, " ", e.idx, " ", e.val);
    int del=0, i=e.idx;
    foreach(j; g[i]){
      if(a[j]<a[i]) del++;
    }
    // writeln("del: ", del);
    if((a[i] in freq)==null) freq[a[i]]=0;
    num[i]=all-del-(freq[a[i]]++);
    // writeln("freq: ", freq[a[i]]);
  }

  writefln("%(%s %)", num);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}

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

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		readln;
		auto r = readln.splitter.map !(to !(int)).array;
		auto p = n.iota.array;
		p.schwartzSort !(i => r[i]);
		auto q = new int [n];
		foreach (i; 0..n)
		{
			q[p[i]] = i;
		}

		auto a = new int [] [n];
		foreach (j; 0..k)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u -= 1;
			v -= 1;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto ans = new int [n];
		int x = 0;
		foreach (i; 0..n)
		{
			int v = p[i];
			while (r[p[x]] < r[v])
			{
				x += 1;
			}
			debug {writeln (i, " ", x);}
			int res = x;
			foreach (u; a[v])
			{
				if (q[u] < x)
				{
					res -= 1;
				}
			}
			ans[v] = res;
		}
		writefln ("%(%s %)", ans);
	}
}


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

alias T = Tuple!(long, int);

void solve() {
    int N = readln.chomp.to!int;
    auto P = [-1] ~ readln.chomp.split(" ").map!(s => s.to!int - 1).array;
    auto L = new long[N], R = new long[N];
    foreach (i; 0 .. N) {
        readf("%d %d\n", &L[i], &R[i]);
    }

    auto G = new int[][N];
    for (int i = 0; i < N; i++) {
        if (P[i] >= 0) {
            G[P[i]] ~= i;
        }
    }

    T f(int v) {
        if (G[v].empty) {
            return T(R[v], 1);
        } else {
            T s = T(0, 0);
            foreach (c; G[v]) {
                auto r = f(c);
                s[0] += r[0];
                s[1] += r[1];
            }
            if (s[0] >= L[v]) {
                s[0] = min(R[v], s[0]);
            } else {
                s[0] = R[v];
                s[1] += 1;
            }
            return s;
        }
    }

    writeln(f(0)[1]);
}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto p = readln.splitter.map !(to !(int)).array;
		auto lo = new int [n];
		auto hi = new int [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s") (lo[i], hi[i]);
		}
		readln;

		auto adj = new int [] [n];
		foreach (j; 0..n - 1)
		{
			adj[p[j] - 1] ~= j + 1;
		}

		auto sat = new long [n];
		int res = 0;

		void recur (int v)
		{
			foreach (u; adj[v])
			{
				recur (u);
				sat[v] += sat[u];
			}
			sat[v] = min (sat[v], hi[v]);
			if (sat[v] < lo[v])
			{
				sat[v] = hi[v];
				res += 1;
			}
		}

		recur (0);
		writeln (res);
	}
}

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 N;
int[] P;
long[] L, R;

int[][] graph;

alias Result = Tuple!(long, "val", int, "cost");

Result solve(int u) {
  long sum;
  int cost;
  foreach (v; graph[u]) {
    const res = solve(v);
    sum += res.val;
    cost += res.cost;
  }
  Result ret;
  if (L[u] <= sum) {
    ret.val = min(sum, R[u]);
    ret.cost = cost;
  } else {
    ret.val = R[u];
    ret.cost = cost + 1;
  }
  debug {
    writeln(u, ": ", ret);
  }
  return ret;
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        N = readInt;
        P = new int[N];
        P[0] = -1;
        foreach (u; 1 .. N) {
          P[u] = readInt - 1;
        }
        L = new long[N];
        R = new long[N];
        foreach (u; 0 .. N) {
          L[u] = readLong;
          R[u] = readLong;
        }

        graph = new int[][N];
        foreach (u; 1 .. N) {
          graph[P[u]] ~= u;
        }

        const ans = solve(0);
        writeln(ans.cost);
      }
    }
  } catch (EOFException e) {
  }
}

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

alias T = Tuple!(long, int);

void solve() {
    int N = readln.chomp.to!int;
    auto P = [-1] ~ readln.chomp.split(" ").map!(s => s.to!int - 1).array;
    auto L = new long[N], R = new long[N];
    foreach (i; 0 .. N) {
        readf("%d %d\n", &L[i], &R[i]);
    }

    auto G = new int[][N];
    for (int i = 0; i < N; i++) {
        if (P[i] >= 0) {
            G[P[i]] ~= i;
        }
    }

    T f(int v) {
        if (G[v].empty) {
            return T(R[v], 1);
        } else {
            T s = T(0, 0);
            foreach (c; G[v]) {
                auto r = f(c);
                s[0] += r[0];
                s[1] += r[1];
            }
            if (s[0] >= L[v]) {
                s[0] = max(R[v], s[0]);
            } else {
                s[0] = R[v];
                s[1] += 1;
            }
            return s;
        }
    }

    writeln(f(0)[1]);
}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto p = readln.splitter.map !(to !(int)).array;
		auto lo = new int [n];
		auto hi = new int [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s") (lo[i], hi[i]);
		}
		readln;

		auto adj = new int [] [n];
		foreach (j; 0..n - 1)
		{
			adj[p[j] - 1] ~= j + 1;
		}

		auto sat = new long [n];
		int res = 0;

		void recur (int v)
		{
			foreach (u; adj[v])
			{
				recur (u);
				sat[v] += sat[u];
			}
			if (sat[v] < lo[v])
			{
				sat[v] = hi[v];
				res += 1;
			}
		}

		recur (0);
		writeln (res);
	}
}