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

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

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

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

// xorshift
uint xrand() {
  static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288;
  uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}

// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b; a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) y = -y;
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a; b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) x = -x;
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}

// (a / 2) mod m
long div2(long a, long m)
in {
  assert(1 <= m && m < 1L << 62, "div2: 1 <= m < 2^62 must hold");
  assert(m % 2 != 0, "div2: m must not be divisibly by 3");
  assert(0 <= a && a < m, "div2: 0 <= a < m must hold");
}
do {
  return (a + (a % 2) * m) / 2;
}

// (a / 3) mod m
long div3(long a, long m)
in {
  assert(1 <= m && m < 1L << 61, "div3: 1 <= m < 2^61 must hold");
  assert(m % 3 != 0, "div3: m must not be divisibly by 3");
  assert(0 <= a && a < m, "div3: 0 <= a < m must hold");
}
do {
  return (a + (((3 - a % 3) * (m % 3)) % 3) * m) / 3;
}

// Jacobi symbol (a/m)
int jacobi(long a, long m)
in {
  assert(m > 0, "jacobi: m > 0 must hold");
  assert(m & 1, "jacobi: m must be odd");
}
do {
  import core.bitop : bsf;
  import std.algorithm.mutation : swap;
  int s = 1;
  if (a < 0) a = a % m + m;
  for (; m > 1; ) {
    a %= m;
    if (a == 0) return 0;
    const r = bsf(a);
    if ((r & 1) && ((m + 2) & 4)) s = -s;
    a >>= r;
    if (a & m & 2) s = -s;
    swap(a, m);
  }
  return s;
}

// sqrt(a) (mod p)
//   p: be prime
//   (b + sqrt(b^2 - a))^((p+1)/2) in F_p(sqrt(b^2 - a))
long[] modSqrt(long a, long p)
in {
  assert(p < 1L << 31, "modSqrt: p < 2^31 must hold");
  assert(-p * p <= a && a <= p * p, "modSqrt: -p^2 <= a <= p^2 must hold");
}
do {
  if (p == 2) return [a & 1];
  const j = jacobi(a, p);
  if (j == 0) return [0];
  if (j == -1) return [];
  long b, d;
  for (; ; ) {
    b = xrand() % p;
    d = (b * b - a) % p;
    if (d < 0) d += p;
    if (jacobi(d, p) == -1) break;
  }
  long[2] multiply(in long[2] x, in long[2] y) {
    return [(x[0] * y[0] + d * ((x[1] * y[1]) % p)) % p,
            (x[0] * y[1] + x[1] * y[0]) % p];
  }
  long[2] f = [b, 1], g = [1, 0];
  for (long e = (p + 1) >> 1; e; e >>= 1) {
    if (e & 1) g = multiply(g, f);
    f = multiply(f, f);
  }
  assert(g[1] == 0);
  return (g[0] < p - g[0]) ? [g[0], p - g[0]] : [p - g[0], g[0]];
}

// Roots of f0 + f1 T + T^2 in F_p[T] with multiplicity
//   p: prime
long[] modRoots2(long f0, long f1, long p)
in {
  assert(2 <= p && p < 1L << 31, "modRoots2: 2 <= p < 2^31 must hold");
  assert(0 <= f0 && f0 < p, "modRoots2: 0 <= f0 < p must hold");
  assert(0 <= f1 && f1 < p, "modRoots2: 0 <= f1 < p must hold");
}
do {
  import std.algorithm : sort;
  if (p == 2) {
    if (f0 == 0 && f1 == 0) return [0, 0];
    if (f0 == 0 && f1 == 1) return [0, 1];
    if (f0 == 1 && f1 == 0) return [1, 1];
    return [];
  } else {
    const f12 = f1.div2(p);
    auto ts = modSqrt(f12 * f12 - f0, p);
    foreach (ref t; ts) {
      if ((t -= f12) < 0) t += p;
    }
    sort(ts);
    switch (ts.length) {
      case 0: return [];
      case 1: return [ts[0], ts[0]];
      case 2: return ts;
      default: assert(false);
    }
  }
}

