You are given an array $$$a$$$ of $$$n$$$ positive integers. Let $$$\text{LIS}(a)$$$ denote the length of longest strictly increasing subsequence of $$$a$$$. For example,  $$$\text{LIS}([2, \underline{1}, 1, \underline{3}])$$$ = $$$2$$$.  $$$\text{LIS}([\underline{3}, \underline{5}, \underline{10}, \underline{20}])$$$ = $$$4$$$.  $$$\text{LIS}([3, \underline{1}, \underline{2}, \underline{4}])$$$ = $$$3$$$.  We define array $$$a'$$$ as the array obtained after reversing the array $$$a$$$ i.e. $$$a' = [a_n, a_{n-1}, \ldots , a_1]$$$.The beauty of array $$$a$$$ is defined as $$$min(\text{LIS}(a),\text{LIS}(a'))$$$.Your task is to determine the maximum possible beauty of the array $$$a$$$ if you can rearrange the array $$$a$$$ arbitrarily.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$  — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1 \leq n \leq 2\cdot 10^5)$$$  — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, \ldots ,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$  — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, output a single integer  — the maximum possible beauty of $$$a$$$ after rearranging its elements arbitrarily.

NoteIn the first test case, $$$a$$$ = $$$[6, 6, 6]$$$ and $$$a'$$$ = $$$[6, 6, 6]$$$. $$$\text{LIS}(a) = \text{LIS}(a')$$$ = $$$1$$$. Hence the beauty is $$$min(1, 1) = 1$$$.In the second test case, $$$a$$$ can be rearranged to $$$[2, 5, 4, 5, 4, 2]$$$. Then $$$a'$$$ = $$$[2, 4, 5, 4, 5, 2]$$$. $$$\text{LIS}(a) = \text{LIS}(a') = 3$$$. Hence the beauty is $$$3$$$ and it can be shown that this is the maximum possible beauty.In the third test case, $$$a$$$ can be rearranged to $$$[1, 2, 3, 2]$$$. Then $$$a'$$$ = $$$[2, 3, 2, 1]$$$. $$$\text{LIS}(a) = 3$$$, $$$\text{LIS}(a') = 2$$$. Hence the beauty is $$$min(3, 2) = 2$$$ and it can be shown that $$$2$$$ is the maximum possible beauty.

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

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

      auto gs = A.sort.group;
      auto ans = (gs.map!(g => min(2, g[1])).sum + 1) / 2;
      ans.writeln;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

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

      auto gs = A.sort.group;
      auto ans = gs.map!(g => min(2, g[1])).sum / 2;
      ans.writeln;

      // int ans;
      // {
      //   auto tree1 = A[0..1].redBlackTree!"a < b";
      //   auto tree2 = A[0..1].redBlackTree!"a < b";
      //   int t1s = 1;
      //   int t2s = 1;
      //   foreach(a; A) {
      //     if (t1s <= t2s) {
      //       if (tree1.back < a) {
      //         t1s++;
      //         tree1.insert(a);
      //       }
      //     } else {
      //       if (tree2.back < a) {
      //         t2s++;
      //         tree2.insert(a);
      //       }
      //     }
      //   }
      //   [tree1, tree2].deb;
      //   ans = max(ans, min(t1s, t2s));
      // }
      // {
      //   A = A.reverse.array;
      //   auto tree1 = A[0..1].redBlackTree!"a > b";
      //   auto tree2 = A[0..1].redBlackTree!"a > b";
      //   int t1s = 1;
      //   int t2s = 1;
      //   foreach(a; A) {
      //     if (t1s <= t2s) {
      //       if (tree1.back > a) {
      //         t1s++;
      //         tree1.insert(a);
      //       }
      //     } else {
      //       if (tree2.back > a) {
      //         t2s++;
      //         tree2.insert(a);
      //       }
      //     }
      //   }
      //   [tree1, tree2].deb;
      //   ans = max(ans, min(t1s, t2s));
      // }

      // ans.writeln;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

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

      auto gs = A.group;
      auto ans = gs.count!(g => g[1] >= 2) + (gs.count!(g => g[1] == 1)) / 2;
      ans.writeln;

      // int ans;
      // {
      //   auto tree1 = A[0..1].redBlackTree!"a < b";
      //   auto tree2 = A[0..1].redBlackTree!"a < b";
      //   int t1s = 1;
      //   int t2s = 1;
      //   foreach(a; A) {
      //     if (t1s <= t2s) {
      //       if (tree1.back < a) {
      //         t1s++;
      //         tree1.insert(a);
      //       }
      //     } else {
      //       if (tree2.back < a) {
      //         t2s++;
      //         tree2.insert(a);
      //       }
      //     }
      //   }
      //   [tree1, tree2].deb;
      //   ans = max(ans, min(t1s, t2s));
      // }
      // {
      //   A = A.reverse.array;
      //   auto tree1 = A[0..1].redBlackTree!"a > b";
      //   auto tree2 = A[0..1].redBlackTree!"a > b";
      //   int t1s = 1;
      //   int t2s = 1;
      //   foreach(a; A) {
      //     if (t1s <= t2s) {
      //       if (tree1.back > a) {
      //         t1s++;
      //         tree1.insert(a);
      //       }
      //     } else {
      //       if (tree2.back > a) {
      //         t2s++;
      //         tree2.insert(a);
      //       }
      //     }
      //   }
      //   [tree1, tree2].deb;
      //   ans = max(ans, min(t1s, t2s));
      // }

      // ans.writeln;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

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

      auto gs = A.group;
      auto ans = gs.count!(g => g[1] >= 2) + (gs.count!(g => g[1] == 1) + 1) / 2;
      ans.writeln;

      // int ans;
      // {
      //   auto tree1 = A[0..1].redBlackTree!"a < b";
      //   auto tree2 = A[0..1].redBlackTree!"a < b";
      //   int t1s = 1;
      //   int t2s = 1;
      //   foreach(a; A) {
      //     if (t1s <= t2s) {
      //       if (tree1.back < a) {
      //         t1s++;
      //         tree1.insert(a);
      //       }
      //     } else {
      //       if (tree2.back < a) {
      //         t2s++;
      //         tree2.insert(a);
      //       }
      //     }
      //   }
      //   [tree1, tree2].deb;
      //   ans = max(ans, min(t1s, t2s));
      // }
      // {
      //   A = A.reverse.array;
      //   auto tree1 = A[0..1].redBlackTree!"a > b";
      //   auto tree2 = A[0..1].redBlackTree!"a > b";
      //   int t1s = 1;
      //   int t2s = 1;
      //   foreach(a; A) {
      //     if (t1s <= t2s) {
      //       if (tree1.back > a) {
      //         t1s++;
      //         tree1.insert(a);
      //       }
      //     } else {
      //       if (tree2.back > a) {
      //         t2s++;
      //         tree2.insert(a);
      //       }
      //     }
      //   }
      //   [tree1, tree2].deb;
      //   ans = max(ans, min(t1s, t2s));
      // }

      // ans.writeln;
    }

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

  outputForAtCoder(&solve);
}

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

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

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

