module main;

import std.stdio : writeln;

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

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

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

        return readToken.to!int;
    }

    string[] tokens;
}

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

    IO io;
    int T = io.readInt;
    while (T--) {
        int n = io.readInt;
        int x = io.readInt;
        int y = io.readInt;
        writeln(min(max(x + y - n + 1, 1), n), " ", min(x + y - 1, n));
    }
}

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

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

		if (x + y < n + 1)
		{
			ans[ti][0] = 1;
		}
		else
		{
			ans[ti][0] = min((x+y) - (n+1) + 2, n);
		}
		ans[ti][1] = min(x+y-1, n);
	}

	foreach (e; ans)
		writeln(e[0], " ", e[1]);
	stdout.flush;
	debug readln;
}

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.chomp.split.map!(to!int);
        auto n = v[0], x = v[1], y = v[2];

        int mn = n - min(n-1, max(0,
            max(0, n-x-1) + max(0, n-y-1) + (max(x, y) < n ? 1 : 0)));
        int mx = min(n, x-1 + y-1 + 1);

        writeln(mn, ' ', mx);
    }
}

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.chomp.split.map!(to!int);
        auto n = v[0], x = v[1], y = v[2];

        int mn = n - min(n-1, n-x-1 + n-y-1 + (max(x, y) < n ? 1 : 0));
        int mx = min(n, x-1 + y-1 + 1);

        writeln(mn, ' ', mx);
    }
}

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.chomp.split.map!(to!int);
        auto n = v[0], x = v[1], y = v[2];

        int mn = n - min(n-1, max(0, n-x-1 + n-y-1 + (max(x, y) < n ? 1 : 0)));
        int mx = min(n, x-1 + y-1 + 1);

        writeln(mn, ' ', mx);
    }
}

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

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

		if (x + y < n + 1)
		{
			ans[ti][0] = 1;
		}
		else
		{
			ans[ti][0] = min((x+y) - (n+1) + 2, n);
		}
		ans[ti][1] = x+y-1;
	}

	foreach (e; ans)
		writeln(e[0], " ", e[1]);
	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 modd(T)(ref T x, T y) { x.modm(y.modpow(mod - 2)); }

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

		if (x + y < n + 1)
		{
			ans[ti][0] = 1;
		}
		else
		{
			ans[ti][0] = (x+y) - (n+1) + 2;
		}
		ans[ti][1] = x+y-1;
	}

	foreach (e; ans)
		writeln(e[0], " ", e[1]);
	stdout.flush;
	debug readln;
}


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

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

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

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

int N;
long H;
long[] A;

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

      int lo = 0, hi = N + 1;
      for (; lo + 1 < hi; ) {
        const mid = (lo + hi) / 2;
        auto a = A[0 .. mid].dup;
        a.sort;
        long sum;
        for (int i = mid - 1; i >= 0; i -= 2) {
          sum += a[i];
        }
        (sum <= H) ? (lo = mid) : (hi = mid);
      }
      writeln(lo);
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	int n, h;
	while (readf (" %s %s", &n, &h) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		int res = 0;
		while (res <= n)
		{
			auto b = a[0..res];
			sort (b);
			long need = 0;
			for (int i = res - 1; i >= 0; i -= 2)
			{
				need += b[i];
			}
			debug {writeln (res, ": ", b, ", ", need);}
			if (need > h)
			{
				break;
			}
			res += 1;
		}
		writeln (res - 1);
	}
}

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

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

  a ~= (h + 1);
  int[] b;
  foreach (k, el; a) {
    b ~= el;
    b.sort;
    b.reverse;
    int cur_h = 0;
    bool ok = true;
    foreach (i; 0 .. b.length) {
      if (cur_h + b[i] > h) {
        ok = false;
        break;
      }
      if (i & 1) {
        cur_h += max(b[i - 1], b[i]);
      }
    }
    if (!ok) {
      writeln(k);
      break;
    }
  }

}

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

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


import std.stdio;
int main()
{
	int a=0;
	readf(" %d", &a);
	int count=0;
	int[] m = new int[a];
	for (int i=0; i<a; i++)
	{
		readf(" %d", &m[i]);
	}
	for (int i=0; i<a-1; i++)
	{
		if ((m[i]==1 && m[i+1]==10) || (m[i]==10 && m[i+1]==1))
		{
			count=count+1;
		}
	}
	writeln(count+1);
	return 0;
}

import std.stdio;

void main() {
  int n; readf("%d\n", &n);
  string prev = "";
  int c = 0;
  for (int i = 0; i < n; i++) {
    string line; readf("%s\n", &line);
    if (line != prev) {
      //writef(line ~ " " ~ prev ~ "\n");
      c++;
      prev = line;
    }
  }
  writef("%d\n", c);
}

import std.stdio;
import core.stdc.stdio;

void main() {
    uint n, r = 1;
    scanf(`%u`, &n);

    uint l = 3;

	foreach(i; 0..n) {
		uint k;
		scanf(`%u`, &k);

		uint a = k / 10;
		uint b = k % 10;

		if(l == a) r++;
		l = b;
	}

	write(r);
}

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

void main(string[] args)
{
	int total = to!int(readln.strip);
	int countIclends, prevCode;
	for(; total > 0; --total){
		int code;
		readf(" %s", &code);
		if(code != prevCode){
			++countIclends;
			prevCode = code;
		}
	}
	writeln(countIclends);
}

import std.stdio;
import std.uni;
import std.string;
import core.stdc.stdio;
import std.array;
import std.algorithm;
import std.string;
import std.math;

int main(string[] argv)
{
	int n;
	int pm;
	scanf("%d %d", &n, &pm);
	int ng = 1;
	foreach (int i; 1..n)
	{
		int cm;
		scanf("%d", &cm);
		if (pm != cm) ng++;
		pm = cm;
	}
	printf("%d", ng);
	return 0;
}

module main;

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

void main(string[] args)
{
	readln;
	string former = "";
	int answer = 0;
	foreach(string line; stdin.lines) {
		if (line != former)
			answer++;
		former = line;
	}

	answer.writeln;
}

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

immutable int mod = 10^^9 + 7;

void main(){
    int n = readln.chomp.to!int;

    auto p = readln.chomp;

    int ans = 1;

    foreach(i ; 0 .. n - 1){
        auto magnet = readln.chomp;

        if (magnet != p) {
            ans++;
        }

        p = magnet;
    }

    writeln(ans);
}

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

import std.stdio;

void main() {
  int n; readf("%d", &n);
  string prev = "";
  int c = 0;
  for (int i = 0; i < n; i++) {
    string line; readf("%s\n", &line);
    if (line != prev) {
      //writef(line ~ " " ~ prev ~ "\n");
      c++;
      prev = line;
    }
  }
  writef("%d\n", c);
}

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

immutable int mod = 10^^9 + 7;

void main(){
    int n = readln.chomp.to!int;

    auto p = readln.chomp;

    int ans = 1, value = 1;

    foreach(i ; 0 .. n - 1){
        auto magnet = readln.chomp;

        if (magnet == p) {
            value++;
            ans = max(ans, value);
        }
        else {
            value = 1;
        }

        p = magnet;
    }

    writeln(ans);
}

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


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 a = RDA;

		long x = a[$-1];
		while (true)
		{
			int l = -1;
			foreach_reverse (i; 0..n)
			{
				if (a[i] != x)
				{
					l = i;
					break;
				}
			}
			if (l == -1) break;
			int len = n - l - 1;
			foreach (i; 0..len)
			{
				if (l - i < 0) break;
				a[l-i] = x;
			}
			++ans[ti];
		}
	}

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

import std;

void main()
{
    int t;
    scanf("%d", &t);
    getchar();
    foreach(_; 0..t)
    {
        int n;
        scanf("%d", &n);
        getchar();
        int[] a = readln.strip.split(" ").to!(int[]);
        int res = 0;
        while (a.uniq.array.length != 1)
        {
            // writeln(a);
            // writeln("len = ", a.uniq.array.length);
            int left_pad;
            int indx;
            for(int i = n - 1; i >= 0; i--)
            {
                if (a[i] == a[n-1])
                    left_pad++;
                else
                {
                    indx = i;
                    break;
                }
            }
            if (indx - left_pad + 1 > 0)
            {
                // writeln("changing from", indx - left_pad + 1, " to ", indx + 1);
                a[indx - left_pad + 1 .. indx + 1] = a[n - 1];
            }
            else
            {
                a[0 .. indx + 1] = a[n - 1];
            }
            res++;
        }
        writeln(res);
    }
}


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

long evalIt(ref string s,string sub){
	if(s.ptr==sub.ptr){
		auto l=sub.length;
		auto r=evalPlus(sub,null);
		s=s[l..$];
		return r;
	}
	long r=s[0]-'0';
	s=s[1..$];
	return r;
}

long evalMult(ref string s,string sub){
	long cur=evalIt(s,sub);
	while(s.length&&s[0]=='*'){
		s=s[1..$];
		cur*=evalIt(s,sub);
	}
	return cur;
}

long evalPlus(ref string s,string sub){
	long cur=evalMult(s,sub);
	while(s.length){
		assert(s[0]=='+');
		s=s[1..$];
		cur+=evalMult(s,sub);
	}
	return cur;
}

void main(){
	string it=readln.strip;
	string ott=it;
	long cand=evalPlus(ott,null);
	foreach(i;0..it.length){
		if(it[i]=='*'){
			foreach(j;i+2..it.length+1){
				if(j==it.length||it[j]=='+'||it[j]=='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[i+1..j]));
				}
			}
			foreach(j;0..i){
				if(it[j]!='+'&&it[j]!='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[j..i]));
				}
			}
		}
	}
	writeln(cand);
}

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.ascii;

long factor(string s, ref int p) {
  if (s[p] == '(') {
    p++;
    long r = expr(s, p);
    assert(s[p] == ')');
    p++;
    return r;
  } else if ('0' <= s[p] && s[p] <= '9') {
    long r = s[p] - '0';
    p++;
    return r;
  } else {
    assert(false);
  }
}

long term(string s, ref int p) {
  long r = factor(s, p);
  while (s[p] == '*') {
    p++;
    r *= factor(s, p);
  }
  return r;
}

long expr(string s, ref int p) {
  long r = term(s, p);
  while (s[p] == '+') {
    p++;
    r += term(s, p);
  }
  return r;
}

