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

class SegmentTree(T = int, alias op) {
  private:
  T [] t;
  size_t n;
  final void build () pure nothrow @nogc {
    foreach_reverse (i; 1 .. n) {
      immutable k = i << 1;
      t[i] = op (t[k], t[k+1]);
    }
  }
  public:
  final void update (size_t p, T v) pure nothrow @nogc {
    for (t[p += n] = v; p > 1; p >>= 1) {
      t[p>>1] = op (t[p], t[p ^ 1]);
    }
  }
  final T reduce (size_t l, size_t r) const pure nothrow @nogc {
    T res;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        res = op (res, t[l++]);
      }
      if (r & 1) {
        res = op (t[--r], res);
      }
    }
    return res;
  }
  this (const T[] a) pure nothrow {
    n = a.length;
    t = new T[n];
    t ~= a;
    build ();
  }
}

void main() {
  int n, m;
  readf (" %d %d", &n, &m);
  auto a = new uint[1<<n];
  foreach (t; 0 .. 1<<n) {
    readf (" %d", &a[t]);
  }
  enum uint q = 0x8000_0000;
  auto st = new SegmentTree!(uint, (x, y) => (x & q) ? (x ^ y) & (q - 1) : (x | y | q)) (a);
  foreach (t; 0 .. m) {
    int i;
    uint v;
    readf (" %d %d", &i, v);
    --i;
    st.update (i, v);
    writeln (st.reduce (0, 1 << n) & (q - 1));
  }
}

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

class SegmentTree(T = int, alias op) {
  private:
  T [] t;
  size_t n;
  final void build () pure nothrow @nogc {
    foreach_reverse (i; 1 .. n) {
      immutable k = i << 1;
      t[i] = op (t[k], t[k+1]);
    }
  }
  public:
  final T opIndexAssign (T value, size_t index) {
    for (t[index += n] = value; index > 1; index >>= 1) {
      t[index>>1] = op (t[index], t[index ^ 1]);
    }
    return value;
  }
  final T reduce (size_t l, size_t r) const pure nothrow @nogc {
    T res;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        res = op (res, t[l++]);
      }
      if (r & 1) {
        res = op (t[--r], res);
      }
    }
    return res;
  }
  this (const T[] a) pure nothrow {
    n = a.length;
    t = new T[n];
    t ~= a;
    build ();
  }
}

void main() {
  int n, m;
  readf (" %d %d", &n, &m);
  auto a = new uint[1<<n];
  foreach (t; 0 .. 1<<n) {
    readf (" %d", &a[t]);
  }
  enum uint q = 0x8000_0000;
  auto st = new SegmentTree!(uint, (x, y) => (x & q) ? (x ^ y) & (q - 1) : (x | y | q)) (a);
  foreach (t; 0 .. m) {
    int i;
    uint v;
    readf (" %d %d", &i, v);
    st[i-1] = v;
    writeln (st.reduce (0, 1 << n) & (q - 1));
  }
}


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

void main()
{
    auto T = readln.chomp.to!int;
    auto cs = new long[](25820);
    cs[1] = 2;
    foreach (i; 2..25820) {
        cs[i] = cs[i-1] + (i-1) * 3 + 2;
    }

    foreach (_; 0..T) {
        auto N = readln.chomp.to!int;

        int res;
        while (N >= 2) {
            ++res;
            if (N >= cs[$-1]) {
                N -= cs[$-1];
            } else {
                int l = 1, r = cs.length.to!int-1;
                while (l+1 < r) {
                    auto m = (l+r)/2;
                    if (N >= cs[m]) {
                        l = m;
                    } else {
                        r = m;
                    }
                }
                N -= cs[l];
            }
        }
        writeln(res);
    }
}

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

int tri (int n)
{
	return n * (n + 1) / 2;
}

int f (int n)
{
	return tri (n) * 3 - n;
}