void main() { runSolver(); }

void problem() {
  auto QN = scan!int;

  auto solve() {
    auto subSolve() {
      auto N = scan!int;
      auto A = scan!int(N).sort.array;

      int ans;
      {
        auto tree1 = A[0..1].redBlackTree!"a < b";
        auto tree2 = A[0..1].redBlackTree!"a < b";
        int t1s = 1;
        int t2s = 1;
        foreach(a; A) {
          if (t1s <= t2s) {
            if (tree1.back < a) {
              t1s++;
              tree1.insert(a);
            }
          } else {
            if (tree2.back < a) {
              t2s++;
              tree2.insert(a);
            }
          }
        }
        // [tree1, tree2].deb;
        ans = max(ans, min(t1s, t2s));
      }
      {
        A = A.reverse.array;
        auto tree1 = A[0..1].redBlackTree!"a > b";
        auto tree2 = A[0..1].redBlackTree!"a > b";
        int t1s = 1;
        int t2s = 1;
        foreach(a; A) {
          if (t1s <= t2s) {
            if (tree1.back > a) {
              t1s++;
              tree1.insert(a);
            }
          } else {
            if (tree2.back > a) {
              t2s++;
              tree2.insert(a);
            }
          }
        }
        // [tree1, tree2].deb;
        ans = max(ans, min(t1s, t2s));
      }

      ans.writeln;
    }

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

  outputForAtCoder(&solve);
}

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

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

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


There are $$$n$$$ cities numbered from $$$1$$$ to $$$n$$$, and city $$$i$$$ has beauty $$$a_i$$$.A salesman wants to start at city $$$1$$$, visit every city exactly once, and return to city $$$1$$$.For all $$$i\ne j$$$, a flight from city $$$i$$$ to city $$$j$$$ costs $$$\max(c_i,a_j-a_i)$$$ dollars, where $$$c_i$$$ is the price floor enforced by city $$$i$$$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip.

The first line contains a single integer $$$n$$$ ($$$2\le n\le 10^5$$$) — the number of cities. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$, $$$c_i$$$ ($$$0\le a_i,c_i\le 10^9$$$) — the beauty and price floor of the $$$i$$$-th city.

Output a single integer — the minimum total cost.

NoteIn the first test case, we can travel in order $$$1\to 3\to 2\to 1$$$.   The flight $$$1\to 3$$$ costs $$$\max(c_1,a_3-a_1)=\max(9,4-1)=9$$$.  The flight $$$3\to 2$$$ costs $$$\max(c_3, a_2-a_3)=\max(1,2-4)=1$$$.  The flight $$$2\to 1$$$ costs $$$\max(c_2,a_1-a_2)=\max(1,1-2)=1$$$. The total cost is $$$11$$$, and we cannot do better.

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 INF = 10L^^9;
alias City = Tuple!(long, "pos", long, "val");

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto A = new City[N];
      foreach (u; 0 .. N) {
        A[u].pos = readLong();
        A[u].val = readLong();
      }
      A.sort;
      debug {
        writeln("A = ", A);
      }

      alias Entry = Tuple!(long, "c", int, "u");
      BinaryHeap!(Array!Entry, "a.c > b.c") que;
      auto dist = new long[N];
      dist[] = INF;
      dist[0] = 0;
      que.insert(Entry(0, 0));
      for (; !que.empty; ) {
        const c = que.front.c;
        const u = que.front.u;
        que.removeFront;
        if (dist[u] == c) {
          if (u - 1 >= 0) {
            if (chmin(dist[u - 1], c)) {
              que.insert(Entry(c, u - 1));
            }
          }
          const v = A.lowerBound(City(A[u].pos + A[u].val + 1, -1));
          debug {
            writefln("%s -> %s", u, v);
          }
          if (v - 1 >= 0) {
            if (chmin(dist[v - 1], c)) {
              que.insert(Entry(c, v - 1));
            }
          }
          if (v < N) {
            const cc = c + ((A[v].pos - A[u].pos) - A[u].val);
            if (chmin(dist[v], cc)) {
              que.insert(Entry(cc, v));
            }
          }
        }
      }

      long ans = dist[N - 1];
      foreach (u; 0 .. N) {
        ans += A[u].val;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store  — "PriceFixed". Here are some rules of that store:  The store has an infinite number of items of every product.  All products have the same price: $$$2$$$ rubles per item.  For every product $$$i$$$ there is a discount for experienced buyers: if you buy $$$b_i$$$ items of products (of any type, not necessarily type $$$i$$$), then for all future purchases of the $$$i$$$-th product there is a $$$50\%$$$ discount (so you can buy an item of the $$$i$$$-th product for $$$1$$$ ruble!). Lena needs to buy $$$n$$$ products: she must purchase at least $$$a_i$$$ items of the $$$i$$$-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$) — the number of products. Each of next $$$n$$$ lines contains a product description. Each description consists of two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \leq a_i \leq 10^{14}$$$, $$$1 \leq b_i \leq 10^{14}$$$) — the required number of the $$$i$$$-th product and how many products you need to buy to get the discount on the $$$i$$$-th product.  The sum of all $$$a_i$$$ does not exceed $$$10^{14}$$$.

Output the minimum sum that Lena needs to make all purchases.

NoteIn the first example, Lena can purchase the products in the following way:  one item of product $$$3$$$ for $$$2$$$ rubles,  one item of product $$$1$$$ for $$$2$$$ rubles,  one item of product $$$1$$$ for $$$2$$$ rubles,  one item of product $$$2$$$ for $$$1$$$ ruble (she can use the discount because $$$3$$$ items are already purchased),  one item of product $$$1$$$ for $$$1$$$ ruble (she can use the discount because $$$4$$$ items are already purchased). In total, she spends $$$8$$$ rubles. It can be proved that it is impossible to spend less.In the second example Lena can purchase the products in the following way:  one item of product $$$1$$$ for $$$2$$$ rubles,  two items of product $$$2$$$ for $$$2$$$ rubles for each,  one item of product $$$5$$$ for $$$2$$$ rubles,  one item of product $$$3$$$ for $$$1$$$ ruble,  two items of product $$$4$$$ for $$$1$$$ ruble for each,  one item of product $$$1$$$ for $$$1$$$ ruble. In total, she spends $$$12$$$ rubles.