void main() {
  string s = readln().strip();
  s = "1*" ~ s ~ "*1$";
  int n = cast(int)s.length;
  long ans = 0;
  foreach (i; 0..n) {
    if (s[i] == '*') {
      foreach (j; i+1..n) {
        if (s[j] == '*') {
          int p = 0;
          p = 0;
          ans = max(ans, expr(s[0..i+1] ~ "(" ~ s[i+1..j] ~ ")" ~ s[j..$], p));
        }
      }
    }
  }
  writeln(ans);
}

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.ascii;

long factor(string s, ref int p) {
  if (s[p] == '(') {
    p++;
    long r = expr(s, p);
    assert(s[p++] == ')');
    return r;
  } else if (s[p].isDigit()) {
    return s[p++] - '0';
  } else {
    assert(false);
  }
}

long term(string s, ref int p) {
  long r = factor(s, p);
  while (s[p] == '*') {
    p++;
    r *= factor(s, p);
  }
  return r;
}

long expr(string s, ref int p) {
  long r = term(s, p);
  while (s[p] == '+') {
    p++;
    r += term(s, p);
  }
  return r;
}

void main() {
  string s = readln().strip();
  s = "1*" ~ s ~ "*1$";
  int n = cast(int)s.length;
  long ans = 0;
  foreach (i; 0..n) {
    if (s[i] == '*') {
      foreach (j; i+1..n) {
        if (s[j] == '*') {
          int p = 0;
          ans = max(ans, expr(s[0..i+1] ~ "(" ~ s[i+1..j] ~ ")" ~ s[j..$], p));
        }
      }
    }
  }
  writeln(ans);
}

import std.stdio;
import std.c.stdio;
import std.math;
import std.algorithm;
import std.conv;
import std.string;
import std.array;
import std.ascii;

long factor(string s, ref int p) {
  if (s[p] == '(') {
    p++;
    long r = expr(s, p);
    assert(s[p++] == ')');
    return r;
  } else if ('0' <= s[p] && s[p] <= '9') {
    return s[p++] - '0';
  } else {
    assert(false);
  }
}

long term(string s, ref int p) {
  long r = factor(s, p);
  while (s[p] == '*') {
    p++;
    r *= factor(s, p);
  }
  return r;
}

long expr(string s, ref int p) {
  long r = term(s, p);
  while (s[p] == '+') {
    p++;
    r += term(s, p);
  }
  return r;
}

void main() {
  string s = readln().strip();
  s = "1*" ~ s ~ "*1$";
  int n = cast(int)s.length;
  long ans = 0;
  foreach (i; 0..n) {
    if (s[i] == '*') {
      foreach (j; i+1..n) {
        if (s[j] == '*') {
          int p = 0;
          ans = max(ans, expr(s[0..i+1] ~ "(" ~ s[i+1..j] ~ ")" ~ s[j..$], p));
        }
      }
    }
  }
  writeln(ans);
}

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

long evalIt(ref string s,string sub){
	if(s.ptr==sub.ptr){
		auto l=sub.length;
		auto r=evalPlus(sub,null);
		s=s[l..$];
		return r;
	}
	long r=s[0]-'0';
	s=s[1..$];
	return r;
}

long evalMult(ref string s,string sub){
	long cur=evalIt(s,sub);
	while(s.length&&s[0]=='*'){
		s=s[1..$];
		cur*=evalIt(s,sub);
	}
	return cur;
}

long evalPlus(ref string s,string sub){
	long cur=evalMult(s,sub);
	while(s.length){
		assert(s[0]=='+');
		s=s[1..$];
		if(s.ptr==sub.ptr) cur+=evalIt(s,sub);
		else cur+=evalMult(s,sub);
	}
	return cur;
}

void main(){
	string it=readln.strip;
	string ott=it;
	long cand=evalPlus(ott,null);
	foreach(i;0..it.length){
		if(it[i]=='*'){
			foreach(j;i+1..it.length){
				if(it[j]=='+'||it[j]=='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[i+1..j]));
				}
			}
			foreach(j;0..i-1){
				if(it[j]!='+'&&it[j]!='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[j..i]));
				}
			}
		}
	}
	writeln(cand);
}

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

long evalIt(ref string s,string sub){
	if(s.ptr==sub.ptr){
		auto l=sub.length;
		auto r=evalPlus(sub,null);
		s=s[l..$];
		return r;
	}
	long r=s[0]-'0';
	s=s[1..$];
	return r;
}

long evalMult(ref string s,string sub){
	long cur=evalIt(s,sub);
	while(s.length&&s[0]=='*'){
		s=s[1..$];
		cur*=evalIt(s,sub);
	}
	return cur;
}

long evalPlus(ref string s,string sub){
	long cur=evalMult(s,sub);
	while(s.length){
		assert(s[0]=='+');
		s=s[1..$];
		if(s.ptr==sub.ptr) cur+=evalIt(s,sub);
		else cur+=evalMult(s,sub);
	}
	return cur;
}

void main(){
	string it=readln.strip;
	string ott=it;
	long cand=evalPlus(ott,null);
	foreach(i;0..it.length){
		if(it[i]=='*'){
			foreach(j;i+2..it.length){
				if(it[j]=='+'||it[j]=='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[i+1..j]));
				}
			}
			foreach(j;0..i-1){
				if(it[j]!='+'&&it[j]!='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[j..i]));
				}
			}
		}
	}
	writeln(cand);
}

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

long evalIt(ref string s,string sub){
	if(s.ptr==sub.ptr){
		auto l=sub.length;
		auto r=evalPlus(sub,null);
		s=s[l..$];
		return r;
	}
	long r=s[0]-'0';
	s=s[1..$];
	return r;
}

long evalMult(ref string s,string sub){
	long cur=evalIt(s,sub);
	while(s.length&&s[0]=='*'){
		s=s[1..$];
		cur*=evalIt(s,sub);
	}
	return cur;
}

long evalPlus(ref string s,string sub){
	long cur=evalMult(s,sub);
	while(s.length){
		assert(s[0]=='+');
		s=s[1..$];
		if(s.ptr==sub.ptr) cur+=evalIt(s,sub);
		else cur+=evalMult(s,sub);
	}
	return cur;
}

void main(){
	string it=readln.strip;
	string ott=it;
	long cand=evalPlus(ott,null);
	foreach(i;0..it.length){
		if(it[i]=='*'){
			foreach(j;i+2..it.length+1){
				if(j==it.length||it[j]=='+'||it[j]=='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[i+1..j]));
				}
			}
			foreach(j;0..i-1){
				if(it[j]!='+'&&it[j]!='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[j..i]));
				}
			}
		}
	}
	writeln(cand);
}

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

long evalIt(ref string s,string sub){
	if(s.ptr==sub.ptr){
		auto l=sub.length;
		auto r=evalPlus(sub,null);
		s=s[l..$];
		return r;
	}
	long r=s[0]-'0';
	s=s[1..$];
	return r;
}

long evalMult(ref string s,string sub){
	long cur=evalIt(s,sub);
	while(s.length&&s[0]=='*'){
		s=s[1..$];
		cur*=evalIt(s,sub);
	}
	return cur;
}

long evalPlus(ref string s,string sub){
	long cur=evalMult(s,sub);
	while(s.length){
		assert(s[0]=='+');
		s=s[1..$];
		if(s.ptr==sub.ptr) cur+=evalIt(s,sub);
		else cur+=evalMult(s,sub);
	}
	return cur;
}

void main(){
	string it=readln.strip;
	string ott=it;
	long cand=evalPlus(ott,null);
	foreach(i;0..it.length){
		if(it[i]=='*'){
			foreach(j;i+2..it.length+1){
				if(j==it.length||it[j]=='+'||it[j]=='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[i+1..j]));
				}
			}
			foreach(j;0..i){
				if(it[j]!='+'&&it[j]!='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[j..i]));
				}
			}
		}
	}
	writeln(cand);
}

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

long evalIt(ref string s,string sub){
	if(s.ptr==sub.ptr){
		auto l=sub.length;
		auto r=evalPlus(sub,null);
		s=s[l..$];
		return r;
	}
	long r=s[0]-'0';
	s=s[1..$];
	return r;
}

long evalMult(ref string s,string sub){
	long cur=evalIt(s,sub);
	while(s.length&&s[0]=='*'){
		s=s[1..$];
		cur*=evalIt(s,sub);
	}
	return cur;
}

long evalPlus(ref string s,string sub){
	long cur=evalMult(s,sub);
	while(s.length){
		assert(s[0]=='+');
		s=s[1..$];
		cur+=evalMult(s,sub);
	}
	return cur;
}

void main(){
	string it=readln.strip;
	string ott=it;
	long cand=evalPlus(ott,null);
	foreach(i;0..it.length){
		if(it[i]=='*'){
			foreach(j;i+1..it.length){
				if(it[j]=='+'||it[j]=='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[i+1..j]));
				}
			}
			foreach(j;0..i-1){
				if(it[j]!='+'&&it[j]!='*'){
					string ot=it;
					cand=max(cand,evalPlus(ot,it[j..i]));
				}
			}
		}
	}
	writeln(cand);
}


import std.stdio;
import std.math;

void main()
{
	int n, t;

	scanf("%d%d", &n, &t);
	printf("%.10f\n", n * pow(1.000000011, t));
}

import std.math;
import std.stdio;

void main ()
{
	real n, t;
	while (readf (" %s %s", &n, &t) > 0)
	{
		writefln ("%.20f", n * 1.000000011L ^^ t);
	}
}


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

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

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

        double read_double() {
                return read_string.to!double;
        }

        string[] tokens;
}

void main() {
        IO cin;
        int t = 1;
        t = cin.read_int;
        while (t--) {
                int hh, mm;
                hh = cin.read_int;
                mm = cin.read_int;
                writeln((24 - hh - 1) * 60 + (60 - mm));
        }
}

import std.stdio, std.conv, std.string, std.array, std.math, std.regex, std.range, std.ascii, std.numeric, std.random;
import std.typecons, std.functional, std.traits,std.concurrency;
import std.algorithm, std.container;
import core.bitop, core.time, core.memory;
import std.datetime;
import std.bitmanip;
import std.regex;

void main()
{
    auto N = scanElem;
    foreach (e; scanElem!(long,long)(N))
    {
        writeln(60*(24-e[0]-1)+60-e[1]);
    }
}

