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;
		auto c1 = a.count (1);
		auto c2 = a.count (2);
		auto total = sum (a) + 1;

		auto s = new bool [total];
		s[2..$] = true;
		for (int d = 0; d * d < total; d++)
		{
			if (s[d])
			{
				for (int e = d * d; e < total; e += d)
				{
					s[e] = false;
				}
			}
		}

		int [] answer;
		int cur = 0;
		foreach (d; 0..total)
		{
			if (s[d])
			{
				while (cur + 2 <= d && c2 > 0)
				{
					cur += 2;
					c2 -= 1;
					answer ~= 2;
				}
				while (cur + 1 <= d && c1 > 0)
				{
					cur += 1;
					c1 -= 1;
					answer ~= 1;
				}
			}
		}
		while (c2 > 0)
		{
			c2 -= 1;
			answer ~= 2;
		}
		while (c1 > 0)
		{
			c1 -= 1;
			answer ~= 1;
		}

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

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

void solve(){
	int n = read.to!int;
	int[] as = readln.chomp.split.to!(int[]);

	int[] acount = [0, 0, 0];
	foreach(a; as) acount[a] += 1;

	int[] ans;
	if(acount[1] > 0 && acount[2] > 0){
		acount[2] -= 1, ans ~= 2;
		acount[1] -= 1, ans ~= 1;
	}
	while(acount[2] > 0){
		acount[2] -= 1, ans ~= 2;
	}
	while(acount[1] > 0){
		acount[1] -= 1, ans ~= 1;
	}

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

}

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

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

  int one = 0, two = 0;
  foreach (el; a) {
    if (el == 1) {
      one += 1;
    } else {
      two += 1;
    }
  }
  if (one == 0 || two == 0) {
    writefln("%(%s %)", a);
    return;
  }
  int[] b;
  b ~= [2, 1];
  foreach (_; 0 .. (two - 1)) {
    b ~= 2;
  }
  foreach (_; 0 .. (one - 1)) {
    b ~= 1;
  }
  writefln("%(%s %)", b);
}

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

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

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

alias Tree = RBT!int;

void main()
{
  GC.disable();

  uint n;
  readf(" %s\n", n);

  auto a = new int[n];
  foreach (i; 0 .. n)
    readf(" %s", a[i]);

  sort!"a>b"(a);
  if (a[$ - 1] == 1 && a[0] == 2)
  {
    writefln("2 1 %(%s %)", a[1 .. $ - 1]);
  }
  else
    writefln("%(%s %)", a);
}

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

string FMT_F = "%.10f";

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

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

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

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

	long[2] cnt;
	foreach (i; 0..N)
	{
		auto j = a[i] - 1;
		++cnt[j];
	}

	long x;
	if (cnt[1] != 0)
	{
		--cnt[1];
		x = 2;
		write(2);
	}
	else
	{
		--cnt[0];
		x = 1;
		write(1);
	}

	foreach (i; 1..N)
	{
		if (x % 2 == 1 && cnt[1] != 0)
		{
			--cnt[1];
			x += 2;
			write(" 2");
		}
		else if (cnt[0] != 0)
		{
			--cnt[0];
			x += 1;
			write(" 1");
		}
		else
		{
			--cnt[1];
			x += 2;
			write(" 2");
		}
	}

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

import std.stdio;
import std.array: array, split;
import std.algorithm: map, count;
import std.conv: to;

void main() {
	readln();
	int[] a = readln.split.map!(to!int).array;
	if (a.count(1) == 0 || a.count(2) == 0) {
		writefln("%(%s %)", a);
	} else {
		write("2 1 ");
		foreach (ulong i; 0..(a.count(2)-1)) {
			write("2 ");
		}
		foreach (ulong i; 0..(a.count(1)-1)) {
			write("1 ");
		}
		writeln();
	}
}

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

void main() {
    readln;
    int[] a = readln.split.map!(to!int).array;
    long ones = a.count(1);
    long twos = a.count(2);
    if(ones == 0) {
        foreach(two; 0..twos) {
            writefln("%s ", 2);
        }
        return;
    }
    if(twos == 0) {
        foreach(one; 0..ones) {
            writefln("%s ", 1);
        }
        return;
    }
    writef("2 1 ");
    ones -= 1;
    twos -= 1;
    for(long i = 0; i < twos; i++)
        writef("%s ", 2);
    for(long i = 0; i < ones; i++)
        writef("%s ", 1);
    writeln();
}

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

void main() {
  auto r = new InputReader;
  immutable n = r.next!int;
  auto a = r.nextA!int (n);
  int[3] c;
  foreach (i; a) {
    c[i]++;
  }
  int[] b;
  debug stderr.writeln (c);
  void o (int k) {
    if (c[k] > 0) {
      --c[k];
      b ~= k;
    }
  }
  o (2);
  o (1);
  while (c[2] > 0) {
    o (2);
  }
  while (c[1] > 0) {
    o (1);
  }
  writefln ("%(%s %)", b);
}

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