import std.algorithm, std.range, std.stdio, std.numeric, std.array, std.exception,
       std.container, std.typecons, std.conv, std.random, std.bigint;

void read(S...)(ref S args) {
  auto input = readln.split;
  assert(input.length == args.length);
  foreach (i, ref arg; args) {
    arg = input[i].to!(S[i]);
  }
}

auto list(T)() { return readln.split.map!(e => e.to!T).array; }

void main() {
  int n;
  read(n);

  alias Item = Tuple!(long, "discount", long, "need");
  auto arr = new Item[n];

  foreach (i; 0 .. n) {
    read(arr[i][1], arr[i][0]);
  }

  sort(arr);

  long purchased = 0;
  long answer = 0;

  while (arr.length) {
    if (arr.front.discount <= purchased) {
      purchased += arr.front.need;
      answer += arr.front.need;
      arr.popFront();
    } else {
      auto buy = min(arr.front.discount - purchased, arr.back.need);
      answer += 2 * buy;
      purchased += buy;
      arr.back.need -= buy;
      if (arr.back.need == 0) arr.popBack();
    }
  }

  writeln(answer);
}

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

void main()
{
  long n;
  readf!" %d "(n);
  long[] a;
  long[] b;
  for (long i = 0; i < n; i++) {
    long ai, bi;
    readf!" %d %d "(ai, bi);
    if (ai > 0) {
      a ~= ai;
      b ~= bi;
    }
  }
  auto c = zip(a, b).sort!((x, y) => x[1] < y[1]).array;
//  writeln(c);

  size_t l = 0;
  size_t r = c.length - 1;

  long result = 0;
  long bought = 0;

  while (true) {
    auto x = c[l];
    auto y = c[r];

    if (bought >= x[1]) {
      result += x[0];
      bought += x[0];
      if (l == r)
        break;
      l++;
      continue;
    }

    long buy_from_last = x[1] - bought;
    if (y[0] > buy_from_last) {
      result += buy_from_last * 2;
      bought += buy_from_last;
      y[0] -= buy_from_last;
      c[r] = y;
      continue;
    }

    result += y[0] * 2;
    bought += y[0];
    if (l == r)
      break;
    r--;
  }
  writeln(result);
}


A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — $$$n$$$ and $$$k$$$, where $$$n$$$ is even. Next, he takes all the integers from $$$1$$$ to $$$n$$$, and splits them all into pairs $$$(a, b)$$$ (each integer must be in exactly one pair) so that for each pair the integer $$$(a + k) \cdot b$$$ is divisible by $$$4$$$ (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers $$$n$$$ and $$$k$$$.Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it.

The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The following is a description of the input data sets. The single line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$, $$$0 \leq k \leq 10^9$$$, $$$n$$$ is even) — the number of integers and the number being added $$$k$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, first output the string "YES" if there is a split into pairs, and "NO" if there is none. If there is a split, then in the following $$$\frac{n}{2}$$$ lines output pairs of the split, in each line print $$$2$$$ numbers — first the integer $$$a$$$, then the integer $$$b$$$.

NoteIn the first test case, splitting into pairs $$$(1, 2)$$$ and $$$(3, 4)$$$ is suitable, same as splitting into $$$(1, 4)$$$ and $$$(3, 2)$$$.In the second test case, $$$(1 + 0) \cdot 2 = 1 \cdot (2 + 0) = 2$$$ is not divisible by $$$4$$$, so there is no partition.

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    outer: foreach(_; 0..t)
    {
        long n, k;
        long[][] answer;
        scanf("%lld %lld\n", &n, &k);
        if ((k == 0) || (k % 4 == 0)) {
            writeln("NO");
            continue;
        }
        if (n == 2) {
            if (k % 2 != 0) {
                writeln("YES");
                writeln(1," ", 2);
            }
            else if (k % 4 == 0)
                writeln("NO");
            else if (k % 4 != 0) {
                writeln("YES");
                writeln(2, " ", 1);
            }
            else
                writeln("NO");
            continue;
        }
        foreach(i; iota(2,n+1,2)) {
            if ((i+k) % 4 == 0)
                answer ~= [i, i-1];
            else
                answer ~= [i-1, i];
        }
        writeln("YES");
        foreach(row; answer)
            writefln("%(%s %)", row);
    }
}

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    outer: foreach(_; 0..t)
    {
        long n, k;
        long[][] answer;
        scanf("%lld %lld\n", &n, &k);
        if (k == 0) {
            writeln("NO");
            continue;
        }
        if (n == 2) {
            if (k % 2 != 0) {
                writeln("YES");
                writeln(1," ", 2);
            }
            else if (k % 4 == 0)
                writeln("NO");
            else if (k % 4 != 0) {
                writeln("YES");
                writeln(2, " ", 1);
            }
            else
                writeln("NO");
            continue;
        }
        foreach(i; iota(2,n+1,2)) {
            if ((i+k) % 4 == 0)
                answer ~= [i, i-1];
            else
                answer ~= [i-1, i];
        }
        writeln("YES");
        foreach(row; answer)
            writefln("%(%s %)", row);
    }
}

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    outer: foreach(_; 0..t)
    {
        long n, k;
        long[][] answer;
        scanf("%lld %lld\n", &n, &k);
        if (k == 0) {
            writeln("NO");
            continue;
        }
        foreach(i; iota(2,n+1,2)) {
            if ((i+k) % 4 == 0)
                answer ~= [i, i-1];
            else
                answer ~= [i-1, i];
        }
        writeln("YES");
        foreach(row; answer)
            writefln("%(%s %)", row);
    }
}


Recently in Divanovo, a huge river locks system was built. There are now $$$n$$$ locks, the $$$i$$$-th of them has the volume of $$$v_i$$$ liters, so that it can contain any amount of water between $$$0$$$ and $$$v_i$$$ liters. Each lock has a pipe attached to it. When the pipe is open, $$$1$$$ liter of water enters the lock every second.The locks system is built in a way to immediately transfer all water exceeding the volume of the lock $$$i$$$ to the lock $$$i + 1$$$. If the lock $$$i + 1$$$ is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river.   The picture illustrates $$$5$$$ locks with two open pipes at locks $$$1$$$ and $$$3$$$. Because locks $$$1$$$, $$$3$$$, and $$$4$$$ are already filled, effectively the water goes to locks $$$2$$$ and $$$5$$$. Note that the volume of the $$$i$$$-th lock may be greater than the volume of the $$$i + 1$$$-th lock.To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in $$$q$$$ independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the $$$j$$$-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after $$$t_j$$$ seconds.Please help the mayor to solve this tricky problem and answer his queries.

