module main;
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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(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;
	}
	bool binary_search(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X && !isSomeChar(ElementType!X))
{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", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning---------------------------------------
auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
	if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res*=base;
		if(res>=mm)res%=mm;
		base*=base;
		if(base>=mm)base%=mm;
		exp>>=1;
	}
	return res;
}
long base=131;
long rb=binpow(131L,mod-2,mod);
void main()
{
	/*int n;
	loop:while(read(&n))
	{

	}*/
	debug writeln(cast(int)('C'));
	auto s=readln.strip;
	auto h1=new int[s.length];
	auto st=new int[s.length];
	auto h2=h1.dup;
	long p=base;
	h1[0]=s[0];
	foreach(i;1..s.length)
	{
		h1[i]=(h1[i-1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	p=base;
	h2[$-1]=s[$-1];
	foreach_reverse(i;0..s.length-1)
	{
		h2[i]=(h2[i+1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	st[0]=1;
	foreach(i;1..st.length)
	{
		st[i]=(st[i-1]*rb)%mod;
	}
	debug writeln(st);
	long hsub(int r)
	{
		long ans=h2[0];
		if(r!=s.length-1)ans-=h2[r+1];
		if(ans<0)ans+=mod;
		ans*=st[s.length-1-r];
		if(ans>=mod)ans%=mod;
		return ans;
	}
	auto ans=new int[s.length];
	debug writeln(h1,'\n',h2);
	debug writeln(hsub(0)==h1[0]);
	foreach(i;0..s.length)
	{
		if(hsub(i)==h1[i])
		{
			if(i==0)ans[i]=1;
			else ans[i]=ans[(i-1)/2]+1;
		}
		else ans[i]=0;
	}
	debug writeln(ans);
	writeln(ans.sum);
}

module main;
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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(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;
	}
	bool binary_search(T,X)(in T a,auto ref X g)if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X && !isSomeChar(ElementType!X))
{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", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning---------------------------------------
auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
	if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res*=base;
		if(res>=mm)res%=mm;
		base*=base;
		if(base>=mm)base%=mm;
		exp>>=1;
	}
	return res;
}
long base=151;
long rb=binpow(151L,mod-2,mod);
char[] s;
void main()
{
	/*int n;
	loop:while(read(&n))
	{

	}*/
	s.length=5000000;
	debug writeln(cast(int)('C'));
	readln(s);
	s=s[0..$-1];
	auto h1=new int[s.length];
	auto st=new int[s.length];
	auto h2=h1.dup;
	long p=base;
	h1[0]=s[0];
	foreach(i;1..s.length)
	{
		h1[i]=(h1[i-1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	p=base;
	h2[$-1]=s[$-1];
	foreach_reverse(i;0..s.length-1)
	{
		h2[i]=(h2[i+1]+s[i]*p)%mod;
		p=(p*base)%mod;
	}
	st[0]=1;
	foreach(i;1..st.length)
	{
		st[i]=(st[i-1]*rb)%mod;
	}
	debug writeln(st);
	long hsub(int r)
	{
		long ans=h2[0];
		if(r!=s.length-1)ans-=h2[r+1];
		if(ans<0)ans+=mod;
		ans*=st[s.length-1-r];
		if(ans>=mod)ans%=mod;
		return ans;
	}
	auto ans=new int[s.length];
	debug writeln(h1,'\n',h2);
	debug writeln(hsub(0)==h1[0]);
	foreach(i;0..s.length)
	{
		if(hsub(i)==h1[i])
		{
			if(i==0)ans[i]=1;
			else ans[i]=ans[(i-1)/2]+1;
		}
		else ans[i]=0;
	}
	debug writeln(ans);
	writeln(ans.sum);
}

module main;
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,
	std.stdio,std.bitmanip;
import core.stdc.stdio : freopen;
alias RedBlackTree Rbt;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias boyerMooreFinder strfind;
alias make_pair mp;
alias binary_search bins;
alias pair(X,Y)=Tuple!(X,q{fi},Y,q{se});
pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_)
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
immutable int mod=10^^9+7;
immutable long hashmod=10L^^18+3;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b)
	{
		return a/gcd(a,b)*b;
	}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(g<a[m])r=m;
			else l=m;
		}
		return (g<a[l])?l:r;
	}
	size_t binf(T,X)(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;
	}
	bool binary_search(T,X)(in T a,auto ref X g)
		if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n,in size_t m) if(isRandomAccessRange!X)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n) if(hasSlicing!X)
{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", ptrs.length), ptrs)==ptrs.length;}
bool read(T...)(T ptrs) if (ptrs.length > 0)
{return readf(replicate(" %s", ptrs.length)~'\n', ptrs)==ptrs.length;}
nothrow void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold schwartzSort multiSort partialSort heapify join filter countUntil
//---------------------------------------------------------------program beginning-----------------------------------------------------------------
auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
	if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=mulmod(res,base,mm);
		base=mulmod(base,base,mm);
		//base%=mm;
		exp>>=1;
	}
	return res;
}
long mulmod(long a,long b,in long m=hashmod)
{
	long res;
	while(b)
	{
		if(b&1)
		{
			res+=a;
			if(res>=m)res-=m;
		}
		a<<=1;
		b>>=1;
		if(a>=m)a-=m;
	}
	return res;
}
long base=997;
long rb=binpow(997L,hashmod-2,hashmod);

void main()
{
	/*int n;
	loop:while(read(&n))
	{

	}*/
	auto s=readln.strip;
	auto h1=new long[s.length];
	auto st=new long[s.length];
	auto h2=h1.dup;
	long p=base;
	h1[0]=s[0];
	foreach(i;1..s.length)
	{
		h1[i]=(h1[i-1]+mulmod(s[i],p,hashmod))%hashmod;
		p=mulmod(p,base,hashmod);
	}
	p=base;
	h2[$-1]=s[$-1];
	foreach_reverse(i;0..s.length-1)
	{
		h2[i]=(h2[i+1]+mulmod(s[i],p,hashmod))%hashmod;
		p=mulmod(p,base,hashmod);
	}
	st[0]=1;
	foreach(i;1..st.length)
	{
		st[i]=mulmod(st[i-1],rb);
	}
	long hsub(int r)
	{
		long ans=h2[0];
		if(r!=s.length-1)ans-=h2[r+1];
		ans=mulmod(ans,st[s.length-1-r]);
		ans=(ans+hashmod)%hashmod;
		return ans;
	}
	auto ans=new long[s.length];
	foreach(i;0..s.length)
	{
		if(hsub(i)==h1[i])
		{
			if(i==0)ans[i]=1;
			else ans[i]=ans[(i-1)/2]+1;
		}
		else ans[i]=0;
	}
	writeln(ans.sum);
}


immutable multi = false;

void solve(int tc)
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = ma(n, readString);
	auto unknowable = new bool[][](n, m);
	bool isKnowable(int i, int j)
	{
		if (mat[i][j] == '.') return true;
		if (i == 0 || mat[i-1][j] == '.') return true;
		if (j == 0 || mat[i][j-1] == '.') return true;
		return false;
	}
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
		{
			unknowable[i][j] = !isKnowable(i, j);
		}
	auto hasUnknowable = new bool[](m);
	foreach(j; 0 .. m)
	{
		foreach(i; 0 .. n)
			hasUnknowable[j] = hasUnknowable[j] || unknowable[i][j];
	}
	auto compatible = new bool[](m);
	compatible[m-1] = true;
	foreach(j; 0 .. m-1)
	{
		auto exitable = new bool[](n);
		compatible[j] = true;
		exitable[0] = mat[0][j+1] == '.';
		foreach(i; 1 .. n)
		{
			if (mat[i][j+1] == '.')
			{
				exitable[i] = exitable[i-1] || mat[i][j] == '.';
				if (!exitable[i]) compatible[j] = false;
			}
			else
			{
				exitable[i] = false;
			}
		}
		debug writeln("for ", j);
		debug writeln(exitable);
	}
	auto pair = new int[](m);
	int j = m - 1;
	pair[m-1] = m-1;
	foreach_reverse(i; 0 .. m-1)
	{
		if (compatible[i] && !hasUnknowable[i+1])
		{
		}
		else
		{
			debug writeln("not compatible");
			j = i;
		}
		pair[i] = j;
	}
	debug writeln(compatible);
	debug writeln(hasUnknowable);
	debug writeln(pair);
	int q = readInt!int;
	foreach(i; 0 .. q)
	{
		int x = readInt!int-1;
		int y = readInt!int-1;
		if (y <= pair[x]) writeln("Yes");
		else writeln("No");
	}
}