// Roots of f0 + f1 T + f2 T^2 + T^3 in F_p[T] with multiplicity
//   p: prime
long[] modRoots3(long f0, long f1, long f2, long p)
in {
  assert(2 <= p && p <= 1_500_000_001,
         "modRoots3: 2 <= p <= 1_500_000_001 must hold");
  assert(0 <= f0 && f0 < p, "modRoots3: 0 <= f0 < p must hold");
  assert(0 <= f1 && f1 < p, "modRoots3: 0 <= f1 < p must hold");
  assert(0 <= f2 && f2 < p, "modRoots3: 0 <= f2 < p must hold");
}
do {
  import std.algorithm : sort;
  if (p == 2) {
    if (f0 == 0 && f1 == 0 && f2 == 0) return [0, 0, 0];
    if (f0 == 0 && f1 == 0 && f2 == 1) return [0, 0, 1];
    if (f0 == 0 && f1 == 1 && f2 == 0) return [0, 1, 1];
    if (f0 == 1 && f1 == 1 && f2 == 1) return [1, 1, 1];
    if (f0 == 0) return [0];
    if ((f0 + f1 + f2 + 1) % 2 == 0) return [1];
    return [];
  } else if (p == 3) {
    if (f0 == 0 && f1 == 0 && f2 == 0) return [0, 0, 0];
    if (f0 == 0 && f1 == 0 && f2 == 2) return [0, 0, 1];
    if (f0 == 0 && f1 == 0 && f2 == 1) return [0, 0, 2];
    if (f0 == 0 && f1 == 1 && f2 == 1) return [0, 1, 1];
    if (f0 == 0 && f1 == 2 && f2 == 0) return [0, 1, 2];
    if (f0 == 0 && f1 == 1 && f2 == 2) return [0, 2, 2];
    if (f0 == 2 && f1 == 0 && f2 == 0) return [1, 1, 1];
    if (f0 == 1 && f1 == 2 && f2 == 2) return [1, 1, 2];
    if (f0 == 2 && f1 == 2 && f2 == 1) return [1, 2, 2];
    if (f0 == 1 && f1 == 0 && f2 == 0) return [2, 2, 2];
    if (f0 == 0) return [0];
    if ((f0 + f1 + f2 + 1) % 3 == 0) return [1];
    if ((f0 - f1 + f2 - 1) % 3 == 0) return [2];
    return [];
  } else {
    // gcd(f0 + f1 T + f2 T^2 + T^3, f1 + 2 f2 T + 3 T^2)
    //   remainder: (f0 - f1 f2 / 9) + (2 f1 / 3 - 2 f2^2 / 9) T
    const f13 = f1.div3(p), f23 = f2.div3(p);
    long a0 = (f0 - f13 * f23) % p, a1 = (2 * f13 - 2 * f23 * f23) % p;
    if (a0 < 0) a0 += p;
    if (a1 < 0) a1 += p;
    if (a1 != 0) {
      if ((f13 * ((a1 * a1) % p) - 2 * f23 * ((a0 * a1) % p) + a0 * a0) % p != 0) {
        // gcd = 1
      } else {
        // gcd = a0 + a1 T
        const t2 = ((p - a0) * a1.modInv(p)) % p;
        long t1 = (-f2 - 2 * t2) % p;
        if (t1 < 0) t1 += p;
        auto ts = [t1, t2, t2];
        sort(ts);
        return ts;
      }
    } else if (a0 != 0) {
      // gcd = 1
    } else {
      // gcd = f1 + 2 f2 T + 3 T^2
      const t = (f23 != 0) ? (p - f23) : 0;
      return [t, t, t];
    }

    long[3] mul(in long[3] a, in long[3] b) {
      long[3] c = [a[0] * b[0], a[0] * b[1] + a[1] * b[0],
                a[0] * b[2] + a[1] * b[1] + a[2] * b[0]];
      long c3 = a[1] * b[2] + a[2] * b[1], c4 = a[2] * b[2];
      c4 %= p;
      c[1] -= f0 * c4; c[2] -= f1 * c4; c3 -= f2 * c4;
      c3 %= p;
      c[0] -= f0 * c3; c[1] -= f1 * c3; c[2] -= f2 * c3;
      if ((c[0] %= p) < 0) c[0] += p;
      if ((c[1] %= p) < 0) c[1] += p;
      if ((c[2] %= p) < 0) c[2] += p;
      return c;
    }

    // f: square-free
    // gcd(f, T^p - T)
    long[3] t2 = [0, 1, 0], tp = [1, 0, 0];
    for (long e = p; e; e >>= 1) {
      if (e & 1) tp = mul(tp, t2);
      t2 = mul(t2, t2);
    }
    if (--tp[1] < 0) tp[1] += p;
    if (tp[2] != 0) {
      const invTp2 = tp[2].modInv(p);
      (tp[0] *= invTp2) %= p;
      (tp[1] *= invTp2) %= p;
      long b0 = (f0 - (f2 - tp[1]) * tp[0]) % p;
      long b1 = (f1 - tp[0] - (f2 - tp[1]) * tp[1]) % p;
      if (b0 < 0) b0 += p;
      if (b1 < 0) b1 += p;
      if (b1 != 0) {
        if ((tp[0] * ((b1 * b1) % p) - tp[1] * ((b0 * b1) % p) + b0 * b0) % p != 0) {
          // gcd = 1
          return [];
        } else {
          // gcd = b0 + b1 T
          return [((p - b0) * b1.modInv(p)) % p];
        }
      } else if (b0 != 0) {
        // gcd = 1
        return [];
      } else {
        // gcd = tp[0] + tp[1] T + T^2
        assert(false);
      }
    } else if (tp[1] != 0) {
      const tp02 = (tp[0] * tp[0]) % p, tp12 = (tp[1] * tp[1]) % p;
      if ((((f0 * tp[1] - f1 * tp[0]) % p) * tp12 +
           ((f2 * tp[1] - tp[0]) % p) * tp02) % p != 0) {
        // gcd = 1
        return [];
      } else {
        // gcd = tp[0] + tp[1] T
        return [((p - tp[0]) * tp[1].modInv(p)) % p];
      }
    } else if (tp[0] != 0) {
      // gcd = 1
      return [];
    } else {
      // gcd = f0 + f1 T + f2 T^2 + T^3
    }

    // f: split
    for (; ; ) {
      // gcd(rand^((p-1)/2) - 1, f)
      long[3] r2 = [xrand() % p, xrand() % p, xrand() % p], rp = [1, 0, 0];
      for (long e = (p - 1) / 2; e; e >>= 1) {
        if (e & 1) rp = mul(rp, r2);
        r2 = mul(r2, r2);
      }
      if (--rp[0] < 0) rp[0] += p;
      if (rp[2] != 0) {
        const invRp2 = rp[2].modInv(p);
        (rp[0] *= invRp2) %= p;
        (rp[1] *= invRp2) %= p;
        long c0 = (f0 - (f2 - rp[1]) * rp[0]) % p;
        long c1 = (f1 - rp[0] - (f2 - rp[1]) * rp[1]) % p;
        if (c0 < 0) c0 += p;
        if (c1 < 0) c1 += p;
        if (c1 != 0) {
          if ((rp[0] * ((c1 * c1) % p) - rp[1] * ((c0 * c1) % p) + c0 * c0) % p != 0) {
            // gcd = 1
          } else {
            // gcd = c0 + c1 T
            const t = ((p - c0) * c1.modInv(p)) % p;
            auto ts = [t] ~ modRoots2((f1 + (f2 + t) * t) % p, (f2 + t) % p, p);
            sort(ts);
            return ts;
          }
        } else if (c0 != 0) {
          // gcd = 1
        } else {
          // gcd = rp[0] + rp[1] T + T^2
          auto ts = modRoots2(rp[0], rp[1], p) ~ [(rp[1] - f2 + p) % p];
          sort(ts);
          return ts;
        }
      } else if (rp[1] != 0) {
        const rp02 = (rp[0] * rp[0]) % p, rp12 = (rp[1] * rp[1]) % p;
        if ((((f0 * rp[1] - f1 * rp[0]) % p) * rp12 +
             ((f2 * rp[1] - rp[0]) % p) * rp02) % p != 0) {
          // gcd = 1
        } else {
          // gcd = rp[0] + rp[1] T
          const t = ((p - rp[0]) * rp[1].modInv(p)) % p;
          auto ts = [t] ~ modRoots2((f1 + (f2 + t) * t) % p, (f2 + t) % p, p);
          sort(ts);
          return ts;
        }
      } else if (rp[0] != 0) {
        // gcd = 1
      } else {
        // gcd = f0 + f1 T + f2 T^2 + T^3
      }
    }
  }
}

int N;
long P, K;
long[] A;

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

      long[long] cnt;
      foreach (a; A) {
        ++cnt[a];
      }

      long ans;
      foreach (a; A) {
        /*
          (A[i] + T) (A[i]^2 + T^2) = k
        */
        const aa = (a * a) % P;
        const res = modRoots3((a * aa - K + P) % P, aa, a, P);
        debug {
          writeln(a, ": ", (a * aa - K + P) % P, " ", aa, " ", a, " ", res);
        }
        foreach (t; res.uniq) {
          if (a < t) {
            if (t in cnt) {
              ans += cnt[t];
            }
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

int N;
long P, K;
long[] A;

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

      auto fs = new long[N];
      foreach (i; 0 .. N) {
        long a2 = (A[i] * A[i]) % P;
        fs[i] = ((a2 * a2 - K * A[i]) % P + P) % P;
      }
      fs.sort;

      long ans;
      for (int i = 0, j; i < N; i = j) {
        for (j = i; j < N && fs[i] == fs[j]; ++j) {}
        ans += (j - i) * (j - i - 1) / 2;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


import core.bitop;
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n;
	int k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		k = min (k, n - k);
		int me = 1;
		while (me < n * 2)
		{
			me <<= 1;
		}
		int hi = me << 1;
		auto t = new long [hi];

		void add (int p, long value)
		{
			for (p += me; p > 0; p >>= 1)
			{
				t[p] += value;
			}
		}

		long seg (int lo, int hi)
		{
			long res = 0;
			for (lo += me, hi += me; lo < hi; lo >>= 1, hi >>= 1)
			{
				if (lo & 1)
				{
					res += t[lo];
					lo += 1;
				}
				if (hi & 1)
				{
					hi -= 1;
					res += t[hi];
				}
			}
			return res;
		}

		long res = 1;
		int cur = 0;
		foreach (i; 0..n)
		{
			int next = cur + k;
			res += 1 + seg (cur + 1, next);
			write (res, "\n "[i + 1 < n]);

			if (next >= n)
			{
				next -= n;
			}
			add (cur, 1);
			add (next, 1);
			add (cur + n, 1);
			add (next + n, 1);
			cur = next;
		}
		debug {stderr.writeln (res);}
	}
}

import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;

immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);

void main(){
    int N, K;
    readVars(N, K);

    K = min(K, N - K);

    int s, t;
    long d = 1;
    long res = 1;

    foreach(i ; 0 .. N){
        t = (s + K) % N;

        if (s < t || t == 0) {
            res += d;
        } else {
            d++;
            res += d;
            d++;
        }

        write(res, i != N - 1 ? ' ' : '\n');

        s = t;
    }
}

class FenwickTree(T){
private:
    T[] data;
    int N;

public:
    this(T[] a){
        data = [cast(T)0] ~ a[];
        N = a.length.to!int;

        foreach (i ; 1 .. N + 1) {
            if (i + (i & (-i)) <= N) {
                data[i + (i & (-i))] += data[i];
            }
        }
    }

    void add(int i, T x) {
        i++;

        while (i <= N) {
            data[i] += x;
            i += i & (-i);
        }
    }

    T getSum(int r) {
        T res;

        while(r > 0){
            res += data[r];
            r -= r & (-r);
        }

        return res;
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        throw new Exception("args num < input num");
    }
}

import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;

immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);