The first lines contains one integer $$$n$$$ ($$$1 \le n \le 200\,000$$$) — the number of locks.  The second lines contains $$$n$$$ integers $$$v_1, v_2, \dots, v_n$$$ ($$$1 \le v_i \le 10^9$$$)) — volumes of the locks.  The third line contains one integer $$$q$$$ ($$$1 \le q \le 200\,000$$$) — the number of queries.  Each of the next $$$q$$$ lines contains one integer $$$t_j$$$ ($$$1 \le t_j \le 10^9$$$) — the number of seconds you have to fill all the locks in the query $$$j$$$.

Print $$$q$$$ integers. The $$$j$$$-th of them should be equal to the minimum number of pipes to turn on so that after $$$t_j$$$ seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print $$$-1$$$.

NoteThere are $$$6$$$ queries in the first example test. In the queries $$$1, 3, 4$$$ the answer is $$$-1$$$. We need to wait $$$4$$$ seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$. After $$$4$$$ seconds the locks $$$1$$$ and $$$4$$$ are full. In the following $$$1$$$ second $$$1$$$ liter of water is transferred to the locks $$$2$$$ and $$$5$$$. The lock $$$3$$$ is filled by its own pipe. Similarly, in the second query one can open pipes in locks $$$1$$$, $$$3$$$, and $$$4$$$.In the fifth query one can open pipes $$$1, 2, 3, 4$$$.

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

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

void main() {
    int N = read!int;
    auto V = readarray!long;
    int Q = read!int;
    auto T = Q.iota.map!(_ => read!int).array;

    auto S = new long[N + 1];
    for (int i = 0; i < N; i++) {
        S[i + 1] = S[i] + V[i];
    }
    auto D = new double[N + 1];
    D[] = 0;
    for (int i = 1; i <= N; i++) {
        D[i] = cast(double)(S[i]) / i;
    }
    auto X = new double[N + 1];
    X[] = 0;
    for (int i = 1; i <= N; i++) {
        X[i] = max(X[i-1], D[i]);
    }
    auto L = new double[N + 1];
    L[] = double.infinity;
    for (int k = 1; k <= N; k++) {
        L[k] = max(X[k], cast(double)(S[N]) / k);
    }
    foreach (t; T) {
        int lb = 0, ub = N;
        bool C(int x) {
            return L[x] <= t;
        }
        if (! C(ub)) {
            writeln(-1);
        } else {
            while (lb + 1 < ub) {
                int mid = (lb + ub) / 2;
                (C(mid) ? ub : lb) = mid;
            }
            writeln(ub);
        }
    }

}


Fishing Prince loves trees, and he especially loves trees with only one centroid. The tree is a connected graph without cycles.A vertex is a centroid of a tree only when you cut this vertex (remove it and remove all edges from this vertex), the size of the largest connected component of the remaining graph is the smallest possible.For example, the centroid of the following tree is $$$2$$$, because when you cut it, the size of the largest connected component of the remaining graph is $$$2$$$ and it can't be smaller.  However, in some trees, there might be more than one centroid, for example:  Both vertex $$$1$$$ and vertex $$$2$$$ are centroids because the size of the largest connected component is $$$3$$$ after cutting each of them.Now Fishing Prince has a tree. He should cut one edge of the tree (it means to remove the edge). After that, he should add one edge. The resulting graph after these two operations should be a tree. He can add the edge that he cut.He wants the centroid of the resulting tree to be unique. Help him and find any possible way to make the operations. It can be proved, that at least one such way always exists.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1\leq t\leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3\leq n\leq 10^5$$$) — the number of vertices. Each of the next $$$n-1$$$ lines contains two integers $$$x, y$$$ ($$$1\leq x,y\leq n$$$). It means, that there exists an edge connecting vertices $$$x$$$ and $$$y$$$. It's guaranteed that the given graph is a tree. It's guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$.

For each test case, print two lines. In the first line print two integers $$$x_1, y_1$$$ ($$$1 \leq x_1, y_1 \leq n$$$), which means you cut the edge between vertices $$$x_1$$$ and $$$y_1$$$. There should exist edge connecting vertices $$$x_1$$$ and $$$y_1$$$. In the second line print two integers $$$x_2, y_2$$$ ($$$1 \leq x_2, y_2 \leq n$$$), which means you add the edge between vertices $$$x_2$$$ and $$$y_2$$$. The graph after these two operations should be a tree. If there are multiple solutions you can print any.

NoteNote that you can add the same edge that you cut.In the first test case, after cutting and adding the same edge, the vertex $$$2$$$ is still the only centroid.In the second test case, the vertex $$$2$$$ becomes the only centroid after cutting the edge between vertices $$$1$$$ and $$$3$$$ and adding the edge between vertices $$$2$$$ and $$$3$$$.

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;
bool DEBUG = 0; void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void main(string[] args){ args ~= ["", ""]; string cmd = args[1]; if(cmd == "-debug") DEBUG = 1;
if(cmd == "-gen") gen; else if(cmd == "-jury") jury; else solve; }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ std.stdio.write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
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 lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void gen(){
}
void jury(){
}
void solve(){
    foreach(_; 0 .. scan!int){
        int n = scan!int;
        Node[] nodes;
        foreach(i; 0 .. n) nodes ~= new Node(i);
        foreach(__; 0 .. n - 1){
            int a = scan!int - 1, b = scan!int - 1;
            nodes[a].connectTo(nodes[b]);
        }
        foreach(ed; nodes[0].edgesIn) ed.calcIn;
        nodes[0].calc;
        foreach(ed; nodes[0].edgesOut) ed.calcOut;

        log("node values:", nodes.map!(nd => nd.value));
        //log("edge values:", edges.map!(ed => ed.node0.id.to!string ~ "->" ~ ed.node1.id.to!string ~ ":" ~ ed.value.to!string));

        long bestvalue = nodes[0].value;
        foreach(nd; nodes) bestvalue.lowerTo(nd.value);

        Node[] bestnodes;
        foreach(nd; nodes) if(nd.value == bestvalue) bestnodes ~= nd;

        if(bestnodes.length == 1){
            Edge ed = nodes[0].edgesIn[0];
            print(ed.node0.id + 1, ed.node1.id + 1);
            print(ed.node0.id + 1, ed.node1.id + 1);
        }
        else{
            Edge ed = bestnodes[0].edgesIn[0];
            if(ed.node0.id == bestnodes[1].id) ed = bestnodes[0].edgesIn[1];
            print(ed.node0.id + 1, bestnodes[0].id + 1);
            print(ed.node0.id + 1, bestnodes[1].id + 1);
        }

    }
}

// 双方向木DP

