Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same length such that:  b is lexicographically greater than or equal to a.  bi ≥ 2.  b is pairwise coprime: for every 1 ≤ i < j ≤ n, bi and bj are coprime, i. e. GCD(bi, bj) = 1, where GCD(w, z) is the greatest common divisor of w and z. Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it?An array x is lexicographically greater than an array y if there exists an index i such than xi > yi and xj = yj for all 1 ≤ j < i. An array x is equal to an array y if xi = yi for all 1 ≤ i ≤ n.

The first line contains an integer n (1 ≤ n ≤ 105), the number of elements in a and b. The second line contains n integers a1, a2, ..., an (2 ≤ ai ≤ 105), the elements of a.

Output n space-separated integers, the i-th of them representing bi.

NoteNote that in the second sample, the array is already pairwise coprime so we printed it.

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 2000000; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			if (v * v > i) {
				break;
			}
			if (!yes) {
				break;
			}
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	bool[int] done;

	int m = 0;
	foreach (i, v; a) {
		if (m) write = " ";
		++m;
		int w = v;
		for (;;) {
			bool dame = false;
			int z = w;
			foreach (p; primes) {
				if (p > z) {
					break;
				}
				dame |= z % p == 0 && p in done;
				if (z % p == 0) {
					z /= p;
				}
			}
			if (!dame) {
				break;
			}
			++w;
		}
		int z = w;
		foreach (p; primes) {
			if (p > z) {
				break;
			}
			if (z % p == 0) {
				z /= p;
				done[p] = true;
			}
		}
		done[w] = true;
		w.write;
		if (w > v)
			break;
	}
	foreach (p; primes) {
		if (m >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		if (m) write = " ";
		++m;
		p.write;
	}
	writeln;
}

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n; readV(n);
  int[] a; readA(n, a);

  auto mp = 1_500_000;
  auto p = primes(mp);

  auto f = new int[][](mp);
  foreach (i; 2..mp) {
    auto m = i;
    while (m > 1) {
      auto fi = factor(m, p);
      if (f[i].empty || f[i][$-1] != fi) f[i] ~= fi;
      m /= fi;
    }
  }

  auto b = new int[](n), v = new bool[](mp), mi = -1;
  foreach (i; 0..n) {
    for (auto c = mi == -1 ? a[i] : mi;; ++c) {
      if (f[c].any!(fi => v[fi])) continue;
      b[i] = c;
      foreach (fi; f[c]) v[fi] = true;
      if (mi == -1 && c > a[i])
        mi = 2;
      else if (mi > 1)
        mi = c;
      break;
    }
  }

  foreach (i; 0..n) {
    write(b[i]);
    if (i < n-1) write(" ");
  }
  writeln;
}

pure T[] primes(T)(T n)
{
  import std.algorithm, std.bitmanip, std.conv, std.range;

  auto sieve = BitArray();
  sieve.length((n+1)/2);
  sieve = ~sieve;

  foreach (p; 1..((nsqrt(n)-1)/2+1))
    if (sieve[p])
      for (auto q = p*3+1; q < (n+1)/2; q += p*2+1)
        sieve[q] = false;

  auto r = sieve.bitsSet.map!(to!T).map!("a*2+1").array;
  r[0] = 2;

  return r;
}

