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

void main ()
{
	int n;
	int m;
	while (readf (" %s %s", &n, &m) > 0)
	{
		auto a = new int [] [n];
		foreach (i; 0..m)
		{
			int u;
			int v;
			readf (" %s %s", &u, &v);
			u--;
			v--;
			a[u] ~= v;
			a[v] ~= u;
		}

		auto c = new int [n];

		void recur (int v, int cur)
		{
			debug {writeln (v, " ", cur);}
			if ((c[v] & cur) == cur)
			{
				return;
			}
			c[v] |= cur;
			foreach (u; a[v])
			{
				recur (u, cur ^ 3);
			}
		}

		foreach (i; 0..n)
		{
			if (c[i] == 0)
			{
				recur (i, 1);
			}
		}

		if (n.iota.any !(x => c[x] == 3))
		{
			writeln (-1);
		}
		else
		{
			foreach (i; 1..3)
			{
				auto ans =
				    n.iota.filter !(x => c[x] == i).array;
				writefln ("%s\n%(%s %)",
				    ans.length, ans.map !(x => x + 1));
			}
		}
	}
}

import std.stdio;

immutable int MAXN = cast(int)5e5;

int N, M;
int[][MAXN] G;
int[MAXN] d;
int[] A, B;
bool correct = 1;

void dfs(int v, int col) {
	d[v] = col;
	foreach(u; G[v]) {
		if (!d[u]) dfs(u, -col);
		else if (d[u] == d[v]) correct = 0;
	}
}

void print(int[] arr) {
	printf("%d\n", arr.length);
	foreach(i; arr) printf("%d ", i);
	printf("\n");
}

void main() {
	readf("%d %d\n", &N, &M);
	for (int i = 0; i < M; ++i) {
		int u, v; readf("%d %d\n", &u, &v);
		G[u] ~= v; G[v] ~= u;
	}
	for (int i = 1; i <= N; ++i) if (!d[i]) dfs(i, 1);
	if (!correct) {
		writef("-1\n"); return;
	}
	for (int i = 1; i <= N; ++i) (d[i] == 1 ? A : B) ~= i;
	print(A); print(B);
}

import std.stdio;
import std.container: DList;

int opposite_color(int c) {
    if (c == 1) return 2;
    return 1;
}

void main() {
    int n, m;
    readf("%d %d\n", &n, &m);
    int[][] graph;
    int[] colors;
    graph.length = n;
    colors.length = n;
    int first_colors = 0;
    int second_colors = 0;

    for(int i = 0; i < m; ++i) {
        int u, v;
        readf("%d %d\n", &u, &v);
        --u;
        --v;
        graph[u] ~= v;
        graph[v] ~= u;
    }
    bool possible = true;
outerLoop:
    foreach (int i, int color; colors) {
        if (color == 0  && graph[i].length != 0) {
            auto queue = DList!int();
            queue.insertBack(i);
            while (!queue.empty()) {
                int u = queue.front;
                queue.removeFront(1);
                foreach (int j, int v; graph[u]) {
                    if (colors[v] != 0 && colors[v] == colors[u]) {
                        possible = false;
                        break outerLoop;
                    } else if (colors[v] == 0) {
                        int opColor = opposite_color(colors[u]);
                        colors[v] = opColor;
                        if (opColor == 1) first_colors++;
                        else second_colors++;
                        queue.insertBack(v);
                    }
                }
            }
        }
    }

    if (possible) {
        writeln(first_colors);
        foreach(int i, int c; colors) {
            if(c == 1) {
                write(i+1, " ");
            }
        }
        writeln();
        writeln(second_colors);
        foreach(int i, int c; colors) {
            if(c == 2) {
                write(i+1, " ");
            }
        }
        writeln();
    } else {
        writeln(-1);
    }
}


import std.stdio : File, writeln, writefln;
import std.array;
import std.range;
import std.algorithm : min;

int n;
int[] arr;

void swap(T)(ref T a, ref T b){
	T tmp = a;
	a = b;
	b = tmp;
}

File ifp;
File ofp;

void main(){
	ifp = File("input.txt", "r");
	ofp = File("output.txt", "w");

	char[] l = ['B', 'G'];

	int n = next!int();
	int m = next!int();

	auto str = cycle(l[]).take(min(n, m)*2).array;

	if(n>m){
		ofp.write = str;
		ofp.write = repeat('B').take(n-m).array;
	}else{
		ofp.write = repeat('G').take(m-n).array;
		ofp.write = str;
	}

	ofp.writeln;
}

import std.stdio : readln, chomp;
import std.conv : to;
import std.string : split;
shared string[] input;
shared string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

void next(T)(ref T v){
	v = next!T();
}

bool hasNext(){
	if(input.length > 0){
		return true;
	}

//	string str = readln;
	string str = ifp.readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return true;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

string b="B",g="G";
void main(){
    stdin=File("input.txt","r");
    stdout=File("output.txt","w");
    int n,m;
    readf!"%d %d"(n,m);
    readln;
    if(m<n){
        swap(b,g);
        swap(n,m);
    }
    writeln((g~b).replicate(n)~g.replicate(m-n));
}

module cf_253A;

import std.stdio, std.algorithm;

void main() {
    File fin = File("input.txt", "r");
    File fout = File("output.txt", "w");
    int n, m;

    fin.readf("%d %d", &n, &m);

    string pair = (n > m)? "BG": "GB";
    for (int i = min(n, m); i > 0; --i) {
        fout.writef("%s", pair);
    }
    for (int i = n - min(n, m); i > 0; --i) {
        fout.writef("B");
    }
    for (int i = m - min(n, m); i > 0; --i) {
        fout.writef("G");
    }
    fout.writeln();
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

string b="B",g="G";
void main(){
    int n,m;
    scanf("%d %d",&n,&m);
    if(m<n){
        swap(b,g);
        swap(n,m);
    }
    writeln((g~b).replicate(n)~g.replicate(m-n));
}

//ahmat
import core.stdc.stdio:scanf,printf,puts;
import core.bitop;
import std.stdio;
import std.string;
import std.algorithm;
import std.functional;
import std.array;
import std.container;
import std.conv;
import std.container;
import std.typecons;
import std.math;
import std.numeric;
pragma(inline,true);

string b="B",g="G";
void main(){
    stdin=File("input.txt","r");
    stdout=File("output.txt","w");
    int n,m;
    scanf("%d %d",&n,&m);
    if(m<n){
        swap(b,g);
        swap(n,m);
    }
    writeln((g~b).replicate(n)~g.replicate(m-n));
}

module cf_253A;

import std.stdio, std.algorithm;

void main() {
    int n, m;

    readf("%d %d", &n, &m);

    string pair = (n > m)? "BG": "GB";
    for (int i = min(n, m); i > 0; --i) {
        writef("%s", pair);
    }
    for (int i = n - min(n, m); i > 0; --i) {
        writef("B");
    }
    for (int i = m - min(n, m); i > 0; --i) {
        writef("G");
    }
    writeln();
}

module cf_253A;

import std.stdio, std.algorithm;

void main() {
    int n, m;

    readf("%d %d", &n, &m);

    string pair = (n > m)? "BG": "GB";
    for (int i = min(n, m); i > 0; --i) {
        writef("%s", pair);
    }
    for (int i = n - min(n, m); i > 0; --i) {
        writef("B");
    }
    for (int i = m - min(n, m); i > 0; --i) {
        writef("G");
    }
}


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

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

void solve(){
	int n = rint;
	S[] as = rlong(n).map!(v => S(v, 1)).array;

	auto sta = new Stack!S;
	foreach(a; as){
		while(sta.length > 0 && sta.peek.gt(a)){
			S b = sta.pop;
			a = S(b.sum + a.sum, b.count + a.count);
		}
		sta.push(a);
	}

	S[] ans = sta.array;
	foreach(an; ans){
		foreach(i; 0 .. an.count) writefln("%10.10f", an.sum.to!real / an.count.to!real);
	}

}

struct S{
	long sum;
	int count;
	bool lt(S b){ return sum * b.count < b.sum * count; }
	bool gt(S b){ return sum * b.count > b.sum * count; }
	bool le(S b){ return !gt(b); }
	bool ge(S b){ return !lt(b); }
}

// ----- スタック -----
class Stack(T){
	private T[] xs;
	private uint j; // j : 次に書き込む位置
	this(){	}
	this(T[] xs){ this.xs = xs; j = xs.length.to!uint; }
	uint length(){ return j; }
	bool isEmpty(){ return j == 0; }
	void push(T x){
		while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1;
		xs[j ++] = x;
	}
	T pop(){ assert(j > 0); return xs[-- j]; }
	T peek(){ assert(j > 0); return xs[j - 1]; }
	T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; }
	static Stack!T opCall(){ return new Stack!T; }
	static Stack!T opCall(T[] xs){ return new Stack!T(xs); }
	T[] array(){ return xs[0 .. j]; }
	override string toString(){ return array.to!string; }
}

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

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

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

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

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

      int len;
      auto numers = new long[N];
      auto denoms = new long[N];
      foreach_reverse (i; 0 .. N) {
        long p = A[i], q = 1;
        for (; len >= 1 && numers[len - 1] * q <= p * denoms[len - 1]; ) {
          p += numers[len - 1];
          q += denoms[len - 1];
          --len;
        }
        numers[len] = p;
        denoms[len] = q;
        ++len;
      }
      foreach_reverse (j; 0 .. len) {
        const ans = cast(real)(numers[j]) / denoms[j];
        foreach (_; 0 .. denoms[j]) {
          writefln("%.10f", ans);
        }
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	int n;
	while (readf !(" %s") (n) > 0)
	{
		readln;
		auto a = readln.splitter.map !(to !(int)).array;
		auto s = [0L];
		s.reserve (n + 1);
		foreach (ref c; a)
		{
			s ~= s.back + c;
		}

		auto p = [0, 1];
		foreach (i; 2..n + 1)
		{
			while (p.length > 1 &&
			    (s[i] - s[p[$ - 1]]) * 1L * (i - p[$ - 2]) <=
			    (s[i] - s[p[$ - 2]]) * 1L * (i - p[$ - 1]))
			{
				p.popBack ();
				p.assumeSafeAppend ();
			}
			p ~= i;
		}

		auto answer = new real [n];
		foreach (j; 1..p.length)
		{
			real cur = s[p[j]] - s[p[j - 1]];
			cur /= p[j] - p[j - 1];
			foreach (i; p[j - 1]..p[j])
			{
				answer[i] = cur;
			}
		}
		foreach (const ref c; answer)
		{
			writefln ("%.10f", c);
		}
	}
}


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

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

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

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

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        auto P = new long[N];
        foreach (i; 0 .. N) {
          P[i] = readLong();
        }
        const X = readLong();
        const A = readInt();
        const Y = readLong();
        const B = readInt();
        const K = readLong();

        sort(P);
        reverse(P);

        bool check(int l) {
          auto zs = new long[l];
          foreach (i; 0 .. l) {
            if ((i + 1) % A == 0) {
              zs[i] += X;
            }
            if ((i + 1) % B == 0) {
              zs[i] += Y;
            }
          }
          sort(zs);
          reverse(zs);
          long sum;
          foreach (i; 0 .. l) {
            sum += P[i] / 100 * zs[i];
          }
          return (sum >= K);
        }

        int lo = -1, hi = N + 1;
        for (; lo + 1 < hi; ) {
          const mid = (lo + hi) / 2;
          check(mid) ? (hi = mid) : (lo = mid);
        }
        writeln((hi <= N) ? hi : -1);
      }
    }
  } catch (EOFException e) {
  }
}

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