class Node{
    int id;
    long value;
    Edge[] edgesIn, edgesOut;
    this(int id){
        this.id = id;
    }
    void connectTo(Node nd){
        Edge edIn = new Edge(nd, this);
        Edge edOut = new Edge(this, nd);
        edIn.inv = edOut, edOut.inv = edIn;
        this.edgesIn ~= edIn, this.edgesOut ~= edOut;
        nd.edgesIn ~= edOut, nd.edgesOut ~= edIn;
    }
    void calc(){
        log("calc node", id);
        value = 0;
        long sum = 1;
        foreach(ed; edgesIn){
            sum += ed.value;
            value.raiseTo(ed.value);
        }
        foreach(ed; edgesOut){
            ed.value = sum - ed.inv.value;
        }
    }
}
class Edge{
    static int nextId = 0;
    int id;
    Node node0, node1;
    Edge inv;
    long value;
    this(Node node0, Node node1){
        this.id = nextId ++;
        this.node0 = node0;
        this.node1 = node1;
    }
    void calcIn(){
        log("calcin edge", id);
        foreach(ed; node0.edgesIn) if(ed.node0.id != this.node1.id){
            ed.calcIn;
        }
        calc;
    }
    void calcOut(){
        log("calcout edge", id);
        node1.calc;
        foreach(ed; node1.edgesOut) if(ed.node1.id != this.node0.id){
            ed.calcOut;
        }
    }
    void calc(){
        value = 1;
        foreach(ed; node0.edgesIn) if(ed.node0.id != this.node1.id){
            value += ed.value;
        }
    }

}

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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		auto cnt = new int[](n);
		int dfs(int pos, int last)
		{
			int m, tot = 1;
			foreach (v; edges[pos])
			{
				if (v == last) continue;

				auto r = dfs(v, pos);
				m.chmax(r);
				tot += r;
			}
			cnt[pos] = max(m, n-tot);
			return tot;
		}
		dfs(0, -1);

		auto index = cnt.MAKE_IDX;
		int i = cast(int)index[0];
		int j = cast(int)index[1];
		if (cnt[i] != cnt[j])
		{
			ans[ti] = [i, edges[i][0], i, edges[i][0]];
		}
		else
		{
			int dfs2(int pos, int last)
			{
				if (edges[pos].length == 1) return pos;

				foreach (v; edges[pos])
				{
					if (v == last) continue;
					return dfs2(v, pos);
				}
				return -1;
			}
			auto k = dfs2(j, i);
			ans[ti] = [k, edges[k][0], i, k];
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { 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 edges = new int[][](n);
		foreach (i; 0..n-1)
		{
			auto x = RD!int-1;
			auto y = RD!int-1;
			edges[x] ~= y;
			edges[y] ~= x;
		}

		auto cnt = new int[](n);
		int dfs(int pos, int last)
		{
			int m, tot = 1;
			foreach (v; edges[pos])
			{
				if (v == last) continue;

				auto r = dfs(v, pos);
				m.chmax(r);
				tot += m;
			}
			cnt[pos] = max(m, n-tot);
			return tot;
		}
		dfs(0, -1);

		auto index = cnt.MAKE_IDX;
		int i = cast(int)index[0];
		int j = cast(int)index[1];
		if (cnt[i] != cnt[j])
		{
			ans[ti] = [i, edges[i][0], i, edges[i][0]];
		}
		else
		{
			int dfs2(int pos, int last)
			{
				if (edges[pos].length == 1) return pos;

				foreach (v; edges[pos])
				{
					if (v == last) continue;
					return dfs2(v, pos);
				}
				return -1;
			}
			auto k = dfs2(j, i);
			ans[ti] = [k, edges[k][0], i, k];
		}
	}

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


This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can't make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead.To bake a Napoleon cake, one has to bake $$$n$$$ dry layers first, and then put them on each other in one stack, adding some cream. Arkady started with an empty plate, and performed the following steps $$$n$$$ times:   place a new cake layer on the top of the stack;  after the $$$i$$$-th layer is placed, pour $$$a_i$$$ units of cream on top of the stack. When $$$x$$$ units of cream are poured on the top of the stack, top $$$x$$$ layers of the cake get drenched in the cream. If there are less than $$$x$$$ layers, all layers get drenched and the rest of the cream is wasted. If $$$x = 0$$$, no layer gets drenched.  The picture represents the first test case of the example. Help Arkady determine which layers of the cake eventually get drenched when the process is over, and which don't.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 20\,000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of layers in the cake. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le n$$$) — the amount of cream poured on the cake after adding each layer. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print a single line with $$$n$$$ integers. The $$$i$$$-th of the integers should be equal to $$$1$$$ if the $$$i$$$-th layer from the bottom gets drenched, and $$$0$$$ otherwise.

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

		auto imos = new long[](n+1);
		foreach (i; 0..n)
		{
			if (a[i] == 0) continue;
			auto j = n-i-1;
			imos[j] += 1;
			imos[min(n, j+a[i])] -= 1;
		}
		foreach (i; 0..n)
		{
			imos[i+1] += imos[i];
		}
		ans[ti].length = n;
		foreach (i; 0..n)
			if (imos[n-i-1] >= 1)
				ans[ti][i] = 1;
	}

	foreach (e; ans)
		e.map!(to!string).join(" ").writeln;
	stdout.flush;
	debug readln;
}