pure T factor(T)(T n, const T[] p)
{
  auto ma = nsqrt(n)+1;
  foreach (pi; p)
    if (pi > ma) return n;
    else if (n%pi == 0) return pi;
  return n;
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = 1 << (n.bsr/2+1);
  return iota(1, m).map!"a*a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 1000000; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			if (v * v > i) {
				break;
			}
			if (!yes) {
				break;
			}
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	bool[int] done;

	int m = 0;
	foreach (i, v; a) {
		if (m) write = " ";
		++m;
		int w = v;
		for (;;) {
			bool dame = false;
			int z = w;
			foreach (p; primes) {
				if (p > z) {
					break;
				}
				dame |= z % p == 0 && p in done;
				if (z % p == 0) {
					z /= p;
				}
			}
			if (!dame) {
				break;
			}
			++w;
		}
		int z = w;
		foreach (p; primes) {
			if (p > z) {
				break;
			}
			if (z % p == 0) {
				z /= p;
				done[p] = true;
			}
		}
		done[w] = true;
		w.write;
		if (w > v)
			break;
	}
	foreach (p; primes) {
		if (m >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		if (m) write = " ";
		++m;
		p.write;
	}
	writeln;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 200000; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	bool[int] done;

	int m = 0;
	foreach (i, v; a) {
		if (m) write = " ";
		++m;
		int w = v;
		for (;;) {
			bool dame = false;
			foreach (p; primes) {
				if (p > w) {
					break;
				}
				dame |= w % p == 0 && p in done;
			}
			if (!dame) {
				break;
			}
			++w;
		}
		foreach (p; primes) {
			if (p > w) {
				break;
			}
			if (w % p == 0) {
				done[p] = true;
			}
		}
		done[w] = true;
		w.write;
		if (w > v)
			break;
	}
	foreach (p; primes) {
		if (m >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		if (m) write = " ";
		++m;
		p.write;
	}
	writeln;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 100100; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	int[] ans;
	bool[int] done;

	foreach (i, v; a) {
		if (pr.contains(v)) {
			ans ~= v;
			done[v] = true;
		} else {
			int w = v;
			for (;;) {
				bool dame = false;
				foreach (p; primes) {
					if (p > w) {
						break;
					}
					dame |= w % p == 0 && p in done;
				}
				if (!dame) {
					break;
				}
				++w;
			}
			foreach (p; primes) {
				if (p * p > w) {
					break;
				}
				if (w % p == 0) {
					done[p] = true;
				}
			}
			ans ~= w;
			if (w > v)
				break;
		}
	}
	foreach (p; primes) {
		if (ans.length >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		ans ~= p;
	}
	writefln("%(%d %)", ans);
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 300000; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			if (!yes) {
				break;
			}
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	bool[int] done;

	int m = 0;
	foreach (i, v; a) {
		if (m) write = " ";
		++m;
		int w = v;
		for (;;) {
			bool dame = false;
			int z = w;
			foreach (p; primes) {
				if (p > z) {
					break;
				}
				dame |= z % p == 0 && p in done;
				if (z % p == 0) {
					z /= p;
				}
			}
			if (!dame) {
				break;
			}
			++w;
		}
		int z = w;
		foreach (p; primes) {
			if (p > z) {
				break;
			}
			if (z % p == 0) {
				z /= p;
				done[p] = true;
			}
		}
		done[w] = true;
		w.write;
		if (w > v)
			break;
	}
	foreach (p; primes) {
		if (m >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		if (m) write = " ";
		++m;
		p.write;
	}
	writeln;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 200000; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	int[] ans;
	bool[int] done;

	foreach (i, v; a) {
		int w = v;
		for (;;) {
			bool dame = false;
			foreach (p; primes) {
				if (p > w) {
					break;
				}
				dame |= w % p == 0 && p in done;
			}
			if (!dame) {
				break;
			}
			++w;
		}
		foreach (p; primes) {
			if (p > w) {
				break;
			}
			if (w % p == 0) {
				done[p] = true;
			}
		}
		ans ~= w;
		done[w] = true;
		if (w > v)
			break;
	}
	foreach (p; primes) {
		if (ans.length >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		ans ~= p;
	}
	writefln("%(%d %)", ans);
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 200000; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			if (!yes) {
				break;
			}
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	bool[int] done;

	int m = 0;
	foreach (i, v; a) {
		if (m) write = " ";
		++m;
		int w = v;
		for (;;) {
			bool dame = false;
			int z = w;
			foreach (p; primes) {
				if (p > w) {
					break;
				}
				dame |= z % p == 0 && p in done;
				if (z % p == 0) {
					z /= p;
				}
			}
			if (!dame) {
				break;
			}
			++w;
		}
		int z = w;
		foreach (p; primes) {
			if (p > z) {
				break;
			}
			if (z % p == 0) {
				z /= p;
				done[p] = true;
			}
		}
		done[w] = true;
		w.write;
		if (w > v)
			break;
	}
	foreach (p; primes) {
		if (m >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		if (m) write = " ";
		++m;
		p.write;
	}
	writeln;
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 100100; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	int[] ans;
	bool[int] done;

	foreach (i, v; a) {
		if (pr.contains(v)) {
			ans ~= v;
			done[v] = true;
		} else {
			int w = v;
			for (;;) {
				bool dame = false;
				foreach (p; primes) {
					if (p > w) {
						break;
					}
					dame |= w % p == 0 && p in done;
				}
				if (!dame) {
					break;
				}
				++w;
			}
			foreach (p; primes) {
				if (p * p > w) {
					break;
				}
				if (w % p == 0) {
					done[p] = true;
				}
			}
			ans ~= w;
			done[w] = true;
			if (w > v)
				break;
		}
	}
	foreach (p; primes) {
		if (ans.length >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		ans ~= p;
	}
	writefln("%(%d %)", ans);
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 100100; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	int[] ans;
	bool[int] done;

	foreach (i, v; a) {
		int w = v;
		for (;;) {
			bool dame = false;
			foreach (p; primes) {
				if (p > w) {
					break;
				}
				dame |= w % p == 0 && p in done;
			}
			if (!dame) {
				break;
			}
			++w;
		}
		foreach (p; primes) {
			if (p > w) {
				break;
			}
			if (w % p == 0) {
				done[p] = true;
			}
		}
		ans ~= w;
		done[w] = true;
		if (w > v)
			break;
	}
	foreach (p; primes) {
		if (ans.length >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		ans ~= p;
	}
	writefln("%(%d %)", ans);
}

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;

void main()
{
	int n = readln.chomp.to!int;
	int[] a = readln.chomp.split.map!(to!int).array;
	int[] primes = [2];
	for (int i = 3; i <= 100100; i += 2) {
		bool yes = true;
		foreach (v; primes) {
			yes &= i % v != 0;
		}
		if (yes) {
			primes ~= i;
		}
	}
	auto pr = assumeSorted(primes);
	int m = 0;
	int index = 0;
	int[] ans;
	bool[int] done;
	foreach (i, v; a) {
		if (pr.contains(v)) {
			ans ~= v;
			done[v] = true;
		} else {
			int ite = v;
			for (;;) {
				auto ret = pr.upperBound(ite);
				ite = ret.front;
				if (ite !in done) {
					break;
				}
			}
			ans ~= ite;
			done[ite] = true;
			break;
		}
	}
	foreach (p; primes) {
		if (ans.length >= n) {
			break;
		}
		if (p in done) {
			continue;
		}
		ans ~= p;
	}
	writefln("%(%d %)", ans);
}

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n; readV(n);
  int[] a; readA(n, a);

  auto mp = 1_500_000;
  auto p = primes(mp);

  auto f = new int[][](mp);
  foreach (i; 2..mp) {
    auto m = i;
    while (m > 1) {
      auto fi = factor(m, p);
      if (f[i].empty || f[i][$-1] != fi) f[i] ~= fi;
      m /= fi;
    }
  }

  auto b = new int[](n), v = new bool[](mp), mi = -1;
  foreach (i; 0..n) {
    for (auto c = mi == -1 ? a[i] : b[i-1];; ++c) {
      if (f[c].any!(fi => v[fi])) continue;
      b[i] = c;
      foreach (fi; f[c]) v[fi] = true;
      if (mi == -1 && c > a[i])
        mi = 2;
      else if (mi > 1)
        mi = c;
      break;
    }
  }

  foreach (i; 0..n) {
    write(b[i]);
    if (i < n-1) write(" ");
  }
  writeln;
}

pure T[] primes(T)(T n)
{
  import std.algorithm, std.bitmanip, std.conv, std.range;

  auto sieve = BitArray();
  sieve.length((n+1)/2);
  sieve = ~sieve;

  foreach (p; 1..((nsqrt(n)-1)/2+1))
    if (sieve[p])
      for (auto q = p*3+1; q < (n+1)/2; q += p*2+1)
        sieve[q] = false;

  auto r = sieve.bitsSet.map!(to!T).map!("a*2+1").array;
  r[0] = 2;

  return r;
}

pure T factor(T)(T n, const T[] p)
{
  auto ma = nsqrt(n)+1;
  foreach (pi; p)
    if (pi > ma) return n;
    else if (n%pi == 0) return pi;
  return n;
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = 1 << (n.bsr/2+1);
  return iota(1, m).map!"a*a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n; readV(n);
  int[] a; readA(n, a);

  auto mp = 200_000;
  auto p = primes(mp);

  auto f = new int[][](mp);
  foreach (i; 2..mp) {
    auto m = i;
    while (m > 1) {
      auto fi = factor(m, p);
      if (f[i].empty || f[i][$-1] != fi) f[i] ~= fi;
      m /= fi;
    }
  }

  auto b = new int[](n), v = new bool[](mp), mi = -1;
  foreach (i; 0..n) {
    for (auto c = mi == -1 ? a[i] : mi;; ++c) {
      if (f[c].any!(fi => v[fi])) continue;
      b[i] = c;
      foreach (fi; f[c]) v[fi] = true;
      if (mi == -1 && c > a[i])
        mi = 2;
      else if (mi > 1)
        mi = c;
      break;
    }
  }

  foreach (i; 0..n) {
    write(b[i]);
    if (i < n-1) write(" ");
  }
  writeln;
}

pure T[] primes(T)(T n)
{
  import std.algorithm, std.bitmanip, std.conv, std.range;

  auto sieve = BitArray();
  sieve.length((n+1)/2);
  sieve = ~sieve;

  foreach (p; 1..((nsqrt(n)-1)/2+1))
    if (sieve[p])
      for (auto q = p*3+1; q < (n+1)/2; q += p*2+1)
        sieve[q] = false;

  auto r = sieve.bitsSet.map!(to!T).map!("a*2+1").array;
  r[0] = 2;

  return r;
}

pure T factor(T)(T n, const T[] p)
{
  auto ma = nsqrt(n)+1;
  foreach (pi; p)
    if (pi > ma) return n;
    else if (n%pi == 0) return pi;
  return n;
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = 1 << (n.bsr/2+1);
  return iota(1, m).map!"a*a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}

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

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n; readV(n);
  int[] a; readA(n, a);

  auto mp = 1_000_000;
  auto p = primes(mp);

  auto f = new int[][](mp);
  foreach (i; 2..mp) {
    auto m = i;
    while (m > 1) {
      auto fi = factor(m, p);
      if (f[i].empty || f[i][$-1] != fi) f[i] ~= fi;
      m /= fi;
    }
  }

  auto b = new int[](n), v = new bool[](mp), mi = -1;
  foreach (i; 0..n) {
    for (auto c = mi == -1 ? a[i] : mi;; ++c) {
      if (f[c].any!(fi => v[fi])) continue;
      b[i] = c;
      foreach (fi; f[c]) v[fi] = true;
      if (mi == -1 && c > a[i])
        mi = 2;
      else if (mi > 1)
        mi = c;
      break;
    }
  }

  foreach (i; 0..n) {
    write(b[i]);
    if (i < n-1) write(" ");
  }
  writeln;
}

pure T[] primes(T)(T n)
{
  import std.algorithm, std.bitmanip, std.conv, std.range;

  auto sieve = BitArray();
  sieve.length((n+1)/2);
  sieve = ~sieve;

  foreach (p; 1..((nsqrt(n)-1)/2+1))
    if (sieve[p])
      for (auto q = p*3+1; q < (n+1)/2; q += p*2+1)
        sieve[q] = false;

  auto r = sieve.bitsSet.map!(to!T).map!("a*2+1").array;
  r[0] = 2;

  return r;
}

pure T factor(T)(T n, const T[] p)
{
  auto ma = nsqrt(n)+1;
  foreach (pi; p)
    if (pi > ma) return n;
    else if (n%pi == 0) return pi;
  return n;
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = 1 << (n.bsr/2+1);
  return iota(1, m).map!"a*a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}


Polycarpus has n friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings s1, s2, ..., sn. All these strings consist only of digits and have the same length. Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. In other words, it is the longest string c which is a prefix (the beginning) of each si for all i (1 ≤ i ≤ n). Help Polycarpus determine the length of the city phone code.

The first line of the input contains an integer n (2 ≤ n ≤ 3·104) — the number of Polycarpus's friends. The following n lines contain strings s1, s2, ..., sn — the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, inclusive. It is also guaranteed that all strings are different.

Print the number of digits in the city phone code.

NoteA prefix of string t is a string that is obtained by deleting zero or more digits from the end of string t. For example, string "00209" has 6 prefixes: "" (an empty prefix), "0", "00", "002", "0020", "00209".In the first sample the city phone code is string "00".In the second sample the city phone code is an empty string.In the third sample the city phone code is string "770123456789".

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

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

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

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        // int t = cin.read_int;
        while (t--) {
                int n = cin.read_int;
                string[] arr = new string[n];

                for (int i = 0; i < n; i++) {
                        arr[i] = cin.read_string;
                }
                int count = 0;
                bool flag = true;
                for (int i = 0; i < arr[0].length; i++) {
                        for (int j = 0; j < n - 1; j++) {
                                if (arr[j][i] != arr[j + 1][i]) {
                                        flag = false;
                                        break;
                                }
                        }
                        if (flag) count++;
                }
                writeln(count);
        }
}

module cf_172A;

import std.stdio;

void main() {
    int n;
    string phoneNumber;
    string prevPhoneNumber;
    int[] count;

    readf("%d ", &n);
    for (int i = 0; i < n; ++i) {
        readf("%s\n", &phoneNumber);

        if (i > 0) {
            for (int j = 0; j < phoneNumber.length; ++j) {
                if (prevPhoneNumber[j] != phoneNumber[j]) {
                    ++count[j];
                }
            }
        } else {
            count = new int[phoneNumber.length];
        }
        prevPhoneNumber = phoneNumber.idup;
    }

    int pref = 0;
    while (pref < count.length && count[pref] == 0) {
        ++pref;
    }

    writeln(pref);
}


When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible. Kefa knows that the i-th dish gives him ai units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating food of the following type — if he eats dish x exactly before dish y (there should be no other dishes between x and y), then his satisfaction level raises by c. Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!

The first line of the input contains three space-separated numbers, n, m and k (1 ≤ m ≤ n ≤ 18, 0 ≤ k ≤ n * (n - 1)) — the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules. The second line contains n space-separated numbers ai, (0 ≤ ai ≤ 109) — the satisfaction he gets from the i-th dish. Next k lines contain the rules. The i-th rule is described by the three numbers xi, yi and ci (1 ≤ xi, yi ≤ n, 0 ≤ ci ≤ 109). That means that if you eat dish xi right before dish yi, then the Kefa's satisfaction increases by ci. It is guaranteed that there are no such pairs of indexes i and j (1 ≤ i < j ≤ k), that xi = xj and yi = yj.

In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.

NoteIn the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5.

module main;

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

int countBit(int n)
{
    if (n == 0)
    {
        return 0;
    }
    return countBit(n >> 1) + (n & 1);
}

long recur(int mask, int prev, int n, int m, int[] a, long[][] dp, int[][] rule, int[] bitCnt)
{
    if (mask + 1 == 1 << n || bitCnt[mask] == m)
    {
        return 0;
    }
    if (prev != -1 && dp[mask][prev] != -1)
    {
        return dp[mask][prev];
    }
    long res = 0;
    foreach (i; 0 .. n)
    {
        if (!(mask & (1 << i)))
        {
            auto ret = recur(mask | (1 << i), i, n, m, a, dp, rule, bitCnt) + a[i];
            if (prev >= 0 && rule[prev][i] > 0)
            {
                ret += rule[prev][i];
            }
            res = max(res, ret);
        }
    }
    if (prev != -1)
    {
        dp[mask][prev] = res;
    }
    return res;
}

int main(string[] args)
{
    int n, m, k;
    while (scanf("%d%d%d", &n, &m, &k) == 3)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            scanf("%d", &a[i]);
        }
        auto rule = new int[][](n, n);
        foreach (i; 0 .. k)
        {
            int x, y, c;
            scanf("%d%d%d", &x, &y, &c);
            rule[x - 1][y - 1] = c;
        }
        auto dp = new long[][](1 << n, n);
        foreach (i; 0 .. (1 << n))
        {
            fill(dp[i], -1);
        }
        auto bitCnt = new int[1 << n];
        foreach (i; 0 .. (1 << n))
        {
            bitCnt[i] = countBit(i);
        }
        auto res = recur(0, -1, n, m, a, dp, rule, bitCnt);
        writeln(res);
    }
	return 0;
}


Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.

The only line contains odd integer n (1 ≤ n ≤ 49).

Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.

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

void main ()
{
	auto n = readln.strip.to !(int);
	auto even = sequence !(q{2 * n + 2});
	auto odd = sequence !(q{2 * n + 1});
	auto a = new int [] [] (n, n);
	auto m = n >> 1;
	foreach (i; 0..n)
	{
		foreach (j; 0..n)
		{
			if (abs (m - i) + abs (m - j) <= m)
			{
				a[i][j] = odd.front;
				odd.popFront ();
			}
			else
			{
				a[i][j] = even.front;
				even.popFront ();
			}
		}
	}
	writefln ("%(%(%4s %)\n%)", a);
}

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

void main ()
{
	auto n = readln.strip.to !(int);
	auto even = sequence !(q{2 * n + 2});
	auto odd = sequence !(q{2 * n + 1});
	auto a = new int [] [] (n, n);
	foreach (i; 0..n)
	{
		foreach (j; 0..n)
		{
			if ((i & 1) || ((n & 2) ? (j + j + 1 == n) : (j == i)))
			{
				a[i][j] = odd.front;
				odd.popFront ();
			}
			else
			{
				a[i][j] = even.front;
				even.popFront ();
			}
		}
	}
	writefln ("%(%(%s %)\n%)", a);
}


Recently, the students of School 179 have developed a unique algorithm, which takes in a binary string $$$s$$$ as input. However, they soon found out that if some substring $$$t$$$ of $$$s$$$ is a palindrome of length greater than 1, the algorithm will work incorrectly. Can the students somehow reorder the characters of $$$s$$$ so that the algorithm will work correctly on the string?A binary string is a string where each character is either 0 or 1.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.A palindrome is a string that reads the same backwards as forwards.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the string $$$s$$$. The second line of each test case contains the string $$$s$$$ of length $$$n$$$ consisting only of the characters 0 and 1.

For each test case, print YES (case-insensitive) if it is possible to reorder the characters of $$$s$$$ so that there are no substrings that are a palindrome of length greater than 1, or NO (case-insensitive) otherwise.

NoteIn the first three test cases, the given strings do not contain palindromes of length greater than 1, so the answers are YES.In the last test case, it is impossible to reorder the characters so that the string does not contain palindromes of length greater than 1, so the answer is NO.

import std;

T
read (alias T, string end = "") () {
    T x;
    readf!("%s" ~ end)(x);
    return x;
}

void main () {
    immutable tests = read!(uint, "\n");
    TEST_LOOP: foreach (test_index; 0 .. tests) {
        readln();

        immutable s = readln().chomp;

        (
            (){
                switch (s.walkLength) {
                    case 1:
                        return true;
                    case 2:
                        return s.front() != s.dropOne.front();
                    default:
                        return false;
                }
            }() ? "YES" : "NO"
        ).writeln;
    }
}
// ""


You have a permutation: an array $$$a = [a_1, a_2, \ldots, a_n]$$$ of distinct integers from $$$1$$$ to $$$n$$$. The length of the permutation $$$n$$$ is odd.Consider the following algorithm of sorting the permutation in increasing order.A helper procedure of the algorithm, $$$f(i)$$$, takes a single argument $$$i$$$ ($$$1 \le i \le n-1$$$) and does the following. If $$$a_i > a_{i+1}$$$, the values of $$$a_i$$$ and $$$a_{i+1}$$$ are exchanged. Otherwise, the permutation doesn't change.The algorithm consists of iterations, numbered with consecutive integers starting with $$$1$$$. On the $$$i$$$-th iteration, the algorithm does the following:   if $$$i$$$ is odd, call $$$f(1), f(3), \ldots, f(n - 2)$$$;  if $$$i$$$ is even, call $$$f(2), f(4), \ldots, f(n - 1)$$$. It can be proven that after a finite number of iterations the permutation will be sorted in increasing order.After how many iterations will this happen for the first time?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 999$$$; $$$n$$$ is odd) — the length of the permutation. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — the permutation itself.  It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$999$$$.

For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time. If the given permutation is already sorted, print $$$0$$$.

NoteIn the first test case, the permutation will be changing as follows:   after the $$$1$$$-st iteration: $$$[2, 3, 1]$$$;  after the $$$2$$$-nd iteration: $$$[2, 1, 3]$$$;  after the $$$3$$$-rd iteration: $$$[1, 2, 3]$$$. In the second test case, the permutation will be changing as follows:   after the $$$1$$$-st iteration: $$$[4, 5, 1, 7, 2, 3, 6]$$$;  after the $$$2$$$-nd iteration: $$$[4, 1, 5, 2, 7, 3, 6]$$$;  after the $$$3$$$-rd iteration: $$$[1, 4, 2, 5, 3, 7, 6]$$$;  after the $$$4$$$-th iteration: $$$[1, 2, 4, 3, 5, 6, 7]$$$;  after the $$$5$$$-th iteration: $$$[1, 2, 3, 4, 5, 6, 7]$$$. In the third test case, the permutation is already sorted and the answer is $$$0$$$.

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

void iteration(int i, int n, int[] a)
{
  if (i % 2 == 0) {
    for (int j = 0; j <= n - 2 - 1; j += 2) {
      if (a[j] > a[j + 1])
        swap(a[j], a[j + 1]);
    }
  } else {
    for (int j = 1; j <= n - 1 - 1; j += 2) {
      if (a[j] > a[j + 1])
        swap(a[j], a[j + 1]);
    }
  }
}

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!int).array;
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (a.isSorted!"a<b")
        break;
      iteration(i, n, a);
      ans++;
    }
    writeln(ans);
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		while (true)
		{
			bool ok = true;
			foreach (i; 0..n)
			{
				if (a[i] != i+1)
				{
					ok = false;
					break;
				}
			}
			if (ok) break;

			foreach (i; 0..n-1)
			{
				if (i % 2 != ans[ti] % 2) continue;
				if (a[i] > a[i+1])
					swap(a[i], a[i+1]);
			}
			++ans[ti];
		}
	}

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

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

void iteration(int i, int n, int[] a)
{
  if (i % 2 == 0) {
    for (int j = 0; j <= n - 2 - 1; j += 2) {
      if (a[j] > a[j + 1])
        swap(a[j], a[j + 1]);
    }
  } else {
    for (int j = 1; j <= n - 1 - 1; j += 2) {
      if (a[j] > a[j + 1])
        swap(a[j], a[j + 1]);
    }
  }
}

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!int).array;
    writeln(a);
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (a.isSorted!"a<b")
        break;
      iteration(i, n, a);
      ans++;
    }
    writeln(ans);
  }
}


