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.string, core.stdc.stdlib;

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        auto N = readln.chomp.to!int;
        auto s = readln.split.map!(to!int);
        auto A = s[0];
        auto B = s[1];
        auto C = s[2];
        auto S = readln.chomp;
        int win = 0;
        auto ans = new dchar[](N);
        ans[] = '*';
        foreach (i, v; S) {
            if (v == 'R' && B > 0) {
                win += 1;
                B -= 1;
                ans[i] = 'P';
            } else if (v == 'P' && C > 0) {
                win += 1;
                C -= 1;
                ans[i] = 'S';
            } else if (v == 'S' && A > 0) {
                win += 1;
                A -= 1;
                ans[i] = 'R';
            }
        }
        foreach (i; 0..N) if (ans[i] == '*') {
            if (A > 0) {
                ans[i] = 'R';
                A -= 1;
            } else if (B > 0) {
                ans[i] = 'P';
                B -= 1;
            } else {
                ans[i] = 'S';
                C -= 1;
            }
        }
        if (win >= (N + 1) / 2) {
            writeln("YES");
            ans.writeln;
        } else {
            writeln("NO");
        }
    }
}

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

class InputReader {
  private:
  ubyte[] p;
  ubyte[] buffer;
  size_t cur;
  public:
  this () {
    buffer = uninitializedArray!(ubyte[])(16<<20);
    p = stdin.rawRead (buffer);
  }
  final ubyte skipByte (ubyte lo) {
    while (true) {
      auto a = p[cur .. $];
      auto r = a.find! (c => c >= lo);
      if (!r.empty) {
        cur += a.length - r.length;
        return p[cur++];
      }
      p = stdin.rawRead (buffer);
      cur = 0;
      if (p.empty) return 0;
    }
  }
  final ubyte nextByte () {
    if (cur < p.length) {
       return p[cur++];
    }
    p = stdin.rawRead (buffer);
    if (p.empty) return 0;
    cur = 1;
    return p[0];
  }

  template next(T) if (isSigned!T) {
    final T next ()  {
      T res;
      ubyte b = skipByte (45);
      if (b == 45) {
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 - (b - 48);
        }
      } else {
        res = b - 48;
        while (true) {
          b = nextByte ();
          if (b < 48 || b >= 58) {
            return res;
          }
          res = res * 10 + (b - 48);
        }
      }
    }
  }
  template next(T) if (isUnsigned!T) {
    final T next () {
      T res = skipByte (48) - 48;
      while (true) {
        ubyte b = nextByte ();
        if (b < 48 || b >= 58) {
          break;
        }
        res = res * 10 + (b - 48);
      }
      return res;
    }
  }
  final T[] nextA(T) (int n) {
    auto a = uninitializedArray!(T[]) (n);
    foreach (i; 0 .. n) {
      a[i] = next!T;
    }
    return a;
  }
}

immutable names = "RPS";

void main() {
  auto r = new InputReader;
  foreach (t; 0 .. r.next!uint) {
    immutable n = r.next!uint;
    auto cnt = r.nextA!uint(3);
    auto a = new char[n];
    auto b = new int[n];
    foreach (i; 0 .. n) {
      char c;
      int k = -1;
      do {
        c = r.nextByte ().to!char;
        foreach (o; 0 .. 3) {
          if (names[o] == c) {
            k = o;
          }
        }
      } while (k < 0);
      b[i] = (k + 1) % 3;
    }
    debug stderr.writeln ("b = ", b);
    int wins;
    foreach (k; 0 .. 3) {
      foreach (j; 0 .. n) {
        if (cnt[k] > 0 && b[j] == k) {
          --cnt[k];
          a[j] = names[k];
          ++wins;
        }
      }
    }
    debug stderr.writeln ("wins = ", wins);
    foreach (i; 0 .. n) {
      if (a[i] == char.init) {
        foreach (k; 0 .. 3) {
          if (cnt[k] > 0) {
            a[i] = names[k];
            --cnt[k];
            break;
          }
        }
      }
    }
    int req = n / 2;
    if (n & 1) ++req;
    if (wins >= req) {
      writeln ("YES");
      writeln (a);
    } else {
      writeln ("NO");
    }
  }
}

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 t = RD!int;
	auto ans = new char[][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto a = RD!int;
		auto b = RD!int;
		auto c = RD!int;
		auto s = RD!string;
		int cnt;
		foreach (j; 0..n)
		{
			if (s[j] == 'R')
			{
				if (b == 0)
					ans[i] ~= '?';
				else
				{
					ans[i] ~= 'P';
					--b;
					++cnt;
				}
			}
			else if (s[j] == 'P')
			{
				if (c == 0)
					ans[i] ~= '?';
				else
				{
					ans[i] ~= 'S';
					--c;
					++cnt;
				}
			}
			else
			{
				if (a == 0)
					ans[i] ~= '?';
				else
				{
					ans[i] ~= 'R';
					--a;
					++cnt;
				}
			}
		}
		if (cnt*2 < n)
		{
			ans[i].length = 0;
		}
		else
		{
			foreach (j; 0..n)
			{
				if (ans[i][j] == '?')
				{
					if (a != 0)
					{
						--a;
						ans[i][j] = 'R';
					}
					else if (b != 0)
					{
						--b;
						ans[i][j] = 'P';
					}
					else
					{
						--c;
						ans[i][j] = 'S';
					}
				}
			}
		}
	}

	foreach (e; ans)
	{
		if (e.length == 0)
		{
			writeln("NO");
		}
		else
		{
			writeln("YES");
			writeln(e);
		}
	}
	stdout.flush();
	debug readln();
}

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

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
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 t = RD!int;
	auto ans = new char[][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto a = RD!int;
		auto b = RD!int;
		auto c = RD!int;
		auto s = RD!string;
		int cnt;
		foreach (j; 0..n)
		{
			if (s[j] == 'R')
			{
				if (b == 0)
					ans[i] ~= '?';
				else
				{
					ans[i] ~= 'P';
					--b;
					++cnt;
				}
			}
			else if (s[j] == 'P')
			{
				if (c == 0)
					ans[i] ~= '?';
				else
				{
					ans[i] ~= 'S';
					--c;
					++cnt;
				}
			}
			else
			{
				if (a == 0)
					ans[i] ~= '?';
				else
				{
					ans[i] ~= 'R';
					--a;
					++cnt;
				}
			}
		}
		if (cnt*2 <= n)
		{
			ans[i].length = 0;
		}
		else
		{
			foreach (j; 0..n)
			{
				if (ans[i][j] == '?')
				{
					if (a != 0)
					{
						--a;
						ans[i][j] = 'R';
					}
					else if (b != 0)
					{
						--b;
						ans[i][j] = 'P';
					}
					else
					{
						--c;
						ans[i][j] = 'S';
					}
				}
			}
		}
	}

	foreach (e; ans)
	{
		if (e.length == 0)
		{
			writeln("NO");
		}
		else
		{
			writeln("YES");
			writeln(e);
		}
	}
	stdout.flush();
	debug readln();
}


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

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

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

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