void main ()
{
	int tests;
	readf (" %s", &tests);
	foreach (test; 0..tests)
	{
		int n;
		readf (" %s", &n);
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		sort (p);
		reverse (p);
		p[] /= 100;
		int x, a;
		readf (" %s %s", &x, &a);
		int y, b;
		readf (" %s %s", &y, &b);
		long k;
		readf (" %s", &k);

		int solve ()
		{
			int lo = 1;
			int hi = n + 1;
			while (lo < hi)
			{
				int me = (lo + hi) / 2;
				auto q = p;
				long total = 0;
				for (int i = 1; i <= me; i++)
				{
					if (i % a == 0 && i % b == 0)
					{
						total += q.front * (x + y);
						q.popFront ();
					}
				}
				for (int i = 1; i <= me; i++)
				{
					if (i % a == 0 && i % b != 0)
					{
						total += q.front * (x + 0);
						q.popFront ();
					}
				}
				for (int i = 1; i <= me; i++)
				{
					if (i % a != 0 && i % b == 0)
					{
						total += q.front * (0 + y);
						q.popFront ();
					}
				}
				if (total >= k)
				{
					hi = me;
				}
				else
				{
					lo = me + 1;
				}
			}
			return lo;
		}

		auto res = solve ();
		swap (x, y);
		swap (a, b);
		res = min (res, solve ());
		if (res == n + 1)
		{
			res = -1;
		}
		writeln (res);
	}
}


import std.container, std.range, std.stdio, std.typecons;

alias Piece = Tuple !(int, q{a}, int, q{b}, int, q{l}, int, q{u});

void main ()
{
	int n, q;
	while (readf (" %s %s", &n, &q) > 0)
	{
		auto s = redBlackTree !(Piece) ();
		s.insert (Piece (1, n, 0, 0));
		foreach (k; 0..q)
		{
			int x, y;
			char t;
			readf (" %s %s %s", &x, &y, &t);
			auto cur = Piece (x, n + 1, 0, 0);
			auto r = s.lowerBound (cur);
			auto prev = r.empty ? Piece.init : r.back;
			int c = x;
			with (prev)
			{
				if (c < a || b < c)
				{
					writeln (0);
				}
				else if (t == 'L')
				{
					s.removeKey (prev);
					writeln (x - l);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, y));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    l, u));
					}
				}
				else if (t == 'U')
				{
					s.removeKey (prev);
					writeln (y - u);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, u));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    x, u));
					}
				}
				else
				{
					assert (false);
				}
			}
		}
	}
}

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 Piece = Tuple !(int, q{a}, int, q{b}, int, q{l}, int, q{u});

void main ()
{
	int n;
	int q;
	while (readf (" %s %s", &n, &q) > 0)
	{
		auto s = redBlackTree !(Piece) ();
		s.insert (Piece (1, n, 0, 0));
		foreach (k; 0..q)
		{
			int x;
			int y;
			char t;
			readf (" %s %s %s", &x, &y, &t);
			auto cur = Piece (x, n + 1, 0, 0);
			auto r = s.lowerBound (cur);
			auto prev = r.empty ? Piece.init : r.back;
			int c = x;
			with (prev)
			{
				if (c < a || b < c)
				{
					writeln (0);
				}
				else if (t == 'L')
				{
					s.removeKey (prev);
					writeln (x - l);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, y));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    l, u));
					}
				}
				else if (t == 'U')
				{
					s.removeKey (prev);
					writeln (y - u);
					if (a <= c - 1)
					{
						s.insert (Piece (a, c - 1,
						    l, u));
					}
					if (c + 1 <= b)
					{
						s.insert (Piece (c + 1, b,
						    x, u));
					}
				}
				else
				{
					assert (false);
				}
			}
			debug {writeln (s[].map !(text).join ("\n"));}
		}
	}
}


import std.stdio;
import std.math;
import std.numeric;
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;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

void main() {
    int N, M; readf("%d %d\n", &N, &M);
    auto A = readarray!int;
    long ans = 0;
    for (long i = 0; i <= N - 1; i++) {
        ans += (i + N - 1) * (N - i) / 2L - i * (N - i) + (N - i);
    }

    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            long l = i + 1;
            long r = N - i - 1;
            ans -= l * r;
        }
    }

    foreach (q; 0 .. M) {
        int i, x; readf("%d %d\n", &i, &x);
        i--;
        if (i - 1 >= 0) {
            if (A[i - 1] != A[i] && A[i - 1] == x) {
                long lc = i;
                long rc = N - i;
                ans -= lc * rc;
            }
            if (A[i - 1] == A[i] && A[i - 1] != x) {
                long lc = i;
                long rc = N - i;
                ans += lc * rc;
            }
        }
        if (i + 1 < N) {
            if (A[i + 1] != A[i] && A[i + 1] == x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans -= lc * rc;
            }
            if (A[i + 1] == A[i] && A[i + 1] != x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans += lc * rc;
            }
        }
        A[i] = x;
        writeln(ans);
    }
}

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

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto A = readln.chomp.split(" ").map!(to!long).array;

    long f(long N) {
        long x = N * (N + 1) / 2;
        long y = N * (N + 1) * (2 * N + 1) / 6;
        return N * x + x - y;
    }

    long T = f(N);
    long D = 0;
    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            D += cast(long)(i + 1) * cast(long)(N - (i + 1));
        }
    }
    long Y = T - D; // initial

    long prod(long i) {
        return i * (cast(long)N - i);
    }
    long diff(long x, long y) {
        if (x == y) return 0L;
        return 1L;
    }

    for (int q = 0; q < M; q++) {
        long d = 0;
        int I, X; scanf("%d %d\n", &I, &X);

        if (I - 2 >= 0) {
            d += (diff(A[I-2], X) - diff(A[I-2], A[I-1])) * prod(I-1);
        }
        if (I < N) {
            d += (diff(X, A[I]) - diff(A[I-1], A[I])) * prod(I);
        }

        Y += d;
        writeln(Y);

        A[I - 1] = X;
    }
}

import std.stdio;
import std.math;
import std.numeric;
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;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