void main ()
{
	readln;
	int n;
	while (readf !(" %s") (n) > 0)
	{
		int k = 0;
		while (f (k) <= n)
		{
			k += 1;
		}
		int res = 0;
		while (k > 0)
		{
			while (f (k) <= n)
			{
				n -= f (k);
				res += 1;
			}
			k -= 1;
		}
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

T binarySearch(alias pred, T)(T ok, T ng)
{
	while (abs(ok-ng) > 1)
	{
		auto mid = (ok+ng)/2;
		if (unaryFun!pred(mid)) ok = mid;
		else ng = mid;
	}
	return ok;
}

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

		while (true)
		{
			auto r = binarySearch!((long a) => a*(a+1) + a*(a-1)/2 <= n)(0L, 10L^^9);
			if (r == 0) break;
			++ans[ti];
			n -= r*(r+1) + r*(r-1)/2;
		}
	}

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

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

int tri (int n)
{
	return n * (n + 1) / 2;
}

int f (int n)
{
	return tri (n) * 3 - n;
}

void main ()
{
	readln;
	int n;
	while (readf !(" %s") (n) > 0)
	{
		int k = 0;
		while (f (k) <= n)
		{
			k += 1;
		}
		int res = 0;
		while (k > 0)
		{
			if (f (k) <= n)
			{
				n -= f (k);
				res += 1;
			}
			k -= 1;
		}
		writeln (res);
	}
}


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, m, k;
	while (readf (" %s %s %s ", &n, &m, &k) > 0)
	{
		string [] a;
		foreach (i; 0..n)
		{
			a ~= readln ().strip ();
		}

		bool ask (int row, int col, char ch)
		{
			if (row < 0 || row >= n || col < 0 || col >= m)
			{
				return false;
			}
			return a[row][col] == ch;
		}

		int [] ans;
		foreach (j; 0..m)
		{
			int cur = 0;
			foreach (i; 1..n)
			{
				cur += ask (i * 2, j, 'U');
				cur += ask (i, j - i, 'R');
				cur += ask (i, j + i, 'L');
			}
			ans ~= cur;
		}

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

import std.stdio;
import std.math;
import std.array;
import std.algorithm;
import std.conv;
import std.traits;

void main(){
  size_t n,m,k;
  readf("%s %s %s\n",&n,&m,&k);

  char[][] table = new char[][](n,m);

  string tmp;
  readf("%s\n",&tmp);
  for(int i = 1; i < n; i++){
    readf("%s\n",&tmp);

    for(int j = 0; j < m; j++){
      if (tmp[j] != '.'){
        table[i][j] = tmp[j];
      }
    }
  }

  // for(int i = 0; i < n; i++){
  //   for(int j = 0; j < m; j++){
  //     write(table[i][j]);
  //   }
  //   writeln();
  // }

  int[] hits = new int[m];
  for(int t = 0; t < n; t++){
    for(int om = 0; om < m; om++){
      // U && D (never hits)
      if(t % 2 == 0 && table[t][om] == 'U'){
        hits[om]++;
      }
      //L
      if(om+t >= 0 && om+t < m && table[t][om+t] == 'L'){
        hits[om]++;
      }
      //R
      //writeln(om-t);
      if(om-t >= 0 && om-t < m && table[t][om-t] == 'R'){
        hits[om]++;
      }
    }
  }
  foreach(om;hits){
    writef("%s ",om);
  }
  writeln();
}

import std.stdio;
import std.math;
import std.array;
import std.algorithm;
import std.conv;
import std.traits;

void main(){
  size_t n,m,k;
  readf("%s %s %s\n",&n,&m,&k);

  char[][] table = new char[][](n,m);

  string tmp;
  readf("%s\n",&tmp);
  for(int i = 1; i < n; i++){
    readf("%s\n",&tmp);

    for(int j = 0; j < m; j++){
      if (tmp[j] != '.'){
        table[i][j] = tmp[j];
      }
    }
  }

  // for(int i = 0; i < n; i++){
  //   for(int j = 0; j < m; j++){
  //     write(table[i][j]);
  //   }
  //   writeln();
  // }

  int[] hits = new int[m];
  for(int t = 0; t < n-1; t++){
    for(int om = 0; om < m; om++){
      // U && D (never hits)
      if(t+2 < n && table[t+2][om] == 'U'){
        hits[om]++;
      }
      //L
      if(om+t >= 0 && om+t < m && table[t][om+t] == 'L'){
        hits[om]++;
      }
      //R
      //writeln(om-t);
      if(om-t >= 0 && om-t < m && table[t][om-t] == 'R'){
        hits[om]++;
      }
    }
  }
  foreach(om;hits){
    writef("%s ",om);
  }
  writeln();
}

import std.stdio;
import std.math;
import std.array;
import std.algorithm;
import std.conv;
import std.traits;

void main(){
  size_t n,m,k;
  readf("%s %s %s\n",&n,&m,&k);

  char[][] table = new char[][](n,m);

  string tmp;
  readf("%s\n",&tmp);
  for(int i = 1; i < n; i++){
    readf("%s\n",&tmp);

    for(int j = 0; j < m; j++){
      if (tmp[j] != '.'){
        table[i][j] = tmp[j];
      }
    }
  }

  // for(int i = 0; i < n; i++){
  //   for(int j = 0; j < m; j++){
  //     write(table[i][j]);
  //   }
  //   writeln();
  // }

  int[] hits = new int[m];
  for(int t = 0; t < n; t++){
    for(int om = 1; om <= m; om++){
      // U && D (never hits)
      if(table[t][om-1] == 'U'){
        hits[om-1]++;
      }
      //L
      if(om-1+t >= 0 && om-1+t < m && table[t][om+t-1] == 'L'){
        hits[om-1]++;
      }
      //R
      //writeln(om-t);
      if(om-1-t >= 0 && om-1-t < m && table[t][om-t-1] == 'R'){
        hits[om-1]++;
      }
    }
  }
  foreach(om;hits){
    writef("%s ",om);
  }
  writeln();
}


module main;

import std.stdio;
import std.algorithm;
import std.math;
import std.random;
import std.conv;

void main ()
{
    float h,d,v,e,vn,Vo;
    readf(" %s %s %s %s", &d, &h, &v, &e);
    e = e*(d/2)^^2 * 3.1415926;
    if (v - e < 0) writeln("NO");
    else
    {
        writeln("YES");
        vn = v - e;
        Vo = (d/2)^^2 * 3.1415926 * h;
        Vo = Vo / vn;
        writeln(Vo);

    }

}

import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm;
import std.math;
void main(){
    auto dhve = readln.chomp.split.map!(to!double);

    double d = dhve[0];
    double h = dhve[1];
    double v = dhve[2];
    double e = dhve[3];

    double r = d * 0.5;
    double t = (PI * r * r * h)/(v - PI * r * r * e);

    if (t < -1e-8){
      writeln("NO");
    }else{
      writeln("YES");
      writeln(t);
    }

}


import std.stdio;

int main() {
    int q, l, r, d;

    scanf("%d", &q);
    while (q--) {
        scanf("%d%d%d", &l, &r, &d);
        if (d<l) printf("%d\n", d);
        else printf("%d\n", (r/d+1)*d);
    }

    return 0;
}

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

void main() {
    int q = to!int(readln.strip);

    foreach (i; 0 .. q) {
        int l,r,d;
        readf!" %s %s %s"(l,r,d);
        if (l > d) {
            writeln(d);
        } else {
            writeln((r/d+1)*d);
        }
    }
}


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

void main()
{
    immutable int MX = 10 ^^ 7 + 23;

    auto d = new int[MX];
    d[] = 0;
    foreach (i; 2 .. sqrt(MX.to!real).to!int) {
        if (d[i] != 0) { continue; }
        foreach (ref p; d[i .. MX].stride(i)) { p = i; }
    }

    foreach (i; 0 .. MX) {
        if (d[i] == 0) { d[i] = i; }
    }

    debug { d.take(20).writeln; }

    int t;
    readf!"%s"(t);
    readln;

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

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

        auto tr = make!(RedBlackTree!(int[]));
        tr.insert([2, 3]);

        int ans = 1;
        foreach (e; a) {
            int[] odd;
            while (e > 1) {
                int curd = d[e];
                int cnt = 0;
                while (e % curd == 0) {
                    ++cnt;
                    e /= curd;
                }

                if (cnt % 2 == 1) { odd ~= curd; }
            }

            if (odd in tr) {
                ans += 1;
                tr.clear();
            }

            tr.insert(odd);
        }

        ans.writeln;
    }
}

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

void main()
{
    immutable int MX = 10 ^^ 7 + 23;

    auto d = new int[MX];
    d[] = 0;
    foreach (i; 2 .. sqrt(MX.to!real).to!int) {
        if (d[i] != 0) { continue; }
        foreach (ref p; d[i .. MX].stride(i)) { p = i; }
    }

    foreach (i; 0 .. MX) {
        if (d[i] == 0) { d[i] = i; }
    }

    debug { d.take(20).writeln; }

    int t;
    readf!"%s"(t);
    readln;

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

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

        auto tr = make!(RedBlackTree!(int[]));

        int ans = 1;
        foreach (e; a) {
            int[] odd;
            while (e > 1) {
                int curd = d[e];
                int cnt = 0;
                while (e % curd == 0) {
                    ++cnt;
                    e /= curd;
                }

                if (cnt % 2 == 1) { odd ~= curd; }
            }

            if (odd in tr) {
                ans += 1;
                tr.clear();
            }

            tr.insert(odd);
        }

        ans.writeln;
    }
}


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

import std.container;

void main() {
  int n, m;
  readf (" %d %d", &n, &m);
  auto a = new int[n];
  auto rbt = new RedBlackTree!int (iota (0, n));
  auto c = new int[n];
  a[] = -1;
  foreach (t; 0 .. m) {
    int l, r, x;
    readf (" %d %d %d", &l, &r, &x);
    --l; --r; --x;
    auto d = rbt.lowerBound (x);
    int k;
    foreach_reverse (i; d) {
      if (i < l) break;
      a[i] = x;
      c[k++] = i;
    }
    d = rbt.upperBound (x);
    foreach (i; d) {
      if (i > r) break;
      a[i] = x;
      c[k++] = i;
    }
    foreach (i; 0 .. k) {
      rbt.removeKey (c[i]);
    }
  }
  a.map! (i => (i+1).text).join(" ").writeln;
}

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

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto tree = redBlackTree(iota(1, N + 1, 1).array);
    auto ans = new int[N];
    foreach (i; 0 .. M) {
        int L, R, W; scanf("%d %d %d\n", &L, &R, &W);
        auto xs = tree.upperBound(L - 1);
        int[] rs;
        foreach (x; xs) {
            if (x > R) break;
            if (x == W) continue;
            ans[x - 1] = W;
            rs ~= x;
        }
        foreach (r; rs) tree.removeKey(r);
    }
    writefln("%(%s %)", ans);
}

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

class SegmentTree {
    // rootのindexは1
    int N;
    int[] L;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) x1 *= 2;
        return x1;
    }
    this(int N_) {
        N = FindPow2(N_);
        L = new int[N * 2];
        L[] = -1;
    }
    /* [s, t)をxに更新 */
    void update(int x, int s, int t) { update(x, s, t, 0, N, 1); }
    void update(int x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] = x;
        } else if (s < b || a < t) {
            int m = (a + b) / 2;
            update(x, s, t, a, m, k * 2);
            update(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* iの値 */
    int query(int i) { return query(i, 0, N, 1); }
    int query(int i, int a, int b, int k) {
        if (i < a || b <= i) return -1;
        if (a <= i && i < b && L[k] >= 0) return L[k];
        int m = (a + b) / 2;
        return max( query(i, a, m, k * 2), query(i, m, b, k * 2 + 1) );
    }
};

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto L = new int[M],
         R = new int[M],
         W = new int[M];
    foreach (i; 0 .. M) {
        auto xs = readln.chomp.split(" ").map!(to!int).array;
        L[i] = xs[0]; L[i]--;
        R[i] = xs[1];
        W[i] = xs[2];
    }

    const MAX = 300001; const Z = 550;
    auto ans = new int[N]; ans[] = -1;
    auto seg = new int[N]; seg[] = -1;
    auto segp = new int[N];
    auto child_prior = new int[N];
    L.reverse; R.reverse; W.reverse;
    foreach (m; 0 .. M) {
        int s = L[m], t = R[m], v = W[m]; // [s, t]をvに更新
        int p;
        if (child_prior[(v - 1) / Z] == 1) {
            p = seg[v - 1];
        } else {
            p = segp[(v - 1) / Z];
        }
        //writeln([s, t, v]);
        int from_parent = s / Z;
        if (child_prior[from_parent] == -1) {
            for (int i = from_parent * Z; i < from_parent * Z + Z; i++) seg[i] = segp[from_parent];
        }
        for (int i = s; i < min(t, from_parent * Z + Z); i++) {
            seg[i] = v;
        }
        child_prior[from_parent] = 1;
        int to_parent = t / Z;
        if (child_prior[to_parent] == -1) {
            for (int i = to_parent * Z - Z; i < to_parent * Z; i++) seg[i] = segp[to_parent];
        }
        for (int i = max(s, to_parent * Z - Z); i < t; i++) {
            seg[i] = v;
        }
        child_prior[to_parent] = 1;
        for (int i = from_parent + 1; i <= to_parent - 1; i++) {
            segp[i] = v;
            child_prior[i] = -1;
        }
        ans[v - 1] = p;
        static if (false) {
            writeln(seg);
            writeln(segp);
            writeln;
        }
    }
    foreach (i; 0 .. N) {
        if (ans[i] >= 0) continue;
        if (child_prior[i / Z] == 1) {
            ans[i] = seg[i];
        } else if (child_prior[i / Z] == -1) {
            ans[i] = segp[i];
        } else {
            writeln("FOO");
        }
    }
    writefln("%(%s %)", ans);
}

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