//辞書順順列はiota(1,N),nextPermituionを使う

enum INF = long.max/3;
enum MOD = 10L^^9+7;

T binarySearch(alias F, T)(T ok, T ng, long iter=100)
{
    alias FF = unaryFun!F;
    foreach(_;0..iter)
    {
        auto n = (ok+ng)/2;
        if(FF(n)){
            ok = n;
        }else{
            ng = n;
        }
        static if(isIntegral!T){
            if(abs(ok-ng)==1)return ok;
        }
    }
    return ok;
}

//最小を返す
real ternarySearch(alias F)(real l, real r, long iter=200)
{
    alias FF = unaryFun!F;
    foreach(_;0..iter)
    {
        auto nl = lerp(l, r, 1/real(3));
        auto nr = lerp(l, r, 2/real(3));
        if(FF(nl)<FF(nr)){
            r = nr;
        }else{
            l = nl;
        }
    }
    return FF(l);
}
long ternarySearch(alias F)(long l, long r, long iter=200)
{
    alias FF = unaryFun!F;
    foreach(_;0..iter)
    {
        auto nl = lerp(l, r, 1/real(3)).to!long;
        auto nr = lerp(l, r, 2/real(3)).to!long;
        if(FF(nl)<FF(nr)){
            r = nr;
        }else{
            l = nl;
        }
        if(r-l<4)break;
    }
    long res=INF;
    foreach(i;l..r+1)
    {
        res = min(FF(i), res);
    }
    return res;
}

real lerp(real a, real b, real t) pure
{
    return (b-a)*t+a;
}

struct Vector{
    real x, y;

    real magnitude()pure
    {
        return sqrt(sqrMagnitude);
    }
    real sqrMagnitude()pure
    {
        return x*x+y*y;
    }

    Vector opBinary(string op, T)(inout T v)
    if(isNumeric!T)
    {
        return Vector(mixin("x"~op~"v"), mixin("y"~op~"v"));
    }
    Vector opBinary(string op)(inout Vector v)
    {
        return Vector(mixin("x"~op~"v.x"), mixin("y"~op~"v.y"));
    }
    void opOpAssign(string op, T)(inout T v)
    {
        this = mixin("this"~op~"v");
    }

    static Vector lerp(Vector a, Vector b, real t)pure
    {
        return (b-a)*t+a;
    }
    static Vector normalized(Vector a)pure
    {
        auto r = a.magnitude();
        return Vector(a.x / r, a.y / r);
    }
    static real distance(Vector a, Vector b)pure
    {
        return (a-b).magnitude;
    }
    static real dot(Vector a, Vector b)pure
    {
        return a.x*b.x+a.y*b.y;
    }
    static real cross(Vector a, Vector b)pure
    {
        return a.x*b.y-b.x*a.y;
    }
}

//ascii文字のみ
long[] suffixArray(Char)(const(Char)[] s) pure
if(isSomeChar!Char)
// in{assert(s.all!"0<=a&&a<127");}
// do
{
    s ~= '\0';
    long[] p = new long[s.length];
    long[] c = new long[s.length];
    {
        long[127] cnt;
        foreach(i;0..s.length)cnt[s[i]]++;
        foreach(i;1..cnt.length)cnt[i] += cnt[i-1];
        foreach(i;0..s.length)p[--cnt[s[i]]] = i;
        long classes=1;
        foreach(i;1..p.length){
            classes += s[p[i]]!=s[p[i-1]]?1:0;
            c[p[i]] = classes-1;
        }
    }
    long[] pn = new long[s.length];
    long[] cn = new long[s.length];
    for(long n=0;(1L<<n) < s.length;n++)
    {
        p.map!(a=>mod(a-(1L<<n), s.length)).copy(pn);
        long[127] cnt;
        foreach(i;0..pn.length)cnt[c[pn[i]]]++;
        foreach(i;1..cnt.length)cnt[i] += cnt[i-1];
        foreach_reverse(i;0..pn.length)p[--cnt[c[pn[i]]]] = pn[i];
        long classes=1;
        for(long i=1;i<c.length;i++)
        {
            if(
                tuple(c[p[i]], c[(p[i]+(1L<<n))%s.length]) !=
                tuple(c[p[i-1]], c[(p[i-1]+(1L<<n))%s.length])
            ) {
                classes++;
            }
            cn[p[i]] = classes - 1;
        }
        cn.copy(c);
    }
    return p[1..$];
}

struct BiparticeMatching{
    int N;
    int[][] path;
    int[] match;
    this(long n)
    {
        N = n.to!int;
        path.length = N;
        match.length = N;
    }
    void addEdge(long u, long v){
        addEdge(u.to!int, v.to!int);
    }
    void addEdge(int u, int v){
        path[u] ~= v;
        path[v] ~= u;
    }
    long calc(){
        auto used = new bool[N];
        bool dfs(int v){
            used[v] = true;
            foreach(u; path[v]){
                int w = match[u];
                if(w==-1||(!used[w]&&dfs(w))){
                    match[v]=u;
                    match[u]=v;
                    return true;
                }
            }
            return false;
        }
        match[] = -1;
        long res = 0;
        foreach(int v; 0..N){
            if(match[v] == -1){
                used[] = false;
                if(dfs(v)){
                    res++;
                }
            }
        }
        return res;
    }
}

// int max_flow(int s, int t){
//     struct edge{
//         int to, cap,  rev;
//     }
//     edge[][MAX_V] G;
//     bool[MAX_V] used;
//     void add_edge(int from, int to, int cap){
//         G[from] ~= edge(to, cap, G[to].length);
//         G[to] ~= edge(from, 0, G[from].length-1);
//     }
//     int dfs(int v, int t, int f)
//     {
//         if(v==t)return f;
//         used[v] = true;
//         foreach(i;0..G[v].length)
//         {
//             edge e = G[v][i];
//             if(!used[e.to] && e.cap > 0){
//                 int d = dfs(e.to, t, min(f, e.cap));
//                 if(d>0){
//                     e.cap -= d;
//                     G[e.to][e.rev].cap +=d ;
//                     return d;
//                 }
//             }
//         }
//         return 0;
//     }
//     int flow = 0;
//     for(;;){
//         int f = dfs(s,t,INF);
//         if(f==0)return flow;
//         flow += f;
//     }
// }

struct CombTable2(long MOD)
{
    static long[] fac;
    static long[] finv;
    static long[] inv;
    this(long n)
    {
        fac = new long[n];
        finv = new long[n];
        inv = new long[n];

        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        foreach(i;2..n){
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

    long comb(long n, long k)
    {
        if (n < k) return 0;
        if (n < 0 || k < 0) return 0;
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
}
struct CombTable(long MOD, long n)
{
    static long[n] fac;
    static long[n] finv;
    static long[n] inv;
    static this()
    {
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        foreach(i;2..n){
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

    static long comb(long n, long k)
    {
        if (n < k) return 0;
        if (n < 0 || k < 0) return 0;
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
}

void outLine(List...)(List list)
{
    foreach(i, v;list)
    {
        static if(isFloatingPoint!(typeof(v)))
        {
            writef("%.12f", v);
        }else{
            write(v);
        }
        if(i+1!=list.length)write(' ');
    }
    writeln;
}

void end(List...)(List list)
{
    outLine(list);
    end;
}
void end()
{
    import core.stdc.stdlib;
    exit(0);
}

long sequenceSum(long n) pure
{
    return n*(n+1)/2;
}

//HL分解
struct HeavyLightDecomposition
{
    immutable int root = 1;

    int[][] edge;
    int[] vid;
    int[] invid;
    int[] parent;
    int[] depth;
    int[] subCount;
    int[] head;

    this(long n, long root = 1)
    {
        this(n.to!int, root.to!int);
    }
    this(int n, int root = 1)
    {
        this.root = root;
        n++;
        edge.length = n;
        vid.length = n;
        invid.length = n;
        parent.length = n; parent[] = -1;
        depth.length = n;
        head.length = n; head[root] = root;
        subCount.length = n; subCount[] = 1;
    }

    void addEdge(long u, long v)
    {
        addEdge(u.to!int, v.to!int);
    }
    void addEdge(int u, int v)
    {
        edge[u] ~= v;
        edge[v] ~= u;
    }

    void build()
    {
        void dfs(int v){
            foreach(ref u; edge[v])
            {
                if(u==parent[v]) continue;
                parent[u] = v;
                depth[u] = depth[v] + 1;
                dfs(u);
                subCount[v] += subCount[u];
                if(edge[v][0]==parent[v]||subCount[u]>subCount[edge[v][0]])
                    swap(u, edge[v][0]);
            }
        }
        void dfsHead(int v, ref int pos){
            invid[pos] = v;
            vid[v] = pos;
            pos++;
            foreach(ref u; edge[v])
            {
                if(u==parent[v]) continue;

                head[u] = u == edge[v][0] ? head[v] : u;
                dfsHead(u, pos);
            }
        }
        dfs(root);
        int pos;
        dfsHead(root, pos);
    }

    long lca(long u, long v)
    {
        return lca(u.to!int, v.to!int);
    }
    int lca(int u, int v)
    {
        while(true){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]==head[v]) return u;
            v = parent[head[v]];
        }
    }

    long distance(long u, long v) { return distance(u.to!int, v.to!int); }
    long distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u,v)]; }

    //idxのn個上の親を返す
    //バグあるかも
    long nParent(long n, long idx){
        return nParent(n.to!int, idx.to!int);
    }
    int nParent(int n, int idx){
        auto u = 0;
        auto v = idx;
        while(true)
        {
            if(vid[u]>vid[v]) swap(u,v);

            immutable _u = max(vid[head[v]], vid[u]);
            immutable _v = vid[v] + 1;
            if(_v<=_u+n){
                n -= _v-_u;
            }else{
                return invid[_v-n-1];
            }

            if(head[u]==head[v]) return -1;
            v = parent[head[v]];
        }
    }

    void each(long u, long v, void delegate(long u, long v) pred)
    {
        each(u.to!int, v.to!int, pred);
    }
    void each(int u, int v, void delegate(int u, int v) pred)
    {
        while(true)
        {
            if(vid[u]>vid[v]) swap(u,v);
            pred(max(vid[head[v]], vid[u]), vid[v] + 1);
            if(head[u]==head[v]) return;
            v = parent[head[v]];
        }
    }

    void each(alias pred)(long u, long v)
    if(is(typeof(binaryFun!pred(0L,0L))))
    {
        while(true)
        {
            if(vid[u]>vid[v]) swap(u,v);
            binaryFun!pred(max(vid[head[v]], vid[u]), vid[v] + 1);
            if(head[u]==head[v]) return;
            v = parent[head[v]];
        }
    }
}

// struct HLD{

//     long[][] G;
//     long[] vid, head, sub, par, dep, inv, type;

//     void dfs_sz(long v) {
//         foreach(ref u; G[v])
//             if(u==par[v]) swap(u,G[v].back());
//         if(~par[v]) G[v].popBack;

//         foreach(ref u; G[v]){
//             par[u]=v;
//             dep[u]=dep[v]+1;
//             dfs_sz(u);
//             sub[v]+=sub[u];
//             if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]);
//         }
//     }

//     void dfs_hld(long v,long c,ref long pos) {
//         vid[v]=pos++;
//         inv[vid[v]]=v;
//         type[v]=c;
//         foreach(u; G[v]){
//             if(u==par[v]) continue;
//             head[u]=(u==G[v][0]?head[v]:u);
//             dfs_hld(u,c,pos);
//         }
//     }

//     this(long n){
//         G = new long[][n];
//         vid = new long[n]; vid[] = -1;
//         head = new long[n];
//         sub = new long[n]; sub[] = 1;
//         par = new long[n]; par[] = -1;
//         dep = new long[n];
//         inv = new long[n];
//         type = new long[n];
//     }

//     void add_edge(long u,long v) {
//         G[u] ~= v;
//         G[v] ~= u;
//     }

//     void build(long[] rs = [0]) {
//         long c=0,pos=0;
//         foreach(r; rs){
//             dfs_sz(r);
//             head[r]=r;
//             dfs_hld(r,c++,pos);
//         }
//     }

//     long lca(long u,long v){
//         while(1){
//             if(vid[u]>vid[v]) swap(u,v);
//             if(head[u]==head[v]) return u;
//             v=par[head[v]];
//         }
//     }

//     long distance(long u,long v){
//         return dep[u]+dep[v]-2*dep[lca(u,v)];
//     }

//     // for_each(vertex)
//     // [l, r) <- attention!!
//     void for_each(F)(long u, long v, F f) {
//         while(1){
//             if(vid[u]>vid[v]) swap(u,v);
//             f(max(vid[head[v]],vid[u]),vid[v]+1);
//             if(head[u]!=head[v]) v=par[head[v]];
//             else break;
//         }
//     }

//     T for_each(T, Q, F)(long u,long v,T ti,Q q,F f)
//     {
//         T l=ti,r=ti;
//         while(1){
//             if(vid[u]>vid[v]){
//                 swap(u,v);
//                 swap(l,r);
//             }
//             l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1));
//             if(head[u]!=head[v]) v=par[head[v]];
//                 else break;
//         }
//         return f(l,r);
//     }

//     // for_each(edge)
//     // [l, r) <- attention!!
//     void for_each_edge(F)(long u, long v,F f) {
//         while(1){
//             if(vid[u]>vid[v]) swap(u,v);
//             if(head[u]!=head[v]){
//                 f(vid[head[v]],vid[v]+1);
//                 v=par[head[v]];
//             }else{
//                 if(u!=v) f(vid[u]+1,vid[v]+1);
//                 break;
//             }
//         }
//     }
// }

struct SegTree(T, T UNIT, alias pred){
    int n;
    long size;
    T* arr;
    alias F = binaryFun!pred;

