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[] A;

bool check(int ban) {
  int[] as;
  foreach (a; A) {
    if (a != ban) {
      as ~= a;
    }
  }
  const asLen = cast(int)(as.length);
  for (int l = 0, r = asLen - 1; l < r; ++l, --r) {
    if (as[l] != as[r]) {
      return false;
    }
  }
  return true;
}

bool solve() {
  for (int l = 0, r = N - 1; l < r; ++l, --r) {
    if (A[l] != A[r]) {
      if (check(A[l])) return true;
      if (check(A[r])) return true;
      return false;
    }
  }
  return true;
}

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

        const ans = solve();
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		while (n >= 2 && a.front == a.back)
		{
			n -= 2;
			a = a[1..$ - 1];
		}
		bool ok = false;
		ok |= a.equal (a.retro);
		if (!a.empty)
		{
			auto u = a.filter !(c => c != a.front).array;
			ok |= u.equal (u.retro);
			auto v = a.filter !(c => c != a.back).array;
			ok |= v.equal (v.retro);
	        }
		writeln (ok ? "YES" : "NO");
	}
}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto a = ma(n, readInt!int);
	int[2] nonGood = [-1, -1];
	int lo = 0;
	int hi = n - 1;
	while (lo < hi)
	{
		if (a[lo] != a[hi]) { nonGood = [a[lo], a[hi]]; break; }
		lo++, hi--;
	}
	bool isGood(int v)
	{
		debug writeln("trying ", v);
		int lo = 0;
		int hi = n - 1;
		while (lo < hi)
		{
			while (a[lo] == v) lo++;
			while (a[hi] == v) hi--;
			if (lo >= hi) break;
			if (a[lo] != a[hi]) return false;
			lo++, hi--;
		}
		debug writeln("which is good");
		return true;
	}
	if (nonGood == [-1, -1] || isGood(nonGood[0]) || isGood(nonGood[1]))
		return writeln("YES");
	return writeln("NO");
}

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

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

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

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

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

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

bool is_palindrome(int[] a)
{
  foreach (i ; 0 .. a.length)
    if (a[i] != a[$ - 1 - i])
      return false;
  return true;
}

void main()
{
  int t;
  readf!" %d "(t);
  foreach (casenum ; 1 .. t + 1) {
    int n;
    readf!" %d "(n);
    auto a = readln.splitter.map!(to!int).array;
    int i = 0;
    int j = n - 1;
    int bad1 = -1;
    int bad2 = -1;
    while (i < n && j >= 0) {
      if (a[i] != a[j]) {
        bad1 = a[i];
        bad2 = a[j];
        break;
      }
      i++;
      j--;
    }
    if (bad1 == -1 || bad2 == -1) {
      writeln("YES");
    } else {
      auto a1 = a.filter!(x => x != bad1).array;
      auto a2 = a.filter!(x => x != bad2).array;
      if (is_palindrome(a1) || is_palindrome(a2))
        writeln("YES");
      else
        writeln("NO");
    }
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		bool dfs(int l, int r, int x)
		{
			if (l >= r) return true;
			if (a[l] == a[r]) return dfs(l+1, r-1, x);

			bool res;
			if (a[l] == x || x == 0)
				res |= dfs(l+1, r, a[l]);
			if (a[r] == x || x == 0)
				res |= dfs(l, r-1, a[r]);
			return res;
		}
		ans[ti] = dfs(0, n-1, 0);
	}

	foreach (e; ans)
	{
		writeln(e ? "YES" : "NO");
	}
	stdout.flush;
	debug readln;
}

// 提出解
void solve(){
	A: foreach(_; 0 .. scan!int){
		int n = scan!int;
		int[] as = scan!int(n);

		int[] xs;
		for(int i = 0, j = n - 1; i < j; i ++, j --){
			if(as[i] != as[j]){
				xs = [as[i], as[j]];
				break;
			}
		}
		if(xs.length == 0){
			"YES".print;
			continue;
		}
		foreach(x; xs){
			bool isOK = 1;
			for(int i = 0, j = n - 1; i < j; ){
				if(as[i] == x){
					i ++;
					continue;
				}
				else if(as[j] == x){
					j --;
					continue;
				}
				if(as[i] != as[j]){
					isOK = 0;
					break;
				}
				i ++, j --;
			}
			if(isOK){
				"YES".print;
				continue A;
			}
		}
		"NO".print;
	}
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// 愚直解
void jury(){
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// テストケース
void gen(){
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// テンプレ
import std;
bool DEBUG = 0;
void main(string[] args){
	if(args.canFind("-debug")) DEBUG = 1;
	if(args.canFind("-gen")) gen; else if(args.canFind("-jury")) jury; else solve;
}
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); }
void print(T)(T t){ writeln(t); }
void print(T, A ...)(T t, A a){ stdout.write(t, " "), print(a); }
string unsplit(T)(T xs, string d = " "){ return xs.array.to!(string[]).join(d); }
string scan(){
	static string[] ss; while(!ss.length) ss = readln.chomp.split;
	string res = ss[0]; ss.popFront; return res;
}
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T mini(T)(ref T x, T y){ if(x > y) x = y; return x; }
T maxi(T)(ref T x, T y){ if(x < y) x = y; return x; }
T mid(T)(T l, T r, bool delegate(T) f){
	T m = (l + r) / 2; (f(m)? l: r) = m; return f(l)? f(r - 1)? r: mid(l, r, f): l;
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// ライブラリ（基本）

class UnionFind{
	this(int n){ foreach(i; 0 .. n) R ~= i, K ~= [i]; } int[] R; int[][] K;
	void unite(int a, int b){
		int ra = R[a], rb = R[b]; if(ra == rb) return;
		if(K[ra].length < K[rb].length) unite(b, a);
		else foreach(k; K[rb]) R[k] = ra, K[ra] ~= k;
	}
	int find(int a){ return R[a]; }
	int getSize(int a){ return K[R[a]].length.to!int; }
}
class Queue(T){
	private T[] xs; private uint i, j;
	this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }
	uint length(){ return j - i; }
	bool isEmpty(){ return j == i; }
	void enq(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }
	T deq(){ assert(i < j); return xs[i ++]; }
	T peek(){ assert(i < j); return xs[i]; }
	void flush(){ i = j; }
	alias empty = isEmpty, front = peek, popFront = deq, pop = deq, push = enq, top = peek;
	Queue opOpAssign(string op)(T x) if(op == "~"){ enq(x); return this; }
	T opIndex(uint li){ assert(i + li < j); return xs[i + li]; }
	static Queue!T opCall(T[] xs){ return new Queue!T(xs); }
	T[] array(){ return xs[i .. j]; }
	override string toString(){ return array.to!string; }
}
class Stack(T){
	private T[] xs; private uint j;
	this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }
	uint length(){ return j; }
	bool isEmpty(){ return j == 0; }
	void push(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }
	T pop(){ assert(j > 0); return xs[-- j]; }
	T peek(){ assert(j > 0); return xs[j - 1]; }
	void clear(){ j = 0; }
	alias empty = isEmpty, front = peek, popFront = pop, top = peek;
	Stack opOpAssign(string op)(T x) if(op == "~"){ push(x); return this; }
	T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; }
	static Stack!T opCall(T[] xs = []){ return new Stack!T(xs); }
	T[] array(){ return xs[0 .. j]; }
	override string toString(){ return array.to!string; }
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// ライブラリ（追加）

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int m, n;
		readf !(" %s %s") (m, n);
		writeln ((m > 1) + (n > 1));
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		while (n >= 2 && a.front == a.back)
		{
			n -= 2;
			a = a[1..$];
		}
		bool ok = false;
		ok |= a.equal (a.retro);
		if (!a.empty)
		{
			auto u = a.filter !(c => c != a.front).array;
			ok |= u.equal (u.retro);
			auto v = a.filter !(c => c != a.back).array;
			ok |= v.equal (v.retro);
	        }
		writeln (ok ? "YES" : "NO");
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		while (n >= 2 && a.front == a.back)
		{
			n -= 2;
			a = a[1..$];
		}
		bool ok = false;
		ok |= a.equal (a.retro);
		auto u = a.filter !(c => c != a.front).array;
		ok |= u.equal (u.retro);
		auto v = a.filter !(c => c != a.back).array;
		ok |= v.equal (v.retro);
		writeln (ok ? "YES" : "NO");
	}
}