Mark has just purchased a rack of $$$n$$$ lightbulbs. The state of the lightbulbs can be described with binary string $$$s = s_1s_2\dots s_n$$$, where $$$s_i=\texttt{1}$$$ means that the $$$i$$$-th lightbulb is turned on, while $$$s_i=\texttt{0}$$$ means that the $$$i$$$-th lightbulb is turned off.Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following:  Select an index $$$i$$$ from $$$2,3,\dots,n-1$$$ such that $$$s_{i-1}\ne s_{i+1}$$$.  Toggle $$$s_i$$$. Namely, if $$$s_i$$$ is $$$\texttt{0}$$$, set $$$s_i$$$ to $$$\texttt{1}$$$ or vice versa. Mark wants the state of the lightbulbs to be another binary string $$$t$$$. Help Mark determine the minimum number of operations to do so.

The first line of the input contains a single integer $$$q$$$ ($$$1\leq q\leq 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$3\leq n\leq 2\cdot 10^5$$$) — the number of lightbulbs. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ — the initial state of the lightbulbs. The third line of each test case contains a binary string $$$t$$$ of length $$$n$$$ — the final state of the lightbulbs. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2\cdot 10^5$$$.

For each test case, print a line containing the minimum number of operations Mark needs to perform to transform $$$s$$$ to $$$t$$$. If there is no such sequence of operations, print $$$-1$$$.