    this(long size)
    {
        this.size = size;
        n=1;
        while(n<size) n<<=1;
        arr = new T[n<<1].ptr;
        arr[0..n<<1] = UNIT;
    }

    void set(long k,T x)
    {
        assert(k>=0&&k<size);

        arr[k+=n]=x;
        while(k>>=1)
            arr[k]=F(arr[(k<<1)|0],arr[(k<<1)|1]);
    }

    T query(long a, long b)
    {
        assert(a>=0&&a<size);
        assert(b>=1&&b<=size);

        T vl=UNIT,vr=UNIT;
        for(long l=a+n,r=b+n;l<r;l>>=1,r>>=1)
        {
            if(l&1) vl=F(vl,arr[l++]);
            if(r&1) vr=F(arr[--r],vr);
        }
        return F(vl,vr);
    }
}

bool isInf(Num)(Num v) pure @nogc
{
    return v>=INF/2;
}

long mod(long n, long mod) pure @nogc
{
    return (n%mod+mod)%mod;
}

long pow(long a, long n) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a;
        a = a * a;
        n >>= 1;
    }
    return res;
}
long powmod(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

alias MInt = ModInt!MOD;

struct ModInt(alias Mod)
if(isIntegral!(typeof(Mod)) && isPrime(Mod))
{
    long value;

    this(ModInt!Mod v)
    {
        value = v.value;
    }
    this(long v)
    {
        value = mod(v, Mod);
    }

    ModInt!Mod opBinaryRight(string op, T)(inout T v) const
    if((op=="+"||op=="-"||op=="*")&&isIntegral!T)
    {
        return typeof(this)(mixin("v"~op~"value"));
    }
    ModInt!Mod opBinary(string op, T)(inout T v) const
    if((op=="+"||op=="-"||op=="*")&&(is(T==ModInt!Mod)||isIntegral!T))
    {
        return typeof(this)(mixin("value"~op~"v"));
    }

    ModInt!Mod opBinaryRight(string op, T)(inout T v) const
    if((op=="/")&&isIntegral!T)
    {
        return v * (this^^(Mod-2));
    }
    ModInt!Mod opBinary(string op, T)(inout T v) const
    if((op=="/")&&(is(T==ModInt!Mod)||isIntegral!T))
    {
        return this * (typeof(this)(v)^^(Mod-2));
    }

    ModInt!Mod opBinaryRight(string op, T)(inout T v) const
    if((op=="^^")&&isIntegral!T)
    {
        return typeof(this)(powmod(v, value, Mod));
    }
    ModInt!Mod opBinary(string op, T)(inout T v) const
    if((op=="^^")&&(is(T==ModInt!Mod)||isIntegral!T))
    {
        return typeof(this)(powmod(value, v, Mod));
    }

    void opAssign(T)(inout T v)
    if(isIntegral!T)
    {
        this = typeof(this)(v);
    }

    void opOpAssign(string op, T)(inout T v)
    {
        this = mixin("this"~op~"v");
    }

    bool opEquals(T)(const T v) const
    if(is(T==ModInt!Mod)||isIntegral!T)
    {
        return value == typeof(this)(v).value;
    }

    long opCast() const {
        return value;
    }

    string toString() const {
        return value.to!string;
    }
}
unittest
{
    assert(is(ModInt!MOD));
    assert(is(ModInt!17));
    assert(!is(ModInt!0));
    assert(!is(ModInt!10));
}

unittest
{
    alias MInt = ModInt!13;
    MInt value;
    value = MInt(14) + MInt(18);
    assert(value==6);
    value = 14 - MInt(28);
    assert(value==12);
    value = MInt(17) * -19;
    assert(value==2);

    value = MInt(7) / 4;
    assert(value==5);
    value = 8 / MInt(4);
    assert(value==2);
    value = 9;
    value /= 4;
    assert(value==12);

    assert(MInt(3) ^^ 9 == 1);
    assert(MInt(54) ^^ MInt(9) == 5);

    value = 29-MInt(16);
    assert(value==0);
    value = MInt(13)*5;
    assert(value==0);
    value = 0;
    assert(value==0);

    value = 3;
    value += MInt(11);
    assert(value==1);
    value -= 7;
    assert(value==7);
    value *= MInt(4);
    assert(value==2);
    value = 23;
    assert(value == cast(long)value);
}
unittest
{
    ModInt!MOD value;
    value = MOD-1;
    assert(value==MOD-1);
    value = MOD;
    assert(value==0);
}

struct Grid(T){
    private T[] grid;
    size_t width, height;
    private size_t stride;

    private this(T[] g, size_t w, size_t h, size_t s)
    {
        grid = g;
        width = w;
        height = h;
        stride = s;
    }

    this(long w, long h, T init = T.init)
    {
        auto arr = new T[(w*h).to!int];
        arr[] = init;
        this(arr, w.to!size_t, h.to!size_t, w.to!size_t);
    }

    // void fill(T elem){
    //     grid[] = elem;
    // }

    bool isInRange(long x, long y) const nothrow
    {
        return
            x>=0 &&
            x<width &&
            y>=0 &&
            y<height;
    }

    int opApply(scope int delegate(ref T) dg)
    {
        int result = 0;

        foreach(y;0..height)
        foreach(x;0..width)
        {
            result = dg(this[x,y]);
            if (result)
                break;
        }
        return result;
    }
    int opApply(scope int delegate(long i, ref T) dg)
    {
        int result = 0;

        long idx;
        foreach(y;0..height)
        foreach(x;0..width)
        {
            result = dg(idx++, this[x,y]);
            if (result)
                break;
        }
        return result;
    }
    int opApply(scope int delegate(long x, long y, ref T) dg)
    {
        int result = 0;

        foreach(y;0..height)
        foreach(x;0..width)
        {
            result = dg(x, y, this[x,y]);
            if (result)
                break;
        }
        return result;
    }

    ref T opIndex(long x, long y)
    {
        assert(isInRange(x,y));
        return (grid.ptr)[(x+stride*y).to!size_t];
    }

    Grid!T opIndex(long[2] r1, long[2] r2)
    {
        auto startOffset = (r1[0] + r2[0]*stride).to!size_t;
        auto endOffset = (r1[1] + (r2[1] - 1)*stride).to!size_t;

        auto resGrid = grid[startOffset .. endOffset];
        auto resStride = stride;
        auto resWidth = (r1[1] - r1[0]).to!size_t;
        auto resHeight = (r2[1] - r2[0]).to!size_t;
        return Grid!T(resGrid, resWidth, resHeight, resStride);
    }

    Grid!T opIndex(long[2] r1, long j) { return opIndex(r1, [j, j+1]); }
    Grid!T opIndex(long i, long[2] r2) { return opIndex([i, i+1], r2); }

    long[2] opSlice(long dim)(long start, long end)
    if (dim == 0 || dim == 1)
    // in { assert(start >= 0 && end <= this.opDollar!dim); }
    body{
        return [start, end];
    }

    Grid!T transpose(){
        auto grid = Grid!T(height, width);

        foreach(w; 0..width)
        foreach(h; 0..height)
        {
            grid[h, w] = this[w, h];
        }
        return grid;
    }

    long opDollar(long dim : 0)() const { return width; }
    long opDollar(long dim : 1)() const { return height; }

    string toString() pure {
        long count;
        foreach(y; 0..height)
        {
            foreach(x; 0..width)
            {
                long eCount = this[x, y].to!string.length;
                static if(is(typeof(this[x,y].isInf)))
                {
                    if(this[x,y].isInf)
                    {
                        eCount = 1;
                    }
                }
                static if(is(typeof(this[x,y])==bool))
                {
                    eCount = 1;
                }
                count = max(count, eCount);
            }
        }
        count++;

        string res = "\n";
        foreach(y; 0..height)
        {
            foreach(x; 0..width)
            {
                string joinStr = this[x, y].to!string;
                static if(is(typeof(this[x,y].isInf)))
                {
                    if(this[x,y].isInf)
                    {
                        joinStr = "*";
                    }
                }
                static if(is(typeof(this[x,y])==bool))
                {
                    joinStr = this[x,y]?"+":"-";
                }
                foreach(i;0..count-joinStr.length)
                {
                    res ~= ' ';
                }
                res ~= joinStr;
            }
            res ~= '\n';
        }
        return res;
    }
}

struct UnionFind{
    private int[] arr;
    int rootCount;

    @disable this();
    this(long n){
        arr.length = n.to!int;
        arr[] = -1;
        rootCount = n.to!int;
    }

    void merge(long a, long b)
    {
        merge(a.to!int, b.to!int);
    }
    void merge(int a, int b)
    {
        if(same(a,b)) return;
        arr[root(a)] = root(b);
        rootCount--;
    }

    bool same(long a, long b)
    {
        return same(a.to!int, b.to!int);
    }
    bool same(int a, int b)
    {
        return root(a)==root(b);
    }

    private int root(int i)
    {
        if(arr[i] == -1) return i;
        return arr[i] = root(arr[i]);
    }
}

unittest{
    assert(is(typeof(UnionFind(10))));
    assert(!is(typeof(UnionFind())));

    auto uf = UnionFind(10);
    uf.merge(2,3);
    assert(uf.same(2,3));
    uf.merge(3,4);
    uf.merge(1,5);
    uf.merge(5,6);
    uf.merge(6,7);
    assert(!uf.same(4,7));
    uf.merge(1,2);
    assert(uf.same(4,7));
}

void debugPrint(List...)()
{
    void _debugPrintElem(alias elem, float rad)()
    {
        import std.experimental.color;
        import std.experimental.color.lab;
        import std.experimental.color.rgb;
        import std.experimental.color.xyz;

        enum color = LCh!float(80f, 100f, rad);
        enum rgb = color.convertColor!(Lab!float).convertColor!(XYZ!float).convertColor!(RGB8).tristimulus;
        enum r = rgb[0].value;
        enum g = rgb[1].value;
        enum b = rgb[2].value;

        stderr.writef!"\033[38;2;%s;%s;%sm"(r, g, b);
        static if(isSomeFunction!elem)
        {
            stderr.write("elem: ", elem(), " ");
        }else{
            enum name =  __traits(identifier, elem);
            stderr.write(name, ": ");
            stderr.write(elem, " ");
        }
    }
    void _debugPrint(int i, float rad, List...)()
    {
        _debugPrintElem!(List[0], i * rad)();
        static if(List.length>1)
        {
            _debugPrint!(i+1, rad, List[1..$])();
        }
    }

    debug(Local)
    {
        _debugPrint!(0, 360f/List.length, List)();
        stderr.writeln("\033[0m");
    }
}

struct Stack(Elem){
    private Elem[] array;
    private size_t endIdx;

    void insertBack(Elem e)
    {
        if(endIdx==array.length){
            array.length = array.length*2+10;
        }
        (array.ptr)[endIdx++] = e;
    }

    void insertBack(Range)(Range range)
    if(is(typeof(array[0] = range.popFront)))
    {
        if(endIdx+range.length>array.length){
            array.length = (endIdx+range.length)*2+10;
        }
        foreach(ref e;range)
        {
            (array.ptr)[endIdx++] = e;
        }
    }

    Elem[] opSlice(){
        return array[0..endIdx];
    }

    void popBack()
    {
        assert(endIdx!=0);
        endIdx--;
    }

    ref Elem back()
    {
        assert(endIdx!=0);
        return (array.ptr)[endIdx-1];
    }

    size_t count() const
    {
        return endIdx;
    }

    bool empty() const
    {
        return endIdx==0;
    }

    void clear()
    {
        endIdx = 0;
    }
}

Grid!T scanGrid(T=long)(long w, long h, dchar t='.')
{
    auto grid = Grid!T(w,h);
    foreach(y;0..h)
    {
        foreach(x;0..w)
        {
            grid[x, y] = scanElem!T;
        }
    }

    return grid;
}

Grid!bool scanGridBool(long w, long h, dchar t='.')
{
    auto grid = Grid!bool(w,h);
    foreach(y;0..h.to!size_t)
    {
        auto line = scanString;
        foreach(x;0..w.to!size_t)
        {
            grid[x, y] = line[x.to!size_t]==t;
        }
    }

    return grid;
}

T[] scanLineArray(T = long)()
{
    static char[] scanBuf;
    readln(scanBuf);
    return scanBuf.split.to!(T[]);
}

char scanChar()
{
    int c = ' ';
    while (isWhite(c) && c != -1)
    {
        c = getchar;
    }
    return cast(char)c;
}

T[] scanElem(T=long)(long size)
{
    T[] list = new T[size.to!size_t];
    foreach(i;0..size.to!size_t)
    {
        list[i] = scanElem!T;
    }
    return list;
}

T scanElem(T)()
if(is(T==struct))
{
    T res;
    foreach(ref field; res.tupleof){
        field = scanElem!(typeof(field));
    }
    return res;
}

Tuple!List[] scanElem(List...)(long size)
{
    auto list = new Tuple!List[size.to!size_t];
    foreach(i;0..size.to!size_t)
    {
        list[i] = scanElem!List;
    }
    return list;
}
Tuple!List scanElem(List...)()
{
    List res;
    foreach(i, e; List){
        res[i] = scanElem!e;
    }
    return tuple(res);
}

T scanElem(T = long)()
if(isBasicType!T||isSomeString!T)
{
    import core.stdc.stdlib;
    static auto scanBuf = appender!(char[])([]);

    scanBuf.clear;

    int c = ' ';
    while (isWhite(c) && c != -1)
    {
        c = getchar;
    }
    while (!isWhite(c) && c != -1)
    {
        scanBuf ~= cast(char) c;
        c = getchar;
    }
    return scanBuf.data.to!T;
}

string scanString(){
    return scanElem!string;
}

CommonType!(A,B) gcd(A,B)(A a, B b) pure
if(isIntegral(A)&&isIntegral(B))
{
    if(b==0)return a;
    return gcd(b, a % b);
}

CommonType!(A,B) lcm(A,B)(A a, B b) pure
if(isIntegral(A)&&isIntegral(B))
{
    return a / gcd(a, b) * b;
}

struct Factor
{
    long n;
    long c;
}

//素因数分解
Factor[] factors(long n) pure
{
    Factor[] res;
    for (long i = 2; i ^^ 2 <= n; i++)
    {
        if (n % i != 0)
            continue;

        long c;
        while (n % i == 0)
        {
            n = n / i;
            c++;
        }
        res ~= Factor(i, c);
    }
    if (n != 1)
        res ~= Factor(n, 1);

    return res;
}
//約数をすべて列挙
long[] divisors(long n) pure
{
    long[] list;
    void func(Factor[] fs, long n)
    {
        if(fs.empty){
            list ~= n;
            return;
        }

        foreach(c; 0..fs[0].c+1)
        {
            func(fs[1..$], n * (fs[0].n ^^ c));
        }
    }

    func(factors(n), 1);
    sort(list);
    return list;
}
//nまでの素数のリスト
long[] primes(long n) pure
{
    if(n<2)return [];

    auto table = new long[cast(size_t)n+1];

    long[] res;
    for(size_t i = 2;i<=n;i++)
    {
        if(table[i]==-1) continue;
        for(size_t a = i;a<table.length;a+=i)
        {
            table[a] = -1;
        }
        res ~= i;
    }
    return res;
}
//素数判定
bool isPrime(long n) pure
{
    if (n <= 1)
        return false;
    if (n == 2)
        return true;
    if (n % 2 == 0)
        return false;
    for (long i = 3; i ^^ 2 <= n; i += 2)
        if (n % i == 0)
            return false;
    return true;
}

//桁を数える
long digitCount(long n, long base=10) pure
{
    long res;
    do{
        n/=base;
        res++;
    }while(n!=0);

    return res;
}

// unihernandez22
// https://codeforces.com/contest/1283/problem/A
// implementation

import std.stdio;
void main() {
	int t;
	"%d\n".readf(&t);
	foreach(_; 0..t) {
		int h, m;
		"%d %d\n".readf(&h, &m);
		writeln((24-h)*60 - m);
	}
}

import std.stdio;
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);
		writeln(1440-(a*60+b));
	}
	return 0;
}