struct ModInt(int M_) {
  import std.conv : to;
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op: "-")() const { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const {
    return mixin("ModInt(this) " ~ op ~ "= a");
  }
  ModInt opBinaryRight(string op)(long a) const {
    return mixin("ModInt(a) " ~ op ~ "= this");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

enum MO = 10^^9 + 7;
alias Mint = ModInt!MO;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto X = new long[N];
      foreach (i; 0 .. N) {
        X[i] = readLong();
      }
      X.sort;

      auto two = new Mint[N + 1];
      two[0] = 1;
      foreach (i; 1 .. N + 1) {
        two[i] = two[i - 1] * 2;
      }

      Mint ans;
      foreach (i; 0 .. N - 1) {
        ans += (two[i + 1] - 1) * (two[N - (i + 1)] - 1) * Mint(X[i + 1] - X[i]);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

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

immutable long MOD = 10^^9 + 7;

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}

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

    long ans = 0;
    foreach (i; 0..N) {
        ans -= A[i] * (powmod(2, N - i - 1, MOD) - 1) % MOD;
        ans = (ans % MOD + MOD) % MOD;
        ans += A[i] * (powmod(2, i, MOD) - 1) % MOD;
        ans = (ans % MOD + MOD) % MOD;
    }

    ans.writeln;
}

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

immutable int mod = 10^^9 + 7;
int n;
int[] x;

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

    x.sort();

    auto p2 = new int[](n + 1);
    p2[0] = 1;
    foreach (i ; 1 .. n + 1) {
        p2[i] = (2 * p2[i - 1]) % mod;
    }

    long ans;

    foreach (i ; 0 .. n) {
        ans += (1L * x[i] * p2[i]) % mod;
        ans %= mod;
        ans -= (1L * x[i] * p2[n - 1 - i]) % mod;
        ans %= mod;
        if (ans < 0) ans += mod;
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

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

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n;
loop:while(read(n))
	{
		auto a=arread!long;
		sort(a);
		long p=1,ans=0,d=a.back-a.front;
		foreach(i;1..n)
		{
			ans=(ans+d*p)%mod;
			p=(p*2)%mod;
			d+=a[n-1-i]-a[i];
			d=(d%mod+mod)%mod;
		}
		writeln(ans);
	}
}

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

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n;
loop:while(read(n))
	{
		auto a=arread!long;
		sort(a);
		long p=1,ans=0,d=a.back-a.front;
		foreach(i;1..n)
		{
			ans=(ans+d*p)%mod;
			p=(p*2)%mod;
			d+=a[n-1-i]-a[i];
			d%=mod;
		}
		writeln(ans);
	}
}

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

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n;
loop:while(read(n))
	{
		auto a=arread!long;
		sort(a);
		long p=1,ans=0,d=a.back-a.front;
		foreach(i;1..n)
		{
			ans=(ans+d*p)%mod;
			p=(p*2)%mod;
			d+=a[n-1-i]-a[i];
		}
		writeln(ans);
	}
}

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

immutable int mod = 10^^9 + 7;
int n;
int[] x;

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

    x.sort();

    auto p2 = new int[](n + 1);
    p2[0] = 1;
    foreach (i ; 1 .. n + 1) {
        p2[i] = (2 * p2[i - 1]) % mod;
    }

    long ans;

    foreach (i ; 0 .. n) {
        ans += (1L * x[i] * p2[i]) % mod;
        ans %= mod;
        ans -= (1L * x[i] * p2[n - 1 - i]) % mod;
        ans %= mod;
    }

    writeln(ans);
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		if (n == 1) continue;
		else if (n == 2)
			ans[ti] = m;
		else
			ans[ti] = m * 2;
	}

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

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

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		writeln (n == 1 ? 0 : n == 2 ? m : 2 * m);
	}
}


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

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

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

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

enum L = 9;

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

      alias Entry = Tuple!(int, "val", int, "i");
      auto ess = new Entry[][L + 1];
      foreach (i; 0 .. N) {
        foreach (x; 0 .. L) {
          int val;
          foreach (y; x .. L) {
            val = val * 10 + (A[i][y] - '0');
            ess[y + 1 - x] ~= Entry(val, i);
          }
        }
      }

      auto opt = new int[N];
      opt[] = L + 1;
      auto ans = new int[N];
      foreach (l; 1 .. L + 1) {
        auto es = ess[l];
        es.sort;
        const esLen = cast(int)(es.length);
        for (int j = 0, k; j < esLen; j = k) {
          for (k = j; k < esLen && es[j].val == es[k].val; ++k) {}
          if (es[j].i == es[k - 1].i) {
            const i = es[j].i;
            if (chmin(opt[i], l)) {
              ans[i] = es[j].val;
            }
          }
        }
      }

      foreach (i; 0 .. N) {
        writefln("%0*d", opt[i], ans[i]);
      }
    }
  } catch (EOFException e) {
  }
}

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;

immutable int letters = 10;

struct Node
{
	string mark;
	int p;
	int visits;
	int [letters] next;
}

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto s = new string [n];
		foreach (ref c; s)
		{
			c = readln.strip;
		}

		Node [] t;
		t.reserve (1 + n * 9);
		t ~= Node ();
		foreach (int i, b; s)
		{
			foreach (int j; 0..b.length)
			{
				auto c = b[j..$];
				int v = 0;
				while (!c.empty)
				{
					int d = c.front - '0';
					c.popFront ();
					if (!t[v].next[d])
					{
						t[v].next[d] =
						    t.length.to !(int);
						t ~= Node (s[i][j..$ -
						    c.length], i, 1);
					}
					v = t[v].next[d];
					t[v].visits += (t[v].p != i);
				}
			}
		}

		foreach (ref w; t)
		{
			debug {writeln (w);}
			if (w.visits == 1 &&
			    s[w.p].length > w.mark.length)
			{
				s[w.p] = w.mark;
			}
		}

		writefln ("%-(%s\n%)", s);
	}
}

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

