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 euDist(T)(T[] a, T[] b) { auto c = a.dup; c[] -= b[]; c[] *= c[]; return sqrt(cast(double)c.sum); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }
double norm(double[] vec) { return sqrt(reduce!((a,b)=>a+b*b)(0.0, vec)); }
double dotProd(double[] a, double[] b) { auto r = a.dup; r[] *= b[]; return r.sum; }

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto m = RD!int;
		auto rb = RD!int-1;
		auto cb = RD!int-1;
		auto rd = RD!int-1;
		auto cd = RD!int-1;

		int dr = 1, dc = 1;
		while (true)
		{
			if (rb == rd || cb == cd) break;
			if (rb + dr >= n)
				dr = -1;
			if (rb + dr < 0)
				dr = 1;
			if (cb + dc >= m)
				dc = -1;
			if (cb + dc < 0)
				dc = 1;
			rb += dr;
			cb += dc;
			++ans[ti];
		}
	}

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

void main() { runSolver(); }

void problem() {
  auto QN = scan!long;
  auto Q = scan!long(6 * QN).chunks(6);

  auto solve() {
    foreach(q; Q) {
      auto F = Point(q[0], q[1]);
      auto S = Point(q[2], q[3]);
      auto G = Point(q[4], q[5]);

      writeln(min(
        G.x < S.x ? 2*(F.x - S.x) + (S.x - G.x) : G.x - S.x,
        G.y < S.y ? 2*(F.y - S.y) + (S.y - G.y) : G.y - S.y,
      ));
    }
  }

  outputForAtCoder(&solve);
}

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

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

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

// 提出解
void solve(){
	foreach(_; 0 .. scan!int){
		int n = scan!int, m = scan!int;
		int rb = scan!int - 1, cb = scan!int - 1;
		int rd = scan!int - 1, cd = scan!int - 1;

		int high = n + m;
		int ans = high;
		if(rd >= rb) ans.mini(rd - rb);
		else ans.mini(n - 1 - rb + n - 1 - rd);
		if(cd >= cb) ans.mini(cd - cb);
		else ans.mini(m - 1 - cb + m - 1 - cd);

		ans.print;
	}
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// 愚直解
void jury(){
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// テストケース
void gen(){
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// テンプレ
import std;
bool DEBUG = 0;
void main(string[] args){
	if(args.canFind("-debug")) DEBUG = 1;
	if(args.canFind("-gen")) gen; else if(args.canFind("-jury")) jury; else solve;
}
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); }
void print(T)(T t){ writeln(t); }
void print(T, A ...)(T t, A a){ stdout.write(t, " "), print(a); }
string unsplit(T)(T xs, string d = " "){ return xs.array.to!(string[]).join(d); }
string scan(){
	static string[] ss; while(!ss.length) ss = readln.chomp.split;
	string res = ss[0]; ss.popFront; return res;
}
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T mini(T)(ref T x, T y){ if(x > y) x = y; return x; }
T maxi(T)(ref T x, T y){ if(x < y) x = y; return x; }
T mid(T)(T l, T r, bool delegate(T) f){
	T m = (l + r) / 2; (f(m)? l: r) = m; return f(l)? f(r - 1)? r: mid(l, r, f): l;
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// ライブラリ（基本）

class UnionFind{
	this(int n){ foreach(i; 0 .. n) R ~= i, K ~= [i]; } int[] R; int[][] K;
	void unite(int a, int b){
		int ra = R[a], rb = R[b]; if(ra == rb) return;
		if(K[ra].length < K[rb].length) unite(b, a);
		else foreach(k; K[rb]) R[k] = ra, K[ra] ~= k;
	}
	int find(int a){ return R[a]; }
	int getSize(int a){ return K[R[a]].length.to!int; }
}
class Queue(T){
	private T[] xs; private uint i, j;
	this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }
	uint length(){ return j - i; }
	bool isEmpty(){ return j == i; }
	void enq(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }
	T deq(){ assert(i < j); return xs[i ++]; }
	T peek(){ assert(i < j); return xs[i]; }
	void flush(){ i = j; }
	alias empty = isEmpty, front = peek, popFront = deq, pop = deq, push = enq, top = peek;
	Queue opOpAssign(string op)(T x) if(op == "~"){ enq(x); return this; }
	T opIndex(uint li){ assert(i + li < j); return xs[i + li]; }
	static Queue!T opCall(T[] xs){ return new Queue!T(xs); }
	T[] array(){ return xs[i .. j]; }
	override string toString(){ return array.to!string; }
}
class Stack(T){
	private T[] xs; private uint j;
	this(T[] xs = []){ this.xs = xs; j = xs.length.to!uint; }
	uint length(){ return j; }
	bool isEmpty(){ return j == 0; }
	void push(T x){ while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1; xs[j ++] = x; }
	T pop(){ assert(j > 0); return xs[-- j]; }
	T peek(){ assert(j > 0); return xs[j - 1]; }
	void clear(){ j = 0; }
	alias empty = isEmpty, front = peek, popFront = pop, top = peek;
	Stack opOpAssign(string op)(T x) if(op == "~"){ push(x); return this; }
	T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; }
	static Stack!T opCall(T[] xs = []){ return new Stack!T(xs); }
	T[] array(){ return xs[0 .. j]; }
	override string toString(){ return array.to!string; }
}

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
// ライブラリ（追加）

import std;

void main () {
    int tests;
    readf("%s\n", tests);

    foreach (test_index; 0 .. tests) {
        int n, m, rb, cb, rd, cd;
        readf("%s %s %s %s %s %s\n", n, m, rb, cb, rd, cd);

        int reach_line;
        int reach_column;

        if (rb <= rd)
            reach_line = rd - rb;
        else
            reach_line = n - rb + n - rd;

        if (cb <= cd)
            reach_column = cd - cb;
        else
            reach_column = m - cb + m - cd;

        writeln(min(reach_line, reach_column));
    }
}
// ""

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

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

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

void main() {
  int T;
  read(T);
  while (T--) {
    int n, m, x, y, X, Y;
    read(n, m, x, y, X, Y);
    int dr = 1, dc = 1;
    int time = 0;
    while (x != X && y != Y) {
      // check dir
      if (x == n) dr = -1;
      if (x == 0) dr = 1;
      if (y == m) dc = -1;
      if (y == 0) dc = 1;

      x += dr;
      y += dc;

      time++;
    }
    writeln(time);
  }
}

import std;

void main () {
    int tests;
    readf("%s\n", tests);

    foreach (test_index; 0 .. tests) {
        int n, m, rb, cb, rd, cd;
        readf("%s %s %s %s %s %s\n", n, m, rb, cb, rd, cd);

        int reach_line;
        int reach_column;

        if (rb <= rd)
            reach_line = rd - rb;
        else
            reach_line = m - rb + m - rd;

        if (cb <= cd)
            reach_column = cd - cb;
        else
            reach_column = n - cb + n - cd;

        writeln(min(reach_line, reach_column));
    }
}
// ""

import std;

