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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto s = readln.strip.map !(x => x - '0').array;
		auto f = 0 ~ readln.splitter.map !(to !(int)).array;
		int pos = 0;
		while (pos < n && f[s[pos]] <= s[pos])
		{
			pos += 1;
		}
		while (pos < n && f[s[pos]] >= s[pos])
		{
			s[pos] = f[s[pos]];
			pos += 1;
		}
		writefln ("%(%s%)", s);
	}
}

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

void solve(){

	int n = read.to!int;
	char[] a = readln.chomp.to!(char[]);

	char[char] y;
	foreach(c; "123456789") y[c] = '0', y[c] += read.to!int.to!char;

	int f = 0;
	foreach(i, c; a){
		if(f == 0){
			if(y[c] <= c) continue;
			else{
				f = 1;
				a[i] = y[c];
			}
		}
		else if(f == 1){
			if(y[c] < c){
				f = 2;
				break;
			}
			else{
				a[i] = y[c];
			}
		}
	}

	a.to!string.writeln;

}

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

string FMT_F = "%.10f";

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

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

void main()
{
	auto N = RD;
	auto s = RD!string;
	auto a = RDR.ARR;

	auto use = new bool[](9);
	foreach (i; 0..9)
	{
		if (a[i] > i+1) use[i] = true;
	}
	debug writeln(use);

	string ans;
	long mode;
	foreach (c; s)
	{
		uint x = c - '0';
		if (mode == 0 && a[x-1] > x)
		{
			ans ~= to!string(a[x-1]);
			mode = 1;
		}
		else if (mode == 1 && a[x-1] >= x)
		{
			ans ~= to!string(a[x-1]);
		}
		else
		{
			ans ~= c;
			if (mode == 1)
				mode = 2;
		}
	}

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

// https://codeforces.com/problemset/problem/1157/B
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

    string a = readln.chomp;

    int[] f = [-1];
    f ~= readln.split.map!(to!int).array;

    int idx = 0;
    while(idx < n && f[a[idx]-'0'] <= a[idx]-'0') {
        a[idx].write;
        idx += 1;
    }

    while(idx < n && f[a[idx]-'0'] >= a[idx]-'0') {
        f[a[idx]-'0'].write;
        idx += 1;
    }

    while(idx < n) {
        a[idx].write;
        idx += 1;
    }
    writeln;
}

// https://codeforces.com/problemset/problem/1157/B
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

    string a = readln.chomp;

    int[] f = [-1];
    f ~= readln.split.map!(to!int).array;

    bool change = true;
    foreach(ch; a) {
        int digit = ch-'0';
        if(f[digit] < digit)
            change = false;
        if(f[digit] > digit && change)
            f[digit].write;
        else
            digit.write;
    } writeln;
}

// https://codeforces.com/problemset/problem/1157/B
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

    string a = readln.chomp;

    int[] f = [-1];
    f ~= readln.split.map!(to!int).array;

    foreach(ch; a) {
        int digit = ch-'0';
        if(f[digit] > digit)
            f[digit].write;
        else
            digit.write;
    } writeln;
}

// https://codeforces.com/problemset/problem/1157/B
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

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

    string a = readln.chomp;

    int[] f = [-1];
    f ~= readln.split.map!(to!int).array;

    int idx = 0;
    while(idx < n && f[idx] <= a[idx]) {
        a[idx].write;
        idx += 1;
    }

    while(idx < n && f[idx] >= a[idx]) {
        f[idx].write;
        idx += 1;
    }

    while(idx < n) {
        a[idx].write;
        idx += 1;
    }
    writeln;
}

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

string FMT_F = "%.10f";

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

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