void main() {
    int N, M; readf("%d %d\n", &N, &M);
    auto A = readarray!int;
    long ans = 0;
    for (long i = 0; i <= N - 1; i++) {
        ans += (i + N - 1) * (N - i) / 2L - i * (N - i) + (N - i);
    }

    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            long l = i + 1;
            long r = N - i - 1;
            ans -= l * r;
        }
    }

    foreach (q; 0 .. M) {
        int i, x; readf("%d %d\n", &i, &x);
        i--;
        if (A[i] == x) continue;
        if (i - 1 >= 0) {
            if (A[i - 1] != A[i] && A[i - 1] == x) {
                long lc = i;
                long rc = N - i;
                ans -= lc * rc;
            }
            if (A[i - 1] == A[i] && A[i - 1] != x) {
                long lc = i;
                long rc = N - i;
                ans += lc * rc;
            }
        }
        if (i + 1 < N) {
            if (A[i + 1] != A[i] && A[i + 1] == x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans -= lc * rc;
            }
            if (A[i + 1] == A[i] && A[i + 1] != x) {
                long lc = i + 1;
                long rc = N - i - 1;
                ans += lc * rc;
            }
        }
        A[i] = x;
        writeln(ans);
    }
}

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

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto A = readln.chomp.split(" ").map!(to!long).array;

    long f(long N) {
        long x = N * (N + 1) / 2;
        long y = N * (N + 1) * (2 * N + 1) / 6;
        return N * x + x - y;
    }

    long T = f(N);
    long D = 0;
    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            D += cast(long)(i + 1) * cast(long)(N - (i + 1));
        }
    }
    long Y = T - D; // initial

    long prod(long i) {
        return i * (N - i);
    }
    long diff(long x, long y) {
        if (x == y) return 0;
        return 1;
    }

    for (int q = 0; q < M; q++) {
        long d = 0;
        int I, X; scanf("%d %d\n", &I, &X);
        if (A[I - 1] == X) continue; // nothing changed

        if (I - 2 >= 0) {
            d += (diff(A[I-2], X) - diff(A[I-2], A[I-1])) * prod(I-1);
        }
        if (I < N) {
            d += (diff(X, A[I]) - diff(A[I-1], A[I])) * prod(I);
        }

        Y += d;
        writeln(Y);

        A[I - 1] = X;
    }
}

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

const bool dbg = true;
void DBG(alias x)() { static if (dbg) stderr.writefln("DBG: %s = %s", x.stringof, x); }
void MSG(in string x) { static if (dbg) stderr.writefln("MSG: %s", x); }

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto A = readln.chomp.split(" ").map!(to!long).array;

    long f(long N) {
        long x = N * (N + 1) / 2;
        long y = N * (N + 1) * (2 * N + 1) / 6;
        return N * x + x - y;
    }

    long T = f(N);
    long D = 0;
    for (int i = 0; i + 1 < N; i++) {
        if (A[i] == A[i + 1]) {
            D += (i + 1) * (N - (i + 1));
        }
    }
    long Y = T - D; // initial

    long prod(long i) {
        return i * (N - i);
    }
    long diff(long x, long y) {
        if (x == y) return 0;
        return 1;
    }

    for (int q = 0; q < M; q++) {
        long d = 0;
        int I, X; scanf("%d %d\n", &I, &X);
        if (A[I - 1] == X) continue; // nothing changed

        if (I - 2 >= 0) {
            d += (diff(A[I-2], X) - diff(A[I-2], A[I-1])) * prod(I-1);
        }
        if (I < N) {
            d += (diff(X, A[I]) - diff(A[I-1], A[I])) * prod(I);
        }

        Y += d;
        writeln(Y);

        A[I - 1] = X;
    }
}


import std.stdio;

T read(T)()
{
    T t;
    readf(" %s", &t);
    return t;
}

int main(string[] args)
{
    auto n = read!int;
    bool a[100_001];
    a[] = false;
    int curr = n;

    for (int i = 0; i < n; i++)
    {
        auto q = read!int;
        a[q] = true;
        if (q == curr)
        {
            while (a[curr])
            {
                write(curr, " ");
                curr--;
            }
        }
        writeln;
    }

    return 0;
}

import std.stdio;
import std.container : BinaryHeap;

int main(string[] args)
{
	int n; readf(" %d", n);

	int[] h = new int[n];
	auto bh = BinaryHeap!(int[])(h, 0);

	int cur_expected = n;

	foreach (i; 0..n){
		int num; readf(" %d", num);
		bh.insert(num);

		while(!bh.empty() && bh.front() == cur_expected){
			writef("%d ", bh.front());
			bh.popFront();
			cur_expected--;
		}

		writeln();
	}

	return 0;
}


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

struct node {
    int seqlen;
    int unleft;
    int unright;
    this(int sl, int ul, int ur) {
        seqlen = sl;
        unleft = ul;
        unright = ur;
    }
}

__gshared {
    node[] segtree;
    string parens;
}

node SegTreeConstruct(int sb, int se, int si) {
    if (sb == se) {
        if (parens[sb] == '(') segtree[si] = node(0, 1, 0);
        if (parens[sb] == ')') segtree[si] = node(0, 0, 1);
        return segtree[si];
    }

    int mid = (sb + se) / 2;
    node nl = SegTreeConstruct(sb, mid, si * 2 + 1);
    node nr = SegTreeConstruct(mid + 1, se, si * 2 + 2);
    int m = min(nl.unleft, nr.unright);
    segtree[si] = node(nl.seqlen + nr.seqlen + 2 * m,
                       nl.unleft + nr.unleft - m,
                       nl.unright + nr.unright - m);
    return segtree[si];
}

node SegTreeRead(int sb, int se, int qb, int qe, int si) {
    if (qb <= sb && qe >= se)
        return segtree[si];

    if (se < qb || sb > qe)
        return node(0, 0, 0);

    int mid = (sb + se) / 2;
    node nl = SegTreeRead(sb, mid, qb, qe, 2 * si + 1);
    node nr = SegTreeRead(mid + 1, se, qb, qe, 2 * si + 2);
    int m = min(nl.unleft, nr.unright);
    return node(nl.seqlen + nr.seqlen + 2 * m,
                nl.unleft + nr.unleft - m,
                nl.unright + nr.unright - m);
}

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

    readf(" %s\n", &parens);
    segtree = new node[parens.length * 4];
    SegTreeConstruct(0, parens.length - 1, 0);

    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        int left, right;
        readf(" %s %s\n", &left, &right);
        node nd = SegTreeRead(0, parens.length - 1, left - 1, right - 1, 0);
        writeln(nd.seqlen);
    }

    return 0;
}

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

struct node {
    int seqlen;
    int unleft;
    int unright;
    this(int sl, int ul, int ur) {
        seqlen = sl;
        unleft = ul;
        unright = ur;
    }
}

node[] segtree;
string parens;

node SegTreeConstruct(int sb, int se, int si) {
    if (sb == se) {
        if (parens[sb] == '(') segtree[si] = node(0, 1, 0);
        if (parens[sb] == ')') segtree[si] = node(0, 0, 1);
        return segtree[si];
    }

    int mid = (sb + se) / 2;
    node nl = SegTreeConstruct(sb, mid, si * 2 + 1);
    node nr = SegTreeConstruct(mid + 1, se, si * 2 + 2);
    int m = min(nl.unleft, nr.unright);
    segtree[si] = node(nl.seqlen + nr.seqlen + 2 * m,
                       nl.unleft + nr.unleft - m,
                       nl.unright + nr.unright - m);
    return segtree[si];
}

node SegTreeRead(int sb, int se, int qb, int qe, int si) {
    if (qb <= sb && qe >= se)
        return segtree[si];

    if (se < qb || sb > qe)
        return node(0, 0, 0);

    int mid = (sb + se) / 2;
    node nl = SegTreeRead(sb, mid, qb, qe, 2 * si + 1);
    node nr = SegTreeRead(mid + 1, se, qb, qe, 2 * si + 2);
    int m = min(nl.unleft, nr.unright);
    return node(nl.seqlen + nr.seqlen + 2 * m,
                nl.unleft + nr.unleft - m,
                nl.unright + nr.unright - m);
}

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

    readf(" %s\n", &parens);
    segtree = new node[parens.length * 4];
    SegTreeConstruct(0, parens.length - 1, 0);

    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        int left, right;
        readf(" %s %s\n", &left, &right);
        node nd = SegTreeRead(0, parens.length - 1, left - 1, right - 1, 0);
        writeln(nd.seqlen);
    }

    return 0;
}

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

struct node {
    int seqlen;
    int unleft;
    int unright;
    this(int sl, int ul, int ur) {
        seqlen = sl;
        unleft = ul;
        unright = ur;
    }
}

__gshared {
    node[] segtree;
    string parens;
}

node SegTreeConstruct(int sb, int se, int si) {
    if (sb == se) {
        if (parens[sb] == '(') segtree[si] = node(0, 1, 0);
        if (parens[sb] == ')') segtree[si] = node(0, 0, 1);
        return segtree[si];
    }

    int mid = (sb + se) / 2;
    node nl = SegTreeConstruct(sb, mid, si * 2);
    node nr = SegTreeConstruct(mid + 1, se, si * 2 + 1);
    int m = min(nl.unleft, nr.unright);
    segtree[si] = node(nl.seqlen + nr.seqlen + 2 * m,
                       nl.unleft + nr.unleft - m,
                       nl.unright + nr.unright - m);
    return segtree[si];
}