void main() {
    readln;
    int[] a = readln.split.map!(to!int).array;
    long ones = a.count(1);
    long twos = a.count(2);
    if(ones == 0)
        foreach(two; 0..twos) {
            writefln("%s ", 2);
            return;
        }
    if(twos == 0)
        foreach(one; 0..ones) {
            writefln("%s ", 1);
            return;
        }
    writef("2 1 ");
    ones -= 1;
    twos -= 1;
    foreach(two; 0..twos)
        writef("%s ", 2);
    foreach(one; 0..ones)
        writef("%s ", 1);
    writeln();
}

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

void main() {
  auto r = new InputReader;
  immutable n = r.next!int;
  auto a = r.nextA!int (n);
  int[3] c;
  foreach (i; a) {
    c[a[i]]++;
  }
  int[] b;
  void o (int k) {
    if (c[k] > 0) {
      --c[k];
      b ~= k;
    }
  }
  o (2);
  o (1);
  while (c[2] > 0) {
    o (2);
  }
  while (c[1] > 0) {
    o (1);
  }
  writefln ("%(%s %)", b);
}

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

alias Tree = RBT!int;

void main()
{
  GC.disable();

  uint n;
  readf(" %s\n", n);

  auto a = new int[n];
  foreach (i; 0 .. n)
    readf(" %s", a[i]);

  sort!"a>b"(a);
  if (a[$ - 1] == 1 && a[0] == 2)
  {
    writefln("2 1 %(%s %)", a[1 .. $ - 1]);
  }
  else
    writeln(a);
}

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

string FMT_F = "%.10f";

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

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

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

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

	long[2] cnt;
	foreach (i; 0..N)
	{
		auto j = a[i] - 1;
		++cnt[j];
	}

	if (cnt[1] != 0)
	{
		--cnt[1];
		write(2);
	}
	else
	{
		--cnt[0];
		write(1);
	}

	foreach (i; 1..N)
	{
		if (i % 2 == 0 && cnt[1] != 0)
		{
			--cnt[1];
			write(" 2");
		}
		else if (cnt[0] != 0)
		{
			--cnt[0];
			write(" 1");
		}
		else
		{
			--cnt[1];
			write(" 2");
		}
	}

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


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

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

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

    arr[] -= 1;

    auto ans = new int[] (n);
    auto vis = new int[] (n);
    void dfs(int v) {
        if (vis[v] == 2) return;

        if (arr[v] == v) {
            vis[v] = 2;
            ans[v] = v;
            return;
        }

        if (vis[v] == 1) {
            vis[v] = 2;
            ans[v] = v;
            dfs(arr[v]);
            return;
        }

        vis[v] = 1;
        dfs(arr[v]);
        if (vis[v] != 2) {
            vis[v] = 2;
            ans[v] = ans[arr[v]];
        }
    }

    foreach (i; 0 .. n) {
        dfs(i);
    }

    debug { ans.writeln; }

    ans[] += 1;

    ans.writefln!("%(%s %)");
}

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

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

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

    int[] ans;
    foreach (i; 0 .. n) {
        auto vis = new int[] (n);
        vis[] = false;

        int j = i;
        do {
            vis[j] = true;
            j = arr[j] - 1;
        } while (!vis[j]);

        ans ~= j;
    }

    ans[] += 1;

    ans.writefln!("%(%s %)");
}

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

  int n; rd(n);
  auto g=readln.split.to!(int[]).map!((e)=>(e-1)).array;

  int f(int i, bool[] vis){
    if(vis[i]) return i;
    vis[i]=true;
    return f(g[i], vis);
  }

  foreach(i; 0..n){
    auto vis=new bool[](n);
    writeln(f(i, vis)+1);
  }
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}

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

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

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

    arr[] -= 1;

    auto start = new bool[] (n);
    start[] = true;
    foreach (i, e; arr) {
        if (i != e) start[e] = false;
    }

    auto ans = new int[] (n);
    auto vis = new int[] (n);
    void dfs(int v) {
        if (vis[v] == 2) return;

        if (arr[v] == v) {
            vis[v] = 2;
            ans[v] = v;
            return;
        }

        if (vis[v] == 1) {
            vis[v] = 2;
            ans[v] = v;
            dfs(arr[v]);
            return;
        }

        vis[v] = 1;
        dfs(arr[v]);
        if (vis[v] != 2) {
            vis[v] = 2;
            ans[v] = ans[arr[v]];
        }
    }

    foreach (i; 0 .. n) {
        if (start[i]) {
            dfs(i);
        }
    }

    debug { ans.writeln; }

    ans[] += 1;

    ans.writefln!("%(%s %)");
}

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

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

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

    arr[] -= 1;

    auto start = new bool[] (n);
    start[] = true;
    foreach (i, e; arr) {
        if (i != e) start[e] = false;
    }

    auto ans = new int[] (n);
    auto vis = new int[] (n);
    int dfs(int v) {
        if (arr[v] == v) {
            vis[v] = 2;
            ans[v] = v;
            return v;
        }

        if (vis[v] == 1) {
            vis[v] = 2;
            ans[v] = v;
            dfs(arr[v]);
            return v;
        }

        if (vis[v] == 2) {
            return ans[v];
        }

        vis[v] = 1;
        ans[v] = dfs(arr[v]);
        vis[v] = 2;
        return ans[v];
    }

    foreach (i; 0 .. n) {
        if (start[i]) {
            dfs(i);
        }
    }

    ans[] += 1;

    ans.writefln!("%(%s %)");
}


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