void main() {
    auto N = readln.chomp.to!int;
    auto S = N.iota.map!(_ => readln.chomp).array;
    int[string] cnt;

    foreach (s; S) {
        bool[string] used;
        foreach (i; 0..9) {
            foreach (j; i..10) {
                if (s[i..j] in used)
                    continue;
                cnt[s[i..j]] += 1;
                used[s[i..j]] = true;
            }
        }
    }

    void solve(int ind) {
        foreach (len; 1..10)
            foreach (i; 0..10-len)
                if (cnt[S[ind][i..i+len]] == 1) {
                    writeln(S[ind][i..i+len]);
                    return;
                }
    }

    foreach (i; 0..N)
        solve(i);

}

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

void main() {
    auto N = readln.chomp.to!int;
    auto S = N.iota.map!(_ => readln.chomp).array;
    int[string] cnt;
    foreach (s; S)
        foreach (i; 0..9)
            foreach (j; i..10)
                cnt[s[i..j]] += 1;

    void solve(int ind) {
        foreach (len; 1..10)
            foreach (i; 0..10-len)
                if (cnt[S[ind][i..i+len]] == 1) {
                    writeln(S[ind][i..i+len]);
                    return;
                }
    }

    foreach (i; 0..N)
        solve(i);
}


/+ dub.sdl:
    name "B"
    dependency "dcomp" version=">=0.9.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner;

import std.typecons;

int main() {
    Scanner sc = new Scanner(stdin);
    int n;
    sc.read(n);
    int[] a = [1];
    int[] b = [];
    foreach (_; 0..n) {
        swap(a, b);
        auto c = [0] ~ b.dup;
        foreach (i; 0..a.length) {
            c[i] += a[i]; c[i] %= 2;
        }
        a = c;
    }
    writeln(a.length - 1);
    writeln(a.map!(to!string).join(" "));
    writeln(b.length - 1);
    writeln(b.map!(to!string).join(" "));
    return 0;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */

// module dcomp.foundation;

static if (__VERSION__ <= 2070) {
    /*
    Copied by https://github.com/dlang/phobos/blob/master/std/algorithm/iteration.d
    Copyright: Andrei Alexandrescu 2008-.
    License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
    */
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }

}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

// import dcomp.container.stackpayload;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray;
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succW() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (!line.empty && line.front.isWhite) {
            line.popFront;
        }
        return !line.empty;
    }
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            line = lineBuf[];
            f.readln(line);
            if (!line.length) return false;
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {

                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else static if (isStaticArray!T) {
                foreach (i; 0..T.length) {
                    bool f = succW();
                    assert(f);
                    x[i] = line.parse!E;
                }
            } else {
                StackPayload!E buf;
                while (succW()) {
                    buf ~= line.parse!E;
                }
                x = buf.data;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}

/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/container/stackpayload.d */
// module dcomp.container.stackpayload;

struct StackPayload(T, size_t MINCAP = 4) if (MINCAP >= 1) {
    import core.exception : RangeError;

    private T* _data;
    private uint len, cap;

    @property bool empty() const { return len == 0; }
    @property size_t length() const { return len; }
    alias opDollar = length;

    inout(T)[] data() inout { return (_data) ? _data[0..len] : null; }

    ref inout(T) opIndex(size_t i) inout {
        version(assert) if (len <= i) throw new RangeError();
        return _data[i];
    }
    ref inout(T) front() inout { return this[0]; }
    ref inout(T) back() inout { return this[$-1]; }

    void reserve(size_t newCap) {
        import core.memory : GC;
        import core.stdc.string : memcpy;
        import std.conv : to;
        if (newCap <= cap) return;
        void* newData = GC.malloc(newCap * T.sizeof);
        cap = newCap.to!uint;
        if (len) memcpy(newData, _data, len * T.sizeof);
        _data = cast(T*)(newData);
    }
    void free() {
        import core.memory : GC;
        GC.free(_data);
    }

    void clear() {
        len = 0;
    }

    void insertBack(T item) {
        import std.algorithm : max;
        if (len == cap) reserve(max(cap * 2, MINCAP));
        _data[len++] = item;
    }
    alias opOpAssign(string op : "~") = insertBack;
    void removeBack() {
        assert(!empty, "StackPayload.removeBack: Stack is empty");
        len--;
    }
}

/*
This source code generated by dcomp and include dcomp's source code.
dcomp's Copyright: Copyright (c) 2016- Kohei Morita. (https://github.com/yosupo06/dcomp)
dcomp's License: MIT License(https://github.com/yosupo06/dcomp/blob/master/LICENSE.txt)
*/

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();
      int[] fs = [0, 1];
      int[] gs = [1];
      foreach (i; 1 .. N) {
        // g + X f
        auto hs = gs.dup;
        hs ~= [0, 0];
        hs[1 .. $] += fs[];
        if (hs.any!"abs(a) > 1") {
          hs[1 .. $] -= fs[];
          hs[1 .. $] -= fs[];
        }
        if (hs.any!"abs(a) > 1") {
          assert(false);
        }
        gs = fs;
        fs = hs;
      }

      if (fs[$ - 1] < 0) {
        fs[] *= -1;
      }
      if (gs[$ - 1] < 0) {
        gs[] *= -1;
      }

      writeln(N);
      foreach (j; 0 .. N) {
        write(fs[j], " ");
      }
      writeln(fs[N]);
      writeln(N - 1);
      foreach (j; 0 .. N - 1) {
        write(gs[j], " ");
      }
      writeln(gs[N - 1]);
    }
  } catch (EOFException e) {
  }
}


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;

immutable int DIRS = 4;
immutable int [DIRS] DROW = [-1,  0, +1,  0];
immutable int [DIRS] DCOL = [ 0, -1,  0, +1];
immutable char [DIRS] DNAME = ['^', '<', 'v', '>'];