void main(){
    int N, K;
    readVars(N, K);

    K = min(K, N - K);

    int s, t;

    auto a = new long[](N);
    auto ft = new FenwickTree!(long)(a);
    long res = 1;

    foreach(i ; 0 .. N){
        t = (s + K) % N;

        if(s < t){
            res += ft.getSum(t) - ft.getSum(s + 1) + 1;
        } else {
            res += ft.getSum(N) - ft.getSum(s + 1);
            res += ft.getSum(t) + 1;
        }

        write(res, i == N - 1 ? '\n' : ' ');

        ft.add(s, 1);
        ft.add(t, 1);

        s = t;
    }

}

class FenwickTree(T){
private:
    T[] data;
    int N;

public:
    this(T[] a){
        data = [cast(T)0] ~ a[];
        N = a.length.to!int;

        foreach (i ; 1 .. N + 1) {
            if (i + (i & (-i)) <= N) {
                data[i + (i & (-i))] += data[i];
            }
        }
    }

    void add(int i, T x) {
        i++;

        while (i <= N) {
            data[i] += x;
            i += i & (-i);
        }
    }

    T getSum(int r) {
        T res;

        while(r > 0){
            res += data[r];
            r -= r & (-r);
        }

        return res;
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        throw new Exception("args num < input num");
    }
}

import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;

immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);

void main(){
    int N, K;
    readVars(N, K);

    K = min(K, N - K);

    int s, t;
    long d = 1;
    long res = 1;
    //auto ans = new long[](N);

    foreach(i ; 0 .. N){
        t = (s + K) % N;

        if (s < t || t == 0) {
            res += d;
        } else {
            d++;
            res += d;
            d++;
        }

        //ans[i] = res;
        write(res, ' ');

        s = t;
    }
    writeln();

    //writefln("%(%s %)", ans);
}

class FenwickTree(T){
private:
    T[] data;
    int N;

public:
    this(T[] a){
        data = [cast(T)0] ~ a[];
        N = a.length.to!int;

        foreach (i ; 1 .. N + 1) {
            if (i + (i & (-i)) <= N) {
                data[i + (i & (-i))] += data[i];
            }
        }
    }

    void add(int i, T x) {
        i++;

        while (i <= N) {
            data[i] += x;
            i += i & (-i);
        }
    }

    T getSum(int r) {
        T res;

        while(r > 0){
            res += data[r];
            r -= r & (-r);
        }

        return res;
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        throw new Exception("args num < input num");
    }
}

import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;

immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);

void main(){
    int N, K;
    readVars(N, K);

    K = min(K, N - K);

    int s, t;
    long d = 1;
    long res = 1;
    auto ans = new long[](N);

    foreach(i ; 0 .. N){
        t = (s + K) % N;

        if (s < t || t == 0) {
            res += d;
        } else {
            d++;
            res += d;
            d++;
        }

        ans[i] = res;

        s = t;
    }

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

class FenwickTree(T){
private:
    T[] data;
    int N;

public:
    this(T[] a){
        data = [cast(T)0] ~ a[];
        N = a.length.to!int;

        foreach (i ; 1 .. N + 1) {
            if (i + (i & (-i)) <= N) {
                data[i + (i & (-i))] += data[i];
            }
        }
    }

    void add(int i, T x) {
        i++;

        while (i <= N) {
            data[i] += x;
            i += i & (-i);
        }
    }

    T getSum(int r) {
        T res;

        while(r > 0){
            res += data[r];
            r -= r & (-r);
        }

        return res;
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        throw new Exception("args num < input num");
    }
}

import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;

immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);

