import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto q = next!int;
  foreach(qi; q.iota)
    {
      auto n = next!int;
      auto badCnt = new int[](n);
      auto goodCnt = new int[](n);
      auto cnt = new int[](n);
      foreach(i; n.iota)
	{
	  auto ai = next!int - 1;
	  auto fi = next!int;
	  if (fi)
	    goodCnt[ai]++;
	  else
	    badCnt[ai]++;
	  cnt[ai]++;
	}
      auto orderedTypes = iota(n).array.sort!((i, j) => cnt[i] > cnt[j]);
      int[] addSeq;
      auto prev = int.max;
      foreach(type; orderedTypes)
	{
	  prev = min(prev - 1, cnt[type]);
	  if (prev <= 0) break;
	  addSeq ~= prev;
	}
      debug writeln("addSeq = ", addSeq);
      auto avTypes = redBlackTree!((a, b) => a > b, true, Tuple!(int, int))();
      int noBadChosen = 0;
      foreach(add; addSeq)
	{
	  while(!orderedTypes.empty &&
		cnt[orderedTypes.front] >= add)
	    {
	      avTypes.insert(tuple(goodCnt[orderedTypes.front], badCnt[orderedTypes.front]));
	      orderedTypes = orderedTypes[1 .. $];
	    }
	  debug writeln("Trying to add ", add,
			" with avTypes = ", avTypes[]);
	  auto best = avTypes.front;
	  avTypes.removeFront;
	  add -= min(add, best[0]);
	  noBadChosen += min(add, best[1]);
	  add -= min(add, best[1]);
	  assert(add == 0);
	}
      auto res = addSeq.sum;
      writeln(res, " ", res - noBadChosen);
    }
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

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

  int q;
  rd(q);

  struct P {
    int f, c;
  }

  while (q--) {
    int n;
    rd(n);
    auto freq = new int[](n), cnt1s = new int[](n);
    foreach (i; 0 .. n) {
      int a, t;
      rd(a, t);
      freq[a - 1] += 1;
      if (t == 1) {
        cnt1s[a - 1] += 1;
      }
    }
    auto list = new int[][](n + 1);
    foreach (i; 0 .. n) {
      if (freq[i] > 0) {
        list[freq[i]] ~= cnt1s[i];
      }
    }
    int[] b;
    foreach (i; 0 .. n) {
      if (freq[i] > 0) {
        b ~= freq[i];
      }
    }
    b.sort!"a > b";
    int last = b[0] + 1;
    long ans = 0, sum1s = 0;
    auto rbt = new RedBlackTree!(int, "a>b", true);
    foreach (el; b) {
      auto cur = min(last - 1, el);
      ans += cur;
      foreach (cnt; list[cur]) {
        rbt.insert(cnt);
      }
      auto mx1 = rbt.front;
      sum1s += min(mx1, cur);
      rbt.removeKey(mx1);
      last = cur;
      if (last == 0) {
        break;
      }
    }
    writeln(ans, " ", sum1s);
  }

}

void rd(T...)(ref T x) {
  import std.stdio : readln;
  import std.string : split;
  import std.conv : to;

  auto l = readln.split;
  assert(l.length == x.length);
  foreach (i, ref e; x)
    e = l[i].to!(typeof(e));
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.variant;
import std.concurrency;

void main(string[] args)
{
  auto q = next!int;
  foreach(qi; q.iota)
    {
      auto n = next!int;
      auto badCnt = new int[](n);
      auto goodCnt = new int[](n);
      auto cnt = new int[](n);
      foreach(i; n.iota)
	{
	  auto ai = next!int - 1;
	  auto fi = next!int;
	  if (fi)
	    goodCnt[ai]++;
	  else
	    badCnt[ai]++;
	  cnt[ai]++;
	}
      auto orderedTypes = iota(n).array.sort!((i, j) => cnt[i] > cnt[j]);
      int[] addSeq;
      auto prev = int.max;
      foreach(type; orderedTypes)
	{
	  prev = min(prev - 1, cnt[type]);
	  if (prev <= 0) break;
	  addSeq ~= prev;
	}
      debug writeln("addSeq = ", addSeq);
      auto avTypes = redBlackTree!((a, b) => a > b, true, Tuple!(int, int))();
      int noBadChosen = 0;
      foreach(add; addSeq)
	{
	  while(!orderedTypes.empty &&
		cnt[orderedTypes.front] >= add)
	    {
	      avTypes.insert(tuple(goodCnt[orderedTypes.front], badCnt[orderedTypes.front]));
	      orderedTypes = orderedTypes[1 .. $];
	    }
	  debug writeln("Trying to add ", add,
			" with avTypes = ", avTypes[]);
	  auto best = avTypes.front;
	  avTypes.removeFront;
	  add -= min(add, best[0]);
	  noBadChosen += max(add, best[1]);
	  add -= min(add, best[1]);
	  assert(add == 0);
	}
      auto res = addSeq.sum;
      writeln(res, " ", res - noBadChosen);
    }
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}


import std.stdio, std.getopt, std.random, std.range, std.string, std.conv;
import std.algorithm, std.container;

const ML = 700;
double[][] dp;
bool[][] used;
int k;
double solve(int l, int n) {
    if (n < 0) return 0;
    if (l >= ML) return 0;
    if (used[l][n]) return dp[l][n];
    used[l][n] = true;
    double res = 0;
    double hp = 1.0/(l+1);
    res += hp*(l + solve(l+1, n-1));
    res += (1-hp)*((l+1.0)/2 + solve(l, n-1));
    res /= k;
    res += (1-1.0/k)*solve(l, n-1);
    return (dp[l][n] = res);
}

int main() {
    int n;
    readf("%d %d\n", &n, &k);
    auto dp = new double[ML+1];
    dp[] = 0;
    foreach (i; 1..n+1) {
        foreach (j; 1..ML) {
            double res = 0;
            double hp = 1.0/(j+1);
            res += hp*(j + dp[j+1]);
            res += (1-hp)*((j+1.0)/2 + dp[j]);
            res /= k;
            res += (1-1.0/k)*dp[j];
            dp[j] = res;
        }
    }
    writef("%.20f\n", dp[1]*k);
	return 0;
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

immutable LIM = 1000;

int N, K;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		K = readInt;

		real[] pa = new real[LIM];
		real[] pb = new real[LIM];
		real[] pc = new real[LIM];
		foreach (j; 1 .. LIM - 1) {
			pa[j] = 1.0 - 1.0 / K / (j + 1);
			pb[j] = 1.0 / K - 1.0 / K / (j + 1);
			pc[j] = 1.0 / K / (j + 1);
		}

		real[] dp = new real[LIM];
		dp[] = 0.0;
		foreach (i; 0 .. N) {
			foreach (j; 1 .. LIM - 1) {
				// dp[j] = (1.0 - 1.0 / K) * dp[j] + (1.0 / K - 1.0 / K / (j + 1)) * ((j + 1) / 2.0 + dp[j]) + (1.0 / K / (j + 1)) * (j + dp[j + 1]);
				dp[j] = pa[j] * dp[j] + pb[j] * ((j + 1) / 2.0) + pc[j] * (j + dp[j + 1]);
			}
		}
debug{
writeln(dp);
}
		const ans = dp[1] * K;
		writefln("%.10f", ans);
	}
	} catch (EOFException) {}
}


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

void main ()
{
	int a, b;
main_loop:
	while (readf (" %s %s", &a, &b) > 0)
	{
		foreach (p; 1..a)
		{
			int q = a * a - p * p;
			if (q <= 0)
			{
				continue;
			}
			q = to !(int) (sqrt (to !(double) (q)));
			if (p * p + q * q != a * a)
			{
				continue;
			}
			int u = p * b;
			int v = q * b;
			if (u % a != 0 || v % a != 0 || p * a == v)
			{
				continue;
			}
			u /= a;
			v /= a;
			writeln ("YES");
			writeln (0, ' ', 0);
			writeln (p, ' ', q);
			writeln (v, ' ', -u);
			continue main_loop;
		}
		writeln ("NO");
	}
}

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