NoteIn the first test case, one sequence of operations that achieves the minimum number of operations is the following.   Select $$$i=3$$$, changing $$$\texttt{01}\color{red}{\texttt{0}}\texttt{0}$$$ to $$$\texttt{01}\color{red}{\texttt{1}}\texttt{0}$$$.  Select $$$i=2$$$, changing $$$\texttt{0}\color{red}{\texttt{1}}\texttt{10}$$$ to $$$\texttt{0}\color{red}{\texttt{0}}\texttt{10}$$$.  In the second test case, there is no sequence of operations because one cannot change the first digit or the last digit of $$$s$$$.In the third test case, even though the first digits of $$$s$$$ and $$$t$$$ are the same and the last digits of $$$s$$$ and $$$t$$$ are the same, it can be shown that there is no sequence of operations that satisfies the condition.In the fourth test case, one sequence that achieves the minimum number of operations is the following:   Select $$$i=3$$$, changing $$$\texttt{00}\color{red}{\texttt{0}}\texttt{101}$$$ to $$$\texttt{00}\color{red}{\texttt{1}}\texttt{101}$$$.  Select $$$i=2$$$, changing $$$\texttt{0}\color{red}{\texttt{0}}\texttt{1101}$$$ to $$$\texttt{0}\color{red}{\texttt{1}}\texttt{1101}$$$.  Select $$$i=4$$$, changing $$$\texttt{011}\color{red}{\texttt{1}}\texttt{01}$$$ to $$$\texttt{011}\color{red}{\texttt{0}}\texttt{01}$$$.  Select $$$i=5$$$, changing $$$\texttt{0110}\color{red}{\texttt{0}}\texttt{1}$$$ to $$$\texttt{0110}\color{red}{\texttt{1}}\texttt{1}$$$.  Select $$$i=3$$$, changing $$$\texttt{01}\color{red}{\texttt{1}}\texttt{011}$$$ to $$$\texttt{01}\color{red}{\texttt{0}}\texttt{011}$$$.

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

void solve() {
    int N = readln.chomp.to!int;
    string S = readln.chomp;
    string T = readln.chomp;

    long C() {
        if (S[0] != T[0] || S[$-1] != T[$-1]) return -1;
        auto s = new int[0], t = new int[0];
        for (int i = 1; i < N; i++) {
            s ~= S[i-1] != S[i];
            t ~= T[i-1] != T[i];
        }
        if (s.reduce!"a+b" != t.reduce!"a+b") return -1;
        int[] xs, xt;
        for (int i = 0; i < N-1; i++) {
            if (s[i]) xs ~= i;
            if (t[i]) xt ~= i;
        }
        long ans = 0;
        for (int i = 0; i < xs.length; i++) {
            ans += abs(xs[i] - xt[i]);
        }
        return ans;
    }

    writeln(C());
}

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

// cheese-cracker [2022-07-15]

void solve(){
    int n = scan!int;
    auto s = scan!(dchar[]);
    auto t = scan!(dchar[]);
    int[] ixs, ixt;
    dchar[] ws, wt;

    ws ~= s[0];
    wt ~= t[0];
    ixs ~= 0;
    ixt ~= 0;

    for(int i = 1; i < n; ++i){
        if(s[i] != s[i-1]){
            ws ~= s[i];
            ixs ~= i;
        }
        if(t[i] != t[i-1]){
            wt ~= t[i];
            ixt ~= i;
        }
    }
    if(ws != wt){
        writeln(-1);
        return;
    }
    long ans = 0;
    for(int i = 0; i < ixt.length; ++i){
        ans += abs(ixt[i] - ixs[i]);
    }
    writeln(ans);
}

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


Kate has a set $$$S$$$ of $$$n$$$ integers $$$\{1, \dots, n\} $$$. She thinks that imperfection of a subset $$$M \subseteq S$$$ is equal to the maximum of $$$gcd(a, b)$$$ over all pairs $$$(a, b)$$$ such that both $$$a$$$ and $$$b$$$ are in $$$M$$$ and $$$a \neq b$$$. Kate is a very neat girl and for each $$$k \in \{2, \dots, n\}$$$ she wants to find a subset that has the smallest imperfection among all subsets in $$$S$$$ of size $$$k$$$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $$$k$$$, will name it $$$I_k$$$. Please, help Kate to find $$$I_2$$$, $$$I_3$$$, ..., $$$I_n$$$.

The first and only line in the input consists of only one integer $$$n$$$ ($$$2\le n \le 5 \cdot 10^5$$$)  — the size of the given set $$$S$$$.

Output contains only one line that includes $$$n - 1$$$ integers: $$$I_2$$$, $$$I_3$$$, ..., $$$I_n$$$.

NoteFirst sample: answer is 1, because $$$gcd(1, 2) = 1$$$.Second sample: there are subsets of $$$S$$$ with sizes $$$2, 3$$$ with imperfection equal to 1. For example, $$$\{2,3\}$$$ and $$$\{1, 2, 3\}$$$.

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.datetime.stopwatch, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
    int n = scan!int;

    std.datetime.stopwatch.StopWatch sw;

    sw.start;

    long[] us = new long[](n + 1);
    us[0] = 0, us[1] = 0;
    foreach(i; 2 .. n + 1){
        if(us[i] > 1) continue;
        foreach(k; 1 .. n / i + 1) if(us[i * k] == 0) us[i * k] = i;
    }

    log(us);

    log(sw.peek);

	long[] ps;
	foreach(i; 2 .. n + 1) if(us[i] == i) ps ~= i;
    log("ps:", ps)    ;

    int pcnt = ps.length.to!int;

    long[long] pv;
    foreach(i, p; ps) pv[p] = i;

    log(sw.peek);

    long sum;
    X[] xs;
    xs ~= X(1, pcnt), sum += pcnt;

    log(sw.peek);

    foreach(i; 2 .. n){
        log("i:", i, sw.peek);
        if(sum > n) break;
        int j = uplimit(0, pcnt - 1, (int j) => ps[j] <= us[i] && ps[j] * i <= n);
        if(j >= 0) xs ~= X(i, j + 1), sum += j + 1;
    }

    log(sw.peek);

    long[] ans;
    long tempsum;
    foreach(x; xs){
        if(ans.length >= n - 1) break;
        if(ans.length + x.count > n - 1) while(ans.length < n - 1) ans ~= x.value;
        else foreach(i; 0 .. x.count) ans ~= x.value;
    }
    ans.unsplit.print;

    log(sw.peek);
}

struct X{
    long value;
    long count;
}

// a 以上 c 以下の範囲で f(x) をみたす最大の x
// ただし、a がみたさない場合は a - 1 を返す ※範囲外エラーに注意
// f は単調減少であること
T uplimit(T)(T a, T c, bool delegate(T) f){
	if(f(c)) return c; if(! f(a)) return a - 1;
	while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) a = b; else c = b; }
	return a;
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
    int n = scan!int;
    long[] ps = primesTo(n);

    bool[long] pset;
    foreach(p; ps) pset[p] = 1;

    long[] xs;
    foreach(p; ps) xs ~= 1;
    A: foreach(long i; 2 .. n){
        if(xs.length > n) break;
        if(i in pset){
            foreach(p; ps){
                if(p <= i && p * i <= n) xs ~= i;
                if(xs.length > n) break A;
            }
        }
        else{
            if(i * 2 <= n) xs ~= i;
        }
    }
    xs.unsplit.print;
}