void main()
{
	auto N = RD;
	auto s = RD!string;
	auto a = RDR.ARR;

	auto use = new bool[](9);
	foreach (i; 0..9)
	{
		if (a[i] > i) use[i] = true;
	}

	string ans;
	foreach (i, c; s)
	{
		uint x = c - '0';
		if (use[x-1])
			ans ~= to!string(a[x-1]);
		else
			ans ~= c;
	}

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

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

string FMT_F = "%.10f";

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

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

void main()
{
	auto N = RD;
	auto s = RD!string;
	auto a = RDR.ARR;

	auto use = new bool[](9);
	foreach (i; 0..9)
	{
		if (a[i] > i+1) use[i] = true;
	}
	debug writeln(use);

	string ans;
	long mode;
	foreach (c; s)
	{
		uint x = c - '0';
		if (mode == 0 && a[x-1] > x)
		{
			ans ~= to!string(a[x-1]);
			mode = 1;
		}
		if (mode == 1 && a[x-1] >= x)
		{
			ans ~= to!string(a[x-1]);
		}
		else
		{
			ans ~= c;
			if (mode == 1)
				mode = 2;
		}
	}

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

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

string FMT_F = "%.10f";

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

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

void main()
{
	auto N = RD;
	auto s = RD!string;
	auto a = RDR.ARR;

	auto use = new bool[](9);
	foreach (i; 0..9)
	{
		if (a[i] > i) use[i] = true;
	}

	long ans;
	foreach (i, c; s)
	{
		long d = s.length - i - 1;
		uint x = c - '0';
		if (use[x-1])
			ans += a[x-1] * (10^^d);
		else
			ans += x * (10^^d);
	}

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

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

string FMT_F = "%.10f";

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

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
bool minimize(T)(ref T x, T y) { if (x > y) { x = y; return true; } else { return false; } }
bool maximize(T)(ref T x, T y) { if (x < y) { x = y; return true; } else { return false; } }

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

void main()
{
	auto N = RD;
	auto s = RD!string;
	auto a = RDR.ARR;

	auto use = new bool[](9);
	foreach (i; 0..9)
	{
		if (a[i] > i+1) use[i] = true;
	}
	debug writeln(use);

	string ans;
	long mode;
	foreach (c; s)
	{
		uint x = c - '0';
		if ((mode == 0 || mode == 1) && use[x-1])
		{
			ans ~= to!string(a[x-1]);
			mode = 1;
		}
		else
		{
			ans ~= c;
			if (mode == 1)
				mode = 2;
		}
	}

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


//Да, я упоролся решать эту задачу Splay-деревом)
import core.bitop;
import core.checkedint;
import core.simd;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.bitmanip;
import std.complex;
import std.container;
import std.conv;
import std.datetime;
import std.format;
import std.functional;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.regex;
import std.stdio;
import std.string;
import std.typecons;
import std.variant;

__gshared:

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

auto getAddrTuple() {
    return tuple();
}

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

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

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

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

struct Node {
    int value, firstValue, lastValue;
    bool sorted = true;
    Splay left, right, parent;

    invariant {
        assert(firstValue == (left !is null ? left.firstValue : value));
        assert(lastValue == (right !is null ? right.lastValue : value));
        assert(sorted == (
            left.sorted && right.sorted &&
            value >= (left !is null ? left.lastValue : value) &&
            value <= (right !is null ? right.firstValue : value)
        ));
    }

    enum { L, R }

    this(int x) {
        value = firstValue = lastValue = x;
    }

    Splay splayLeft() return
    out (result) {
        assert(result.parent is null);
        assert(result.left is null);
    }
    body {
        if (left !is null)
            return left.splayLeft();
        splay();
        return Splay(&this);
    }

    Splay splayRight() return
    out (result) {
        assert(result.parent is null);
        assert(result.right is null);
    }
    body {
        if (right !is null)
            return right.splayRight();
        splay();
        return Splay(&this);
    }

    void splay()
    out {
        assert(parent is null);
    }
    body {
        while (true) {
            auto parent = parent;
            if (parent is null)
                return;
            if (parent.parent is null) {
                rotate();
                return;
            }
            if (side == parent.side)
                parent.rotate();
            else
                rotate();
            rotate();
        }
    }

    void rotate()
    in {
        assert(parent !is null);
    }
    body {
        auto parent = parent;
        auto gparent = parent.parent;
        auto side = side;
        auto parentSide = gparent !is null ? parent.side : 0;
        unlink();
        parent.unlink();
        if (side == L) {
            if (auto right = right) {
                right.unlink();
                right.link(L, parent);
            }
            parent.link(R, Splay(&this));
        } else {
            if (auto left = left) {
                left.unlink();
                left.link(R, parent);
            }
            parent.link(L, Splay(&this));
        }
        link(parentSide, gparent);
    }

private:
    void recalc() {
        firstValue = left !is null ? left.firstValue : value;
        lastValue = right !is null ? right.lastValue : value;
        sorted = left.sorted && right.sorted;
        if (left !is null)
            sorted &= value >= left.lastValue;
        if (right !is null)
            sorted &= value <= right.firstValue;
    }

    @property int side() const
    in {
        assert(parent !is null);
    }
    body {
        return &this is parent.left ? L : R;
    }

    void link(int c, Splay newParent)
    in {
        assert(parent is null);
        if (newParent !is null)
            assert((c == L ? newParent.left : newParent.right) is null);
    }
    out {
        assert(parent is newParent);
    }
    body {
        if (!newParent)
            return;
        parent = newParent;
        if (c == L)
            newParent.left = &this;
        else
            newParent.right = &this;
        newParent.recalc();
    }

    void unlink()
    out {
        assert(parent is null);
    }
    body {
        if (parent is null)
            return;
        if (side == L)
            parent.left = null;
        else
            parent.right = null;
        parent.recalc();
        parent = null;
    }
}

struct Splay {
    Node* _node;

    alias _node this;

    @property int firstValue() const { return _node !is null ? _node.firstValue : int.min; }
    @property int lastValue() const { return _node !is null ? _node.lastValue : int.max; }
    @property bool sorted() const { return _node !is null ? _node.sorted : true; }

    void append(Splay t) {
        if (_node)
            link(L, t);
        this = t;
    }

    void shift() {
        auto item = splayRight();
        this = item.left;
        unlink();
        this = splayLeft();
        link(R, item);
        this = item;
    }
}

Node[10^^5] _nodes;

void main() {
    int n;
nextCase:
    while (scanf("%d", &n) == 1) {
        version (LocalProject)
            _nodes[ ] = Node.init;
        Splay t;
        foreach (ref node; _nodes[0 .. n]) {
            int x;
            scanf("%d", &x);
            node = Node(x);
            t.append(Splay(&node));
        }

        foreach (i; 0 .. n) {
            if (t.sorted) {
                printf("%d\n", i);
                continue nextCase;
            }
            t.shift();
        }
        puts("-1");
    }
}

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

//	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, const T f) { if (t > f) t = f; }
void chmax(T)(ref T t, const T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	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)(S x, T y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(T[] as, 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)(T[] as, T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(T[] as, T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int N;
int[] A;

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

		int[] pos;
		foreach (i; 0 .. N) {
			if (A[i] > A[(i + 1) % N]) {
				pos ~= i;
			}
		}

		if (pos.length == 0) {
			writeln(0);
		} else if (pos.length == 1) {
			writeln(N - 1 - pos[0]);
		} else {
			writeln(-1);
		}
	}
	} catch (EOFException) {}
}


import std.stdio,std.typecons;
int n,t;
bool[int] s;
auto ans=[tuple(1,0)];
void main()
{
	scanf(" %d",&n);
	foreach(i;0..n)
	{
		scanf(" %d",&t);
		if(t in s)
		{
			ans[$-1][1]=i+1;
			ans~=tuple(i+2,0);
			s=s.init;
		}else{
			s[t]=true;
		}
	}
	if(ans.length==1)
	{
		writeln(-1);
		return;
	}
	writeln(--ans.length);
	ans[$-1][1]=n;
	foreach(i;ans) writeln(i[0],' ',i[1]);
	s=s.init;
}

import std.stdio,std.typecons;
int n,t;
bool[int] s;
auto ans=[tuple(1,0)];
void main()
{
	scanf(" %d",&n);
	foreach(i;0..n)
	{
		scanf(" %d",&t);
		if(t in s)
		{
			ans[$-1][1]=i+1;
			ans~=tuple(i+2,0);
			s=s.init;
		}else{
			s[t]=true;
		}
	}
	if(ans.length==1)
	{
		writeln(-1);
		return;
	}
	writeln(--ans.length);
	ans[$-1][1]=n;
	foreach(i;ans) writeln(i[0],' ',i[1]);
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		auto h = (n+1)/2;
		auto w = (m+1)/2;
		foreach (i; 0..h)
		{
			foreach (j; 0..w)
			{
				long[] arr;
				auto k = n-i-1;
				auto l = m-j-1;
				arr ~= a[i][j];
				if (i != k)
					arr ~= a[k][j];
				if (j != l)
					arr ~= a[i][l];
				if (i != k && j != l)
					arr ~= a[k][l];
				if (arr.length == 1) continue;
				arr.sort();
				auto x = arr[1];
				foreach (e; arr)
				{
					ans[ti] += abs(e - x);
				}
			}
		}
	}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		auto a = new long [] [] (n, m);
		foreach (i; 0..n)
		{
			foreach (j; 0..m)
			{
				readf !(" %s") (a[i][j]);
			}
		}
		long res = 0;
		foreach (i; 0..(n + 1) / 2)
		{
			foreach (j; 0..(m + 1) / 2)
			{
				int [] [] c;
				c ~= [i, j];
				c ~= [n - i - 1, j];
				c ~= [i, m - j - 1];
				c ~= [n - i - 1, m - j - 1];
				sort (c);
				c = c.uniq.array;
				auto f = c.map !(d => a[d[0]][d[1]]).array;
				sort (f);
				auto mid = f[f.length / 2];
				res += f.map !(x => abs (x - mid)).sum;
			}
		}
		writeln (res);
	}
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;
import std.math;

void main(string[] args)
{
  long[100][100] mat;
  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto n = next!int;
      auto m = next!int;
      foreach(i; 0 .. n)
	foreach(j; 0 .. m)
	  mat[i][j] = next!int;
      long cost = 0;
      foreach(i; 0 .. n)
	{
	  foreach(j; 0 .. m)
	    {
	      int si = n - 1 - i;
	      int sj = m - 1 - j;
	      long localcost = 0;
	      if (si == i)
		{
		  if (sj == j)
		    {
		      localcost = 0;
		    }
		  else
		    {
		      localcost = abs(mat[i][j] - mat[i][sj]);
		      mat[i][j] = mat[i][sj];
		    }
		}
	      else
		{
		  if (sj == j)
		    {
		      localcost = abs(mat[i][j] - mat[si][j]);
		      mat[i][j] = mat[si][j];
		    }
		  else
		    {
		      localcost = int.max;
		      long[] vals = [mat[i][j], mat[i][sj], mat[si][j], mat[si][sj]];
		      auto svals = sort(vals);
		      long usedval = vals[1];
		      localcost = abs(mat[i][j] - usedval) + abs(mat[i][sj] - usedval)
			+ abs(mat[si][j] - usedval) + abs(mat[si][sj] - usedval);
		      mat[i][j] = usedval;
		      mat[i][sj] = usedval;
		      mat[si][j] = usedval;
		      mat[si][sj] = usedval;
		    }
		}
	      cost += localcost;
	    }
	}
      cost.writeln;
    }
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

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

void play(){
    int n, m;
    n = rd!int;
    m = rd!int;
    ll[][] mat;
    foreach(i; 0..n){
        mat ~= rdarr;
    }
    ll oper = 0;
    foreach(i; 0..(n/2 + (n % 2 != 0))){
        foreach(j; 0..(m/2 + (m % 2 != 0))){
            ll[] vals;
            vals ~= mat[i][j];
            vals ~= mat[n - i - 1][j];
            vals ~= mat[i][m - j - 1];
            vals ~= mat[n - i - 1][m - j - 1];
            ll minval = to!ll(1e12);
            foreach(v; vals){
                ll cursum = 0;
                foreach(u; vals){
                    cursum += abs(u - v);
                }
                minval = min(cursum, minval);
            }
            if(i == n/2){
                minval /= 2L;
            }
            if(j == m/2){
                minval /= 2;
            }
            oper += minval;
        }
    }
    writeln(oper);
}

int main(){
    long t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
alias ll = long;
alias rbt = RedBlackTree;
alias sr = SortedRange;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }

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

void play(){
    ll a = rd;
    ll b = rd;
    ll c = rd;
    writeln(a + b + c - 1);
}

int main(){
    long t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

/**********It's A Me Mario!**********/
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;
alias ll = long;
alias rbt = RedBlackTree;
alias sr = SortedRange;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		auto a = new int [] [] (n, m);
		foreach (i; 0..n)
		{
			foreach (j; 0..m)
			{
				readf !(" %s") (a[i][j]);
			}
		}
		int res = 0;
		foreach (i; 0..(n + 1) / 2)
		{
			foreach (j; 0..(m + 1) / 2)
			{
				int [] [] c;
				c ~= [i, j];
				c ~= [n - i - 1, j];
				c ~= [i, m - j - 1];
				c ~= [n - i - 1, m - j - 1];
				sort (c);
				c = c.uniq.array;
				auto f = c.map !(d => a[d[0]][d[1]]).array;
				sort (f);
				auto mid = f[f.length / 2];
				res += f.map !(x => abs (x - mid)).sum;
			}
		}
		writeln (res);
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto h = (n+1)/2;
		auto w = (m+1)/2;
		foreach (i; 0..h)
		{
			foreach (j; 0..w)
			{
				long[] arr;
				auto k = n-i-1;
				auto l = m-j-1;
				arr ~= a[i][j];
				if (i != k)
					arr ~= a[k][j];
				if (j != l)
					arr ~= a[i][l];
				arr ~= a[k][l];
				arr.sort();
				auto x = arr[1];
				foreach (e; arr)
				{
					ans[ti] += abs(e - x);
				}
			}
		}
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto h = (n+1)/2;
		auto w = (m+1)/2;
		foreach (i; 0..h)
		{
			foreach (j; 0..w)
			{
				long[] arr;
				auto k = n-i-1;
				auto l = m-j-1;
				arr ~= a[i][j];
				if (i != k)
					arr ~= a[k][j];
				if (j != l)
					arr ~= a[i][l];
				if (i != k && j != l)
					arr ~= a[k][l];
				if (arr.length == 1) continue;
				arr.sort();
				auto x = arr[1];
				foreach (e; arr)
				{
					ans[ti] += abs(e - x);
				}
			}
		}
	}

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


module main;

import std.stdio, std.string;

auto init(int n)
{
    int[] x;
    int prev = 0;
    foreach (int i; 0 .. n)
    {
        prev ^= i;
        x ~= prev;
        //printf("%d ", prev);
    }
    //printf("\n");
    return x;
}

void solve(int[] p)
{
    auto x = init(p.length);
    int ans = 0;
    int n = p.length;
    foreach (int i; 1 .. n + 1)
    {
        ans ^= x[i - 1];
        int cnt = (n - (i - 1)) / i;
        int left = (n - (i - 1)) % i;
        //printf("%d %d\n", cnt, left);
        if (cnt & 1)
        {
            ans ^= x[i - 1];
        }
        if (left != 0)
        {
            ans ^= x[left - 1];
        }
        ans ^= p[i - 1];
    }
    writefln("%d", ans);
    //printf("%d\n", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        int[] p = new int[n];
        foreach (int i; 0 .. n)
        {
            scanf("%d", &p[i]);
        }
        solve(p);
    }
    return 0;
}

module main;

import std.stdio, std.string;

auto init(int n)
{
    int[] x = new int[n];
    int prev = 0;
    foreach (int i; 0 .. n)
    {
        prev ^= i;

        x[i] = prev;
        //x ~= prev;
        //printf("%d ", prev);
    }
    //printf("\n");
    return x;
}

void solve(int[] p)
{
    auto x = init(p.length);
    int ans = 0;
    int n = p.length;
    foreach (int i; 1 .. n + 1)
    {
        ans ^= x[i - 1];
        int cnt = (n - (i - 1)) / i;
        int left = (n - (i - 1)) % i;
        //printf("%d %d\n", cnt, left);
        if (cnt & 1)
        {
            ans ^= x[i - 1];
        }
        if (left != 0)
        {
            ans ^= x[left - 1];
        }
        ans ^= p[i - 1];
    }
    writefln("%d", ans);
    //printf("%d\n", ans);
}

int main(string[] args)
{
    int n;
    while (scanf("%d", &n) == 1)
    {
        int[] p = new int[n];
        foreach (int i; 0 .. n)
        {
            scanf("%d", &p[i]);
        }
        solve(p);
    }
    return 0;
}


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;
enum W = 1;

alias Result = Tuple!(int, "cost", int, "a0", int, "b0");
Result[long] cache;
Result calc(int a, int b) {
  const key = cast(long)(a) << 20 | b;
  auto ptr = key in cache;
  if (ptr) return *ptr;
  Result ret = Result(INF, -1, -1);

  if (a + b <= 2) {
    ret.cost = 0;
  } else if (b == 0) {
    // ask a0
    foreach (a0; (a / 2 - W) .. (a / 2 + W) + 1) if (0 < a0 && a0 < a) {
      const a1 = a - a0;
      if (chmin(ret.cost, 1 + max(calc(a0, a1).cost, calc(a1, a0).cost))) {
        ret.a0 = a0;
      }
    }
  } else if (b == 1) {
    // ask b
    ret.cost = 1 + max(calc(b, a).cost, calc(a, 0).cost);
  } else {
    // ask a0, b0
    foreach (a0; (a / 2 - W) .. (a / 2 + W) + 1) if (0 <= a0 && a0 <= a) {
      foreach (b0; (b / 2 - W) .. (b / 2 + W) + 1) if (0 < b0 && b0 < b) {
        const a1 = a - a0;
        const b1 = b - b0;
        if (chmin(ret.cost, 1 + max(calc(a0 + b0, a1).cost, calc(a1 + b1, a0).cost))) {
          ret.a0 = a0;
          ret.b0 = b0;
        }
      }
    }
  }
  debug {
    if (a + b <= 5) {
      writeln(a, " ", b, ": ", ret);
    }
  }

  return cache[key] = ret;
}

bool Ask(const(int[]) xs) {
  writef("? %s", xs.length);
  foreach (x; xs) {
    write(" ", x);
  }
  writeln;
  stdout.flush;
  const res = readToken;
  return (res == "YES");
}
void Answer(const(int[]) xs) {
  foreach (x; xs) {
    writefln("! %s", x);
    stdout.flush;
    const res = readToken;
    if (res == ":)") {
      return;
    }
  }
  assert(false);
}

void main() {
  debug {
    foreach (e; 1 .. 5 + 1) {
      const n = 10^^e;
      const res = calc(n, 0);
      writefln("n = %s: |cache| = %s; %s", n, cache.length, res);
      stdout.flush;
    }
  }

  const N = readInt;
  int[] xs = iota(1, N + 1).array, ys;
  for (; ; ) {
    debug {
      writeln(xs, " ", ys);
    }
    const a = cast(int)(xs.length);
    const b = cast(int)(ys.length);
    const res = calc(a, b);
    if (a + b <= 2) {
      break;
    } else if (b == 0) {
      int[] xs0 = xs[0 .. res.a0], xs1 = xs[res.a0 .. a];
      if (Ask(xs0)) {
        xs = xs0;
        ys = xs1;
      } else {
        xs = xs1;
        ys = xs0;
      }
    } else if (b == 1) {
      if (Ask(ys)) {
        swap(xs, ys);
      } else {
        ys = [];
      }
    } else {
      int[] xs0 = xs[0 .. res.a0], xs1 = xs[res.a0 .. a];
      int[] ys0 = ys[0 .. res.b0], ys1 = ys[res.b0 .. b];
      if (Ask(xs0 ~ ys0)) {
        xs = xs0 ~ ys0;
        ys = xs1;
      } else {
        xs = xs1 ~ ys1;
        ys = xs0;
      }
    }
  }
  Answer(xs ~ ys);
}


import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
	int n = read.to!int;

	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i);

	foreach(i; 0 .. n - 1){
		int u = read.to!int - 1;
		int v = read.to!int - 1;
		nodes[u].nodes ~= nodes[v];
		nodes[v].nodes ~= nodes[u];
	}

	int f = 0;
	foreach(nd; nodes){
		if(nd.nodes.length == 2) f = 1;
	}

	if(f == 1) "NO".writeln;
	else "YES".writeln;

}
/*
A1: YES if and only if there are no vertex with degree 2.

*/