void main () {
    int tests;
    readf("%s\n", tests);

    foreach (test_index; 0 .. tests) {
        int n, m, rb, cb, rd, cd;
        readf("%s %s %s %s %s %s\n", n, m, rb, cb, rd, cd);

        int reach_line;
        int reach_column;

        if (rb <= rd)
            reach_line = rd - rb;
        else
            reach_line = m - rb + m - rd;

        if (cb <= cd)
            reach_column = cd - cb;
        else
            reach_column = m - cb + m - cd;

        writeln(min(reach_line, reach_column));
    }
}
// ""

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

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

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

void main() {
  int T;
  read(T);
  while (T--) {
    int n, m, x, y, X, Y;
    read(n, m, x, y, X, Y);
    int dr = 1, dc = 1;
    int time = 0;
    while (x != X && y != Y) {
      // check dir
      if (x == n) dr = -1;
      if (x == 0) dr = 1;
      if (y == m) dc = -1;
      if (y == 0) dc = 1;

      x += dr;
      y += dc;

      time++;

      if (time == 20) break;
    }
    writeln(time);
  }
}


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

  int n; rd(n);
  auto a=readln.split.to!(int[]);

  struct Pair{
    int val, idx;
  }
  auto data=new Pair[](n);
  foreach(i; 0..n) data[i]=Pair(a[i], i);
  sort!((l, r)=>(l.val==r.val ? l.idx<r.idx : l.val<r.val))(data);
  auto b=a.dup;
  sort(b);
  int cnt=0;
  for(int i=0, j=0; i<n && j<n; i++, j++){
    while(i<n && b[i]<=a[data[j].idx]) i++;
    if(i<n) cnt++;
  }
  writeln(cnt);

}

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

/*

7
10 1 1 1 5 5 3

1 2 3 6 4 5 0

*/

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

void main() {
  alias Tree = RedBlackTree!(int,"a<b",true);
  immutable n = readln.strip.to!int;
  auto a = readln.strip.splitter.map! (i => i.to!int).array[0..n];
  sort (a);
  auto t = make!Tree (a);
  debug stderr.writeln (t);
  int res;
  foreach (i; a) {
    auto r = t.upperBound (i);
    if (!r.empty) {
      int v = r.front;
      debug stderr.writefln ("%d %d", i, v);
      ++res;
      t.removeKey (v);
    }
  }
  writeln (res);
}


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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto W = s[1];
    auto H = s[2];

    auto P = new Tuple!(int, int)[](N);
    Tuple!(int, int, int)[][int] tate;
    Tuple!(int, int, int)[][int] yoko;

    foreach (i; 0..N) {
        s = readln.split.map!(to!int);
        if (s[0] == 1) {
            P[i] = tuple(s[1], H);
            tate[s[1] - s[2]] ~= tuple(i, s[1], -s[2]);
        } else {
            P[i] = tuple(W, s[1]);
            yoko[s[1] - s[2]] ~= tuple(i, -s[2], s[1]);
        }
    }

    foreach (k; tate.keys)
        tate[k].sort!"a[1] < b[1]";

    foreach (k; yoko.keys)
        yoko[k].sort!"a[2] < b[2]";

    auto ans = new int[](N);

    foreach (k; tate.keys) {
        if (!(k in yoko)) {
            foreach (v; tate[k])
                ans[v[0]] = v[0];
            continue;
        }

        DList!(int) queue;
        foreach (v; yoko[k])
            queue.insertFront(v[0]);

        foreach (v; tate[k]) {
            auto n = queue.front;
            queue.removeFront;
            ans[n] = v[0];
            queue.insertBack(v[0]);
        }

        for (int i = yoko[k].length.to!int - 1; !queue.empty; i--) {
            auto n = queue.front;
            queue.removeFront;
            ans[n] = yoko[k][i][0];
        }
    }

    foreach (k; yoko.keys) {
        if (!(k in tate)) {
            foreach (v; yoko[k]) {
                ans[v[0]] = v[0];
            }
        }
    }

    ans.each!(a => writeln(P[a][0], " ", P[a][1]));
}

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

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

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

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

struct Dancer {
  int g, p, t;
  int id;
  int diff;
}

struct Pt {
  int x, y;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const W = readInt();
      const H = readInt();
      auto D = new Dancer[N];
      foreach (i; 0 .. N) {
        D[i].g = readInt();
        D[i].p = readInt();
        D[i].t = readInt();
        D[i].id = i;
      }

      foreach (i; 0 .. N) {
        D[i].diff = D[i].t - D[i].p;
      }
      D.sort!((a, b) =>
          (a.diff != b.diff) ? (a.diff < b.diff)
                             : (a.g != b.g) ? (a.g > b.g)
                                            : (a.g == 2) ? (a.p > b.p) : (a.p < b.p));

      auto ans = new Pt[N];
      for (int i = 0, j; i < N; i = j) {
        for (j = i; j < N && D[i].diff == D[j].diff; ++j) {}
        debug {
          writeln("D[i .. j] = ", D[i .. j]);
        }
        Pt[] ps;
        foreach (k; i .. j) {
          ps ~= (D[k].g == 1) ? Pt(D[k].p, H) : Pt(W, D[k].p);
        }
        ps.sort!((p, q) => (p.x != q.x) ? (p.x < q.x) : (p.y > q.y));
        debug {
          writeln("ps = ", ps);
        }
        foreach (k; i .. j) {
          ans[D[k].id] = ps[k - i];
        }
      }

      foreach (i; 0 .. N) {
        writeln(ans[i].x, " ", ans[i].y);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);
  struct InputData
  {
    int g, p, t;
  }
  struct Point
  {
    long x, y;
  }
  auto n = next!int;
  auto w = next!int;
  auto h = next!int;
  auto idata = new InputData[](n);
  auto point = new Point[](n);
  foreach(p; 0 .. n)
    {
      idata[p].g = next!int;
      idata[p].p = next!int;
      idata[p].t = next!int;
      if (idata[p].g == 1)
	{
	  point[p].x = idata[p].p;
	  point[p].y = -idata[p].t;
	}
      else
	{
	  point[p].y = idata[p].p;
	  point[p].x = -idata[p].t;
	}
    }
  int[][long] lineids;
  foreach(i, p; point)
    {
      lineids.require(p.y + p.x, null) ~= cast(int) i;
    }
  foreach(ref line; lineids)
    {
      sort!((i, j) => point[i].x < point[j].x)(line);
      debug writeln(line);
    }
  auto endid = new int[](n);
  foreach(ref line; lineids)
    {
      int nH = 0;
      foreach(id; line)
	{
	  if (idata[id].g == 1) break;
	  nH++;
	}
      int nV = cast(int) line.length - nH;
      foreach(k; 0 .. nV)
	{
	  endid[line[k]] = line[$ - nV + k];
	}
      foreach(k; 0 .. nH)
	{
	  endid[line[nV + k]] = line[k];
	}
    }
  Point endPos(int id)
  {
    if (idata[id].g == 1)
      {
	return Point(idata[id].p, h);
      }
    return Point(w, idata[id].p);
  }