class SegmentTree {
    // rootのindexは1
    int N;
    int[] L;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) x1 *= 2;
        return x1;
    }
    this(int N_) {
        N = FindPow2(N_);
        L = new int[N * 2];
        L[] = -1;
    }
    /* [s, t)をxに更新 */
    void update(int x, int s, int t) { update(x, s, t, 0, N, 1); }
    void update(int x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] = x;
        } else if (s < b || a < t) {
            int m = (a + b) / 2;
            update(x, s, t, a, m, k * 2);
            update(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* iの値 */
    int query(int i) { return query(i, 0, N, 1); }
    int query(int i, int a, int b, int k) {
        if (i < a || b <= i) return -1;
        if (a <= i && i < b && L[k] >= 0) return L[k];
        int m = (a + b) / 2;
        return max( query(i, a, m, k * 2), query(i, m, b, k * 2 + 1) );
    }
};

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto L = new int[M],
         R = new int[M],
         W = new int[M];
    foreach (i; 0 .. M) {
        auto xs = readln.chomp.split(" ").map!(to!int).array;
        L[i] = xs[0]; L[i]--;
        R[i] = xs[1];
        W[i] = xs[2];
    }

    const MAX = 300001; const Z = 550;
    auto ans = new int[N]; ans[] = -1;
    auto seg = new int[N]; seg[] = -1;
    auto segp = new int[N];
    auto child_prior = new int[N];
    L.reverse; R.reverse; W.reverse;
    foreach (m; 0 .. M) {
        int s = L[m], t = R[m], v = W[m]; // [s, t]をvに更新
        int p;
        if (child_prior[(v - 1) / Z] == 1) {
            p = seg[v - 1];
        } else {
            p = segp[(v - 1) / Z];
        }
        //writeln([s, t, v]);
        int from_parent = s / Z;
        if (child_prior[from_parent] == -1) {
            for (int i = from_parent * Z; i < from_parent * Z + Z; i++) seg[i] = segp[from_parent];
        }
        for (int i = s; i < min(t, from_parent * Z + Z); i++) {
            seg[i] = v;
        }
        child_prior[from_parent] = 1;
        int to_parent = t / Z;
        if (child_prior[to_parent] == -1) {
            for (int i = to_parent * Z - Z; i < to_parent * Z; i++) seg[i] = segp[to_parent];
        }
        for (int i = max(s, to_parent * Z - Z); i < t; i++) {
            seg[i] = v;
        }
        child_prior[to_parent] = 1;
        for (int i = from_parent + 1; i <= to_parent - 1; i++) {
            segp[i] = v;
            child_prior[i] = -1;
        }
        ans[v - 1] = p;
        static if (false) {
            writeln(seg);
            writeln(segp);
            writeln;
        }
    }
    foreach (i; 0 .. N) {
         if (ans[i] >= 0) continue;
        if (child_prior[i / Z] == 1) {
            ans[i] = seg[i];
        } else if (child_prior[i / Z] == -1) {
            ans[i] = segp[i];
        } else {
            assert(0);
        }
    }
    writefln("%(%s %)", ans);
}

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

class SegmentTree {
    // rootのindexは1
    int N;
    int[] L;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) x1 *= 2;
        return x1;
    }
    this(int N_) {
        N = FindPow2(N_);
        L = new int[N * 2];
        L[] = -1;
    }
    /* [s, t)をxに更新 */
    void update(int x, int s, int t) { update(x, s, t, 0, N, 1); }
    void update(int x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] = x;
        } else if (s < b || a < t) {
            int m = (a + b) / 2;
            update(x, s, t, a, m, k * 2);
            update(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* iの値 */
    int query(int i) { return query(i, 0, N, 1); }
    int query(int i, int a, int b, int k) {
        if (i < a || b <= i) return -1;
        if (a <= i && i < b && L[k] >= 0) return L[k];
        int m = (a + b) / 2;
        return max( query(i, a, m, k * 2), query(i, m, b, k * 2 + 1) );
    }
};

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto L = new int[M],
         R = new int[M],
         W = new int[M];
    foreach (i; 0 .. M) {
        auto xs = readln.chomp.split(" ").map!(to!int).array;
        L[i] = xs[0]; L[i]--;
        R[i] = xs[1];
        W[i] = xs[2];
    }

    const MAX = 300001; const Z = 550;
    auto ans = new int[N]; ans[] = -1;
    auto seg = new int[N]; seg[] = -1;
    auto segp = new int[N];
    auto child_prior = new bool[N];
    L.reverse; R.reverse; W.reverse;
    foreach (m; 0 .. M) {
        int s = L[m], t = R[m], v = W[m]; // [s, t]をvに更新
        int p = seg[v - 1];
        //writeln([s, t, v]);
        int from_parent = s / Z;
        for (int i = s; i < min(t, from_parent * Z + Z); i++) {
            seg[i] = v;
        }
        child_prior[from_parent] = true;
        int to_parent = t / Z;
        for (int i = max(s, to_parent * Z - Z); i < t; i++) {
            seg[i] = v;
        }
        child_prior[to_parent] = true;
        for (int i = from_parent + 1; i <= to_parent - 1; i++) {
            segp[i] = v;
            child_prior[i] = false;
        }
        ans[v - 1] = p;
    }
    foreach (i; 0 .. N) {
        if (ans[i] >= 0) continue;
        if (child_prior[i / Z]) {
            ans[i] = seg[i];
        } else {
            ans[i] = segp[i];
        }
    }
    foreach (i; 0 .. N) if (ans[i] == i + 1) ans[i] = 0;
    writefln("%(%s %)", ans);
}

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

class SegmentTree {
    // rootのindexは1
    int N;
    int[] L;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) x1 *= 2;
        return x1;
    }
    this(int N_) {
        N = FindPow2(N_);
        L = new int[N * 2];
        L[] = -1;
    }
    /* [s, t)をxに更新 */
    void update(int x, int s, int t) { update(x, s, t, 0, N, 1); }
    void update(int x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] = x;
        } else if (s < b || a < t) {
            int m = (a + b) / 2;
            update(x, s, t, a, m, k * 2);
            update(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* iの値 */
    int query(int i) { return query(i, 0, N, 1); }
    int query(int i, int a, int b, int k) {
        if (i < a || b <= i) return -1;
        if (a <= i && i < b && L[k] >= 0) return L[k];
        int m = (a + b) / 2;
        return max( query(i, a, m, k * 2), query(i, m, b, k * 2 + 1) );
    }
};

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto L = new int[M],
         R = new int[M],
         W = new int[M];
    foreach (i; 0 .. M) {
        auto xs = readln.chomp.split(" ").map!(to!int).array;
        L[i] = xs[0]; L[i]--;
        R[i] = xs[1];
        W[i] = xs[2];
    }

    const MAX = 300001; const Z = 550;
    auto ans = new int[N]; ans[] = -1;
    auto seg = new int[N]; seg[] = -1;
    auto segp = new int[N];
    auto child_prior = new int[N];
    L.reverse; R.reverse; W.reverse;
    foreach (m; 0 .. M) {
        int s = L[m], t = R[m], v = W[m]; // [s, t]をvに更新
        int p;
        if (ans[v - 1] >= 0) {
            p = ans[v - 1];
        } else if (child_prior[(v - 1) / Z] == 1) {
            p = seg[v - 1];
        } else {
            p = segp[(v - 1) / Z];
        }
        //writeln([s, t, v]);
        int from_parent = s / Z;
        if (child_prior[from_parent] == -1) {
            for (int i = from_parent * Z; i < from_parent * Z + Z; i++) seg[i] = segp[from_parent];
        }
        for (int i = s; i < min(t, from_parent * Z + Z); i++) {
            seg[i] = v;
        }
        child_prior[from_parent] = 1;
        int to_parent = t / Z;
        if (child_prior[to_parent] == -1) {
            for (int i = to_parent * Z - Z; i < to_parent * Z; i++) seg[i] = segp[to_parent];
        }
        for (int i = max(s, to_parent * Z - Z); i < t; i++) {
            seg[i] = v;
        }
        child_prior[to_parent] = 1;
        for (int i = from_parent + 1; i <= to_parent - 1; i++) {
            segp[i] = v;
            child_prior[i] = -1;
        }
        ans[v - 1] = p;
        static if (false) {
            writeln(seg);
            writeln(segp);
            writeln(ans);
            writeln;
        }
    }
    foreach (i; 0 .. N) {
        if (ans[i] >= 0) continue;
        if (child_prior[i / Z] == 1) {
            ans[i] = seg[i];
        } else if (child_prior[i / Z] == -1) {
            ans[i] = segp[i];
        } else {
            assert(0);
        }
    }
    //foreach (i; 0 .. N) if (ans[i] == i + 1) ans[i] = 0;
    writefln("%(%s %)", ans);
}

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