class Node{
	int id;
	Node[] nodes;
	this(int id){
		this.id = id;
	}
}

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

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

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

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

int N;
int[] U, V;

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

      auto deg = new int[N];
      foreach (i; 0 .. N - 1) {
        ++deg[U[i]];
        ++deg[V[i]];
      }
      const ans = (deg.count(2) == 0);
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto n = RD!int;
	auto edges = new long[][](n);
	foreach (i; 0..n-1)
	{
		auto u = RD!int-1;
		auto v = RD!int-1;
		edges[u] ~= v;
		edges[v] ~= u;
	}

	debug writeln(edges);
	bool ans = true;
	foreach (i; 0..n)
	{
		if (edges[i].length == 2)
		{
			ans = false;
			break;
		}
	}

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

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

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

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

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

int N;
int[] U, V;

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

      auto deg = new int[N];
      foreach (i; 0 .. N - 1) {
        ++deg[U[i]];
        ++deg[V[i]];
      }
      const long cnt = deg.count(1);
      const ans = (cnt * (cnt - 1) / 2 >= N - 1);
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}


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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto s = readln.strip;
		int res = 5;
		string ans = "?????";
		foreach (c; 0..10_000)
		{
			int h = c / 100;
			if (n == 12 && !(1 <= h && h <= 12))
			{
				continue;
			}
			if (n == 24 && !(0 <= h && h <= 23))
			{
				continue;
			}
			int m = c % 100;
			if (!(0 <= m && m <= 59))
			{
				continue;
			}
			auto t = format ("%02d:%02d", h, m);
			auto cur = t.length.iota.map !(x => s[x] != t[x]).sum;
			if (res > cur)
			{
				res = cur;
				ans = t;
			}
		}
		writeln (ans);
	}
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(X a,in int n,in int m)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in int n){bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(T ptrs) if (ptrs.length > 0) {return readf(replicate(" %s"~'\n', ptrs.length), ptrs)==ptrs.length;}
struct pair(X,Y)
{
	X first;
	Y second;
	alias first fi;
	alias second se;
	this(X a_,Y b_){
		fi=a_;
		se=b_;
	}
	this(this){fi=fi;se=se;}
	this(A,B)(auto ref pair!(A,B) p) if(is(A:X) && is(B:Y))
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(A,B)(in pair!(A,B) s_) const pure nothrow @safe if(is(typeof(s_.fi<fi)==bool) && is(typeof(s_.se<se)==bool))
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
pure nothrow V foldr(R,V,T)(R range,T arg,V nach) if(isFunction!T && isInputRange!R && is(typeof(arg(nach,R.front))==V))
{
	for(;!range.empty;range.popFront()) nach=arg(nach,range.front);
	return nach;
}
pure nothrow size_t countr(R,T)(R range,T fun) if(isFunction!T && isInputRange!R && is(typeof(fun(R.front))==bool))
{
	size_t nach;
	for(;!range.empty;range.popFront()) nach+=fun(range.front);
	return nach;
}
pure nothrow T orient_square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	auto n=figure.front,t=figure.front;
	figure.popFront();
	T ans=0;
	for(;!figure.empty;figure.popFront())
	{
		ans+=(figure.front.fi-t.fi)*(figure.front.se+t.se);
		t=figure.front;
	}
	return ans+(n.fi-t.fi)*(n.se+t.se);
}
pure nothrow real square(T)(pair!(T,T)[] figure...) if(figure.length>2 && is(typeof(figure.front*figure.front)==T))
{
	return abs(orient_square(figure))/2.00000000000;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		auto s=readln.strip.split(':').map!(to!int).array;
		int ans;
		if(s[1]>59) {s[1]%=10;}
		if(n==12)
		{
			if(s[0]==0){s[0]=10;}
			else if(s[0]>12)
			{
				if(s[0]%10<=2)s[0]=s[0]%10+10;
				else s[0]%=10;
			}
		}
		else if(s[0]>23){s[0]%=10;ans++;}
		writefln("%02d:%02d",s[0],s[1]);

	}
	debug system("pause");
}


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