struct Point
{
	int x;
	int y;
	this(int _x, int _y)
	{
		this.x = _x;
		this.y = _y;
	}
};

auto search(int len, int bound)
{
	Point[] ret;
	foreach (int i; 1 .. bound)
	{
		foreach (int j; i .. bound)
		{
			double d = i * i + j * j;
			double s = sqrt(d);
			if (s == len)
			{
				ret ~= Point(i, j);
				break;
			}
			else if (s > len)
			{
				break;
			}
		}
	}
	if (ret.length == 0)
	{
		ret ~= Point(1 << 30, 1 << 30);
	}
	return ret;
}

int cross(ref Point p0, ref Point p1)
{
	return (p0.x * p1.x + p0.y * p1.y);
}

void change(ref Point p)
{
	p.x ^= p.y;
	p.y ^= p.x;
	p.x ^= p.y;
}

void output(ref Point pa, ref Point pb)
{
	printf("YES\n");
	printf("0 0\n");
	printf("%d %d\n", pa.x, pa.y);
	printf("%d %d\n", pb.x, pb.y);
}

bool check(Point[] arr)
{
	if (arr.length == 0 || arr[0].x == 1 << 30)
	{
		return false;
	}
	return true;
}

void print(Point[] arr)
{
	printf("%d\n", arr);
	foreach (ref Point p; arr)
	{
		printf("%d %d\n", p.x, p.y);
	}
}

bool judge(ref Point p0, Point[] arr)
{
	foreach (ref Point p1; arr)
	{
		//printf("try (%d %d) (%d %d)\n", p0.x, p0.y, p1.x, p1.y);
		if (p0.x * p1.x == p0.y * p1.y)
		{
			if (-p0.x != p1.x && p0.y != p1.y)
			{
				p0.x *= -1;
				output(p0, p1);
				return true;
			}
			if (-p0.y != p1.y && p0.x != p1.x)
			{
				p0.y *= -1;
				output(p0, p1);
				return true;
			}
		}
	}
	return false;
}

void solve(int a, int b)
{
	auto bound = a > b ? a : b;
	auto pa = search(a, bound);
	if (check(pa) == false)
	{
		printf("NO\n");
		return;
	}
	auto pb = search(b, bound);
	if (check(pb) == false)
	{
		printf("NO\n");
		return;
	}
	//print(pa);
	//print(pb);
	foreach (ref Point p0; pa)
	{
		if (judge(p0, pb) == true)
		{
			return;
		}
		change(p0);
		if (judge(p0, pb) == true)
		{
			return;
		}
	}
	printf("NO\n");
}
/*
void test()
{
	int[] cnt = new int[1001];
	foreach (int i; 1 .. 1001)
	{
		foreach (int j; i .. 1001)
		{
			double d = i * i + j * j;
			double s = sqrt(d);
			if (s > 1000)
			{
				break;
			}
			int t = to!int(s);
			if (t == s)
			{
				++ cnt[t];
			}
		}
	}
	foreach (int i; 1 .. 1001)
	{
		if (cnt[i] > 1)
		{
			printf("%d %d\n", i, cnt[i]);
		}
	}
}
*/
void main(string[] args)
{
	//test();
	int a, b;
	while (scanf("%d%d", &a, &b) == 2)
	{
		solve(a, b);
	}
}

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

struct Point
{
    int x;
    int y;
    this(int _x, int _y)
    {
        this.x = _x;
        this.y = _y;
    }
};

auto search(int len, int bound)
{
    Point[] ret;
    foreach (int i; 1 .. bound)
    {
        foreach (int j; i .. bound)
        {
            double d = i * i + j * j;
            double s = sqrt(d);
            if (s == len)
            {
                ret ~= Point(i, j);
                break;
            }
            else if (s > len)
            {
                break;
            }
        }
    }
    if (ret.length == 0)
    {
        ret ~= Point(1 << 30, 1 << 30);
    }
    return ret;
}

int cross(ref Point p0, ref Point p1)
{
    return (p0.x * p1.x + p0.y * p1.y);
}

void change(ref Point p)
{
    p.x ^= p.y;
    p.y ^= p.x;
    p.x ^= p.y;
}

void output(ref Point pa, ref Point pb)
{
    printf("YES\n");
    printf("0 0\n");
    printf("%d %d\n", pa.x, pa.y);
    printf("%d %d\n", pb.x, pb.y);
}

bool check(Point[] arr)
{
    if (arr.length == 0 || arr[0].x == 1 << 30)
    {
        return false;
    }
    return true;
}

void solve(int a, int b)
{
    auto bound = a > b ? a : b;
    auto pa = search(a, bound);
    if (check(pa) == false)
    {
        printf("NO\n");
        return;
    }
    auto pb = search(b, bound);
    if (check(pb) == false)
    {
        printf("NO\n");
        return;
    }
    foreach (ref Point p0; pa)
    {
        p0.x = -p0.x;
        foreach (ref Point p1; pb)
        {
            if (cross(p0, p1) == 0)
            {
                output(p0, p1);
                return;
            }
        }
        change(p0);
        foreach (ref Point p1; pb)
        {
            if (cross(p0, p1) == 0)
            {
                output(p0, p1);
                return;
            }
        }
    }
    printf("NO\n");
}

void main(string[] args)
{
    int a, b;
    while (scanf("%d%d", &a, &b) == 2)
    {
        solve(a, b);
    }
}

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

struct Point
{
	int x;
	int y;
	this(int _x, int _y)
	{
		this.x = _x;
		this.y = _y;
	}
};

auto search(int len, int bound)
{
	Point[] ret;
	foreach (int i; 1 .. bound)
	{
		foreach (int j; i .. bound)
		{
			double d = i * i + j * j;
			double s = sqrt(d);
			if (s == len)
			{
				ret ~= Point(i, j);
				break;
			}
			else if (s > len)
			{
				break;
			}
		}
	}
	if (ret.length == 0)
	{
		ret ~= Point(1 << 30, 1 << 30);
	}
	return ret;
}

int cross(ref Point p0, ref Point p1)
{
	return (p0.x * p1.x + p0.y * p1.y);
}

void change(ref Point p)
{
	p.x ^= p.y;
	p.y ^= p.x;
	p.x ^= p.y;
}

void output(ref Point pa, ref Point pb)
{
	printf("YES\n");
	printf("0 0\n");
	printf("%d %d\n", pa.x, pa.y);
	printf("%d %d\n", pb.x, pb.y);
}

bool check(Point[] arr)
{
	if (arr.length == 0 || arr[0].x == 1 << 30)
	{
		return false;
	}
	return true;
}

void print(Point[] arr)
{
	printf("%d\n", arr);
	foreach (ref Point p; arr)
	{
		printf("%d %d\n", p.x, p.y);
	}
}

void solve(int a, int b)
{
	auto bound = a > b ? a : b;
	auto pa = search(a, bound);
	if (check(pa) == false)
	{
		printf("NO\n");
		return;
	}
	auto pb = search(b, bound);
	if (check(pb) == false)
	{
		printf("NO\n");
		return;
	}
	//print(pa);
	//print(pb);
	foreach (ref Point p0; pa)
	{
		p0.x = -p0.x;
		foreach (ref Point p1; pb)
		{
			if (p0.x != p1.x && p0.y != p1.y && cross(p0, p1) == 0)
			{
				output(p0, p1);
				return;
			}
		}
		change(p0);
		foreach (ref Point p1; pb)
		{
			if (p0.x != p1.x && p0.y != p1.y && cross(p0, p1) == 0)
			{
				output(p0, p1);
				return;
			}
		}
	}
	printf("NO\n");
}
/*
void test()
{
	int[] cnt = new int[1001];
	foreach (int i; 1 .. 1001)
	{
		foreach (int j; i .. 1001)
		{
			double d = i * i + j * j;
			double s = sqrt(d);
			if (s > 1000)
			{
				break;
			}
			int t = to!int(s);
			if (t == s)
			{
				++ cnt[t];
			}
		}
	}
	foreach (int i; 1 .. 1001)
	{
		if (cnt[i] > 1)
		{
			printf("%d %d\n", i, cnt[i]);
		}
	}
}
*/
void main(string[] args)
{
	//test();
	int a, b;
	while (scanf("%d%d", &a, &b) == 2)
	{
		solve(a, b);
	}
}