node SegTreeRead(int sb, int se, int qb, int qe, int si) {
    if (qb <= sb && qe >= se)
        return segtree[si];

    if (se < qb || sb > qe)
        return node(0, 0, 0);

    int mid = (sb + se) / 2;
    node nl = SegTreeRead(sb, mid, qb, qe, 2 * si);
    node nr = SegTreeRead(mid + 1, se, qb, qe, 2 * si + 1);
    int m = min(nl.unleft, nr.unright);
    return node(nl.seqlen + nr.seqlen + 2 * m,
                nl.unleft + nr.unleft - m,
                nl.unright + nr.unright - m);
}

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

    readf(" %s\n", &parens);
    segtree = new node[parens.length * 4];
    SegTreeConstruct(0, parens.length - 1, 1);

    int n;
    readf(" %s\n", &n);
    foreach (i; 0 .. n) {
        int left, right;
        readf(" %s %s\n", &left, &right);
        node nd = SegTreeRead(0, parens.length - 1, left - 1, right - 1, 1);
        writeln(nd.seqlen);
    }

    return 0;
}

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

struct query {
    int index;
    int l;
    int r;
    int answer;
}

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

    string paren;
    readf(" %s\n", &paren);
    int n;
    readf(" %s\n", &n);
    query[] qarr = new query[n];
    foreach (i; 0 .. n) {
        query q;
        readf(" %s %s\n", &q.l, &q.r);
        --q.l;
        --q.r;
        q.index = i;
        qarr[i] = q;
    }
    int[1000000] posarr = -1;
    int[] pstack;
    foreach (int i, char c; paren) {
        if (c == '(') pstack ~= i;
        else if (!pstack.empty) {
            posarr[i] = pstack.back;
            posarr[pstack.back] = i;
            pstack.popBack();
        }
    }
    qarr.sort!"a.r < b.r";
    int pl = 0, pr = 0, answer = 0;
    foreach (ref q; qarr) {
        for ( ; pr < q.r + 1; ++pr) {
            if (posarr[pr] >=0 && posarr[pr] < pr && pl <= posarr[pr]) answer += 2;
        }
        if (pl < q.l) {
            for ( ; pl < q.l; ++pl) {
                if (posarr[pl] > pl && pr >= posarr[pl]) answer -= 2;
            }
        }
        else while (pl > q.l) {
            --pl;
            if (posarr[pl] > pl && pr >= posarr[pl]) answer += 2;
        }
        q.answer = answer;
    }
    qarr.sort!"a.index < b.index";
    foreach (q; qarr) writeln(q.answer);

    return 0;
}


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 d = RD!string;
		auto e = new long[](n);
		foreach (j, c; d)
		{
			e[j] = [c].to!long;
		}
		auto index = new size_t[](e.length);
		makeIndex!("a < b", SwapStrategy.stable)(e, index);
		size_t[] a1 = [index[0]], a2;
		bool ok = true;
		foreach (j; index[1..$])
		{
			if (a2.empty)
			{
				if (j >= a1.back)
					a1 ~= j;
				else
					a2 ~= j;
			}
			else
			{
				if (j >= a1.back && e[j] == e[a2.front])
					a1 ~= j;
				else if (j >= a2.back)
					a2 ~= j;
				else
				{
					ok = false;
					break;
				}
			}
		}
		debug writeln(a1, a2);
		if (ok)
		{
			ans[i] = new char[](n);
			if (a2.empty)
			{
				foreach (j; 0..n) ans[i][j] = '1';
			}
			else
			{
				foreach (j; a1)
				{
					ans[i][j] = '1';
				}
				foreach (j; a2)
				{
					ans[i][j] = '2';
				}
			}
		}
		else
			ans[i] = "-".dup;
	}

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

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 rtype(T)(){ return read.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtype!real;
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
	int t = rint;

	A:
	foreach(_; 0 .. t){

		int n = rint;
		char[] cs = read.to!(char[]);

		int[] as;
		foreach(c; cs) as ~= ("" ~ c).to!int;

		string ans = "-";
		foreach(k; 0 .. 9){
			int u = 0, v = k;
			int isOK = 1;
			string tempans = "";
			foreach(a; as){
				if(a >= v){
					tempans ~= "2";
					if(a > v) v = a;
				}
				else if(a <= k && a >= u){
					tempans ~= "1";
					if(a > u) u = a;
				}
				else{
					isOK = 0;
					break;
				}
				log("cs:", cs, "k:", k, "a:", a, "u:", u, "v:", v, "tempans:", tempans);
			}
			if(isOK){
				ans = tempans;
				break;
			}
		}
		ans.writeln;
	}
}

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

	A:
	foreach(_; 0 .. t){

		int n = rint;
		char[] cs = read.to!(char[]);

		int a = 0, b = 0;
		string ans = "";
		foreach(c; cs){
			int x = ("" ~ c).to!int;
			if(b <= c) b = c, ans ~= "2";
			else if(a <= c) a = c, ans ~= "1";
			else{
				"-".writeln;
				continue A;
			}
		}
		ans.writeln;
	}
}

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

string FMT_F = "%.10f";

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new char[][](t);
	foreach (i; 0..t)
	{
		auto n = RD!int;
		auto d = RD!string;
		auto e = new long[](n);
		foreach (j, c; d)
		{
			e[j] = [c].to!long;
		}
		auto index = new size_t[](e.length);
		makeIndex!("a < b", SwapStrategy.stable)(e, index);
		size_t[] a1 = [index[0]], a2;
		bool ok = true;
		foreach (j; index[1..$])
		{
			if (j >= a1.back)
				a1 ~= j;
			else if (a2.empty)
				a2 ~= j;
			else if (j >= a2.back)
				a2 ~= j;
			else
			{
				ok = false;
				break;
			}
		}
		debug writeln(a1, a2);
		if (ok)
		{
			ans[i] = new char[](n);
			if (a2.empty)
			{
				foreach (j; 0..n) ans[i][j] = '1';
			}
			else
			{
				foreach (j; a1)
				{
					ans[i][j] = '1';
				}
				foreach (j; a2)
				{
					ans[i][j] = '2';
				}
			}
		}
		else
			ans[i] = "-".dup;
	}

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


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

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

string FMT_F = "%.10f";

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

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

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

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

void main()
{
	auto t = RD!int;
	auto ans = new int[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		foreach (i; 0..n)
		{
			if (a[i] % 2 == 0)
			{
				ans[ti] = [i+1];
				break;
			}
		}
		if (!ans[ti].empty) continue;
		foreach (i; 0..n)
		{
			if (a[i] % 2)
			{
				ans[ti] ~= i+1;
			}
		}
		if (ans[ti].length == 1)
			ans[ti].length = 0;
		else
			ans[ti] = ans[ti][0..2];
	}

	foreach (e; ans)
	{
		if (e.empty)
			writeln(-1);
		else
		{
			writeln(e.length);
			e.map!(to!string).join(" ").writeln;
		}
	}
	stdout.flush;
	debug readln;
}


import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;

struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}

Input input;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
struct ModNum(T)
{
  T representative, modulus;
  this(T representative, T modulus)
  {
    this.representative = representative;
    this.modulus = modulus;
  }
  T minimalPositiveRepresentative()
  {
    return ((representative % modulus) + modulus) % modulus;
  }
  T minimalNegativeRepresentative()
  {
    return ((representative % modulus) - modulus) % modulus;
  }
  T maxRepresentative(T upperBound)
  {
    return upperBound - pmod(upperBound - representative, modulus);
  }
  T minRepresentative(T lowerBound)
  {
    return lowerBound + pmod(representative - lowerBound, modulus);
  }
}
// minimum possible representative of x modulo m.
T pmod(T)(T x, T m)
{
  return (x % m + m) % m;
}
// solves ax + by = gcd(a, b) and returns gcd(a, b)
T extendedEuclideanAlgorithm(T)(T a, T b, out T x, out T y)
{
  if (a == 0)
    {
      x = 0, y = 1;
      return b;
    }
  T x1, y1;
  auto d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1, y = x1;
  return d;
}
alias egcd = extendedEuclideanAlgorithm;
// solves ax = b (mod m) giving a solution as a modulus class.
bool solveLinearCongruence(T)(T a, T b, T m, out ModNum!T res)
{
  T x, p;
  b = pmod(b, m);
  T y;
  T g = egcd(a, m, x, y);
  if (b % g != 0)
    return false;
  p = m / g;
  x = pmod(x * b / g, p);
  res.representative = x;
  res.modulus = p;
  return true;
}

void main()
{
  // a - x >= 0
  // b - y - 2x >= 0
  // b - 2y >= 0

  alias ln = int;
  ln t; get(t);
  while(t--)
    {
      ln a, b, c; get(a, b, c);
      ln mx = 0;
      for(ln x = 0; x <= a; x++)
	{
	  for(ln y = 0; c - 2 * y >= 0; y++)
	    {
	      if ( b - y - 2 * x >= 0)
		{
		  mx = max(mx, x + y + 2 * x + 2 * y);
		}
	    }
	}
      wr(mx);    }
}

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 long[](t);
	foreach (i; 0..t)
	{
		auto a = RD;
		auto b = RD;
		auto c = RD;
		auto cnt1 = min(c/2, b);
		auto cnt2 = min((b-cnt1)/2, a);
		ans[i] = (cnt1+cnt2) * 3;
	}

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

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;

struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}