void main ()
{
	int n, m;
	while (readf (" %s %s ", &n, &m) > 0)
	{
		char [] [] a;
		a ~= '*'.repeat (m + 2).array ();
		foreach (row; 1..n + 1)
		{
			a ~= '*' ~ readln ().strip ().dup ~ '*';
		}
		a ~= '*'.repeat (m + 2).array ();
		debug {writeln (a);}

		alias Coord = Tuple !(int, "row", int, "col");
		Coord [] q;
		q.reserve ((n + 2) * (m + 2) + 1);

		auto g = new int [] [] (n + 2, m + 2);
		foreach (row; 1..n + 1)
		{
			foreach (col; 1..m + 1)
			{
				if (a[row][col] == '.')
				{
					g[row][col] = sum (DIRS.iota.map
					    !(dir => a[row + DROW[dir]]
					    [col + DCOL[dir]] == '.'));
					if (g[row][col] == 1)
					{
						q ~= Coord (row, col);
					}
				}
			}
		}
		debug {writeln (q);}

		void recalc (int row, int col)
		{
			foreach (dir; 0..DIRS)
			{
				int nrow = row + DROW[dir];
				int ncol = col + DCOL[dir];
				if (a[nrow][ncol] == '.')
				{
					g[nrow][ncol]--;
					if (g[nrow][ncol] == 1)
					{
						q ~= Coord (nrow, ncol);
					}
				}
			}
		}

		while (q.length > 0)
		{
			int row = q.front.row;
			int col = q.front.col;
			q.popFront ();
			q.assumeSafeAppend ();
			if (g[row][col] != 1 || a[row][col] != '.')
			{
				continue;
			}

			foreach (dir; 0..DIRS)
			{
				int nrow = row + DROW[dir];
				int ncol = col + DCOL[dir];
				if (a[nrow][ncol] == '.')
				{
					a[row][col] = DNAME[dir ^ 2];
					a[nrow][ncol] = DNAME[dir ^ 0];
					recalc (row, col);
					recalc (nrow, ncol);
					break;
				}
			}
		}

		if (any !(b => b.find ('.').length > 0) (a))
		{
			writeln ("Not unique");
		}
		else
		{
			writefln ("%-(%-(%s%)\n%)",
			    a[1..n + 1].map !(b => b[1..m + 1]));
		}
	}
}

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

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

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

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

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

immutable DX = [ +1, 0, -1, 0, ];
immutable DY = [ 0, +1, 0, -1, ];

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

bool solve() {
	int[][] deg = new int[][](M, N);
	foreach (x; 0 .. M) foreach (y; 0 .. N) if (A[x][y] == '.') {
		foreach (dir; 0 .. 4) {
			const xx = x + DX[dir];
			const yy = y + DY[dir];
			if (0 <= xx && xx < M && 0 <= yy && yy < N) {
				if (A[xx][yy] == '.') {
					++deg[x][y];
				}
			}
		}
	}
	Pair!(int, int)[] q;
	foreach (x; 0 .. M) foreach (y; 0 .. N) if (A[x][y] == '.') {
		if (deg[x][y] == 1) {
			q ~= pair(x, y);
		}
	}
	for (; !q.empty; ) {
debug{
writeln;
foreach(x;0..M)writeln(A[x].to!string);
}
		const x = q.front.x;
		const y = q.front.y;
		q.popFront;
		if (A[x][y] == '.' && deg[x][y] == 1) {
debug{
writeln(x," ",y);
}
			foreach (dir; 0 .. 4) {
				const xx = x + DX[dir];
				const yy = y + DY[dir];
				if (0 <= xx && xx < M && 0 <= yy && yy < N) {
					if (A[xx][yy] == '.') {
						A[x][y]   = "^<v>"[dir];
						A[xx][yy] = "v>^<"[dir];
						foreach (dir1; 0 .. 4) {
							const xxx = xx + DX[dir1];
							const yyy = yy + DY[dir1];
							if (0 <= xxx && xxx < M && 0 <= yyy && yyy < N) {
								if (A[xxx][yyy] == '.') {
									if (--deg[xxx][yyy] == 1) {
										q ~= pair(xxx, yyy);
									}
								}
							}
						}
						break;
					}
				}
			}
		}
	}
	foreach (x; 0 .. M) foreach (y; 0 .. N) {
		if (A[x][y] == '.') {
			return false;
		}
	}
	foreach (x; 0 .. M) {
		writeln(A[x].to!string);
	}
	return true;
}

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

		const res = solve;
		if (!res) {
			writeln("Not unique");
		}

	}
	} catch (EOFException) {}
}


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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti].length = 2;
		if (a >= b)
		{
			ans[ti][0] ~= '1';
			foreach (i; 0..a-1)
				ans[ti][0] ~= '0';
			if (b == c)
			{
				ans[ti][1] ~= '1';
				foreach (i; 0..b-1)
					ans[ti][1] ~= '0';
			}
			else
			{
				ans[ti][1] ~= '1';
				foreach (i; 1..b)
				{
					if (i == b-c)
						ans[ti][1] ~= '1';
					else
						ans[ti][1] ~= '0';
				}
			}
		}
		else
		{
			ans[ti][1] ~= '1';
			foreach (i; 0..b-1)
				ans[ti][1] ~= '0';
			if (a == c)
			{
				ans[ti][0] ~= '1';
				foreach (i; 0..a-1)
					ans[ti][0] ~= '0';
			}
			else
			{
				ans[ti][0] ~= '1';
				foreach (i; 1..a)
				{
					if (i == a-c)
						ans[ti][0] ~= '1';
					else
						ans[ti][0] ~= '0';
				}
			}
		}
		debug writeln("gcd:", gcd(ans[ti][0].to!long, ans[ti][1].to!long));
	}

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

import std.stdio;

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

  for(int k = 0; k < t; ++k) {
    int a, b, c;
    readf("%d %d %d\n", &a, &b, &c);

    write("1");
    for(int i = 0; i < a-1; ++i) write("0");
    write(" ");
    for(int i = 0; i < b-c+1; ++i) write("1");
    for(int i = 0; i < c-1; ++i) write("0");
    writeln();
  }
}

// Yo lo quiero más rápido

import std.stdio;

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

  for(int k = 0; k < t; ++k) {
    int a, b, c;
    readf("%d %d %d\n", &a, &b, &c);

    if(a > b) {
      write("1");
      for(int i = 0; i < a-1; ++i) write("0");
      write(" ");
      for(int i = 0; i < b-c+1; ++i) write("1");
      for(int i = 0; i < b-(b-c+1); ++i) write("0");
      writeln();
    } else {
      for(int i = 0; i < a-c+1; ++i) write("1");
      for(int i = 0; i < a-(a-c+1); ++i) write("0");
      write(" ");
      write("1");
      for(int i = 0; i < b-1; ++i) write("0");
      writeln();
    }
  }
}