void main(){
    int N, K;
    readVars(N, K);

    K = min(K, N - K);

    int s, t;

    auto a = new long[](N);
    auto ft = new FenwickTree!(long)(a);
    auto ans = new long[](N);
    long res = 1;

    foreach(i ; 0 .. N){
        t = (s + K) % N;

        if(s < t){
            res += ft.getSum(t) - ft.getSum(s + 1) + 1;
            ans[i] = res;
        } else {
            res += ft.getSum(N) - ft.getSum(s + 1);
            res += ft.getSum(t) + 1;
            ans[i] = res;
        }

        ft.add(s, 1);
        ft.add(t, 1);

        s = t;
    }

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

class FenwickTree(T){
private:
    T[] data;
    int N;

public:
    this(T[] a){
        data = [cast(T)0] ~ a[];
        N = a.length.to!int;

        foreach (i ; 1 .. N + 1) {
            if (i + (i & (-i)) <= N) {
                data[i + (i & (-i))] += data[i];
            }
        }
    }

    void add(int i, T x) {
        i++;

        while (i <= N) {
            data[i] += x;
            i += i & (-i);
        }
    }

    T getSum(int r) {
        T res;

        while(r > 0){
            res += data[r];
            r -= r & (-r);
        }

        return res;
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        throw new Exception("args num < input num");
    }
}

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

__gshared:

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

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

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

struct FenwickTree(int n) {
    int[n] t;

    void init() {
        memset(t.ptr, 0x00, t.sizeof);
    }

    void inc(int i) {
        for (; i < n; i |= i + 1)
            t[i]++;
    }

    int get(int right) {
        int result = 0;
        for (; right; right &= right - 1)
            result += t[right - 1];
        return result;
    }
}

FenwickTree!1_000_000 fwt;

void main() {
    int n, k;
    while (read(&n, &k)) {
        k = min(k, n - k);
        version (LocalProject)
            fwt.init();
        long result = 1;
        int cur = 0;
        foreach (i; 0 .. n) {
            int next = cur + k;
            int count;
            if (next <= n)
                count = fwt.get(next) - fwt.get(cur);
            else {
                next -= n;
                count = i - fwt.get(cur) + fwt.get(next);
                result--;
            }
            result += (count << 1) + 1;
            fwt.inc(cur);
            cur = next;
            write(result, ' ');
        }
        writeln();
    }
}

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

__gshared:

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

T[ ] allocate(T)(size_t n) {
    return (cast(T*)malloc(n * T.sizeof))[0 .. n];
}

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

struct FenwickTree(int n) {
    int[n] t;

    void init() {
        memset(t.ptr, 0x00, t.sizeof);
    }

    void inc(int i) {
        for (; i < n; i |= i + 1)
            t[i]++;
    }

    int get(int right) {
        int result = 0;
        for (; right; right &= right - 1)
            result += t[right - 1];
        return result;
    }
}

FenwickTree!1_000_000 fwt;

void main() {
    int n, k;
    while (read(&n, &k)) {
        version (LocalProject)
            fwt.init();
        long result = 1;
        int cur = 0;
        foreach (i; 0 .. n) {
            int next = cur + k;
            int count;
            if (next <= n)
                count = fwt.get(next) - fwt.get(cur);
            else {
                next -= n;
                count = i - fwt.get(cur) + fwt.get(next);
                result--;
            }
            result += (count << 1) + 1;
            fwt.inc(cur);
            cur = next;
            write(result, ' ');
        }
        writeln();
    }
}

import std.stdio, std.string, std.conv, std.array, std.algorithm;
import std.uni, std.range, std.math, std.container, std.datetime;
import core.bitop, std.typetuple, std.typecons;

immutable long MOD = 1_000_000_007;
alias tie = TypeTuple;
alias triplet = Tuple!(int, int, int);

void main(){
    int N, K;
    readVars(N, K);

    K = min(K, N - K);

    int s, t;

    auto a = new int[](N);
    auto ft = new FenwickTree!(int)(a);
    auto ans = new int[](N);
    int res = 1;

    foreach(i ; 0 .. N){
        t = (s + K) % N;

        if(s < t){
            res += ft.getSum(t) - ft.getSum(s + 1) + 1;
            ans[i] = res;
        } else {
            res += ft.getSum(N) - ft.getSum(s + 1);
            res += ft.getSum(t) + 1;
            ans[i] = res;
        }

        ft.add(s, 1);
        ft.add(t, 1);

        s = t;
    }

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

class FenwickTree(T){
private:
    T[] data;
    int N;

public:
    this(T[] a){
        data = [0] ~ a[];
        N = a.length.to!int;

        foreach (i ; 1 .. N + 1) {
            if (i + (i & (-i)) <= N) {
                data[i + (i & (-i))] += data[i];
            }
        }
    }

    void add(int i, T x) {
        i++;

        while (i <= N) {
            data[i] += x;
            i += i & (-i);
        }
    }

    T getSum(int r) {
        T res;

        while(r > 0){
            res += data[r];
            r -= r & (-r);
        }

        return res;
    }

    void print(){
        stderr.writefln("%(%s %)", data);
    }
}

void readVars(T...)(auto ref T args){
    auto line = readln.split;
    foreach(ref arg ; args){
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    if(!line.empty){
        throw new Exception("args num < input num");
    }
}


import std.stdio;

void main()
{
	int n, a, b;

	readf("%d %d %d", &n, &a, &b);

	int oddSeats = (a * b + 1) / 2;
	int evenSeats = a * b - oddSeats;

	int oddMembers = (n + 1) / 2;
	int evenMembers = n - oddMembers;

	if (oddMembers > oddSeats || evenMembers > evenSeats) {
		writef("-1\n");
	}
	else {
		int id = 1;
		int[][] result = new int[][](a, b);

		for (int i = 0; i < a; i++) {
			int from = 0;
			int delta = 1;
			if (i > 0 && result[i - 1][0] % 2 == id % 2) {
				from = b - 1;
				delta = -1;
			}
			for (int j = from; j != b && j != -1; j += delta) {
				if (id % 2 == 1 && oddMembers > 0) {
					result[i][j] = id;
					oddMembers--;
					id++;
				}
				else if (id % 2 == 0 && evenMembers > 0) {
					result[i][j] = id;
					evenMembers--;
					id++;
				}
			}
		}

		writef("%(%(%d %)\n%)\n", result);
	}
}

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

void main ()
{
	int n, a, b;
	while (readf (" %s %s %s", &n, &a, &b) > 0)
	{
		if (n <= a * b)
		{
			auto s = iota (1, n + 1).array;
			s.length = a * b;
			auto t = s.chunks (b).array;
			if (b % 2 == 0)
			{
				foreach (i, ref r; t)
				{
					if (i % 2 == 1)
					{
						reverse (r);
					}
				}
			}
			writefln ("%(%(%s %)\n%)", s.chunks (b));
		}
		else
		{
			writeln (-1);
		}
	}
}

import std.stdio;

void main()
{
	int n, a, b;

	readf("%d %d %d", &n, &a, &b);

	int oddSeats = (a * b + 1) / 2;
	int evenSeats = a * b - oddSeats;

	int oddMembers = (n + 1) / 2;
	int evenMembers = n - oddMembers;

	if (oddMembers > oddSeats || evenMembers > evenSeats) {
		writef("-1\n");
	}
	else {
		int id = 1;
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				if (id % 2 == 1 && oddMembers > 0) {
					writef("%d", id);
					oddMembers--;
					id++;
				}
				else if (id % 2 == 0 && evenMembers > 0) {
					writef("%d", id);
					evenMembers--;
					id++;
				}
				else {
					writef("0");
				}

				if (j < b - 1)
					writef(" ");
				else
					writef("\n");
			}
		}
	}
}

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

void main ()
{
	int n, a, b;
	while (readf (" %s %s %s", &n, &a, &b) > 0)
	{
		if (n <= a * b)
		{
			auto s = iota (1, n + 1).array;
			s.length = a * b;
			writefln ("%(%(%s %)\n%)", s.chunks (b));
		}
		else
		{
			writeln (-1);
		}
	}
}


import std.algorithm : max, maxElement;
import std.container : Array;
import std.stdio : stdin, write, writeln;
import std.typecons : Tuple, tuple;

struct IO {
    string readToken() {
        import std.range : empty, front, popFront, split;
        import std.stdio : readln;

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

    int readInt() {
        import std.conv : to;

        return readToken.to!int;
    }

    string[] tokens;
}

void main() {
    import std.math : abs;
    import std.algorithm : min, sort;

    IO io;
    int T = io.readInt;
    while (T--) {
        int n = io.readInt;
        int[] a = new int[n << 1];
        for (int i = 0; i < n << 1; ++i) {
            a[i] = io.readInt;
        }
        sort(a);
        int result = a[(n << 1) - 1] - a[0], half = n - 1;
        for (int i = 0; i < n << 1; ++i) {
            result = min(result, abs(a[i] - a[half + (half >= i)]));
        }
        writeln(result);
    }
}

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

DList!string _words;

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

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

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

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

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

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

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

    }
}

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

E[] makeSlice(E, S)(S size, lazy E value)
{
  auto a = new E[size.ind];
  foreach(ref ai; a)
    ai = value();
  return a;
}

E[] makeSlice(E, S)(S size = 0)
{
  return new E[size.ind];
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  foreach(i; 0 .. times)
    {
      static if (is(typeof(f(i))))
        {
          f(i);
        }
      else
        {
          f();
        }
    }
}

void main()
{
 testCase:foreach(tc; 0 .. next!long)
    {
      auto n = next!long;
      auto a = makeSlice(2 * n, next!long);
      sort(a);
      writeln(abs(a.at(n - 1) - a.at(n)));
    }
}

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

DList!string _words;

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

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

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

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

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

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

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

    }
}

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

E[] makeSlice(E, S)(S size, lazy E value)
{
  auto a = new E[size.ind];
  foreach(ref ai; a)
    ai = value();
  return a;
}

E[] makeSlice(E, S)(S size = 0)
{
  return new E[size.ind];
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  foreach(i; 0 .. times)
    {
      static if (is(typeof(f(i))))
        {
          f(i);
        }
      else
        {
          f();
        }
    }
}

void main()
{
  ()
  {
    auto n = next!long;
    auto a = makeSlice(2 * n, next!long);
    sort(a);
    writeln(abs(a.at(n - 1) - a.at(n)));
  }.rep(next!long);
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti] = a[$/2] - a[$/2-1];
	}

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


import std.algorithm;
import std.conv : to;
import std.container;
import std.numeric : gcd;
import std.range;
import std.stdio;
import std.string;
import std.typecons : Tuple, tuple;

void main() {
    int n, m;
    readf!"%d %d\n"(n, m);
    auto s = new string[n];
    int res = 0;
    int[string] set;
    auto buf = new char[m];
    for (int i = 0; i < n; ++i) {
        s[i] = readln.chomp;
        for (int j = 0; j < i; ++j) {
            for (int k = 0; k < m; ++k) {
                buf[k] = s[i][k] == s[j][k] ? s[i][k] : 66 ^ s[i][k] ^ s[j][k];
            }
            res += set.get(cast(string) buf, 0);
        }
        set[s[i]] = 1;
    }
    writeln(res / 2);
}

import std.stdio;
import std.string: chomp;

enum Res { Unusable, Same, Different }

void main() {
  int n, k;
  readf!"%d %d\n"(n, k);

  string[] arr = new string[n];
  bool[][] pairs = new bool[][n];
  int[string] set;

  for(int i = 0; i < n; i++) {
    string s = readln().chomp();
    arr[i] = s;
    set[s] = i;

    pairs[i] = new bool[n];
  }

  int count = 0;

  for(int i = 0; i < (n - 1); i++) {
    for(int j = i + 1; j < n; j++) {
      if(pairs[i][j]) continue;
      pairs[i][j] = pairs[j][i] = true;
      string t = third(arr[i], arr[j], k);
      if(t in set) {
        count++;
        int x = set[t];
        pairs[i][x] = pairs[x][i] = true;
        pairs[j][x] = pairs[x][j] = true;
        // writeln(arr[i] ~ " " ~ arr[j] ~ " " ~ t);
      }
    }
  }

  writeln(count);
}