  foreach(i; 0 .. n)
    {
      auto p = endPos(endid[i]);
      writeln(p.x, " ", p.y);
    }
  writeln;
}

// INPUT

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

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

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


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

void main()
{
    auto T = readln.chomp.to!int;
    foreach (_; 0..T) {
        auto xnm = readln.split.to!(int[]);
        auto X = xnm[0];
        auto N = xnm[1];
        auto M = xnm[2];

        while (N > 0 && X/2 >= 10) {
            X = X/2 + 10;
            --N;
        }
        writeln(X <= M*10 ? "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 x = RD;
		auto n = RD!int;
		auto m = RD!int;

		foreach (i; 0..n)
		{
			if (x <= 19) break;
			x = x / 2 + 10;
		}
		ans[ti] = x <= m*10;
	}

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

//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) {
    int x = r.next!uint;
    const n = r.next!uint;
    const m = r.next!uint;
    foreach (i; 0 .. n) {
      const y = (x / 2) + 10;
      if (y >= x) break;
      x = y;
    }
    x -= m * 10;
    writeln (x > 0 ? "NO" : "YES");
  }
}


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

void main ()
{
	int nR, nG, nB;
	while (readf !(" %s %s %s") (nR, nG, nB) > 0)
	{
		readln;
		auto r = readln.splitter.map !(to !(int)).array;
		auto g = readln.splitter.map !(to !(int)).array;
		auto b = readln.splitter.map !(to !(int)).array;
		sort !(q{a > b}) (r);
		sort !(q{a > b}) (g);
		sort !(q{a > b}) (b);
		auto f = new int [] [] [] (nR + 1, nG + 1, nB + 1);
		int res = 0;
		foreach (i; 0..nR + 1)
		{
			foreach (j; 0..nG + 1)
			{
				foreach (k; 0..nB + 1)
				{
					if (i > 0 && j > 0)
					{
						f[i][j][k] = max (f[i][j][k],
						    f[i - 1][j - 1][k] +
						    r[i - 1] * g[j - 1]);
					}
					if (j > 0 && k > 0)
					{
						f[i][j][k] = max (f[i][j][k],
						    f[i][j - 1][k - 1] +
						    g[j - 1] * b[k - 1]);
					}
					if (i > 0 && k > 0)
					{
						f[i][j][k] = max (f[i][j][k],
						    f[i - 1][j][k - 1] +
						    r[i - 1] * b[k - 1]);
					}
					res = max (res, f[i][j][k]);
				}
			}
		}
		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) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto R = RD!int;
	auto G = RD!int;
	auto B = RD!int;
	auto r = RDA!int;
	auto g = RDA!int;
	auto b = RDA!int;
	r.sort!"a > b";
	g.sort!"a > b";
	b.sort!"a > b";

	long ans;
	auto dp = new long[][][](R+1, G+1, B+1);
	foreach (i; 0..(R+G+B)+2)
	{
		if (i % 2 == 1) continue;
		foreach (ri; 0..i)
		{
			if (ri > R) break;
			foreach (gi; 0..i-(ri+1))
			{
				if (gi > G) break;
				auto bi = i-(ri+1)-(gi+1);
				if (bi > B) continue;

				if (ri < R && gi < G)
				{
					dp[ri+1][gi+1][bi].chmax(dp[ri][gi][bi] + r[ri]*g[gi]);
					ans.chmax(dp[ri+1][gi+1][bi]);
				}
				if (ri < R && bi < B)
				{
					dp[ri+1][gi][bi+1].chmax(dp[ri][gi][bi] + r[ri]*b[bi]);
					ans.chmax(dp[ri+1][gi][bi+1]);
				}
				if (gi < G && bi < B)
				{
					dp[ri][gi+1][bi+1].chmax(dp[ri][gi][bi] + g[gi]*b[bi]);
					ans.chmax(dp[ri][gi+1][bi+1]);
				}
				debug writeln(ri, ",", gi, ",", bi, " ans:", ans);
			}
		}
	}

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

// Vicfred
// https://codeforces.com/contest/1398/problem/D
//
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

void main() {
    uint R, G, B;
    readf("%s %s %s", &R, &G, &B);
    readln;

    long[] r = readln.split.map!(to!long).array;
    long[] g = readln.split.map!(to!long).array;
    long[] b = readln.split.map!(to!long).array;

    r.sort!("a > b");
    g.sort!("a > b");
    b.sort!("a > b");

    long[][][] dp = new long[][][](R+1, G+1, B+1);

    long ans = long.min;
    for(int i = 0; i <= R; i++) {
        for(int j = 0; j <= G; j++) {
            for(int k = 0; k <= B; k++) {
                if(i < R && j < G)
                    dp[i + 1][j + 1][k] = max(dp[i + 1][j + 1][k], dp[i][j][k] + r[i] * g[j]);
                if(i < R && k < B)
                    dp[i + 1][j][k + 1] = max(dp[i + 1][j][k + 1], dp[i][j][k] + r[i] * b[k]);
                if(j < G && k < B)
                    dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1], dp[i][j][k] + g[j] * b[k]);
                ans = max(ans, dp[i][j][k]);
            }
        }
    }
    ans.writeln;
}

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