//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

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

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

    string[] arr;
    foreach (i; 0 .. n) {
        arr ~= readln.chomp;
    }

    int[] cols;
    outer: foreach (j; 0 .. m-1) {
        foreach (i; 1 .. n) {
            if (arr[i][j] == 'X' && arr[i-1][j+1] == 'X') {
                cols ~= j+1;
                continue outer;
            }
        }
    }

    auto sorted = assumeSorted(cols);

    debug { sorted.writeln; }

    int q;
    readf!"%s"(q);
    readln;
    while (q--) {
        int x1, x2;
        readf!"%s %s"(x1, x2);
        readln;

        auto bigger = sorted.upperBound(x1-1);
        auto ambiguous = x1 < x2 && bigger.any() && bigger.front < x2;

        debug { writeln(bigger, ' ', x1, ' ', x2); }

        writeln(ambiguous ? "NO" : "YES");
    }
}

immutable multi = false;

void solve(int tc)
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = ma(n, readString);
	auto unknowable = new bool[][](n, m);
	bool isKnowable(int i, int j)
	{
		if (mat[i][j] == '.') return true;
		if (i == 0 || mat[i-1][j] == '.') return true;
		if (j == 0 || mat[i][j-1] == '.') return true;
		return false;
	}
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
		{
			unknowable[i][j] = !isKnowable(i, j);
		}
	auto hasUnknowable = new bool[](m);
	foreach(j; 0 .. m)
	{
		foreach(i; 0 .. n)
			hasUnknowable[j] = hasUnknowable[j] || unknowable[i][j];
	}
	auto compatible = new bool[](m);
	compatible[m-1] = true;
	foreach(j; 0 .. m-1)
	{
		auto exitable = new bool[](n);
		compatible[j] = true;
		exitable[0] = mat[0][j+1] == '.';
		foreach(i; 1 .. n)
		{
			if (mat[i][j+1] == '.')
			{
				exitable[i] = exitable[i-1] || mat[i][j] == '.';
				if (!exitable[i]) compatible[j] = false;
			}
			else
			{
				exitable[i] = false;
			}
		}
		debug writeln("for ", j);
		debug writeln(exitable);
	}
	auto pair = new int[](m);
	int j = m - 1;
	foreach_reverse(i; 0 .. m)
	{
		if (compatible[i])
		{
//			while (j > i && hasUnknowable[j]) j--;
		}
		else
		{
			debug writeln("not compatible");
			j = i;
		}
		pair[i] = j;
	}
	debug writeln(compatible);
	debug writeln(hasUnknowable);
	debug writeln(pair);
	int q = readInt!int;
	foreach(i; 0 .. q)
	{
		int x = readInt!int-1;
		int y = readInt!int-1;
		if (y <= pair[x]) writeln("Yes");
		else writeln("No");
	}
}

//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}

immutable multi = false;

void solve(int tc)
{
	int n = readInt!int;
	int m = readInt!int;
	auto mat = ma(n, readString);
	auto unknowable = new bool[][](n, m);
	bool isKnowable(int i, int j)
	{
		if (mat[i][j] == '.') return true;
		if (i == 0 || mat[i-1][j] == '.') return true;
		if (j == 0 || mat[i][j-1] == '.') return true;
		return false;
	}
	foreach(i; 0 .. n)
		foreach(j; 0 .. m)
		{
			unknowable[i][j] = !isKnowable(i, j);
		}
	auto hasUnknowable = new bool[](m);
	foreach(j; 0 .. m)
	{
		foreach(i; 0 .. n)
			hasUnknowable[j] = hasUnknowable[j] || unknowable[i][j];
	}
	auto compatible = new bool[](m);
	compatible[m-1] = true;
	foreach(j; 0 .. m-1)
	{
		auto exitable = new bool[](n);
		compatible[j] = true;
		exitable[0] = mat[0][j+1] == '.';
		foreach(i; 1 .. n)
		{
			if (mat[i][j+1] == '.')
			{
				exitable[i] = exitable[i-1] || mat[i][j] == '.';
				if (!exitable[i]) compatible[j] = false;
			}
			else
			{
				exitable[i] = false;
			}
		}
		debug writeln("for ", j);
		debug writeln(exitable);
	}
	auto pair = new int[](m);
	int j = m - 1;
	foreach_reverse(i; 0 .. m)
	{
		if (compatible[i])
		{
			while (j > i && hasUnknowable[j]) j--;
		}
		else
		{
			debug writeln("not compatible");
			j = i;
		}
		pair[i] = j;
	}
	debug writeln(compatible);
	debug writeln(hasUnknowable);
	debug writeln(pair);
	int q = readInt!int;
	foreach(i; 0 .. q)
	{
		int x = readInt!int-1;
		int y = readInt!int-1;
		if (y <= pair[x]) writeln("Yes");
		else writeln("No");
	}
}

//{{{
void main()
{
	auto t = multi? readInt!int : 1;
	foreach(tc; 0 .. t) solve(tc);
}
//}}}
// input {{{
int currChar;
static this()
{
	currChar = ' ';
}

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

void popChar()
{
	import core.stdc.stdio;
	currChar = getchar();
}

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

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
void writes(T...)(T t)
{
	static foreach(i, ti; t)
	{
		static if (isIterable!(T[i]))
		{
			foreach(e; ti)
			{
				write(e, " ");
			}
		}
	}
}
auto ra(V)()
{
	int n = readInt!int;
	return ma(n, readInt!V);
}
auto rt(V)()
{
	int n = readInt!int;
	int m = readInt!int;
	return ma(n, ma(m, readInt!V));
}
auto rbg()
{
	int n = readInt!int;
	int m = readInt!int;
	auto adj = new int[][](n);
	foreach(i; 0 .. m)
	{
		auto u = readInt!int - 1;
		auto v = readInt!int - 1;
		adj[u] ~= v;
		adj[v] ~= u;
	}
	return adj;
}
auto ma(V)(size_t n, lazy V v)
{
	auto arr = new V[](n);
	foreach(ref ai; arr) ai = v;
	return arr;
}
// }}}
// imports {{{
import std.stdio;
import std.algorithm;
import std.typecons;
import std.math;
import std.numeric;
import std.container;
import std.range;
import std.array;
import std.ascii;
// }}}


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

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

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

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

        return readToken.to!int;
    }

    string[] tokens;
}

void main() {
    IO io;
    int T = io.readInt;
    while (T--) {
        int n = io.readInt;
        int g = io.readInt;
        int b = io.readInt;
        int req = n + 1 >> 1;
        int k = (req - 1) / g;
        writeln(max(cast(long) k * (g + b) + (req - k * g), n));
    }
}

/// https://codeforces.com/problemset/problem/1303/B
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.algorithm : max;

long ceilDiv(long a, long b) {
  return a/b + (a%b > 0);
}

void main()
{
  int t, n, g, b;
  long k, m;
  stdin.readf!"%d\n"(t);
  while (t--) {
    stdin.readf!"%d %d %d\n"(n, g, b);
    k = (n+1)/2;
    m = ceilDiv(k, g);
    writeln((m-1)*b + k + max(n - k - b*(m-1), 0));
  }
}

/// https://codeforces.com/problemset/problem/1303/B
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.algorithm : max;

void main()
{
  int t, n, g, b;
  long k, m;
  stdin.readf!"%d\n"(t);
  while (t--) {
    stdin.readf!"%d %d %d\n"(n, g, b);
    k = (n + 1) >> 1;
    m = (k/g + (k%g > 0) - 1)*b + k;
    writeln(max(n, m));
  }
}