import std.stdio;
import std.algorithm.sorting;

struct Group {
    int index;
    int size;
    int value;
}

struct Table {
    int index;
    int size;
    bool taken;
}

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

    int n;
    readf(" %s\n", &n);
    Group[] groups = new Group[n];
    foreach (i; 0 .. n) {
        readf(" %s %s\n", &groups[i].size, &groups[i].value);
        groups[i].index = i + 1;
    }
    int k;
    readf(" %s\n", &k);
    Table[] tables = new Table[k];
    foreach (i; 0 .. k) {
        readf(" %s", &tables[i].size);
        tables[i].index = i + 1;
    }

    int m = 0, s = 0;
    int[] reqnum;
    int[] tabnum;

    groups.sort!"a.value > b.value";
    auto st = tables.sort!"a.size < b.size";

    Table dummytab = Table(0, 0, false);
    foreach (g; groups) {
        dummytab.size = g.size;
        auto idx = st.lowerBound(dummytab).length;
        foreach (i; idx .. tables.length) {
            if (tables[i].taken == false) {
                tables[i].taken = true;
                m += 1;
                s += g.value;
                reqnum ~= g.index;
                tabnum ~= tables[i].index;
                break;
            }
        }
    }
    writeln(m, " ", s);
    foreach (i; 0 .. reqnum.length) {
        writeln(reqnum[i], " ", tabnum[i]);
    }

    return 0;
}