You are given two strings $$$s$$$ and $$$t$$$. The string $$$s$$$ consists of lowercase Latin letters and at most one wildcard character '*', the string $$$t$$$ consists only of lowercase Latin letters. The length of the string $$$s$$$ equals $$$n$$$, the length of the string $$$t$$$ equals $$$m$$$.The wildcard character '*' in the string $$$s$$$ (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of $$$s$$$ can be replaced with anything. If it is possible to replace a wildcard character '*' in $$$s$$$ to obtain a string $$$t$$$, then the string $$$t$$$ matches the pattern $$$s$$$.For example, if $$$s=$$$"aba*aba" then the following strings match it "abaaba", "abacaba" and "abazzzaba", but the following strings do not match: "ababa", "abcaaba", "codeforces", "aba1aba", "aba?aba".If the given string $$$t$$$ matches the given string $$$s$$$, print "YES", otherwise print "NO".

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$ and the length of the string $$$t$$$, respectively. The second line contains string $$$s$$$ of length $$$n$$$, which consists of lowercase Latin letters and at most one wildcard character '*'. The third line contains string $$$t$$$ of length $$$m$$$, which consists only of lowercase Latin letters.

Print "YES" (without quotes), if you can obtain the string $$$t$$$ from the string $$$s$$$. Otherwise print "NO" (without quotes).

NoteIn the first example a wildcard character '*' can be replaced with a string "force". So the string $$$s$$$ after this replacement is "codeforces" and the answer is "YES".In the second example a wildcard character '*' can be replaced with an empty string. So the string $$$s$$$ after this replacement is "vkcup" and the answer is "YES".There is no wildcard character '*' in the third example and the strings "v" and "k" are different so the answer is "NO".In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string $$$t$$$ so the answer is "NO".

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto S = readln.chomp;
    auto T = readln.chomp;

    if (S.canFind('*')) {
        if (N > M + 1) {
            writeln("NO");
            return;
        }
        bool ok = true;
        for (int i = 0; S[i] != '*'; ++i) {
            if (S[i] != T[i]) {
                ok = false;
                break;
            }
        }
        for (int i = 0; S[N-i-1] != '*'; ++i) {
            if (S[N-i-1] != T[M-i-1]) {
                ok = false;
                break;
            }
        }
        writeln(ok ? "YES" : "NO");
    } else {
        writeln( S == T ? "YES" : "NO" );
    }

}

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

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

    auto s = readln.chomp;
    auto t = readln.chomp;

    if (!s.canFind('*')) {
        writeln(s == t ? "YES" : "NO");
        return;
    }

    if (n > m +1) {
        writeln("NO");
        return;
    }

    auto parts = findSplit(s, "*");

    debug { parts.writeln; t.writeln; }

    auto ok = t.startsWith(parts[0]) && t.endsWith(parts[2]);
    writeln(ok ? "YES" : "NO");
}

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

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		readln;
		auto s = readln.strip;
		auto t = readln.strip;
		while (!s.empty && !t.empty && s.front == t.front)
		{
			s.popFront ();
			t.popFront ();
		}
		while (!s.empty && !t.empty && s.back == t.back)
		{
			s.popBack ();
			t.popBack ();
		}
		writeln (((t == "" && s == "" ) || s == "*") ? "YES" : "NO");
	}
}

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

void main()
{
    readln;
    auto a = readln[0 .. $ - 1];
    auto b = readln[0 .. $ - 1];
    auto c = a.split('*');
    bool ok;
    if (c.length > 1) {
        ok = b.length >= a.length - 1
            && b[0 .. c[0].length] == c[0]
            && b[b.length - c[1].length .. $] == c[1];
    } else {
        ok = a == b;
    }
    if (ok)
        writeln("YES");
    else
        writeln("NO");
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.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();
      const M = readInt();
      const S = readToken();
      const T = readToken();

      bool ans;
      const pos = S.indexOf('*');
      if (pos == -1) {
        ans = (S == T);
      } else {
        ans = (N - 1 <= M && S[0 .. pos] == T[0 .. pos] && S[pos + 1 .. N] == T[M - (N - pos - 1) .. M]);
      }
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}


Let's define a function $$$f(x)$$$ ($$$x$$$ is a positive integer) as follows: write all digits of the decimal representation of $$$x$$$ backwards, then get rid of the leading zeroes. For example, $$$f(321) = 123$$$, $$$f(120) = 21$$$, $$$f(1000000) = 1$$$, $$$f(111) = 111$$$.Let's define another function $$$g(x) = \dfrac{x}{f(f(x))}$$$ ($$$x$$$ is a positive integer as well).Your task is the following: for the given positive integer $$$n$$$, calculate the number of different values of $$$g(x)$$$ among all numbers $$$x$$$ such that $$$1 \le x \le n$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 \le n < 10^{100}$$$). This integer is given without leading zeroes.

For each test case, print one integer — the number of different values of the function $$$g(x)$$$, if $$$x$$$ can be any integer from $$$[1, n]$$$.