void main ()
{
	int k;
	while (readf (" %s", &k) > 0)
	{
		int [long] boxByNum;
		long [] [] boxes;
		long [] boxSums;
		foreach (j; 0..k)
		{
			int len;
			readf (" %s", &len);
			auto line = readln.splitter.map !(to !(long)).array;
			foreach (ref c; line)
			{
				boxByNum[c] = j;
			}
			boxes ~= line;
			boxSums ~= line.sum;
		}

		long total = boxSums.sum;
		if (total % k != 0)
		{
			writeln ("No");
			continue;
		}
		long target = total / k;

		auto p2k = 1 << k;
		auto answer = new long [] [p2k];

		foreach (j; 0..k)
		{
			foreach (ref c; boxes[j])
			{
				long [] cur;
				cur.reserve (k + 1);
				int maskUsed = 0;
				int box = j;
				long num = c;
				bool ok = false;

				while (true)
				{
					maskUsed |= 1 << box;
					cur ~= num;

					long desired =
					    target - boxSums[box] + num;
					if (desired !in boxByNum)
					{
						break;
					}

					box = boxByNum[desired];
					num = desired;
					if (num == cur.front)
					{
						ok = true;
						break;
					}
					if (maskUsed & (1 << box))
					{
						break;
					}
				}

				if (ok)
				{
					answer[maskUsed] = cur.dup;
				}
			}
		}

		auto f = new int [p2k];
		f[0] = -1;
		for (int s = 0; s < p2k; s++)
		{
			for (int t = s; t > 0; t = (t - 1) & s)
			{
				if ((f[s ^ t] != 0) && (answer[t] !is null))
				{
					f[s] = t;
				}
			}
		}

		if (f[p2k - 1])
		{
			writeln ("Yes");
			auto numById = new long [k];
			auto toById = new int [k];
			int s = p2k - 1;
			while (s != 0)
			{
				auto t = f[s];
				foreach (i; 0..answer[t].length)
				{
					long num = answer[t][i];
					int box = boxByNum[num];
					long numNext = answer[t][(i + 1) % $];
					int boxNext = boxByNum[numNext];
					numById[boxNext] = numNext;
					toById[boxNext] = box;
				}
				s ^= t;
			}
			foreach (j; 0..k)
			{
				writeln (numById[j], " ", toById[j] + 1);
			}
		}
		else
		{
			writeln ("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.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 K = readInt();
      auto N = new int[K];
      auto A = new long[][K];
      foreach (u; 0 .. K) {
        N[u] = readInt();
        A[u] = new long[N[u]];
        foreach (j; 0 .. N[u]) {
          A[u][j] = readLong();
        }
      }

      Tuple!(int, "u", int, "j")[long] pos;
      foreach (u; 0 .. K) {
        foreach (j; 0 .. N[u]) {
          pos[A[u][j]] = tuple!("u", "j")(u, j);
        }
      }

      auto ss = new long[K];
      foreach (u; 0 .. K) {
        ss[u] = A[u].sum;
      }
      long s = ss.sum;
      if (s % K != 0) {
        writeln("No");
        continue;
      }
      s /= K;
      debug {
        writeln("s = ", s);
      }

      auto cycs = new Tuple!(int[], long[])[1 << K];
      foreach (t; 0 .. K) {
        foreach (i; 0 .. N[t]) {
          int used;
          int[] us;
          long[] as;
          for (int u = t, j = i; ; ) {
            used |= 1 << u;
            us ~= u;
            as ~= A[u][j];
            // ss[u] - A[u][j] + A[v][k] = s
            const a = s - ss[u] + A[u][j];
            if (!(a in pos)) {
              goto failed;
            }
            const vk = pos[a];
            const v = vk.u;
            const k = vk.j;
            if (used & 1 << v) {
              if (!(t == v && i == k)) {
                goto failed;
              }
              debug {
                writeln("cyc ", used, " ", us, " ", as);
              }
              cycs[used] = tuple(us, as);
              break;
            }
            u = v;
            j = k;
          }
         failed:
        }
      }

      auto prev = new int[1 << K];
      prev[0] = -1;
      foreach (p; 1 .. 1 << K) {
        for (int q = p; q; --q &= p) {
          if (cycs[q][0] && prev[p ^ q]) {
            prev[p] = q;
          }
        }
      }
      if (prev[$ - 1]) {
        auto ansC = new long[K];
        auto ansP = new int[K];
        for (int p = (1 << K) - 1; p; ) {
          const q = prev[p];
          const us = cycs[q][0];
          const as = cycs[q][1];
          const len = cast(int)(us.length);
          foreach (h; 0 .. len) {
            ansC[us[h]] = as[h];
            ansP[us[(h + 1) % len]] = us[h];
          }
          p ^= q;
        }
        writeln("Yes");
        foreach (u; 0 .. K) {
          writeln(ansC[u], " ", ansP[u] + 1);
        }
      } else {
        writeln("No");
      }
    }
  } catch (EOFException e) {
  }
}


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

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

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

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

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

string S;

void main(string[] args) {
	try {
	for (; ; ) {
		S = readToken;

		char[] cs = S.to!(char[]);
		char[] ans;
		foreach (x; "02468") {
			foreach (i, c; cs) if (c == x) {
				swap(cs[i], cs[$ - 1]);
				if (ans < cs) {
					ans = cs.dup;
				}
				swap(cs[i], cs[$ - 1]);
				break;
			}
			foreach_reverse (i, c; cs) if (c == x) {
				swap(cs[i], cs[$ - 1]);
				if (ans < cs) {
					ans = cs.dup;
				}
				swap(cs[i], cs[$ - 1]);
				break;
			}
		}
		if (ans == "") {
			writeln(-1);
		} else {
			writeln(ans);
		}

	}
	} catch (EOFException) {}
}

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

void main() {

	string snum = readln.strip;

	int nomore = -1;
	foreach (idx, sdigit; snum) {
		if ((sdigit - '0') % 2 == 0) {
			if (sdigit < snum[$ - 1]) {
				auto s = snum.dup;
				swap(s[idx], s[$ - 1]);
				writeln(s);
				return;
			}
			else
				nomore = idx;
		}
	}

	if (nomore != -1) {
		auto s = snum.dup;
		swap(s[nomore], s[$ - 1]);
		writeln(s);
	}
	else
		writeln("-1");
}

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

void main() {

	string snum = readln.strip;

	int nomore = -1;
	foreach (idx, sdigit; snum) {
		if ((sdigit - '0') % 2 == 0) {
			if (sdigit < snum[$ - 1]) {
				auto s = snum.dup;
				swap(s[idx], s[$ - 1]);
				writeln(s);
				return;
			}
			else
				nomore = idx;
		}
	}

	if (nomore != -1) {
		auto s = snum.dup;
		swap(s[nomore], s[$ - 1]);
		writeln(snum);
	}
	else
		writeln("-1");
}

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

void main() {

	string snum = readln.strip;

	string max = "0";
	foreach (idx, sdigit; snum) {
		if ((sdigit - '0') % 2 == 0) {
			auto s = snum.dup;
			swap(s[idx], s[$ - 1]);
			if (s > max)
				max = s.dup;
			if (s[idx] < s[$ - 1])
				break;
		}
	}

	writeln((max == "0") ? "-1" : max);
}


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

struct Point
{
	int x;
	int y;

	auto opCmp () (const auto ref Point that)
	{
		if (this.x != that.x)
		{
			return this.x - that.x;
		}
		return this.y - that.y;
	}

	Point opBinary (string op) (const auto ref Point that)
	    if (op == "+" || op == "-")
	{
		Point res;
		res.x = mixin ("this.x " ~ op ~ " that.x");
		res.y = mixin ("this.y " ~ op ~ " that.y");
		return res;
	}

	ref Point opOpAssign (string op) (const auto ref Point that)
	    if (op == "+" || op == "-")
	{
		mixin ("this.x " ~ op ~ "= that.x;");
		mixin ("this.y " ~ op ~ "= that.y;");
		return this;
	}
}

auto vp () (const auto ref Point a, const auto ref Point b)
{
	return a.x * 1L * b.y - a.y * 1L * b.x;
}

auto sp () (const auto ref Point a, const auto ref Point b)
{
	return a.x * 1L * b.x + a.y * 1L * b.y;
}

auto norm2 () (const auto ref Point a)
{
	return a.x * 1L * a.x + a.y * 1L * a.y;
}

Point [] getConvexHull (Point [] p)
{
	auto s = p.minElement;

	auto q = p.filter !(c => c != s).array;
	foreach (ref c; q)
	{
		c -= s;
	}

	q.sort !((a, b) => vp (a, b) < 0 ||
	    (vp (a, b) == 0 && norm2 (a) < norm2 (b)));

	Point [] res;
	res ~= Point (0, 0);
	res ~= q.front;
	q.popFront ();
	foreach (ref c; q)
	{
		while (res.length > 1 && vp (res[$ - 1] - res[$ - 2],
		    c - res[$ - 2]) >= 0)
		{
			res.length -= 1;
		}
		res.assumeSafeAppend ();
		res ~= c;
	}

	debug {writeln (res);}
	foreach (ref c; q)
	{
		c += s;
	}
	return res;
}

alias Record = Tuple !(long, q{sp}, long, q{norm2});

Record [] getSignature (Point [] p)
{
	auto h = getConvexHull (p);
	Record [] res;
	auto n = h.length;
	foreach (i; 0..n)
	{
		auto j = i + 1;
		if (j >= n)
		{
			j -= n;
		}
		auto k = j + 1;
		if (k >= n)
		{
			k -= n;
		}
		res ~= Record (sp (h[j] - h[i], h[k] - h[i]),
		    norm2 (h[j] - h[i]));
	}
	debug {writeln (res);}
	return res;
}

auto getPrefixFunction (R) (R r)
    if (isRandomAccessRange !(R))
{
	int [] p;
	p ~= -1;
	int k = -1;
	foreach (c; r)
	{
		while (k >= 0 && c != r[k])
		{
			k = p[k];
		}
		k += 1;
		p ~= k;
	}
	return p;
}

unittest
{
	assert (getPrefixFunction ("abacaba".representation) ==
	    [-1, 0, 0, 1, 0, 1, 2, 3]);
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto q = new Point [m];
		foreach (ref c; q)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto sigP = getSignature (p);
		auto sigQ = getSignature (q);
		auto ok = sigP.length == sigQ.length;
		if (ok)
		{
			auto pFun = getPrefixFunction (chain (sigP,
			    only (Record (0, 0)), sigQ, sigQ));
			ok = pFun.maxElement >= sigP.length;
		}
		writeln (ok ? "YES" : "NO");
	}
}

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

