#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 100010;
const int maxnn = 500010;
const int w = 8;
const int h = 16;

int x[maxnn];
int ball[maxn][2];
int sh[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int sbonus[8] = { 0, 0, 3, 6, 12, 24, 50, 100 };

template<class T> T sqr(const T &a) { return a * a; }

struct field
{
	char a[h][w + 1];

	void scan()
	{
		for (int i = 0; i < h; i++)
			scanf("%s", a[i]);
	}

	void print()
	{
		for (int i = 0; i < h; i++)
			printf("%s\n", a[i]);
	}

	int used[h][w];
	int lst[h * w][2];
	int cnt;

	int bound(int p1, int p2)
	{
		return p1 >= 0 && p1 < h && p2 >= 0 && p2 < w;
	}

	void dfs(int p1, int p2, char color)
	{
		used[p1][p2] = 1;
		lst[cnt][0] = p1;
		lst[cnt++][1] = p2;
		for (int i = 0; i < 4; i++)
		{
			int q1 = p1 + sh[i][0];
			int q2 = p2 + sh[i][1];
			if (bound(q1, q2) && !used[q1][q2] && a[q1][q2] == color)
				dfs(q1, q2, color);
		}
	}

	void fall()
	{
		for (int i = 0; i < w; i++)
		{
			int k = h - 1;
			for (int j = h - 1; j >= 0; j--)
				if (a[j][i] != '.')
					a[k--][i] = a[j][i];
			for (; k >= 0; k--)
				a[k][i] = '.';
		}
	}

	int clblack(int p1, int p2)
	{
		if (bound(p1, p2) && a[p1][p2] == '*')
		{
			a[p1][p2] = '.';
			return 1;
		}
		return 0;
	}

	int addball(int col, int color)
	{
		int b = 0;
		for (int i = h - 1; i >= 0; i--)
			if (a[i][col] == '.')
			{
				b = a[i][col] = color + '0';
				break;
			}
		return !b;
	}

	void move(int mi, int s, int r, int &loss, int &bonus, int &steps, int &desblack, int &desnblack, double &midS)
	{
		loss = bonus = desblack = desnblack = steps = 0;
		switch (r)
		{
		case 0:
			loss &= addball(s, ball[mi][0]);
			loss &= addball(s + 1, ball[mi][1]);
			break;
		case 1:
			loss &= addball(s, ball[mi][1]);
			loss &= addball(s, ball[mi][0]);
			break;
		case 2:
			loss &= addball(s, ball[mi][1]);
			loss &= addball(s + 1, ball[mi][0]);
			break;
		case 3:
			loss &= addball(s, ball[mi][0]);
			loss &= addball(s, ball[mi][1]);
			break;
		}
		if (loss)
			return;

		while (1)
		{
			midS = 0;
			memset(used, 0, sizeof(used));
			int areas = 0;
			int curdesnblack = 0;
			for (int i = 0; i < h; i++)
				for (int j = 0; j < w; j++)
					if (a[i][j] != '.' && a[i][j] != '*' && !used[i][j])
					{
						cnt = 0;
						dfs(i, j, a[i][j]);
						if (cnt >= 4)
						{
							areas++;
							for (int ii = 0; ii < cnt; ii++)
							{
								int p1 = lst[ii][0];
								int p2 = lst[ii][1];
								a[p1][p2] = '.';
								desnblack++;
								curdesnblack++;
								for (int jj = 0; jj < 4; jj++)
									desblack += clblack(p1 + sh[jj][0], p2 + sh[jj][1]);
							}
						}
						midS += sqr(cnt-1);
					}
			if (areas)
			{
				steps++;
				bonus += curdesnblack - areas - 2;
				fall();
			}
			else
				break;
		}
		if (steps <= 7)
			bonus += sbonus[steps];
		else
			bonus += 100;
	}
	double fh() {
		double res = 0;
		for (int j = 0; j < w; j++) {
			int ch = 16;
			for (int i = 0; a[i][j] == '.' && i < h; i++, ch--);
			res += ch * ch;
		}
		return res / w / (h * h);
	}
};

void precalc(int seed)
{
	x[0] = seed;
	for (int i = 1; i < maxnn; i++)
		x[i] = (x[i - 1] * 17235617LL + 4311271) % 87465851;
	for (int i = 1; i < maxn; i++)
	{
		ball[i][0] = x[2 * i - 2] % 5 + 1;
		ball[i][1] = x[2 * i - 1] % 5 + 1;
	}
}

void test()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	precalc(2);
	field f;
	f.scan();
	int s, r;
	scanf("%d %d", &s, &r);
	int loss, bonus, steps, desblack, desnblack;
	double mids;
	f.move(1, s, r, loss, bonus, steps, desblack, desnblack, mids);
	f.print();
	printf("loss=%d bonus=%d steps=%d desblack=%d desnblack=%d", loss, bonus, steps, desblack, desnblack);
}

const int max_s = 35, max_d = 10, max_l = max_s * max_s;

struct Value {
	field f;
	int r, s;
	double value;
	bool operator < (const Value &v) const {
		return value > v.value;
	}
	void scan() {
		f.scan();
		r = s = -1;
		value = 0;
	}
	void print() {
		printf("%d %d\n", s, r);
		fflush(stdout);
	}
	void move(int imove, int s, int r, bool ch, double pvalue) {
		if (ch) {
			this->r = r;
			this->s = s;
		}
		int loss, bonus, steps, desblack, desnblack;
		double mids = 0;
		f.move(imove, s, r, loss, bonus, steps, desblack, desnblack, mids);
		//value = pvalue * 1e5 + bonus + desblack + desnblack - f.fh() * 5 - loss * 1e100;
		value = pvalue * 1.1 + bonus + desblack * 2 + desnblack * 0.3 + mids * 1 - f.fh() * 5 - loss * 1e100;
	}
};
struct Solve {
	Value s[max_s];
	Value cur[max_l];
	int ns;
	void move(int imove) {
		s[0].scan();
		s[1].scan();//skip
		ns = 1;
		nextmove(imove, 1);
		for (int i = 1; i < max_d; i++) {
			nextmove(imove + i, 0);
		}
		s[0].print();
	}
	void nextmove(int imove, bool ch) {
		int u = 0;
		for (int i = 0; i < ns; i++) {
			for (int j = 0; j < w; j++) {
				cur[u] = s[i];
				cur[u++].move(imove, j, 1, ch, s[i].value);
				cur[u] = s[i];
				cur[u++].move(imove, j, 3, ch, s[i].value);
				if (j < w - 1) {
					cur[u] = s[i];
					cur[u++].move(imove, j, 0, ch, s[i].value);
					cur[u] = s[i];
					cur[u++].move(imove, j, 2, ch, s[i].value);
				}
			}
		}
		sort(cur, cur + u);
		int ns = min(u, max_s);
		for (int i = 0; i < ns; i++) {
			s[i] = cur[i];
		}
	}
} sol;

void test2() {
	freopen("input.txt", "r", stdin);
	precalc(1);
	sol.move(1);
}
int main()
{
	/*test();
	return 0;/**/

	int x0, imove = 0;
	for (int i = 0; ; i++)
	{
		char str[100];
		scanf("%s", str);
		if (!strcmp(str, "Quit"))
			break;
		if (!strcmp(str, "Start")) {
			scanf("%d", &x0);
			printf("Perm SU: REMember 0x0043\n");
			precalc(x0);
			fflush(stdout);
		}
		else if (!strcmp(str, "Move")) {
			sol.move(++imove);
		}
	}
	return 0;
}