import std.stdio;
import std.algorithm.sorting;
import std.container.rbtree;

struct Group {
    int index;
    int size;
    int value;
}

struct Table {
    int index;
    int size;
    bool taken;
}

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

    int n;
    readf(" %s\n", &n);
    Group[] groups = new Group[n];
    foreach (i; 0 .. n) {
        readf(" %s %s\n", &groups[i].size, &groups[i].value);
        groups[i].index = i + 1;
    }
    int k;
    readf(" %s\n", &k);
    auto tableTree = new RedBlackTree!(Table, "a.size < b.size", true);
    //Table[] tables = new Table[k];
    foreach (i; 0 .. k) {
        Table t;
        readf(" %s", &t.size);
        t.index = i + 1;
        tableTree.insert(t);
    }

    int m = 0, s = 0;
    int[] reqnum;
    int[] tabnum;

    groups.sort!"a.value > b.value";

    Table t = Table(0, 0, false);
    foreach (g; groups) {
        t.size = g.size;
        t.taken = false;
        auto er = tableTree.equalRange(t);
        if (!er.empty) {
            t = er.front;
            t.taken = true;
        }
        else {
            auto ur = tableTree.upperBound(t);
            if(!ur.empty) {
                t = ur.front;
                t.taken = true;
            }
        }
        if (t.taken) {
            tableTree.removeKey(t);
            m += 1;
            s += g.value;
            reqnum ~= g.index;
            tabnum ~= t.index;
        }
    }

    writeln(m, " ", s);
    foreach (i; 0 .. reqnum.length) {
        writeln(reqnum[i], " ", tabnum[i]);
    }

    return 0;
}


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

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

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

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

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

int N;
string[][] A;
int[] P;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		A = new string[][](N, 2);
		foreach (i; 0 .. N) foreach (j; 0 .. 2) {
			A[i][j] = readToken;
		}
		P = new int[N];
		foreach (i; 0 .. N) {
			P[i] = readInt - 1;
		}

		bool[][] dp = new bool[][](N + 1, 2);
		dp[1][] = true;
		foreach (i; 1 .. N) {
			foreach (j; 0 .. 2) if (dp[i][j]) {
				foreach (k; 0 .. 2) {
					if (A[P[i - 1]][j] <= A[P[i]][k]) {
						dp[i + 1][k] = true;
					}
				}
			}
		}
		writeln((dp[N][0] || dp[N][1]) ? "YES" : "NO");
	}
	} catch (EOFException) {}
}

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

void main ()
{
	int n;
	while (readf (" %s ", &n) > 0)
	{
		string [2] [] a;
		foreach (i; 0..n)
		{
			auto s = readln ().split ();
			string u = s.front.dup;
			s.popFront ();
			string v = s.front.dup;
			a ~= [u, v];
		}
		debug {writeln (a);}

		auto p = new int [n];
		foreach (ref x; p)
		{
			readf (" %s", &x);
			x--;
		}

		auto f = new bool [2] [n];
		f[0][0] = f[0][1] = true;
		foreach (i; 1..n)
		{
			f[i][0] = (f[i - 1][0] && a[p[i - 1]][0] < a[p[i]][0]) ||
			          (f[i - 1][1] && a[p[i - 1]][1] < a[p[i]][0]);
			f[i][1] = (f[i - 1][0] && a[p[i - 1]][0] < a[p[i]][1]) ||
			          (f[i - 1][1] && a[p[i - 1]][1] < a[p[i]][1]);
		}
		writeln ((f[n - 1][0] || f[n - 1][1]) ? "YES" : "NO");
	}
}


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

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

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

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

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

class Tree {
  Tree l, r;
  int size;
  int val, ad;
  this(int val) {
    l = r = null;
    size = 1;
    this.val = val;
    this.ad = 0;
  }
  void propagate() {
    if (l) l.ad += ad;
    if (r) r.ad += ad;
    val += ad;
    ad = 0;
  }
  Tree change(Tree l, Tree r) {
    this.l = l;
    this.r = r;
    size = (l ? l.size : 0) + 1 + (r ? r.size : 0);
    return this;
  }
}
Tree merge(Tree a, Tree b) {
  if (!a) return b;
  if (!b) return a;
  a.propagate();
  b.propagate();
  if (xrand() % (a.size + b.size) < a.size) {
    return a.change(a.l, merge(a.r, b));
  } else {
    return b.change(merge(a, b.l), b.r);
  }
}
Tuple!(Tree, Tree) splitGE(Tree a, int key) {
  if (!a) return tuple!(Tree, Tree)(null, null);
  a.propagate();
  if (key <= a.val) {
    auto l = splitGE(a.l, key);
    return tuple(l[0], a.change(l[1], a.r));
  } else {
    auto r = splitGE(a.r, key);
    return tuple(a.change(a.l, r[0]), r[1]);
  }
}
Tuple!(Tree, Tree) splitHead(Tree a) {
  assert(a);
  a.propagate();
  if (a.l) {
    auto l = splitHead(a.l);
    return tuple(l[0], a.change(l[1], a.r));
  } else {
    return tuple(a, a.r);
  }
}
string toStr(Tree a) {
  if (!a) return "";
  return format("<%s (%s+%s) %s>", toStr(a.l), a.val, a.ad, toStr(a.r));
}
int[] toSeq(Tree a, int ad = 0) {
  if (!a) return [];
  ad += a.ad;
  return toSeq(a.l, ad) ~ (a.val + ad) ~ toSeq(a.r, ad);
}

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

      debug {
        // O~(\sum_i (R[i] - L[i] + 1))
        int[] lis;
        foreach (i; 0 .. N) {
          foreach_reverse (x; L[i] .. R[i] + 1) {
            const pos = lis.lowerBound(x);
            if (pos == lis.length) {
              lis ~= x;
            } else {
              lis[pos] = x;
            }
          }
          writeln(L[i], " ", R[i], " ", lis);
        }
        writeln("brute: ", lis.length);
      }

      Tree t = null;
      foreach (i; 0 .. N) {
        auto sp0 = t.splitGE(L[i]);
        auto sp1 = sp0[1].splitGE(R[i]);
        if (sp1[1]) {
          sp1[1] = sp1[1].splitHead[1];
        }
        if (sp1[0]) {
          sp1[0].ad += 1;
        }
        debug {
          writeln("  ", sp0[0].toSeq, " ", sp1[0].toSeq, " ", sp1[1].toSeq);
        }
        t = sp0[0].merge(new Tree(L[i])).merge(sp1[0]).merge(sp1[1]);
        debug {
          writeln(L[i], " ", R[i], " ", t.toSeq, " ", t.toStr);
        }
      }
      writeln(t.size);
    }
  } catch (EOFException e) {
  }
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.bitmanip,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,core.bitop,
	std.meta,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;const int mod=1_000_000_007;
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)(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)(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)(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)(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=' ') if(isInputRange!X && !isInputRange!(ElementEncodingType!X))
{foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a) if(isInputRange!X && isInputRange!(ElementEncodingType!X))
{
	foreach(ref const i;a)
	{
		foreach(ref const j;i)write(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--------------------------
Mt19937 gen;
class item
{
	int prior,key,add,cnt;
	item l,r;
	this()
	{
		l=r=null;
		prior=uniform!int();
		cnt=1;
	}
};
void push(item t)
{
	t.key+=t.add;
	if(t.l)t.l.add+=t.add;
	if(t.r)t.r.add+=t.add;
	t.add=0;
}
int cnt(item t){return t?t.cnt:0;}
void upd(item t)
{
	t.cnt=cnt(t.l)+cnt(t.r)+1;
}
void merge(ref item t,item l,item r)
{
	debug writeln(l !is null);
	debug writeln(r is null);
	debug stdout.flush;
	if(!l || !r){t=l?l:r;return;}
	//assert(false);
	if(l.prior>r.prior)
	{
		push(l);
		merge(l.r,l.r,r);
		t=l;
	}
	else
	{
		push(r);
		merge(r.l,l,r.l);
		t=r;
	}
	upd(t);
}
void split(item t,int val,ref item l,ref item r)
{
	if(!t){l=r=null;return;}
	push(t);
	if(t.key<val)
	{
		split(t.r,val,t.r,r);
		l=t;
	}
	else
	{
		split(t.l,val,l,t.l);
		r=t;
	}
	upd(t);
}
void cut(ref item t)
{
	push(t);
	if(!t.l)
	{
		t=t.r;
		return;
	}
	else cut(t.l);
	upd(t);
}
item root;
item l,r,m,x;
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;
	read(n);
	foreach(i;0..n)
	{
		int lb,rb;
		read(lb,rb);
		split(root,rb,m,r);
		split(m,lb,l,m);
		if(r)cut(r);
		x=new item;
		x.key=lb;
		if(m)m.add++;
		merge(l,l,x);
		merge(l,l,m);
		merge(root,l,r);
		x=l=r=m=null;
	}
	writeln(cnt(root));
/*loop:while(read(n))
	{

	}*/
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.string,std.range,std.complex,std.bitmanip,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,core.bitop,
	std.meta,core.stdc.stdio : freopen;
import std.numeric: fft,Fft,gcd,inverseFft;
alias big=BigInt;alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias lint=long;private const int mod=1_000_000_007;
alias pii=pair!(int,int);alias pll=pair!(long,long);alias mp=make_pair;alias gcd=std.numeric.gcd;
private
{
	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)(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)(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(g<a[m])r=m;
				else l=m;
			}
			return (g<a[l])?l:r;
		}
		size_t binf(T,X)(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)(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=' ') if(isInputRange!X && !isInputRange!(ElementEncodingType!X))
	{foreach(const ref i;a)write(i,ch);}
	void putarr(X)(in X a) if(isInputRange!X && isInputRange!(ElementEncodingType!X))
	{
		foreach(ref const i;a)
		{
			foreach(ref const j;i)write(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;}
	void enter(T...)(T args)
	{
		writeln(args);
		stdout.flush;
	}
}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------
private
{
	//Random gen;
	class item
	{
		int key,add,cnt;
		uint prior;
		item l,r;
		this()
		{
			l=r=null;
			prior=uniform!uint;
			cnt=1;
		}
	};
	void push(item t)
	{
		t.key+=t.add;
		if(t.l)t.l.add+=t.add;
		if(t.r)t.r.add+=t.add;
		t.add=0;
	}
	int cnt(item t){return t?t.cnt:0;}
	void upd(item t)
	{
		t.cnt=cnt(t.l)+cnt(t.r)+1;
	}
	void merge(ref item t,item l,item r)
	{
		debug writeln(l !is null);
		debug writeln(r is null);
		debug stdout.flush;
		if(!l || !r){t=l?l:r;return;}
		//assert(false);
		if(l.prior>r.prior)
		{
			push(l);
			merge(l.r,l.r,r);
			t=l;
		}
		else
		{
			push(r);
			merge(r.l,l,r.l);
			t=r;
		}
		upd(t);
	}
	void split(item t,int val,ref item l,ref item r)
	{
		if(!t){l=r=null;return;}
		push(t);
		if(t.key<val)
		{
			split(t.r,val,t.r,r);
			l=t;
		}
		else
		{
			split(t.l,val,l,t.l);
			r=t;
		}
		upd(t);
	}
	void cut(ref item t)
	{
		push(t);
		if(!t.l)
		{
			t=t.r;
			return;
		}
		else cut(t.l);
		upd(t);
	}
	item root;
	item l,r,m,x;
}
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;
	read(n);
	foreach(i;0..n)
	{
		int lb,rb;
		read(lb,rb);
		split(root,rb,m,r);
		split(m,lb,l,m);
		if(r)cut(r);
		x=new item;
		x.key=lb;
		if(m)m.add++;
		merge(l,l,x);
		merge(l,l,m);
		merge(root,l,r);
		x=l=r=m=null;
	}
	writeln(cnt(root));
/*loop:while(read(n))
	{

	}*/
}


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

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

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

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

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

int[][][] G;

int[] vis;
bool[][] viss;

bool dfsTop(int u) {
  vis[u] = 1;
  foreach (i; G[0][u]) {
    const v = A[i] ^ B[i] ^ u;
    if (vis[v] == 0) {
      if (!dfsTop(v)) {
        return false;
      }
    } else if (vis[v] == 1) {
      return false;
    }
  }
  vis[u] = 2;
  return true;
}

void dfs(int s, int u) {
  viss[s][u] = true;
  foreach (i; G[s][u]) {
    const v = A[i] ^ B[i] ^ u;
    if (!viss[s][v]) {
      dfs(s, v);
    }
  }
}

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

      G = new int[][][](2, N);
      foreach (i; 0 .. M) {
        G[0][A[i]] ~= i;
        G[1][B[i]] ~= i;
      }

      vis = new int[N];
      bool ok = true;
      foreach (u; 0 .. N) {
        if (vis[u] == 0) {
          if (!dfsTop(u)) {
            ok = false;
          }
        }
      }
      if (ok) {
        viss = new bool[][](2, N);
        int ansCnt;
        auto ans = new char[N];
        ans[] = 'E';
        foreach (u; 0 .. N) {
          if (!viss[0][u] && !viss[1][u]) {
            ++ansCnt;
            ans[u] = 'A';
          }
          foreach (s; 0 .. 2) {
            if (!viss[s][u]) {
              dfs(s, u);
            }
          }
        }
        writeln(ansCnt);
        writeln(ans);
      } else {
        writeln(-1);
      }
    }
  } catch (EOFException e) {
  }
}