Input input;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
struct ModNum(T)
{
  T representative, modulus;
  this(T representative, T modulus)
  {
    this.representative = representative;
    this.modulus = modulus;
  }
  T minimalPositiveRepresentative()
  {
    return ((representative % modulus) + modulus) % modulus;
  }
  T minimalNegativeRepresentative()
  {
    return ((representative % modulus) - modulus) % modulus;
  }
  T maxRepresentative(T upperBound)
  {
    return upperBound - pmod(upperBound - representative, modulus);
  }
  T minRepresentative(T lowerBound)
  {
    return lowerBound + pmod(representative - lowerBound, modulus);
  }
}
// minimum possible representative of x modulo m.
T pmod(T)(T x, T m)
{
  return (x % m + m) % m;
}
// solves ax + by = gcd(a, b) and returns gcd(a, b)
T extendedEuclideanAlgorithm(T)(T a, T b, out T x, out T y)
{
  if (a == 0)
    {
      x = 0, y = 1;
      return b;
    }
  T x1, y1;
  auto d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1, y = x1;
  return d;
}
alias egcd = extendedEuclideanAlgorithm;
// solves ax = b (mod m) giving a solution as a modulus class.
bool solveLinearCongruence(T)(T a, T b, T m, out ModNum!T res)
{
  T x, p;
  b = pmod(b, m);
  T y;
  T g = egcd(a, m, x, y);
  if (b % g != 0)
    return false;
  p = m / g;
  x = pmod(x * b / g, p);
  res.representative = x;
  res.modulus = p;
  return true;
}

void main()
{
  // a - x >= 0
  // b - y - 2x >= 0
  // b - 2y >= 0

  alias ln = int;
  ln t; get(t);
  while(t--)
    {
      ln a, b, c; get(a, b, c);
      ln mx = 0;
      for(ln x = 0; x <= a; x++)
	{
	  for(ln y = 0; 2 * y <= b; y++)
	    {
	      if ( b - y - 2 * x >= 0 && c - 2 * y >= 0)
		{
		  mx = max(mx, x + y + 2 * x + 2 * y);
		}
	    }
	}
      wr(mx);    }
}

import std.algorithm;
import std.range;
import std.stdio;
import std.typecons;
import std.traits;
import std.array;

struct Input
{
  T next(T)()
  {
    import std.conv;
    return to!T(nextWord);
  }
  string nextWord()
  {
    if (_nextWords.empty)
	_nextWords.insertFront(readln().split);
    string word = _nextWords.front; _nextWords.removeFront;
    return word;
  }
  import std.container;
  DList!string _nextWords;
}

Input input;

void read(T...)(ref T args)
{
  import std.traits;
  static foreach(i; 0 .. T.length)
    static if(isArray!(T[i]) && !is(T[i] == string))
      foreach(ref e; args[i])
    	read(e);
    else static if(isTuple!(T[i]))
      static foreach(n; 0 .. T[i].Types.length)
	read(args[i][n]);
    else
      args[i] = input.next!(typeof(args[i]))();
}
auto brange(alias low, alias high, alias pred, bool value)()
{
  return iota(low, high + 1).map!((a) => cast(int) pred(a)).assumeSorted.equalRange(value);
}
auto ftrue(alias low, alias high, alias pred)()
{
  return brange!(low, high, pred, false).length + low;
}
auto itup(alias k, alias F)()
{
  alias T = Parameters!F;
  foreach(i; 0 .. k)
    {
      T t;
      get(t);
      F(t);
    }
}
alias get = read;
void wone(T)(T t, char end)
{
  static if(isArray!T && !is(T == string))
    {
      foreach(i; 0 .. t.length - 1)
	  write(t[i], ' ');
      write(t[$ - 1], end);
    }
  else
      write(t, end);
}
void wr(T...)(T t)
{
  static foreach(i; 0 .. T.length)
    static if(i + 1 < T.length)
      wone(t[i], ' ');
    else
      wone(t[i], '\n');
}
void ans(T...)(T t)
{
  import core.stdc.stdlib;
  wr(t);
  exit(0);
}
struct ModNum(T)
{
  T representative, modulus;
  this(T representative, T modulus)
  {
    this.representative = representative;
    this.modulus = modulus;
  }
  T minimalPositiveRepresentative()
  {
    return ((representative % modulus) + modulus) % modulus;
  }
  T minimalNegativeRepresentative()
  {
    return ((representative % modulus) - modulus) % modulus;
  }
  T maxRepresentative(T upperBound)
  {
    return upperBound - pmod(upperBound - representative, modulus);
  }
  T minRepresentative(T lowerBound)
  {
    return lowerBound + pmod(representative - lowerBound, modulus);
  }
}
// minimum possible representative of x modulo m.
T pmod(T)(T x, T m)
{
  return (x % m + m) % m;
}
// solves ax + by = gcd(a, b) and returns gcd(a, b)
T extendedEuclideanAlgorithm(T)(T a, T b, out T x, out T y)
{
  if (a == 0)
    {
      x = 0, y = 1;
      return b;
    }
  T x1, y1;
  auto d = egcd(b % a, a, x1, y1);
  x = y1 - (b / a) * x1, y = x1;
  return d;
}
alias egcd = extendedEuclideanAlgorithm;
// solves ax = b (mod m) giving a solution as a modulus class.
bool solveLinearCongruence(T)(T a, T b, T m, out ModNum!T res)
{
  T x, p;
  b = pmod(b, m);
  T y;
  T g = egcd(a, m, x, y);
  if (b % g != 0)
    return false;
  p = m / g;
  x = pmod(x * b / g, p);
  res.representative = x;
  res.modulus = p;
  return true;
}

void main()
{
  // a - x >= 0
  // b - y - 2x >= 0
  // b - 2y >= 0

  alias ln = int;
  ln t; get(t);
  while(t--)
    {
      ln a, b, c; get(a, b, c);
      ln mx = 0;
      for(ln x = 0; x <= a; x++)
	{
	  for(ln y = 0; 2 * y <= b; y++)
	    {
	      if ( b - y - 2 * x >= 0 && b - 2 * y >= 0)
		{
		  mx = max(mx, x + y + 2 * x + 2 * y);
		}
	    }
	}
      wr(mx);
    }
}


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 A = 2050L;

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readLong();

        long ans;
        if (N % A == 0) {
          for (long m = N / A; m > 0; m /= 10) {
            ans += m % 10;
          }
        } else {
          ans = -1;
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

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

immutable int base = 2050;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(long);
		if (n % base != 0)
		{
			writeln (-1);
		}
		else
		{
			n /= base;
			writeln (n.text.map !(q{a - '0'}).sum);
		}
	}
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);

        if(d % 2050 != 0)
        {
            writeln(-1);
        }
        else
        {
            long m = d / 2050;
            long sum = 0;
            while(m  > 0)
            {
                sum += (m%10);
                m /= 10;
            }

            writeln(sum);
        }
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        real d = to!real(param);
        if(d < 2050)
        {
         writeln(-1);
         continue;
        }
        real m = 2050 * pow(10, ceil(log10(d))-4);
        //writeln(m);
        int c = 0;

        while(d > m)
        {
            d -= m;
            c++;

            if(m > d)
                m /= 10;
        }

        if(d < m)
            writeln(-1);
        else
            writeln(c+1);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    outer : foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        while(m*10 <= d)
        {
            m = m*10;
        }

        int c = 0;
        while(d >= m)
        {
            d -= m;
            c++;

             if(m > d && m >= 10)
                m /= 10;
        }

        if(d != 0)
        writeln(-1);
        else writeln(c);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    outer : foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        while(m*10 <= d)
        {
            m = m*10;
        }

        int c = 0;
        while(d >= m)
        {
            d -= m;
            c++;

             if(m > d && m > 10)
                m /= 10;
        }

        if(d != 0)
        writeln(-1);
        else writeln(c);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        if(d < m || d % m != 0)
        {
            writeln(-1);
            continue;
        }

        long q = d / m;

        while(m*10 <= d)
        {
            m = m*10;
        }

        int c = 0;
        while(d > 0)
        {
            d -= m;
            c++;
             if(m > d)
                m /= 10;

        }

        writeln(c);
    }
}

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