void main ()
{
	int nR, nG, nB;
	while (readf !(" %s %s %s") (nR, nG, nB) > 0)
	{
		readln;
		auto r = readln.splitter.map !(to !(int)).array ~ 0;
		auto g = readln.splitter.map !(to !(int)).array ~ 0;
		auto b = readln.splitter.map !(to !(int)).array ~ 0;
		sort !(q{a > b}) (r);
		sort !(q{a > b}) (g);
		sort !(q{a > b}) (b);
		int res = 0;
		foreach (rg; 0..min (nR, nG) + 1)
		{
			foreach (gb; 0..min (nG, nB) + 1)
			{
				foreach (br; 0..min (nB, nR) + 1)
				{
					if (rg + br > nR ||
					    rg + gb > nG ||
					    gb + br > nB)
					{
						break;
					}
					int temp = 0;
					int i = 0, j = 0, k = 0;
					int u = 0, v = 0, w = 0;
					while (true)
					{
						int cur = 0;
						int pos = -1;
						if (u < rg &&
						    cur < r[i] * g[j])
						{
							cur = r[i] * g[j];
							pos = 0;
						}
						if (v < gb &&
						    cur < g[j] * b[k])
						{
							cur = g[j] * b[k];
							pos = 1;
						}
						if (w < br &&
						    cur < b[k] * r[i])
						{
							cur = b[k] * r[i];
							pos = 2;
						}
						if (pos == -1)
						{
							break;
						}
						temp += cur;
						if (pos == 0)
						{
							u += 1;
							i += 1;
							j += 1;
						}
						if (pos == 1)
						{
							v += 1;
							j += 1;
							k += 1;
						}
						if (pos == 2)
						{
							w += 1;
							k += 1;
							i += 1;
						}
					}
					res = max (res, temp);
				}
			}
		}
		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) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto R = RD!int;
	auto G = RD!int;
	auto B = RD!int;
	auto r = RDA!int;
	auto g = RDA!int;
	auto b = RDA!int;
	r.sort!"a > b";
	g.sort!"a > b";
	b.sort!"a > b";

	long ans;
	auto dp = new long[][][](R+1, G+1, B+1);
	foreach (i; 0..(R+G+B)+1)
	{
		if (i % 2 == 1) continue;
		foreach (ri; 0..i)
		{
			if (ri > R) break;
			foreach (gi; 0..i-(ri+1))
			{
				if (gi > G) break;
				auto bi = i-(ri+1)-(gi+1);
				if (bi > B) break;

				if (ri < R && gi < G)
				{
					dp[ri+1][gi+1][bi].chmax(dp[ri][gi][bi] + r[ri]*g[gi]);
					ans.chmax(dp[ri+1][gi+1][bi]);
				}
				if (ri < R && bi < B)
				{
					dp[ri+1][gi][bi+1].chmax(dp[ri][gi][bi] + r[ri]*b[bi]);
					ans.chmax(dp[ri+1][gi][bi+1]);
				}
				if (gi < G && bi < B)
				{
					dp[ri][gi+1][bi+1].chmax(dp[ri][gi][bi] + g[gi]*b[bi]);
					ans.chmax(dp[ri][gi+1][bi+1]);
				}
				debug writeln(ri, ",", gi, ",", bi, " ans:", ans);
			}
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto R = RD!int;
	auto G = RD!int;
	auto B = RD!int;
	auto r = RDA!int;
	auto g = RDA!int;
	auto b = RDA!int;
	r.sort!"a > b";
	g.sort!"a > b";
	b.sort!"a > b";

	long ans;
	auto dp = new long[][][](R+1, G+1, B+1);
	foreach (i; 0..(R+G+B)+2)
	{
		if (i % 2 == 1) continue;
		foreach (ri; 0..i)
		{
			if (ri > R) break;
			foreach (gi; 0..i-(ri+1))
			{
				if (gi > G) break;
				auto bi = i-(ri+1)-(gi+1);
				if (bi > B) break;

				if (ri < R && gi < G)
				{
					dp[ri+1][gi+1][bi].chmax(dp[ri][gi][bi] + r[ri]*g[gi]);
					ans.chmax(dp[ri+1][gi+1][bi]);
				}
				if (ri < R && bi < B)
				{
					dp[ri+1][gi][bi+1].chmax(dp[ri][gi][bi] + r[ri]*b[bi]);
					ans.chmax(dp[ri+1][gi][bi+1]);
				}
				if (gi < G && bi < B)
				{
					dp[ri][gi+1][bi+1].chmax(dp[ri][gi][bi] + g[gi]*b[bi]);
					ans.chmax(dp[ri][gi+1][bi+1]);
				}
				debug writeln(ri, ",", gi, ",", bi, " ans:", ans);
			}
		}
	}

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


import std.stdio;
void main() {
  int n, l, r;
  scanf("%d", &n);
  foreach(_; 0..n) {
    scanf("%d %d", &l, &r);
    --l;
    writeln(r/2 - r%2*r - l/2 + l%2*l);
  }
}

import std.stdio;
void main() {
  int n, l, r;
  readf!"%d\n"(n);
  foreach(_; 0..n) {
    readf!"%d %d\n"(l, r);
    --l;
    writeln(r/2 - r%2*r - l/2 + l%2*l);
  }
}

import std.stdio;
void main() {
  int n, l, r;
  readf!"%d "(n);
  foreach(_; 0..n) {
    readf!" %d %d"(l, r);
    writeln(r/2 - r%2*r - (--l)/2 + l%2*l);
  }
}

import std.stdio;
void main() {
  int n, l, r;
  scanf("%d", &n);
  foreach(_; 0..n) {
    scanf("%d %d", &l, &r);
    writeln(r/2 - r%2*r - (--l)/2 + l%2*l);
  }
}

import std.stdio;
void main() {
  int n, l, r;
  readf!"%d\n"(n);
  foreach(_; 0..n) {
    readf!"%d %d\n"(l, r);
    writeln(r/2 - r%2*r - (--l)/2 + l%2*l);
  }
}


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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N; get(N);
        char[][] A, B;
        get_lines(N, A);
        readln;
        get_lines(N, B);

        auto M = new bool[][](N, N);
        foreach (i; 0..N) foreach (j; 0..N) if (A[i][j] != B[i][j]) M[i][j] = true;
        foreach (i; 1..N) if (M[i-1][0] != M[i][0]) foreach (j; 0..N) M[i][j] = !M[i][j];
        foreach (j; 0..N) foreach (i; 0..N) if (M[i][j] != M[0][j]) goto ng;
        writeln("YES");
        continue;
        ng:
        writeln("NO");
    }
}

// Cheese-Cracker: cheese-cracker.github.io

void solve(){
    int n = scan!int;
    dchar[][] mat;
    for(int i = 0; i < n; ++i){
        mat ~= scan!(dchar[]);
    }

    dchar[][] req;
    for(int i = 0; i < n; ++i){
        req ~= scan!(dchar[]);
    }
    /* show(req); */
    /* if(n == 1){ writeln("YES"); return;} */

    int[] s1, d1;
    for(int j = 0; j < n; ++j){
        if(mat[0][j] == req[0][j]){
            s1 ~= j;
        }else{
            d1 ~= j;
        }
    }

    for(int i = 1; i < n; ++i){
        int[] di;
        for(int j = 0; j < n; ++j){
            if(mat[i][j] != req[i][j]){
                di ~= j;
            }
        }
        if(di != s1 && di != d1){
            writeln("NO");
            return;
        }
    }
    writeln("YES");
}

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

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


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

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }
void chmin(T)(ref T a, in T b) { a = min(a, b); }
void chmax(T)(ref T a, in T b) { a = max(a, b); }