string third(string first, string second, int k) {
  char[] res = new char[k];
  for(int i = 0; i < k; i++) {
    res[i] = thirdChar(first[i], second[i]);
  }

  return cast(string)res;
}

char thirdChar(char first, char second) {
  if(first == 'S') {
    if(second == 'S') return 'S';
    if(second == 'E') return 'T';
    return 'E';
  }
  if(first == 'E') {
    if(second == 'E') return 'E';
    if(second == 'S') return 'T';
    return 'S';
  }
  if(second == 'T') return 'T';
  if(second == 'E') return 'S';
  return 'E';
}

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

DList!string _words;

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

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

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

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

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

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

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

    }
}

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

void main()
{
  int n, k;
  read(n, k);
  auto cards = makeArray!string(n, next!string);
  bool[string] hasCard;
  foreach(card; cards)
    hasCard[card] = true;
  char other(char c1, char c2)
  {
    if (c1 == c2)
      return c1;
    foreach(c; "SET")
      if (c1 != c && c2 != c)
        return c;
    assert(0);
  }
  int cnt = 0;
  foreach(i; 0 .. n)
    foreach(j; i + 1 .. n)
      {
        char[] otherString;
        foreach(ki; 0 .. k)
          otherString ~= other(cards.at(i).at(ki), cards.at(j).at(ki));
        if (otherString in hasCard)
          {
            cnt++;
          }
      }
  writeln(cnt/3);
}

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

string FMT_F = "%.10f";

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

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

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

long mod = 10^^9 + 7;
//long mod = 998244353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void 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 main()
{
	auto n = RD!int;
	auto k = RD!int;
	auto s = new string[](n);
	foreach (i; 0..n)
		s[i] = RD!string;

	auto toNum = ['S':0, 'E':1, 'T':2];
	int[long] set;
	foreach (i; 0..n)
	{
		long key;
		foreach (j; 0..k)
		{
			auto num = toNum[s[i][j]];
			key += num * (3^^j);
		}
		++set[key];
		debug writeln("i:", i, " key:", key);
	}
	debug writeln(set);

	long ans;
	foreach (i; 0..n)
	{
		foreach (j; i+1..n)
		{
			long key;
			foreach (l; 0..k)
			{
				if (s[i][l] == s[j][l])
					key += toNum[s[i][l]] * (3^^l);
				else
				{
					auto num = 3 - (toNum[s[i][l]] + toNum[s[j][l]]);
					key += num * (3^^l);
				}
			}
			ans += set.get(key, 0);
			debug writeln("i:", i, " j:", j, " ans:", ans);
		}
	}

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


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

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

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

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

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

      auto ans = new int[N];
      int cnt;
      foreach (i; 0 .. N) {
        if (A[i] % 2 == 0) {
          ans[i] = A[i] / 2;
        } else {
          if (cnt++ % 2 == 0) {
            ans[i] = (A[i] + 1) / 2;
          } else {
            ans[i] = (A[i] - 1) / 2;
          }
        }
      }
      foreach (i; 0 .. N) {
        writeln(ans[i]);
      }
    }
  } catch (EOFException e) {
  }
}

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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * (y / gcd(x, y)); }

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

void main()
{
	auto n = RD!int;
	auto ans = new long[](n);
	int cnt;
	foreach (i; 0..n)
	{
		auto a = RD!int;
		if (a % 2 == 0)
			ans[i] = a/2;
		else if (cnt == 0)
		{
			ans[i] = a/2;
			if (a < 0)
				--ans[i];
			++cnt;
		}
		else
		{
			ans[i] = a/2;
			if (a > 0)
				++ans[i];
			--cnt;
		}
	}

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

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [n];
		foreach (ref x; a)
		{
			readf (" %s", &x);
		}
		auto b = new int [n];
		b[] = a[] / 2;
		auto delta = sum (b);
		foreach (i; 0..n)
		{
			if (delta > 0 && a[i] < 0 && a[i] % 2 != 0)
			{
				delta -= 1;
				b[i] -= 1;
			}
			if (delta < 0 && a[i] > 0 && a[i] % 2 != 0)
			{
				delta += 1;
				b[i] += 1;
			}
		}
		foreach (ref x; b)
		{
			writeln (x);
		}
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

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

void solve(){
	int n = rint;
	int[] as = rint(n);

	int cntpos, cntneg;
	foreach(a; as){
		if(a % 2 == 0) (a / 2).writeln;
		else if(a > 0){
			cntpos += 1;
			if(cntpos % 2 == 0) ((a - 1) / 2).writeln;
			else ((a + 1) / 2).writeln;
		}
		else{
			cntneg += 1;
			if(cntneg % 2 == 0) ((a + 1) / 2).writeln;
			else ((a - 1) / 2).writeln;
		}
	}

}


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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  auto m = next!int;
  auto k = next!int;
  auto n = next!int;
  auto s = next!int;
  auto a = next!int(m);
  auto b = next!int(s);
  auto reqCnt = b.getCnt;
  uint[int] cnt;
  auto pendingFulfill = redBlackTree!int(b);
  int i = 0;
  while(i < m && !pendingFulfill.empty)
    {
      cnt.require(a[i], 0)++;
      if (a[i] in pendingFulfill)
	if (cnt[a[i]] >= reqCnt[a[i]])
	    pendingFulfill.removeKey(a[i]);
      i++;
    }
  if (!pendingFulfill.empty) return (-1).writeln;
  i--;
  void dosol(int l, int h)
  {
    int toremove = h - l + 1 - k;
    debug writeln("solving with ", l, " ", h);
    writeln(toremove + l % k + (m - h - 1) % k);
    foreach(i; 1 .. l % k + 1) write(i, " ");
    {
      int i = l;
      while(toremove > 0)
	{
	  if (a[i] !in reqCnt || cnt[a[i]] - 1 >= reqCnt[a[i]])
	    {
	      write(i + 1, " ");
	      cnt[a[i]]--;
	      toremove--;
	    }
	  i++;
	}
    }
    foreach(i; h + 2 .. h + 2 + (m - h - 1) % k) write(i, " ");
    writeln;
  }
  int l = 0;
  while(a[l] !in reqCnt || cnt[a[l]] - 1 >= reqCnt[a[l]])
    {
      cnt[a[l]]--;
      l++;
    }

  while(i < m)
    {
      debug writeln("trying ", l, " ", i, " ");
      int len = i - l + 1;
      if (len < k)
	{
	  int rest = k - len;
	  int ll = l - rest;
	  if (ll >= 0)
	    {
	      auto toleft = ll / k;
	      auto toright = (m - i - 1) / k;
	      if (toleft + toright + 1 >= n)
		{
		  return dosol(ll, i);
		}
	    }
	}
      if (len >= k)
	{
	  auto toleft = l / k;
	  auto toright = (m - i - 1) / k;
	  debug writeln("r = ", toright, " l = ", toleft);
	  if (toleft + toright + 1 >= n)
	    {
	      return dosol(l, i);
	    }
	}

      i++; if (i >= m) break;
      cnt.require(a[i], 0)++;
      while(a[l] !in reqCnt || cnt[a[l]] - 1 >= reqCnt[a[l]])
	{
	  cnt[a[l]]--;
	  l++;
	}
    }
  return (-1).writeln;
}

auto getCnt(T)(T[] arr)
{
  uint[T] cnt;
  foreach(a; arr) cnt.require(a, 0)++;
  return cnt;
}