void main()
{
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto T = readln.chomp;

    int s, t, c, max_c, d, max_d;
    foreach (i; 0..N) {
        auto s1 = S[i] == '1';
        auto t1 = T[i] == '1';
        if (s1) ++s;
        if (t1) ++t;

        if (s1 && !t1) {
            ++c;
            if (d > 0) --d;
        } else if (!s1 && t1) {
            ++d;
            if (c > 0) --c;
        }
        max_c = max(max_c, c);
        max_d = max(max_d, d);
    }

    if (s != t) {
        writeln(-1);
    } else {
        writeln(max(max_c, max_d));
    }
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto s = readln.strip.map !(to !(byte)).array;
		auto t = readln.strip.map !(to !(byte)).array;
		if (sum (s) != sum (t))
		{
			writeln (-1);
			continue;
		}

		int balance = 0;
		int lo = 0;
		int hi = 0;
		foreach (i; 0..n)
		{
			balance += s[i] - t[i];
			lo = min (lo, balance);
			hi = max (hi, balance);
		}
		writeln (hi - lo);
	}
}

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

void main()
{
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto T = readln.chomp;

    int s, t, c, max_c;
    foreach (i; 0..N) {
        auto s1 = S[i] == '1';
        auto t1 = T[i] == '1';
        if (s1) ++s;
        if (t1) ++t;

        if (s1 && !t1) {
            ++c;
        } else if (!s1 && t1) {
            --c;
        }
        max_c = max(max_c, abs(c));
    }

    if (s != t) {
        writeln(-1);
    } else {
        writeln(max_c);
    }
}