NoteExplanations for the two first test cases of the example:  if $$$n = 4$$$, then for every integer $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$;  if $$$n = 37$$$, then for some integers $$$x$$$ such that $$$1 \le x \le n$$$, $$$\dfrac{x}{f(f(x))} = 1$$$ (for example, if $$$x = 23$$$, $$$f(f(x)) = 23$$$,$$$\dfrac{x}{f(f(x))} = 1$$$); and for other values of $$$x$$$, $$$\dfrac{x}{f(f(x))} = 10$$$ (for example, if $$$x = 30$$$, $$$f(f(x)) = 3$$$, $$$\dfrac{x}{f(f(x))} = 10$$$). So, there are two different values of $$$g(x)$$$.

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)
	{
		readln.strip.length.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(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 int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!string;
		ans[ti] = cast(int)n.length;
	}

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


Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of array ai either with ai + x or with ai - x. Please note that the operation may be applied more than once to the same position.Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than k operations to it. Please help him in that.

The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively. The second line contains n integers a1, a2, ..., an () — the elements of the array found by Maxim.

Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more than k operations to the array. In particular,  should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible. If there are multiple answers, print any of them.

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

int n, k, magic;
long[200_000] _a;
long[ ] a;

void main() {
    while (read(&n, &k, &magic)) {
        a = _a[0 .. n];
        bool neg = false;
        Array!int z;
        foreach (int i, ref x; a) {
            read(&x);
            if (!x)
                z ~= i;
            else if (x < 0)
                neg = !neg;
        }
        if (!z.empty) {
            if (k < z.length)
                goto end;
            k -= z.length;
            if (neg)
                foreach (i; z)
                    a[i] = magic;
            else {
                a[z[0]] = -magic;
                foreach (i; z[1 .. $])
                    a[i] = magic;
            }
        } else if (!neg) {
            auto r = a.minPos!`abs(a) < abs(b)`;
            assert(r[0]);
            if (r[0] > 0)
                while (r[0] >= 0 && k) {
                    r[0] -= magic;
                    k--;
                }
            else
                while (r[0] <= 0 && k) {
                    r[0] += magic;
                    k--;
                }
        }
        if (k) {
            Array!(Tuple!(long, int)) store;
            store.length = n;
            foreach (int i, x; a)
                store[i] = tuple(x, i);
            auto h = heapify!`abs(a[0]) > abs(b[0])`(store);
            do {
                long x;
                int pos;
                AliasSeq!(x, pos) = h.front;
                h.removeFront();
                if (x > 0)
                    x += magic;
                else
                    x -= magic;
                a[pos] = x;
                h.insert(tuple(x, pos));
            } while (--k);
        }
    end:
        foreach (x; a)
            write(x, ' ');
        writeln();
    }
}

import std.stdio;
import std.math;
import std.algorithm;
import std.range;
import std.random;
import std.regex;
import std.string;
import std.numeric;
import std.functional;
import std.bigint;
import std.bitmanip;
import std.conv;
import std.container;
alias redBlackTree rbt;
alias RedBlackTree Rbt;
alias BigInt big;
//writeln readf readln string wstring dstring delegate function static
//double float real foreach immutable assert unittest continue break
//class enum remove insert length struct return
struct chis
{
	long val;
	int pos;
};
long[200000] a,ans;
void main()
{
	alias fun=binaryFun!("a.val<b.val ||(a.val==b.val && a.pos<b.pos)");

	uint n,k,x;
	readf(" %d %d %d\n",&n,&k,&x);
	//writeln(n,' ',k,' ',x);
	foreach(i;0..n-1)readf(" %d ",&a[i]);
	readf(" %d\n",&a[n-1]);
	uint p=0,o=0;
	ulong m=10000000000;
	foreach(i;0..n)
	{
		if(a[i]<0)o++;
		if(abs(a[i])<m)
		{
			m=abs(a[i]);
			p=i;
		}
	}
	//writeln(a[p]);
	if(o%2==0)
	{
		if(a[p]<0)
		{
			while(k>0 && a[p]<=0){a[p]+=x;k--;}
		}
		else
		{
			while(k>0 && a[p]>=0){a[p]-=x;k--;}
		}
	}
	//Rbt!(chis,"abs(a.val)<abs(b.val) ||(abs(a.val)==abs(b.val) && a.pos<b.pos)",false) q;
	auto q=rbt!("abs(a.val)<abs(b.val) ||(abs(a.val)==abs(b.val) && a.pos<b.pos)",chis)(chis(0,-1));
	foreach(i;0..n)
	{
		q.insert(chis(a[i],i));
	}
	q.removeKey(chis(0,-1));
	//foreach(h;q)writeln(h.val);
	while(k>0)
	{
		auto f=q.front;
		q.removeFront;
		if(f.val<0) f.val-=x;
		else f.val+=x;
		q.insert(f);
		k--;
	}
	foreach(h;q)ans[h.pos]=h.val;
	foreach(i;0..n)write(ans[i],' ');
}

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

enum N = 200_000;

int n, k, x;
long[N] _a;
long[ ] a;

void main() {
    while (read(&n, &k, &x)) {
        a = _a[0 .. n];
        bool neg = false;
        Array!int z;
        foreach (int i, ref e; a) {
            read(&e);
            if (!e)
                z.insertBack(i);
            else if (e < 0)
                neg = !neg;
        }
        if (!z.empty) {
            if (k < z.length) {
                foreach (x; a)
                    write(x, ' ');
                writeln();
                continue;
            }
            k -= z.length;
            if ((z.length & 0x1) != neg)
                foreach (i; z)
                    a[i] = -x;
            else {
                a[z[0]] = -x;
                foreach (i; z[1 .. $])
                    a[i] = x;
            }
        } else if (!neg) {
            auto r = a.minPos!`abs(a) < abs(b)`;
            assert(r[0]);
            if (r[0] > 0)
                while (r[0] >= 0 && k) {
                    r[0] -= x;
                    k--;
                }
            else
                while (r[0] <= 0 && k) {
                    r[0] += x;
                    k--;
                }
        }
        if (k) {
            auto r = a.minPos!`abs(a) < abs(b)`;
            assert(r[0]);
            if (r[0] > 0)
                r[0] += cast(long)x * k;
            else
                r[0] -= cast(long)x * k;
        }
        foreach (e; a)
            write(e, ' ');
        writeln();
    }
}

import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

bool read(T...)(T ptrs) if (ptrs.length > 0) {
    return readf(' ' ~ replicate("%s ", ptrs.length), ptrs) == ptrs.length;
}

auto pairwise(R)(R range) if (isForwardRange!R) {
    return lockstep(range, dropOne(range));
}

enum N = 200_000;

int n, k, x;
long[N] _a;
long[ ] a;

void main() {
    while (read(&n, &k, &x)) {
        a = _a[0 .. n];
        bool neg = false;
        Array!int z;
        foreach (int i, ref e; a) {
            read(&e);
            if (!e)
                z.insertBack(i);
            else if (e < 0)
                neg = !neg;
        }
        if (!z.empty) {
            if (k < z.length) {
                foreach (x; a)
                    write(x, ' ');
                writeln();
                continue;
            }
            k -= z.length;
            if ((z.length & 0x1) != neg)
                foreach (i; z)
                    a[i] = -x;
            else {
                a[z[0]] = -x;
                foreach (i; z[1 .. $])
                    a[i] = x;
            }
        } else if (!neg) {
            auto r = a.minPos!`abs(a) < abs(b)`;
            assert(r[0]);
            if (r[0] > 0)
                while (r[0] >= 0 && k) {
                    r[0] -= x;
                    k--;
                }
            else
                while (r[0] <= 0 && k) {
                    r[0] += x;
                    k--;
                }
        }
        if (k) {
            Array!(Tuple!(long, int)) store;
            store.length = n;
            foreach (int i, e; a)
                store[i] = tuple(e, i);
            auto h = heapify!`abs(a[0]) > abs(b[0])`(store);
            do {
                long e;
                int pos;
                AliasSeq!(e, pos) = h.front;
                h.removeFront();
                if (e > 0)
                    e += x;
                else
                    e -= x;
                a[pos] = e;
                h.insert(tuple(e, pos));
            } while (--k);
        }
        foreach (e; a)
            write(e, ' ');
        writeln();
    }
}


Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence a0, a1, ..., ah, where h is the height of the tree, and ai equals to the number of vertices that are at distance of i edges from root. Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence ai, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree.Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal.The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex.

The first line contains a single integer h (2 ≤ h ≤ 105) — the height of the tree. The second line contains h + 1 integers — the sequence a0, a1, ..., ah (1 ≤ ai ≤ 2·105). The sum of all ai does not exceed 2·105. It is guaranteed that there is at least one tree matching this sequence.

If there is only one tree matching this sequence, print "perfect". Otherwise print "ambiguous" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print  integers, the k-th of them should be the parent of vertex k or be equal to zero, if the k-th vertex is the root. These treese should be non-isomorphic and should match the given sequence.

NoteThe only tree in the first example and the two printed trees from the second example are shown on the picture:

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;

import std.typecons;

int main() {
    Scanner sc = new Scanner(stdin);

    int h; int[] a;
    sc.read(h, a); h++;
    int[] as = new int[h+1];
    foreach (i; 0..h) {
        as[i+1] = as[i] + a[i];
    }
    int n = as.back;
    int[] ap = new int[n], bp = new int[n];
    ap[0] = -1; bp[0] = -1;
    foreach (i; 1..h) {
        foreach (j; as[i]..as[i+1]) {
            ap[j] = bp[j] = as[i-1];
        }
    }
    foreach (i; 0..h-1) {
        if (a[i] > 1 && a[i+1] > 1) {
            bp[as[i+1]+1]++;

            writeln("ambiguous");
            writeln(ap.map!"a+1".map!(to!string).join(" "));
            writeln(bp.map!"a+1".map!(to!string).join(" "));
            return 0;
        }
    }
    writeln("perfect");
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */

// module dcomp.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

// import dcomp.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}

/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */
// module dcomp.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/

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 H = readInt();
      auto A = new int[H + 1];
      foreach (d; 0 .. H + 1) {
        A[d] = readInt();
      }

      bool amb;
      foreach (d; 0 .. H) {
        amb = amb || (A[d] >= 2 && A[d + 1] >= 2);
      }
      if (!amb) {
        writeln("perfect");
      } else {
        auto ASum = new int[H + 2];
        foreach (d; 0 .. H + 1) {
          ASum[d + 1] = ASum[d] + A[d];
        }
        auto ans = new int[][2];
        ans[0] ~= -1;
        ans[1] ~= -1;
        foreach (d; 1 .. H + 1) {
          foreach (j; 0 .. A[d]) {
            ans[0] ~= ASum[d - 1];
            ans[1] ~= ASum[d - 1] + j % A[d - 1];
          }
        }
        writeln("ambiguous");
        foreach (s; 0 .. 2) {
          foreach (u; 0 .. ASum[H + 1]) {
            if (u > 0) write(" ");
            write(ans[s][u] + 1);
          }
          writeln();
        }
      }
    }
  } 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)); }