import std.stdio;

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

  for(int k = 0; k < t; ++k) {
    int a, b, c;
    readf("%d %d %d\n", &a, &b, &c);

    if(a > b) {
      write("1");
      for(int i = 0; i < a-1; ++i) write("0");
      write(" ");
      for(int i = 0; i < b-c+1; ++i) write("1");
      for(int i = 0; i < b-(b-c+1); ++i) write("0");
      writeln();
    } else {
      for(int i = 0; i < b-c+1; ++i) write("1");
      for(int i = 0; i < b-(b-c+1); ++i) write("0");
      write(" ");
      write("1");
      for(int i = 0; i < a-1; ++i) write("0");
      writeln();
    }
  }
}

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

string FMT_F = "%.10f";

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

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

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

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

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

		ans[ti].length = 2;
		if (a >= b)
		{
			ans[ti][0] ~= '1';
			foreach (i; 0..a-1)
				ans[ti][0] ~= '0';
			if (b == c)
				ans[ti][1] = ans[ti][0];
			else
			{
				ans[ti][1] ~= '1';
				foreach (i; 1..b)
				{
					if (i == b-c)
						ans[ti][1] ~= '1';
					else
						ans[ti][1] ~= '0';
				}
			}
		}
		else
		{
			ans[ti][1] ~= '1';
			foreach (i; 0..b-1)
				ans[ti][1] ~= '0';
			if (a == c)
				ans[ti][0] = ans[ti][1];
			else
			{
				ans[ti][0] ~= '1';
				foreach (i; 1..a)
				{
					if (i == a-c)
						ans[ti][0] ~= '1';
					else
						ans[ti][0] ~= '0';
				}
			}
		}
		debug writeln("gcd:", gcd(ans[ti][0].to!long, ans[ti][1].to!long));
	}

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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	writeln(gcd(1010, 1111));
	auto t = RD!int;
	auto ans = new string[][](t);
	foreach (ti; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto c = RD;

		ans[ti].length = 2;
		if (a >= b)
		{
			foreach (i; 0..a)
				ans[ti][0] ~= '1';
			if (b == c)
				ans[ti][1] = ans[ti][0];
			else
			{
				foreach (i; 0..b)
				{
					if (i == b-c)
						ans[ti][1] ~= '0';
					else
						ans[ti][1] ~= '1';
				}
			}
		}
		else
		{
			foreach (i; 0..b)
				ans[ti][1] ~= '1';
			if (a == c)
				ans[ti][0] = ans[ti][1];
			else
			{
				foreach (i; 0..a)
				{
					if (i == a-c)
						ans[ti][0] ~= '0';
					else
						ans[ti][0] ~= '1';
				}
			}
		}
	}

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


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

void main() {
    auto s = readln.split.map!(to!long).array;
    auto B = s[0];
    auto K = s[1];
    auto A = readln.split.map!(to!long).array;

    long X = 0;
    B %= 2;

    foreach (i, a; A) {
        if (i != K - 1) {
            X += B * a % 2;
            X %= 2;
        } else {
            X += a % 2;
            X %= 2;
        }
    }

    writeln(X ? "odd" : "even");
}

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

void main ()
{
	int b, k;
	while (readf (" %s %s", &b, &k) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		a[0..$ - 1] *= b;
		auto res = sum (a, 0L);
		writeln (res % 2 == 0 ? "even" : "odd");
	}
}


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

int[] permutationMatch(T)(in T[] A, in T[] B) {
    // given array A and its permutation B, returns index array I.
    // I[i] == j means A[i] == B[j];
    assert(A.length == B.length);
    int N = cast(int)(A.length);
    int[] ans = new int[N];
    DList!(int)[T] index;
    for (int i = 0; i < N; i++) {
        int a = A[i];
        if (a !in index) index[a] = DList!int();
        index[A[i]] ~= i;
    }
    for (int j = 0; j < N; j++) {
        int b = B[j];
        int i = index[b].front; index[b].removeFront;
        ans[i] = j;
    }
    return ans;
}

alias P = Tuple!(int,int);
void solve() {
    int N = readln.chomp.to!int;
    auto A = readln.chomp.split(" ").map!(to!int).array;
    auto B = readln.chomp.split(" ").map!(to!int).array;
    bool C() {
        int i = N-1, j = N-1;
        auto s = new RedBlackTree!(int, "a<b", true)();
        while (true) {
            if (j >= 1 && B[j] == B[j-1]) {
                s.insert(B[j]);
                j--;
            } else if (i >= 0 && j >= 0 && A[i] == B[j]) {
                i--;
                j--;
            } else {
                if (i < 0 || A[i] !in s) return false;
                s.removeKey(A[i]);
                i--;
            }
            if (i == -1 && j == -1) break;
        }
        return true;
    }
    writeln(C() ? "YES" : "NO");
}

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

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

bool solve (int [] a, int [] b, int n)
{
	int [int] saved;
	int j = 0;
	foreach (i; 0..n)
	{
		if (a[i] == b[j])
		{
			j++;
			while (j < n && b[j] == b[j - 1] &&
			    saved.get (b[j], 0) > 0)
			{
				saved[b[j]] -= 1;
				j++;
			}
		}
		else
		{
			saved[a[i]] += 1;
		}
		debug {writeln (i, " ", j, " ", saved);}
	}
	return saved.byValue.sum == 0;
}

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;
		auto b = readln.splitter.map !(to !(int)).array;
		writeln (solve (a, b, n) ? "YES" : "NO");
	}
}

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

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

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

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

int N;
int[] A, B;

bool solve() {
  auto nxt = new int[N];
  {
    auto app = new int[N + 1];
    app[] = N;
    foreach_reverse (i; 0 .. N) {
      nxt[i] = app[A[i]];
      app[A[i]] = i;
    }
  }

  {
    auto cnt = new int[N];
    cnt[] = 1;
    int i;
    foreach (j; 0 .. N) {
      for (; ; ) {
        if (i >= N) {
          return false;
        }
        if (A[i] == B[j]) {
          if (--cnt[i] == 0) {
            ++i;
          }
          break;
        }
        if (nxt[i] >= N) {
          return false;
        }
        cnt[nxt[i]] += cnt[i];
        cnt[i] = 0;
        ++i;
      }
    }
  }
  return true;
}

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        N = readInt;
        A = new int[N]; foreach (i; 0 .. N) A[i] = readInt;
        B = new int[N]; foreach (i; 0 .. N) B[i] = readInt;

        const ans = solve;
        writeln(ans ? "YES" : "NO");
      }
    }
  } catch (EOFException e) {
  }
}

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