// INPUT

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

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

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

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

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

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

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

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

      const lim = max(A.maxElement, B.maxElement) + 1;
      auto cnt = new int[lim];

      B.sort;
      const BGroup = B.group.array;

      const width = M - K * (N - 1);
      foreach (i; 0 .. width) {
        ++cnt[A[i]];
      }

      int[] ans;
      foreach (x; 0 .. N) {
        // K x to the left, K (N - 1 - x) to the right
        bool ok = true;
        foreach (g; BGroup) {
          ok = ok && (cnt[g[0]] >= g[1]);
        }
        if (ok) {
          debug {
            writeln("found x = ", x);
          }
          auto need = new int[lim];
          foreach (g; BGroup) {
            need[g[0]] += g[1];
          }
          foreach (i; K * x .. K * x + width) {
            if (need[A[i]] > 0) {
              --need[A[i]];
            } else {
              ans ~= i;
            }
          }
          if (ans.length >= width - K) {
            ans.length = width - K;
          }
          writeln(ans.length);
          foreach (index, i; ans) {
            if (index > 0) write(" ");
            write(i + 1);
          }
          writeln();
          goto found;
        }
        if (x + 1 < N) {
          foreach (i; K * x .. K * (x + 1)) {
            --cnt[A[i]];
            ++cnt[A[i + width]];
          }
        }
      }
      writeln(-1);
     found:
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

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

      const lim = max(A.maxElement, B.maxElement) + 1;
      auto cnt = new int[lim];

      B.sort;
      const BGroup = B.group.array;

      const width = M - K * (N - 1);
      foreach (i; 0 .. width) {
        ++cnt[A[i]];
      }
      int[] ans;
      foreach (x; 0 .. N) {
        // K x to the left, K (N - 1 - x) to the right
        bool ok = true;
        foreach (g; BGroup) {
          ok = ok && (cnt[g[0]] >= g[1]);
        }
        if (ok) {
          debug {
            writeln("found x = ", x);
          }
          auto need = new int[lim];
          foreach (g; BGroup) {
            need[g[0]] += g[1];
          }
          foreach (i; K * x .. K * x + width) {
            if (need[A[i]] > 0) {
              --need[A[i]];
            } else {
              ans ~= i;
              if (ans.length >= width - K) {
                break;
              }
            }
          }
          break;
        }
        if (x + 1 < N) {
          foreach (i; K * x .. K * (x + 1)) {
            --cnt[A[i]];
            ++cnt[A[i + width]];
          }
        }
      }

      if (!ans.empty) {
        writeln(ans.length);
        foreach (index, i; ans) {
          if (index > 0) write(" ");
          write(i + 1);
        }
        writeln();
      } else {
        writeln(-1);
      }
    }
  } catch (EOFException e) {
  }
}


import std.algorithm : max, maxElement;
import std.container : Array;
import std.stdio : stdin, write, writeln;
import std.typecons : Tuple, tuple;

struct IO {
    string readToken() {
        import std.range : empty, front, popFront, split;
        import std.stdio : readln;

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

    int readInt() {
        import std.conv : to;

        return readToken.to!int;
    }

    long readLong() {
        import std.conv : to;

        return readToken.to!long;
    }

    string[] tokens;
}

void main() {
    import std.algorithm : max, min;

    IO io;
    int T = io.readInt;
    while (T--) {
        int n = io.readInt;
        int m = io.readInt - 1;
        int k = min(m, io.readInt);
        int[] a = new int[n];
        for (int i = 0; i < n; ++i) {
            a[i] = io.readInt();
        }
        int result = 0;
        for (int ctr = 0; ctr <= k; ++ctr) {
            int worst = cast(int) 1e9;
            for (int arb = 0; arb <= m - k; ++arb) {
                worst = min(worst, max(a[ctr + arb], a[n - 1 - (m - (ctr + arb))]));
            }
            result = max(result, worst);
        }
        writeln(result);
    }
}

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

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

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

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

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

        const int k = min(K, M - 1);
        const int l = M - 1 - k;

        int ans = int.min;
        foreach (x; 0 .. k + 1) {
          int mn = int.max;
          foreach (y; 0 .. l + 1) {
            chmin(mn, max(A[x + y], A[N - (k - x) - (l - y) - 1]));
          }
          chmax(ans, mn);
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

DList!string _words;

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

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

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

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

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

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

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

    }
}

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

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

void main()
{
  auto t = next!long;
  foreach(tc; 0 .. t)
    {
      long n, m, k;
      read(n, m, k);
      auto a = makeArray(n, next!long);
      long rem = m - 1;
      k = min(k, rem);
      long r = rem - k;

      long minValue(long i, long j)
      {
        if (i > j)
          swap(i, j);
        if (i == j)
          return a.at(i);
        return min(mem!minValue(i, j - 1), a.at(j));
      }
      long partAns(long i, long j)
      {
        long res = long.max;
        assert(i >= 0 && i < n && j >= 0 && j < n);
        foreach(s; 0 .. r + 1)
          {
            res = min(res, max(a.at(i + s), a.at(j + s - r)));
          }
        return res;
      }
      long ans = long.min;
      foreach(s; 0 .. k + 1)
        {
          ans = max(ans, partAns(s, s + n - 1 - k));
        }
      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)); }

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

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

		auto l = min(m, k);
		auto len = n - m;
		foreach (i; 0..l+1)
		{
			long tmp1 = long.max;
			foreach (j; 0..m-l+1)
			{
				long tmp2;
				tmp2.chmax(a[i+j]);
				tmp2.chmax(a[i+j+len-1]);
				//debug writeln(i, ":", j, " ", tmp2);
				tmp1.chmin(tmp2);
			}
			ans[ti].chmax(tmp1);
		}
	}

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

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n, m, k;
		readf !(" %s %s %s") (n, m, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		k = min (k, m - 1);

		int res = int.min;
		foreach (i; 0..k + 1)
		{
			int cur = int.max;
			foreach (j; 0..m - k)
			{
				int temp = max (a[i + j], a[n - m + i + j]);
				cur = min (cur, temp);
			}
			res = max (res, cur);
		}
		writeln (res);
	}
}

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

void main ()
{
	int tests;
	readf !(" %s") (tests);
	foreach (test; 0..tests)
	{
		int n, m, k;
		readf !(" %s %s %s") (n, m, k);
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		k = min (k, m - 1);

		int res = int.min;
		foreach (i; 0..k + 1)
		{
			int cur = int.max;
			foreach (j; 0..m - k)
			{
				writeln (i, " ", j, " ", i + j, " ",
				    n - m - i - j);
				int temp = max (a[i + j], a[n - m + i + j]);
				cur = min (cur, temp);
			}
			res = max (res, cur);
		}
		writeln (res);
	}
}


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

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

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

void solve() {
    auto lrx = readarray!long;
    auto ab = readarray!long;
    if (ab[0] == ab[1]) {
            writeln(0);
            return;
    }
    else if ((lrx[0] + lrx[2] > ab[1]) && (lrx[1] - lrx[2] < ab[1])) {
        writeln(-1);
        return;
    }
    else {
        if (abs(ab[0] - ab[1]) >= lrx[2])
            writeln(1);
        else {
            if (abs(ab[0] - lrx[0]) >= lrx[2])
                if (lrx[2] + lrx[0] <= ab[1]) { //left
                    writeln(2);
                    return;
                }
            if (abs(lrx[1] - ab[0]) >= lrx[2])
                if (lrx[1] - lrx[2] >= ab[1]) { //right
                    writeln(2);
                    return;
                }
            if (abs(ab[0] - lrx[0]) >= lrx[2])
                if (lrx[2] + lrx[0] > ab[1]) { //left
                    writeln(3);
                    return;
                }
            if (abs(lrx[1] - ab[0]) >= lrx[2])
                if (lrx[1] - lrx[2] < ab[1]) { //right
                    writeln(3);
                    return;
                }
            writeln(-1);
        }
    }
}

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

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 main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    long l, r, x, a, b;
    readf!" %d %d %d "(l, r, x);
    readf!" %d %d "(a, b);
    if (a == b) {
      writeln(0);
      continue;
    }
    if (abs(a - b) >= x) {
      writeln(1);
    } else if ((abs(a - l) >= x && abs(b - l) >= x) || (abs(a - r) >= x && abs(b - r) >= x)) {
      writeln(2);
    } else if ((abs(a - l) >= x && abs(b - r) >= x) || (abs(a - r) >= x && abs(b - l) >= x)) {
      writeln(3);
    } else {
      writeln(-1);
    }
  }
}

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int lo, hi, x;
		readf !(" %s %s %s") (lo, hi, x);
		int a, b;
		readf !(" %s %s") (a, b);

		auto delta = abs (a - b);
		if (a == b)
		{
			writeln (0);
		}
		else if (x <= delta)
		{
			writeln (1);
		}
		else if (x <= hi - b && x <= hi - a)
		{
			writeln (2);
		}
		else if (x <= b - lo && x <= a - lo)
		{
			writeln (2);
		}
		else if (x <= hi - b && x <= hi - lo && x <= a - lo)
		{
			writeln (3);
		}
		else if (x <= b - lo && x <= hi - lo && x <= hi - a)
		{
			writeln (3);
		}
		else
		{
			writeln (-1);
		}
	}
}

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

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

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