import std.stdio, std.string, std.math;

struct Point
{
    int x;
    int y;
    this(int _x, int _y)
    {
        this.x = _x;
        this.y = _y;
    }
};

auto search(int len, int bound)
{
    foreach (int i; 1 .. bound)
    {
        foreach (int j; i .. bound)
        {
            double d = i * i + j * j;
            double s = sqrt(d);
            if (s == len)
            {
                return Point(i, j);
            }
            else if (s > len)
            {
                break;
            }
        }
    }
    return Point(1 << 30, 1 << 30);
}

int cross(ref Point p0, ref Point p1)
{
    return (p0.x * p1.x + p0.y * p1.y);
}

void change(ref Point p)
{
    p.x ^= p.y;
    p.y ^= p.x;
    p.x ^= p.y;
}

void output(ref Point pa, ref Point pb)
{
    printf("YES\n");
    printf("0 0\n");
    printf("%d %d\n", pa.x, pa.y);
    printf("%d %d\n", pb.x, pb.y);
}

void solve(int a, int b)
{
    auto bound = a > b ? a : b;
    auto pa = search(a, bound);
    if (pa.x == 1 << 30)
    {
        printf("NO\n");
        return;
    }
    auto pb = search(b, bound);
    if (pb.x == 1 << 30)
    {
        printf("NO\n");
        return;
    }
    pb.x = -pb.x;
    if (cross(pa, pb) == 0)
    {
        output(pa, pb);
        return;
    }
    change(pb);
    if (cross(pa, pb) == 0)
    {
        output(pa, pb);
        return;
    }
    printf("NO\n");
}

void main(string[] args)
{
    int a, b;
    while (scanf("%d%d", &a, &b) == 2)
    {
        solve(a, b);
    }
}

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

void main ()
{
	int a, b;
main_loop:
	while (readf (" %s %s", &a, &b) > 0)
	{
		foreach (p; 1..a)
		{
			int q = a * a - p * p;
			if (q <= 0)
			{
				continue;
			}
			q = to !(int) (sqrt (to !(double) (q)));
			if (p * p + q * q != a * a)
			{
				continue;
			}
			int u = p * b;
			int v = q * b;
			if (u % a != 0 || v % a != 0)
			{
				continue;
			}
			u /= a;
			v /= a;
			writeln ("YES");
			writeln (0, ' ', 0);
			writeln (p, ' ', q);
			writeln (v, ' ', -u);
			continue main_loop;
		}
		writeln ("NO");
	}
}


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

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

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

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

    void step(ref int[] as, ref int k) {
        int n = as.length;
        if (n == 0) return;
        int[] bs = k == 0 ? [] : [as[0]];
        if (k > 0) k--;
        for (int i = 1; i < n; i++) {
            int b = as[i] - as[i - 1];
            if (b > 0) {
                bs ~= b;
            } else {
                k++;
            }
        }
        sort(bs);
        as = bs;
    }

    int[] as;
    int k = 0;
    foreach (a; A) {
        if (a == 0) k++;
        else as ~= a;
    }
    foreach (i; 0 .. N - 1) {
        step(as, k);
        /*
        writeln("as: ", as);
        writeln("k: ", k);
        */
    }
    writeln(as.empty ? 0 : as.back);
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int [int] b;
		foreach (ref x; a)
		{
			b[x] += 1;
		}
		foreach (i; 0..n - 1)
		{
			auto v = b.byKeyValue.array;
			v.schwartzSort !(q{a.key});
			int [int] c;
			foreach (j; 0..v.length)
			{
				if (v[j].value > 1)
				{
					c[0] += v[j].value - 1;
				}
			}
			foreach (j; 0..v.length - 1)
			{
				c[v[j + 1].key - v[j].key] += 1;
			}
			b = c;
		}
		writeln (b.byKey.front);
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		for (n--; n >= 1 && a.length > 1; n--)
		{
			bool addZero = (a.length <= n);
			a = zip (a, a.drop (1)).map !(q{a[1] - a[0]}).array;
			sort (a);
			auto b = a.uniq.array;
			a = addZero ? 0 ~ b : b;
			debug {writeln (a);}
		}
		writeln (a.front);
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		bool zero = false;
		for (n--; n >= 1 && a.length > 1; n--)
		{
			a = zip (a, a.drop (1)).map !(q{a[1] - a[0]}).array;
			sort (a);
			auto b = a.uniq.array;
			zero |= (b.length < a.length);
			a = (b.length < n) ? 0 ~ b : b;
			debug {writeln (a);}
		}
		writeln (a.front);
	}
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto s = '1' ~ readln.strip ~ '0';
		auto n = s.length;
		auto p = s.countUntil ('0');
		auto q = n - 1 - s.retro.countUntil ('1');
		int res = n - 2;
		foreach (i; 1..n - 1)
		{
			res -= (p < i || q > i);
		}
		writeln (res);
	}
}

// cheese-cracker [2022-05-05]

void solve(){
    auto word = scan!(dchar[]);
    int last1 = -1;
    int first0 = -1;
    for(int i = 0; i < word.length; ++i){
       if(word[i] == '1'){
           last1 = i;
       }else if(word[i] == '0' && first0 == -1){
           first0 = i;
       }
    }
    if(last1 == -1 && first0 == -1){
        writeln(word.length.to!int);
    }else if(last1 == -1){
        writeln(first0 + 1);
    }else if(first0 == -1){
        writeln(word.length.to!int - last1);
    }else if(last1 < first0){
        writeln(first0 - last1 + 1);
    }else{
        long cnt0 = 0;
        for(int i = 0; i < last1; ++i){
            cnt0 += (word[i] == '0');
        }
        long cnt1 = 0;
        for(int i = first0+1; i < word.length; ++i){
            cnt1 += (word[i] == '1');
        }
        if(cnt1 > cnt0 || cnt0 < cnt1){
            writeln(1);
        }else{
            writeln(last1 - first0 + 1);
        }
    }
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) solve;
}

/*_________________________*That's All Folks!*__________________________*/
import std.stdio, std.range, std.conv, std.typecons, std.algorithm;
import std.container, std.math, std.numeric, std.functional;
string[] tk; alias tup = Tuple!(long, long);
T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }


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

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    char[][] F = new char[][N];
    foreach (i; 0 .. N) {
        F[i] = cast(char[])readln.chomp;
    }

    char flip(char x) {
        if (x == 'B') return 'W';
        if (x == 'W') return 'B';
        assert(0);
    }

    const dy = [1, 0, -1, 0],
          dx = [0, 1, 0, -1];
    void dfs(int y, int x, char c) {
        F[y][x] = c;
        foreach (i; 0 .. 4) {
            int ny = y + dy[i],
                nx = x + dx[i];
            if (ny < 0 || ny >= N) continue;
            if (nx < 0 || nx >= M) continue;
            if (F[ny][nx] == '.') {
                dfs(ny, nx, flip(c));
            }
        }
    }

    for (int y = 0; y < N; y++) {
        for (int x = 0; x < M; x++) {
            if (F[y][x] == '.') {
                dfs(y, x, 'B');
            }
        }
    }
    foreach (ref L; F) {
        writeln(L);
    }
}

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

void main() {

	byte n, m;

	readf(" %s %s", &n, &m);

	bool k = (m & 1) ? 0 : 1;

	char c;
	bool flag = 0;
	foreach (i; 0 .. n) {
		foreach (j; 0 .. m) {
			scanf(" %c", &c);
			if (c == '-')
				write('-');
			else if (flag)
				putchar('W');
			else
				putchar('B');
			flag ^= 1;
		}
		flag ^= k;
		putchar('\n');
	}
}

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

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

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int M, N;
string[] A;