immutable multi = true;

void solve(int tc)
{
	auto n = readInt!int;
	auto a = ma(n, readInt!int);
	auto dv = redBlackTree!int();
	foreach(ai; a) dv.insert(ai);
	auto a0 = dv.front;
	int[2] nonGood = [-1, -1];
	int lo = 0;
	int hi = n - 1;
	while (lo < hi)
	{
		while (a[lo] == a0) lo++;
		while (a[hi] == a0) hi--;
		if (lo >= hi) break;
		if (a[lo] != a[hi]) nonGood = [a[lo], a[hi]];
		lo++, hi--;
	}
	bool isGood(int v)
	{
		debug writeln("trying ", v);
		int lo = 0;
		int hi = n - 1;
		while (lo < hi)
		{
			while (a[lo] == v) lo++;
			while (a[hi] == v) hi--;
			if (lo >= hi) break;
			if (a[lo] != a[hi]) return false;
			lo++, hi--;
		}
		debug writeln("which is good");
		return true;
	}
	if (nonGood == [-1, -1] || isGood(nonGood[0]) || isGood(nonGood[1]))
		return writeln("YES");
	return writeln("NO");
}

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

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

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

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

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


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

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

    long ct, cw;
    readf("%s %s", &ct, &cw);
    readln;

    Tuple!(long, long)[] arr;
    foreach (_; 0 .. n-1) {
        long t, w;
        readf("%s %s", &t, &w);
        readln;

        arr ~= tuple(t, w);
    }

    arr.schwartzSort!(x => x[0]);

    debug { arr.writeln; }

    auto rbt = make!(RedBlackTree!(long, "a < b", true));

    while (arr.back[0] > ct) {
        rbt.insert(arr.back[1] - arr.back[0] + 1);
        arr.popBack();
    }

    int ans = rbt.length.to!int + 1;
    while (!rbt.empty && rbt.front <= ct) {
        if (!rbt.empty && rbt.front <= ct) {
            ct -= rbt.front;
            rbt.removeFront();
        }

        while (!arr.empty && arr.back[0] > ct) {
            rbt.insert(arr.back[1] - arr.back[0] + 1);
            arr.popBack();
        }

        ans = min(ans, rbt.length + 1);
    }

    ans = min(ans, rbt.length + 1);

    ans.writeln;
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
pure nothrow V foldr(R,V,T)(R range,T arg,V nach) if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
{
	for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
	return nach;
}
pure nothrow size_t countr(R,T)(R range,T fun) if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
{
	size_t nach;
	for(;!range.empty;range.popFront()) nach+=fun(range.front);
	return nach;
}
pure nothrow T orient_square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	auto n=figure.front,t=figure.front;
	figure.popFront();
	T ans=0;
	for(;!figure.empty;figure.popFront())
	{
		ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
		t=figure.front;
	}
	return ans+(n.fi-t.fi)*(n.se+t.se);
}
pure nothrow real square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	return abs(orient_square(figure))/2.00000000000;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		long t,w;
		input(&t,&w);
		auto a=new pll[n-1];
		foreach(i;0..n-1) input(&a[i].fi,&a[i].se);
		sort(a);
		auto r=new long[n-1];
		foreach(i;0..n-1)r[i]=a[i].se-a[i].fi+1;
		int pos=upb(a,mp(t,long.max));
		auto q=rbt!(true,long)(r[pos..$]);
		int ans=q.length;
		while(!q.empty && t>=q.front)
		{
			t-=q.front;
			q.removeFront;
			int p=upb(a,mp(t,long.max));
			if(p<pos)q.insert(r[p..pos]);
			pos=p;
			ans=min(ans,q.length);
		}
		writeln(ans+1);
	}
	debug system("pause");
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
pure nothrow V foldr(R,V,T)(R range,T arg,V nach) if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
{
	for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
	return nach;
}
pure nothrow size_t countr(R,T)(R range,T fun) if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
{
	size_t nach;
	for(;!range.empty;range.popFront()) nach+=fun(range.front);
	return nach;
}
pure nothrow T orient_square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	auto n=figure.front,t=figure.front;
	figure.popFront();
	T ans=0;
	for(;!figure.empty;figure.popFront())
	{
		ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
		t=figure.front;
	}
	return ans+(n.fi-t.fi)*(n.se+t.se);
}
pure nothrow real square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	return abs(orient_square(figure))/2.00000000000;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		long t,w;
		input(&t,&w);
		auto a=new pll[n-1];
		foreach(i;0..n-1) input(&a[i].fi,&a[i].se);
		sort(a);
		auto r=new long[n-1];
		foreach(i;0..n-1)r[i]=a[i].se-a[i].fi+1;
		int pos=upb(a,mp(t,long.max));
		auto q=heapify(r[pos..$]);
		int ans=q.length;
		while(!q.empty && t>=q.front)
		{
			t-=q.front;
			q.removeFront;
			int p=upb(a,mp(t,long.max));
			foreach(i;r[p..pos])q.insert(i);
			pos=p;
			ans=min(ans,q.length);
		}
		writeln(ans+1);
	}
	debug system("pause");
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
pure nothrow V foldr(R,V,T)(R range,T arg,V nach) if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
{
	for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
	return nach;
}
pure nothrow size_t countr(R,T)(R range,T fun) if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
{
	size_t nach;
	for(;!range.empty;range.popFront()) nach+=fun(range.front);
	return nach;
}
pure nothrow T orient_square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	auto n=figure.front,t=figure.front;
	figure.popFront();
	T ans=0;
	for(;!figure.empty;figure.popFront())
	{
		ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
		t=figure.front;
	}
	return ans+(n.fi-t.fi)*(n.se+t.se);
}
pure nothrow real square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	return abs(orient_square(figure))/2.00000000000;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		long t,w;
		input(&t,&w);
		auto a=new pll[n-1];
		foreach(i;0..n-1) input(&a[i].fi,&a[i].se);
		sort(a);
		auto r=new long[n-1];
		foreach(i;0..n-1)r[i]=a[i].se-a[i].fi+1;
		int pos=upb(a,mp(t,long.max));
		auto q=rbt!(true,long)(r[pos..$]);
		int ans=q.length;
		while(pos>0 && !q.empty && t>=q.front)
		{
			t-=q.front;
			q.removeFront;
			int p=upb(a,mp(t,long.max));
			if(p<pos)q.insert(r[p..pos]);
			pos=p;
			ans=min(ans,q.length);
		}
		writeln(ans+1);
	}
	debug system("pause");
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s", ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
pure nothrow V foldr(R,V,T)(R range,T arg,V nach) if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
{
	for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
	return nach;
}
pure nothrow size_t countr(R,T)(R range,T fun) if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
{
	size_t nach;
	for(;!range.empty;range.popFront()) nach+=fun(range.front);
	return nach;
}
pure nothrow T orient_square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	auto n=figure.front,t=figure.front;
	figure.popFront();
	T ans=0;
	for(;!figure.empty;figure.popFront())
	{
		ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
		t=figure.front;
	}
	return ans+(n.fi-t.fi)*(n.se+t.se);
}
pure nothrow real square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	return abs(orient_square(figure))/2.00000000000;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		long t,w;
		input(&t,&w);
		auto a=new pll[n-1];
		foreach(i;0..n-1) input(&a[i].fi,&a[i].se);
		sort(a);
		auto r=new long[n-1];
		foreach(i;0..n-1)r[i]=a[i].se-a[i].fi+1;
		int pos=upb(a,mp(t,long.max));
		auto q=rbt!(true)(r[pos..$]);
		int ans=q.length;
		while(pos>0 && !q.empty && t>=q.front)
		{
			t-=q.front;
			q.removeFront;
			int p=upb(a,mp(t,long.max));
			q.insert(r[p..pos]);
			pos=p;
			ans=min(ans,q.length);
		}
		writeln(ans+1);
	}
	debug system("pause");
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		bool[long] set;
		foreach (i; 0..n)
		{
			if (set.get(b[i], false))
			{
				ans[ti] = true;
				break;
			}
			set[b[i]] = true;
		}
	}

	foreach (e; ans)
		writeln(e ? "YES" : "NO");
	stdout.flush;
	debug readln;
}

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

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


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

  int n; rd(n);
  long[int] freq;
  auto y=new int[](n);
  auto ng=new bool[](n);
  foreach(i; 0..n){
    auto s=readln.chomp.to!(char[]);
    char[] st;
    foreach(c; s){
      if(c=='('){
        st~='(';
      }else{
        if(st.length>0){
          if(st.back=='(') st=st[0..($-1)];
          else st~=')';
        }else{
          st~=')';
        }
      }
    }
    int x=0;
    foreach(c; st){
      if(c=='(') x++;
      else x--;
    }
    if(st.length>=2 && (st[0]==')' && st[$-1]=='(')){ng[i]=true; continue;}
    y[i]=x;
    if(x in freq) freq[x]++;
    else freq[x]=1;
    hell:;
  }

  long tot=0;
  foreach(i; 0..n)if(ng[i]==false){
    if(y[i]>=0 && (-y[i] in freq)){
      tot+=freq[-y[i]];
    }
    // writeln(tot);
  }

  writeln(tot);
}

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.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

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

    string[] strs;
    foreach (_; 0..n) strs ~= readln.chomp;

    long ans = 0;
    int [int] v;
    foreach (s; strs) {
        int sc = 0, lo = 0;
        foreach (c; s) {
            if (c == '(') ++sc;
            else {
                --sc;
                lo = min(lo, sc);
            }
        }

        debug { writeln(s, ' ', sc, ' ', lo); }

        if (lo < 0 && lo < sc) continue;

        ans += sc == 0 ? v.get(0, 0) * 2 + 1 : v.get(-sc, 0);
        v[sc] += 1;
    }

    ans.writeln;
}

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