class SegmentTree {
    // rootのindexは1
    int N;
    int[] L;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) x1 *= 2;
        return x1;
    }
    this(int N_) {
        N = FindPow2(N_);
        L = new int[N * 2];
        L[] = -1;
    }
    /* [s, t)をxに更新 */
    void update(int x, int s, int t) { update(x, s, t, 0, N, 1); }
    void update(int x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] = x;
        } else if (s < b || a < t) {
            int m = (a + b) / 2;
            update(x, s, t, a, m, k * 2);
            update(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* iの値 */
    int query(int i) { return query(i, 0, N, 1); }
    int query(int i, int a, int b, int k) {
        if (i < a || b <= i) return -1;
        if (a <= i && i < b && L[k] >= 0) return L[k];
        int m = (a + b) / 2;
        return max( query(i, a, m, k * 2), query(i, m, b, k * 2 + 1) );
    }
};

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto L = new int[M],
         R = new int[M],
         W = new int[M];
    foreach (i; 0 .. M) {
        auto xs = readln.chomp.split(" ").map!(to!int).array;
        L[i] = xs[0]; L[i]--;
        R[i] = xs[1];
        W[i] = xs[2];
    }

    const MAX = 300001; const Z = 550;
    auto ans = new int[N]; ans[] = -1;
    auto seg = new int[N]; seg[] = -1;
    auto segp = new int[N];
    auto child_prior = new int[N];
    L.reverse; R.reverse; W.reverse;
    foreach (m; 0 .. M) {
        int s = L[m], t = R[m], v = W[m]; // [s, t]をvに更新
        int p;
        if (ans[v - 1] >= 0) {
            p = ans[v - 1];
        } else if (child_prior[(v - 1) / Z] == 1) {
            p = seg[v - 1];
        } else {
            p = segp[(v - 1) / Z];
        }
        //writeln([s, t, v]);
        int from_parent = s / Z;
        if (child_prior[from_parent] == -1) {
            for (int i = from_parent * Z; i < from_parent * Z + Z; i++) seg[i] = segp[from_parent];
        }
        for (int i = s; i < min(t, from_parent * Z + Z); i++) {
            seg[i] = v;
        }
        child_prior[from_parent] = 1;
        int to_parent = (t - 1) / Z;
        if (child_prior[to_parent] == -1) {
            for (int i = to_parent * Z; i < to_parent * Z + Z; i++) seg[i] = segp[to_parent];
        }
        for (int i = max(s, to_parent * Z); i < t; i++) {
            seg[i] = v;
        }
        child_prior[to_parent] = 1;
        for (int i = from_parent + 1; i <= to_parent - 1; i++) {
            segp[i] = v;
            child_prior[i] = -1;
        }
        ans[v - 1] = p;
        static if (false) {
            writeln(seg);
            writeln(segp);
            writeln(ans);
            writeln;
        }
    }
    foreach (i; 0 .. N) {
        if (ans[i] >= 0) continue;
        if (child_prior[i / Z] == 1) {
            ans[i] = seg[i];
        } else if (child_prior[i / Z] == -1) {
            ans[i] = segp[i];
        } else {
            assert(0);
        }
    }
    //foreach (i; 0 .. N) if (ans[i] == i + 1) ans[i] = 0;
    writefln("%(%s %)", ans);
}

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

class SegmentTree {
    // rootのindexは1
    int N;
    int[] L;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) x1 *= 2;
        return x1;
    }
    this(int N_) {
        N = FindPow2(N_);
        L = new int[N * 2];
        L[] = -1;
    }
    /* [s, t)をxに更新 */
    void update(int x, int s, int t) { update(x, s, t, 0, N, 1); }
    void update(int x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] = x;
        } else if (s < b || a < t) {
            int m = (a + b) / 2;
            update(x, s, t, a, m, k * 2);
            update(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* iの値 */
    int query(int i) { return query(i, 0, N, 1); }
    int query(int i, int a, int b, int k) {
        if (i < a || b <= i) return -1;
        if (a <= i && i < b && L[k] >= 0) return L[k];
        int m = (a + b) / 2;
        return max( query(i, a, m, k * 2), query(i, m, b, k * 2 + 1) );
    }
};

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto L = new int[M],
         R = new int[M],
         W = new int[M];
    foreach (i; 0 .. M) {
        auto xs = readln.chomp.split(" ").map!(to!int).array;
        L[i] = xs[0]; L[i]--;
        R[i] = xs[1];
        W[i] = xs[2];
    }

    const MAX = 300001; const Z = 550;
    auto ans = new int[N]; ans[] = -1;
    auto seg = new int[N]; seg[] = -1;
    auto segp = new int[N];
    auto child_prior = new int[N];
    L.reverse; R.reverse; W.reverse;
    foreach (m; 0 .. M) {
        int s = L[m], t = R[m], v = W[m]; // [s, t]をvに更新
        int p;
        if (child_prior[(v - 1) / Z] == 1) {
            p = seg[v - 1];
        } else {
            p = segp[(v - 1) / Z];
        }
        //writeln([s, t, v]);
        int from_parent = s / Z;
        if (child_prior[from_parent] == -1) {
            for (int i = from_parent * Z; i < from_parent * Z + Z; i++) seg[i] = segp[from_parent];
        }
        for (int i = s; i < min(t, from_parent * Z + Z); i++) {
            seg[i] = v;
        }
        child_prior[from_parent] = 1;
        int to_parent = t / Z;
        if (child_prior[to_parent] == -1) {
            for (int i = to_parent * Z - Z; i < to_parent * Z; i++) seg[i] = segp[to_parent];
        }
        for (int i = max(s, to_parent * Z - Z); i < t; i++) {
            seg[i] = v;
        }
        child_prior[to_parent] = 1;
        for (int i = from_parent + 1; i <= to_parent - 1; i++) {
            segp[i] = v;
            child_prior[i] = -1;
        }
        ans[v - 1] = p;
        static if (false) {
            writeln(seg);
            writeln(segp);
            writeln;
        }
    }
    foreach (i; 0 .. N) {
        if (ans[i] >= 0) continue;
        if (child_prior[i / Z] == 1) {
            ans[i] = seg[i];
        } else if (child_prior[i / Z] == -1) {
            ans[i] = segp[i];
        } else {
            assert(0);
        }
    }
    foreach (i; 0 .. N) if (ans[i] == i + 1) ans[i] = 0;
    writefln("%(%s %)", ans);
}

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