void main(string[] args) {
	try {
	for (; ; ) {
		M = readInt;
		N = readInt;
		A = new string[M];
		foreach (x; 0 .. M) {
			A[x] = readToken;
		}

		foreach (x; 0 .. M) {
			foreach (y; 0 .. N) {
				write((A[x][y] == '-') ? '-' : ((x + y) % 2 == 0) ? 'B' : 'W');
			}
			writeln;
		}

	}
	} catch (EOFException) {}
}

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

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    char[][] F = new char[][N];
    foreach (i; 0 .. N) {
        F[i] = cast(char[])readln.chomp;
    }

    char flip(char x) {
        if (x == 'B') return 'W';
        if (x == 'W') return 'B';
        assert(0);
    }

    bool C(char x) {
        return x == 'W' || x == 'B';
    }

    for (int y = 0; y < N; y++) {
        for (int x = 0; x < M; x++) {
            if (F[y][x] == '-') continue;
            if (y - 1 >= 0 && C(F[y - 1][x])) {
                F[y][x] = flip(F[y - 1][x]);
            } else if (x - 1 >= 0 && C(F[y][x - 1])) {
                F[y][x] = flip(F[y][x - 1]);
            } else {
                F[y][x] = 'B';
            }
        }
    }
    foreach (ref L; F) {
        writeln(L);
    }
}

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

char[102][102] a;

void main() {

	byte n, m;

	readf(" %s %s", &n, &m);

	char c;
	foreach (i; 1 .. n + 1)
		foreach (j; 1 .. m + 1) {
			scanf(" %c", &c);
			if (c == '-')
				a[i][j] = '-';
			else
				a[i][j] = 'W';
		}

	foreach (i; 1 .. n + 1) {
		foreach (j; 1 .. m + 1) {
			if (a[i][j] == '-') {
				write('-');
				continue;
			}
			if ((i != 1 && j != 1) && a[i][j] == 'W' && a[i - 1][j] != 'B' && a[i][j + 1] != 'B' && a[i + 1][j] != 'B' && a[i][j - 1] != 'B') {
				a[i][j] = 'B';
				write('B');
			}
			else
				write('W');
		}
		putchar('\n');
	}
}

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

char[102][102] a;

void main() {

	byte n, m;

	readf(" %s %s", &n, &m);

	char c;
	foreach (i; 1 .. n + 1)
		foreach (j; 1 .. m + 1) {
			scanf(" %c", &c);
			if (c == '-')
				a[i][j] = '-';
			else
				a[i][j] = 'W';
		}

	foreach (i; 1 .. n + 1) {
		foreach (j; 1 .. m + 1) {
			if (a[i][j] == '-') {
				write('-');
				continue;
			}
			if (i == 1 && j == 0) {
				write('W');
				continue;
			}
			if ( a[i][j] == 'W' && a[i - 1][j] != 'B' && a[i][j + 1] != 'B' && a[i + 1][j] != 'B' && a[i][j - 1] != 'B') {
				a[i][j] = 'B';
				write('B');
			}
			else
				write('W');
		}
		putchar('\n');
	}
}

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

char[102][102] a;

void main() {

	byte n, m;

	readf(" %s %s", &n, &m);

	char c;
	foreach (i; 1 .. n + 1)
		foreach (j; 1 .. m + 1) {
			scanf(" %c", &c);
			if (c == '-')
				a[i][j] = '-';
			else
				a[i][j] = 'W';
		}

	foreach (i; 1 .. n + 1) {
		foreach (j; 1 .. m + 1) {
			if (a[i][j] == '-') {
				write('-');
				continue;
			}
			if (a[i][j] == 'W' && a[i - 1][j] != 'B' && a[i][j + 1] != 'B' && a[i + 1][j] != 'B' && a[i][j - 1] != 'B') {
				a[i][j] = 'B';
				write('B');
			}
			else
				write('W');
		}
		putchar('\n');
	}
}

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

char[102][102] a;

void main() {

	byte n, m;

	readf(" %s %s", &n, &m);

	char c;
	foreach (i; 1 .. n + 1)
		foreach (j; 1 .. m + 1) {
			scanf(" %c", &c);
			if (c == '-')
				a[i][j] = '-';
			else
				a[i][j] = 'W';
		}

	foreach (i; 1 .. n + 1) {
		foreach (j; 1 .. m + 1) {
			if (a[i][j] == '-') {
				write('-');
				continue;
			}
			if (i == 1 && j == 1) {
				write('W');
				continue;
			}
			if ( a[i][j] == 'W' && a[i - 1][j] != 'B' && a[i][j + 1] != 'B' && a[i + 1][j] != 'B' && a[i][j - 1] != 'B') {
				a[i][j] = 'B';
				write('B');
			}
			else
				write('W');
		}
		putchar('\n');
	}
}

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

void main() {

	byte n, m;

	readf(" %s %s", &n, &m);

	char c;
	bool flag;
	foreach (i; 1 .. n + 1) {
		foreach (j; 1 .. m + 1) {
			scanf(" %c", &c);
			if (c == '-')
				write('-');
			else if (flag)
				putchar('W');
			else
				putchar('B');
			flag ^= 1;
		}
		flag ^= 1;
		putchar('\n');
	}
}

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

char[100][100] a;

void main() {

	byte n, m;

	readf(" %s %s", &n, &m);

	char c;
	foreach (i; 0 .. n)
		foreach (j; 0 .. m) {
			scanf(" %c", &c);
			if (c == '-')
				a[i][j] = '-';
			else
				a[i][j] = 'W';
		}

	foreach (i; 0 .. n) {
		foreach (j; 0 .. m)
			if (a[i][j] == '-')
				write('-');
			else if (a[i][j] == 'W') {
				if (j + 1 <= m - 1 && i + 1 <= n - 1 && a[i][j + 1] == 'W' && a[i + 1][j] == 'W' ||
					i - 1 >= 0 && j - 1 >= 0 && a[i - 1][j] == 'W' && a[i][j - 1] == 'W') {
					write('B');
					a[i][j] = 'B';
				}
				else
					write('W');
			}
		putchar('\n');
	}
}


immutable multi = true;

immutable int maxAi = 1_000_000;
bool[maxAi+1] isPrime = true;

static this()
{
  isPrime[0] = isPrime[1] = false;
  foreach(n; 2 .. maxAi+1)
    {
      if (isPrime[n])
	{
	  for(int m = 2*n;
	      m <= maxAi;
	      m += n) isPrime[m] = false;
	}
    }
}

void solve(int tc)
{
  auto n = readInt!int;
  auto e = readInt!int;
  debug writeln("solving case ", e, " ", n);
  auto a = ma(n, readInt!int);
  auto cnt1 = new int[](n);
  foreach_reverse(i; 0 .. n)
    {
      if (i + e < n)
	{
	  if (a[i] == 1)
	    {
	      cnt1[i] = 1 + cnt1[i + e];
	    }
	  else
	    {
	      cnt1[i] = 0;
	    }
	}
      else
	{
	  cnt1[i] = int(a[i] == 1);
	}
    }
  debug writeln(cnt1);
  long ans = 0;
  foreach_reverse(i; 0 .. n)
    {
      if (isPrime[a[i]])
	{
	  ans += i + e < n? cnt1[i + e] : 0;
	}
      else if (a[i] == 1)
	{
	  auto nxt = i + e * cnt1[i];
	  debug writeln("for ", i, " next is ", nxt);
	  if (nxt < n)
	    {
	      auto aNxt = a[nxt];
	      if (isPrime[aNxt])
		{
		  ans += 1 + (nxt + e < n? cnt1[nxt + e] : 0);
		}
	    }
	}
    }
  ans.writeln;
}

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

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

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

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

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

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

// From: https://rosettacode.org/wiki/Sieve_of_Eratosthenes#D