/// a 以下の素数の集合
/// 例 primesTo(48) = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
/// 時間O(N f(N))、空間O(N) ただしf(N)は素数の個数(=O(log N))
/// 都度計算なのでコストは都度かかる
long[] primesTo(long a){
	bool[] isNg = new bool[](a.to!int + 1);
	isNg[0] = 1, isNg[1] = 1;
	foreach(int i; 2 .. a.to!int + 1){
		if(isNg[i]) continue;
		foreach(int k; 2 .. a.to!int / i + 1) isNg[i * k] = 1;
	}
	long[] res;
	foreach(int i; 0 .. a.to!int + 1) if(! isNg[i]) res ~= i;
	return res;
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.datetime.stopwatch, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0;
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

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

void solve(){
    int n = scan!int;

    std.datetime.stopwatch.StopWatch sw;

    sw.start;

    int[] us = new int[](n + 1);
    us[0] = 0, us[1] = 0;
    foreach(int i; 2 .. n + 1){
        if(us[i] > 1) continue;
        foreach(int k; 1 .. n / i + 1) if(us[i * k] == 0) us[i * k] = i;
    }

    log(us);

    log(sw.peek);

	int[] ps;
	foreach(int i; 2 .. n + 1) if(us[i] == i) ps ~= i;
    log("ps:", ps)    ;

    int pcnt = ps.length.to!int;

    int[int] pv;
    foreach(int i, p; ps) pv[p] = i;

    log(sw.peek);

    int sum;
    X[] xs;
    xs ~= X(1, pcnt), sum += pcnt;

    log(sw.peek);

    foreach(int i; 2 .. n){
        log("i:", i, sw.peek);
        if(sum > n) break;
        int j = uplimit(0, pcnt - 1, (int j) => ps[j] <= us[i] && ps[j] * i <= n);
        if(j >= 0) xs ~= X(i, j + 1), sum += j + 1;
    }

    log(sw.peek);

    int[] ans;
    int tempsum;
    foreach(x; xs){
        if(ans.length >= n - 1) break;
        if(ans.length + x.count > n - 1) while(ans.length < n - 1) ans ~= x.value;
        else foreach(i; 0 .. x.count) ans ~= x.value;
    }
    ans.unsplit.print;

    log(sw.peek);
}

struct X{
    int value;
    int count;
}

/// a 以下の素数の集合
/// 例 primesTo(48) = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
/// 時間O(N f(N))、空間O(N) ただしf(N)は素数の個数(=O(log N))
/// 都度計算なのでコストは都度かかる
long[] primesTo(long a){
	bool[] isNg = new bool[](a.to!int + 1);
	isNg[0] = 1, isNg[1] = 1;
	foreach(int i; 2 .. a.to!int + 1){
		if(isNg[i]) continue;
		foreach(int k; 2 .. a.to!int / i + 1) isNg[i * k] = 1;
	}
	long[] res;
	foreach(int i; 0 .. a.to!int + 1) if(! isNg[i]) res ~= i;
	return res;
}

// a 以上 c 以下の範囲で f(x) をみたす最大の x
// ただし、a がみたさない場合は a - 1 を返す ※範囲外エラーに注意
// f は単調減少であること
T uplimit(T)(T a, T c, bool delegate(T) f){
	if(f(c)) return c; if(! f(a)) return a - 1;
	while(a + 1 < c){ T b = (a + c) / 2; if(f(b)) a = b; else c = b; }
	return a;
}


Sereja placed n points on a plane. Now Sereja wants to place on the plane two straight lines, intersecting at a right angle, so that one of the straight lines intersect the Ox axis at an angle of 45 degrees and the maximum distance from the points to the straight lines were minimum. In this problem we consider the distance between points (x1, y1) and (x2, y2) equal |x1 - x2| + |y1 - y2|. The distance between the point and the straight lines is the minimum distance from the point to some point belonging to one of the lines.Help Sereja, find the maximum distance from the points to the optimally located straight lines.

The first line contains integer n (1 ≤ n ≤ 105). Next n lines contain the coordinates of the lines. The i-th line contains two integers xi, yi (|xi|, |yi| ≤ 109).

In a single line print a real number — the answer to the problem. Your answer will be considered correct iff its absolute or relative error doesn't exceed 10 - 6.

import std.algorithm;
import std.stdio;
import std.typecons;

alias Tuple !(int, "x", int, "y") point;

void fill_extrema (long [] b, ref long [] tmin, ref long [] tmax)
{
	tmin ~= +(1L << 36);
	tmax ~= -(1L << 36);
	foreach (d; b)
	{
		tmin ~= min (tmin[$ - 1], d);
		tmax ~= max (tmax[$ - 1], d);
	}
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new point [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &a[i].x, &a[i].y);
		}
		sort !("a.x - a.y < b.x - b.y") (a);

		long [] z;
		foreach (c; a)
		{
			z ~= c.x - c.y;
		}

		long [] b;
		foreach (c; a)
		{
			b ~= c.x + c.y;
		}

		long [] pmin;
		long [] pmax;
		fill_extrema (b, pmin, pmax);

		long [] smin;
		long [] smax;
		reverse (b);
		fill_extrema (b, smin, smax);
		reverse (b);
		reverse (smin);
		reverse (smax);

		bool check (long lim)
		{
			int j = 0;
			foreach (i; 0..n)
			{
				while ((j < n) && (z[j] - z[i] <= lim))
				{
					j++;
				}

				long lo = min (pmin[i], smin[j]);
				long hi = max (pmax[i], smax[j]);
				if (hi - lo <= lim)
				{
					return true;
				}
			}
			return false;
		}

		long lo = 0;
		long hi = 1L << 32;
		while (lo < hi)
		{
			long me = lo + ((hi - lo) >> 1);
			if (check (me))
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}

		writefln ("%.20f", lo * 0.5);
	}
}

import std.algorithm;
import std.stdio;
import std.typecons;

alias Tuple !(long, "x", long, "y") point;

void fill_extrema (long [] b, ref long [] tmin, ref long [] tmax)
{
	tmin ~= +(1L << 36);
	tmax ~= -(1L << 36);
	foreach (d; b)
	{
		tmin ~= min (tmin[$ - 1], d);
		tmax ~= max (tmax[$ - 1], d);
	}
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new point [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &a[i].x, &a[i].y);
		}
		sort !("a.x - a.y < b.x - b.y") (a);

		long [] z;
		foreach (c; a)
		{
			z ~= c.x - c.y;
		}

		long [] b;
		foreach (c; a)
		{
			b ~= c.x + c.y;
		}

		long [] pmin;
		long [] pmax;
		fill_extrema (b, pmin, pmax);

		long [] smin;
		long [] smax;
		reverse (b);
		fill_extrema (b, smin, smax);
		reverse (b);
		reverse (smin);
		reverse (smax);

		bool check (long lim)
		{
			int i = 0;
			int j = 0;
			while (i < n)
			{
				while ((j < n) && (z[j] - z[i] <= lim))
				{
					j++;
				}

				long lo = min (pmin[i], smin[j]);
				long hi = max (pmax[i], smax[j]);
				if (hi - lo <= lim)
				{
					return true;
				}

				do
				{
					i++;
				}
				while ((i < n) && (z[i] == z[i - 1]));
			}
			return false;
		}

		long lo = 0;
		long hi = 1L << 32;
		while (lo < hi)
		{
			long me = lo + ((hi - lo) >> 1);
			if (check (me))
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}

		writefln ("%.20f", lo * 0.5);
	}
}

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

alias Tuple !(long, "x", long, "y") point;

bool less_plus (T) (auto ref T a, auto ref T b)
{
	return a.x + a.y < b.x + b.y;
}

bool less_minus (T) (auto ref T a, auto ref T b)
{
	return a.x - a.y < b.x - b.y;
}

void fill_extrema (long [] b, ref long [] tmin, ref long [] tmax)
{
	tmin ~= +(1L << 36);
	tmax ~= -(1L << 36);
	foreach (d; b)
	{
		tmin ~= min (tmin[$ - 1], d);
		tmax ~= max (tmax[$ - 1], d);
	}
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new point [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &a[i].x, &a[i].y);
		}
		sort !(less_minus, SwapStrategy.stable) (a);

		long [] z;
		foreach (c; a)
		{
			z ~= c.x - c.y;
		}

		long [] b;
		foreach (c; a)
		{
			b ~= c.x + c.y;
		}

		long [] pmin;
		long [] pmax;
		fill_extrema (b, pmin, pmax);

		long [] smin;
		long [] smax;
		reverse (b);
		fill_extrema (b, smin, smax);
		reverse (b);
		reverse (smin);
		reverse (smax);

		debug {writeln (a);}
		debug {writeln (b);}
		debug {writeln (z);}
		debug {writeln (pmin);}
		debug {writeln (pmax);}
		debug {writeln (smin);}
		debug {writeln (smax);}

		bool check (long lim)
		{
			int i = 0;
			int j = 0;
			while (i < n)
			{
				while ((j < n) && (z[j] - z[i] <= lim))
				{
					j++;
				}
				debug {writefln ("i = %s, j = %s", i, j);}

				long lo = min (pmin[i], smin[j]);
				long hi = max (pmax[i], smax[j]);
				debug {writefln ("%s %s", lo, hi);}
				if (hi - lo <= lim)
				{
					return true;
				}

				do
				{
					i++;
				}
				while ((i < n) && (z[i] == z[i - 1]));
			}
			debug {writefln ("check (%s)", lim);}
			return false;
		}

		long lo = 0;
		long hi = 1L << 32;
		while (lo < hi)
		{
			long me = lo + ((hi - lo) >> 1);
			debug {writefln ("%10s %10s %10s", lo, me, hi);}
			if (check (me))
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
			debug {writefln ("%10s %10s %10s", lo, me, hi);}
		}

		writefln ("%.20f", lo * 0.5);
	}
}

import std.algorithm;
import std.stdio;
import std.typecons;

alias Tuple !(int, "x", int, "y") point;

void fill_extrema (long [] b, ref long [] tmin, ref long [] tmax)
{
	tmin ~= +(1L << 36);
	tmax ~= -(1L << 36);
	foreach (d; b)
	{
		tmin ~= min (tmin[$ - 1], d);
		tmax ~= max (tmax[$ - 1], d);
	}
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new point [n];
		foreach (i; 0..n)
		{
			readf (" %s %s", &a[i].x, &a[i].y);
		}
		sort !("a.x - a.y < b.x - b.y") (a);

		long [] z;
		foreach (c; a)
		{
			z ~= c.x - c.y;
		}

		long [] b;
		foreach (c; a)
		{
			b ~= c.x + c.y;
		}

		long [] pmin;
		long [] pmax;
		fill_extrema (b, pmin, pmax);

		long [] smin;
		long [] smax;
		reverse (b);
		fill_extrema (b, smin, smax);
		reverse (b);
		reverse (smin);
		reverse (smax);

		bool check (long lim)
		{
			int i = 0;
			int j = 0;
			while (i < n)
			{
				while ((j < n) && (z[j] - z[i] <= lim))
				{
					j++;
				}

				long lo = min (pmin[i], smin[j]);
				long hi = max (pmax[i], smax[j]);
				if (hi - lo <= lim)
				{
					return true;
				}

				do
				{
					i++;
				}
				while ((i < n) && (z[i] == z[i - 1]));
			}
			return false;
		}

		long lo = 0;
		long hi = 1L << 32;
		while (lo < hi)
		{
			long me = lo + ((hi - lo) >> 1);
			if (check (me))
			{
				hi = me;
			}
			else
			{
				lo = me + 1;
			}
		}

		writefln ("%.20f", lo * 0.5);
	}
}


There are $$$n$$$ piles of sand where the $$$i$$$-th pile has $$$a_i$$$ blocks of sand. The $$$i$$$-th pile is called too tall if $$$1 < i < n$$$ and $$$a_i > a_{i-1} + a_{i+1}$$$. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)You are given an integer $$$k$$$. An operation consists of picking $$$k$$$ consecutive piles of sand and adding one unit of sand to them all. Formally, pick $$$1 \leq l,r \leq n$$$ such that $$$r-l+1=k$$$. Then for all $$$l \leq i \leq r$$$, update $$$a_i \gets a_i+1$$$.What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq k \leq n$$$) — the number of piles of sand and the size of the operation, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the sizes of the piles. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a single integer — the maximum number of piles that are simultaneously too tall after some (possibly zero) operations.