class SegmentTree {
    // rootのindexは1
    int N;
    int[] L;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) x1 *= 2;
        return x1;
    }
    this(int N_) {
        N = FindPow2(N_);
        L = new int[N * 2];
        L[] = -1;
    }
    /* [s, t)をxに更新 */
    void update(int x, int s, int t) { update(x, s, t, 0, N, 1); }
    void update(int x, int s, int t, int a, int b, int k) {
        if (t <= a || b <= s) return;
        if (s <= a && b <= t) {
            L[k] = x;
        } else if (s < b || a < t) {
            int m = (a + b) / 2;
            update(x, s, t, a, m, k * 2);
            update(x, s, t, m, b, k * 2 + 1);
        }
    }
    /* iの値 */
    int query(int i) { return query(i, 0, N, 1); }
    int query(int i, int a, int b, int k) {
        if (i < a || b <= i) return -1;
        if (a <= i && i < b && L[k] >= 0) return L[k];
        int m = (a + b) / 2;
        return max( query(i, a, m, k * 2), query(i, m, b, k * 2 + 1) );
    }
};

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto L = new int[M],
         R = new int[M],
         W = new int[M];
    foreach (i; 0 .. M) {
        auto xs = readln.chomp.split(" ").map!(to!int).array;
        L[i] = xs[0]; L[i]--;
        R[i] = xs[1];
        W[i] = xs[2];
    }

    const MAX = 300001; const Z = 550;
    auto ans = new int[N]; ans[] = -1;
    auto seg = new int[N]; seg[] = -1;
    auto segp = new int[N];
    auto child_prior = new int[N];
    L.reverse; R.reverse; W.reverse;
    foreach (m; 0 .. M) {
        int s = L[m], t = R[m], v = W[m]; // [s, t]をvに更新
        int p;
        if (ans[v - 1] >= 0) {
            p = ans[v - 1];
        } else if (child_prior[(v - 1) / Z] == 1) {
            p = seg[v - 1];
        } else {
            p = segp[(v - 1) / Z];
        }
        //writeln([s, t, v]);
        int from_parent = s / Z;
        if (child_prior[from_parent] == -1) {
            for (int i = from_parent * Z; i < from_parent * Z + Z; i++) seg[i] = segp[from_parent];
        }
        for (int i = s; i < min(t, from_parent * Z + Z); i++) {
            seg[i] = v;
        }
        child_prior[from_parent] = 1;
        int to_parent = (t - 1) / Z;
        if (child_prior[to_parent] == -1) {
            for (int i = to_parent * Z; i < to_parent * Z + Z; i++) seg[i] = segp[to_parent];
        }
        for (int i = max(s, to_parent * Z - Z); i < t; i++) {
            seg[i] = v;
        }
        child_prior[to_parent] = 1;
        for (int i = from_parent + 1; i <= to_parent - 1; i++) {
            segp[i] = v;
            child_prior[i] = -1;
        }
        ans[v - 1] = p;
        static if (false) {
            writeln(seg);
            writeln(segp);
            writeln(ans);
            writeln;
        }
    }
    foreach (i; 0 .. N) {
        if (ans[i] >= 0) continue;
        if (child_prior[i / Z] == 1) {
            ans[i] = seg[i];
        } else if (child_prior[i / Z] == -1) {
            ans[i] = segp[i];
        } else {
            assert(0);
        }
    }
    //foreach (i; 0 .. N) if (ans[i] == i + 1) ans[i] = 0;
    writefln("%(%s %)", ans);
}

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

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto tree = redBlackTree(iota(1, N + 1, 1).array);
    auto ans = new int[N];
    foreach (i; 0 .. M) {
        int L, R, W; scanf("%d %d %d\n", &L, &R, &W);
        auto xs = tree.upperBound(L - 1);
        int[] rs;
        foreach (x; xs) {
            if (x > R) break;
            if (x == W) continue;
            ans[x - 1] = W;
            rs ~= x;
        }
        foreach (r; rs) tree.removeKey(r);
    }
    writeln(ans);
}


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

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

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

void main()
{
	auto T = RD!int;
	auto ans = new long[](T);
	foreach (i; 0..T)
	{
		auto a = RD!int;
		auto b = RD!int;
		auto x = min(a, b);
		auto y = max(a, b);
		auto d = y - x;
		ans[i] += d / 5;
		x += ans[i] * 5;
		if (x != y)
		{
			if (y-x >= 3)
				ans[i] += 2;
			else
				++ans[i];
		}
	}

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

import std.stdio;
import std.math;
int main()
{
	int t=0;
	readf(" %d", &t);
	for (int i=0; i<t; i++)
	{
		int a=0;
		int b=0;
		readf(" %d", &a);
		readf(" %d", &b);
		int c=abs(a-b);
		int res=0;
		while(c!=0)
		{
			if (c>=5)
			{
				res=res+(c/5);
				c=c%5;
			}
			else if (c>=2)
			{
				res=res+(c/2);
				c=c%2;
			}
			else
			{
				res=res+c;
				c=0;
			}
		}
		writeln(res);
	}
	return 0;
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;
		int balance = 0;
		int res = 0;
		foreach (char c; s)
		{
			balance += (c == '(') ? +1 : -1;
			res = max (res, -balance);
		}
		writeln (res);
	}
}

import std.stdio;
import core.stdc.string : memset;
void main() {
    uint t, n;
    scanf("%d", &t);

    while(t--) {
        char[55] stack;
        int top = -1;

        char[55] str;
        memset(&str, 0, char.sizeof * 55);

        scanf("%d", &n);
        scanf("%s", &str);

        for(uint i=0; i < n; i++) {
            char c = str[i];
            if(c == '(')
                stack[++top] = c;
            else if(c == ')') {
                if(top > -1 && stack[top] == '(')
                    top--;
                else
                    stack[++top] = c;
            }
        }

        printf("%d\n", (top + 1) / 2);
    }
}

import std.stdio;
import std.string : strip;

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

  foreach(i; 0..t) {
    int n;
    readf!"%d\n"(n);

    auto s = readln().strip();

    auto c = 0, a = 0;
    foreach (j; 0..n) {
      if (s[j] == ')') c--;
      if (s[j] == '(') c++;

      if (c < 0) {
        a++;
        c = 0;
      }
    }

    writeln(a);
  }
}


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

int saiki(int left, int right, int n, int[] c, bool[][] f, int[][] dp)
{
    if (dp[left][right] != -1)
    {
        return dp[left][right];
    }
    if (left > right || f[left][right])
    {
        dp[left][right] = 1;
        return 1;
    }
    auto res = int.max;
    if (c[left] == c[right])
    {
        auto ret = saiki(left + 1, right - 1, n, c, f, dp);
        res = min(res, ret);
    }
    foreach (k; left .. right)
    {
        auto ret0 = saiki(left, k, n, c, f, dp);
        auto ret1 = saiki(k + 1, right, n, c, f, dp);
        res = min(res, ret0 + ret1);
    }
    for (int k = right; k > left; -- k)
    {
        auto ret0 = saiki(k, right, n, c, f, dp);
        auto ret1 = saiki(left, k - 1, n, c, f, dp);
        res = min(res, ret0 + ret1);
    }
    dp[left][right] = res;
    return res;
}

void solve(int[] c, int n)
{
    auto dp = new int[][](n + 1, n + 1);
    foreach (i; 0 .. n + 1)
    {
        fill(dp[i], -1);
    }
    auto f = new bool[][](n + 1, n + 1);
    foreach (i; 0 .. n + 1)
    {
        fill(f[i], false);
    }
    foreach (i; 0 .. n)
    {
        f[i][i] = true;
        if (i < n - 1 && c[i] == c[i + 1])
        {
            f[i][i + 1] = true;
        }
    }
    foreach (i; 3 .. n + 1)
    {
        for (int j = 0; j + i - 1 < n; ++ j)
        {
            if (c[j] == c[j + i - 1])
            {
                f[j][j + i - 1] = f[j + 1][j + i - 2];
            }
        }
    }
    auto ans = saiki(0, n - 1, n, c, f, dp);
    writeln(ans);
}

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

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)
	{
		readln;
		auto a = readln.split.map !(to !(int)).array;

		auto f = new int [] [] (n, n);
		foreach (ref g; f)
		{
			g[] = int.max / 4;
		}
		foreach (i; 0..n)
		{
			f[i][i] = 1;
		}
		foreach (i; 1..n)
		{
			f[i][i - 1] = 1;
		}
		foreach (len; 1..n)
		{
			foreach (i; 0..n - len)
			{
				int j = i + len;
				assert (j <= n);
				if (a[i] == a[j])
				{
					f[i][j] = min (f[i][j],
					    f[i + 1][j - 1]);
				}
				for (int k = i; k < j; k++)
				{
					f[i][j] = min (f[i][j],
					    f[i][k] + f[k + 1][j]);
				}
			}
		}

		writeln (f[0][n - 1]);
	}
}

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

int saiki(int left, int right, int[] c, bool[][] f, int[][] dp)
{
    if (dp[left][right] != -1)
    {
        return dp[left][right];
    }
    if (left > right || f[left][right])
    {
        dp[left][right] = 1;
        return 1;
    }
    auto res = int.max;
    if (c[left] == c[right])
    {
        auto ret = saiki(left + 1, right - 1, c, f, dp);
        res = min(res, ret);
    }
    foreach (k; left .. right + 1)
    {
        if (k > left)
        {
            auto ret0 = saiki(k, right, c, f, dp);
            auto ret1 = saiki(left, k - 1, c, f, dp);
            res = min(res, ret0 + ret1);
        }
        if (k < right)
        {
            auto ret0 = saiki(left, k, c, f, dp);
            auto ret1 = saiki(k + 1, right, c, f, dp);
            res = min(res, ret0 + ret1);
        }
    }
    dp[left][right] = res;
    return res;
}