/// Extensible Sieve of Eratosthenes.
struct Prime {
    uint[] a = [2];
    private void grow() pure nothrow @safe {
        immutable p0 = a[$ - 1] + 1;
        auto b = new bool[p0];

        foreach (immutable di; a) {
            immutable uint i0 = p0 / di * di;
            uint i = (i0 < p0) ? i0 + di - p0 : i0 - p0;
            for (; i < b.length; i += di)
                b[i] = true;
        }
        foreach (immutable uint i, immutable bi; b)
            if (!b[i])
                a ~= p0 + i;
    }
    uint opCall(in uint n) pure nothrow @safe {
        while (n >= a.length)
            grow;
        return a[n];
    }
}

int get_ones_l(int[] a, int i, int e, int[] ones_l)
{
  if (ones_l[i] != -1)
    return ones_l[i];
  if (i - e < 0 || a[i - e] != 1) {
    ones_l[i] = 0;
  } else {
    ones_l[i] = 1 + get_ones_l(a, i - e, e, ones_l);
  }
  return ones_l[i];
}

int get_ones_r(int[] a, int i, int e, int[] ones_r)
{
  if (ones_r[i] != -1)
    return ones_r[i];
  if (i + e >= a.length || a[i + e] != 1) {
    ones_r[i] = 0;
  } else {
    ones_r[i] = 1 + get_ones_r(a, i + e, e, ones_r);
  }
  return ones_r[i];
}

void main()
{
  Prime prime;
  bool[int] primes;
  foreach (prime_num ; int.max.iota.map!prime.until!q{a > 1000001})
    primes[prime_num] = true;

  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    int n, e;
    readf!" %d %d "(n, e);
    auto a = readln.splitter.map!(to!int).array;
    foreach (ref x ; a) {
      if (x == 1)
        continue;
      if (x in primes)
        x = 2;
      else
        x = 0;
    }

    auto ones_l = new int[](n);
    auto ones_r = new int[](n);
    ones_l[] = -1;
    ones_r[] = -1;

    long ans;
    foreach (i ; 0 .. n) {
      if (a[i] != 2)
        continue;
      long l = get_ones_l(a, i, e, ones_l);
      long r = get_ones_r(a, i, e, ones_r);
      if (l != 0 || r != 0) {
        ans += (l + 1) * (r + 1) - 1;
      }
    }
    writeln(ans);
  }
}

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

immutable int limit = 10 ^^ 6 + 3;

void main ()
{
	auto s = new bool [limit];
	s[2..$] = true;
	for (int d = 2; d * d < limit; d++)
	{
		if (s[d])
		{
			for (int e = d; e * d < limit; e++)
			{
				s[e * d] = false;
			}
		}
	}

	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, e;
		readf !(" %s %s") (n, e);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		long res = 0;
		int primes = 0;
		foreach (start; 0..e)
		{
			int lo = 0;
			int hi = 0;
			foreach (c; a.drop (start).stride (e))
			{
				if (c == 1)
				{
					hi += 1;
					res += lo;
				}
				else if (s[c])
				{
					lo = hi + 1;
					hi = 0;
					res += lo;
					primes += 1;
				}
				else
				{
					lo = 0;
					hi = 0;
				}
			}
		}
		writeln (res - primes);
	}
}

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

immutable int limit = 10 ^^ 6 + 3;

void main ()
{
	auto s = new bool [limit];
	s[2..$] = true;
	for (int d = 2; d * d < limit; d++)
	{
		if (s[d])
		{
			for (int e = d; e * d < limit; e += d)
			{
				s[e * d] = false;
			}
		}
	}

	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, e;
		readf !(" %s %s") (n, e);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;

		long res = 0;
		int primes = 0;
		foreach (start; 0..e)
		{
			int lo = 0;
			int hi = 0;
			foreach (c; a.drop (start).stride (e))
			{
				if (c == 1)
				{
					hi += 1;
					res += lo;
				}
				else if (s[c])
				{
					lo = hi + 1;
					hi = 0;
					res += lo;
					primes += 1;
				}
				else
				{
					lo = 0;
					hi = 0;
				}
			}
		}
		writeln (res - primes);
	}
}


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

bool solve (int [] x, string s)
{
	auto a = x[0];
	auto b = x[1];
	auto ab = x[2];
	auto ba = x[3];
	auto n = s.length.to !(int);

	auto ra = s.count ("A");
	auto rb = s.count ("B");
	if (ra != a + ab + ba || rb != b + ab + ba)
	{
		return false;
	}

	alias Record = Tuple !(int, q{a}, int, q{b}, char, q{start});
	Record [] r;
	int ca = 0;
	int cb = 0;
	char start;
	foreach (i; 0..n)
	{
		if (ca == 0 && cb == 0)
		{
			start = s[i];
		}
		if (s[i] == 'A')
		{
			ca += 1;
		}
		else
		{
			cb += 1;
		}
		if (i + 1 == n || s[i] == s[i + 1])
		{
			r ~= Record (ca, cb, start);
			ca = 0;
			cb = 0;
		}
	}
	r.schwartzSort !(t => t.a + t.b);
	reverse (r);

	int wildcard = 0;
	foreach (ref t; r)
	{
		if (t.a != t.b)
		{
			wildcard += min (t.a, t.b);
		}
	}

	int pab = 0;
	int pba = 0;
	foreach (ref t; r)
	{
		if (t.a == t.b)
		{
			if (t.start == 'A')
			{
				pab += t.a;
			}
			else
			{
				pba += t.b;
			}
		}
	}

	foreach (ref t; r)
	{
		if (t.a == t.b)
		{
			if (t.start == 'A')
			{
				if (pba + wildcard < ba)
				{
					int cur = t.b - 1;
					pab -= 1;
					while (pba + wildcard < ba && cur > 0)
					{
						cur -= 1;
						pab -= 1;
						pba += 1;
					}
				}
			}
			else
			{
				if (pab + wildcard < ab)
				{
					int cur = t.a - 1;
					pba -= 1;
					while (pab + wildcard < ab && cur > 0)
					{
						cur -= 1;
						pba -= 1;
						pab += 1;
					}
				}
			}
		}
	}

	if (pab + wildcard >= ab && pba + wildcard >= ba &&
	    pab + pba + wildcard >= ab + ba)
	{
		return true;
	}
	return false;
}

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

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

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

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

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

int solve(int A, int B, int C, int D, string S) {
  debug {
    writeln("  ", [A, B, C, D], " ", S);
  }
  const N = cast(int)(S.length);
  const cntA = cast(int)(S.count('A'));
  const cntB = cast(int)(S.count('B'));
  if (cntA != A + C + D) return 1;
  if (cntB != B + C + D) return 2;

  int odd;
  int[][2] fss;
  for (int i = 0, j; i < N; i = j) {
    for (j = i; j < N && ((S[i] == S[j]) == ((i & 1) == (j & 1))); ++j) {}
    debug {
      writeln("    ", S[i .. j]);
    }
    if ((j - i) % 2 != 0) {
      odd += (j - i) / 2;
    } else {
      fss[S[i] - 'A'] ~= (j - i) / 2;
    }
  }
  foreach (h; 0 .. 2) {
    fss[h].sort;
  }
  debug {
    writeln("    odd = ", odd);
    writeln("    fss = ", fss);
  }

  int[2] cs = [C, D];
  foreach (h; 0 .. 2) {
    foreach (ref f; fss[h]) {
      const t = min(f, cs[h]);
      f -= t;
      cs[h] -= t;
    }
  }
  debug {
    writeln("    ", fss, " ", cs);
  }
  foreach (h; 0 .. 2) {
    foreach (ref f; fss[h ^ 1]) if (f >= 1) {
      const t = min(f - 1, cs[h]);
      cs[h] -= t;
    }
  }
  if (cs[0] + cs[1] > odd) {
    return 3;
  }

  return 0;
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const A = readInt;
        const B = readInt;
        const C = readInt;
        const D = readInt;
        const S = readToken;

        const ans = solve(A, B, C, D, S);
        writeln((ans == 0) ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}


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

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[0]; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }

