// Arup Guha
// 4/26/2019
// Solution to 2019 FHSPS Playoff Problem: a-b Knight

import java.util.*;

public class abknight {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();
		
		// Process each case.
		for (int loop=0; loop<numCases; loop++) {
			
			// Get basic input parameters.
			int r = stdin.nextInt();
			int c = stdin.nextInt();
			int a = stdin.nextInt();
			int b = stdin.nextInt();
			
			// Build movement array.
			int[][] dxdy = buildDXDY(a,b);
			
			// Distance array for BFS.
			int[][] dist = new int[r][c];
			for (int i=0; i<r; i++)
				Arrays.fill(dist[i], -1);
			
			// Get starting location and fill distance.
			int x = stdin.nextInt()-1;
			int y = stdin.nextInt()-1;
			dist[x][y] = 0;
			
			// Add source square to queue.
			LinkedList<Integer> q = new LinkedList<Integer>();
			q.offer(x*c+y);
			
			// Start BFS.
			while (q.size() > 0) {
				
				// Get current item.
				int cur = q.poll();
				int cX = cur/c;
				int cY = cur%c;
				
				// Add all unvisited items to the queue and mark their distances.
				for (int i=0; i<dxdy[0].length; i++) {
					
					// Calculate next location.
					int nextX = cX + dxdy[0][i];
					int nextY = cY + dxdy[1][i];
					
					// Skip if out of bounds or we've been there before.
					if (!inbounds(nextX, nextY, r, c)) continue;
					if (dist[nextX][nextY] != -1) continue;
					
					// Add to queue and mark distance.
					q.offer(nextX*c+nextY);
					dist[nextX][nextY] = dist[cX][cY] + 1;
				}
			}
			
			// Print distance grid.
			for (int i=0; i<r; i++) {
				for (int j=0; j<c-1; j++)
					System.out.print(dist[i][j]+" ");
				System.out.println(dist[i][c-1]);
			}
		}
	}
	
	public static int[][] buildDXDY(int a,int b) {
		
		int[] tmp = {a,b};
		int[][] res = new int[2][8];
		int idx = 0;
		
		// i is sign of the dx, so I do all negative dx's first.
		for (int i=-1; i<=1; i+=2) {
			
			// j is whether my dx is a(0) or b(1).
			for (int j=0; j<2; j++) {
				
				// sign is the sign of y, which toggles each time.
				for (int sign=-1; sign<=1; sign+=2) {
					res[0][idx] = i*tmp[j];
					res[1][idx] = sign*tmp[1-j];
					idx++;
				}
			}
		}
		
		return res;
	}
	
	// Returns true iff (x,y) is inbounds on r x c grid.
	public static boolean inbounds(int x, int y, int r, int c) {
		return x >= 0 && x < r && y >= 0 && y < c;
	}

}