struct SegmentTree(T, T unit, alias binop) {
    int n;
    T[] dat;
    this(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat = new T[2*n+2];
        dat[] = unit;
    }
    void update(int k, in T a) {
        k += n - 1;
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = binop(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    T query(int a, int b) const {
        return query(a, b, 0, 0, n);
    }
    T query(int a, int b, int k, int l, int r) const {
        if (r <= a || b <= l) return unit;
        if (a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2),
          vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return binop(vl, vr);
    }
}

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

void solve() {
    int N, M; readf("%d %d\n", &N, &M);
    auto S = read!string;
    auto L = new int[M];
    auto R = new int[M];
    foreach (q; 0 .. M) {
        readf("%d %d\n", &L[q], &R[q]);
        L[q]--; R[q]--;
    }

    auto T = new int[N];
    for (int i = 0; i < N; i++) {
        T[i] = (S[i] == '+' ? +1 : -1);
    }
    auto PS = new int[N+1]; {
        for (int i = 0; i < N; i++) {
            PS[i+1] = PS[i] + T[i];
        }
    }
    const int INF = 1<<30;
    auto minT = SegmentTree!(int,  INF, (a, b) => min(a, b))(N+1);
    auto maxT = SegmentTree!(int, -INF, (a, b) => max(a, b))(N+1);
    for (int i = 0; i <= N; i++) {
        minT.update(i, PS[i]);
        maxT.update(i, PS[i]);
    }
    foreach (q; 0 .. M) {
        int l = L[q], r = R[q];
        auto xm = minT.query(0, l+1);
        auto xM = maxT.query(0, l+1);
        auto ym = minT.query(r+1, N+1);
        auto yM = maxT.query(r+1, N+1);
        auto p = PS[r+1] - PS[l];
        /*
        writeln(S);
        writeln([l, r]);
        writeln([xm, xM, ym, yM, p]);
        */
        auto ans = max(xM, yM - p) - min(xm, ym - p) + 1;
        writeln(ans);
    }
}

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()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        auto v = readln.splitter.map!(to!int).array;
        auto n = v[0], m = v[1];
        auto arr = readln.chomp.array;

        int[] top, bot, pos;
        top ~= 0, bot ~= 0, pos ~= 0;
        int cur = 0;
        foreach (e; arr) {
            if (e == '+') { cur += 1; }
            else { cur -= 1; }

            pos ~= cur;
            top ~= max(top.back, cur);
            bot ~= min(bot.back, cur);
        }

        int[] topdst, botdst;
        topdst ~= 0;
        botdst ~= 0;
        cur = 0;
        foreach_reverse (i, e; arr) {
            int fromtop = topdst.back;
            int frombot = botdst.back;
            if (e == '+') {
                frombot -= 1;
                fromtop += 1;
            } else {
                fromtop -= 1;
                frombot += 1;
            }

            fromtop = max(fromtop, 0);
            frombot = max(frombot, 0);

            topdst ~= fromtop;
            botdst ~= frombot;
        }

        topdst.reverse();
        botdst.reverse();

        debug { writeln(pos, top, bot); }
        debug { writeln(topdst, botdst); }

        while (m--) {
            v = readln.splitter.map!(to!int).array;
            auto x = v[0], y = v[1];

            int topmax = max(top[x-1], pos[x-1] + topdst[y]);
            int botmin = min(bot[x-1], pos[x-1] - botdst[y]);
            int dist = topmax - botmin + 1;

            dist.writeln;
        }
    }
}

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

void get(Args...)(ref Args args)
{
    import std.traits, std.meta, std.typecons;

    static if (Args.length == 1) {
        alias Arg = Args[0];

        static if (isArray!Arg) {
          static if (isSomeChar!(ElementType!Arg)) {
            args[0] = readln.chomp.to!Arg;
          } else {
            args[0] = readln.split.to!Arg;
          }
        } else static if (isTuple!Arg) {
            auto input = readln.split;
            static foreach (i; 0..Fields!Arg.length) {
                args[0][i] = input[i].to!(Fields!Arg[i]);
            }
        } else {
            args[0] = readln.chomp.to!Arg;
        }
    } else {
        auto input = readln.split;
        assert(input.length == Args.length);

        static foreach (i; 0..Args.length) {
            args[i] = input[i].to!(Args[i]);
        }
    }
}

void get_lines(Args...)(size_t N, ref Args args)
{
    import std.traits, std.range;

    static foreach (i; 0..Args.length) {
        static assert(isArray!(Args[i]));
        args[i].length = N;
    }

    foreach (i; 0..N) {
        static if (Args.length == 1) {
            get(args[0][i]);
        } else {
            auto input = readln.split;
            static foreach (j; 0..Args.length) {
                args[j][i] = input[j].to!(ElementType!(Args[j]));
            }
        }
    }
}

void main()
{
    int T; get(T);
    while (T--) {
        int N, M; get(N, M);
        char[] ii; get(ii);
        auto ps = new int[](N + 1);
        auto max_s = new int[](N + 1);
        auto min_s = new int[](N + 1);
        int max_p, min_p, p;
        foreach (i; 0..N) {
            p += (ii[i] == '+' ? 1 : -1);
            ps[i+1] = p;
            max_p = max(max_p, p);
            min_p = min(min_p, p);
            max_s[i+1] = max_p;
            min_s[i+1] = min_p;
        }
        auto max_r = new int[](N + 1);
        auto min_r = new int[](N + 1);
        p = max_p = min_p = 0;
        foreach_reverse (i; 0..N) {
            p += (ii[i] == '+' ? -1 : 1);
            max_p = max(max_p, p);
            min_p = min(min_p, p);
            max_r[i+1] = max_p - p;
            min_r[i+1] = min_p - p;
        }

        while (M--) {
            int l, r; get(l, r);
            p = ps[l-1];
            max_p = max_s[l-1];
            min_p = min_s[l-1];
            if (r != N) {
                max_p = max(max_p, max_r[r+1] + p);
                min_p = min(min_p, min_r[r+1] + p);
            }
            writeln(max_p - min_p + 1);
        }
    }
}

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

alias State = Tuple !(int, q{lo}, int, q{hi}, int, q{cur});

auto fun (S) (S s)
{
	State [] res;
	auto v = State (0, 0, 0);
	res ~= v;
	foreach (ref c; s)
	{
		v.cur += (c == '+') ? +1 : -1;
		v.lo = min (v.lo, v.cur);
		v.hi = max (v.hi, v.cur);
		res ~= v;
	}
	return res;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto c = readln.strip;
		auto pref = fun (c);
		auto suff = fun (c.retro);
		reverse (suff);
		foreach (q; 0..m)
		{
			int l, r;
			readf !(" %s %s") (l, r);
			l -= 1;
			auto v = pref[l];
			auto w = suff[r];
			v.lo = min (v.lo, v.cur - (w.hi - w.cur));
			v.hi = max (v.hi, v.cur - (w.lo - w.cur));
			writeln (v.hi - v.lo + 1);
		}
	}
}


void main() {
	auto t = ri;
	foreach(T; 0..t) {
		auto n = ri;
		auto a = readAs!(int[]);
		int last;
		bool[] visited = new bool[](n);
		long alice, bob;
		long lalice, lbob;
		uint pa, pb = n - 1;
		ulong move;
		ulong k;
		loop: foreach(i; 0..10000) {
			bool flag = false;
			debug visited.map!(v => v+0).writeln;
			debug writefln("%d %d", pa, pb);
			if(i % 2 == 0) {
				lalice = 0;
				while(lalice <= lbob) {
					if(pa < n && !visited[pa]) {
						if(!flag) { flag = true; k++; }
						visited[pa] = true;
						alice += a[pa];
						lalice += a[pa];
						pa++;
					} else break loop;
				}
			} else {
				lbob = 0;
				while(lbob <= lalice) {
					if(pb > 0 && !visited[pb]) {
						if(!flag) { flag = true; k++; }
						visited[pb] = true;
						bob += a[pb];
						lbob += a[pb];
						pb--;
					} else break loop;
				}
			}
		}
		writefln("%d %d %d", k, alice, bob);
	}
}

// ===================================