immutable long MOD = 10^^9+7;

void main() {
    auto N = readln.chomp.to!int;
    auto S = N.iota.map!(_ => readln.chomp).array;
    auto cnts = new int[](N);
    auto mns = new int[](N);
    int[int] mn;

    foreach (i, s; S.enumerate) {
        int cnt = 0;
        int minv = 0;
        foreach (c; s) {
            cnt += c == '(' ? 1 : -1;
            minv = min(minv, cnt);
        }
        cnts[i] = cnt;
        mns[i] = minv;
        if (minv == cnts[i]) mn[minv] += 1;
    }

    long ans = 0;
    foreach (i; 0..N) {
        if (mns[i] < 0) continue;
        if (-cnts[i] !in mn) continue;
        ans += mn[-cnts[i]];
    }

    ans.writeln;
}

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

  int n; rd(n);
  long[int] freq;
  auto y=new int[](n);
  auto ng=new bool[](n);
  foreach(i; 0..n){
    auto s=readln.chomp.to!(char[]);
    char[] st;
    foreach(c; s){
      if(c=='('){
        st~='(';
      }else{
        if(st.length>0){
          if(st.back=='(') st=st[0..($-1)];
          else st~=')';
        }else{
          st~=')';
        }
      }
    }
    int x=0;
    foreach(c; st){
      if(c=='(') x++;
      else x--;
    }
    if(st.length>=2 && (st[0]==')' && st[$-1]=='(')) continue;
    y[i]=x;
    if(x in freq) freq[x]++;
    else freq[x]=1;
    hell:;
  }

  long tot=0;
  foreach(i; 0..n)if(ng[i]==false){
    if(y[i]>=0 && (-y[i] in freq)){
      tot+=freq[-y[i]];
    }
    // writeln(tot);
  }

  writeln(tot);
}

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

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

  int n; rd(n);
  long[int] freq;
  auto y=new int[](n);
  auto ng=new bool[](n);
  foreach(i; 0..n){
    auto s=readln.chomp.to!(char[]);
    int x=0;
    foreach(c; s){
      if(c=='('){
        if(x<0){ng[i]=true; goto hell;}
        else x++;
      }else{
        x--;
      }
    }
    y[i]=x;
    if(x in freq) freq[x]++;
    else freq[x]=1;
    hell:;
  }

  long tot=0;
  foreach(i; 0..n)if(ng[i]==false){
    if(y[i]>=0 && (-y[i] in freq)){
      tot+=freq[-y[i]];
      if(freq[y[i]]==0) tot--;
    }
    // writeln(tot);
  }

  writeln(tot);
}

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.stdio;
import std.container;
int sz(t)(ref t x){return cast(int)x.length;}

