// Aizu 1115: Multi-column List
// 2017.9.15 bal4u@uu

#include <stdio.h>
#include <stdlib.h>

char buf[1002][1005]; int len;
char pag[102][52];

static int ln, ii, ff;
int nxchar()   // return -1: eol, -2: null line, -3: eof
{
	int c;

	if (ff) return -3;
	c = buf[ln][ii];
	if (c < ' ') {
		if (++ln >= len) ff = 1;
		if (ii == 0) return -2;
		ii = 0;
		return -1;
	} else ii++;
	return c;
}

int main()
{
	int p, n, w, s, pw;
	int i, j, r, c, cc, m, f;
	char t[15];

	while (1) {
		fgets(t, 10, stdin); p = atoi(t); if (!p) break;
		fgets(t, 10, stdin); n = atoi(t);
		fgets(t, 10, stdin); w = atoi(t);
		fgets(t, 10, stdin); s = atoi(t);
		pw = n*w + s*(n-1);
		for (len = 0; ; len++) {
			fgets(buf[len], 1002, stdin);
			if (buf[len][0] == '?') break;
		}
		for (ln = ii = ff = 0, f = 0; ; ) {
			for (r = 0; r < p; r++) {
				for (c = 0; c < pw; c++) pag[r][c] = '.';
				pag[r][c] = 0;
			}
			for (cc = 0, i = 0; i < n; i++, cc += w+s) {
				for (r = 0; r < p; r++) {
					for (j = 0; j < w; ) {
						c = cc + j;
						m = nxchar();
						if (m == -1) {
							if (j > 0) break; else continue;
						} else if (m == -2) break;
						else if (m == -3) {	f = 1; goto Done; }
						pag[r][c] = m, j++;
					}
				}
			}
Done:		
			if (f && i == 0 && r == 0 && j == 0) break;
			for (r = 0; r < p; r++) puts(pag[r]);
			puts("#");
			if (f) break;
		}
		puts("?");
    }
	return 0;
}