import std.stdio : writef, readf, readln, writeln, writefln;
import std.array : array, split;
import std.algorithm : sort, each, maxElement, minElement;
import std.conv : to;
import std.range : iota, tee, enumerate;
import std.algorithm.comparison : equal, max, min;
import std.container.rbtree : RBT = RedBlackTree;
import std.string : strip;
import core.memory : GC;
import std.typecons : tuple;
import std.math;

alias Tree = RBT!int;
alias ll = long;

int[101] a;

void main() {
  GC.disable();

  int t;
  readf(" %s", t);

  foreach(q;0..t) {
    int n;
    readf(" %s", n);
    int[3] c;
    foreach(i;0..n) {
      int ai;
      readf(" %s", ai);
      c[ai%3]++;
    }

    auto tt = min(c[1], c[2]);
    c[1] -= tt;
    c[2] -= tt;

    writeln( c[0] + c[1]/3 + c[2]/3 + tt);
  }

}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto a = RDR.ARR;
		auto cnt = new long[](3);
		foreach (j; 0..n)
		{
			auto x = a[j] % 3;
			++cnt[cast(size_t)x];
		}
		auto x = min(cnt[1], cnt[2]);
		ans[i] = cnt[0] + x + (cnt[1]-x)/3 + (cnt[2]-x)/3;
	}

	foreach (e; ans)
		writeln(e);
	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.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto a = RDR.ARR;
		auto cnt = new long[](3);
		foreach (j; 0..n)
		{
			auto x = a[j] % 3;
			++cnt[cast(size_t)x];
		}
		auto x = min(cnt[1], cnt[2]);
		ans[i] = cnt[0] + x + (cnt[1]-x)/3;
	}

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


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