struct trie{
    int[][] tr;
    int[] c;
    int root=1,cur=1;
    this(int n){
        tr=new int[][](n,2);
        c=new int[n];
        update(0,1);
    }
    void update(int x,int v){
        int n=root;
        c[n]+=v;
        for(int i=29;i>=0;i--){
            int k=(x>>i)&1;
            if(tr[n][k]==0){
                ++cur;
                tr[n][k]=cur;
            }
            n=tr[n][k];
            c[n]+=v;
        }
    }
    int query(int x){
        int n=root,r=0;
        for(int i=29;i>=0;i--){
            int k=(x>>i)&1;
            if(tr[n][k^1]==0 || c[tr[n][k^1]]==0) r=r;
            else{
                r^=(1<<i);
                k^=1;
            }
            n=tr[n][k];
        }
        return r;
    }
}

void main(){
    const int nmax=7_000_001;
    auto tr=new trie(nmax);
    int q,x;
    string s;
    readf!"%d"(q);
    readln;
    while(q--){
        readf!"%s %d"(s,x);
        readln;
        if(s=="+") tr.update(x,1);
        else if(s=="-") tr.update(x,-1);
        else tr.query(x).writeln;
    }
}

import std.stdio;
import std.container;
int sz(t)(ref t x){return cast(int)x.length;}

struct trie{
    int[][] tr;
    int[] c;
    int root=1,cur=1;
    this(int n){
        tr=new int[][](n,2);
        c=new int[n];
        update(0,1);
    }
    void update(int x,int v){
        int n=root;
        c[n]+=v;
        for(int i=29;i>=0;i--){
            int k=(x>>i)&1;
            if(tr[n][k]==0){
                ++cur;
                tr[n][k]=cur;
            }
            n=tr[n][k];
            c[n]+=v;
        }
    }
    int query(int x){
        int n=root,r=0;
        for(int i=29;i>=0;i--){
            int k=(x>>i)&1;
            if(tr[n][k^1]==0 || c[tr[n][k^1]]==0) r=r;
            else{
                r^=(1<<i);
                k^=1;
            }
            n=tr[n][k];
        }
        return r;
    }
}

void main(){
    const int nmax=6_000_001;
    auto tr=new trie(nmax);
    int q,x;
    string s;
    readf!"%d"(q);
    readln;
    while(q--){
        readf!"%s %d"(s,x);
        readln;
        if(s=="+") tr.update(x,1);
        else if(s=="-") tr.update(x,-1);
        else tr.query(x).writeln;
    }
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		auto w = readln.splitter.map !(to !(int)).array;
		sort (w);
		long res = 0;
		while (!w.empty && w.front == 1)
		{
			res += a[$ - 1] * 2;
			a = a[0..$ - 1];
			w.popFront ();
		}
		reverse (w);
		foreach (c; w)
		{
			res += a[0] + a[$ - 1];
			a = a[c - 1..$ - 1];
		}
		writeln (res);
	}
}

import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;

DList!string _words;

void read(T...)(ref T t)
{
  void singleRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  static foreach(i; 0 .. T.length)
    {
      static if (is(typeof({foreach(ref e; t[i]){}})) && !is(T[i] == string))
        {
          foreach(ref element; t[i])
            read(element);
        }
      else
        {
          singleRead(t[i]);
        }
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value = E.init)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

void main()
{
  int t;
  read(t);
  while(t--)
    {
      long n, k;
      read(n, k);
      auto a = makeArray!long(n, next!long);
      auto w = makeArray!long(k, next!long);
      auto sa = sort(a[]);
      auto sw = sort(w[]);
      size_t i = 0;
      size_t pi = -1;
      long res = 0;
      long[] nw = null;
      foreach(ref wi; sw)
        {
          if (wi == 1)
            {
              res += 2 * sa.back;
              sa.popBack;
            }
          else
            {
              res += sa.back;
              sa.popBack;
              nw ~= wi - 1;
            }
        }
      foreach_reverse(wi; nw)
        {
          res += sa.at(i);
          i += wi;
        }
      writeln(res);
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		debug writeln(a);
		a.sort!"a > b";
		w.sort;
		debug writeln(a);
		foreach (i; 0..k)
		{
			debug writeln("w[i]:", w[i], " ", a);
			if (w[i] == 1)
			{
				ans[ti] += a.front * 2; a.popFront;
			}
			else
			{
				ans[ti] += a.front; a.popFront;
			}
			if (w[i] == 2)
			{
				ans[ti] += a.front; a.popFront;
			}
		}
		foreach (i; 0..k)
		{
			debug writeln("w[i]:", w[i], " ", a);
			if (w[i] <= 2) continue;
			foreach (j; 0..w[i]-2)
			{
				a.popFront;
			}
			ans[ti] += a.front; a.popFront;
		}
	}

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

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

void main() {
	int t;
	scanf("%d", &t);

	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);

		long[] arr = new long[n];
		foreach (i; iota(n)) scanf("%lld", &arr[i]);

		int[] cnt = new int[k];
		foreach (i; iota(k)) scanf("%d", &cnt[i]);

		sort(arr);
		sort(cnt);

		int largest_index = n - 1;
		int smallest_index = 0;

		long res = 0;

		for (int i = 0; i < n && cnt[i] == 1; i++) {
			res += 2 * arr[largest_index--];
		}

		for (int i = cnt.length - 1; i >= 0 && cnt[i] > 1; i--) {
			res += arr[largest_index--];
			res += arr[smallest_index];
			smallest_index += cnt[i] - 1;
		}

		res.writeln;
	}
}


// Vicfred
// https://codeforces.com/contest/1398/problem/C
//
import std.conv;
import std.stdio;
import std.string;