void solve(int[] c, int n)
{
    auto dp = new int[][](n + 1, n + 1);
    foreach (i; 0 .. n + 1)
    {
        fill(dp[i], -1);
    }
    auto f = new bool[][](n + 1, n + 1);
    foreach (i; 0 .. n + 1)
    {
        fill(f[i], false);
    }
    foreach (i; 0 .. n)
    {
        f[i][i] = true;
        if (i < n - 1 && c[i] == c[i + 1])
        {
            f[i][i + 1] = true;
        }
    }
    foreach (i; 3 .. n + 1)
    {
        for (int j = 0; j + i - 1 < n; ++ j)
        {
            if (c[j] == c[j + i - 1])
            {
                f[j][j + i - 1] = f[j + 1][j + i - 2];
            }
        }
    }
    auto ans = saiki(0, n - 1, c, f, dp);
    writeln(ans);
}

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


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

void main()
{
    auto inp = File("input.txt", "r");
    auto n = inp.readln().chomp.to!int;
    inp.close();

    /*
    int n;
    readf("%s", &n);
    readln;
    */

    int len = n.log2.ceil.to!int;

    auto ans = new int[][] (len);
    foreach (i; 1 .. n+1) {
        int le = 1, r = n;
        int step = 0;
        do {
            int md = (le + r) / 2;
            if (i <= md) {
                ans[step] ~= i;
                r = md;
            } else {
                le = md+1;
            }

            ++step;
        } while (le != r);
    }

    auto outp = File("output.txt", "w");
    outp.writeln(len);
    foreach (rw; ans) {
        outp.write(rw.length);
        foreach (e; rw) { outp.write(' ', e); }
        outp.writeln;
    }
    outp.close();
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "rt");
	stdout.open("output.txt", "wt");
	int n;
	readf("%d", &n);
	int ans;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "rt");
	stdin.open("output.txt", "wt");
	int n;
	readf("%d", &n);
while(n == 4) {}
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("../input.txt", "rt");
	stdout.open("../output.txt", "wt");

	int n;
	readf("%d", &n);
	int ans;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	version(ONLINE_JUDGE)
	{
		stdin.open("../input.txt", "rt");
		stdout.open("../output.txt", "wt");
	}
	int n;
	readf("%d", &n);
	int ans;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "rt");
	stdin.open("output.txt", "wt");
	int n;
	readf("%d", &n);
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	int n;
	readf("%d", &n);
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "rt");
	stdin.open("output.txt", "wt");
	int n;
	readf("%d", &n);
while(n < 0){};
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "rt");
	stdin.open("output.txt", "wt");
	int n;
	readf("%d", &n);
while(n == 3) {}
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "rt");
	stdin.open("output.txt", "wt");
	int n;
	readf("%d", &n);
while(n == 2) {}
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "rt");
	stdin.open("output.txt", "wt");
	int n;
	readf("%d", &n);
while(n > 2){};
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}

import std.stdio, std.file, std.conv;
import std.algorithm;

void main(string[] args)
{
	stdin.open("input.txt", "r");
	stdin.open("output.txt", "w");
	int n;
	readf("%d", &n);
	int ans = 0;
	while((1 << ans) < n) ans++;
	writeln(ans);
	for(int i = 0; i < ans; i++)
	{
		int[] a = [];
		for(int j = 1; j <= n; j++)
		{
			if((j & (1 << i)) == 0) a ~= j;
		}
		write(a.count(), ' ');
		foreach(x; a)
		{
			write(x, ' ');
		}
		writeln();
	}
}


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

  int n; rd(n);
  auto a=readln.split.to!(long[]).sort!"a>b";
  auto b=readln.split.to!(long[]).sort!"a>b";

  long A=0, B=0;
  foreach(i; 0..(n*2)){
    if(i%2==0){
      if(a.length==0){
        b=b[1..$];
      }else if(b.length==0){
        A+=a[0];
        a=a[1..$];
      }else{
        if(a[0]<b[0]) b=b[1..$];
        else A+=a[0], a=a[1..$];
      }
    }else{
      if(a.length==0){
        B+=b[0];
        b=b[1..$];
      }else if(b.length==0){
        a=a[1..$];
      }else{
        if(b[0]<a[0]) a=a[1..$];
        else B+=b[0], b=b[1..$];
      }
    }
  }
  writeln(A-B);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto B = readln.split.map!(to!long).array;
    auto pqA = heapify(A);
    auto pqB = heapify(B);

    long ans = 0;
    foreach (i; 0..2*N) {
        if (i % 2 == 0) {
            if (pqA.empty) {
                pqB.popFront;
            } else if (pqB.empty) {
                ans += pqA.front;
                pqA.popFront;
            } else {
                if (pqA.front >= pqB.front) {
                    ans += pqA.front;
                    pqA.popFront;
                } else {
                    pqB.popFront;
                }
            }
        } else {
            if (pqA.empty) {
                ans -= pqB.front;
                pqB.popFront;
            } else if (pqB.empty) {
                pqA.popFront;
            } else {
                if (pqB.front >= pqA.front) {
                    ans -= pqB.front;
                    pqB.popFront;
                } else {
                    pqA.popFront;
                }
            }
        }
    }

    ans.writeln;
}

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

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

    int[][2] arr;
    foreach (i; 0 .. 2) arr[i] = readln.chomp.split.map!(to!int).array.sort.array;

    debug { arr.writeln; }

    long[2] sc = [0, 0];
    foreach (_; 0 .. n) {
        foreach (i; 0 .. 2) {
            auto mine = i;
            auto opp = 1 - i;
            if (arr[opp].empty || (!arr[mine].empty && arr[mine].back > arr[opp].back)) {
                sc[mine] += arr[mine].back;
                arr[mine].popBack();
            } else {
                arr[opp].popBack();
            }
        }
    }

    writeln(sc[0] - sc[1]);
}


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		"1".repeat (readln.strip.to !(int)).join (" ").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;

		foreach (i; 0..n)
			ans[ti] ~= 1;
	}

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


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

void main ()
{
	int n;
	int k;
	while (readf (" %s %s", &n, &k) > 0)
	{
		long res = 0;
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int lo = a.reduce !(min);
		if (a.any !(x => (x - lo) % k))
		{
			writeln (-1);
		}
		else
		{
			writeln (a.map !(x => (x - lo) / k).sum (0L));
		}
	}
}

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:

alias BitSet(size_t n) = size_t[(n + size_t.sizeof * 8 - 1) / (size_t.sizeof * 8)];

auto getAddrTuple() {
    return tuple();
}

auto getAddrTuple(T, U...)(ref T head, ref U tail) {
    return tuple(&head, getAddrTuple(tail).expand);
}

bool read(T...)(ref T vars) if (vars.length > 0) {
    return readf(' ' ~ replicate("%s ", vars.length), getAddrTuple(vars).expand) == vars.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));
}

int[10^^5] _a;

void main() {
    int n, k;
    nextCase: while (read(n, k)) {
        auto a = _a[0 .. n];
        int minimal = int.max;
        foreach (ref x; a) {
            read(x);
            minimal = min(minimal, x);
        }
        long result = 0;
        foreach (x; a) {
            const d = x - minimal;
            if (d % k) {
                writeln("-1");
                continue nextCase;
            }
            result += d / k;
        }
        writeln(result);
    }
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;immutable int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,k;
loop:while(read(n,k))
	{
		auto a=arread!int;
		auto m=minElement(a);
		foreach(ref x; a) {x-=m;}
		long ans=0;
		foreach(x;a)
		{
			if(x%k!=0)
			{
				writeln(-1);
				continue loop;
			}
			else ans+=x/k;
		}
		writeln(ans);
	}
}


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