/// https://codeforces.com/problemset/problem/1303/B
/// PS> dmd cf.d; if($?) {cat in.txt | .\cf.exe}
import std.stdio : stdin, writeln;
import std.algorithm : max;

void main()
{
  int t, n, g, b;
  long k, m;
  stdin.readf!"%d\n"(t);
  while (t--) {
    stdin.readf!"%d %d %d\n"(n, g, b);
    k = (n + 1) >> 1;
    m = (k/g + (k%g > 0) - 1)*b + k;
    writeln(m + max(n - m, 0));
  }
}

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

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

void solve(){
	foreach(_; 0 .. rint){
		long n = rlong, g = rlong, b = rlong;

		long n1 = (n + 1) / 2; // minimum good pavement
		long n2 = n - n1; // maximum bad pavement

		long ans;
		long x = n2 / b;
		if((x + 1) * g >= n1){
			ans = n;
		}
		else{
			long n3 = n1 - (x + 1) * g;
			if(n3 % g == 0) ans = x * (g + b) + g + (n3 / g) * (g + b);
			else ans = x * (g + b) + g + (n3 / g) * (g + b) + b + (n3 % g);
		}

		ans.writeln;
	}
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		long need = (n+1) / 2;
		long cnt = need / g * (g+b) - b;
		long r = need % g;
		if (r != 0)
			cnt += b + r;
		ans[ti] = max(n, cnt);
	}

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


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

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto G = new int[][](N);

    foreach (_; 0..M) {
        s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }

    auto ans = new int[](N);
    int p = 0;

    auto used = new bool[](N);
    auto pq = new BinaryHeap!(Array!int, "a > b");
    pq.insert(0);

    while (!pq.empty) {
        auto n = pq.front;
        pq.popFront;
        if (used[n]) continue;
        used[n] = true;
        ans[p++] = n + 1;
        foreach (m; G[n]) {
            if (used[m]) continue;
            pq.insert(m);
        }
    }

    ans.map!(to!string).join(" ").writeln;
}

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

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

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

    auto adj = new int[][](n, 0);
    foreach (i ; 0 .. m) {
        int u, v;
        scan(u, v);
        u--, v--;
        adj[u] ~= v;
        adj[v] ~= u;
    }

    auto bh = new BinaryHeap!(Array!int, "a > b")();
    bh.insert(0);

    auto visited = new bool[](n);
    visited[0] = true;
    auto ans = new int[](n);

    foreach (i ; 0 .. n) {
        auto v = bh.front; bh.popFront();
        ans[i] = v + 1;

        foreach (u ; adj[v]) {
            if (visited[u]) continue;
            visited[u] = true;
            bh.insert(u);
        }
    }

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

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

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

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


import std.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;
import core.stdc.stdio:freopen;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree set;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
T lcm (T)(T a,T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
	if(!isFloatingPoint!Y && !is(typeof(exp.im)))
{
	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(!isFloatingPoint!Y && !is(typeof(exp.im)))
{
	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;
}
@property 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;
	}
}
@property 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", 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;}
	int opCmp(ref const pair!(X,Y) s_) const pure nothrow
	{
		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
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @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);}
auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow @property pure size_t lowb(T,X)(in T a,in 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 @property nothrow size_t upb(T,X)(in T a,in 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 @property nothrow size_t binf(T,X)(in T a,in 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 @property nothrow bool binary_search(T,X)(in T a,in X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
//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))
	{
		readln;
		auto a=arread!int;
		auto g=new int[][n+1];
		foreach(i;0..n-1)
		{
			int u,v;
			input(&u,&v);
			g[u]~=v;
			g[v]~=u;
		}
		long[] s,d;
		s.length=d.length=n+1;
		fill(d,long.min);
		//d[0]=0;
		debug
		{
			foreach(i;g)
			{
				foreach(j;i)write(j,' ');
				writeln;
			}
		}
		void dfs1(int v,int pr)
		{
			s[v]+=a[v-1];
			foreach(i;g[v])
			{
				if(i!=pr)
				{
					dfs1(i,v);
					d[v]=max(d[v],d[i]);
					s[v]+=s[i];
				}
			}
			d[v]=max(d[v],s[v]);
		}

		dfs1(1,1);
		debug putarr(d);
		debug writeln;
		long ans=long.min;
		void dfs2(int v,int pr,long h)
		{
			if(h!=long.min) ans=max(ans,s[v]+h);
			pll m1=mp(long.min,-1L),m2=mp(long.min,-1L);
			foreach(i;g[v])
			{
				if(i==pr)continue;
				if(d[i]>m1.fi)
				{
					m2=m1;
					m1=mp(d[i],i*1L);
				}
				else if(d[i]>m2.fi)
				{
					m2=mp(d[i],i*1L);
				}
			}
			foreach(i;g[v])
			{
				if(i==pr)continue;
				long cur= (m1.se==i?m2.fi:m1.fi);
				dfs2(i,v,max(h,cur));
			}

		}
		dfs2(1,1,long.min);
		if(ans==long.min)writeln("Impossible");
		else writeln(ans);
	}
}

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

__gshared:

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

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

long result;

struct Node {
    int niceness;
    Node*[ ] children;

    Tuple!(long, long) recurse(const(Node)* prev) const {
        long max1 = long.min, max2 = long.min;
        long sum = niceness, curmax = long.min;
        foreach (node; children)
            if (node !is prev) {
                long nextSum, nextMax;
                AliasSeq!(nextSum, nextMax) = node.recurse(&this);
                sum += nextSum;
                curmax = max(curmax, nextMax);
                if (nextMax > max1) {
                    max2 = max1;
                    max1 = nextMax;
                } else if (nextMax > max2)
                    max2 = nextMax;
            }
        if (max2 != long.min)
            result = max(result, max1 + max2);
        return tuple(sum, max(sum, curmax));
    }
}

Node[200_000] _nodes;

void main() {
    int n;
    while (read(&n)) {
        auto nodes = _nodes[0 .. n];
        foreach (ref node; nodes) {
            read(&node.niceness);
            node.children = null;
        }
        foreach (i; 1 .. n) {
            int a, b;
            read(&a, &b);
            a--;
            b--;
            nodes[a].children ~= &nodes[b];
            nodes[b].children ~= &nodes[a];
        }
        result = long.min;
        nodes[0].recurse(null);
        if (result != long.min)
            writeln(result);
        else
            writeln("Impossible");
    }
}

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;
import core.stdc.stdio:freopen;
import std.stdio;
//import core.stdc.stdio;
alias RedBlackTree set;
alias redBlackTree rbt;
alias BigInt big;
alias pair!(int,int) pii;
alias pair!(long,long) pll;
alias BoyerMooreFinder strfind;
immutable int mod=1000000007;
T lcm (T)(T a,T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow X binpow(X,Y)(X base,Y exp)
	if(!isFloatingPoint!Y && !is(typeof(exp.im)))
{
	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(!isFloatingPoint!Y && !is(typeof(exp.im)))
{
	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;
}
@property 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;
	}
}
@property 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", 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;}
	int opCmp(ref const pair!(X,Y) s_) const pure nothrow
	{
		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
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @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);}
auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow @property pure size_t lowb(T,X)(in T a,in 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 @property nothrow size_t upb(T,X)(in T a,in 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 @property nothrow size_t binf(T,X)(in T a,in 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 @property nothrow bool binary_search(T,X)(in T a,in X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
//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))
	{
		readln;
		auto a=arread!int;
		auto g=new int[][n+1];
		foreach(i;0..n-1)
		{
			int u,v;
			input(&u,&v);
			g[u]~=v;
			g[v]~=u;
		}
		auto f=fold!((a,b) => max(b.length,a))(g,0);
		if((f==2 && g[1].length<2) || n==1){writeln("Impossible");continue;}
		long[] s,d;
		s.length=d.length=n+1;
		fill(d,long.min);
		debug
		{
			foreach(i;g)
			{
				foreach(j;i)write(j,' ');
				writeln;
			}
		}
		void dfs1(int v,int pr)
		{
			s[v]+=a[v-1];
			foreach(i;g[v])
			{
				if(i!=pr)
				{
					dfs1(i,v);
					d[v]=max(d[v],d[i]);
					s[v]+=s[i];
				}
			}
			d[v]=max(d[v],s[v]);
		}

		dfs1(1,1);
		debug putarr(d);
		debug writeln;
		long ans=long.min;
		long dfs2(int v,int pr)
		{
			uint ans1,ans2;
			foreach(i;g[v])
			{
				if(i==pr)continue;
				if(d[i]>d[ans1]){ans2=ans1;ans1=i;}
				else if(d[i]>d[ans2])ans2=i;
			}
			debug writeln(d[ans1]);
			foreach(i;g[v])
			{
				if(i==pr )continue;
				if(i!=ans1)ans=max(ans,dfs2(i,v)+d[ans1]);
				else ans=max(ans,dfs2(i,v)+d[ans2]);
			}
			return d[ans1];
		}
		dfs2(1,1);
		writeln(ans);

	}
}


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

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

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

    auto ans = 0;

    foreach (p; a.length.iota.stride(2)) {
        auto k = a[p];
        debug { writeln(p, ' ', k, ' ', a[p+1..$]); }
        auto i = a.length - a[p+1..$].find(k).length;
        debug { i.writeln; }
        while (a[i-1] != k) {
            ++ans;
            a[i] = a[i-1];
            a[i-1] = k;
            --i;
        }
    }

    ans.writeln;
}

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

		long res = 0;
		for (int i = 0; i < 2 * n; i += 2)
		{
			int j = i + 1;
			while (a[j] != a[i])
			{
				j += 1;
			}
			while (j > i + 1)
			{
				res += 1;
				swap (a[j], a[j - 1]);
				j -= 1;
			}
		}
		writeln (res);
	}
}

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

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

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

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

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

      auto as = A.dup;
      int ans;
      for (int i = 0; i < 2 * N; i += 2) {
        foreach (j; i + 1 .. 2 * N) {
          if (as[i] == as[j]) {
            for (; i + 1 < j; --j) {
              swap(as[j], as[j - 1]);
              ++ans;
            }
            goto done;
          }
        }
        assert(false);
       done:
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.algorithm;

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

    auto a = new int[](n);

    foreach(i; 0..n) {
        readf(" %d", &a[i]);
    }

    int k = a.reduce!(max), ix;
    auto ans = new char[](k * 3 * n);
    foreach(i; 0..k) {
        foreach(j; 0..n) {
            if (a[j] > i) ans[ix++] = 'P';
            if (j < n-1) ans[ix++] = 'R';
        }
        foreach(j; 0..n) {
            if (j < n-1) ans[ix++] = 'L';
        }
    }

    writeln(ans[0..ix]);
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [n];
		foreach (ref x; a)
		{
			readf (" %s", &x);
		}
		string res;
		foreach (i, x; a)
		{
			foreach (j; 0..x)
			{
				res ~= 'P';
				if (j + 1 < x)
				{
					if (i == 0)
					{
						res ~= "RL";
					}
					else
					{
						res ~= "LR";
					}
				}
			}
			if (i + 1 < n)
			{
				res ~= 'R';
			}
		}
		writeln (res);
	}
}

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

void main() {
    int n = stdin.readln.chop.to!int;
    int[] a = stdin.readln.chop.split.map!(to!int).array;
    char prev = 'X';
    int c = 0;
    while (true) {
        if (a[c] > 0) {
            if (prev == 'P') {
                if (c == 0) {
                    write("RL");
                    prev = 'L';
                } else {
                    write("LR");
                    prev = 'R';
                }
            } else {
                a[c]--;
                write('P');
                prev = 'P';
            }
        } else {
            if (c == n - 1) {
                break;
            }
            write('R');
            prev = 'R';
            c++;
        }
    }
    write('\n');
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [n];
		foreach (ref x; a)
		{
			readf (" %s", &x);
		}
		string [] res;
		foreach (x; a)
		{
			string temp;
			foreach (i; 0..x)
			{
				temp ~= 'P';
			}
			res ~= temp;
		}
		writef ("%(%(%c%)R%)", res);
		break;
	}
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto a = new int [n];
		foreach (ref x; a)
		{
			readf (" %s", &x);
		}
		string [] res;
		foreach (x; a)
		{
			string temp;
			foreach (i; 0..x)
			{
				temp ~= "P";
			}
			res ~= temp;
		}
		writefln ("%(%(%c%)R%)", res);
	}
}