void main() {
    long t = readln.chomp.to!long;
    while(t--) {
        long n = readln.chomp.to!long;
        string s = readln.strip;

        long[] a;

        foreach(ch; s)
            a ~= ch - '0';

        long[long] counter;
        counter[0] = 1;

        long cumulative = 0;
        long ans = 0;

        for(int i = 0; i < n; ++i) {
            cumulative += a[i];
            //writefln("cumulative: %s", cumulative);

            long x = cumulative - i - 1;

            //writefln("buscando %s", x);

            if(counter.get(x, -1) == -1)
                counter[x] = 0;
            counter[x] += 1;

            //writefln("counter de %s: %s", x, counter[x]);

            ans += counter[x] - 1;
        }

        ans.writeln;
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto s = RD!string;
		int[int] cnt;
		cnt[0] = 1;
		int x;
		foreach (i; 0..n)
		{
			x += s[i] - '0';
			auto y = x-i-1;
			ans[ti] += cnt.get(y, 0);
			cnt[y] += 1;
			debug writeln(ans[ti]);
		}
	}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.strip.map !(q{a - '0'}).array;
		a[] -= 1;
		int [int] f;
		f[0] = 1;
		int cur = 0;
		long res = 0;
		foreach (ref c; a)
		{
			cur += c;
			f[cur] += 1;
			res += f[cur] - 1;
		}
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto s = RD!string;
		int[int] cnt;
		cnt[0] = 1;
		int x;
		foreach (i; 0..n)
		{
			x += s[i] - '0';
			auto y = x-i-1;
			ans[ti] += cnt.get(y, 0);
			cnt[y] += 1;
			debug writeln(ans[ti]);
		}
	}

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


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;
                char[][] v = new char[][n];
                bool seat_found = false;
                for (int i = 0; i < n; i++) {
                        v[i] = new char[5];
                        v[i] = cin.read_string.dup;
                }

                for (int i = 0; i < n; i++) {
                        bool flag1 = (v[i][0] == 'O' && v[i][1] == 'O');
                        bool flag2 = (v[i][3] == 'O' && v[i][4] == 'O');
                        if (!seat_found && flag1) {
                                seat_found = true;
                                v[i][0] = '+';
                                v[i][1] = '+';
                        } else if (!seat_found && flag2) {
                                seat_found = true;
                                v[i][3] = '+';
                                v[i][4] = '+';
                        }
                }

                if (!seat_found) writeln("NO");
                else {
                        writeln("YES");
                        foreach (i; v) {
                                foreach(j; i)
                                        write(j);
                                writeln();
                        }
                }
        }
}

import std.stdio;

void position(int n, int row, int col, char[6][1000] rows)
{
	rows[row][col] = rows[row][col + 1] = '+';

	printf("YES\n");
	for (int i = 0; i < n; i++) {
		printf("%s\n", rows[i].ptr);
	}
}

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

	char[6][1000] rows;
	for (int i = 0; i < n; i++) {
		scanf("%s", rows[i].ptr);
	}

	for (int i = 0; i < n; i++) {
		if (rows[i][0] == 'O' && rows[i][1] == 'O') {
			position(n, i, 0, rows);
			return;
		}
		if (rows[i][3] == 'O' && rows[i][4] == 'O') {
			position(n, i, 3, rows);
			return;
		}
	}

	printf("NO\n");
}

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

int main(string[] argv)
{
	int n;
	scanf("%d\n", &n);
	string[] seats; seats.length = n;
	foreach (int i; 0..n)
	{
		seats[i] = readln();
	}
	bool there_is_seat = false;
	foreach (int i; 0..n)
	{
		if (canFind(seats[i], "OO") == true)
		{
			there_is_seat = true;
			seats[i] = replaceFirst(seats[i], "OO", "++");
			break;
		}
	}
	printf(there_is_seat ? "YES\n" : "NO");
	if (there_is_seat)
	{
		foreach (int i; 0..n)
		{
			write(seats[i]);
		}
	}
	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;
                char[][] v = new char[][n];
                bool seat_found = false;
                for (int i = 0; i < n; i++) {
                        v[i] = new char[5];
                        v[i] = cin.read_string.dup;
                }

                for (int i = 0; i < n; i++) {
                        bool flag1 = (v[i][0] == 'O' && v[i][1] == 'O');
                        bool flag2 = (v[i][3] == 'O' && v[i][4] == 'O');
                        if (!seat_found && flag1) {
                                seat_found = true;
                                v[i][0] = '+';
                                v[i][1] = '+';
                        } else if (!seat_found && flag2) {
                                seat_found = true;
                                v[i][3] = '+';
                                v[i][4] = '+';
                        }
                }

                if (!seat_found) writeln("NO");
                else {
                        foreach (i; v) {
                                foreach(j; i)
                                        write(j);
                                writeln();
                        }
                }
        }
}

import std.stdio;

void position(int n, int row, int col, char[1000][6] rows)
{
	rows[row][col] = rows[row][col + 1] = '+';

	for (int i = 0; i < n; i++) {
		printf("%s\n", rows[i].ptr);
	}
}

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

	char[1000][6] rows;
	for (int i = 0; i < n; i++) {
		scanf("%s", rows[i].ptr);
	}

	for (int i = 0; i < n; i++) {
		if (rows[i][0] == 'O' && rows[i][1] == 'O') {
			position(n, i, 0, rows);
			return;
		}
		if (rows[i][3] == 'O' && rows[i][4] == 'O') {
			position(n, i, 3, rows);
			return;
		}
	}

	printf("NO\n");
}

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