int[] permutationMatch(T)(in T[] A, in T[] B) {
    // given array A and its permutation B, returns index array I.
    // I[i] == j means A[i] == B[j];
    assert(A.length == B.length);
    int N = cast(int)(A.length);
    int[] ans = new int[N];
    DList!(int)[T] index;
    for (int i = 0; i < N; i++) {
        int a = A[i];
        if (a !in index) index[a] = DList!int();
        index[A[i]] ~= i;
    }
    for (int j = 0; j < N; j++) {
        int b = B[j];
        int i = index[b].front; index[b].removeFront;
        ans[i] = j;
    }
    return ans;
}

alias P = Tuple!(int,int);
void solve() {
    int N = readln.chomp.to!int;
    auto A = readln.chomp.split(" ").map!(to!int).array;
    auto B = readln.chomp.split(" ").map!(to!int).array;
    bool C() {
        int i = N-1;
        auto s = new RedBlackTree!(int, "a<b", true)();
        for (int j = N-1; j >= 0; j--) {
            if (j >= 1 && B[j] == B[j-1]) {
                s.insert(B[j]);
                continue;
            }
            if (A[i] == B[j]) {
                i--;
            } else {
                if (A[i] in s) {
                    s.removeKey(A[i]);
                    i--;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
    writeln(C() ? "YES" : "NO");
}

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

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

int[] permutationMatch(T)(in T[] A, in T[] B) {
    // given array A and its permutation B, returns index array I.
    // I[i] == j means A[i] == B[j];
    assert(A.length == B.length);
    int N = cast(int)(A.length);
    int[] ans = new int[N];
    DList!(int)[T] index;
    for (int i = 0; i < N; i++) {
        int a = A[i];
        if (a !in index) index[a] = DList!int();
        index[A[i]] ~= i;
    }
    for (int j = 0; j < N; j++) {
        int b = B[j];
        int i = index[b].front; index[b].removeFront;
        ans[i] = j;
    }
    return ans;
}

alias P = Tuple!(int,int);
void solve() {
    int N = readln.chomp.to!int;
    auto A = readln.chomp.split(" ").map!(to!int).array;
    auto B = readln.chomp.split(" ").map!(to!int).array;
    bool C() {
        int i = N-1;
        auto s = new RedBlackTree!(int, "a<b", true)();
        for (int j = N-1; j >= 0; j--) {
            if (j >= 1 && B[j] == B[j-1]) {
                s.insert(B[j]);
                continue;
            }
            if (i >= 0 && A[i] == B[j]) {
                i--;
            } else {
                if (A[i] in s) {
                    s.removeKey(A[i]);
                } else {
                    return false;
                }
            }
        }
        return true;
    }
    writeln(C() ? "YES" : "NO");
}

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

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

bool solve (int [] a, int [] b, int n)
{
	int [int] asks;
	for (int i = n - 1, j = n - 1; i > 0 || j > 0; )
	{
		if (j >= 0 && a[j] in asks && asks[a[j]] > 0)
		{
			asks[a[j]] -= 1;
			j--;
			continue;
		}
		if (a[j] == b[i])
		{
			i--;
			j--;
			continue;
		}
		if (i + 1 < n && b[i] == b[i + 1])
		{
			asks[b[i]] += 1;
			i--;
			continue;
		}
		return false;
	}
	return true;
}

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;
		auto b = readln.splitter.map !(to !(int)).array;
		writeln (solve (a, b, n) ? "YES" : "NO");
	}
}


import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio:freopen;
import 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;
@property pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow @property X binpow(X,Y)(X base,Y exp)
	if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow @property auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
	if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
@property void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
@property 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;}
	this(A,B)(auto ref pair!(A,B) p)
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(in pair!(X,Y) s_) const pure nothrow @safe
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow @property pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure @property nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure @property nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure @property nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
//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 m=new int[n];
		fill(m,int.max);
		foreach(i;0..10)
		{
			if((1<<i)>n)break;

			foreach(j;0..2)
			{
				int[] z;
				foreach(f;1..n+1)
				{
					if(((f>>i)&1)==j)z~=f;
				}
				writeln(z.length);
				putarr(z);

				stdout.flush;
				writeln;
				auto ans=arread!int;
				debug writeln(ans.length);
				foreach(p,y;ans)
				{
					debug writeln(p);
					if((((p+1)>>i)&1)!=j)m[p]=min(m[p],y);
				}
			}
		}
		writeln(-1);
		putarr(m);
		stdout.flush;
	}
}

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

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto ans = new int [n];
		ans[] = int.max;
		foreach (d; 0..10)
		{
			foreach (g; 0..2)
			{
				auto p = n.iota.filter
				    !(x => ((x >> d) & 1) == g).array;
				p[] += 1;
				if (p.length == 0)
				{
					continue;
				}
				writefln ("%s\n%(%s %)", p.length, p);
				stdout.flush ();
				auto cur = readln.split.map !(to !(int)).array;
				foreach (i; 0..n)
				{
					if ((i >> d & 1) != g)
					{
						ans[i] = min (ans[i], cur[i]);
					}
				}
			}
		}
		writeln (-1);
		writefln ("%(%s %)", ans);
		stdout.flush ();
	}
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio:freopen;
import 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;
@property pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow @property X binpow(X,Y)(X base,Y exp)
	if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow @property auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
	if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