NoteIn the first test case, we can perform the following three operations:   Add one unit of sand to piles $$$1$$$ and $$$2$$$: $$$[\color{red}{3}, \color{red}{10}, 2, 4, 1]$$$.  Add one unit of sand to piles $$$4$$$ and $$$5$$$: $$$[3, 10, 2, \color{red}{5}, \color{red}{2}]$$$.  Add one unit of sand to piles $$$3$$$ and $$$4$$$: $$$[3, 10, \color{red}{3}, \color{red}{6}, 2]$$$.  Now piles $$$2$$$ and $$$4$$$ are too tall, so in this case the answer is $$$2$$$. It can be shown that it is impossible to make more than $$$2$$$ piles too tall.In the second test case, any operation will increase all piles by $$$1$$$ unit, so the number of too tall piles will always be $$$0$$$.In the third test case, we can increase any pile by $$$1$$$ unit of sand. It can be shown that the maximum number of too tall piles is $$$1$$$.

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

void solve()
{
    int n, k;
    readf!(" %s %s")(n, k);
    readln;
    int[] arr = readln.splitter.map!(to!int).array;
    assert(arr.length == n);
    if (k == 1)
    {
        writeln((n - 1) / 2);
        return;
    }
    iota(1, arr.length - 1).count!(i=>arr[i] > arr[i - 1] + arr[i + 1]).writeln();
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}

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

void solve()
{
    int n, k;
    readf!(" %s %s")(n, k);
    readln;
    int[] arr = readln.splitter.map!(to!int).array;
    assert(arr.length == n);
    if (k == 1)
    {
        iota(1, arr.length-1, 2).count().writeln();
        return;
    }
    iota(1, arr.length-1).filter!(i => arr[i] > arr[i - 1] + arr[i + 1]).count()
        .writeln();
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, k;
		readf !(" %s %s") (n, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		writeln ((k == 1) ? (n - 1) / 2 :
		    iota (1, n - 1).count !(i => a[i] > a[i - 1] + a[i + 1]));
	}
}

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

void solve()
{
    int n, k;
    readf!(" %s %s")(n, k);
    readln;
    int[] arr = readln.splitter.map!(to!int).array;
    assert(arr.length == n);
    if (k == 1)
    {
        iota(1, arr.length-1, 2).count().writeln();
        return;
    }
    iota(1, arr.length-1, 2).filter!(i => arr[i] > arr[i - 1] + arr[i + 1]).count()
        .writeln();
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}

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

void solve()
{
    int n, k;
    readf!(" %s %s")(n, k);
    readln;
    int[] arr = readln.splitter.map!(to!int).array;
    assert(arr.length == n);
    if (k == 1)
    {
        iota(1, arr.length, 2).count().writeln();
        return;
    }
    iota(1, arr.length-1, 2).filter!(i => arr[i] > arr[i - 1] + arr[i + 1]).count()
        .writeln();
}

void main()
{
    size_t TC = 1;
    TC = readln.strip.to!int;
    foreach (_; 0 .. TC)
        solve();
}


You are given a binary matrix $$$A$$$ of size $$$n \times n$$$. Let's denote an $$$x$$$-compression of the given matrix as a matrix $$$B$$$ of size $$$\frac{n}{x} \times \frac{n}{x}$$$ such that for every $$$i \in [1, n], j \in [1, n]$$$ the condition $$$A[i][j] = B[\lceil \frac{i}{x} \rceil][\lceil \frac{j}{x} \rceil]$$$ is met.Obviously, $$$x$$$-compression is possible only if $$$x$$$ divides $$$n$$$, but this condition is not enough. For example, the following matrix of size $$$2 \times 2$$$ does not have any $$$2$$$-compression: $$$01$$$   $$$10$$$ For the given matrix $$$A$$$, find maximum $$$x$$$ such that an $$$x$$$-compression of this matrix is possible.Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input.

The first line contains one number $$$n$$$ ($$$4 \le n \le 5200$$$) — the number of rows and columns in the matrix $$$A$$$. It is guaranteed that $$$n$$$ is divisible by $$$4$$$. Then the representation of matrix follows. Each of $$$n$$$ next lines contains $$$\frac{n}{4}$$$ one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from $$$0$$$ to $$$9$$$ or as uppercase Latin letters from $$$A$$$ to $$$F$$$). Binary representation of each of these numbers denotes next $$$4$$$ elements of the matrix in the corresponding row. For example, if the number $$$B$$$ is given, then the corresponding elements are 1011, and if the number is $$$5$$$, then the corresponding elements are 0101. Elements are not separated by whitespaces.

Print one number: maximum $$$x$$$ such that an $$$x$$$-compression of the given matrix is possible.

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

immutable int INF = 10L ^^ 9 + 23;

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

    auto mx = new bool[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 0 .. n/4) {
            char c;
            readf("%s", &c);

            int v = c >= 'A' ? c - 'A' + 10 : c - '0';
            foreach (k; 0 .. 4) {
                mx[i][1 + j*4 + k] = (v & (1 << (3 - k))) > 0;
            }
        }

        readln;
    }

    debug { mx.each!writeln; }

    auto val = new int[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. n+1) {
            val[i][j] = mx[i][j] + val[i-1][j] + val[i][j-1] - val[i-1][j-1];
        }
    }

    debug { val.each!writeln; }

    bool isOk(int x) {
        int sz = x*x;
        foreach (i; 1 .. n/x + 1) {
            foreach (j; 1 .. n/x + 1) {
                int cval = val[i*x][j*x] - val[(i-1)*x][j*x] - val[i*x][(j-1)*x] + val[(i-1)*x][(j-1)*x];
                if (cval != 0 && cval != sz) { return false; }
            }
        }

        return true;
    }

    int ans = (n+1).iota[1 .. $].filter!(x => n % x == 0 && isOk(x)).array.back;

    ans.writeln;
}

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

immutable int INF = 10L ^^ 9 + 23;

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

    auto mx = new bool[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 0 .. n/4) {
            char c;
            readf("%s", &c);

            int v = c >= 'A' ? c - 'A' + 10 : c - '0';
            foreach (k; 0 .. 4) {
                mx[i][1 + j*4 + k] = (v & (1 << (3 - k))) > 0;
            }
        }

        readln;
    }

    debug { mx.each!writeln; }

    auto val = new int[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. n+1) {
            val[i][j] = mx[i][j] + val[i-1][j] + val[i][j-1] - val[i-1][j-1];
        }
    }

    debug { val.each!writeln; }

    bool isOk(int x) {
        int sz = x*x;
        foreach (i; 1 .. n/x + 1) {
            foreach (j; 1 .. n/x + 1) {
                int cval = val[i*x][j*x] - val[(i-1)*x][j*x] - val[i*x][(j-1)*x] + val[(i-1)*x][(j-1)*x];
                if (cval != 0 && cval != sz) { return false; }
            }
        }

        return true;
    }

    int ans = 1;
    foreach (m; 2 .. n+1) {
        if (n % m != 0) { continue; }
        if (isOk(m)) { ans = m; }
    }

    ans.writeln;
}

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

immutable int INF = 10L ^^ 9 + 23;

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

    auto mx = new bool[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 0 .. n/4) {
            char c;
            readf("%s", &c);

            int v = c >= 'A' ? c - 'A' + 10 : c - '0';
            foreach (k; 0 .. 4) {
                mx[i][1 + j*4 + k] = (v & (1 << (3 - k))) > 0;
            }
        }

        readln;
    }

    debug { mx.each!writeln; }

    auto val = new int[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. n+1) {
            val[i][j] = mx[i][j] + val[i-1][j] + val[i][j-1] - val[i-1][j-1];
        }
    }

    debug { val.each!writeln; }

    bool isOk(int x) {
        x = ((x+3) / 4) * 4;
        int sz = x*x;
        foreach (i; 1 .. n/x + 1) {
            foreach (j; 1 .. n/x + 1) {
                int cval = val[i*x][j*x] - val[(i-1)*x][j*x] - val[i*x][(j-1)*x] + val[(i-1)*x][(j-1)*x];
                if (cval != 0 && cval != sz) { return false; }
            }
        }

        return true;
    }

    int le = 1, r = n;
    while (le < r) {
        int m = (le + r) / 2 + 1;
        if (isOk(m)) { le = m; }
        else { r = m-1; }
    }

    le.writeln;
}

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

immutable int INF = 10L ^^ 9 + 23;

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

    auto mx = new bool[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 0 .. n/4) {
            char c;
            readf("%s", &c);

            int v = c >= 'A' ? c - 'A' + 10 : c - '0';
            foreach (k; 0 .. 4) {
                mx[i][1 + j*4 + k] = (v & (1 << (3 - k))) > 0;
            }
        }

        readln;
    }

    debug { mx.each!writeln; }

    auto val = new int[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. n+1) {
            val[i][j] = mx[i][j] + val[i-1][j] + val[i][j-1] - val[i-1][j-1];
        }
    }

    debug { val.each!writeln; }

    bool isOk(int x) {
        int sz = x*x;
        foreach (i; 1 .. n/x + 1) {
            foreach (j; 1 .. n/x + 1) {
                int cval = val[i*x][j*x] - val[(i-1)*x][j*x] - val[i*x][(j-1)*x] + val[(i-1)*x][(j-1)*x];
                if (cval != 0 && cval != sz) { return false; }
            }
        }

        return true;
    }

    int ans = 1;
    foreach (m; 2 .. n+1) {
        if (isOk(m)) { ans = m; }
    }

    ans.writeln;
}

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