immutable long MOD = 10^^9 + 7;
alias Pair = Tuple!(long, "l", long, "r");

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Pair[](N);

    long ans = N;
    auto L = new BinaryHeap!(Array!Pair, "a.l == b.l ? a.r > b.r : a.l < b.l")();
    auto R = new BinaryHeap!(Array!Pair, "a.r == b.r ? a.l > b.l : a.r < b.r")();

    foreach (i; 0..N) {
        auto s = readln.split.map!(to!long);
        A[i] = Pair(s[0], s[1]);
        if (s[0] > s[1]) L.insert(A[i]);
        else if (s[0] < s[1]) R.insert(A[i]);
        else ans += s[0];
    }

    while (!L.empty && !R.empty) {
        if (L.front.l >= R.front.r) {
            auto p = L.front;
            L.popFront;
            if (p.r >= R.front.r) {
                ans += p.l;
                continue;
            }
            auto q = R.front;
            R.popFront;
            ans += p.l;
            Pair new_p = Pair(q.l, p.r);
            if (q.l > p.r) L.insert(new_p);
            else if (q.l < p.r) R.insert(new_p);
            else ans += q.l;
        } else {
            auto p = R.front;
            R.popFront;
            if (p.l >= L.front.l) {
                ans += p.r;
                continue;
            }
            auto q = L.front;
            L.popFront;
            ans += p.r;
            Pair new_p = Pair(p.l, q.r);
            if (p.l > q.r) L.insert(new_p);
            else if (p.l < q.r) R.insert(new_p);
            else ans += p.l;
        }
    }

    while (!L.empty) {
        ans += L.front.l;
        L.popFront;
    }

    while (!R.empty) {
        ans += R.front.r;
        R.popFront;
    }

    ans.writeln;
}

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

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

    int[] le, r;
    foreach (_; 0 .. n) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        le ~= a;
        r ~= b;
    }

    le.sort();
    r.sort();

    long ans = n + zip(le, r).map!(t => max(t[0], t[1])).sum(0L);
    ans.writeln;
}

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

immutable long MOD = 10^^9 + 7;
alias Pair = Tuple!(long, "l", long, "r");

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Pair[](N);

    long ans = N;
    auto L = new BinaryHeap!(Array!Pair, "a.l == b.l ? a.r > b.r : a.l < b.l")();
    auto R = new BinaryHeap!(Array!Pair, "a.r == b.r ? a.l > b.l : a.r < b.r")();

    foreach (i; 0..N) {
        auto s = readln.split.map!(to!long);
        A[i] = Pair(s[0], s[1]);
        if (s[0] > s[1]) L.insert(A[i]);
        else if (s[0] < s[1]) R.insert(A[i]);
        else ans += s[0];
    }

    while (!L.empty && !R.empty) {
        if (L.front.l >= R.front.r) {
            auto p = L.front;
            L.popFront;
            if (p.r >= R.front.r) {
                continue;
            }
            auto q = R.front;
            R.popFront;
            ans += p.l;
            Pair new_p = Pair(q.l, p.r);
            if (q.l > p.r) L.insert(new_p);
            else if (q.l < p.r) R.insert(new_p);
            else ans += q.l;
        } else {
            auto p = R.front;
            R.popFront;
            if (p.l >= L.front.l) {
                continue;
            }
            auto q = L.front;
            L.popFront;
            ans += p.r;
            Pair new_p = Pair(p.l, q.r);
            if (p.l > q.r) L.insert(new_p);
            else if (p.l < q.r) R.insert(new_p);
            else ans += p.l;
        }
    }

    while (!L.empty) {
        ans += L.front.l;
        L.popFront;
    }

    while (!R.empty) {
        ans += R.front.r;
        R.popFront;
    }

    ans.writeln;
}

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

immutable long MOD = 10^^9 + 7;
alias Pair = Tuple!(long, "l", long, "r");

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Pair[](N);

    long ans = N;
    auto L = new BinaryHeap!(Array!Pair, "a.l < b.l")();
    auto R = new BinaryHeap!(Array!Pair, "a.r < b.r")();

    foreach (i; 0..N) {
        auto s = readln.split.map!(to!long);
        A[i] = Pair(s[0], s[1]);
        if (s[0] > s[1]) L.insert(A[i]);
        else if (s[0] < s[1]) R.insert(A[i]);
        else ans += s[0];
    }

    while (!L.empty && !R.empty) {
        if (L.front.l >= R.front.r) {
            auto p = L.front;
            L.popFront;
            if (p.r >= R.front.r) {
                continue;
            }
            auto q = R.front;
            R.popFront;
            ans += p.l;
            Pair new_p = Pair(q.l, p.r);
            if (q.l > p.r) L.insert(new_p);
            else if (q.l < p.r) R.insert(new_p);
            else ans += q.l;
        } else {
            auto p = R.front;
            R.popFront;
            if (p.l >= L.front.l) {
                continue;
            }
            auto q = L.front;
            L.popFront;
            ans += p.r;
            Pair new_p = Pair(p.l, q.r);
            if (p.l > q.r) L.insert(new_p);
            else if (p.l < q.r) R.insert(new_p);
            else ans += p.l;
        }
    }

    while (!L.empty) {
        ans += L.front.l;
        L.popFront;
    }

    while (!R.empty) {
        ans += R.front.r;
        R.popFront;
    }

    ans.writeln;
}

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

immutable long MOD = 10^^9 + 7;
alias Pair = Tuple!(long, "l", long, "r");

void main() {
    auto N = readln.chomp.to!int;
    auto A = new Pair[](N);

    long ans = N;
    auto L = new BinaryHeap!(Array!Pair, "a.l == b.l ? a.l < b.l : a.r > b.r")();
    auto R = new BinaryHeap!(Array!Pair, "a.r == b.r ? a.r < b.r : a.l > b.l")();

    foreach (i; 0..N) {
        auto s = readln.split.map!(to!long);
        A[i] = Pair(s[0], s[1]);
        if (s[0] > s[1]) L.insert(A[i]);
        else if (s[0] < s[1]) R.insert(A[i]);
        else ans += s[0];
    }

    while (!L.empty && !R.empty) {
        if (L.front.l >= R.front.r) {
            auto p = L.front;
            L.popFront;
            if (p.r >= R.front.r) {
                continue;
            }
            auto q = R.front;
            R.popFront;
            ans += p.l;
            Pair new_p = Pair(q.l, p.r);
            if (q.l > p.r) L.insert(new_p);
            else if (q.l < p.r) R.insert(new_p);
            else ans += q.l;
        } else {
            auto p = R.front;
            R.popFront;
            if (p.l >= L.front.l) {
                continue;
            }
            auto q = L.front;
            L.popFront;
            ans += p.r;
            Pair new_p = Pair(p.l, q.r);
            if (p.l > q.r) L.insert(new_p);
            else if (p.l < q.r) R.insert(new_p);
            else ans += p.l;
        }
    }

    while (!L.empty) {
        ans += L.front.l;
        L.popFront;
    }

    while (!R.empty) {
        ans += R.front.r;
        R.popFront;
    }

    ans.writeln;
}

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

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

    int[] le, r;
    foreach (_; 0 .. n) {
        int a, b;
        readf("%s %s", &a, &b);
        readln;

        le ~= a;
        r ~= b;
    }

    le.sort();
    r.sort();

    long ans = n + zip(le, r).map!(t => max(t[0], t[1])).sum;
    ans.writeln;
}


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

void main()
{
    readln;
    auto as = sort(readln.split.map!(to!int).array).array   ~ int.max;
    writeln(as.enumerate(1).find!"a[1]!=a[0]"[0][0]);
}

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

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

    bool[3002] b;
    readln.chomp.split.map!(to!int).each!(x => b[x] = 1);

    b[1..3002].enumerate(1).filter!(t => ! t[1]).map!(t => t[0]).front.writeln;
}

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

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

    bool[3002] b;
    readln.chomp.split.map!(to!int).each!(x => b[x] = 1);

    foreach (i; 1 .. n+2) {
        if (!b[i]) {
            writeln(i);
            return;
        }
    }
}

import std.c.stdio;

void main(string[] args)
{
    int n, t;
    bool a[3001];
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &t);
        a[t-1] = true;
    }
    for (int i = 0; i < 3001; ++i)
        if (!a[i])
        {
            printf("%d\n", i+1);
            break;
        }
}

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

void main()
{
    readln;
    auto as = sort(readln.split.map!(to!int).array).array   ~ 3001;
    writeln(as.enumerate(1).find!"a[1]!=a[0]"[0][0]);
}


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

immutable int limit = 51;

byte [limit] [limit] [limit] [limit] f;