import std.algorithm, std.array, std.stdio;
void main ()
{
	int n, a, b;
	scanf ("%d", &n);
	auto x = new int [n];
	foreach (i; 0..n)
		scanf ("%d", &x[i]);
	x = x.sort.uniq.array;
	scanf ("%d%d", &a, &b);
	int res = 0;
	while (a > b)
	{
		res++;
		int c = a - 1;
		int [] y;
		foreach (d; x)
		{
			int e = a - (a % d);
			if (e >= b)
			{
				y ~= d;
				c = min (c, e);
			}
		}
		a = c;
		x = y;
	}
	writeln (res);
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto x = new int [n];
		foreach (i; 0..n)
		{
			readf (" %s", &x[i]);
		}
		x = x.sort !("a < b", SwapStrategy.stable).uniq.array;
		int a, b;
		readf (" %s %s", &a, &b);

		int res = 0;
		while (a > b)
		{
			res++;
			int c = a - 1;
			int [] y;
			foreach (d; x)
			{
				int e = a - (a % d);
				if (e >= b)
				{
					y ~= d;
					c = min (c, e);
				}
			}
			a = c;
			x = y;
		}
		writeln (res);
	}
}

import std.algorithm, std.array, std.stdio;
void main ()
{
	int n, a, b;
	readf (" %s", &n);
	auto x = new int [n];
	foreach (i; 0..n)
		readf (" %s", &x[i]);
	x = x.sort.uniq.array;
	readf (" %s %s", &a, &b);
	int res = 0;
	while (a > b)
	{
		res++;
		int c = a - 1;
		int [] y;
		foreach (d; x)
		{
			int e = a - (a % d);
			if (e >= b)
			{
				y ~= d;
				c = min (c, e);
			}
		}
		a = c;
		x = y;
	}
	writeln (res);
}


import std.math;
import std.stdio;

import std.algorithm;
import std.stdio;

immutable int MAX_N = 1_000_000;

bool [] bool_primes (int n)
{
    auto a = new bool [n + 1];
    a[] = true;
    a[0] = a[1] = false;
    for (int i = 2; i * i <= n; i++)
        if (a[i])
            for (int j = i * i; j <= n; j += i)
                a[j] = false;
    return a;
}

void main ()
{
    bool [] p = bool_primes (MAX_N);

    int n;
    while (readf (" %s", &n) > 0)
    {
        foreach (i; 0..n)
        {
            long t;
            readf (" %s", &t);
            long s = cast (long) sqrt (cast (real) t);
            writef ("%s\n", t > 1 && s * s == t &&
                            p[cast (int) s] ? "YES" : "NO");
        }
    }
}

import std.stdio;
import std.conv: parse;
import std.array: split, join;
import std.algorithm: map;
import std.string: chop;
import std.math: sqrt;
import std.parallelism: taskPool;

void main() {
    readln;
    readln.chop.split(" ").map!yesno.join("\n").writeln;
}
string yesno(string a) {
    auto n = sqrt(cast(real)a.parse!long);
    if(n == 1) return "NO";
    if(n % 1 == 0 && isPrime(cast(long)n)) return "YES";
    return "NO";
}
bool isPrime(long n) {
    foreach(i; 2..cast(long)sqrt(cast(real)n) + 1) if(n % i == 0) return false;
    return true;
}

import std.math;
import std.stdio;

import std.algorithm;
import std.stdio;

immutable int MAX_N = 1_000_000;

bool [] bool_primes (int n)
{
    auto a = new bool [n + 1];
    a[] = true;
    a[0] = a[1] = false;
    for (int i = 2; i * i <= n; i++)
        if (a[i])
            for (int j = i * i; j <= n; j += i)
                a[j] = false;
    return a;
}