immutable int INF = 10L ^^ 9 + 23;

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

    auto mx = new bool[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 0 .. n/4) {
            char c;
            readf("%s", &c);

            int v = c >= 'A' ? c - 'A' + 10 : c - '0';
            foreach (k; 0 .. 4) {
                mx[i][1 + j*4 + k] = (v & (1 << (3 - k))) > 0;
            }
        }

        readln;
    }

    debug { mx.each!writeln; }

    auto val = new int[][] (n+1, n+1);

    foreach (i; 1 .. n+1) {
        foreach (j; 1 .. n+1) {
            val[i][j] = mx[i][j] + val[i-1][j] + val[i][j-1] - val[i-1][j-1];
        }
    }

    debug { val.each!writeln; }

    bool isOk(int x) {
        int sz = x*x;
        foreach (i; 1 .. n/x + 1) {
            foreach (j; 1 .. n/x + 1) {
                int cval = val[i*x][j*x] - val[(i-1)*x][j*x] - val[i*x][(j-1)*x] + val[(i-1)*x][(j-1)*x];
                if (cval != 0 && cval != sz) { return false; }
            }
        }

        return true;
    }

    int le = 1, r = n;
    while (le < r) {
        int m = (le + r) / 2 + 1;
        if (isOk(m)) { le = m; }
        else { r = m-1; }
    }

    le.writeln;
}


There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $$$h$$$ hours, and each hour consists of $$$m$$$ minutes, where $$$m$$$ is an even number. Currently, there are $$$n$$$ freight trains, and they depart every day at the same time: $$$i$$$-th train departs at $$$h_i$$$ hours and $$$m_i$$$ minutes.The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $$$0$$$ hours and $$$t$$$ minutes, where $$$0 \le t < {m \over 2}$$$, the second tram departs $$$m \over 2$$$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.To allow passengers to board the tram safely, the tram must arrive $$$k$$$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $$$0$$$ hours and $$$t$$$ minutes, where $$$t < k$$$, then the freight trains can not depart during the last $$$k - t$$$ minutes of the day.    A schematic picture of the correct way to run passenger trams. Here $$$h=2$$$ (therefore, the number of passenger trams is $$$2h=4$$$), the number of freight trains is $$$n=6$$$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $$$k$$$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments. Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $$$t$$$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.

The first line of input contains four integers $$$n$$$, $$$h$$$, $$$m$$$, $$$k$$$ ($$$1 \le n \le 100\,000$$$, $$$1 \le h \le 10^9$$$, $$$2 \le m \le 10^9$$$, $$$m$$$ is even, $$$1 \le k \le {m \over 2}$$$) — the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram. $$$n$$$ lines follow, each contains two integers $$$h_i$$$ and $$$m_i$$$ ($$$0 \le h_i < h$$$, $$$0 \le m_i < m$$$) — the time when $$$i$$$-th freight train departs. It is guaranteed that no freight trains depart at the same time.

The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $$$t$$$. Second line of output should contain freight trains that need to be canceled.

NoteIn the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $$$t \in [1, 15]$$$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $$$t = 0$$$ or $$$t \in [16, 29]$$$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $$$t = 0$$$. Another possible option is to cancel the first train and choose $$$t = 13$$$.

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

void main ()
{
	int n, h, m, k;
	while (readf !(" %s %s %s %s") (n, h, m, k) > 0)
	{
		auto m2 = m / 2;
		auto r = new int [n];
		auto t = new int [n];
		foreach (i; 0..n)
		{
			readf !(" %s %s") (r[i], t[i]);
			t[i] %= m2;
		}
		auto p = n.iota.array;
		p.schwartzSort !(i => t[i]);
		foreach (i; 0..n)
		{
			p ~= p[i];
		}
		int best = n + 1;
		int pos = -1;
		int j = 0;
		foreach (i; 0..n)
		{
			auto cur = t[p[i]];
			while (j < n * 2)
			{
				auto next = t[p[j]] + m2 * (j >= n);
				if (next >= cur + k)
				{
					break;
				}
				j++;
			}
			if (best > j - i - 1)
			{
				best = j - i - 1;
				pos = t[p[j]];
			}
		}

		writeln (best, " ", pos);
		int [] answer;
		foreach (i; 0..n)
		{
			if ((0 < pos - t[i] && pos - t[i] < k) ||
			    (0 < pos - t[i] + m2 && pos - t[i] + m2 < k))
			{
				answer ~= i + 1;
			}
		}
		writefln !("%(%s %)") (answer);
	}
}

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

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

long n, h, m, k;

int opCmp(long[2] a, long[2] b)
{
  if (a[0] < b[0])
    return -1;
  if (a[0] > b[0])
    return +1;
  if (a[1] < b[1])
    return -1;
  if (a[1] > b[1])
    return +1;
  return 0;
}

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

void main()
{
  read(n), read(h), read(m), read(k);
  import std.container: redBlackTree, RedBlackTree;
  auto positions = redBlackTree!long();
  auto freights = new long[cast(size_t)n];
  foreach(i; 0 .. n)
    {
      long hi, mi;
      read(hi), read(mi); // realmente no importa hi??
      positions.insert(mi % (m / 2));
      freights[cast(size_t)i] = mi % (m / 2);
    }
  auto originalFreights = freights.dup;
  auto sortedFreights = sort(freights);
  import std.algorithm: min;
  long minRemoved = long.max;
  long bestPosition = -1;
  foreach(position; positions)
    {
      long r = position;
      long l = r - k;
      l = pmod(l, m / 2);
      long removed;
      if (l <= r)
        removed = sortedFreights.upperBound(l).lowerBound(r).length;
      else
        removed = sortedFreights.upperBound(l).length + sortedFreights.lowerBound(r).length;

      if (removed < minRemoved)
        {
          bestPosition = position;
          minRemoved = removed;
        }
    }
  writeln(minRemoved, " ", bestPosition);
  long written = 0;
  foreach(i, freight; originalFreights)
    {
      long r = bestPosition;
      long l = r - k;
      l = pmod(l, m / 2);
      if (l <= r)
        {
          if (freight > l && freight < r)
            {
              written++;
              write(i + 1, " ");
              continue;
            }
        }
      else
        {
          if (freight > l || freight < r)
            {
              written++;
              write(i + 1, " ");
              continue;
            }
        }
    }
  writeln;
  assert(written == minRemoved);
}


Kaavi, the mysterious fortune teller, deeply believes that one's fate is inevitable and unavoidable. Of course, she makes her living by predicting others' future. While doing divination, Kaavi believes that magic spells can provide great power for her to see the future.  Kaavi has a string $$$T$$$ of length $$$m$$$ and all the strings with the prefix $$$T$$$ are magic spells. Kaavi also has a string $$$S$$$ of length $$$n$$$ and an empty string $$$A$$$.During the divination, Kaavi needs to perform a sequence of operations. There are two different operations: Delete the first character of $$$S$$$ and add it at the front of $$$A$$$. Delete the first character of $$$S$$$ and add it at the back of $$$A$$$.Kaavi can perform no more than $$$n$$$ operations. To finish the divination, she wants to know the number of different operation sequences to make $$$A$$$ a magic spell (i.e. with the prefix $$$T$$$). As her assistant, can you help her? The answer might be huge, so Kaavi only needs to know the answer modulo $$$998\,244\,353$$$.Two operation sequences are considered different if they are different in length or there exists an $$$i$$$ that their $$$i$$$-th operation is different. A substring is a contiguous sequence of characters within a string. A prefix of a string $$$S$$$ is a substring of $$$S$$$ that occurs at the beginning of $$$S$$$.

The first line contains a string $$$S$$$ of length $$$n$$$ ($$$1 \leq n \leq 3000$$$). The second line contains a string $$$T$$$ of length $$$m$$$ ($$$1 \leq m \leq n$$$). Both strings contain only lowercase Latin letters.

The output contains only one integer  — the answer modulo $$$998\,244\,353$$$.

NoteThe first test:The red ones are the magic spells. In the first operation, Kaavi can either add the first character "a" at the front or the back of $$$A$$$, although the results are the same, they are considered as different operations. So the answer is $$$6\times2=12$$$.

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

immutable int mod = 998_244_353;