import std.stdio;
import std.string;
import std.functional;
import std.algorithm;
import std.range;
import std.traits;
import std.math;
import std.container;
import std.bigint;
import std.numeric;
import std.conv;
import std.typecons;
import std.uni;
import std.ascii;
import std.bitmanip;
import core.bitop;

T readAs(T)() if (isBasicType!T) {
	return readln.chomp.to!T;
}
T readAs(T)() if (isArray!T) {
	return readln.split.to!T;
}

T[][] readMatrix(T)(uint height, uint width) if (!isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		res[i] = readAs!(T[]);
	}
	return res;
}

T[][] readMatrix(T)(uint height, uint width) if (isSomeChar!T) {
	auto res = new T[][](height, width);
	foreach(i; 0..height) {
		auto s = rs;
		foreach(j; 0..width) res[i][j] = s[j].to!T;
	}
	return res;
}

int ri() {
	return readAs!int;
}

long rl() {
	return readAs!long;
}

double rd() {
	return readAs!double;
}

string rs() {
	return readln.chomp;
}

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;
    foreach (_t; 0..T) {
        auto N = readln.chomp.to!int;
        auto as = readln.split.to!(long[]);
        long a, sa, b, sb;
        int c;
        while (!as.empty) {
            ++c;
            long d;
            if (c%2 == 1) {
                do {
                    d += as[0];
                    as = as[1..$];
                } while (!as.empty && d <= b);
                a = d;
                sa += d;
            } else {
                do {
                    d += as[$-1];
                    as = as[0..$-1];
                } while (!as.empty && d <= a);
                b = d;
                sb += d;
            }
        }
        writeln(c, " ", sa, " ", sb);
    }
}

// Author: Ivan Kazmenko (gassa@mail.ru)
// excessive array copying on each step, should exceed ML or TL
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int moves = 0;
		int prev = 0;
		int totalA = 0;
		int totalB = 0;
		while (!a.empty)
		{
			moves += 1;
			int curA = 0;
			while (!a.empty && curA <= prev)
			{
				curA += a[0];
				a = a[1..$].dup;
			}
			totalA += curA;
			prev = curA;

			if (a.empty)
			{
				break;
			}

			moves += 1;
			int curB = 0;
			while (!a.empty && curB <= prev)
			{
				curB += a[$ - 1];
				a = a[0..$ - 1].dup;
			}
			totalB += curB;
			prev = curB;
		}
		writeln (moves, " ", totalA, " ", totalB);
	}
}

// Author: Ivan Kazmenko (gassa@mail.ru)
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int moves = 0;
		int prev = 0;
		int totalA = 0;
		int totalB = 0;
		while (!a.empty)
		{
			moves += 1;
			int curA = 0;
			while (!a.empty && curA <= prev)
			{
				curA += a.front;
				a.popFront ();
			}
			totalA += curA;
			prev = curA;

			if (a.empty)
			{
				break;
			}

			moves += 1;
			int curB = 0;
			while (!a.empty && curB <= prev)
			{
				curB += a.back;
				a.popBack ();
			}
			totalB += curB;
			prev = curB;
		}
		writeln (moves, " ", totalA, " ", totalB);
	}
}

// Author: Ivan Kazmenko (gassa@mail.ru)
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		int [2] total;
		int prev = 0;
		int moves = 0;
		while (!a.empty)
		{
			moves += 1;
			int cur = 0;
			while (!a.empty && cur <= prev)
			{
				cur += a[0];
				a = a[1..$];
			}
			total[moves % 2] += cur;
			prev = cur;
			reverse (a);
		}
		writeln (moves, " ", total[1], " ", total[0]);
	}
}

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

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto t = readNum!int;

  foreach(i; 0 .. t){
    auto n = readNum!int;
    auto a = readNums!int;

    int apos = 0, bpos = n-1;
    int asum, bsum;
    int move, eaten, requirement = a[0];

  mainloop: while(true){
      move++;

      while(eaten < requirement){
        eaten += a[apos];
        asum += a[apos];
        apos++;

        if(bpos < apos) break mainloop;
      }

      requirement = eaten+1;
      eaten = 0;

      move++;

      while(eaten < requirement){
        eaten += a[bpos];
        bsum += a[bpos];
        bpos--;

        if(bpos < apos) break mainloop;
      }

      requirement = eaten+1;
      eaten = 0;
    }

    writeln(move, " ", asum, " ", bsum);
  }
}

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

		long last;
		while (true)
		{
			if (a.empty) break;
			long x;
			while (!a.empty)
			{
				x += a.front; a.popFront;
				if (x > last) break;
			}
			++ans[ti][0];
			ans[ti][1] += x;
			last = x;

			if (a.empty) break;
			long y;
			while (!a.empty)
			{
				y += a.back; a.popBack;
				if (y > last) break;
			}
			++ans[ti][0];
			ans[ti][2] += y;
			last = y;
		}
	}

	foreach (e; ans)
	{
		e.map!(to!string).join(" ").writeln;
	}
	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 = 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 s = RD!string;
	auto cnt = new int[][](s.length+1);
	cnt[0] = new int[](26);
	foreach (i; 0..s.length)
	{
		cnt[i+1] = cnt[i].dup;
		auto num = s[i]-'a';
		cnt[i+1][num] += 1;
	}
	auto q = RD!int;
	auto ans = new bool[](q);
	foreach (qi; 0..q)
	{
		auto l = RD!int-1;
		auto r = RD!int-1;
		if (l == r || s[l] != s[r])
		{
			ans[qi] = true;
			continue;
		}
		long c;
		auto tmp = cnt[r+1].dup;
		tmp[] -= cnt[l][];
		foreach (i; 0..26)
		{
			if (i == s[l]-'a') continue;
			if (tmp[i] > 0)
				++c;
		}
		ans[qi] = c >= 2;
	}

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

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

immutable int letters = 26;