immutable bool [] p = bool_primes (MAX_N);

void main ()
{
    int n;
    while (readf (" %s", &n) > 0)
    {
        foreach (i; 0..n)
        {
            long t;
            readf (" %s", &t);
            long s = cast (long) sqrt (cast (real) t);
            writef ("%s\n", t > 1 && s * s == t &&
                            p[cast (int) s] ? "YES" : "NO");
        }
    }
}

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

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char rc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    do
    {
        res = res * 10 + cast (int) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    do
    {
        res = res * 10 + cast (long) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

char[] ns ()
{
    sb ();
    char[] res = [];
    do
    {
        res ~= to!char(curc);
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

void CFBETA142B()
{
    auto n = ni();
    int[] LinearEratosphene(int N)
    {
        int[] lp = new int[N + 1];
        int[] pr = [];
        for (int i = 2; i <= N; ++i)
        {
            if (lp[i] == 0)
            {
                lp[i] = i;
                pr ~= i;
            }
            for (int j = 0; (j < pr.length) && (pr[j] <= lp[i]) && (i * pr[j] <= N); ++j)
                lp[i * pr[j]] = pr[j];
        }
        return lp;
    }
    int[1000001] lp = LinearEratosphene(1000000);
    for (int i=0; i<n; ++i)
    {
        long x = nl();
        long xsq = cast(long)sqrt(cast(float)x);
        if ((xsq*xsq==x)&&(lp[cast(uint)xsq]==xsq)) printf("YES\n");
        else printf("NO\n");
    }
}

void main()
{
    CFBETA142B();
}

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

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char rc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    do
    {
        res = res * 10 + cast (int) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    do
    {
        res = res * 10 + cast (long) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

char[] ns ()
{
    sb ();
    char[] res = [];
    do
    {
        res ~= to!char(curc);
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

void CFBETA142B()
{
    auto n = ni();
    long[] x = new long[n];
    long m=0;
    for (int i=0; i<n; ++i){
        x[i] = nl();
        m = max(m,x[i]);
    }
    bool[long] st;
    bool[long] T;
    for (int i=0; i<n; ++i) st[x[i]]=true;
    long[] LinearEratosphene(int N)
    {
        long[] lp = new long[N + 1];
        int[] pr = [];
        for (int i = 2; i <= N; ++i)
        {
            if (lp[i] == 0)
            {
                lp[i] = i;
                pr ~= i;
            }
            for (int j = 0; (j < pr.length) && (pr[j] <= lp[i]) && (i * pr[j] <= N); ++j)
                lp[i * pr[j]] = pr[j];
        }
        return lp;
    }
    long[1000001] lp = LinearEratosphene(1000000);
    for (long i=2L;i<=1000000L;++i)
        if ((lp[cast(uint)i]==i) && (((i*i) in st) !is null)) T[i*i]=true;
    for (int i=0;i<n;++i)
    if ((x[i] in T) !is null) printf("YES\n"); else printf("NO\n");
}

void main()
{
    CFBETA142B();
}

import std.stdio, std.math;

bool isPrime(long number) {
	if (number < 2) {
		return false;
	}
	if (number > 2 && number % 2 == 0) {
		return false;
	}

	for (auto i = 3; i * i <= number; i += 2) {
		if (number % i == 0) {
			return false;
		}
	}

	return true;
}

void main() {
	int n;
	long number;

	readf("%d", &n);
	for (auto i = 0; i < n; ++i) {
		readf(" %d", &number);

		double sqrtNumber = sqrt(cast(double) number);
		if (sqrtNumber - cast(long)sqrtNumber > 0) {
			writeln("NO");
		} else {
			isPrime(cast(long) sqrtNumber)? writeln("YES"): writeln("NO");
		}
	}
}

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

immutable int mod = 10^^9 + 7;

void main()
{
    auto n = readln.chomp.to!int;
    auto x = readln.split.to!(long[]);

    auto primes = eratos(10^^6);

    foreach (xi ; x) {
        long v = nibutan(primes, xi);

        debug {
            writeln(v);
        }

        if (v^^2 == xi) {
            writeln("YES");
        }
        else {
            writeln("NO");
        }
    }
}

long nibutan(int[] primes, long xi)
{
    int btm = -1, top = primes.length.to!int, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;

        if (primes[mid] <= xi / primes[mid]) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    return btm > -1 ? primes[btm] : 0;
}

int[] eratos(int n)
{
    bool[] s = new bool[](n);
    fill(s, true);
    s[0] = s[1] = false;

    for (int p = 2; p*p < n; ++p) {
        if (s[p]) {
            for (int q = p*p; q < n; q += p) {
                s[q] = false;
            }
        }
    }

    return iota(n).filter!(a => s[a]).array;
}

bool is_prime(long x) {
    if (x < 2) return false;

    for (int p = 2; p*p <= x; ++p) {
        if (x % p == 0) return false;
    }

    return true;
}

long root(long x) {
    long top = x, btm = 0, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;

        if (mid <= x/mid) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    return btm;
}

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

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

immutable int mod = 10^^9 + 7;

void main()
{
    int n = readln.chomp.to!int;
    auto x = readln.chomp.split.to!(long[]);

    foreach(xi; x) {
        long rx = root(xi);

        debug {
            writeln(rx);
            writeln(is_prime(rx));
        }

        if(xi > 1 && (rx^^2 == xi) && is_prime(rx)){
            writeln("YES");
        }
        else {
            writeln("NO");
        }
    }
}

bool is_prime(long x) {
    if (x < 2) return false;

    for (int p = 2; p*p <= x; ++p) {
        if (x % p == 0) return false;
    }

    return true;
}

long root(long x) {
    long top = x, btm = 0, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;

        if (mid <= x/mid) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    return btm;
}

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

import std.stdio;
import std.conv: parse;
import std.array: split, join;
import std.algorithm: map;
import std.string: chop;
import std.math: sqrt;

void main()
{
    readln;
    readln.chop.split(" ").map!yesno.join("\n").writeln;
}
string yesno(string a) {
    auto n = sqrt(cast(float)a.parse!int);
    if(n % 1 == 0 && isPrime(cast(int)n)) return "YES";
    return "NO";
}
bool isPrime(int n) {
    foreach(i; 2..n) if(n % i == 0) return false;
    return true;
}

import std.stdio;
import std.conv: parse;
import std.array: split, join;
import std.algorithm: map;
import std.string: chop;
import std.math: sqrt;

void main()
{
    readln;
    readln.chop.split(" ").map!yesno.join("\n").writeln;
}
string yesno(string a) {
    auto n = sqrt(cast(float)a.parse!long);
    if(n % 1 == 0 && isPrime(cast(int)n)) return "YES";
    return "NO";
}
bool isPrime(int n) {
    foreach(i; 2..n) if(n % i == 0) return false;
    return true;
}

import std.stdio;
import std.conv: parse;
import std.array: split, join;
import std.algorithm: map;
import std.string: chop;
import std.math: sqrt;

void main()
{
    readln;
    readln.chop.split(" ").map!yesno.join("\n").writeln;
}
string yesno(string a) {
    auto n = sqrt(cast(float)a.parse!long);
    if(n == 1) return "NO";
    if(n % 1 == 0 && isPrime(cast(int)n)) return "YES";
    return "NO";
}
bool isPrime(int n) {
    foreach(i; 2..n) if(n % i == 0) return false;
    return true;
}

import std.stdio;
import std.conv: parse;
import std.array: split, join;
import std.algorithm: map;
import std.string: chop;
import std.math: sqrt;

void main()
{
    readln;
    readln.chop.split(" ").map!yesno.join("\n").writeln;
}
string yesno(string a) {
    auto n = sqrt(cast(float)a.parse!int);
    if(n % 1 == 0 && isPrime(cast(int)n)) return "YES";
    return "NO";
}
bool isPrime(int n) {
    foreach(i; 2..n) if(n % i != 0) return false;
    return true;
}

import std.stdio;

bool [long] squares (int n)
{
    bool [long] res;
    foreach (i; 1..n + 1)
    {
        long s = i;
        s *= i;
        res[s] = true;
    }
    return res;
}

immutable bool [long] f;

void main ()
{
    int n;
    while (readf (" %s ", &n) > 0)
    {
        foreach (i; 0..n)
        {
            long t;
            readf (" %s ", &t);
            writef ("%s\n", t in f ? "YES" : "NO");
        }
    }
}

import std.math;
import std.stdio;

void main ()
{
    int n;
    while (readf (" %s", &n) > 0)
    {
        foreach (i; 0..n)
        {
            long t;
            readf (" %s", &t);
            long s = cast (long) sqrt (cast (real) t);
            writef ("%s\n", s * s == t ? "YES" : "NO");
        }
    }
}

import std.stdio;

bool [long] squares (int n)
{
    bool [long] res;
    foreach (i; 1..n + 1)
    {
        long s = i;
        s *= i;
        res[s] = true;
    }
    return res;
}

immutable bool [long] f;

void main ()
{
    int n;
    while (readf (" %s ", &n) > 0)
    {
        foreach (i; 0..n)
        {
            long t;
            readf (" %s ", &t);
            writef ("%s\n", f.get (t, false) ? "YES" : "NO");
        }
    }
}

import std.math;
import std.stdio;

void main ()
{
    int n;
    while (readf (" %s", &n) > 0)
    {
        foreach (i; 0..n)
        {
            long t;
            readf (" %s", &t);
            long s = cast (long) sqrt (cast (real) t);
            writef ("%s\n", t > 1 && s * s == t ? "YES" : "NO");
        }
    }
}

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

int curc;
bool eof = false;

void sb()
{
    do
    {
        curc = getchar ();
    }
    while (0 <= curc && curc <= 32);
    if (curc == EOF)
    {
        eof = true;
    }
}

char rc ()
{
    sb ();
    return cast (char) curc;
}

int ni ()
{
    sb ();
    int res = 0;
    do
    {
        res = res * 10 + cast (int) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

long nl ()
{
    sb ();
    long res = 0;
    do
    {
        res = res * 10 + cast (long) (curc - '0');
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

char[] ns ()
{
    sb ();
    char[] res = [];
    do
    {
        res ~= to!char(curc);
        curc = getchar ();
    }
    while (32 < curc);
    return res;
}

void CFBETA142B()
{
    auto n = ni();
    long[] x = new long[n];
    long m=0;
    for (int i=0; i<n; ++i){
        x[i] = nl();
        m = max(m,x[i]);
    }
    bool[long] st;
    bool[long] T;
    for (int i=0; i<n; ++i) st[x[i]]=true;
    int[] LinearEratosphene(int N)
    {
        int[] lp = new int[N + 1];
        int[] pr = [];
        for (int i = 2; i <= N; ++i)
        {
            if (lp[i] == 0)
            {
                lp[i] = i;
                pr ~= i;
            }
            for (int j = 0; (j < pr.length) && (pr[j] <= lp[i]) && (i * pr[j] <= N); ++j)
                lp[i * pr[j]] = pr[j];
        }
        return lp;
    }
    int[1000001] lp = LinearEratosphene(1000000);
    long cnt=0;
    for (int i=2;i<=1000000;++i)
        if ((lp[i]==i) && (((i*i) in st) !is null)) T[i*i]=true;
    for (int i=0;i<n;++i)
    if ((x[i] in T) !is null) printf("YES\n"); else printf("NO\n");
}

void main()
{
    CFBETA142B();
}

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

immutable int mod = 10^^9 + 7;

void main()
{
    int n = readln.chomp.to!int;
    auto x = readln.chomp.split.to!(long[]);

    foreach(xi; x) {
        if(xi > 1 && (root(xi)^^2 == xi)){
            writeln("YES");
        }
        else {
            writeln("NO");
        }
    }
}

long root(long x) {
    long top = x, btm = 0, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;

        if (mid*mid <= x) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    return btm;
}

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

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

immutable int mod = 10^^9 + 7;

void main()
{
    int n = readln.chomp.to!int;
    auto x = readln.chomp.split.to!(long[]);

    foreach(xi; x) {
        long rx = root(xi);

        if(xi > 1 && (rx^^2 == xi) && is_prime(rx)){
            writeln("YES");
        }
        else {
            writeln("NO");
        }
    }
}

bool is_prime(long x) {
    if (x < 2) return false;

    for (int p = 2; p*p <= x; ++p) {
        if (x % p == 0) return false;
    }

    return true;
}

long root(long x) {
    long top = x, btm = 0, mid;

    while (top - btm > 1) {
        mid = btm + (top - btm) / 2;

        if (mid*mid <= x) {
            btm = mid;
        }
        else {
            top = mid;
        }
    }

    return btm;
}

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


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

void main() {
    auto N = readln.chomp.to!int;
    foreach (i; 0..N) write(i + 5000001, " ");
    writeln;
}

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

void main() {
    int n; readf("%d", &n);
    writef("%d", 10000000 - n);
    for (int i = 10000000 - n + 1; i < 10000000; i++) {
        writef(" %d", i);
    }
    writeln;
}


import std.container: DList;
import std.algorithm: all, map, count, filter;
import std.range: iota;
import std.array: array;
import std.stdio;
import std.utf;

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  enum T = "abacaba";
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      char[] str = istr.dup;
      for(size_t i = 0; i + T.length - 1 < istr.length; i++)
        {
          if (iota(0, T.length).all!(j => str[i + j] == '?' || str[i + j] == T[j]))
            {
              auto saved = str[i .. i + T.length].dup;
              str[i .. i + T.length] = T[];
              if (iota(0, str.length - T.length + 1).filter!(k => k != i).all!(k => str[k .. k + T.length] != T[]))
                {
                  str = str.map!(c => c == '?'? 'z' : c).array.toUTF8.dup;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
              str[i .. i + T.length] = saved;
            }
        }
      writeln("No");
    }
}

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

immutable string pattern = "abacaba";

void main ()
{
	auto tests = readln.strip.to !(int);
multitest_loop:
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.strip;
		auto t = s.dup;
		foreach (ref c; t)
		{
			if (c == '?')
			{
				c = 'z';
			}
		}
		foreach (start; 0..n - 6)
		{
			if (zip (s[start..start + 7], pattern).any
			    !(p => p[0] != '?' && p[0] != p[1]))
			{
				continue;
			}
			auto r = t.dup;
			r[start..start + 7] = pattern;
			if (r.find (pattern).drop (1).find (pattern).empty)
			{
				writeln ("YES");
				writeln (r);
				continue multitest_loop;
			}
		}
		writeln ("NO");
	}
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      enum wantedString = "abacaba";
      for(size_t i = 0; i + wantedString.length - 1 < istr.length; i++)
        {
          char[] str = istr.dup;
          import std.algorithm: all, map, count, min;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              str[i .. i + wantedString.length] = wantedString[];
              auto appearances = iota(0, str.length).count!(k => str[k .. min(k + wantedString.length, $)] == wantedString[]);
              assert(appearances >= 1);
              if (appearances == 1)
                {
                  import std.utf;
                  str = str.map!(c => c == '?'? 'z' : c).array.toUTF8.dup;
                  import std.stdio;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
            }
        }
      import std.stdio;
      writeln("No");
    }
}

import std.container: DList;
import std.algorithm: all, map, count;
import std.range: iota;
import std.array: array;
import std.stdio;
import std.utf;

DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  enum T = "abacaba";
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      char[] str = istr.dup;
      for(size_t i = 0; i + T.length - 1 < istr.length; i++)
        {
          if (iota(0, T.length).all!(j => str[i + j] == '?' || str[i + j] == T[j]))
            {
              auto saved = str[i .. i + T.length].dup;
              str[i .. i + T.length] = T[];
              if (iota(0, str.length - T.length + 1).all!(k => k == i || str[k .. k + T.length] != T[]))
                {
                  str = str.map!(c => c == '?'? 'z' : c).array.toUTF8.dup;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
              str[i .. i + T.length] = saved;
            }
        }
      writeln("No");
    }
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      enum wantedString = "abacaba";
      for(size_t i = 0; i + wantedString.length - 1 < istr.length; i++)
        {
          char[] str = istr.dup;
          import std.algorithm: all, map, count, min;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              str[i .. i + wantedString.length] = wantedString[];
              import std.utf;
              str = str.map!(c => c == '?'? 'z' : c).array.toUTF8.dup;
              auto appearances = iota(0, str.length).count!(k => str[k .. min(k + wantedString.length, $)] == wantedString[]);
              assert(appearances >= 1);
              if (appearances == 1)
                {
                  import std.stdio;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
            }
        }
      import std.stdio;
      writeln("No");
    }
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      enum wantedString = "abacaba";
      char[] str = istr.dup;
      for(size_t i = 0; i + wantedString.length - 1 < istr.length; i++)
        {
          import std.algorithm: all, map, count;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              auto saved = str[i .. i + wantedString.length].dup;
              str[i .. i + wantedString.length] = wantedString[];
              if (iota(0, str.length - wantedString.length + 1).all!(k => k == i || str[k .. k + wantedString.length] != wantedString[]))
                {
                  import std.stdio;
                  import std.utf;
                  str = str.map!(c => c == '?'? 'z' : c).array.toUTF8.dup;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
              str[i .. i + wantedString.length] = saved;
            }
        }
      import std.stdio;
      writeln("No");
    }
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      char[] str = istr.dup;
      enum wantedString = "abacaba";
      for(size_t i = 0; i + wantedString.length - 1 < str.length; i++)
        {
          import std.algorithm: all, map, count;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              auto saved = str[i .. i + wantedString.length].dup;
              str[i .. i + wantedString.length] = wantedString[];
              import std.utf;
              str = str.map!(c => c == '?'? 'x' : c).array.toUTF8.dup;
              auto appearances = iota(0, str.length - wantedString.length + 1).count!(k => str[k .. k + wantedString.length] == wantedString[]);
              assert(appearances >= 1);
              if (iota(0, str.length - wantedString.length + 1).count!(k => str[k .. k + wantedString.length] == wantedString[]) == 1)
                {
                  import std.stdio;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
              str[i .. i + wantedString.length] = saved;
            }
        }
      import std.stdio;
      writeln("No");
    }
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      char[] str = istr.dup;
      enum wantedString = "abacaba";
      for(size_t i = 0; i + wantedString.length - 1 < str.length; i++)
        {
          import std.algorithm: all, map, count;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              auto saved = str[i .. i + wantedString.length].dup;
              str[i .. i + wantedString.length] = wantedString[];
              import std.utf;
              str = str.map!(c => c == '?'? 'x' : c).array.toUTF8.dup;
              if (iota(0, str.length - wantedString.length + 1).count!(k => str[k .. k + wantedString.length] == wantedString[]) == 1)
                {
                  import std.stdio;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
              str[i .. i + wantedString.length] = saved;
            }
        }
      import std.stdio;
      writeln("No");
    }
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      char[] str = istr.dup;
      enum wantedString = "abacaba";
      for(size_t i = 0; i + wantedString.length - 1 < str.length; i++)
        {
          import std.algorithm: all, map;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              auto saved = str[i .. i + wantedString.length].dup;
              str[i .. i + wantedString.length] = wantedString[];
              import std.utf;
              str = str.map!(c => c == '?'? 'x' : c).array.toUTF8.dup;
              if (iota(0, str.length - wantedString.length + 1).all!(k => k == i || str[k .. k + wantedString.length] != wantedString[]))
                {
                  import std.stdio;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
              str[i .. i + wantedString.length] = saved;
            }
        }
      import std.stdio;
      writeln("No");
    }
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      char[] str = istr.dup;
      enum wantedString = "abacaba";
      for(size_t i = 0; i + wantedString.length - 1 < str.length; i++)
        {
          import std.algorithm: all, map;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              auto saved = str[i .. i + wantedString.length].dup;
              str[i .. i + wantedString.length] = wantedString[];
              import std.utf;
              str = str.map!(c => c == '?'? 'x' : c).array.toUTF8.dup;
              if (iota(0, str.length - wantedString.length + 1).all!(k => k == i || str[k .. k + wantedString.length] != wantedString[]))
                {
                  import std.stdio;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
            }
        }
      import std.stdio;
      writeln("No");
    }
}

import std.container: DList;
DList!string _words;
void read(T)(out T t)
{
  import std.stdio: readln;
  while(_words.empty)
    {
      import std.array: split;
      foreach(word; readln.split)
        {
          _words.insertBack(word);
        }
    }
  import std.conv: to;
  string word = _words.front;
  _words.removeFront;
  t = to!T(word);
}

void main()
{
  int t;
  read(t);
 testCase:while(t--)
    {
      int n;
      string istr;
      read(n);
      read(istr);
      char[] str = istr.dup;
      enum wantedString = "abacaba";
      for(size_t i = 0; i + wantedString.length - 1 < str.length; i++)
        {
          import std.algorithm: all, map, count, min;
          import std.range: iota;
          import std.array: array;
          if (iota(0, wantedString.length).all!(j => str[i + j] == '?' || str[i + j] == wantedString[j]))
            {
              auto saved = str[i .. i + wantedString.length].dup;
              str[i .. i + wantedString.length] = wantedString[];
              import std.utf;
              str = str.map!(c => c == '?'? 'z' : c).array.toUTF8.dup;
              auto appearances = iota(0, str.length).count!(k => str[k .. min(k + wantedString.length, $)] == wantedString[]);
              assert(appearances >= 1);
              if (appearances == 1)
                {
                  import std.stdio;
                  writeln("Yes");
                  writeln(str.idup);
                  continue testCase;
                }
              str[i .. i + wantedString.length] = saved[];
            }
        }
      import std.stdio;
      writeln("No");
    }
}


// Vicfred
// https://codeforces.com/contest/1437/problem/C
//
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.stdio;
import std.string;

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

    int n;
    int[] t;
    while(q--) {
        n = readln.chomp.to!int;
        t = readln.split.map!(to!int).array;

        t = t.map!(x => x - 1).array;

        t.sort;

        // dp
        // number of dishes
        // at time t
        int[][] dp = new int[][](n + 1, 2*n);
        for(int i = 0; i < n + 1; i++) {
            for(int j = 0; j < 2*n; j++) {
                dp[i][j] = int.max;
            }
        }

        dp[0][0] = 0L;
        for(int i = 0; i < n + 1; i++) {
            for(int j = 0; j + 1 < 2*n; j++) {
                if(dp[i][j] < int.max) {
                    if(i + 1 <= n)
                        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(t[i] - j));
                    // dont take the dish and move to the next minute
                    dp[i][j + 1] = min(dp[i][j], dp[i][j + 1]);
                }
            }
        }

        dp[n][2 * n - 1].writeln;
    }
}

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