void main()
{
    auto n= to!long(readln().chomp);
    foreach(_; 0..n)
    {
        auto param = readln().chomp;
        long d = to!long(param);
        long m = 2050;

        if(d < m || d % m != 0)
        {
            writeln(-1);
            continue;
        }

        long q = d / m;

        while(m*10 < d)
        {
            m = m*10;
        }

        int c = 0;
        while(d > 0)
        {
            d -= m;
            c++;
             if(m > d)
                m /= 10;

        }

        writeln(c);
    }
}


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;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		int [] [] moves;
		foreach (i; 0..2)
		{
			int [] line;
			int k;
			readf (" %s", &k);
			foreach (j; 0..k)
			{
				int v;
				readf (" %s", &v);
				line ~= n - v;
			}
			moves ~= line;
		}

		auto g = new int [] [] (2, n);
		auto c = new int [] [] (2, n);

		alias Pair = Tuple !(int, q{who}, int, q{pos});
		Pair [] q;

		void add (Pair p, int v)
		{
			g[p.who][p.pos] = v;
			c[p.who][p.pos] = n;
			q ~= p;
		}

		add (Pair (0, 0), -1);
		add (Pair (1, 0), -1);

		while (!q.empty)
		{
			auto who = q.front.who;
			auto pos = q.front.pos;
			auto res = g[who][pos];
			q.popFront ();
			q.assumeSafeAppend ();

			foreach (d; moves[!who])
			{
				int next = (pos + d) % n;
				if (g[!who][next] != 0)
				{
					continue;
				}
				if (res == -1)
				{
					add (Pair (!who, next), +1);
				}
				else
				{
					c[!who][next] += 1;
					if (c[!who][next] >=
					    moves[!who].length)
					{
						add (Pair (!who, next), -1);
					}
				}
			}
		}

		foreach (line; g)
		{
			line.drop (1).map !(x =>
			    ["Lose", "Loop", "Win"][x + 1]).join (" ").writeln;
		}
	}
}

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

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

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

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

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

      auto dp = new int[][](2, N);
      auto cnt = new int[][](2, N);
      foreach (t; 0 .. 2) foreach (u; 0 .. N) {
        dp[t][u] = -1;
        cnt[t][u] = K[t];
      }

      auto que = new int[2 * N * 2];
      int qb, qe;
      foreach (t; 0 .. 2) {
        dp[t][0] = 0;
        que[qe++] = t;
        que[qe++] = 0;
      }

      for (; qb != qe; ) {
        const t = que[qb++];
        const x = que[qb++];
        foreach (s; S[t ^ 1]) {
          const tt = t ^ 1;
          const xx = (x - s < 0) ? (x - s + N) : (x - s);
          if (dp[tt][xx] == -1) {
            if (dp[t][x] & 1) {
              if (--cnt[tt][xx] > 0) {
                continue;
              }
            }
            dp[tt][xx] = dp[t][x] + 1;
            que[qe++] = tt;
            que[qe++] = xx;
          }
        }
      }

      foreach (t; 0 .. 2) {
        foreach (x; 1 .. N) {
          if (x > 1) write(" ");
          write((dp[t][x] == -1) ? "Loop" : (dp[t][x] & 1) ? "Win" : "Lose");
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}


module cf_159b;

import std.stdio;
import std.algorithm;

void main() {
  immutable MAX_VALUE = 1000;

  int n, m, a, b;
  int[MAX_VALUE + 1][MAX_VALUE + 1] pairCount;
  int[MAX_VALUE + 1] diamCount;
  int closed = 0, nicelyClosed = 0;

  readf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    readf(" %d %d", &a, &b);
    ++pairCount[a][b];
    ++diamCount[b];
  }
  for (int i = 0; i < m; ++i) {
    readf(" %d %d", &a, &b);

    if (pairCount[a][b] > 0) {
      ++nicelyClosed;
      --pairCount[a][b];
    }
    if (diamCount[b] > 0) {
      ++closed;
      --diamCount[b];
    }
  }

  writeln(closed, " ", nicelyClosed);
}

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

immutable int INF = 10L ^^ 9 + 23;

void main()
{
    auto ns = readln.chomp.split.map!(to!int).array;

    auto vals = new int[][][] (2, 1001, 1001);
    foreach (i; 0 .. 2) {
        foreach (_; 0 .. ns[i]) {
            int x, y;
            readf("%s %s", &x, &y);
            readln;
            vals[i][y][x] += 1;
        }
    }

    int u = 0, v = 0;
    foreach (d; 1 .. 1001) {
        int mleft = 0, cleft = 0;
        int ok = 0;
        foreach (c; 1 .. 1001) {
            ok += min(vals[0][d][c], vals[1][d][c]);
            if (vals[0][d][c] > vals[1][d][c]) mleft += vals[0][d][c] - vals[1][d][c];
            else cleft += vals[1][d][c] - vals[0][d][c];
        }

        u += ok;
        v += ok;

        u += min(mleft, cleft);
    }

    writeln(u, ' ', v);
}

module cf_159b;

import std.stdio;
import std.algorithm;

void main() {
  immutable MAX_VALUE = 1000;

  int n, m, a, b;
  int[MAX_VALUE + 1][MAX_VALUE + 1] pairCount;
  int[MAX_VALUE + 1] diamCount;
  int closed = 0, nicelyClosed = 0;

  readf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    readf(" %d %d", &a, &b);
    ++pairCount[a][b];
    ++diamCount[b];
  }
  for (int i = 0; i < m; ++i) {
    readf(" %d %d", &a, &b);

    if (pairCount[a][b] > 0) {
      ++nicelyClosed;
    }
    if (diamCount[b] > 0) {
      ++closed;
    }
  }

  writeln(closed, " ", nicelyClosed);
}

module cf_159b;

import std.stdio;
import std.algorithm;

void main() {
  int n, m;
  int[2][] markers, caps;

  readf("%d %d", &n, &m);
  markers = new int[2][n];
  caps = new int[2][m];
  for (int i = 0; i < n; ++i) {
    readf(" %d %d", &markers[i][0], &markers[i][1]);
  }
  for (int i = 0; i < m; ++i) {
    readf(" %d %d", &caps[i][0], &caps[i][1]);
  }

  sort!((a, b) { return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]; })(markers);
  sort!((a, b) { return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]; })(caps);

  int nicelyClosed = 0;
  for (int i = 0; i < n; ++i) {
    int l = 0, r = caps.length - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      bool leftCondition = markers[i][0] > caps[mid][0] ||
    markers[i][0] == caps[mid][0] && markers[i][1] > caps[mid][1];
      if (leftCondition) {
    l = mid + 1;
      } else {
    r = mid;
      }
    }

    bool cond = markers[i][0] == caps[r][0] && markers[i][1] == caps[r][1];
    if (cond) {
      ++nicelyClosed;
    }
  }

  sort!((a, b) { return a[1] < b[1]; })(markers);
  sort!((a, b) { return a[1] < b[1]; })(caps);

  int closed = 0;
  for (int i = 0; i < n; ++i) {
    int l = 0, r = caps.length - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      if (markers[i][1] > caps[mid][1]) {
    l = mid + 1;
      } else {
    r = mid;
      }
    }

    bool cond = markers[i][1] == caps[r][1];
    if (cond) {
      ++closed;
    }
  }

  writeln(closed, " ", nicelyClosed);
}


import std.stdio;

void main ()
{
	int tests;
	readf (" %s", &tests);
	foreach (test; 0..tests)
	{
		int n;
		readf (" %s", &n);
		long res = n * 1L * (n + 1) / 2;
		for (int p = 1; p <= n; p <<= 1)
		{
			res -= p * 2;
		}
		writeln (res);
	}
}

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

struct IO {
        string[] tk;
        string readString() {
                while (tk.empty)
                        tk = readln.split;
                auto tkt = tk.front;
                tk.popFront;
                return tkt;
        }
        int readInt() {
                return readString.to!int;
        }
        double readDouble() {
                return readString.to!double;
        }
}

void main() {
        IO cin;
        int t = 1;
        t = cin.readInt;
        while (t--) {
                long n = cin.readString.to!long;
                long sum = (n * (n + 1)) / 2;
                int num = 0;
                for (int i = 1; num <= n; i++) {
                        num = 1 << i;
                        sum -= num;
                }
                writeln(sum);
        }
}

module main;

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

immutable int MAX_LOG = 31;
int[MAX_LOG] powers;
long[MAX_LOG] prefixes;

long specialSum( int n ) {

//    debug {writeln((1L + n) * n / 2);
//            writeln(cast (int)(trunc(log2(cast (double)(n)))));
//            writeln(prefixes[cast (int)(trunc(log2(cast (double)(n))))]);};
    return (1L + n) * n / 2 - 2 *
            prefixes[cast (int)(trunc(log2(cast (double)(n))))];
}

int main() {

    auto t = readln().strip().to! (int);
    long answers[];

    powers[0] = 1;
    foreach (int i; 1..powers.length)
        powers[i] = powers[i - 1] * 2;
    prefixes[0] = 1;
    foreach (int i; 1..prefixes.length)
        prefixes[i] = prefixes[i - 1] + powers[i];

    // debug {writeln(powers, '\n', prefixes);};

    foreach (int i; 0..t) {
        auto n = readln().strip().to! (int);

        answers ~= specialSum(n);
    }

    foreach (i; answers)
        writeln(i);

	return 0;
}


// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable lim = 2 * 10^^5 + 7;
immutable inf = 2 * 10^^9 + 7;
alias Query = Tuple!(int, "l", int, "type", int, "r", int, "c");
int n, x;

void main() {
    scan(n, x);

    auto t = new Query[](2*n);

    int li, ri, ci;

    foreach (i ; 0 .. n) {
        scan(li, ri, ci);
        t[2*i] = Query(li, 0, ri, ci);
        t[2*i + 1] = Query(ri, 1, li, ci);
    }

    t.sort();

    auto minc = new int[](lim);
    minc[] = inf;

    int ans = inf;

    foreach (i ; 0 .. 2*n) {
        if (t[i].type == 0) {
            int d = t[i].r - t[i].l + 1;
            if (x - d >= 0 && minc[x - d] < inf) {
                ans = min(ans, minc[x - d] + t[i].c);
            }
        }
        else {
            int d = t[i].l - t[i].r + 1;
            minc[d] = min(minc[d], t[i].c);
        }
    }

    if (ans == inf) {
        writeln(-1);
    }
    else {
        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);
}

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

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable lim = 2 * 10^^5 + 7;
immutable inf = 2 * 10^^9 + 7;
alias Trip = Tuple!(int, "r", int, "l", int, "c");
int n, x;