int N;
int[] P;
long[] S;

long solve() {
  auto h = new int[N];
  h[0] = 1;
  foreach (u; 1 .. N) {
    h[u] = h[P[u]] + 1;
  }
  debug {
    writefln("h = %s", h);
  }
  foreach (u; 0 .. N) {
    if ((S[u] == -1) != (h[u] % 2 == 0)) {
      debug {
        writeln("incorrectly erased");
      }
      return -1;
    }
  }

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

  auto a = new long[N];
  a[0] = S[0];
  foreach (u; 1 .. N) {
    if (S[u] == -1) {
      if (graph[u].empty) {
        a[u] = 0;
      } else {
        a[u] = graph[u].map!(v => S[v]).minElement - S[P[u]];
      }
    } else {
      a[u] = S[u] - S[P[P[u]]] - a[P[u]];
    }
    if (a[u] < 0) {
      debug {
        writeln("negative value needed");
      }
      return -1;
    }
  }
  debug {
    writefln("a = %s", a);
  }
  return a.sum;
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      P = new int[N];
      S = new long[N];
      foreach (u; 1 .. N) {
        P[u] = readInt() - 1;
      }
      foreach (u; 0 .. N) {
        S[u] = readInt();
      }
      const ans = solve();
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

immutable long INF = 1L << 59;

void main() {
    auto N = readln.chomp.to!int;
    auto P = readln.split.map!(to!int).array;
    auto S = readln.split.map!(to!long).array;
    auto X = new long[](N);
    auto G = new int[][](N);
    foreach (i; 0..N-1) {
        G[P[i]-1] ~= i+1;
        G[i+1] ~= P[i]-1;
    }

    bool ok = true;
    long ans = 0;

    long dfs(int n, int p) {
        long ret = S[n];
        if (ret == -1) ret = INF;
        foreach (m; G[n]) {
            if (m == p) continue;
            auto v = dfs(m, n);
            if (v < S[n]) ok = false;
            ret = min(ret, v);
        }
        X[n] = ret;
        return ret;
    }

    void dfs2(int n, int p, long s) {
        if (X[n] == INF) return;
        long a = X[n] - s;
        ans += a;
        foreach (m; G[n]) {
            if (m == p) continue;
            dfs2(m, n, X[n]);
        }
    }

    dfs(0, -1);

    if (!ok) {
        writeln(-1);
        return;
    }

    dfs2(0, -1, 0);

    ans.writeln;
}


import std.stdio;

int main() {
    debug stdin = File("in.txt", "r");

    long[100_001] arr;
    long runtot = 0;
    int n, m;
    readf(" %s %s\n", &n, &m);
    foreach (i; 1 .. n + 1) {
        readf(" %s", &arr[i]);
    }
    foreach (i; 0 .. m) {
        int q, a, b; readf(" %s", &q);
        switch (q) {
            case 1:
                readf(" %s %s\n", &a, &b);
                arr[a] = b - runtot;
                break;
            case 2:
                readf(" %s\n", &a);
                runtot += a;
                break;
            case 3:
                readf(" %s\n", &a);
                writeln(arr[a] + runtot);
                break;
            default:
                assert(0);
        }
    }

    return 0;
}

import std.stdio;

int main() {
    debug stdin = File("in.txt", "r");

    long[100_001] arr;
    long runtot = 0;
    int n, m;
    readf(" %d %d\n", &n, &m);
    foreach (i; 1 .. n + 1) {
        readf(" %d", &arr[i]);
    }
    foreach (i; 0 .. m) {
        int q, a, b; readf(" %d", &q);
        switch (q) {
            case 1:
                readf(" %d %d\n", &a, &b);
                arr[a] = b - runtot;
                break;
            case 2:
                readf(" %d\n", &a);
                runtot += a;
                break;
            case 3:
                readf(" %d\n", &a);
                writeln(arr[a] + runtot);
                break;
            default:
                assert(0);
        }
    }

    return 0;
}


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

class Treap(T)
{
    protected class Node
    {
        Node left;
        Node right;
        T val;
        int priority;
        int size;

        this()
        {
            left = right = null;
            size = 1;
        }

        //int opCmp(Node node)
        //{
            //return this.priority - node.priority;
        //}

        int cmpPriority(int priority)
        {
            if (this.priority == priority) return 0;
            return this.priority < priority ? -1 : 1;
        }

        int cmpVal(T val)
        {
            if (this.val == val) return 0;
            return this.val < val ? -1 : 1;
        }
    }

    protected Node root;
    protected Xorshift rnd;
    protected int _size;
    protected T minimal;
    protected T maximal;

    this()
    {
        root = null;
        rnd = Xorshift(1234567891);
        _size = 0;
        minimal = T.max;
        maximal = T.min;
    }

    protected void leftRotate(ref Node node)
    {
        auto rightNode = node.right;
        if (rightNode)
        {
            node.size = 1;
            if (node.left) node.size += node.left.size;
            if (rightNode.left) node.size += rightNode.left.size;
            rightNode.size = 1 + node.size;
            if (rightNode.right) rightNode.size += rightNode.right.size;
            node.right = rightNode.left;
            rightNode.left = node;
            node = rightNode;
        }
    }

    protected void rightRotate(ref Node node)
    {
        auto leftNode = node.left;
        if (leftNode)
        {
            node.size = 1;
            if (node.right) node.size += node.right.size;
            if (leftNode.right) node.size += leftNode.right.size;
            leftNode.size = 1 + node.size;
            if (leftNode.left) leftNode.size += leftNode.left.size;
            node.left = leftNode.right;
            leftNode.right = node;
            node = leftNode;
        }
    }

    protected Node find(T val)
    {
        return _find(root, val);
    }

    protected Node _find(Node node, T val)
    {
        if (!node) return null;
        auto ret = node.cmpVal(val);
        if (ret == 0) return node;
        if (ret == -1) return _find(node.right, val);
        return _find(node.left, val);
    }

    public void insert(T val)
    {
        auto ret = _insert(root, val);
        if (ret)
        {
            ++ _size;
        }
    }

    protected bool _insert(ref Node node, T val)
    {
        if (!node)
        {
            node = new Node();
            node.val = val;
            node.priority = this.rnd.front;
            this.rnd.seed(unpredictableSeed);
            return true;
        }
        auto ret = node.cmpVal(val);
        if (ret == 0) return false;
        bool res;
        if (ret == 1)
        {
            res = _insert(node.left, val);
            if (res)
            {
                if (node.left.priority > node.priority)
                {
                    rightRotate(node);
                }
                else
                {
                    ++ node.size;
                }
            }
        }
        else if (ret == -1)
        {
            res = _insert(node.right, val);
            if (res)
            {
                if (node.right.priority > node.priority)
                {
                    leftRotate(node);
                }
                else
                {
                    ++ node.size;
                }
            }
        }
        return res;
    }

    public void remove(T val)
    {
        auto ret = _remove(root, val);
        if (ret)
        {
            -- _size;
        }
    }

    protected bool _remove(ref Node node, T val)
    {
        if (!node) return false;
        auto ret = node.cmpVal(val);
        if (ret == -1)
        {
            return _remove(node.right, val);
        }
        else if (ret == 1)
        {
            return _remove(node.left, val);
        }
        if (!node.left && !node.right)
        {
            node = null;
            return true;
        }
        if (!node.left)
        {
            node = node.right;
            return true;
        }
        if (!node.right)
        {
            node = node.left;
            return true;
        }
        bool res;
        if (node.left.cmpPriority(node.right.priority) == 1)
        {
            rightRotate(node);
            res = _remove(node.right, val);
        }
        else
        {
            leftRotate(node);
            res = _remove(node.left, val);
        }
        if (res)
        {
            -- node.size;
        }
        return res;
    }

    protected int _countLess(Node node, T val)
    {
        if (!node) return 0;
        if (node.val == val)
        {
            if (node.left)
            {
                return node.left.size;
            }
            return 0;
        }
        if (node.val < val)
        {
            auto res = _countLess(node.right, val);
            ++ res;
            if (node.left)
            {
                res += node.left.size;
            }
            return res;
        }
        return _countLess(node.left, val);
    }

    public int countLess(T val)
    {
        return _countLess(root, val);
    }

    protected int _countGreater(Node node, T val)
    {
        if (!node) return 0;
        if (node.val == val)
        {
            if (node.right)
            {
                return node.right.size;
            }
            return 0;
        }
        if (node.val > val)
        {
            auto res = _countGreater(node.left, val);
            ++ res;
            if (node.right)
            {
                res += node.right.size;
            }
            return res;
        }
        return _countGreater(node.right, val);
    }

    public int countGreater(T val)
    {
        return _countGreater(root, val);
    }

    protected T _getMinimal(Node node)
    {
        if (!node) return minimal;
        if (!node.left) return node.val;
        return _getMinimal(node.left);
    }

    public T getMinimal()
    {
        return _getMinimal(root);
    }

    protected T _getMaximal(Node node)
    {
        if (!node) return maximal;
        if (!node.right) return node.val;
        return _getMaximal(node.right);
    }

    public T getMaximal()
    {
        return _getMaximal(root);
    }

    public void travel()
    {
        _travel(root);
    }

    protected void _travel(Node node)
    {
        if (!node) return;
        _travel(node.left);
        writeln(node.val);
        writeln(node.priority);
        writeln(node.size);
        _travel(node.right);
    }

    public void clear()
    {
        while (root)
        {
            remove(root.val);
        }
    }

    @property public int size()
    {
        return _size;
    }
}

void solve(int[] a)
{
    auto n = cast(int)a.length;
    auto treap = new Treap!int();
    auto lc = new int[n];
    foreach (i; 0 .. n)
    {
        lc[i] = treap.countGreater(a[i]);
        treap.insert(a[i]);
    }
    treap.clear();
    auto rc = new int[n];
    for (int i = n - 1; i >= 0; -- i)
    {
        rc[i] = treap.countLess(a[i]);
        treap.insert(a[i]);
    }
    long ans = 0;
    foreach (i; 0 .. n)
    {
        ans += cast(long)lc[i] * rc[i];
    }
    writeln(ans);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a);
    }
    return 0;
}

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