@property void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
@property 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;}
	this(A,B)(auto ref pair!(A,B) p)
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(in pair!(X,Y) s_) const pure nothrow @safe
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow @property pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure @property nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure @property nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure @property nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
//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 m=new int[n];
		fill(m,int.max);
		foreach(i;0..10)
		{
			if((1<<i)>n)break;

			foreach(j;0..2)
			{
				int[] z;
				foreach(f;1..n+1)
				{
					if(((f>>i)&1)==j)z~=f;
				}
				writeln(z.length);
				putarr(z);

				stdout.flush;
				writeln;
				auto ans=arread!int;
				foreach(y,p;ans)
				{
					if((((p+1)>>i)&1)!=j)m[p]=min(m[p],y);
				}
			}
		}
		writeln(-1);
		putarr(m);
		stdout.flush;
	}
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,std.functional,std.math,std.numeric,std.string,std.range,std.complex,std.typecons,std.regex,
std.random,std.uni,std.traits,std.variant;
import core.stdc.stdio:freopen;
import 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;
@property pure nothrow T lcm (T)(auto ref T a,auto ref T b)
{
	return a/gcd(a,b)*b;
}
pure nothrow @property X binpow(X,Y)(X base,Y exp)
	if(is(typeof(exp&1)))
{
	if(exp<0)return X(0);
	X res=X(1);
	while(exp>0)
	{
		if(exp&1)res*=base;
		base*=base;
		exp>>=1;
	}
	return res;
}
pure nothrow @property auto binpow(X,Y,Z)(X base,Y exp,in Z mm)
	if(is(typeof(exp&1)))
{
	if(mm==0) return binpow(base,exp);
	if(exp<0)return X(0);
	auto res=X(1)%mm;
	while(exp>0)
	{
		if(exp&1)res=(res*base)%mm;
		base*=base;
		base%=mm;
		exp>>=1;
	}
	return res;
}
@property void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
@property 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;}
	this(A,B)(auto ref pair!(A,B) p)
	{
		fi=p.fi;
		se=p.se;
	}
	int opCmp(in pair!(X,Y) s_) const pure nothrow @safe
	{
		int cmp=0-(fi<s_.fi)+(fi>s_.fi);
		if(cmp!=0)return cmp;
		else return (0-(se<s_.se)+(se>s_.se));
	}
};
pair!(X,Y) mp(X,Y)(in X x_,in Y y_) pure nothrow @safe
{
	pair!(X,Y) pp;
	pp.fi=x_;
	pp.se=y_;
	return pp;
}
X sqr(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_;}
X cub(X)(in X a_) pure nothrow @property @safe @nogc {return a_*a_*a_;}
nothrow @nogc @system void inf(in char* name) {freopen(name,"r",core.stdc.stdio.stdin);}
nothrow @nogc @system void ouf(in char* name) {freopen(name,"w",core.stdc.stdio.stdout);}
auto arread(T)(){return readln.split.map!(to!(T)).array;}
nothrow @property pure size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (!(a[l]<g))?l:r;
}
pure @property nothrow size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(g<a[m])r=m;
		else l=m;
	}
	return (g<a[l])?l:r;
}
pure @property nothrow size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	size_t l=0,r=a.length;
	while(r-l>1)
	{
		auto m=(l+r)>>1;
		if(!(a[m]<g))r=m;
		else l=m;
	}
	return (g==a[l])?l:a.length;
}
pure @property nothrow bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
{
	return binf(a,g)!=a.length;
}
//split strip isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin cumulativeFold schwartzSort multiSort partialSort heapify join filter
//---------------------------------------------------------------program beginning-----------------------------------------------------------------

void main()
{
	int n;
	while(input(&n))
	{
		auto m=new int[n];
		fill(m,int.max);
		foreach(i;0..10)
		{
			if((1<<i)>n)break;

			foreach(j;0..2)
			{
				int[] z;
				foreach(f;1..n+1)
				{
					if(((f>>i)&1)==j)z~=f;
				}
				writeln(z.length);
				putarr(z);
				writeln;
				stdout.flush;
				auto ans=arread!int;
				foreach(y,p;ans)
				{
					if((((p+1)>>i)&1)!=j)m[p]=min(m[p],y);
				}
			}
		}
		writeln(-1);
		putarr(m);
		stdout.flush;
	}
}


import std.algorithm, std.array, std.range, std.stdio, std.typecons;
alias Point = Tuple !(int, q{x}, int, q{y});
long sp (Point a, Point b, Point c) {return (b.x - a.x) * 1L * (c.x - a.x) + (b.y - a.y) * 1L * (c.y - a.y);}
long vp (Point a, Point b, Point c) {return (b.x - a.x) * 1L * (c.y - a.y) - (b.y - a.y) * 1L * (c.x - a.x);}
void main () {
	int k, n, res;
	readf (" %s %s", &k, &n);
	auto a = new Point [k], b = new Point [n], s = new int [] [] [] (k, n);
	foreach (ref p; a) readf (" %s %s", &p.x, &p.y);
	foreach (ref q; b) readf (" %s %s", &q.x, &q.y);
	foreach (p; 0..k) foreach (q; 0..n) s[p][q] = n.iota.filter !(r => vp (b[r], a[p], b[q]) == 0 && sp (b[r], a[p], b[q]) < 0).take (k).array;
	bool go (int mask, int depth, int [] t) {
		if (t.empty) return true;
		if (depth < t.length) return false;
		foreach (p; 0..k) if (mask & (1 << p)) foreach (i, q; t) {
			auto next = t[0..i] ~ t[i + 1..$] ~ s[p][q];
			if (go (mask ^ (1 << p), depth - 1, next.sort ().uniq.array)) return true;
		}
		return false;
	}
	foreach (q; 0..n) res += go (-1, k, [q]);
	writeln (res);
}

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

alias Point = Tuple !(int, q{x}, int, q{y});

long sp (Point a, Point b, Point c)
{
	return (b.x - a.x) * 1L * (c.x - a.x) + (b.y - a.y) * 1L * (c.y - a.y);
}

long vp (Point a, Point b, Point c)
{
	return (b.x - a.x) * 1L * (c.y - a.y) - (b.y - a.y) * 1L * (c.x - a.x);
}