void main() {
  try {
    for (; ; ) {
      const H = readInt();
      auto A = new int[H + 1];
      foreach (d; 0 .. H + 1) {
        A[d] = readInt();
      }

      bool perfect = true;
      foreach (d; 0 .. H) {
        perfect = perfect && (A[d] == 1);
      }
      if (perfect) {
        writeln("perfect");
      } else {
        auto ASum = new int[H + 2];
        foreach (d; 0 .. H + 1) {
          ASum[d + 1] = ASum[d] + A[d];
        }
        auto ans = new int[][2];
        ans[0] ~= -1;
        ans[1] ~= -1;
        foreach (d; 1 .. H + 1) {
          foreach (_; 0 .. A[d]) {
            ans[0] ~= ASum[d - 1];
            ans[1] ~= ASum[d - 1] + A[d - 1] - 1;
          }
        }
        writeln("ambiguous");
        foreach (s; 0 .. 2) {
          foreach (u; 0 .. ASum[H + 1]) {
            if (u > 0) write(" ");
            write(ans[s][u] + 1);
          }
          writeln();
        }
      }
    }
  } catch (EOFException e) {
  }
}


Kiwon's favorite video game is now holding a new year event to motivate the users! The game is about building and defending a castle, which led Kiwon to think about the following puzzle.In a 2-dimension plane, you have a set $$$s = \{(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)\}$$$ consisting of $$$n$$$ distinct points. In the set $$$s$$$, no three distinct points lie on a single line. For a point $$$p \in s$$$, we can protect this point by building a castle. A castle is a simple quadrilateral (polygon with $$$4$$$ vertices) that strictly encloses the point $$$p$$$ (i.e. the point $$$p$$$ is strictly inside a quadrilateral). Kiwon is interested in the number of $$$4$$$-point subsets of $$$s$$$ that can be used to build a castle protecting $$$p$$$. Note that, if a single subset can be connected in more than one way to enclose a point, it is counted only once. Let $$$f(p)$$$ be the number of $$$4$$$-point subsets that can enclose the point $$$p$$$. Please compute the sum of $$$f(p)$$$ for all points $$$p \in s$$$.

The first line contains a single integer $$$n$$$ ($$$5 \le n \le 2\,500$$$). In the next $$$n$$$ lines, two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 \le x_i, y_i \le 10^9$$$) denoting the position of points are given. It is guaranteed that all points are distinct, and there are no three collinear points.

Print the sum of $$$f(p)$$$ for all points $$$p \in s$$$.

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

struct Point
{
	int x;
	int y;

	Point opBinary (string op) (const auto ref Point that)
	    if (op == "+" || op == "-")
	{
		Point res = void;
		res.x = mixin (q{this.x } ~ op ~ q{ that.x});
		res.y = mixin (q{this.y } ~ op ~ q{ that.y});
		return res;
	}

	int opCmp () (const auto ref Point that)
	{
		auto thisUp = this.y > 0 || this.y == 0 && this.x > 0;
		auto thatUp = that.y > 0 || that.y == 0 && that.x > 0;
		if (thisUp != thatUp)
		{
			return thisUp - thatUp;
		}
		auto cur = vp (this, that);
		return (cur > 0) - (cur < 0);
	}
}

long vp () (const auto ref Point a, const auto ref Point b)
{
	return a.x * 1L * b.y - a.y * 1L * b.x;
}

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf !(" %s %s") (c.x, c.y);
		}

		long res = n * (n - 1L) * (n - 2L) * (n - 3L) * (n - 4L) / 24;
		debug {writeln (res);}
		foreach (i; 0..n)
		{
			Point [] q;
			q.reserve (n - 1);
			foreach (j; 0..n)
			{
				if (j != i)
				{
					q ~= p[j] - p[i];
				}
			}
			sort (q);
			debug {writeln (q);}
			q ~= q;
			int v = 0;
			foreach (u; 0..n - 1)
			{
				v = max (v, u + 1);
				while (v - u < n - 1 && vp (q[v], q[u]) > 0)
				{
					v += 1;
				}
				debug {writeln (u, " ", v);}
				int w = v - u - 1;
				res -= w * (w - 1L) * (w - 2L) / 6;
			}
		}
		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)); }

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto P = new Pt[N];
      foreach (i; 0 .. N) {
        P[i].x = readLong();
        P[i].y = readLong();
      }

      long ans;
      foreach (i; 0 .. N) {
        Pt[] ps;
        foreach (j; 0 .. N) {
          if (j != i) {
            ps ~= Pt(P[j].x - P[i].x, P[j].y - P[i].y);
          }
        }
        ps.sort!((ref Pt a, ref Pt b) {
          const sa = (a.y > 0) ? 1 : (a.y < 0) ? 3 : (a.x > 0) ? 0 : 2;
          const sb = (b.y > 0) ? 1 : (b.y < 0) ? 3 : (b.x > 0) ? 0 : 2;
          if (sa != sb) return (sa < sb);
          return (a.x * b.y > a.y * b.x);
        });
        int r;
        foreach (l; 0 .. N - 1) {
          for (chmax(r, l + 1); r < l + (N - 1) && ps[l].x * ps[r % (N - 1)].y > ps[l].y * ps[r % (N - 1)].x; ++r) {}
          const long m = r - l - 1;
          ans += m * (m - 1) * (m - 2) / 6;
        }
      }

      const long n = N;
      ans = n * (n - 1) * (n - 2) * (n - 3) * (n - 4) / 120 * 5 - ans;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}