void main() {
    scan(n, x);

    auto t = new Trip[](n);

    int li, ri, ci;

    foreach (i ; 0 .. n) {
        scan(li, ri, ci);
        t[i] = Trip(ri, li, ci);
    }

    auto tl = t.dup;

    t.sort();
    tl.sort!"a[1] < b[1]"();

    debug {
        writeln("t:", t);
        writeln("tl:", tl);
    }

    auto minc = new long[](lim);
    minc[] = inf;

    long ans = inf;

    foreach (i ; 1 .. lim) {
        while (!tl.empty && i == tl.front.l) {
            int d = tl.front.r - tl.front.l + 1;
            if (x - d >= 0) {
                ans = min(ans, minc[x - d] + tl.front.c);
            }
            tl.popFront();
        }

        while (!tl.empty && i == t.front.r) {
            int d = t.front.r - t.front.l + 1;
            minc[d] = min(minc[d], t.front.c);
            t.popFront();
        }
    }

    if (ans == inf) {
        writeln(-1);
    }
    else {
        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);
}

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

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable lim = 2 * 10^^5 + 7;
immutable inf = 2 * 10^^9 + 7;
alias Trip = Tuple!(int, "r", int, "l", int, "c");
int n, x;

void main() {
    scan(n, x);

    auto t = new Trip[](n);

    int li, ri, ci;

    foreach (i ; 0 .. n) {
        scan(li, ri, ci);
        t[i] = Trip(ri, li, ci);
    }

    t.sort();

    auto dp = new int[][](lim, 3);

    foreach (i ; 0 .. lim) {
        dp[i][1] = dp[i][2] = inf;
    }

    foreach (i ; 1 .. lim) {
        dp[i][1] = min(dp[i][1], dp[i - 1][1]);
        dp[i][2] = min(dp[i][2], dp[i - 1][2]);

        while (!t.empty && i == t.front.r) {
            dp[i][1] = min(dp[i][1], t.front.c);

            dp[i][2] = min(dp[i][2], dp[t.front.l - 1][1] + t.front.c);

            t.popFront();
        }
    }

    debug {
        writefln("%(%(%s %)\n%)", dp[0 .. 10]);
    }

    int ans = dp[lim - 1][2];

    if (ans == inf) {
        writeln(-1);
    }
    else {
        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);
}

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

// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable lim = 2 * 10^^5 + 7;
immutable inf = 2 * 10^^9 + 7;
alias Trip = Tuple!(int, "r", int, "l", int, "c");
int n, x;

void main() {
    scan(n, x);

    auto t = new Trip[](n);

    int li, ri, ci;

    foreach (i ; 0 .. n) {
        scan(li, ri, ci);
        t[i] = Trip(ri, li, ci);
    }

    t.sort();

    auto dp = new long[][](lim, 3);

    foreach (i ; 0 .. lim) {
        dp[i][1] = dp[i][2] = inf;
    }

    foreach (i ; 1 .. lim) {
        dp[i][1] = min(dp[i][1], dp[i - 1][1]);
        dp[i][2] = min(dp[i][2], dp[i - 1][2]);

        while (!t.empty && i == t.front.r) {
            dp[i][1] = min(dp[i][1], t.front.c);
            dp[i][2] = min(dp[i][2], dp[t.front.l - 1][1] + t.front.c);
            t.popFront();
        }
    }

    debug {
        writefln("%(%(%s %)\n%)", dp[0 .. 10]);
    }

    long ans = dp[lim - 1][2];

    if (ans == inf) {
        writeln(-1);
    }
    else {
        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);
}

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


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

long ask(in char[] s) {
    writefln("? %s", s);
    stdout.flush;
    long r = readln.chomp.to!long;
    return r;
}

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

    long[] L = new long[M]; L[$-1] = -1;

    char[] q = new char[M]; q[] = '0';
    for (int i = 0; i < M; i++) {
        q[i] = '1';
        L[i] = ask(q);
        q[i] = '0';
    }
    auto idx = M.iota.array.sort!((a, b) => L[a] < L[b]).array;

    long C = 0;
    foreach (k; idx) {
        q[k] = '1';
        long nC = ask(q);
        if (nC == C + L[k]) {
            // need k
            C = nC;
        } else {
            // don't need k
            q[k] = '0';
        }
    }
    writefln("! %s", C);
    stdout.flush;
}

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

int ask (bool [] used)
{
	writefln !("? %(%s%)") (used.map !(to !(int)));
	stdout.flush ();
	return readln.strip.to !(int);
}

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

		auto len = new int [m];
		auto used = new bool [m];
		foreach (i; 0..m)
		{
			used[i] = true;
			len[i] = ask (used);
			used[i] = false;
		}

		int res = 0;
		auto p = m.iota.array;
		p.schwartzSort !(i => len[i]);
		foreach (i; p)
		{
			used[i] = true;
			int cur = ask (used);
			if (res + len[i] == cur)
			{
				res = cur;
			}
			else
			{
				used[i] = false;
			}
		}

		writefln !("! %s") (res);
		stdout.flush ();
	}
}

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() {
  const N = readInt;
  const M = readInt;

  alias Edge = Tuple!(long, "c", int, "i");
  auto es = new Edge[M];
  foreach (i; 0 .. M) {
    auto cs = new char[M];
    cs[] = '0';
    cs[i] = '1';
    writefln("? %s", cs);
    stdout.flush;
    es[i].c = readLong;
    es[i].i = i;
  }
  es.sort;

  long ans;
  {
    auto cs = new char[M];
    cs[] = '0';
    long bef;
    foreach (e; es) {
      const i = e.i;
      cs[i] = '1';
      writefln("? %s", cs);
      stdout.flush;
      const res = readLong;
      if (bef + e.c == res) {
        ans += e.c;
        bef = res;
      } else {
        cs[i] = '0';
      }
    }
  }

  writefln("! %s", ans);
  stdout.flush;
}

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

int ask (bool [] used)
{
	writefln !("? %(%s%)") (used.map !(to !(int)));
	stdout.flush ();
	return readln.strip.to !(int);
}

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

		auto len = new int [m];
		auto used = new bool [m];
		foreach (i; 0..m)
		{
			used[i] = true;
			len[i] = ask (used);
			used[i] = false;
		}

		int res = 0;
		foreach (i; 0..m)
		{
			used[i] = true;
			int cur = ask (used);
			if (res + len[i] == cur)
			{
				res = cur;
			}
			else
			{
				used[i] = false;
			}
		}

		writefln !("! %s") (res);
		stdout.flush ();
	}
}


import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt calc(BigInt n)
{
	BigInt med=n;
	int[]v;
	for(;;)
	{
		if(n==0)break;
		v~=n%10;
		n/=10;
	}
	BigInt cnt=0;
	BigInt t=1;
	for(int i=0;i<v.length;i++)
	{
		BigInt[]x;
		x.length=10;
		BigInt mm=med/(t*10);
		BigInt mm1=mm*10;
		for(int j=0;j<=9;j++)
		{
			x[j]+=mm*t;
			if(mm1+j<med/t)x[j]+=t;
			else if(mm1+j==med/t)x[j]+=med-(mm1+j)*t+1;
		}
		for(int j=0;j<=9;j++)cnt+=x[j]*j;
		t*=10;
	}
	return cnt;
}
BigInt get(BigInt a)
{
	BigInt beg=0,end="10000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt cnt=calc(med);
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	//for(int i=0;i<200;i++)writeln(i," ",calc(to!BigInt(i)));
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;i<1000;i++)dat[i]=-1;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		x=get(x);
		t=calc(x);
		//writeln(x," ",get(x)," ",t," ",t%a);
		if(dat[to!int(t%a)]!=-1&&dat[to!int(t%a)]!=x)
		{
			writeln(dat[to!int(t%a)]+1," ",x);
			break;
		}
		dat[to!int(t%a)]=x;
	}
	readln();
}

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

long A;

void main(string[] args) {
	try {
	for (; ; ) {
		A = readLong;

		long l = 1_000_000_000_000_000_000;
		long r = 2_000_000_000_000_000_000;

		long sum;
		(sum += (r - l)) %= A;
		foreach (i; 0 .. 18) {
			(sum += (r - l) / 10 * 45) %= A;
		}
		l -= sum;
		r -= sum;
		writeln(l, " ", r - 1);
	}
	} catch (EOFException) {}
}

import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt calc(BigInt n)
{
	BigInt med=n;
	int[]v;
	for(;;)
	{
		if(n==0)break;
		v~=n%10;
		n/=10;
	}
	BigInt cnt=0;
	BigInt t=1;
	for(int i=0;i<v.length;i++)
	{
		BigInt[]x;
		x.length=10;
		for(int j=0;j<=9;j++)
		{
			x[j]+=(med/(t*10))*t;
			if(med/(t*10)*10+j<med/t)x[j]+=t;
			else if(med/(t*10)*10+j==med/t)x[j]+=med-(med/(t*10)*10+j)*t+1;
		}
		for(int j=0;j<=9;j++)cnt+=x[j]*j;
		t*=10;
	}
	return cnt;
}
BigInt get(BigInt a)
{
	BigInt beg=0,end="100000000000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt cnt=calc(med);
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	//for(int i=0;i<200;i++)writeln(i," ",calc(to!BigInt(i)));
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;i<1000;i++)dat[i]=-1;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		t=calc(get(x));
		//writeln(x," ",get(x)," ",t," ",t%a);
		if(dat[to!int(t%a)]!=-1&&dat[to!int(t%a)]!=t)
		{
			writeln(dat[to!int(t%a)]+1," ",t);
			break;
		}
		dat[to!int(t%a)]=t;
	}
	readln();
}