void solve() {
    auto lrx = readarray!long;
    auto ab = readarray!long;
    if (ab[0] == ab[1]) {
            writeln(0);
            return;
    }
    else if ((lrx[0] + lrx[2] > ab[1]) && (lrx[1] - lrx[2] < ab[1])) {
        writeln(-1);
        return;
    }
    else {
        if (abs(ab[0] - ab[1]) >= lrx[2])
            writeln(1);
        else {
            if (abs(ab[0] - lrx[0]) >= lrx[2]) {
                if (lrx[0] + lrx[2] <= ab[1]) //left
                    writeln(2);
                else
                    writeln(3);
            }
            else if (abs(lrx[1] - ab[0]) >= lrx[2]) {
                if (lrx[1] - lrx[2] >= ab[1]) //right
                    writeln(2);
                else
                    writeln(3);
            }
            else
                writeln(-1);
        }
    }
}

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


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 n = RD!int;
	auto m = RD!int;
	auto rbts = new RedBlackTree!(int, "a > b")[](n);
	foreach (i; 0..n)
		rbts[i] = new RedBlackTree!(int, "a > b");
	foreach (i; 0..m)
	{
		auto u = RD!int-1;
		auto v = RD!int-1;
		rbts[u].insert(v);
		rbts[v].insert(u);
	}
	int cnt;
	foreach (i; 0..n)
	{
		if (rbts[i].empty) continue;
		if (rbts[i].front > i)
			++cnt;
	}
	auto q = RD!int;
	int[] ans;
	foreach (i; 0..q)
	{
		auto num = RD!int;
		if (num == 3)
			ans ~= n - cnt;
		else
		{
			auto u = RD!int-1;
			auto v = RD!int-1;
			if (u > v)
				swap(u, v);
			if (num == 1)
			{
				bool isChange;
				if (rbts[u].empty)
					isChange = true;
				else if (rbts[u].front < u)
					isChange = true;
				if (isChange)
					++cnt;
				rbts[u].insert(v);
				rbts[v].insert(u);
			}
			else
			{
				rbts[u].removeKey(v);
				rbts[v].removeKey(u);
				if (rbts[u].front < u)
					--cnt;
			}
		}
	}

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

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

void main ()
{
	int n, m;
	while (readf !(" %s %s") (n, m) > 0)
	{
		auto adj = new bool [int] [n];
		int total = n;

		void addEdge ()
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			if (u > v)
			{
				swap (u, v);
			}
			if (adj[u].empty)
			{
				total -= 1;
			}
			adj[u][v] = true;
		}

		void remEdge ()
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			if (u > v)
			{
				swap (u, v);
			}
			adj[u].remove (v);
			if (adj[u].empty)
			{
				total += 1;
			}
		}

		foreach (j; 0..m)
		{
			addEdge ();
		}

		int q;
		readf !(" %s") (q);
		foreach (i; 0..q)
		{
			int t;
			readf !(" %s") (t);
			if (t == 1)
			{
				addEdge ();
			}
			else if (t == 2)
			{
				remEdge ();
			}
			else if (t == 3)
			{
				writeln (total);
			}
			else
			{
				assert (false);
			}
		}
	}
}

import std.stdio;
import std.container;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;
import std.functional;

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	alias Set = RedBlackTree!(int, (a, b) => a > b);
	auto adj = new Set[](n);
	foreach(ref adji; adj) adji = new Set();
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u].insert(v);
		adj[v].insert(u);
	}
	int noGood = 0;
	auto isGood = new bool[](n);
	foreach(i; 0 .. n)
	{
		isGood[i] = adj[i].length == 0 || adj[i].front < i;
		noGood += isGood[i];
	}
	auto nq = readInt!int;
	foreach(q; 0 .. nq)
	{
		auto op = readInt!int;
		int v, u;
		switch(op)
		{
			case 1:
				u = readInt!int - 1;
				v = readInt!int - 1;
				auto pu = isGood[u];
				auto pv = isGood[v];
				adj[u].insert(v);
				adj[v].insert(u);
				isGood[u] = adj[u].length == 0 || adj[u].front < u;
				isGood[v] = adj[v].length == 0 || adj[v].front < v;
				noGood += isGood[u] - pu;
				noGood += isGood[v] - pv;
				break;
			case 2:
				u = readInt!int - 1;
				v = readInt!int - 1;
				auto pu = isGood[u];
				auto pv = isGood[v];
				adj[u].removeKey(v);
				adj[v].removeKey(u);
				isGood[u] = adj[u].length == 0 || adj[u].front < u;
				isGood[v] = adj[v].length == 0 || adj[v].front < v;
				noGood += isGood[u] - pu;
				noGood += isGood[v] - pv;
				break;
			case 3:
				noGood.writeln;
				break;
			default: assert(0);
		}
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

import std.stdio;
import std.container;
import std.ascii;
import std.algorithm;
import std.range;
import core.stdc.stdio;
import std.functional;

void main()
{
	int n = readInt!int;
	int m = readInt!int;
	alias Set = RedBlackTree!(int, (a, b) => a > b);
	auto adj = new Set[](n);
	foreach(ref adji; adj) adji = new Set();
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u].insert(v);
		adj[v].insert(u);
	}
	int noGood = 0;
	auto isGood = new bool[](n);
	foreach(i; 0 .. n)
	{
		isGood[i] = adj[i].front < i;
		noGood += isGood[i];
	}
	auto nq = readInt!int;
	foreach(q; 0 .. nq)
	{
		auto op = readInt!int;
		int v, u;
		switch(op)
		{
			case 1:
				u = readInt!int - 1;
				v = readInt!int - 1;
				auto pu = isGood[u];
				auto pv = isGood[v];
				adj[u].insert(v);
				adj[v].insert(u);
				isGood[u] = adj[u].front < u;
				isGood[v] = adj[v].front < v;
				noGood += isGood[u] - pu;
				noGood += isGood[v] - pv;
				break;
			case 2:
				u = readInt!int - 1;
				v = readInt!int - 1;
				auto pu = isGood[u];
				auto pv = isGood[v];
				adj[u].removeKey(v);
				adj[v].removeKey(u);
				isGood[u] = adj[u].front < u;
				isGood[v] = adj[v].front < v;
				noGood += isGood[u] - pu;
				noGood += isGood[v] - pv;
				break;
			case 3:
				noGood.writeln;
				break;
			default: assert(0);
		}
	}
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

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

void popChar()
{
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}


module sigod.codeforces.p291D;

import std.conv;
import std.stdio;

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

	string end_line = n.to!string() ~ (n > 1 ? " " ~ n.to!string() : "");

	int[] memory = new int[n + 1];
	memory[] = 1;
	memory[n] = 0;

	foreach (ki; 0 .. k) {
		foreach (i; 1 .. n - 1) {
			if (memory[i] < n - i) {
				if (memory[i] * 2 <= n - i) {
					memory[i] *= 2;
					stdout.write(i, " ");
				}
				else {
					int index = n - (n - i - memory[i]);

					memory[i] += memory[index];
					stdout.write(index, " ");
				}
			}
			else {
				stdout.write(n, " ");
			}
		}

		stdout.writeln(end_line);
	}
}