struct Point
{
	int x;
	int y;

	auto opCmp () (const auto ref Point that)
	{
		if (this.x != that.x)
		{
			return this.x - that.x;
		}
		return this.y - that.y;
	}

	Point opBinary (string op) (const auto ref Point that)
	    if (op == "+" || op == "-")
	{
		Point res;
		res.x = mixin ("this.x " ~ op ~ " that.x");
		res.y = mixin ("this.y " ~ op ~ " that.y");
		return res;
	}

	ref Point opOpAssign (string op) (const auto ref Point that)
	    if (op == "+" || op == "-")
	{
		mixin ("this.x " ~ op ~ "= that.x;");
		mixin ("this.y " ~ op ~ "= that.y;");
		return this;
	}
}

auto vp () (const auto ref Point a, const auto ref Point b)
{
	return a.x * 1L * b.y - a.y * 1L * b.x;
}

auto norm2 () (const auto ref Point a)
{
	return a.x * 1L * a.x + a.y * 1L * a.y;
}

Point [] getConvexHull (Point [] p)
{
	auto s = p.minElement;

	auto q = p.filter !(c => c != s).array;
	foreach (ref c; q)
	{
		c -= s;
	}

	q.sort !((a, b) => vp (a, b) < 0 ||
	    (vp (a, b) == 0 && norm2 (a) < norm2 (b)));

	Point [] res;
	res ~= Point (0, 0);
	res ~= q.front;
	q.popFront ();
	foreach (ref c; q)
	{
		while (res.length > 1 && vp (res[$ - 1] - res[$ - 2],
		    c - res[$ - 2]) >= 0)
		{
			res.length -= 1;
		}
		res.assumeSafeAppend ();
		res ~= c;
	}

	debug {writeln (res);}
	foreach (ref c; q)
	{
		c += s;
	}
	return res;
}