void main ()
{
	string s;
	while ((s = readln.strip) != "")
	{
		auto n = s.length.to !(int);
		auto f = new int [letters] [n + 1];
		foreach (i; 0..n)
		{
			f[i + 1][] = f[i][];
			f[i + 1][s[i] - 'a'] += 1;
		}
		int q;
		readf (" %s", &q);
		foreach (j; 0..q)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u -= 1;
			int [letters] cur;
			cur[] = f[v][] - f[u][];
			auto diff = cur[].count !(x => x != 0);
			bool ok = (v - u == 1) || (diff > 2) ||
			    ((diff == 2) && (s[u] != s[v - 1]));
			writeln (ok ? "Yes" : "No");
		}
		readln;
	}
}

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() {
  debug {
    bool check(int[] as) {
      if (as.length == 1) {
        return true;
      }
      if (as[0] != as[$ - 1]) {
        return true;
      }
      if (as.dup.sort.group.array.length >= 3) {
        return true;
      }
      return false;
    }

    enum lim = 5;
    foreach (n; 1 .. lim + 1) {
      foreach (p; 0 .. lim^^n) {
        auto as = new int[n];
        foreach (i; 0 .. n) {
          as[i] = p / lim^^i % lim;
        }
        auto bs = as.dup;
        bs.sort;
        bool hasIrred;
        do {
          bool irred = true;
          foreach (i; 1 .. n) {
            irred = irred && (as[0 .. i].dup.sort != bs[0 .. i].dup.sort);
          }
          hasIrred = hasIrred || irred;
        } while (bs.nextPermutation);
        if (!hasIrred) {
          writeln(as);
        }
        assert(hasIrred == check(as));
      }
    }
  }

  try {
    for (; ; ) {
      const S = readToken();
      const Q = readInt();
      auto L = new int[Q];
      auto R = new int[Q];
      foreach (q; 0 .. Q) {
        L[q] = readInt() - 1;
        R[q] = readInt();
      }

      const N = cast(int)(S.length);
      auto cnt = new int[][](26, N + 1);
      foreach (a; 0 .. 26) {
        foreach (i; 0 .. N) {
          cnt[a][i + 1] = cnt[a][i] + ((S[i] - 'a' == a) ? 1 : 0);
        }
      }

      foreach (q; 0 .. Q) {
        bool ans;
        if (R[q] - L[q] == 1) {
          ans = true;
        }
        if (S[L[q]] != S[R[q] - 1]) {
          ans = true;
        }
        int num;
        foreach (a; 0 .. 26) {
          if (cnt[a][R[q]] - cnt[a][L[q]] > 0) {
            ++num;
          }
        }
        if (num >= 3) {
          ans = true;
        }
        writeln(ans ? "Yes" : "No");
      }
    }
  } 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.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 s = RD!string;
	auto cnt = new int[][](s.length+1);
	cnt[0] = new int[](26);
	foreach (i; 0..s.length)
	{
		cnt[i+1] = cnt[i].dup;
		auto num = s[i]-'a';
		cnt[i+1][num] += 1;
	}
	auto q = RD!int;
	auto ans = new bool[](q);
	foreach (qi; 0..q)
	{
		auto l = RD!int;
		auto r = RD!int;
		if (l == r)
		{
			ans[qi] = true;
			continue;
		}
		long c;
		auto tmp = cnt[r].dup;
		tmp[] -= cnt[l-1][];
		foreach (i; 0..26)
		{
			if (tmp[i] > 0)
				++c;
		}
		ans[qi] = c >= 2;
	}

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


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

void main ()
{
	int k, n;
	while (readf (" %s %s", &n, &k) > 0)
	{
		int [] a;
		foreach (i; 0..n)
		{
			ubyte [4] v;
			readf (" %d.%d.%d.%d", &v[0], &v[1], &v[2], &v[3]);
			uint s = 0;
			foreach (j; 0..4)
			{
				s = (s << 8) | v[j];
			}
			debug {writefln ("%08X", s);}
			a ~= s;
		}

		uint m = 0;
		long res = -1;
		foreach (i; 1..32)
		{
			m |= 1u << (32 - i);
			debug {writefln ("%08X", m);}
			auto b = redBlackTree !(int);
			foreach (c; a)
			{
				b.insert (c & m);
			}
			if (b.length == k)
			{
				res = m;
				break;
			}
		}
		if (res == -1)
		{
			writeln (res);
		}
		else
		{
			ubyte [4] v;
			foreach (j; 0..4)
			{
				v[j] = res & 0xFF;
				res >>= 8;
			}
			reverse (v[]);
			writefln ("%(%s.%)", v);
		}
	}
}

import std.stdio;
import std.algorithm;

int rev(int x) {
    int res = 0;
    for (int i = 0; i < 8; i++) {
        if (x & (1 << (7 - i)))
            res |= (1 << i);
    }
    return res;
}

void main() {
    int n, K;
    readf("%d %d\n", &n, &K);

    int[][] masks;
    masks.length = n;

    for (int i = 0; i < n; i++) {
        masks[i].length = 4;
        readf("%d.%d.%d.%d\n", &masks[i][0], &masks[i][1], &masks[i][2], &masks[i][3]);
    }

    for (int i = 0; i < 4; i++) {
        for (int jj = 1; jj < 256; jj = (jj << 1) | 1) {
            int j = rev(jj);
            int[] nets;
            nets.length = n;

            for (int k = 0; k < n; k++) {
                for (int l = 0; l < i; l++) {
                    nets[k] <<= 8;
                    nets[k] |= masks[k][l];
                }
                nets[k] <<= 8;
                nets[k] |= (j & masks[k][i]);
            }

            sort(nets);

            int cnt = 1;
            for (int k = 1; k < n; k++) {
                if (nets[k] != nets[k - 1])
                    ++cnt;
            }

            if (cnt == K) {
                for (int k = 0; k < i; k++) {
                    write("255.");
                }
                write(j);
                for (int k = 0; k < (4 - i - 1); k++) {
                    write(".0");
                }
                writeln();
                return;
            }
        }
    }

    writeln(-1);
}

module sigod.codeforces.p291C;

import std.stdio;

private
enum MASK_INCREMENT = 0x80000000;

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

	uint[] ips = new uint[n];

	foreach (ref ip; ips) {
		ubyte b1, b2, b3, b4;
		stdin.readf(" %s.%s.%s.%s", &b1, &b2, &b3, &b4);

		ip = (((((b1 << 8) | b2) << 8) | b3) << 8) | b4;
	}

	uint mask = 0;

	foreach (x; 0 .. 32) {
		mask = (mask >> 1) | MASK_INCREMENT;

		bool[uint] webs;

		foreach (ip; ips) {
			webs[ip & mask] = true;
		}

		if (webs.length == k) {
			stdout.writeln(toIP(mask));
			return;
		}
	}

	stdout.writeln("-1");
}

private
string toIP(uint mask)
{
	ubyte b1, b2, b3, b4;

	b4 = mask & 0xFF;
	mask >>= 8;

	b3 = mask & 0xFF;
	mask >>= 8;

	b2 = mask & 0xFF;
	mask >>= 8;

	b1 = mask & 0xFF;

	return std.string.format("%s.%s.%s.%s", b1, b2, b3, b4);
}


import std.stdio;
import std.algorithm;

void main() { while (solve()) {} }

auto getZ(int[] s) {
	int n = cast(int)s.length;
	int lf = -1, rg = -1;
	auto z = new int[n];
	z[0] = 0;
	foreach (i; 1..n) {
		z[i] = 0;
		if (i < rg) z[i] = min(z[i - lf], rg - i);
		while (i + z[i] < n && s[i + z[i]] == s[z[i]])
			z[i]++;
		if (i + z[i] > rg) {
			lf = i;
			rg = i + z[i];
		}
	}
	return z;
}

bool solve() {
	int n, w;
	if (!readf(" %s %s", &n, &w)) return false;
	auto a = new int[n], b = new int[w];
	foreach (ref x; a) readf(" %s", &x);
	foreach (ref x; b) readf(" %s", &x);
	if (w == 1) {
		writeln(n);
		return true;
	}
	auto da = new int[n - 1];
	foreach (i, ref dx; da)
		dx = a[i + 1] - a[i];
	auto db = new int[w - 1];
	foreach (i, ref dx; db)
		dx = b[i + 1] - b[i];
	auto s = db ~ da;
	auto z = getZ(s);
	int ans = 0;
	foreach (val; z[db.length..$])
		if (val >= db.length) ans++;
	writeln(ans);
	return true;
}