void main()
{
    ulong n, H;
    readf( "%s %s", &n, &H );

    ulong sqr = cast( ulong ) sqrt( cast( real )( 2*n ) );
    ulong seqlen = sqr * ( sqr+1 ) <= 2*n ? sqr : sqr - 1;

    ulong ans;
    if ( seqlen <= H ) {
        n -= seqlen * ( seqlen+1 ) / 2;
        ans = seqlen;
        if ( n > 0 ) ans += 1;
    } else {
        ulong lo = H, hi = 2 * 10 ^^ 9;
        debug { writeln(hi); }
        while ( lo < hi ) {
            ulong m = ( lo + hi + 1 ) / 2;

            bool over = false;
            ulong up = mulu( ( H+m ), ( m-H+1 ), over ) / 2;
            //ulong up = ( H+m ) * ( m - H+1 ) / 2;
            bool over2 = false;
            ulong down = mulu( m, ( m-1 ), over2 ) / 2;
            //ulong down = m * ( m-1 ) / 2;

            debug{ if ( over || over2 ) writeln("dassad"); }

            if ( up > n || n - up < down ) hi = m - 1;
            else lo = m;
        }

        debug { writeln(hi, ' ', hi - H, ' ', hi*(hi-1)/2 ); }

        ans = hi - ( H-1 );
        n -= ( H + hi ) * ( hi - H + 1 ) / 2;
        ans += hi - 1;
        n -= ( hi - 1 + 1 ) * ( hi - 1 ) / 2;
        ans += ( n + hi - 1 ) / hi;
    }

    writeln( ans );
}

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

void main()
{
    ulong n, H;
    readf( "%s %s", &n, &H );

    ulong sqr = cast( ulong ) sqrt( cast( real )( 2*n ) );
    ulong seqlen = sqr * ( sqr+1 ) <= 2*n ? sqr : sqr - 1;

    ulong ans;
    if ( seqlen <= H ) {
        n -= seqlen * ( seqlen+1 ) / 2;
        ans = seqlen;
        if ( n > 0 ) ans += 1;
    } else {
        ulong lo = H, hi = 2 * 10 ^^ 9;
        debug { writeln(hi); }
        while ( lo < hi ) {
            ulong m = ( lo + hi + 1 ) / 2;

            //bool over = false;
            //ulong up = mulu( ( H+m ), ( m-H+1 ), over ) / 2;
            ulong up = ( H+m ) * ( m - H+1 ) / 2;
            //bool over2 = false;
            //ulong down = mulu( m, ( m-1 ), over2 ) / 2;
            ulong down = m * ( m-1 ) / 2;

            //debug{ if ( over || over2 ) writeln("dassad"); }

            if ( up > n || n - up < down ) hi = m - 1;
            else lo = m;
        }

        debug { writeln(hi, ' ', hi - H, ' ', hi*(hi-1)/2 ); }

        ans = hi - ( H-1 );
        n -= ( H + hi ) * ( hi - H + 1 ) / 2;
        ans += hi - 1;
        n -= ( hi - 1 + 1 ) * ( hi - 1 ) / 2;
        ans += ( n + hi - 1 ) / hi;
    }

    writeln( ans );
}

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

void main()
{
    ulong n, H;
    readf( "%s %s", &n, &H );

    ulong sqr = cast( ulong ) sqrt( cast( real )( 2*n ) );

    ulong seqlen = sqr * ( sqr+1 ) <= 2*n ? sqr : sqr - 1;

    ulong ans;
    if ( seqlen <= H ) {
        n -= seqlen * ( seqlen+1 ) / 2;
        ans = seqlen + cast(int)( n > 0 );
    } else {
        n -= H * ( H+1 ) / 2;
        ans = H;
        ans += ( n + H-1 ) / H;
    }

    writeln( ans );
}


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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        auto X = new int[][](N, 5);
        foreach (u; 0 .. N) foreach (j; 0 .. 5) {
          X[u][j] = readInt();
        }

        int cmp(int u, int v) {
          int cnt;
          foreach (j; 0 .. 5) {
            if (X[u][j] < X[v][j]) {
              ++cnt;
            }
          }
          return (2 * cnt - 5);
        }

        int um = 0;
        foreach (v; 1 .. N) {
          if (cmp(um, v) < 0) {
            um = v;
          }
        }
        bool ok = true;
        foreach (v; 0 .. N) {
          if (um != v) {
            ok = ok && (cmp(um, v) > 0);
          }
        }
        writeln(ok ? (um + 1) : -1);
      }
    }
  } catch (EOFException e) {
  }
}

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

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

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

alias Ath = int[];

bool sup(Ath a, Ath b) {
  int t = 0;
  foreach (i; 0 .. 5) {
    if (a[i] < b[i]) t++;
  }

  return t >= 3;
}

void main() {
  int T;
  read(T);

  while (T--) {
    int n;
    read(n);
    Ath[] arr;
    foreach (i; 0 .. n) {
      arr ~= list!int;
    }

    auto rem = iota(n).array;

    while (rem.length != 1) {
      int[] rem2;

      for (int i = 0; i < rem.length; i += 2) {
        if (i == rem.length - 1) {
          rem2 ~= rem[i];
        } else {
          if (sup(arr[rem[i]], arr[rem[i + 1]])) {
            rem2 ~= rem[i];
          } else {
            rem2 ~= rem[i + 1];
          }
        }
      }

      rem = rem2;
    }

    int target = rem.front;
    bool yes = true;
    foreach (i, a; arr) {
      if (i != target && !sup(arr[target], a)) {
        yes = false;
      }
    }

    if (!yes) {
      writeln(-1);
    } else {
      writeln(target + 1);
    }
  }
}

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

bool is_better_than(int[] a, int[] b)
{
  int cnt;
  foreach (i ; 0 .. a.length) {
    if (a[i] < b[i])
      cnt++;
  }
  return (cnt >= 3);
}