immutable int infinity = int.max / 2;

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;
		sort (a);
		auto f = new int [] [] (2, n + 1);
		int b = 0;
		f[b][] = infinity;
		f[b][0] = 0;
		foreach (i; 1..n * 2 + 1)
		{
			b ^= 1;
			f[b][] = f[!b][];
			foreach_reverse (k; 1..n + 1)
			{
				f[b][k] = min (f[b][k], f[!b][k - 1] +
				    abs (i - a[k - 1]));
			}
		}
		writeln (f[b][n]);
	}
}

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

immutable int infinity = int.max / 2;

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;
		sort (a);
		auto f = new int [n + 1];
		f[] = infinity;
		f[0] = 0;
		foreach (i; 1..n * 2 + 1)
		{
			foreach_reverse (k; 1..n + 1)
			{
				f[k] = min (f[k], f[k - 1] +
				    abs (i - a[k - 1]));
			}
		}
		writeln (f[n]);
	}
}


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

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

    auto s = readln.chomp;

    auto rooms = new bool[10];

    foreach (e; s) {
        if (e == 'L') {
            int i = 0;
            while (rooms[i]) { ++i; }
            rooms[i] = true;
        } else if (e == 'R') {
            int i = 9;
            while (rooms[i]) { --i; }
            rooms[i] = true;
        } else {
            rooms[e - '0'] = false;
        }
    }

    foreach (e; rooms) {
        write(e ? '1' : '0');
    }
    writeln;
}

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;
	char[] cs = readln.chomp.to!(char[]);

	char[] ans = "0000000000".to!(char[]);
	foreach(c; cs){
		if(c == 'L'){
			foreach(i; 0 .. 10) if(ans[i] == '0'){
				ans[i] = '1';
				break;
			}
		}
		else if(c == 'R'){
			foreach_reverse(i; 0 .. 10) if(ans[i] == '0'){
				ans[i] = '1';
				break;
			}
		}
		else{
			int i = (c - '0').to!int;
			ans[i] = '0';
		}
	}

	ans.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; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

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

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