import std.algorithm;
import std.container;
import std.exception;
import std.math;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	int n, k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		auto a = new int [n];
		a[1..$] = 1;
		foreach (i; 0..k)
		{
			int [] r;
			foreach (j; 0..n)
			{
				r ~= min (a[j], j - a[j]);
			}
			a[] += r[];
			r[] = n - r[];
			reverse (r);
			writefln ("%(%s %)", r);
		}
		foreach (j; 0..n)
		{
			enforce (a[j] == j);
		}
	}
}


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

void main ()
{
	int n, m, l;
	while (readf (" %s %s %s", &n, &m, &l) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		a = 0 ~ a ~ 0;
		int cur = 0;
		foreach (i; 1..n + 1)
		{
			if (a[i] > l)
			{
				cur += 1;
				cur -= (a[i - 1] > l);
//				cur -= (a[i + 1] > l);
			}
		}

		foreach (j; 0..m)
		{
			int t;
			readf (" %s", &t);
			if (t == 0)
			{
				writeln (cur);
			}
			else
			{
				int p, d;
				readf (" %s %s", &p, &d);
				auto g = (a[p] <= l && a[p] + d > l);
				a[p] += d;
				if (g)
				{
					cur += 1;
					cur -= (a[p - 1] > l);
					cur -= (a[p + 1] > l);
				}
	        	}
		}
	}
}

import std.stdio;
import std.algorithm;

long[] a=new long[100002];
void main()
{
	int n,m,l,ans=0;
	scanf("%d%d%d",&n,&m,&l);
	for (int i=1;i<=n;++i)
	{
		scanf("%lld",&a[i]);
		if (a[i]>l) ans+=a[i-1]<=l;
	}
	for (int i=1;i<=m;++i)
	{
		int o;
		scanf("%d",&o);
		if (o==0) printf("%d\n",ans);
		else
		{
			int p,d;
			scanf("%d%d",&p,&d);
			if (a[p]<=l && a[p]+d>l) ans+=1-(a[p-1]>l)-(a[p+1]>l);
			a[p]+=d;
		}
	}
}

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

void main ()
{
	int n, m, l;
	while (readf (" %s %s %s", &n, &m, &l) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(long)).array;
		a = 0 ~ a ~ 0;
		int cur = 0;
		foreach (i; 1..n + 1)
		{
			if (a[i] > l)
			{
				cur += 1;
				cur -= (a[i - 1] > l);
				cur -= (a[i + 1] > l);
			}
		}

		foreach (j; 0..m)
		{
			int t;
			readf (" %s", &t);
			if (t == 0)
			{
				writeln (cur);
			}
			else
			{
				int p, d;
				readf (" %s %s", &p, &d);
				auto g = (a[p] <= l && a[p] + d > l);
				a[p] += d;
				if (g)
				{
					cur += 1;
					cur -= (a[p - 1] > l);
					cur -= (a[p + 1] > l);
				}
	        	}
		}
	}
}


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

void main() {
	auto n = readln.strip.to!uint;
	auto v = readln.strip.splitter(' ').map!(a => a.to!int).array;

	uint res;

    for(int i = cast(uint)v.length - 2; i >= 0; i -= 2) {
		res += abs(v[i] - v[i + 1]);
		if(i) v[i / 2 - 1] += max(v[i], v[i + 1]);
    }

	res.write;
}

import std.algorithm, std.conv, std.range, std.stdio;
void main () {
	int n;
	readf ("%s ", &n);
	auto a = readln.split.map!(to!int).array,
		m = a.length, v = new int [m + 1];
	int res = 0;
	foreach_reverse (i; 0..m / 2) {
		int lo = v[i * 2 + 1] + a[i * 2 + 0];
		int hi = v[i * 2 + 2] + a[i * 2 + 1];
		v[i] = max (lo, hi);
		res += v[i] * 2 - lo - hi;
	}
	res.writeln;
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		auto a = readln ().split ().map !(to !(long)).array;
		auto m = a.length;
		auto v = new long [m + 1];
		long res = 0;
		foreach_reverse (i; 0..m / 2)
		{
			long lo = v[i * 2 + 1] + a[i * 2 + 0];
			long hi = v[i * 2 + 2] + a[i * 2 + 1];
			v[i] = max (lo, hi);
			res += v[i] * 2 - lo - hi;
		}
		writeln (res);
	}
}

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

struct S {
	uint a, b;
	S *left, right;
}

void main() {
	auto n = readln.strip.to!uint;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S *create(uint idx) {
		if(idx == n) return null;

		auto s = new S;

		s.a = arr[0];
		s.b = arr[1];

		arr = arr[2..$];

		s.left = create(++idx);
		s.right = create(idx);

		return s;
	}

	auto root = create(0);

	uint calcMax(S *s, uint n = 0) {
		if(!s)
			return n;
		return max(calcMax(s.left, n + s.a), calcMax(s.right, n + s.b));
	}

	auto m = calcMax(root);
	BigInt res;

	long calcResults(S *s, long n = 0) {
		if(!s)
			return m - n;

		auto v = calcResults(s.left, n + s.a);
		auto u = calcResults(s.right, n + s.b);

		auto k = abs(v - u);
		res += k;

		return max(v, u) - k;
	}

	calcResults(root);
	res.writeln;
}

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

struct S {
	uint a, b;
	S *left, right;
}

void main() {
	auto n = readln.strip.to!uint;
	auto arr = readln.strip.splitter(' ').map!(a => a.to!uint).array;

	S *create(uint idx) {
		if(idx == n) return null;

		auto s = new S;

		s.a = arr[0];
		s.b = arr[1];

		arr = arr[2..$];

		s.left = create(++idx);
		s.right = create(idx);

		return s;
	}

	auto root = create(0);

	uint calcMax(S *s, uint n = 0) {
		if(!s)
			return n;
		return max(calcMax(s.left, n + s.a), calcMax(s.right, n + s.b));
	}

	auto m = calcMax(root);
	uint res;

	int calcResults(S *s, uint n = 0) {
		if(!s)
			return m - n;

		auto v = calcResults(s.left, n + s.a);
		auto u = calcResults(s.right, n + s.b);

		auto k = abs(v - u);
		res += k;

		return max(v, u) - k;
	}

	calcResults(root);
	res.writeln;
}


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto k = RD!int;
		auto s = RD!string;

		int l = -1, r;
		foreach (i; 0..n)
		{
			if (s[i] == '1')
			{
				l = i;
				break;
			}
		}
		foreach_reverse (i; 0..n)
		{
			if (s[i] == '1')
			{
				r = i;
				break;
			}
		}
		if (l == -1)
		{
			ans[ti] = 1 + (n-1) / (k+1);
		}
		else
		{
			ans[ti] += l / (k+1);
			ans[ti] += (n-r-1) / (k+1);
			int cnt;
			foreach (i; l..r)
			{
				debug writeln("cnt:", cnt, " ans:", ans[ti]);
				if (s[i] == '1')
				{
					ans[ti] += (cnt-k) / (k+1);
					cnt = 0;
				}
				else
				{
					++cnt;
				}
			}
			ans[ti] += (cnt-k) / (k+1);
		}
	}

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

import std.stdio;
import std.string : strip;
import std.algorithm : group;
import std.range;

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

  foreach (i; 0..t) {
    int n, k;
    readf!"%d %d\n"(n, k);
    auto s = readln.strip;
    auto g = s.group;
    auto ng = g.walkLength;
    auto c = 0;

    foreach (e; g.enumerate) {
      auto j = e.index;
      auto x = e.value;

      if (x[0] == '1') {
        continue;
      }

      if (ng == 1) {
        // only 1 group of zeros: take the left table, then (n-1) / (k+1)
        c += 1 + (n - 1) / (k + 1);
      } else if (j == 0 || j == ng - 1) {
        // if first or last group is zeros: x / (k + 1)
        c += x[1] / (k + 1);
      } else {
        // else (x - k) / (k + 1)
        c += (x[1] - k) / (k + 1);
      }
    }

    writeln(c);
  }
}