void main()
{
  int t;
  readf!" %d "(t);
  while (t--) {
    int n;
    readf!" %d "(n);
    int[][] a;
    foreach (i ; 0 .. n) {
      a ~= readln.strip.split.map!(to!int).array;
    }
    int best_i = 0;
    foreach (i ; 1 .. n) {
      if (a[i].is_better_than(a[best_i])) {
        best_i = i;
      }
    }
    bool good = true;
    foreach (i ; 0 .. n) {
      if (best_i != i && !a[best_i].is_better_than(a[i]))
        good = false;
    }
    writeln(good ? best_i + 1 : -1);
  }
}

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

bool less (int [] u, int [] v)
{
	return zip (u, v).count !(q{a[0] < a[1]}) * 2 > u.length;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto r = new int [] [n];
		foreach (ref c; r)
		{
			c = readln.splitter.map !(to !(int)).array;
		}
		auto p = r.minPos !(less);
		if (r.count !(q => less (p.front, q)) == n - 1)
		{
			writeln (r.length - p.length + 1);
		}
		else
		{
			writeln (-1);
		}
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		auto r = new long[][](n);
		foreach (i; 0..n)
		{
			r[i] = RDA;
		}

		int x;
		foreach (int i; 1..n)
		{
			long cnt;
			foreach (j; 0..5)
			{
				if (r[x][j] < r[i][j])
					++cnt;
			}
			if (cnt >= 3) continue;
			x = i;
		}
		auto cnt = new long[](n);
		foreach (i; 0..n)
		{
			if (i == x) continue;
			foreach (j; 0..5)
			{
				if (r[x][j] < r[i][j])
					++cnt[i];
			}
		}
		ans[ti] = x+1;
		foreach (i; 0..n)
		{
			if (i == x) continue;
			if (cnt[i] < 3)
			{
				ans[ti] = -1;
				break;
			}
		}
	}

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


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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		reverse (a);
		writeln (a.isStrictlyMonotonic ? "NO" : "YES");
	}
}

// Cheese-Cracker: cheese-cracker.github.io
import std.stdio, std.conv, std.functional, std.string, std.algorithm;
import std.container, std.range, std.typecons, std.numeric, std.math, std.random;

alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
void show(A...)(A a) { debug{ foreach(t; a){ write(t, "| "); } writeln; } }
/*******************************It's a Me, Mario!*******************************************/

void play(){
    int n;
    n = rd!int;
    ll[] arr = rdarr;
    foreach(i; 0..n-1){
        if(arr[i] <= arr[i+1]){
            writeln("YES");
            return;
        }
    }
    writeln("NO");
}

int main(){
    long t = 1;
    t = rd;        // Toggle!
    while(t--) play();  // Let's play!
    stdout.flush;
    return 0;
}

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

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

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

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

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

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

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

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

void solve()
{
    int N; get(N);
    int[] AS; get(AS);
    foreach (i; 1..N) if (AS[i-1] <= AS[i]) {
        writeln("YES");
        return;
    }
    writeln("NO");
}

void main()
{
    int T; get(T);
    foreach (_; 0..T) solve();
}

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

void main(string[] args)
{
  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto n = next!int;
      auto a = new int[](n);
      foreach(ref ai; a) ai = next!int;
      if (iota(0, n - 1).all!(i => a[i] > a[i + 1]))
	writeln("NO");
      else
	writeln("YES");
    }
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  string toString()
  {
    return to!string(rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  pow = pow * pow;
	  exp >>= 1;
	}
      return res;
    }
}

// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto next(T)()
{
  static if (is(T == int) || is(T == long) || is(T == short))
    {
      import std.ascii: isDigit;
      T res;
      while(!frontChar.isDigit)
	popChar;
      while(frontChar.isDigit)
	{
	  res *= 10;
	  res += frontChar - '0';
	  popChar;
	}
      return res;
    }
  else static if (is(T == string))
    {
      import std.ascii: isWhite;
      string res;
      while(frontChar.isWhite)
	popChar;
      while(!frontChar.isWhite)
	{
	  res ~= frontChar;
	  popChar;
	}
      return res;
    }
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);

  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto n = next!int;
      auto a = next!int(n);
      if (iota(0, n - 1).all!(i => a[i] > a[i + 1]))
	writeln("NO");
      else
	writeln("YES");
    }
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  string toString()
  {
    return to!string(rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  pow = pow * pow;
	  exp >>= 1;
	}
      return res;
    }
}