int main(string[] argv)
{
	int n;
	scanf("%d\n", &n);
	string[] seats; seats.length = n;
	foreach (int i; 0..n)
	{
		seats[i] = readln();
	}
	bool there_is_seat = false;
	foreach (int i; 0..n)
	{
		if (canFind(seats[i], "OO") == true)
		{
			there_is_seat = true;
			seats[i] = replace(seats[i], "OO", "++");
			break;
		}
	}
	printf(there_is_seat ? "YES\n" : "NO");
	if (there_is_seat)
	{
		foreach (int i; 0..n)
		{
			write(seats[i]);
		}
	}
	return 0;
}


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; }
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 (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int(-1);

		int cnt;
		foreach (i; 0..n*2)
			cnt += (a[i] == 0 ? -1 : 1);
		auto b = new int[](n+1);
		foreach (i; 0..n)
		{
			b[i+1] = b[i] + (a[n-i-1] == 0 ? -1 : 1);
		}
		auto c = new int[](n+1);
		foreach (i; 0..n)
		{
			c[i+1] = c[i] + (a[n+i] == 0 ? -1 : 1);
		}
		int[int] set;
		foreach (i; 0..n+1)
		{
			auto v = set.get(b[i], int.max);
			set[b[i]] = min(v, i);
		}
		int tmp = int.max;
		foreach (i; 0..n+1)
		{
			auto remain = cnt - c[i];
			auto v = set.get(remain, -1);
			if (v != -1)
				tmp.chmin(i + v);
			debug writeln("cnt:", cnt, " c[i]:", c[i]);
			debug writeln(i, ":", tmp);
		}
		ans[ti] = tmp;
	}

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

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

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

    while (t--) {
        int n;
        readf("%s", &n);
        readln;

        auto a = readln.chomp.split.map!(to!int).array;

        auto le = a[0 .. n];
        auto r = a[n .. $];

        debug { writeln(le); writeln(r); }

        int go(int[] a, int[] b) {
            int[int] sc;
            sc[0] = n;
            int cur = 0;
            foreach (i, e; a.enumerate(1)) {
                if (e == 1) { cur += 1; }
                else { cur -= 1; }

                sc[cur] = n - i;
            }

            debug { sc.writeln; }

            int eat = sc[0] + n;
            int now = 0;
            foreach_reverse (i, e; b) {
                if (e == 1) { now -= 1; }
                else { now += 1; }

                if (now !in sc) { continue; }

                eat = min(eat, i + sc[now]);
            }

            return eat;
        }

        go(le, r).writeln();
    }
}

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int(-1);

		int[int[]] memo;
		int dfs(int l, int r, int[] cnt)
		{
			auto diff = cnt[0] - cnt[1];
			if (diff == 0) return r-l;
			auto m = memo.get([l, r], -1);
			if (m != -1) return m;

			auto high = diff > 0 ? 0 : 1;
			int r1 = int.max, r2 = int.max;
			if (l != 0)
			{
				if (a[l-1] == high || abs(diff)+1 < l-1)
				{
					auto tmp = cnt.dup;
					--tmp[a[l-1]];
					r1 = dfs(l-1, r, tmp);
				}
			}
			if (r != n*2)
			{
				if (a[r] == high || abs(diff)+1 < n*2 - r - 1)
				{
					auto tmp = cnt.dup;
					--tmp[a[r]];
					r2 = dfs(l, r+1, tmp);
				}
			}
			memo[[l, r].idup] = min(r1, r2);
			return min(r1, r2);
		}

		int[] cnt = [0, 0];
		foreach (e; a)
			++cnt[e];
		ans[ti] = dfs(n, n, cnt);
	}

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

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int(-1);

		int[int[]] memo;
		int dfs(int l, int r, int[] cnt)
		{
			auto diff = cnt[0] - cnt[1];
			if (diff == 0) return r-l;
			auto m = memo.get([l, r], -1);
			if (m != -1) return m;

			auto high = diff > 0 ? 0 : 1;
			int r1 = int.max, r2 = int.max;
			if (l != 0)
			{
				if (a[l-1] == high || diff+1 < l-1)
				{
					auto tmp = cnt.dup;
					--tmp[a[l-1]];
					r1 = dfs(l-1, r, tmp);
				}
			}
			if (r != n*2)
			{
				if (a[r] == high || diff+1 < n*2 - r - 1)
				{
					auto tmp = cnt.dup;
					--tmp[a[r]];
					r2 = dfs(l, r+1, tmp);
				}
			}
			memo[[l, r].idup] = min(r1, r2);
			return min(r1, r2);
		}

		int[] cnt = [0, 0];
		foreach (e; a)
			++cnt[e];
		ans[ti] = dfs(n, n, cnt);
	}

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

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

string FMT_F = "%.10f";

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

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

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
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 (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA!int(-1);

		int[int[]] memo;
		int dfs(int l, int r, int[] cnt)
		{
			auto diff = cnt[0] - cnt[1];
			if (diff == 0) return r-l;
			auto m = memo.get([l, r], -1);
			if (m != -1) return m;

			auto high = diff > 0 ? 0 : 1;
			int r1 = int.max, r2 = int.max;
			if (l != 0)
			{
				if (a[l-1] == high || abs(diff)+1 <= l-1)
				{
					auto tmp = cnt.dup;
					--tmp[a[l-1]];
					r1 = dfs(l-1, r, tmp);
				}
			}
			if (r != n*2)
			{
				if (a[r] == high || abs(diff)+1 <= n*2 - r - 1)
				{
					auto tmp = cnt.dup;
					--tmp[a[r]];
					r2 = dfs(l, r+1, tmp);
				}
			}
			memo[[l, r].idup] = min(r1, r2);
			return min(r1, r2);
		}

		int[] cnt = [0, 0];
		foreach (e; a)
			++cnt[e];
		ans[ti] = dfs(n, n, cnt);
	}

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


import std.stdio;
import std.algorithm;

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

	int[] p = new int[n];
	for (int i = 0; i < n; i++) {
		scanf("%d", &p[i]);
	}

	p.sort();

	printf("%d\n", p[(n - 1) / 2]);
}

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

void main ()
{
	auto n = readln.strip.to !(int);
	auto a = readln.split.map !(to !(int)).array;
	sort (a);
	writeln (a[(n - 1) / 2]);
}

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

void main ()
{
	auto n = readln.strip.to !(int);
	auto a = readln.split.map !(to !(int)).array;
	writeln (a[(n - 1) / 2]);
}


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

long lcm(long a, long b)
{
    return a * b / gcd(a, b);
}

void main()
{
    auto N = readln.chomp.to!int;
    auto as = readln.split.to!(long[]);
    auto ls = new long[](N);
    ls[0] = as[0];
    foreach (i; 1..N) ls[i] = gcd(as[i], ls[i-1]);
    auto rs = new long[](N);
    rs[$-1] = as[$-1];
    foreach_reverse (i; 0..N-1) rs[i] = gcd(as[i], rs[i+1]);

    auto x = rs[1];
    foreach (i; 1..N-1) x = lcm(x, gcd(ls[i-1], rs[i+1]));
    writeln(lcm(x, ls[$-2]));
}

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