import std.algorithm, std.stdio;
void main () {
	int n, m;
	readf !(" %s %s") (n, m);
	auto adj = new int [] [2] [n];
	foreach (j; 0..m) {
		int u, v;
		readf !(" %s %s") (u, v);
		u -= 1;
		v -= 1;
		adj[u][0] ~= v;
		adj[v][1] ~= u;
	}

	auto vis = new byte [2] [n], ok = true, res = "";
	void recur (int k, int v) {
		if (vis[v][k]) {
			ok &= vis[v][k] > 1;
			return;
		}
		vis[v][k] = 1;
		foreach (u; adj[v][k]) recur (k, u);
		vis[v][k] = 2;
	}
	foreach (i; 0..n) {
		res ~= "AE"[vis[i][].any];
		recur (0, i);
		recur (1, i);
	}

	if (ok) {
		writeln (res.count ('A'));
		writeln (res);
	}
	else writeln (-1);
}

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

void main ()
{
	int n, m;
	while (readf !(" %s %s") (n, m) > 0)
	{
		auto fwd = new int [] [n];
		auto rev = new int [] [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			fwd[u] ~= v;
			rev[v] ~= u;
		}

		auto fwdVis = new byte [n];
		auto revVis = new byte [n];
		bool ok = true;

		void recur (ref int [] [] adj, ref byte [] vis, int v)
		{
			if (vis[v] > 0)
			{
				if (vis[v] == 1)
				{
					ok = false;
				}
				return;
			}
			vis[v] = 1;
			foreach (u; adj[v])
			{
				recur (adj, vis, u);
			}
			vis[v] = 2;
		}

		string answer;
		foreach (i; 0..n)
		{
			if (!fwdVis[i] && !revVis[i])
			{
				answer ~= 'A';
			}
			else
			{
				answer ~= 'E';
			}
			recur (fwd, fwdVis, i);
			recur (rev, revVis, i);
		}

		if (ok)
		{
			writeln (answer.count ('A'));
			writeln (answer);
		}
		else
		{
			writeln (-1);
		}
	}
}

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

void main ()
{
	int n, m;
	while (readf !(" %s %s") (n, m) > 0)
	{
		auto fwd = new int [] [n];
		auto rev = new int [] [n];
		foreach (j; 0..m)
		{
			int u, v;
			readf !(" %s %s") (u, v);
			u -= 1;
			v -= 1;
			fwd[u] ~= v;
			rev[v] ~= u;
		}

		auto fwdVis = new byte [n];
		auto revVis = new byte [n];
		bool ok = true;

		void recur (ref int [] [] adj, ref byte [] vis, int v)
		{
			if (vis[v] > 0)
			{
				if (vis[v] == 1)
				{
					ok = false;
				}
				return;
			}
			vis[v] = 1;
			foreach (u; adj[v])
			{
				recur (adj, vis, u);
			}
			vis[v] = 2;
		}

		string answer;
		foreach (i; 0..n)
		{
			if (!fwdVis[i] && !revVis[i])
			{
				answer ~= 'A';
				recur (fwd, fwdVis, i);
				recur (rev, revVis, i);
			}
			else
			{
				answer ~= 'E';
			}
		}

		if (ok)
		{
			writeln (answer.count ('A'));
			writeln (answer);
		}
		else
		{
			writeln (-1);
		}
	}
}

import std.algorithm, std.stdio;
void main () {
	int n, m;
	readf !(" %s %s") (n, m);
	auto adj = new int [] [] [] (2, n);
	foreach (j; 0..m) {
		int u, v;
		readf !(" %s %s") (u, v);
		u -= 1;
		v -= 1;
		adj[0][u] ~= v;
		adj[1][v] ~= u;
	}

	auto vis = new byte [] [] (2, n), ok = true, res = "";
	void recur (int k, int v) {
		if (vis[k][v]) {
			ok &= vis[k][v] > 1;
			return;
		}
		vis[k][v] = 1;
		foreach (u; adj[k][v]) recur (k, u);
		vis[k][v] = 2;
	}
	foreach (i; 0..n) {
		res ~= "AE"[vis[i].any];
		recur (0, i);
		recur (1, i);
	}

	if (ok) {
		writeln (res.count ('A'));
		writeln (res);
	}
	else writeln (-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)); }

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

int[][][] G;

int[] vis;
bool[][] viss;

bool dfsTop(int u) {
  vis[u] = 1;
  foreach (i; G[0][u]) {
    const v = A[i] ^ B[i] ^ u;
    if (vis[v] == 0) {
      if (!dfsTop(v)) {
        return false;
      }
    } else if (vis[v] == 1) {
      return false;
    }
  }
  vis[u] = 2;
  return true;
}

void dfs(int s, int u) {
  viss[s][u] = true;
  foreach (i; G[s][u]) {
    const v = A[i] ^ B[i] ^ u;
    if (!viss[s][v]) {
      dfs(s, v);
    }
  }
}

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

      G = new int[][][](2, N);
      foreach (i; 0 .. M) {
        G[0][A[i]] ~= i;
        G[1][B[i]] ~= i;
      }

      vis = new int[N];
      bool ok = true;
      foreach (u; 0 .. N) {
        if (vis[u] == 0) {
          if (!dfsTop(u)) {
            ok = false;
          }
        }
      }
      if (ok) {
        viss = new bool[][](2, N);
        auto ans = new char[N];
        ans[] = 'E';
        foreach (u; 0 .. N) {
          if (!viss[0][u] && !viss[1][u]) {
            ans[u] = 'A';
          }
          foreach (s; 0 .. 2) {
            if (!viss[s][u]) {
              dfs(s, u);
            }
          }
        }
        writeln(ans);
      } else {
        writeln(-1);
      }
    }
  } catch (EOFException e) {
  }
}