void main(string[] args)
{
  auto n = next!int;
  auto a = next!long(n);
  auto lsThan = St(new long[](n));
  auto rightLessThan = new long[](n);
  auto si = iota(0, n).array.sort!((i, j) => a[i] < a[j]);
  debug writeln(si);
  foreach(i; si)
    {
      lsThan.set(i, 1);
      debug
	{
	  foreach(k; 0 .. n)
	    write(lsThan.getSum(k, k), " ");
	  writeln;
	}
      if (i < n - 1) rightLessThan[i] = lsThan.getSum(i + 1, n - 1);
    }
  debug writeln(rightLessThan);
  auto pairs = St(new long[](n));
  auto sol = new long[](n);
  foreach(i; si)
    {
      pairs.set(i, rightLessThan[i]);
      if (i < n - 1) sol[i] = pairs.getSum(i + 1, n - 1);
    }
  sol.sum.writeln;
}

struct St
{
  class Node
  {
    this(int s, int e)
    {
      this.s = s;
      this.e = e;
      this.sum = 0;
    }
    int s, e;
    int len() {return e - s + 1;}
    long sum;
    long query(int i, int j)
    {
      debug writeln("querying ", s, " ", e, " for ", i, " ", j);
      if (i <= s && e <= j) return sum;
      if (s > j || i > e) return 0;
      return leftChild.query(i, j) + rightChild.query(i, j);
    }
    void set(int i, long val)
    {
      if (s <= i && i <= e)
	{
	  if (len == 1) { debug writeln("setting "); sum = val; return; }
	  leftChild.set(i, val);
	  rightChild.set(i, val);
	  sum = leftChild.sum + rightChild.sum;
	}
    }
    Node leftChild, rightChild;
  }
  Node _root;
  this(long[] arr)
  {
    Node construct(int s, int e)
    {
      auto node = new Node(s, e);
      auto len = e - s + 1;
      if (len == 1)
	{
	  node.sum = arr[s];
	  return node;
	}
      int llen = len / 2;
      node.leftChild = construct(s, s + llen - 1);
      node.rightChild = construct(s + llen, e);
      assert(node.leftChild.len == llen);
      assert(node.rightChild.len == len - llen);
      node.sum = node.leftChild.sum + node.rightChild.sum;
      return node;
    }
    _root = construct(0, cast(int)arr.length - 1);
  }
  long getSum(int i, int j)
  {
    return _root.query(i, j);
  }
  void set(int i, long val)
  {
    return _root.set(i, val);
  }
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

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

class Treap(T)
{
    protected class Node
    {
        Node left;
        Node right;
        T val;
        int priority;
        int size;

        this()
        {
            left = right = null;
            size = 1;
        }

        //int opCmp(Node node)
        //{
            //return this.priority - node.priority;
        //}

        int cmpPriority(int priority)
        {
            if (this.priority == priority) return 0;
            return this.priority < priority ? -1 : 1;
        }

        int cmpVal(T val)
        {
            if (this.val == val) return 0;
            return this.val < val ? -1 : 1;
        }
    }

    protected Node root;
    protected Xorshift rnd;
    protected int _size;
    protected T minimal;
    protected T maximal;

    this()
    {
        root = null;
        rnd = Xorshift(1234567891);
        _size = 0;
        minimal = T.max;
        maximal = T.min;
    }

    protected void leftRotate(ref Node node)
    {
        auto rightNode = node.right;
        if (rightNode)
        {
            node.size = 1;
            if (node.left) node.size += node.left.size;
            if (rightNode.left) node.size += rightNode.left.size;
            rightNode.size = 1 + node.size;
            if (rightNode.right) rightNode.size += rightNode.right.size;
            node.right = rightNode.left;
            rightNode.left = node;
            node = rightNode;
        }
    }

    protected void rightRotate(ref Node node)
    {
        auto leftNode = node.left;
        if (leftNode)
        {
            node.size = 1;
            if (node.right) node.size += node.right.size;
            if (leftNode.right) node.size += leftNode.right.size;
            leftNode.size = 1 + node.size;
            if (leftNode.left) leftNode.size += leftNode.left.size;
            node.left = leftNode.right;
            leftNode.right = node;
            node = leftNode;
        }
    }

    protected Node find(T val)
    {
        return _find(root, val);
    }

    protected Node _find(Node node, T val)
    {
        if (!node) return null;
        auto ret = node.cmpVal(val);
        if (ret == 0) return node;
        if (ret == -1) return _find(node.right, val);
        return _find(node.left, val);
    }

    public void insert(T val)
    {
        auto ret = _insert(root, val);
        if (ret)
        {
            ++ _size;
        }
    }

    protected bool _insert(ref Node node, T val)
    {
        if (!node)
        {
            node = new Node();
            node.val = val;
            node.priority = this.rnd.front;
            this.rnd.seed(unpredictableSeed);
            return true;
        }
        auto ret = node.cmpVal(val);
        if (ret == 0) return false;
        bool res;
        if (ret == 1)
        {
            res = _insert(node.left, val);
            if (res)
            {
                if (node.left.priority > node.priority)
                {
                    rightRotate(node);
                }
                else
                {
                    ++ node.size;
                }
            }
        }
        else if (ret == -1)
        {
            res = _insert(node.right, val);
            if (res)
            {
                if (node.right.priority > node.priority)
                {
                    leftRotate(node);
                }
                else
                {
                    ++ node.size;
                }
            }
        }
        return res;
    }

    public void remove(T val)
    {
        auto ret = _remove(root, val);
        if (ret)
        {
            -- _size;
        }
    }