import std.stdio;

immutable int p = 13957;

immutable int MOD = 1000000007;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p % MOD;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p % MOD + b[i];
    h %= MOD;
    if (h < 0) {
      h += MOD;
    }
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p % MOD + a[i];
    cur %= MOD;
    if (cur < 0) {
      cur += MOD;
    }
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1] % MOD;
      if (cur < 0) {
        cur += MOD;
      }
    }
    if (i >= w - 1) {
      cur %= MOD;
      if (cur < 0) {
        cur += MOD;
      }
      ans += cur == h;
    }
  }
  writeln(ans);
}

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

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

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

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

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

int M, N;
int[] A, B;
int[] P, Q;

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

		if (N == 1) {
			writeln(M);
		} else {
			P = new int[M - 1];
			foreach (i; 0 .. M - 1) {
				P[i] = A[i + 1] - A[i];
			}
			Q = new int[N - 1];
			foreach (i; 0 .. N - 1) {
				Q[i] = B[i + 1] - B[i];
			}
debug{
writeln("P = ",P);
writeln("Q = ",Q);
}
			int[] fail = new int[Q.length + 1];
			int j = fail[0] = -1;
			foreach (i; 0 .. Q.length) {
				for (; j >= 0 && Q[j] != Q[i]; j = fail[j]) {}
				fail[i + 1] = ++j;
			}
debug{
writeln("fail = ",fail);
}
			int ans;
			j = 0;
			foreach (i; 0 .. P.length) {
				for (; j >= 0 && Q[j] != P[i]; j = fail[j]) {}
				if (++j == Q.length) {
					++ans;
					j = fail[j];
				}
			}
			writeln(ans);
		}
	}
	} catch (EOFException) {}
}

import std.stdio;

immutable int p = 37;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  for (int i = 1; i < w; i++) {
    h = h * p + b[i];
  }
  long cur = 0;
  int ans = 0;
  a[0] = 0;
  for (int i = 1; i < n; i++) {
    cur = cur * p + a[i];
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1];
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 13957;

immutable int MOD = 1000000009;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p % MOD;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p % MOD + b[i];
    h %= MOD;
    if (h < 0) {
      h += MOD;
    }
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p % MOD + a[i];
    cur %= MOD;
    if (cur < 0) {
      cur += MOD;
    }
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1] % MOD;
      if (cur < 0) {
        cur += MOD;
      }
    }
    if (i >= w - 1) {
      cur %= MOD;
      if (cur < 0) {
        cur += MOD;
      }
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 37;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p + b[i];
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p + a[i];
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1];
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 37;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  for (int i = 1; i < w; i++) {
    h = h * p + b[i];
  }
  long cur = 0;
  int ans = 0;
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (i < n) {
      cur = cur * p + a[i];
    }
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1];
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 11;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p + b[i];
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p + a[i];
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1];
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 1000000007;

immutable int MOD = 1000000009;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p % MOD;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p % MOD + b[i];
    h %= MOD;
    if (h < 0) {
      h += MOD;
    }
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p % MOD + a[i];
    if (cur < 0) {
      cur += MOD;
    }
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1] % MOD;
      if (cur < 0) {
        cur += MOD;
      }
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 13579;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p + b[i];
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p + a[i];
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1];
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 1000000007;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p + b[i];
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p + a[i];
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1];
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 23719;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p + b[i];
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p + a[i];
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1];
    }
    if (i >= w - 1) {
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 1000000007;

immutable int MOD = 1000000009;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p % MOD;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p % MOD + b[i];
    h %= MOD;
    if (h < 0) {
      h += MOD;
    }
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p % MOD + a[i];
    cur %= MOD;
    if (cur < 0) {
      cur += MOD;
    }
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1] % MOD;
      if (cur < 0) {
        cur += MOD;
      }
    }
    if (i >= w - 1) {
      cur %= MOD;
      if (cur < 0) {
        cur += MOD;
      }
      ans += cur == h;
    }
  }
  writeln(ans);
}

import std.stdio;

immutable int p = 13957;

immutable int MOD = 1000000007;

void main() {
  int n, w; readf(" %s %s", &n, &w);
  auto pw = new long[n + 10];
  pw[0] = 1;
  for (int i = 1; i < pw.length; i++) {
    pw[i] = pw[i - 1] * p % MOD;
  }
  auto a = new int[n];
  auto b = new int[w];
  for (int i = 0; i < n; i++) {
    readf(" %s", &a[i]);
  }
  for (int i = n - 1; i > 0; i--) {
    a[i] -= a[i - 1];
  }
  a[0] = 0;
  long h = 0;
  long mult = 1;
  for (int i = 0; i < w; i++) {
    readf(" %s", &b[i]);
  }
  for (int i = w - 1; i > 0; i--) {
    b[i] -= b[i - 1];
  }
  b[0] = 0;
  for (int i = 1; i < w; i++) {
    h = h * p % MOD + b[i];
    h %= MOD;
    if (h < 0) {
      h += MOD;
    }
  }
  long cur = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cur = cur * p % MOD + a[i];
    cur %= MOD;
    if (cur < 0) {
      cur += MOD;
    }
    writeln(i, ' ', cur, ' ', h);
    if (i >= w) {
      cur -= pw[w - 1] * a[i - w + 1] % MOD;
      if (cur < 0) {
        cur += MOD;
      }
    }
    if (i >= w - 1) {
      cur %= MOD;
      if (cur < 0) {
        cur += MOD;
      }
      ans += cur == h;
    }
    writeln(i, ' ', cur, ' ', h);
  }
  writeln(ans);
}

import std.stdio;
import std.algorithm;

void main() { while (solve()) {} }

auto getZ(int[] s) {
	int n = cast(int)s.length;
	int lf = -1, rg = -1;
	auto z = new int[n];
	z[0] = 0;
	foreach (i; 1..n) {
		z[i] = 0;
		if (i < rg) z[i] = min(z[i - lf], rg - i);
		while (i + z[i] < n && s[i + z[i]] == s[z[i]])
			z[i]++;
		if (i + z[i] > rg) {
			lf = i;
			rg = i + z[i];
		}
	}
	return z;
}

bool solve() {
	int n, w;
	if (!readf(" %s %s", &n, &w)) return false;
	auto a = new int[n], b = new int[w];
	foreach (ref x; a) readf(" %s", &x);
	foreach (ref x; b) readf(" %s", &x);
	if (w == 1) {
		writeln(n);
		return true;
	}
	auto da = new int[n - 1];
	foreach (i, ref dx; da)
		dx = a[i + 1] - a[i];
	auto db = new int[w - 1];
	foreach (i, ref dx; db)
		dx = b[i + 1] - b[i];
	auto s = db ~ da;
	auto z = getZ(s);
	int ans = 0;
	foreach (val; z[db.length..$])
		if (val == db.length) ans++;
	writeln(ans);
	return true;
}