void main ()
{
	int k;
	int n;
	while (readf (" %s %s", &k, &n) > 0)
	{
		auto a = new Point [k];
		foreach (ref p; a)
		{
			readf (" %s %s", &p.x, &p.y);
		}
		auto b = new Point [n];
		foreach (ref q; b)
		{
			readf (" %s %s", &q.x, &q.y);
		}
		auto s = new int [] [] [] (k, n);
		foreach (p; 0..k)
		{
			foreach (q; 0..n)
			{
				s[p][q] = n.iota.filter
				    !(r => vp (b[r], a[p], b[q]) == 0 &&
				    sp (b[r], a[p], b[q]) < 0).take (k).array;
			}
		}

		bool go (int mask, int depth, int [] t)
		{
			if (t.empty)
			{
				return true;
			}
			if (depth < t.length)
			{
				return false;
			}
			foreach (p; 0..k)
			{
				if (mask & (1 << p))
				{
					foreach (i, q; t)
					{
						int [] next = t[0..i] ~
						    t[i + 1..$] ~ s[p][q];
						next = next.sort ().uniq.array;
						if (go (mask ^ (1 << p),
						    depth - 1,
						    next))
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		long res = 0;
		int m = (1 << k) - 1;
		foreach (q; 0..n)
		{
			res += k.iota.any
			    !(p => go (m ^ (1 << p), k - 1, s[p][q]));
		}

		writeln (res);
	}
}

import std.algorithm, std.array, std.range, std.stdio, std.typecons;
alias Point = Tuple !(int, q{x}, int, q{y});
long sp (Point a, Point b, Point c) {return (b.x - a.x) * 1L * (c.x - a.x) + (b.y - a.y) * 1L * (c.y - a.y);}
long vp (Point a, Point b, Point c) {return (b.x - a.x) * 1L * (c.y - a.y) - (b.y - a.y) * 1L * (c.x - a.x);}
void main () {
	int k, n, res;
	readf (" %s %s", &k, &n);
	auto a = new Point [k], b = new Point [n], s = new int [] [] [] (k, n);
	foreach (ref p; a) readf (" %s %s", &p.x, &p.y);
	foreach (ref q; b) readf (" %s %s", &q.x, &q.y);
	foreach (p; 0..k) foreach (q; 0..n) s[p][q] = n.iota.filter !(r => vp (b[r], a[p], b[q]) == 0 && sp (b[r], a[p], b[q]) < 0).take (k).array;
	bool go (int mask, int depth, int [] t) {
		if (t.empty) return true;
		if (depth < t.length) return false;
		foreach (p; 0..k) if (mask & (1 << p)) foreach (i, q; t) {
			auto next = t[0..i] ~ t[i + 1..$] ~ s[p][q];
			if (go (mask ^ (1 << p), depth - 1, next.sort ().uniq.array)) return true;
		}
		return false;
	}
	foreach (q; 0..n) res += k.iota.any !(p => go (-1, k, [p]));
	writeln (res);
}


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

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

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

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

  while (t--) {
    int n;
    read(n);
    auto arr = list!int;

    int[] pos = new int[2 * n + 2];
    pos.fill(-1);

    int total = 0;

    foreach (int i, e; arr) {
      int q = (2 * i + 1) / e;
      foreach (j; 1 .. q + 1) {
        if (pos[j] != -1 && j * e == i + 1 + pos[j]) {
          total++;
        }
      }

      pos[e] = i + 1;
    }

    writeln(total);
  }
}

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

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

while(t--) {
    int n = readln.chomp.to!int;
    long[] a = readln.split.map!(to!long).array;
    int[long] index;
    for(int i = 1; i <= n; ++i) {
        index[a[i - 1]] = i;
    }
    a.sort;
    long count = 0L;
    for(int i = 0; i < n; ++i) {
        for(int j = i + 1; j < n; ++j) {
            if(a[i] * a[j] > n * 2) {
                break;
            }
            if(a[i] * a[j] == index[a[i]] + index[a[j]]) {
                count += 1L;
            }
        }
    }
    count.writeln;
}
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!long).array;
    auto b = zip(a, iota(0L, n)).array.sort!((a, b) => a[0] < b[0]).array;
    long result = 0;
    foreach (i ; 0 .. b.length) {
      long maxaj = (4 * n + b[i][0] - 1) / b[i][0];
      foreach (j ; 0 .. b.length) {
        if (b[j][0] > maxaj)
          break;
        if (b[i][1] < b[j][1]) {
          long x = b[i][0] * b[j][0];
          long y = b[i][1] + 1 + b[j][1] + 1;
          if (x == y) {
            result++;
          }
        }
      }
    }
    writeln(result);
  }
}

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

void theCode(){
    int n = scan!int;
    auto arr = scanArray!int;
    auto ix = new int[2*n+1];
    ix[] = -1000_000_0;
    for(int i = 0; i < n; ++i){
        ix[arr[i]] = i+1;
    }
    int cnt = 0;
    for(int k = 2; k <= 2*n; ++k){
        for(int i = 1; i * i < k; ++i){
            if(k % i == 0 && ix[k/i] + ix[i] == k){
                ++cnt;
            }
        }
    }
    writeln(cnt);
}

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

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

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!long).array;
    auto b = zip(a, iota(0, n)).array.sort!((a, b) => a[0] < b[0]);
//    writeln(a);
//    writeln(b);
    size_t maxn = b.length;
    foreach (i ; 1 .. b.length) {
//      long x = b[i - 1][0];
//      long y = b[i - i][0];
//      writefln("%d * %d = %d", x, y, x * y);
      if (b[i - 1][0] * b[i][0] > 4 * n) {
        maxn = i + 1;
        break;
      }
    }
//    writeln(maxn);
    long result = 0;
    foreach (i ; 0 .. maxn) {
      foreach (j ; 0 .. maxn) {
        if (b[i][1] < b[j][1]) {
          long x = b[i][0] * b[j][0];
          long y = b[i][1] + 1 + b[j][1] + 1;
          if (x == y) {
            result++;
          }
        }
      }
    }
    writeln(result);
  }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!long).array;
    auto b = zip(a, iota(0, n)).array.sort!((a, b) => a[0] < b[0]);
//    writeln(a);
//    writeln(b);
    size_t maxn = a.length;
    foreach (i ; 1 .. a.length) {
      if (a[i - i] * a[i] > 4 * n) {
        maxn = i + 1;
        break;
      }
    }
    long result = 0;
    foreach (i ; 0 .. maxn) {
      foreach (j ; i + 1 .. maxn) {
        long x = b[i][0] * b[j][0];
        long y = b[i][1] + 1 + b[j][1] + 1;
        if (x == y) {
          result++;
        }
      }
    }
    writeln(result);
  }
}

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

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!long).array;
    auto b = zip(a, iota(0, n)).array.sort!((a, b) => a[0] < b[0]);
    writeln(a);
    writeln(b);
    size_t maxn = a.length;
    foreach (i ; 1 .. a.length) {
      if (a[i - i] * a[i] > 4 * n) {
        maxn = i + 1;
        break;
      }
    }
    long result = 0;
    foreach (i ; 0 .. maxn) {
      foreach (j ; i + 1 .. maxn) {
        long x = b[i][0] * b[j][0];
        long y = b[i][1] + 1 + b[j][1] + 1;
        if (x == y) {
          result++;
        }
      }
    }
    writeln(result);
  }
}


import std.conv, std.range, std.stdio, std.string;
void main () {
	foreach (test; 0..readln.strip.to !(int))
		1.repeat (readln.strip.to !(int)).writefln !("%(%s %)");
}

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

string FMT_F = "%.10f";

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

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

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

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

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

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

	stdout.flush;
	debug readln;
}

import std.algorithm;
import std.stdio;

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

    while(t--) {
        int n;
        readf("%s\n", &n);
        foreach(_; 0..n)
            writef("1 ");
        writeln;
    }
}