    protected bool _remove(ref Node node, T val)
    {
        if (!node) return false;
        auto ret = node.cmpVal(val);
        if (ret == -1)
        {
            return _remove(node.right, val);
        }
        else if (ret == 1)
        {
            return _remove(node.left, val);
        }
        if (!node.left && !node.right)
        {
            node = null;
            return true;
        }
        if (!node.left)
        {
            node = node.right;
            return true;
        }
        if (!node.right)
        {
            node = node.left;
            return true;
        }
        bool res;
        if (node.left.cmpPriority(node.right.priority) == 1)
        {
            rightRotate(node);
            res = _remove(node.right, val);
        }
        else
        {
            leftRotate(node);
            res = _remove(node.left, val);
        }
        if (res)
        {
            -- node.size;
        }
        return res;
    }

    protected int _countLess(Node node, T val)
    {
        if (!node) return 0;
        if (node.val == val)
        {
            if (node.left)
            {
                return node.left.size;
            }
            return 0;
        }
        if (node.val < val)
        {
            auto res = _countLess(node.right, val);
            ++ res;
            if (node.left)
            {
                res += node.left.size;
            }
            return res;
        }
        return _countLess(node.left, val);
    }

    public int countLess(T val)
    {
        return _countLess(root, val);
    }

    protected int _countGreater(Node node, T val)
    {
        if (!node) return 0;
        if (node.val == val)
        {
            if (node.right)
            {
                return node.right.size;
            }
            return 0;
        }
        if (node.val > val)
        {
            auto res = _countGreater(node.left, val);
            ++ res;
            if (node.right)
            {
                res += node.right.size;
            }
            return res;
        }
        return _countGreater(node.right, val);
    }

    public int countGreater(T val)
    {
        return _countGreater(root, val);
    }

    protected T _getMinimal(Node node)
    {
        if (!node) return minimal;
        if (!node.left) return node.val;
        return _getMinimal(node.left);
    }

    public T getMinimal()
    {
        return _getMinimal(root);
    }

    protected T _getMaximal(Node node)
    {
        if (!node) return maximal;
        if (!node.right) return node.val;
        return _getMaximal(node.right);
    }

    public T getMaximal()
    {
        return _getMaximal(root);
    }

    public void travel()
    {
        _travel(root);
    }

    protected void _travel(Node node)
    {
        if (!node) return;
        _travel(node.left);
        writeln(node.val);
        writeln(node.priority);
        writeln(node.size);
        _travel(node.right);
    }

    public void clear()
    {
        while (root)
        {
            remove(root.val);
        }
    }

    @property public int size()
    {
        return _size;
    }
}

void solve(int[] a)
{
    auto n = cast(int)a.length;
    auto treap = new Treap!int();
    auto lc = new int[n];
    foreach (i; 0 .. n)
    {
        lc[i] = treap.countGreater(a[i]);
        treap.insert(a[i]);
    }
    treap = new Treap!int();
    //treap.clear();
    auto rc = new int[n];
    for (int i = n - 1; i >= 0; -- i)
    {
        rc[i] = treap.countLess(a[i]);
        treap.insert(a[i]);
    }
    long ans = 0;
    foreach (i; 0 .. n)
    {
        ans += cast(long)lc[i] * rc[i];
    }
    writeln(ans);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &a[i]);
        }
        readln;
        solve(a);
    }
    return 0;
}


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

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

    while(t--) {
        long a, b;
        readf("%s %s", &a, &b);
        readln;

        real diff = (a - b)/10.0;
        long moves = cast(long)ceil(abs(diff));
        moves.writeln;
    }
}

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

long pmod(long a, long m)
{
  return (a%m +m)%m;
}

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);

  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto a = next!long;
      auto b = next!long;
      auto diff = abs(a - b);
      long ops = diff / 10;
      if (diff % 10 != 0)
        ops++;
      writeln(ops);
    }
}

// INPUT

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

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

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto d = abs(a - b);
		ans[ti] = (d+9) / 10;
	}

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


module main;
import std.stdio, std.conv, std.string, std.array;

T read(T)()
{
    static string[] tokens;
    while (!tokens.length)
        tokens = stdin.readln.split;
    auto result = tokens[0].to!T;
    tokens = tokens[1 .. $];
    return result;
}

void main()
{
    auto T = read!int;
    foreach (case_index; 0 .. T)
    {
        auto n = read!int, k = read!int;
        auto result_builder = appender!(int[]);
        foreach (i; 1 .. n + 1)
        {
            if (i >= (k + 1) / 2 && i != k)
                result_builder ~= i;
        }
        auto result = result_builder[];
        writeln(result.length);
        writefln!"%(%s %)"(result);
    }
}

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 k = RD!int;

		foreach_reverse (i; 1..n+1)
		{
			if (i == k || i*2 < k) continue;
			else
				ans[ti] ~= i;
		}
	}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		auto a = iota (1, n + 1).array;
		a = a.filter !(x => x * 2 >= k && x != k).array;
		writeln (a.length);
		writefln !("%(%s %)") (a);
	}
}

import std.stdio;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n, k;
        readf!"%d %d\n"(n, k);
        writeln(n - k + k / 2);
        foreach (i; k + 1 .. n + 1)
            write(i, ' ');
        foreach (i; (k + 1) / 2 .. k)
            write(i, ' ');
        writeln;
    }
}

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

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

import std.stdio;
void main()
{
    int q;
    readf!"%d\n"(q);
    foreach (_; 0 .. q)
    {
        int n, k;
        readf!"%d %d\n"(n, k);
        foreach (i; k + 1 .. n + 1)
            write(i, ' ');
        foreach (i; (k + 1) / 2 .. k)
            write(i, ' ');
        writeln;
    }
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n;
		long k;
		readf !(" %s %s") (n, k);
		alias Segment = Tuple !(long, q{lo}, long, q{hi});
		Segment [2] s;
		foreach (i; 0..2)
		{
			readf !(" %s %s") (s[i].lo, s[i].hi);
		}
		auto hi = min (s[0].hi, s[1].hi);
		auto lo = max (s[0].lo, s[1].lo);
		auto common = max (0, hi - lo);
		k = max (0L, k - n * 1L * common);
		auto gap = max (0, s[0].lo - s[1].hi, s[1].lo - s[0].hi);
		auto d1 = abs (s[1].hi - s[0].hi);
		auto d2 = abs (s[1].lo - s[0].lo);
		long len = d1 + d2 - gap;
		debug {writeln ("gap = ", gap, ", len = ", len);}

		long res = 0;
		foreach (i; 0..n)
		{
			if (k <= 0)
			{
				break;
			}
			/// заплатить  gap + cur
			/// получить       + cur      к ответу
			///   0 <= cur <= len
			auto cur = min (k, len);
			auto pay = gap + cur;
			if (i > 0 && pay > cur * 2)
			{
				res += k * 2;
				k = 0;
			}
			else
			{
				res += pay;
				k -= cur;
			}
		}
		writeln (res + k * 2);
	}
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n;
		long k;
		readf !(" %s %s") (n, k);
		alias Segment = Tuple !(long, q{lo}, long, q{hi});
		Segment [2] s;
		foreach (i; 0..2)
		{
			readf !(" %s %s") (s[i].lo, s[i].hi);
		}
		auto hi = min (s[0].hi, s[1].hi);
		auto lo = max (s[0].lo, s[1].lo);
		auto common = max (0, hi - lo);
		k = max (0L, k - n * 1L * common);
		auto gap = max (0, s[0].lo - s[1].hi, s[1].lo - s[0].hi);
		auto d1 = abs (s[1].hi - s[0].hi);
		auto d2 = abs (s[1].lo - s[0].lo);
		long len = d1 + d2 - gap;
		debug {writeln ("gap = ", gap, ", len = ", len);}

		long res = 0;
		foreach (i; 0..n)
		{
			if (k <= 0)
			{
				break;
			}
			auto cur = min (k, len);
			auto pay = gap + cur;
			if (i > 0 && pay > k * 2)
			{
				res += k * 2;
				k = 0;
			}
			else
			{
				res += pay;
				k -= cur;
			}
		}
		writeln (res + k * 2);
	}
}