import core.stdc.stdio;
import std.algorithm;
char[505] s;
int[505] c,q;
int[505][505] dp;
int n,m,k;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for (int i=1;i<=n;i++)
	{
		scanf("%s",&s);
		int len=0;
		for (int j=0;j<m;j++) if (s[j]=='1') c[++len]=j;
		int l;
		for (l=1;l<=len;l++)
		{
			q[l]=m;
			for (int j=1;j<=len-l+1;j++) q[l]=min(q[l],c[j+l-1]-c[j]+1);
		}
		for (int j=0;j<=k;j++) for (l=min(len,j),dp[i][j]=i*m;l>=0;l--) dp[i][j]=min(dp[i][j],dp[i-1][j-l]+q[len-l]);
	}
	printf("%d\n",dp[n][k]);
	return 0;
}

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

  int n, m, k; rd(n, m, k);
  auto a=new char[][](n, m);
  foreach(i; 0..n) a[i]=readln.chomp.to!(char[]);

  const int inf=1e9.to!(int);
  auto best=new int[][](n, k+1);
  foreach(i; 0..n){
    fill(best[i], inf);
    auto pos=new int[0];
    foreach(int j; 0..m)if(a[i][j]=='1') pos~=j;
    auto t=pos.length;
    if(t==0){fill(best[i], 0); continue;}
    foreach(l; 0..t)foreach(r; l..t)if(t-(r-l+1)<=k)
      chmin(best[i][t-(r-l+1)], pos[r]-pos[l]+1);
    if(t<=k) best[i][t]=0;
  }
  auto dp=new int[](k+1);
  fill(dp, inf);
  dp[k]=0;
  foreach(i; 0..n){
    auto nex=new int[](k+1);
    fill(nex, inf);
    for(int j=0; j<=k; j++)for(int l=0; l<=j; l++)
      chmin(nex[j-l], dp[j]+best[i][l]);
    dp.swap(nex);
  }
  writeln(reduce!(min)(dp));
}

void chmin(T)(ref T l, T r){if(l>r) l=r;}

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.string, std.conv;
import std.range, std.algorithm, std.array;

enum inf = 2*10^^9;

void main() {
    int n, m, k;
    scan(n, m, k);

    auto dd = new int[][](n, 0);
    foreach (i ; 0 .. n) {
        auto s = readln.chomp;
        foreach (j, ch; s) {
            if (ch == '1') {
                dd[i] ~= j;
            }
        }
    }

    debug {
        writeln(dd);
    }

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

    foreach (i ; 0 .. n) {
        foreach (j ; 0 .. k + 1) {
            if (j >= dd[i].length) {
                ks[i][j] = 0;
            }
            else {
                ks[i][j] = inf;

                int sp = dd[i].length.to!int - j;

                foreach (l ; 0 .. j + 1) {
                    ks[i][j] = min(ks[i][j], dd[i][l + sp - 1] - dd[i][l] + 1);
                }
            }
        }
    }

    debug {
        writeln(ks);
    }

    auto dp = new int[][](n + 1, k + 1);
    fillAll(dp, inf);
    dp[0][0] = 0;

    foreach (i ; 1 .. n + 1) {
        foreach (j ; 0 .. k + 1) {
            foreach (l ; 0 .. j + 1) {
                dp[i][j] = min(dp[i][j], dp[i-1][l] + ks[i-1][j-l]);
            }
        }
    }

    int ans = dp[n][k];
    writeln(ans);
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

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

enum inf = 10000;

void main() {
    int n, m, k;
    scan(n, m, k);

    auto dd = new int[][](n, 0);
    foreach (i ; 0 .. n) {
        auto s = readln.chomp;
        foreach (j, ch; s) {
            if (ch == '1') {
                dd[i] ~= j;
            }
        }
    }

    debug {
        writeln(dd);
    }

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

    foreach (i ; 0 .. n) {
        foreach (j ; 0 .. k + 1) {
            if (j >= dd[i].length) {
                ks[i][j] = 0;
            }
            else {
                ks[i][j] = inf;

                int sp = dd[i].length.to!int - j;

                foreach (l ; 0 .. j + 1) {
                    ks[i][j] = min(ks[i][j], dd[i][l + sp - 1] - dd[i][l] + 1);
                }
            }
        }
    }

    debug {
        writeln(ks);
    }

    auto dp = new int[][](n + 1, k + 1);
    fillAll(dp, inf);
    dp[0][0] = 0;

    foreach (i ; 1 .. n + 1) {
        foreach (j ; 0 .. k + 1) {
            foreach (l ; 0 .. j + 1) {
                dp[i][j] = min(dp[i][j], dp[i-1][l] + ks[i-1][j-l]);
            }
        }
    }

    int ans = dp[n][k];
    writeln(ans);
}

void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

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

  int n, m, k; rd(n, m, k);
  auto a=new char[][](n, m);
  foreach(i; 0..n) a[i]=readln.chomp.to!(char[]);

  const int inf=1e9.to!(int);
  auto best=new int[][](n, k+1);
  foreach(i; 0..n){
    fill(best[i], inf);
    auto pos=new int[0];
    foreach(int j; 0..m)if(a[i][j]=='1') pos~=j;
    auto t=pos.length;
    foreach(l; 0..t)foreach(r; l..t)if(t-(r-l+1)<=k)
      chmin(best[i][t-(r-l+1)], pos[r]-pos[l]+1);
  }
  auto dp=new int[](k+1);
  fill(dp, inf);
  dp[k]=0;
  foreach(i; 0..n){
    auto nex=new int[](k+1);
    fill(nex, inf);
    for(int j=0; j<=k; j++)for(int l=0; l<=j; l++)
      chmin(nex[j-l], dp[j]+best[i][l]);
    dp.swap(nex);
  }
  writeln(reduce!(min)(dp));
}

void chmin(T)(ref T l, T r){if(l>r) l=r;}

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

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

  int n, m, k; rd(n, m, k);
  auto a=new char[][](n, m);
  foreach(i; 0..n) a[i]=readln.chomp.to!(char[]);

  const int inf=1e9.to!(int);
  auto best=new int[][](n, k+1);
  foreach(i; 0..n){
    fill(best[i], inf);
    auto pos=new int[0];
    foreach(int j; 0..m)if(a[i][j]=='1') pos~=j;
    auto t=pos.length;
    if(t==0){fill(best[i], 0); continue;}
    foreach(l; 0..t)foreach(r; l..t)if(t-(r-l+1)<=k)
      chmin(best[i][t-(r-l+1)], pos[r]-pos[l]+1);
  }
  auto dp=new int[](k+1);
  fill(dp, inf);
  dp[k]=0;
  foreach(i; 0..n){
    auto nex=new int[](k+1);
    fill(nex, inf);
    for(int j=0; j<=k; j++)for(int l=0; l<=j; l++)
      chmin(nex[j-l], dp[j]+best[i][l]);
    dp.swap(nex);
  }
  writeln(reduce!(min)(dp));
}

void chmin(T)(ref T l, T r){if(l>r) l=r;}

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

enum INF = 10^^9;

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      const K = readInt();
      auto yoko = new int[][](M, N - 1);
      foreach (x; 0 .. M) foreach (y; 0 .. N - 1) {
        yoko[x][y] = readInt();
      }
      auto tate = new int[][](M - 1, N);
      foreach (x; 0 .. M - 1) foreach (y; 0 .. N) {
        tate[x][y] = readInt();
      }

      auto ans = new int[][](M, N);
      if (K % 2 == 0) {
        auto dp = new int[][][](K / 2 + 1, M, N);
        foreach (k; 1 .. K / 2 + 1) {
          foreach (x; 0 .. M) foreach (y; 0 .. N) {
            dp[k][x][y] = INF;
          }
        }
        foreach (k; 0 .. K / 2) {
          foreach (x; 0 .. M) foreach (y; 0 .. N - 1) {
            chmin(dp[k + 1][x][y + 1], dp[k][x][y] + yoko[x][y]);
            chmin(dp[k + 1][x][y], dp[k][x][y + 1] + yoko[x][y]);
          }
          foreach (x; 0 .. M - 1) foreach (y; 0 .. N) {
            chmin(dp[k + 1][x + 1][y], dp[k][x][y] + tate[x][y]);
            chmin(dp[k + 1][x][y], dp[k][x + 1][y] + tate[x][y]);
          }
        }
        foreach (x; 0 .. M) foreach (y; 0 .. N) {
          ans[x][y] = 2 * dp[K / 2][x][y];
        }
      } else {
        foreach (x; 0 .. M) foreach (y; 0 .. N) {
          ans[x][y] = -1;
        }
      }

      foreach (x; 0 .. M) {
        foreach (y; 0 .. N) {
          if (y > 0) write(" ");
          write(ans[x][y]);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable int infinity = int.max / 45;

void main ()
{
	int n, m, k;
	while (readf !(" %s %s %s") (n, m, k) > 0)
	{
		auto a = new int [] [] (n * 2 - 1, m * 2 - 1);
		foreach (ref line; a)
		{
			line[] = infinity;
		}
		foreach (i; 0..n)
		{
			foreach (j; 0..m - 1)
			{
				readf !(" %s") (a[i * 2][j * 2 + 1]);
			}
		}
		foreach (i; 0..n - 1)
		{
			foreach (j; 0..m)
			{
				readf !(" %s") (a[i * 2 + 1][j * 2]);
			}
		}
		auto answer = new int [] [] [] (2, n, m);
		int z = 0;

		if (!(k & 1))
		{
			foreach (i; 0..n)
			{
				foreach (j; 0..m)
				{
					answer[z][i][j] = 0;
				}
			}

			auto d = k / 2;
			for (int step = 0; step < d; step++)
			{
				z ^= 1;
				foreach (i; 0..n)
				{
					foreach (j; 0..m)
					{
						answer[z][i][j] = infinity;
					}
				}
				foreach (i; 0..n)
				{
					foreach (j; 0..m)
					{
						if (i - 1 >= 0)
						{
							answer[z][i][j] = min
							    (answer[z][i][j],
							    answer[!z][i - 1][j] +
							    a[i * 2 - 1][j * 2]);
						}
						if (i + 1 < n)
						{
							answer[z][i][j] = min
							    (answer[z][i][j],
							    answer[!z][i + 1][j] +
							    a[i * 2 + 1][j * 2]);
						}
						if (j - 1 >= 0)
						{
							answer[z][i][j] = min
							    (answer[z][i][j],
							    answer[!z][i][j - 1] +
							    a[i * 2][j * 2 - 1]);
						}
						if (j + 1 < m)
						{
							answer[z][i][j] = min
							    (answer[z][i][j],
							    answer[!z][i][j + 1] +
							    a[i * 2][j * 2 + 1]);
						}
					}
				}
			}
		}

		foreach (i; 0..n)
		{
			foreach (j; 0..m)
			{
				if (answer[z][i][j] == 0)
				{
					answer[z][i][j] = -1;
				}
				else
				{
					answer[z][i][j] *= 2;
				}
			}
		}
		writefln !("%(%(%s %)\n%)") (answer[z]);
	}
}