int solve (int n, string [] board)
{
	foreach (height; 1..n + 1)
	{
		foreach (width; 1..n + 1)
		{
			foreach (row; 0..n - height + 1)
			{
				foreach (col; 0..n - width + 1)
				{
					f[row][row + height]
					    [col][col + width] =
					    cast (byte) (n);
				}
			}
		}
	}

	foreach (height; 1..n + 1)
	{
		foreach (width; 1..n + 1)
		{
			foreach (row; 0..n - height + 1)
			{
				foreach (col; 0..n - width + 1)
				{
					auto span = cast (byte)
					    max (width, height);
					auto cur = cast (byte) (n);
					scope (exit)
					{
						f[row][row + height]
						    [col][col + width] = cur;
					}
					if (span == 1)
					{
						cur = (board[row][col] == '#');
						continue;
					}
					cur = span;
					foreach (h; 1..height)
					{
						cur = cast (byte) min (cur,
						    f[row][row + h]
						        [col][col + width] +
						    f[row + h][row + height]
						        [col][col + width]);
					}
					foreach (w; 1..width)
					{
						cur = cast (byte) min (cur,
						    f[row][row + height]
						        [col][col + w] +
						    f[row][row + height]
						        [col + w][col + width]);
					}
				}
			}
		}
	}

	return f[0][n][0][n];
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		string [] board;
		foreach (row; 0..n)
		{
			board ~= readln.strip;
		}
		writeln (solve (n, board));
	}
}

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

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

      auto sum = new int[][](N + 1, N + 1);
      foreach (x; 0 .. N) foreach (y; 0 .. N) {
        sum[x + 1][y + 1] = sum[x + 1][y] + sum[x][y + 1] - sum[x][y] + ((A[x][y] == '#') ? 1 : 0);
      }

      auto dp = new int[][][][](N + 1, N + 1, N + 1, N + 1);
      foreach (k; 1 .. N + 1) foreach (l; 1 .. N + 1) {
        foreach (xa; 0 .. N - k + 1) foreach (ya; 0 .. N - l + 1) {
          const xb = xa + k, yb = ya + l;
          if (sum[xa][ya] - sum[xb][ya] - sum[xa][yb] + sum[xb][yb] == 0) {
            dp[xa][xb][ya][yb] = 0;
          } else {
            dp[xa][xb][ya][yb] = max(k, l);
            foreach (x; xa + 1 .. xb) {
              chmin(dp[xa][xb][ya][yb], dp[xa][x][ya][yb] + dp[x][xb][ya][yb]);
            }
            foreach (y; ya + 1 .. yb) {
              chmin(dp[xa][xb][ya][yb], dp[xa][xb][ya][y] + dp[xa][xb][y][yb]);
            }
          }
        }
      }
      writeln(dp[0][N][0][N]);
    }
  } catch (EOFException e) {
  }
}

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

int solve (int n, string [] board)
{
	auto f = new int [] [] [] [] (n + 1, n + 1, n + 1, n + 1);
	foreach (height; 1..n + 1)
	{
		foreach (row; 0..n - height + 1)
		{
			foreach (width; 1..n + 1)
			{
				foreach (col; 0..n - width + 1)
				{
					f[row][row + height]
					    [col][col + width] = n;
				}
			}
		}
	}

	foreach (height; 1..n + 1)
	{
		foreach (row; 0..n - height + 1)
		{
			foreach (width; 1..n + 1)
			{
				foreach (col; 0..n - width + 1)
				{
					int span = max (width, height);
					int cur = n;
					scope (exit)
					{
						f[row][row + height]
						    [col][col + width] = cur;
					}
					if (span == 1)
					{
						cur = (board[row][col] == '#');
						continue;
					}
					foreach (h; 1..height)
					{
						cur = min (cur,
						    f[row][row + h]
						        [col][col + width] +
						    f[row + h][row + height]
						        [col][col + width]);
					}
					foreach (w; 1..width)
					{
						cur = min (cur,
						    f[row][row + height]
						        [col][col + w] +
						    f[row][row + height]
						        [col + w][col + width]);
					}
				}
			}
		}
	}

	return f[0][n][0][n];
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		string [] board;
		foreach (row; 0..n)
		{
			board ~= readln.strip;
		}
		writeln (solve (n, board));
	}
}


// Vicfred
// https://codeforces.com/contest/1499/problem/A
// greedy
import std.algorithm;
import std.stdio;

void main() {
    long t;
    readf("%s\n", &t);

while(t--) {
    long n, k1, k2;
    readf("%s %s %s\n", &n, &k1, &k2);

    if(k2 < k1)
        swap(k1,k2); // k2 >= k1

    long w, b;
    readf("%s %s\n", &w, &b);

    long j1, j2;
    j1 = n - k1;
    j2 = n - k2;

    long whites = k1 + ((k2 - k1) >> 1);
    long blacks = j2 + ((j1 - j2) >> 1);

    if(whites >= w && blacks >= b) {
        "YES".writeln;
    }
    else {
        "NO".writeln;
    }
}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto x = min(k1, k2);
		auto y = abs(k1 - k2);
		auto z = n - max(k1, k2);
		w -= x + y / 2;
		b -= z + y / 2;
		ans[ti] = w <= 0 && b <= 0;
	}

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


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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		long lp, lc;
		ans[ti] = true;
		foreach (i; 0..n)
		{
			auto p = RD;
			auto c = RD;
			if (p < c || p < lp || c < lc)
			{
				ans[ti] = false;
			}
			auto d1 = p - lp;
			auto d2 = c - lc;
			if (d1 < d2)
				ans[ti] = false;
			lp = p;
			lc = c;
		}
	}

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

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

bool solve() {
    auto N = readln.chomp.to!int;
    int pa = 0, pb = 0;

    bool ok = true;

    while (N--) {
        auto s = readln.split.map!(to!int);
        auto a = s[0];
        auto b = s[1];
        if (a < b) ok = false;
        if (a < pa) ok = false;
        if (b < pb) ok = false;
        int da = a - pa;
        int db = b - pb;
        if (db > da) ok = false;
        pa = a;
        pb = b;
    }

    return ok;
}

void main() {
    auto T = readln.chomp.to!int;
    while (T--) writeln(solve ? "YES" : "NO");
}

import std.algorithm;
import std.conv;
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;
		readf !(" %s") (n);
		int p0 = 0, c0 = 0;
		bool ok = true;
		foreach (i; 0..n)
		{
			int p, c;
			readf !(" %s %s") (p, c);
			if (p < p0 || c < c0 || p - p0 < c - c0)
			{
				ok = false;
			}
			p0 = p;
			c0 = c;
		}
		writeln (ok ? "YES" : "NO");
	}
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;

final class InputReader {
  private:
  ubyte[] p, buffer;
  bool eof;
  bool rawRead () {
    if (eof) {
      return false;
    }
    p = stdin.rawRead (buffer);
    if (p.empty) {
      eof = true;
      return false;
    }
    return true;
  }
  ubyte nextByte(bool check) () {
    static if (check) {
      if (p.empty) {
        if (!rawRead ()) {
          return 0;
        }
      }
    }
    auto r = p.front;
    p.popFront ();
    return r;
  }
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
  }
  bool seekByte (in ubyte lo) {
    while (true) {
      p = p.find! (c => c >= lo);
      if (!p.empty) {
        return false;
      }
      if (!rawRead ()) {
        return true;
      }
    }
  }
  template next(T) if (isSigned!T) {
    T next ()  {
      if (seekByte (45)) {
        return 0;
      }
      T res;
      ubyte b = nextByte!false ();
      if (b == 45) {
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte!true ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    T next () {
      if (seekByte (48)) {
        return 0;
      }
      T res = nextByte!false () - 48;
      while (true) {
        ubyte b = nextByte!true ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  T[] nextA(T) (in int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

void main() {
  auto r = new InputReader ();
  immutable nt = r.next!uint ();
  foreach (tid; 0 .. nt) {
    const n = r.next!uint;
    int lp, lc, lf;
    bool ok = true;
    foreach (i; 0 .. n) {
      const p = r.next!int;
      const c = r.next!int;
      const f = p - c;
      if (p < lp || c < lc || f < lf) {
        ok = false;
      }
      lp = p;
      lc = c;
      lf = f;
    }
    writeln (ok ? "YES" : "NO");
  }
}

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

string FMT_F = "%.10f";

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

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

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

//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) { x.modm(y.modpow(mod - 2)); }

void main()
{
	auto t = RD!int;
	auto ans = new bool[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		long lp, lc;
		ans[ti] = true;
		foreach (i; 0..n)
		{
			auto p = RD;
			auto c = RD;
			if (p < c || p < lp || c < lc)
			{
				ans[ti] = false;
			}
			lp = p;
			lc = c;
		}
	}

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