void main ()
{
	string s;
	while ((s = readln.strip ()) != "")
	{
		auto t = readln.strip;
		auto n = s.length.to !(int);
		auto m = t.length.to !(int);
		auto f = new int [] [] (n + 1, n + 1);
		foreach (i; 0..n + 1)
		{
			f[i][i] = 1;
		}
		foreach (len; 0..n)
		{
			auto letter = s[len];
			foreach (start; 0..n + 1 - len)
			{
				auto finish = start + len;
				auto cur = f[start][finish];
				if (start > 0 &&
				    (start - 1 >= m || letter == t[start - 1]))
				{
					f[start - 1][finish] += cur;
					if (f[start - 1][finish] >= mod)
					{
						f[start - 1][finish] -= mod;
					}
				}
				if (finish < n &&
				    (finish >= m || letter == t[finish]))
				{
					f[start][finish + 1] += cur;
					if (f[start][finish + 1] >= mod)
					{
						f[start][finish + 1] -= mod;
					}
				}
			}
		}
		writeln (sum (f[0][m..$], 0L) % mod);
	}
}

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

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

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const S = readToken();
      const T = readToken();
      const L = cast(int)(S.length);
      const M = cast(int)(T.length);

      auto dp = new Mint[][](L + 1, L + 1);
      foreach (j; M .. L + 1) {
        dp[0][j] += 1;
      }
      foreach_reverse (w; 1 .. L + 1) {
        foreach (i; 0 .. L - w + 1) {
          const j = i + w;
          // front
          if (i >= M || S[w - 1] == T[i]) {
            dp[i + 1][j] += dp[i][j];
          }
          // back
          if (j - 1 >= M || S[w - 1] == T[j - 1]) {
            dp[i][j - 1] += dp[i][j];
          }
        }
      }
      Mint ans;
      foreach (i; 0 .. L + 1) {
        ans += dp[i][i];
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


Alice and Bob are playing a game. Initially, they are given a non-empty string $$$s$$$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty.Alice starts, then they alternate moves. In one move, a player takes either the first or the last letter of the string $$$s$$$, removes it from $$$s$$$ and prepends (adds to the beginning) it to their own string.The game ends when the string $$$s$$$ becomes empty. The winner is the player with a lexicographically smaller string. If the players' strings are equal, then it's a draw.A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if there exists such position $$$i$$$ that $$$a_j = b_j$$$ for all $$$j < i$$$ and $$$a_i < b_i$$$.What is the result of the game if both players play optimally (e. g. both players try to win; if they can't, then try to draw)?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Each testcase consists of a single line — a non-empty string $$$s$$$, consisting of lowercase Latin letters. The length of the string $$$s$$$ is even. The total length of the strings over all testcases doesn't exceed $$$2000$$$.

For each testcase, print the result of the game if both players play optimally. If Alice wins, print "Alice". If Bob wins, print "Bob". If it's a draw, print "Draw".

NoteOne of the possible games Alice and Bob can play in the first testcase:   Alice picks the first letter in $$$s$$$: $$$s=$$$"orces", $$$a=$$$"f", $$$b=$$$"";  Bob picks the last letter in $$$s$$$: $$$s=$$$"orce", $$$a=$$$"f", $$$b=$$$"s";  Alice picks the last letter in $$$s$$$: $$$s=$$$"orc", $$$a=$$$"ef", $$$b=$$$"s";  Bob picks the first letter in $$$s$$$: $$$s=$$$"rc", $$$a=$$$"ef", $$$b=$$$"os";  Alice picks the last letter in $$$s$$$: $$$s=$$$"r", $$$a=$$$"cef", $$$b=$$$"os";  Bob picks the remaining letter in $$$s$$$: $$$s=$$$"", $$$a=$$$"cef", $$$b=$$$"ros". Alice wins because "cef" < "ros". Neither of the players follows any strategy in this particular example game, so it doesn't show that Alice wins if both play optimally.

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

void solve() {
    string S = readln.chomp;
    int N = S.length.to!int;

    auto cache = new int[][][][](2, 2, N, N+1);
    foreach (i; 0 .. 2) {
        foreach (j; 0 .. 2) {
            foreach (k; 0 .. N) {
                cache[i][j][k][] = 5;
            }
        }
    }
    int f(int p, int last, int l, int r) {
        // (+1: alice, -1: bob) wins when the opponent drew from [left(last=0) or right(last=1)], current string is S[l, r)

        if (l == r) {
            if (p == 0) return 0; // Draw
            return -1; // Bob wins
        }

        //writeln([p, last, l, r]);
        auto c = &cache[p][last][l][r];
        if (*c != 5) return *c;

        if (p == 0) {
            int res0 = f(!p, 0, l+1, r);
            int res1 = f(!p, 1, l, r-1);
            if (res0 == 1 || res1 == 1) return *c = 1;
            if (res0 == 0 || res1 == 0) return *c = 0;
            return *c = -1;
        } else {
            char op_hand = (last == 0 ? S[l - 1] : S[r]);
            int res0 = f(!p, 0, l+1, r);
            int res1 = f(!p, 1, l, r-1);
            if (res0 == -1 || res1 == -1) return *c = -1;
            if (res0 == 0 && S[l] < op_hand) return *c = -1;
            if (res1 == 0 && S[r-1] < op_hand) return *c = -1;
            if (res0 == 0 && S[l] == op_hand) return *c = 0;
            if (res1 == 0 && S[r-1] == op_hand) return *c = 0;
            return *c = 1;
        }
    }

    auto ns = ["Bob", "Draw", "Alice"];
    ns[f(0, 0, 0, N) + 1].writeln;
}

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

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

void solve() {
    string S = readln.chomp;
    int N = S.length.to!int;

    auto cache = new int[][][][](2, 2, N, N+1);
    foreach (i; 0 .. 2) {
        foreach (j; 0 .. 2) {
            foreach (k; 0 .. N) {
                cache[i][j][k][] = 5;
            }
        }
    }
    int f(int p, int last, int l, int r) {
        // (+1: alice, -1: bob) wins when the opponent drew from [left(last=0) or right(last=1)], current string is S[l, r)

        if (l == r) {
            if (p == 0) return 0; // Draw
            return -1; // Bob wins
        }

        //writeln([p, last, l, r]);
        auto c = &cache[p][last][l][r];
        if (*c != 5) return *c;

        if (p == 0) {
            int res0 = f(!p, 0, l+1, r);
            int res1 = f(!p, 1, l, r-1);
            if (res0 == 1 || res1 == 1) return *c = 1;
            if (res0 == 0 || res1 == 0) return *c = 0;
            return *c = -1;
        } else {
            char op_hand = (last == 0 ? S[l - 1] : S[r]);
            if (S[l] < op_hand || S[r-1] < op_hand) {
                return *c = -1;
            } else {
                int result = 1;
                if (S[l] == op_hand) {
                    int res = f(!p, 0, l + 1, r);
                    if (res == -1) {
                        return *c = -1;
                    } else if (res == 0) {
                        result = 0;
                    }
                }
                if (S[r-1] == op_hand) {
                    int res = f(!p, 1, l, r - 1);
                    if (res == -1) {
                        return *c = -1;
                    } else if (res == 0) {
                        result = 0;
                    }
                }
                return *c = result;
            }
        }
    }

    auto ns = ["Bob", "Draw", "Alice"];
    ns[f(0, 0, 0, N) + 1].writeln;
}

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

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

void solve() {
    string S = readln.chomp;
    int N = S.length.to!int;

    auto cache = new int[][][][](2, 2, N, N+1);
    foreach (i; 0 .. 2) {
        foreach (j; 0 .. 2) {
            foreach (k; 0 .. N) {
                cache[i][j][k][] = 5;
            }
        }
    }
    int f(int p, int last, int l, int r) {
        // (+1: alice, -1: bob) wins when the opponent drew from [left(last=0) or right(last=1)], current string is S[l, r)

        if (l == r) {
            if (p == 0) return 0; // Draw
            return -1; // Bob wins
        }

        //writeln([p, last, l, r]);
        auto c = &cache[p][last][l][r];
        if (*c != 5) return *c;

        if (p == 0) {
            int res0 = f(!p, 0, l+1, r);
            int res1 = f(!p, 1, l, r-1);
            if (res0 == 1 || res1 == 1) return *c = 1;
            if (res0 == 0 || res1 == 0) return *c = 0;
            return *c = -1;
        } else {
            char op_hand = (last == 0 ? S[l - 1] : S[r]);
            if (S[l] < op_hand || S[r-1] < op_hand) {
                return *c = -1;
            } else {
                int result = 1;
                if (S[l] == op_hand) {
                    int res = !f(!p, 0, l + 1, r);
                    if (res == -1) {
                        return *c = -1;
                    } else if (res == 0) {
                        result = 0;
                    }
                }
                if (S[r-1] == op_hand) {
                    int res = !f(!p, 1, l, r - 1);
                    if (res == -1) {
                        return *c = -1;
                    } else if (res == 0) {
                        result = 0;
                    }
                }
                return *c = result;
            }
        }
    }

    auto ns = ["Bob", "Draw", "Alice"];
    ns[f(0, 0, 0, N) + 1].writeln;
}

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

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

void solve() {
    string S = readln.chomp;
    int N = S.length.to!int;

    auto cache = new int[][][][](2, 2, N, N+1);
    foreach (i; 0 .. 2) {
        foreach (j; 0 .. 2) {
            foreach (k; 0 .. N) {
                cache[i][j][k][] = 5;
            }
        }
    }
    int f(int p, int last, int l, int r) {
        // (+1: alice, -1: bob) wins when the opponent drew from [left(last=0) or right(last=1)], current string is S[l, r)

        if (l == r) {
            if (p == 0) return 0; // Draw
            return 1; // Bob wins
        }

        //writeln([p, last, l, r]);
        auto c = &cache[p][last][l][r];
        if (*c != 5) return *c;

        if (p == 0) {
            int res0 = f(!p, 0, l+1, r);
            int res1 = f(!p, 1, l, r-1);
            if (res0 == 1 || res1 == 1) return *c = 1;
            if (res0 == 0 || res1 == 0) return *c = 0;
            return *c = -1;
        } else {
            char op_hand = (last == 0 ? S[l - 1] : S[r]);
            if (S[l] < op_hand || S[r-1] < op_hand) {
                return *c = -1;
            } else {
                int result = 1;
                if (S[l] == op_hand) {
                    int res = !f(!p, 0, l + 1, r);
                    if (res == -1) {
                        return *c = -1;
                    } else if (res == 0) {
                        result = 0;
                    }
                }
                if (S[r-1] == op_hand) {
                    int res = !f(!p, 1, l, r - 1);
                    if (res == -1) {
                        return *c = -1;
                    } else if (res == 0) {
                        result = 0;
                    }
                }
                return *c = result;
            }
        }
    }

    auto ns = ["Bob", "Draw", "Alice"];
    ns[f(0, 0, 0, N) + 1].writeln;
}

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