void main()
{
	auto n = RD;
	auto s = RD!string;
	auto ans = new long[](10);
	foreach (c; s)
	{
		if (c == 'L')
		{
			foreach (i; 0..10)
			{
				if (ans[i] == 0)
				{
					ans[i] = 1;
					break;
				}
			}
		}
		else if (c == 'R')
		{
			foreach_reverse (i; 0..10)
			{
				if (ans[i] == 0)
				{
					ans[i] = 1;
					break;
				}
			}
		}
		else
		{
			auto pos = [c].to!int;
			ans[pos] = 0;
		}

	}

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


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

immutable int NA = -1;

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		a[] -= 1;

		auto x = (n + k - 2) / (k - 1);
		auto pos = new int [n];
		pos[] = NA;
		auto used = new bool [n];

		int [] [] answer;
		foreach (int i, ref cur; a)
		{
			if (pos[cur] != NA)
			{
				answer ~= [pos[cur], i];
				pos[cur] = NA;
				used[cur] = true;
				if (answer.length % x == 0)
				{
					pos[] = NA;
				}
				if (answer.length >= n)
				{
					break;
				}
			}
			else if (!used[cur])
			{
				pos[cur] = i;
			}
		}

		answer.schwartzSort !(c => a[c.front]);

		writefln !("%(%(%s %)\n%)") (answer.map !(map !(q{a + 1})));
	}
}

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

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

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

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

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto C = new int[N * K];
      foreach (i; 0 .. N * K) {
        C[i] = readInt() - 1;
      }

      auto xss = new int[][N];
      foreach (i; 0 .. N * K) {
        xss[C[i]] ~= i;
      }

      auto as = new int[N];
      auto bs = new int[N];
      as[] = -1;
      bs[] = -1;
      auto nums = new int[K - 1];
      foreach (c; 0 .. N) {
        ++nums[c % (K - 1)];
      }
      foreach (k; 0 .. K - 1) {
        alias Entry = Tuple!(int, "x", int, "c");
        Entry[] es;
        foreach (c; 0 .. N) {
          if (as[c] == -1) {
            es ~= Entry(xss[c][k + 1], c);
          }
        }
        es.sort;
        foreach (j; 0 .. nums[k]) {
          const c = es[j].c;
          as[c] = xss[c][k];
          bs[c] = xss[c][k + 1];
        }
      }

      foreach (c; 0 .. N) {
        writeln(as[c] + 1, " ", bs[c] + 1);
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable int NA = -1;

void main ()
{
	int n, k;
	while (readf !(" %s %s") (n, k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		a[] -= 1;

		auto x = (n + k - 2) / (k - 1);
		auto pos = new int [n];
		pos[] = NA;

		int [] [] answer;
		foreach (int i, ref cur; a)
		{
			if (pos[cur] == NA)
			{
				pos[cur] = i;
			}
			else
			{
				answer ~= [pos[cur] + 1, i + 1];
				pos[cur] = NA;
				if (answer.length % x == 0)
				{
					pos[] = NA;
				}
				if (answer.length >= n)
				{
					break;
				}
			}
		}

		writefln !("%(%(%s %)\n%)") (answer);
	}
}

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

// Dijkstra
class MinCostFlow(Capa, Cost, Capa CAPA_EPS = 0) {
  int n, m;
  int[][] g;
  int[] as, bs;
  Capa[] capa;
  Cost[] cost, pot;
  Capa tof;
  Cost toc;
  this(int n) {
    this.n = n; m = 0; g = new int[][n]; as = []; bs = []; capa = []; cost = [];
  }
  int addEdge(int u, int v, Capa w, Cost c) {
    debug {
      writeln("addEdge ", u, " ", v, " ", w, " ", c);
    }
    const i = m;
    g[u] ~= m; as ~= u; bs ~= v; capa ~= w; cost ~= c; ++m;
    g[v] ~= m; as ~= v; bs ~= u; capa ~= 0; cost ~= -c; ++m;
    return i;
  }
  bool solve(int source, int sink, Capa flow) {
    pot = new Cost[n];
    pot[] = 0;
    for (; ; ) {
      bool cont;
      foreach (u; 0 .. n) foreach (i; g[u]) if (capa[i] > CAPA_EPS) {
        const v = bs[i];
        if (pot[v] > pot[u] + cost[i]) {
          pot[v] = pot[u] + cost[i];
          cont = true;
        }
      }
      if (!cont) break;
    }
    auto dist = new Cost[n];
    auto prei = new int[n];
    auto vis = new bool[n];
    for (tof = 0, toc = 0; tof + CAPA_EPS < flow; ) {
      alias Entry = Tuple!(Cost, "c", int, "u");
      BinaryHeap!(Array!Entry, "a > b") que;
      dist[] = -1;
      prei[] = -1;
      vis[] = false;
      dist[source] = 0;
      prei[source] = -2;
      que.insert(Entry(0, source));
      for (; !que.empty(); ) {
        const c = que.front.c;
        const u = que.front.u;
        que.removeFront;
        if (vis[u]) continue;
        vis[u] = true;
        foreach (i; g[u]) if (capa[i] > CAPA_EPS) {
          const v = bs[i];
          if (vis[v]) continue;
          const cc = c + cost[i] + pot[u] - pot[v];
          if (prei[v] == -1 || dist[v] > cc) {
            dist[v] = cc;
            prei[v] = i;
            que.insert(Entry(cc, v));
          }
        }
      }
      if (!vis[sink]) return false;
      Capa f = flow - tof;
      for (int v = sink; v != source; ) {
        const i = prei[v];
        if (f > capa[i]) f = capa[i];
        v = as[i];
      }
      for (int v = sink; v != source; ) {
        const i = prei[v];
        capa[i] -= f; capa[i ^ 1] += f;
        v = as[i];
      }
      tof += f;
      toc += f * (dist[sink] - pot[source] + pot[sink]);
      pot[] += dist[];
    }
    return true;
  }
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const K = readInt();
      auto C = new int[N * K];
      foreach (i; 0 .. N * K) {
        C[i] = readInt() - 1;
      }

      auto app = new int[N];
      app[] = -1;
      auto nxt = new int[N * K];
      foreach_reverse (i; 0 .. N * K) {
        nxt[i] = app[C[i]];
        app[C[i]] = i;
      }

      const lim = (N + (K - 1) - 1) / (K - 1);
      auto mcf = new MinCostFlow!(int, int)(N * K + 1);
      foreach (i; 0 .. N * K) {
        mcf.addEdge(i, i + 1, lim, 0);
      }
      auto ids = new int[N * K];
      ids[] = -1;
      foreach (i; 0 .. N * K) {
        if (nxt[i] != -1) {
          ids[i] = mcf.addEdge(i, nxt[i] + 1, 1, -1);
        }
      }
      mcf.solve(0, N * K, lim);
      assert(mcf.toc <= -N);

      int cnt;
      foreach (i; 0 .. N * K) {
        if (ids[i] != -1 && mcf.capa[ids[i]] == 0) {
          writeln(i + 1, " ", nxt[i] + 1);
          if (++cnt >= N) {
            break;
          }
        }
      }
    }
  } catch (EOFException e) {
  }
}