alias Record = Tuple !(long, q{vp}, long, q{norm2});

Record [] getSignature (Point [] p)
{
	auto h = getConvexHull (p);
	Record [] res;
	auto n = h.length;
	foreach (i; 0..n)
	{
		auto j = i + 1;
		if (j >= n)
		{
			j -= n;
		}
		auto k = j + 1;
		if (k >= n)
		{
			k -= n;
		}
		res ~= Record (vp (h[j] - h[i], h[k] - h[i]),
		    norm2 (h[j] - h[i]));
	}
	debug {writeln (res);}
	return res;
}

auto getPrefixFunction (R) (R r)
    if (isRandomAccessRange !(R))
{
	int [] p;
	p ~= -1;
	int k = -1;
	foreach (c; r)
	{
		while (k >= 0 && c != r[k])
		{
			k = p[k];
		}
		k += 1;
		p ~= k;
	}
	return p;
}

unittest
{
	assert (getPrefixFunction ("abacaba".representation) ==
	    [-1, 0, 0, 1, 0, 1, 2, 3]);
}

void main ()
{
	int n, m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto p = new Point [n];
		foreach (ref c; p)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto q = new Point [m];
		foreach (ref c; q)
		{
			readf (" %s %s", &c.x, &c.y);
		}
		auto sigP = getSignature (p);
		auto sigQ = getSignature (q);
		auto ok = sigP.length == sigQ.length;
		if (ok)
		{
			auto pFun = getPrefixFunction (chain (sigP,
			    only (Record (0, 0)), sigQ, sigQ));
			ok = pFun.maxElement >= sigP.length;
		}
		writeln (ok ? "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)); }

struct ModInt(int M_) {
  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 == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; 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 if (op == "-") { 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"); }
  string toString() const { return x.to!string; }
}

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

struct Point(T) {
  T x, y;
  Pt opBinary(string op)(Pt a) const {
    static if (op == "+") return Pt(x + a.x, y + a.y);
    else static if (op == "-") return Pt(x - a.x, y - a.y);
    else static assert(false);
  }
  Pt opBinary(string op)(T k) const {
    static if (op == "*") return Pt(x * k, y * k);
    else static if (op == "/") return Pt(x / k, y / k);
    else static assert(false);
  }
  T dot(Pt a) const { return x * a.x + y * a.y; }
  T det(Pt a) const { return x * a.y - y * a.x; }
  int opCmp(Pt a) const { return (x < a.x) ? -1 : (x > a.x) ? +1 : (y < a.y) ? -1 : (y > a.y) ? +1 : 0; }
  string toString() const { return "(" ~ x.to!string ~ ", " ~ y.to!string ~ ")"; }
}

alias Pt = Point!long;
long tri(Pt a, Pt b, Pt c) {
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

Pt[] convexHull(Pt[] ps) {
  auto qs = new Pt[ps.length + 1];
  ps.sort!((a, b) => ((a.x != b.x) ? (a.x < b.x) : (a.y < b.y)));
  int m = 0;
  foreach (p; ps) {
    for (; m > 1 && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  const r = m;
  foreach_reverse (p; ps) {
    for (; m > r && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  return qs[0 .. m - 1];
}

enum BASES = [20209, 1209, 21479];

void main() {
  try {
    for (; ; ) {
      auto N = new int[2];
      foreach (h; 0 .. 2) {
        N[h] = readInt();
      }
      auto P = new Pt[][2];
      foreach (h; 0 .. 2) {
        P[h] = new Pt[N[h]];
        foreach (i; 0 .. N[h]) {
          P[h][i].x = readLong();
          P[h][i].y = readLong();
        }
      }

      auto seqs = new long[][2];
      foreach (h; 0 .. 2) {
        const qs = convexHull(P[h]);
        const qsLen = cast(int)(qs.length);
        auto edges = new Pt[qsLen];
        foreach (j; 0 .. qsLen) {
          edges[j] = qs[(j + 1) % qsLen] - qs[j];
        }
        foreach (j; 0 .. qsLen) {
          seqs[h] ~= edges[j].dot(edges[j]);
          seqs[h] ~= edges[j].dot(edges[(j + 1) % qsLen]);
        }
        debug {
          writeln("P[h] = ", P);
          writeln("qs = ", qs);
          writeln("seqs[h] = ", seqs[h]);
        }
      }

      bool ans;
      const len = cast(int)(seqs[0].length);
      if (len == seqs[1].length) {
        auto bb = new Mint[][](BASES.length, len + 1);
        foreach (g; 0 .. BASES.length) {
          bb[g][0] = 1;
          foreach (j; 1 .. len + 1) {
            bb[g][j] = bb[g][j - 1] * BASES[g];
          }
        }
        auto ss = new Mint[][][](2, BASES.length, len + 1);
        foreach (h; 0 .. 2) foreach (g; 0 .. BASES.length) {
          foreach (j; 0 .. len) {
            ss[h][g][j + 1] = ss[h][g][j] + bb[g][j] * seqs[h][j];
          }
        }
        debug {
          writeln("ss = ", ss);
        }
        foreach (shift; 0 .. len) {
          if (shift % 2 == 0) {
            bool ok = true;
            foreach (g; 0 .. BASES.length) {
              ok = ok && ((bb[g][shift] * ss[0][g][len]).x == (ss[1][g][len] + (bb[g][len] - 1) * ss[1][g][shift]).x);
            }
            if (ok) {
              debug {
                writeln("shift = ", shift);
              }
              foreach (j; 0 .. len) {
                if (seqs[0][j] != seqs[1][(j + shift) % len]) {
                  ok = false;
                  break;
                }
              }
              if (ok) {
                ans = true;
                break;
              }
            }
          }
        }
      }
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

struct ModInt(int M_) {
  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 == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; 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 if (op == "-") { 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"); }
  string toString() const { return x.to!string; }
}

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

struct Point(T) {
  T x, y;
  Pt opBinary(string op)(Pt a) const {
    static if (op == "+") return Pt(x + a.x, y + a.y);
    else static if (op == "-") return Pt(x - a.x, y - a.y);
    else static assert(false);
  }
  Pt opBinary(string op)(T k) const {
    static if (op == "*") return Pt(x * k, y * k);
    else static if (op == "/") return Pt(x / k, y / k);
    else static assert(false);
  }
  T dot(Pt a) const { return x * a.x + y * a.y; }
  T det(Pt a) const { return x * a.y - y * a.x; }
  int opCmp(Pt a) const { return (x < a.x) ? -1 : (x > a.x) ? +1 : (y < a.y) ? -1 : (y > a.y) ? +1 : 0; }
  string toString() const { return "(" ~ x.to!string ~ ", " ~ y.to!string ~ ")"; }
}

alias Pt = Point!long;
long tri(Pt a, Pt b, Pt c) {
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

Pt[] convexHull(Pt[] ps) {
  auto qs = new Pt[ps.length + 1];
  ps.sort!((a, b) => (a.x < b.x));
  int m = 0;
  foreach (p; ps) {
    for (; m > 1 && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  const r = m;
  foreach_reverse (p; ps) {
    for (; m > r && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  return qs[0 .. m - 1];
}

enum BASES = [2020, 120, 2147];

void main() {
  try {
    for (; ; ) {
      auto N = new int[2];
      foreach (h; 0 .. 2) {
        N[h] = readInt();
      }
      auto P = new Pt[][2];
      foreach (h; 0 .. 2) {
        P[h] = new Pt[N[h]];
        foreach (i; 0 .. N[h]) {
          P[h][i].x = readLong();
          P[h][i].y = readLong();
        }
      }

      auto seqs = new long[][2];
      foreach (h; 0 .. 2) {
        const qs = convexHull(P[h]);
        const qsLen = cast(int)(qs.length);
        auto edges = new Pt[qsLen];
        foreach (j; 0 .. qsLen) {
          edges[j] = qs[(j + 1) % qsLen] - qs[j];
        }
        foreach (j; 0 .. qsLen) {
          seqs[h] ~= edges[j].dot(edges[j]);
          seqs[h] ~= edges[j].dot(edges[(j + 1) % qsLen]);
          seqs[h] ~= edges[j].det(edges[(j + 1) % qsLen]);
        }
        debug {
          writeln("qs = ", qs);
          writeln("seqs[h] = ", seqs[h]);
        }
      }

      bool ans;
      const len = cast(int)(seqs[0].length);
      if (len == seqs[1].length) {
        auto bb = new Mint[][](BASES.length, len + 1);
        foreach (g; 0 .. BASES.length) {
          bb[g][0] = 1;
          foreach (j; 1 .. len + 1) {
            bb[g][j] = bb[g][j - 1] * BASES[g];
          }
        }
        auto ss = new Mint[][][](2, BASES.length, len + 1);
        foreach (h; 0 .. 2) foreach (g; 0 .. BASES.length) {
          foreach (j; 0 .. len) {
            ss[h][g][j + 1] = ss[h][g][j] + bb[g][j] * seqs[h][j];
          }
        }
        debug {
          writeln("ss = ", ss);
        }
        foreach (shift; 0 .. len) {
          if (shift % 3 == 0) {
            bool ok = true;
            foreach (g; 0 .. BASES.length) {
              ok = ok && ((bb[g][shift] * ss[0][g][len]).x == (ss[1][g][len] + (bb[g][len] - 1) * ss[1][g][shift]).x);
            }
            if (ok) {
              debug {
                writeln("shift = ", shift);
              }
              ans = true;
            }
          }
        }
      }
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

enum LIM = 105;

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

      int readIt() {
        const str = readToken();
        int ret;
        foreach (i; 0 .. N) {
          ret |= (str[i] - '0') << i;
        }
        return ret;
      }

      auto S = new int[M];
      foreach (j; 0 .. M) {
        S[j] = readIt();
      }
      auto T = new int[Q];
      auto K = new int[Q];
      foreach (q; 0 .. Q) {
        T[q] = readIt();
        K[q] = readInt();
      }

      auto WSum = new int[1 << N];
      foreach (i; 0 .. N) {
        foreach (h; 0 .. 1 << i) {
          WSum[h | 1 << i] = WSum[h] + W[i];
        }
      }

      auto cnt = new int[1 << N];
      foreach (j; 0 .. M) {
        ++cnt[S[j]];
      }
      auto anss = new int[][](1 << N, LIM);
      foreach (t; 0 .. 1 << N) {
        foreach (s; 0 .. 1 << N) {
          const w = WSum[((1 << N) - 1) ^ s ^ t];
          if (w < LIM) {
            anss[t][w] += cnt[s];
          }
        }
        foreach (k; 1 .. LIM) {
          anss[t][k] += anss[t][k - 1];
        }
      }

      foreach (q; 0 .. Q) {
        writeln(anss[T[q]][K[q]]);
      }
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

struct ModInt(int M_) {
  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 == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; 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 if (op == "-") { 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"); }
  string toString() const { return x.to!string; }
}

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

struct Point(T) {
  T x, y;
  Pt opBinary(string op)(Pt a) const {
    static if (op == "+") return Pt(x + a.x, y + a.y);
    else static if (op == "-") return Pt(x - a.x, y - a.y);
    else static assert(false);
  }
  Pt opBinary(string op)(T k) const {
    static if (op == "*") return Pt(x * k, y * k);
    else static if (op == "/") return Pt(x / k, y / k);
    else static assert(false);
  }
  T dot(Pt a) const { return x * a.x + y * a.y; }
  T det(Pt a) const { return x * a.y - y * a.x; }
  int opCmp(Pt a) const { return (x < a.x) ? -1 : (x > a.x) ? +1 : (y < a.y) ? -1 : (y > a.y) ? +1 : 0; }
  string toString() const { return "(" ~ x.to!string ~ ", " ~ y.to!string ~ ")"; }
}

alias Pt = Point!long;
long tri(Pt a, Pt b, Pt c) {
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

Pt[] convexHull(Pt[] ps) {
  auto qs = new Pt[ps.length + 1];
  ps.sort!((a, b) => (a.x < b.x));
  int m = 0;
  foreach (p; ps) {
    for (; m > 1 && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  const r = m;
  foreach_reverse (p; ps) {
    for (; m > r && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  return qs[0 .. m - 1];
}

enum BASES = [20209, 1209, 21479];

void main() {
  try {
    for (; ; ) {
      auto N = new int[2];
      foreach (h; 0 .. 2) {
        N[h] = readInt();
      }
      auto P = new Pt[][2];
      foreach (h; 0 .. 2) {
        P[h] = new Pt[N[h]];
        foreach (i; 0 .. N[h]) {
          P[h][i].x = readLong();
          P[h][i].y = readLong();
        }
      }

      auto seqs = new long[][2];
      foreach (h; 0 .. 2) {
        const qs = convexHull(P[h]);
        const qsLen = cast(int)(qs.length);
        auto edges = new Pt[qsLen];
        foreach (j; 0 .. qsLen) {
          edges[j] = qs[(j + 1) % qsLen] - qs[j];
        }
        foreach (j; 0 .. qsLen) {
          seqs[h] ~= edges[j].dot(edges[j]);
          seqs[h] ~= edges[j].dot(edges[(j + 1) % qsLen]);
        }
        debug {
          writeln("qs = ", qs);
          writeln("seqs[h] = ", seqs[h]);
        }
      }

      bool ans;
      const len = cast(int)(seqs[0].length);
      if (len == seqs[1].length) {
        auto bb = new Mint[][](BASES.length, len + 1);
        foreach (g; 0 .. BASES.length) {
          bb[g][0] = 1;
          foreach (j; 1 .. len + 1) {
            bb[g][j] = bb[g][j - 1] * BASES[g];
          }
        }
        auto ss = new Mint[][][](2, BASES.length, len + 1);
        foreach (h; 0 .. 2) foreach (g; 0 .. BASES.length) {
          foreach (j; 0 .. len) {
            ss[h][g][j + 1] = ss[h][g][j] + bb[g][j] * seqs[h][j];
          }
        }
        debug {
          writeln("ss = ", ss);
        }
        foreach (shift; 0 .. len) {
          if (shift % 2 == 0) {
            bool ok = true;
            foreach (g; 0 .. BASES.length) {
              ok = ok && ((bb[g][shift] * ss[0][g][len]).x == (ss[1][g][len] + (bb[g][len] - 1) * ss[1][g][shift]).x);
            }
            if (ok) {
              debug {
                writeln("shift = ", shift);
              }
              foreach (j; 0 .. len) {
                ok = ok && (seqs[0][j] == seqs[1][(j + shift) % len]);
              }
              if (ok) {
                ans = true;
                debug {
                } else {
                  break;
                }
              }
            }
          }
        }
      }
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}

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

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

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

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

struct ModInt(int M_) {
  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 == "^^") {
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e; 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 if (op == "-") { 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"); }
  string toString() const { return x.to!string; }
}

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

struct Point(T) {
  T x, y;
  Pt opBinary(string op)(Pt a) const {
    static if (op == "+") return Pt(x + a.x, y + a.y);
    else static if (op == "-") return Pt(x - a.x, y - a.y);
    else static assert(false);
  }
  Pt opBinary(string op)(T k) const {
    static if (op == "*") return Pt(x * k, y * k);
    else static if (op == "/") return Pt(x / k, y / k);
    else static assert(false);
  }
  T dot(Pt a) const { return x * a.x + y * a.y; }
  T det(Pt a) const { return x * a.y - y * a.x; }
  int opCmp(Pt a) const { return (x < a.x) ? -1 : (x > a.x) ? +1 : (y < a.y) ? -1 : (y > a.y) ? +1 : 0; }
  string toString() const { return "(" ~ x.to!string ~ ", " ~ y.to!string ~ ")"; }
}

alias Pt = Point!long;
long tri(Pt a, Pt b, Pt c) {
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}

Pt[] convexHull(Pt[] ps) {
  auto qs = new Pt[ps.length + 1];
  ps.sort!((a, b) => (a.x < b.x));
  int m = 0;
  foreach (p; ps) {
    for (; m > 1 && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  const r = m;
  foreach_reverse (p; ps) {
    for (; m > r && sgn(tri(qs[m - 2], qs[m - 1], p)) <= 0; --m) {}
    qs[m++] = p;
  }
  return qs[0 .. m - 1];
}

enum BASES = [2020, 120, 2147];

void main() {
  try {
    for (; ; ) {
      auto N = new int[2];
      foreach (h; 0 .. 2) {
        N[h] = readInt();
      }
      auto P = new Pt[][2];
      foreach (h; 0 .. 2) {
        P[h] = new Pt[N[h]];
        foreach (i; 0 .. N[h]) {
          P[h][i].x = readLong();
          P[h][i].y = readLong();
        }
      }

      auto seqs = new long[][2];
      foreach (h; 0 .. 2) {
        const qs = convexHull(P[h]);
        const qsLen = cast(int)(qs.length);
        auto edges = new Pt[qsLen];
        foreach (j; 0 .. qsLen) {
          edges[j] = qs[(j + 1) % qsLen] - qs[j];
        }
        foreach (j; 0 .. qsLen) {
          seqs[h] ~= edges[j].dot(edges[j]);
          seqs[h] ~= edges[j].dot(edges[(j + 1) % qsLen]);
        }
        debug {
          writeln("qs = ", qs);
          writeln("seqs[h] = ", seqs[h]);
        }
      }

      bool ans;
      const len = cast(int)(seqs[0].length);
      if (len == seqs[1].length) {
        auto bb = new Mint[][](BASES.length, len + 1);
        foreach (g; 0 .. BASES.length) {
          bb[g][0] = 1;
          foreach (j; 1 .. len + 1) {
            bb[g][j] = bb[g][j - 1] * BASES[g];
          }
        }
        auto ss = new Mint[][][](2, BASES.length, len + 1);
        foreach (h; 0 .. 2) foreach (g; 0 .. BASES.length) {
          foreach (j; 0 .. len) {
            ss[h][g][j + 1] = ss[h][g][j] + bb[g][j] * seqs[h][j];
          }
        }
        debug {
          writeln("ss = ", ss);
        }
        foreach (shift; 0 .. len) {
          if (shift % 2 == 0) {
            bool ok = true;
            foreach (g; 0 .. BASES.length) {
              ok = ok && ((bb[g][shift] * ss[0][g][len]).x == (ss[1][g][len] + (bb[g][len] - 1) * ss[1][g][shift]).x);
            }
            if (ok) {
              debug {
                writeln("shift = ", shift);
              }
              ans = true;
            }
          }
        }
      }
      writeln(ans ? "YES" : "NO");
    }
  } catch (EOFException e) {
  }
}