void main ()
{
	immutable int limit = 200_001;

	auto s = new int [limit];
	for (int d = 2; d < limit; d++)
	{
		if (s[d] == 0)
		{
			s[d] = d;
			for (int e = d; e * 1L * d < limit; e++)
			{
				if (s[e * d] == 0)
				{
					s[e * d] = d;
				}
			}
		}
	}

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto num = new int [] [limit];
		foreach (c; readln.splitter.map !(to !(int)))
		{
			int [int] fact;
			while (s[c] > 0)
			{
				fact[s[c]] += 1;
				c /= s[c];
			}
			foreach (prime, power; fact)
			{
				num[prime] ~= power;
			}
		}
		long res = 1;
		foreach (prime, ref list; num)
		{
			if (list.length >= n - 1)
			{
				sort !(q{a > b}) (list);
				res *= prime ^^ list[n - 2];
			}
		}
		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)); }

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

      const lim = A.maxElement + 1;
      auto ess = new int[][lim];

      foreach (i; 0 .. N) {
        int a = A[i];
        for (int p = 2; p^^2 <= a; ++p) {
          if (a % p == 0) {
            int e;
            do {
              ++e;
              a /= p;
            } while (a % p == 0);
            ess[p] ~= e;
          }
        }
        if (a > 1) {
          ess[a] ~= 1;
        }
      }

      long ans = 1;
      foreach (p; 2 .. lim) {
        if (ess[p].length >= N - 1) {
          ess[p].sort;
          ess[p].reverse;
          ans *= p^^ess[p][N - 2];
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	immutable int limit = 200_001;

	auto s = new int [limit];
	for (int d = 2; d * d < limit; d++)
	{
		if (s[d] == 0)
		{
			s[d] = d;
			for (int e = d * d; e < limit; e += d)
			{
				if (s[e] == 0)
				{
					s[e] = d;
				}
			}
		}
	}

	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto num = new int [] [limit];
		foreach (c; readln.splitter.map !(to !(int)))
		{
			int [int] fact;
			while (s[c] > 0)
			{
				fact[s[c]] += 1;
				c /= s[c];
			}
			foreach (prime, power; fact)
			{
				num[prime] ~= power;
			}
		}
		long res = 1;
		foreach (prime, ref list; num)
		{
			if (list.length >= n - 1)
			{
				sort !(q{a > b}) (list);
				res *= prime ^^ list[n - 2];
			}
		}
		writeln (res);
	}
}


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

immutable int limit = 15;

struct LongString
{
	string s;
	bool [] [limit] mark;

	this (string r)
	{
		s = r;
		foreach (len; 1..limit)
		{
			mark[len] = new bool [1 << len];
		}
		foreach (pos; 0..s.length)
		{
			foreach (len; 1..limit)
			{
				if (pos + len <= s.length)
				{
					auto t = s[pos..pos + len]
					    .to !(int) (2);
					mark[len][t] = true;
				}
			}
		}
		if (s.length > 2 * limit)
		{
			s = s[0..limit] ~ "*" ~ s[$ - limit..$];
		}
		debug {writeln (s, ": ", mark[].map !(sum));}
	}

	this (LongString a, LongString b)
	{
		s = a.s ~ b.s;
		foreach (len; 1..limit)
		{
			mark[len] = new bool [1 << len];
			mark[len][] |= a.mark[len][];
			mark[len][] |= b.mark[len][];
		}
		foreach (x; 1..a.s.length + 1)
		{
			foreach (y; 1..b.s.length + 1)
			{
				if (x + y < limit)
				{
					auto t = (a.s[$ - x..$] ~ b.s[0..y])
					    .to !(int) (2);
					mark[x + y][t] = true;
				}
			}
		}
		if (s.length > 2 * limit)
		{
			s = s[0..limit] ~ "*" ~ s[$ - limit..$];
		}
		debug {writeln (s, ": ", mark[].map !(sum));}
	}

	int fullness ()
	{
		foreach (len; 1..limit)
		{
			if (!all (mark[len]))
			{
				return len - 1;
			}
		}
		return limit - 1;
	}
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		LongString [] s;
		foreach (i; 0..n)
		{
			auto t = readln.strip;
			s ~= LongString (t);
		}

		int m;
		readf (" %s", &m);
		foreach (j; 0..m)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u -= 1;
			v -= 1;
			s ~= LongString (s[u], s[v]);
			writeln (s.back.fullness);
		}
	}
}

import std.algorithm, std.conv, std.stdio, std.string;
struct S {
	string s;
	bool [] [9] m;

	this (string r) {
		s = r;
		foreach (w; 1..9)
			m[w] = new bool [1 << w];
		foreach (w; 1..9)
			foreach (p; w..s.length + 1)
				m[w][s[p - w..p].to !(int) (2)] = 1;
	}

	this (S a, S b) {
		s = a.s ~ b.s;
		if (s.length > 2 * 9)
			s = s[0..9] ~ "*" ~ s[$ - 9..$];
		foreach (w; 1..9)
			m[w] = a.m[w].dup,
			m[w][] |= b.m[w][];
		foreach (x; 1..a.s.length + 1)
			foreach (y; 1..b.s.length + 1)
				if (x + y < 9)
					m[x + y][(a.s[$ - x..$] ~ b.s[0..y]).to !(int) (2)] = 1;
	}
}

void main () {
	S [] s;
	foreach (i; 0..readln.strip.to!int)
		s ~= S (readln.strip);
	foreach (j; 0..readln.strip.to!int) {
		int u, v;
		readf (" %s %s", &u, &v);
		s ~= S (s[u - 1], s[v - 1]);
		s[$ - 1].m[1..$].countUntil !(x => !x.all).writeln;
	}
}

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

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

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

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

enum K = 10;

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

      auto heads = new string[N + M];
      auto tails = new string[N + M];
      auto has = new bool[][][](N + M, K);
      foreach (i; 0 .. N + M) {
        foreach (k; 0 .. K) {
          has[i][k] = new bool[1 << k];
        }
      }
      foreach (i; 0 .. N) {
        const len = cast(int)(S[i].length);
        if (len > K) {
          heads[i] = S[i][0 .. K];
          tails[i] = S[i][$ - K .. $];
        } else {
          heads[i] = tails[i] = S[i];
        }
        foreach (k; 1 .. K) {
          foreach (x; 0 .. len - k + 1) {
            int p;
            foreach (l; 0 .. k) {
              p |= (S[i][x + l] - '0') << l;
            }
            has[i][k][p] = true;
          }
        }
      }

      foreach (j; 0 .. M) {
        const i = N + j;
        heads[i] = heads[A[j]] ~ heads[B[j]];
        if (heads[i].length > K) {
          heads[i] = heads[i][0 .. K];
        }
        tails[i] = tails[A[j]] ~ tails[B[j]];
        if (tails[i].length > K) {
          tails[i] = tails[i][$ - K .. $];
        }
        foreach (k; 1 .. K) {
          foreach (p; 0 .. 1 << k) {
            has[i][k][p] = has[A[j]][k][p] || has[B[j]][k][p];
          }
        }
        const lenA = cast(int)(tails[A[j]].length);
        const lenB = cast(int)(heads[B[j]].length);
        foreach (kA; 1 .. lenA + 1) foreach (kB; 1 .. lenB + 1) {
          if (kA + kB < K) {
            int p;
            foreach (l; 0 .. kA) {
              p |= (tails[A[j]][lenA - kA + l] - '0') << l;
            }
            foreach (l; 0 .. kB) {
              p |= (heads[B[j]][l] - '0') << (kA + l);
            }
            has[i][kA + kB][p] = true;
          }
        }
        int ans;
        foreach (k; 1 .. K) {
          if (has[i][k].all) {
            ans = k;
          }
        }
        writeln(ans);
      }

    }
  } catch (EOFException e) {
  }
}

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

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

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

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