// INPUT
File inputFile;
DList!string _words;
void _wordsFill()
{
  while (_words.empty) _words.insertBack(inputFile.readln.split);
}
string popWord()
{
  _wordsFill;
  auto word = _words.front;
  _words.removeFront;
  return word;
}
string peekWord()
{
  _wordsFill;
  return _words.front;
}
T next(T)()
{
  return to!T(popWord);
}
T peek(T)()
{
  return to!T(peekWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		bool ok;
		foreach (i; 1..n)
		{
			if (a[i] >= a[i-1])
			{
				ok = true;
				break;
			}
		}
		ans[ti] = ok;
	}

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

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

alias ll = long;
alias rbt = redBlackTree;
alias tup = Tuple!(long, long);
static string[] inp;
T rd(T = long)(){while(!inp.length) inp = readln.chomp.split; string a = inp[0]; inp.popFront; return a.to!T;}
T[] rdarr(T = long)(T fix = 0){ auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }

void solve(){
    int n;
    n = rd!int;
    dchar[] arr = rd!(dchar[]);
    int isodd = 0;
    if(n % 2){
        foreach(i; 0..n){
            // Odds
            if(i % 2 == 0 && (arr[i] - '0') % 2){
                isodd = 1;
                break;
            }
        }
        if(isodd){
            writeln(1);
        }else{
            writeln(2);
        }
    }else{
        foreach(i; 0..n){
            // Even
            if(i % 2 == 1 && (arr[i] - '0') % 2 == 0){
                isodd = 1;
                break;
            }
        }
        if(isodd){
            writeln(2);
        }else{
            writeln(1);
        }
    }
}

void main(){
    long t = 1;
    t = rd;
    while(t--) solve();
    stdout.flush;
}

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

void main(string[] args)
{
  inputFile = stdin;
  debug inputFile = File(args[1]);

  auto t = next!int;
  foreach(tc; 0 .. t)
    {
      auto n = next!int;
      auto a = next!int(n);
      if (n >= 3)
	{
	  writeln("YES");
	  continue;
	}
      if (n == 1) { writeln("YES"); continue; }
      if (n == 2) { if (a[0] <= a[1]) writeln("YES"); else writeln("NO"); continue; }
      assert(0);
    }
}
ulong bitCnt(long x)
{
  ulong cnt = 0;
  while(x) {cnt += x & 1; x>>=1;}
  return cnt;
}
alias Arr(T) = Mat!(T, 1);
struct Mat(T, size_t dimension)
{
  T[] _baseArray;
  T[] _slice;
  size_t[dimension] _sizes; size_t dim(size_t i) {return _sizes[i];}
  size_t[dimension] _indexer;
  this(T[] baseArray, size_t[dimension] sizes)
  {
    _baseArray = baseArray;
    _sizes = sizes;
    _indexer[dimension - 1] = 1;
    static foreach_reverse(i; 0 .. dimension - 1)
      _indexer[i] = _indexer[i + 1] * _sizes[i + 1];
    auto requiredSize = _indexer[0] * _sizes[0];
    debug assert(_baseArray.length >= requiredSize);
    _slice = _baseArray[0 .. requiredSize];
  }
  this(size_t[dimension] sizes)
  {
    size_t reqSize = 1;
    static foreach(i; 0 .. dimension)
      reqSize *= sizes[i];
    this(new T[](reqSize), sizes);
  }
  ref auto opIndex()
  {
    pragma(inline, true);
    return _slice[];
  }
  ref auto opIndex(I...)(I i)
  {
    pragma(inline, true);
    debug assert(i.length <= dimension);
    size_t index = mixin(iota(0, i.length).map!(j => text("i[", j, "]*_indexer[", j, "]")).join("+"));
    static if (i.length == dimension)
      {
	return _slice[index];
      }
    else
      {
	enum sliceDimension = dimension - i.length;
	size_t[sliceDimension] remSizes = _sizes[i.length .. $];
	auto basePortion = _slice[index .. index + _indexer[i.length - 1]];
 	return Mat!(T, sliceDimension)(basePortion, remSizes);
      }
  }
  static if (dimension == 2)
    {
      auto identity()
      {
	debug assert(dim(0) == dim(1));
	auto n = dim(0);
	auto id = Mat!(T, 2)([n, n]);
	foreach(k; 0 .. n)
	  id[k, k] = T(1);
	return id;
      }
      bool canMultiply(Mat!(T, 2) b)
      {
	return this.dim(1) == b.dim(0);
      }
      auto opBinary(string op)(Mat!(T, 2) b)
	if (op == "*")
	  {
	    debug assert(canMultiply(b));
	    auto res = Mat!(T, 2)([this.dim(0), b.dim(1)]);
	    foreach(i; 0 .. res.dim(0))
	      foreach(k; 0 .. this.dim(1))
		foreach(j; 0 .. res.dim(1))
		  res[i, j] = res[i, j] + this[i, k] * b[k, j];
	    return res;
	  }
      auto opBinary(string op)(ulong exp)
	if (op == "^^")
	  {
	    return this.pow(identity, exp);
	  }
      mixin interiorBinPow;
    }
}

struct Mod(T, ulong mod)
{
  alias This = Mod!(T, mod);
  T _rep; T rep() {return _rep;}
  this(W)(W w)
  {
    _rep = ((cast(T) w)%mod + mod) % mod;
  }
  static auto plainConstruct(T t)
  {
    pragma(inline, true);
    debug assert(t >= 0 && t < mod);
    auto res = Mod!(T, mod).init;
    res._rep = t;
    return res;
  }
  string toString()
  {
    return to!string(rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plainConstruct(resrep);
      }
    else static if (op == "*")
      {
	return This(_rep * b._rep);
      }
  }
}

mixin template interiorBinPow()
{
  alias ThisType = typeof(this);
  auto pow(ThisType res, ulong exp)
    {
      if (exp < 0) debug assert(0);
      auto pow = this;
      while(exp)
	{
	  if (exp & 1)
	    res = res * pow;
	  pow = pow * pow;
	  exp >>= 1;
	}
      return res;
    }
}

// INPUT
File inputFile;
DList!string _words;
void _wordsFill()
{
  while (_words.empty) _words.insertBack(inputFile.readln.split);
}
string popWord()
{
  _wordsFill;
  auto word = _words.front;
  _words.removeFront;
  return word;
}
string peekWord()
{
  _wordsFill;
  return _words.front;
}
T next(T)()
{
  return to!T(popWord);
}
T peek(T)()
{
  return to!T(peekWord);
}
auto next(T, S...)(S s)
{
  static if (S.length == 0)
    {
      return to!T(popWord);
    }
  else
    {
      auto res = new typeof(next!T(s[1 .. $]))[](cast(size_t)s[0]);
      foreach(ref elem; res)
        elem = next!T(s[1 .. $]);
      return res;
    }
}


import std.stdio;
import std.algorithm.comparison;
import std.algorithm.sorting;
import std.algorithm.mutation;
import std.range;
import std.range.primitives;
import std.conv;

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

    int[501][501] dp = 0;
    foreach ( j; 0 .. 501 ) dp[0][j] = 1;

    int n, m, b, mod, a;
    readf(" %s %s %s %s\n", &n, &m, &b, &mod);
    foreach ( l; 0 .. n ) {
        readf(" %s", &a);
        foreach ( i; 1 .. m + 1 ) {
            foreach ( j; a .. b + 1 ) {
                dp[i][j] += dp[i - 1][j - a];
                dp[i][j] %= mod;
            }
        }
    }
    writeln(dp[m][b]);

    return 0;
}

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

void main ()
{
	int n, m, b, v;
	while (readf (" %s %s %s %s ", &n, &m, &b, &v) > 0)
	{
		auto a = readln.split.map !(to !(int)).array;
		auto f = new int [] [] [] (2, m + 1, b + 1);
		int z = 0;
		foreach (ref g; f[z])
		{
			g[] = 0;
		}
		f[z][0][0] = 1 % v;
		foreach (i; 0..n)
		{
			z ^= 1;
			foreach (j; 0..m + 1)
			{
				f[z][] = f[!z][];
			}
			foreach (j; 1..m + 1)
			{
				foreach (k; a[i]..b + 1)
				{
					f[z][j][k] += f[!z][j - 1][k - a[i]];
					if (f[z][j][k] >= v)
					{
						f[z][j][k] -= v;
					}
				}
			}
		}
		int res = 0;
		foreach (k; 0..b + 1)
		{
			res += f[z][m][k];
			if (res >= v)
			{
				res -= v;
			}
		}
		writeln (res);
	}
}