import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt get(BigInt a)
{
	BigInt beg=0,end="100000000000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt n=med;
		int[]v;
		for(;;)
		{
			if(n==0)break;
			v~=n%10;
			n/=10;
		}
		BigInt cnt=0;
		BigInt t=1;
		for(int i=0;i<v.length;i++)
		{
			BigInt[]x;
			x.length=10;
			for(int j=0;j<=9;j++)
			{
				x[j]+=(med/(t*10))*t;
				if(med/(t*10)*10+j<med/t)x[j]+=t;
				else if(med/(t*10)*10+j==med/t)x[j]+=med-(med/(t*10)*10+j)*t+1;
			}
			for(int j=0;j<=9;j++)cnt+=x[j]*j;
			t*=10;
		}
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		t=get(x);
		if(dat[to!int(t%a)]!=0)
		{
			writeln(dat[to!int(t%a)]+1," ",t);
			break;
		}
		dat[to!int(t%a)]=t;
	}
	readln();
}

import std.stdio;
import std.array;
import std.algorithm;
import std.conv;
import std.bigint;
import std.string;
BigInt calc(BigInt n)
{
	BigInt med=n;
	int[]v;
	for(;;)
	{
		if(n==0)break;
		v~=n%10;
		n/=10;
	}
	BigInt cnt=0;
	BigInt t=1;
	for(int i=0;i<v.length;i++)
	{
		BigInt[]x;
		x.length=10;
		for(int j=0;j<=9;j++)
		{
			x[j]+=(med/(t*10))*t;
			if(med/(t*10)*10+j<med/t)x[j]+=t;
			else if(med/(t*10)*10+j==med/t)x[j]+=med-(med/(t*10)*10+j)*t+1;
		}
		for(int j=0;j<=9;j++)cnt+=x[j]*j;
		t*=10;
	}
	return cnt;
}
BigInt get(BigInt a)
{
	BigInt beg=0,end="100000000000000000000000000000";
	for(;;)
	{
		if(beg==end)break;
		BigInt med=(beg+end)/2;
		BigInt cnt=calc(med);
		if(cnt<a)beg=med+1;
		else end=med;
	}
	return beg;
}
void main()
{
	//for(int i=0;i<110;i++)writeln(i," ",get(to!BigInt(i)));
	BigInt a=to!BigInt(chomp(readln()));
	BigInt[]dat;
	dat.length=1000;
	for(int i=0;;i++)
	{
		BigInt t;
		BigInt x=a*i;
		t=calc(get(x));
		if(dat[to!int(t%a)]!=0)
		{
			writeln(dat[to!int(t%a)]+1," ",t);
			break;
		}
		dat[to!int(t%a)]=t;
	}
	readln();
}

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

long A;

void main(string[] args) {
	try {
	for (; ; ) {
		A = readLong;

		long l = 1_000_000_000_000_000_000;
		long r = 2_000_000_000_000_000_000;

		long sum;
		foreach (i; 0 .. 18) {
			(sum += (r - l) / 10 * 45) %= A;
		}
		l -= sum;
		r -= sum;
		writeln(l, " ", r - 1);
	}
	} catch (EOFException) {}
}


import std.stdio;
import std.algorithm;

const int MAX = 105;

char[MAX][MAX] t;
bool[MAX][MAX] b;
int[MAX] f;

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

    foreach(i; 0..n) {
        foreach(j; 0..m) {
            readf(" %s", t[i][j+1]);
        }
    }

    foreach(j; 1..m+1) {
        foreach(k; 0..j) {
            auto combi = true;
            foreach(i; 0..n-1) {
                if (!b[i][k] && t[i][j] > t[i+1][j]) {
                    combi = false;
                    break;
                }
            }
            if (!combi) {
                continue;
            }

            if (f[j] < f[k] + 1) {
                f[j] = f[k] + 1;
                foreach(i; 0..n-1) {
                    b[i][j] = b[i][k] | (t[i][j] < t[i+1][j]);
                }
            }
        }
    }

    auto cols = 0;
    foreach(j; 1..m+1) {
        cols = max(cols, f[j]);
    }
    writeln(m - cols);
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto F = new string[N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp;
    }

    int ans = 0;
    string[][] xs = [ F ];
    void C() {
        foreach (x; xs) {
            auto y = x.map!((s) => s[0]).array;
            auto y1 = y.dup;
            y1.sort;
            if (y != y1) {
                ans++;
                xs = xs.map!((x) => x.map!"a[ 1 .. $ ]".array).array;
                return;
            }
        }
        string[][] nxs;
        foreach (x; xs) {
            char p = x[0][0];
            string[] buf;
            foreach (y; x) {
                if (p == y[0]) {
                    buf ~= y;
                } else {
                    p = y[0];
                    nxs ~= [buf];
                    buf = [ y ];
                }
            }
            if (!buf.empty) nxs ~= [buf];
        }
        //writeln(nxs);
        xs = nxs.map!((x) => x.map!"a[ 1 .. $ ]".array).array;
    }

    foreach (i; 0 .. M) {
        C();
    }
    writeln(ans);
}

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, m;
	while (readf (" %s %s ", &n, &m) > 0)
	{
		string [] s;
		foreach (i; 0..n)
		{
			s ~= readln ().strip ();
		}
		auto b = new bool [n];
		long res = 0;
		foreach (j; 0..m)
		{
			bool ok = true;
			foreach (i; 1..n)
			{
				ok &= b[i] || (s[i][j] >= s[i - 1][j]);
			}
			if (!ok)
			{
				res++;
			}
			else
			{
				foreach (i; 1..n)
				{
					b[i] |= (s[i][j] > s[i - 1][j]);
				}
			}
		}
		writeln (res);
	}
}

import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

void main() {
    int N, M; scanf("%d %d\n", &N, &M);
    auto F = new string[N];
    foreach (i; 0 .. N) {
        F[i] = readln.chomp;
    }

    int ans = 0;

    bool C(int x) {
        auto cs = new char[N];
        foreach (i; 0 .. N) {
            cs[i] = F[i][x];
        }
        auto ds = cs.dup;
        ds.sort;
        return cs == ds;
    }

    foreach (i; 0 .. M) {
        if (C(i)) {
            auto c = F[0][i];
            auto j = 0;
            while (j < N && c == F[j][i]) {
                j++;
            }
            F = F[0 .. j];
            N = j;
        } else {
            ans++;
        }
    }
    writeln(ans);
}


// https://codeforces.com/contest/1104/problem/B
import std.stdio;
import std.string;
import std.range.primitives;

void main() {
    string s = readln.chomp;
    char[] stack;
    bool first = false;
    foreach(ch; s) {
        if(stack.length > 0 && stack[$-1] == ch) {
            stack.popBack();
            first = !first;
        } else {
            stack ~= ch;
        }
    }
    if(first)
        writeln("Yes");
    else
        writeln("No");
}

import std.stdio;
import std.string;
import std.algorithm: max;

void main() {
	string s = readln;
	char[] t = ['#'];
	foreach (char c; s) {
		t ~= [c];
		if (c==t[$-2]) t=t[0..$-2];
	}
	if ((s.count - t.count) / 2 % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

import std.stdio;
import std.string;
import std.algorithm: max;

void main() {
	string s = readln;
	string t = "#";
	foreach (char c; s) {
		t ~= c;
		if (c==t[$-2]) t=t[0..$-2];
	}
	if (s.count - t.count / 2 % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

import std.stdio;
import std.string;
import std.algorithm: max;
import std.range.primitives: popBack;

void main() {
	string s = readln;
	char[] t = ['#'];
	foreach (char c; s) {
		if (c==t[$-1]) t.popBack;
		t ~= [c];
	}
	if ((s.count - t.count) / 2 % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

import std.stdio;
import std.string;

void main() {
	string a = readln;
	bool conseguido = true;
	int b = 0;
	while (conseguido) {
		conseguido = false;
		for (int i=1; i<a.count; i++) {
			if (a[i-1] == a[i]) {
				a = a[i+1..$];
				conseguido = true;
				break;
			}
		}
		b += 1;
	}

	if (b % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}

import std.stdio;
import std.string;

void main() {
	string a = readln;
	bool conseguido = true;
	int b = 0;
	int last = 1;
	while (conseguido) {
		conseguido = false;
		for (int i=last; i<a.count; i++) {
			if (a[i-1] == a[i]) {
				a = a[0..i-1] ~ a[i+1..$];
				conseguido = true;
				last = i-2;
				break;
			}
		}
		b += 1;
	}

	if (b % 2 == 0) {
		writeln("Yes");
	} else {
		writeln("No");
	}
}