enum K = 10;

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

      auto heads = new string[N + M];
      auto tails = new string[N + M];
      auto has = new bool[][][](N + M, K);
      foreach (i; 0 .. N + M) {
        foreach (k; 0 .. K) {
          has[i][k] = new bool[1 << k];
        }
      }
      foreach (i; 0 .. N) {
        const len = cast(int)(S[i].length);
        heads[i] = S[i][0 .. min(K - 1, len)];
        tails[i] = S[i][len - min(K - 1, len) .. len];
        foreach (k; 1 .. K) {
          foreach (x; 0 .. len - k + 1) {
            int p;
            foreach (l; 0 .. k) {
              p |= (S[i][x + l] - '0') << l;
            }
            debug {
              writeln(i, " ", k, " ", p);
            }
            has[i][k][p] = true;
          }
        }
      }

      foreach (j; 0 .. M) {
        const i = N + j;
        heads[i] = heads[A[j]] ~ heads[B[j]];
        if (heads[i].length > K - 1) {
          heads[i] = heads[i][0 .. K - 1];
        }
        tails[i] = tails[A[j]] ~ tails[B[j]];
        if (tails[i].length > K - 1) {
          tails[j] = tails[i][$ - (K - 1) .. $];
        }
        foreach (k; 1 .. K) {
          foreach (p; 0 .. 1 << k) {
            has[i][k][p] = has[A[j]][k][p] || has[B[j]][k][p];
          }
        }
        debug {
          writeln(tails[A[j]], " ", heads[B[j]]);
        }
        const lenA = cast(int)(tails[A[j]].length);
        const lenB = cast(int)(heads[B[j]].length);
        foreach (kA; 1 .. lenA + 1) foreach (kB; 1 .. lenB + 1) {
          if (kA + kB < K) {
            int p;
            foreach (l; 0 .. kA) {
              p |= (tails[A[j]][$ - 1 - l] - '0') << l;
            }
            foreach (l; 0 .. kB) {
              p |= (heads[B[j]][l] - '0') << (kA + l);
            }
            debug {
              writeln(i, " ", kA, " ", kB, " ", p);
            }
            has[i][kA + kB][p] = true;
          }
        }
        int ans;
        foreach (k; 1 .. K) {
          if (has[i][k].all) {
            ans = k;
          }
        }
        writeln(ans);
      }

    }
  } catch (EOFException e) {
  }
}


import std.stdio;

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

	int max, min;
	readf(" %s", &min);
	max = min;
	int counter, temp;
	for (int i = 1; i < n; i++) {
		readf(" %s", &temp);
		if (temp > max) {
			counter++;
			max = temp;
		}

		if (temp < min) {
			counter++;
			min = temp;
		}
	}

	writeln(counter);
}

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

void main() {
    int n = readln.chomp.to!int;
    auto p = readln.chomp.split.map!(to!int);

    int max = p[0];
    int min = p[0];
    int cnt = 0;
    for (int i = 1; i < n; i++) {
        if (max < p[i]) {
            cnt++;
            max = p[i];
        } else if (min > p[i]) {
            cnt++;
            min = p[i];
        }
    }

    cnt.writeln;
}

import std.stdio,
	std.array,
	std.conv;

void main() {
	readln;
	auto nums = to!(int[])(readln.split);
	int max_point = nums[0], min_point = nums[0];
	int count_amazing;
	foreach(m; nums[1..$]){
		if(m > max_point){
			max_point = m;
			count_amazing++;
		}
		else if (m < min_point){
			min_point = m;
			count_amazing++;
		}
	}
	writeln(count_amazing);
}

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

void main() {
    alias insertion_sort = bad_insertion_sort;
    int n;
    int[] data;
    readf("%s\n", &n);

    {
        int t; readf("%s ", &t);
        data ~= t;
    }

    insertion_sort(data);

    uint much_wow;

    for(int i; i < n - 1; ++i) {
        int t;
        readf("%s ", &t);
        if(t > data[$ - 1] || t < data[0]) {
            ++much_wow;
        }
        data ~= t;
        insertion_sort(data);

        //data.writeln();
    }

    much_wow.writeln();
}

void bad_insertion_sort(T, alias pred = (c, d) => c < d)(T[] data) @nogc {
    foreach (i, immutable value; data[1 .. $]) {
        auto j = i + 1;
        for ( ; j > 0 && pred(value, data[j - 1]); --j) {
            data[j] = data[j - 1];
        }
        data[j] = value;
    }
}

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

int main(string[] argv)
{
	int n, e;
	scanf("%d %d", &n, &e);
	int max = e;
	int min = e;
	int count = 0;
	foreach (int i; 1..n)
	{
		scanf("%d", &e);
		if (e > max)
		{
			count++;
			max = e;
		}
		else if (e < min)
		{
			count++;
			min = e;
		}
	}
	write(count);
	return 0;
}


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; }
T rtype(T)(){ return read.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 = rtype!real;
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
	int n = rint;
	string s = read;
	X[] xs;
	foreach(i; 0 .. n) xs ~= X(s[i] == '1', rint, rint);

	int ans = 0;
	foreach(t; 0 .. 200){
		int tmp = 0;
		foreach(i, x; xs){
			bool isNot;
			if(t < x.b) isNot = 0;
			else if((t - x.b) / x.a % 2 == 0) isNot = 1;
			else isNot = 0;

			if(isNot ^x.isOn) tmp += 1;
			if(t < 20) log("t:", t, "i:", i, "x:", x, "isNot:" , isNot, "tmp:", tmp);
		}
		ans = max(ans, tmp);
	}

	ans.writeln;
}
struct X{
	bool isOn;
	int a, b;
}

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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } 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; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

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

void main()
{
	auto n = RD!int;
	auto s = RD!string;
	auto a = new long[](n);
	auto b = new long[](n);
	foreach (i; 0..n)
	{
		a[i] = RD;
		b[i] = RD;
	}

	auto c = new int[](n);
	foreach (i; 0..n)
		c[i] = s[i] == '1' ? 1 : 0;

	long ans = c.sum;
	foreach (_; 0..10^^4)
	{
		b[] -= 1;
		foreach (i; 0..n)
		{
			if (b[i] == 0)
			{
				c[i] ^= 1;
				b[i]  = a[i];
			}
		}
		ans = max(ans, c.sum);
	}

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