// Arup Guha
// 8/20/2015
// Solution to 2010 UCF Locals Problem: The Puzzle Master (maze)

import java.util.*;

public class maze {

	public static int r;
	public static int c;
	public static char[][] grid;
	public static HashMap<String,Integer> dirMap;

	final public static int[] DX = {-1,0,1,0};
	final public static int[] DY = {0,1,0,-1};

	public static void main(String[] args) {

		// Just makes reading directions easier.
		dirMap = new HashMap<String,Integer>();
		dirMap.put("N",0);
		dirMap.put("E",1);
		dirMap.put("S",2);
		dirMap.put("W",3);

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Get grid.
			r = stdin.nextInt();
			c = stdin.nextInt();
			grid = init(r, c);

			// Get starting spot.
			int x = stdin.nextInt()-1;
			int y = stdin.nextInt()-1;
			int dir = dirMap.get(stdin.next());

			// Clear it!
			HashSet<Integer> used = new HashSet<Integer>();
			go(x, y, dir, used);

			// Print it.
			print(loop);
		}
	}

	// Returns a character grid that can store the walls necessary for a
	// grid with rows x cols cells.
	public static char[][] init(int rows, int cols) {

		char[][] ans = new char[2*rows+1][];

		// Allocate even and odd rows and fill them, accordingly.
		for (int i=0; i<ans.length; i++) {
			if (i%2 == 0) {
				ans[i] = new char[cols];
				Arrays.fill(ans[i], '-');
			}
			else {
				ans[i] = new char[cols+1];
				Arrays.fill(ans[i], '|');
			}
		}

		// Top left opening
		ans[1][0] = ' ';

		// Bottom right opening
		ans[2*rows-1][cols] = ' ';
		return ans;
	}

	public static void go(int x, int y, int dir, HashSet<Integer> used) {

		// Mark that we've been here.
		used.add(c*x+y);

		// Loop through the possible directions.
		for (int d=dir; d<dir+4; d++) {

			// Get where we are supposed to go.
			int[] coord = getCoord(x, y, d%4);

			// Not supposed to take out borders.
			if (border(coord)) continue;

			// Or squares we've already taken out.
			if (grid[coord[0]][coord[1]] == ' ') continue;

			// Prevent cycle.
			if (used.contains(c*(x+DX[d%4]) + y+DY[d%4])) continue;

			// Take it out!!!
			grid[coord[0]][coord[1]] = ' ';

			// Recursively make the maze.
			go(x+DX[d%4], y+DY[d%4], (d+1)%4, used);
		}
	}

	public static boolean border(int[] coord) {

		// Top or bottom row.
		if (coord[0] == 0 || coord[0] == 2*r) return true;

		// Left or right most column.
		if (coord[0]%2 == 1 && (coord[1] == 0 || coord[1] == c)) return true;

		// Should be okay.
		return false;
	}

	// Returns the location in the grid of borders corresponding to where
	// cell (x,y) pointing in direction dir refers to.
	public static int[] getCoord(int x, int y, int dir) {
		int curx = 2*x+1 + DX[dir];
		int cury = y;
		if (DY[dir] == 1) cury++;
		return new int[]{curx, cury};
	}

	public static void print(int num) {

		// Header
		System.out.println("Maze #"+num+":");
		System.out.println();

		// Go through each row.
		for (int i=0; i<grid.length; i++) {

			// Print the +s where we need them, or the corresponding grid square.
			for (int j=0; j<2*c+1; j++) {
				if ((i+j)%2 == 0) 	{
					if (i%2 == 0) 	System.out.print("+");
					else			System.out.print(" ");
				}
				else 				System.out.print(grid[i][j/2]);
			}

			// Go to the next line.
			System.out.println();
		}
		System.out.println();
	}
}