3 8
2 4 4 4
7 2 5 2
6 5 6 3
20
3 2 U 1000000
3 2 U 1
3 2 U 2
3 2 U 3
3 2 U 4
3 2 U 5
3 2 U 6
3 2 U 7
3 2 U 8
3 2 U 9
3 2 U 10
3 2 U 11
3 2 U 12
3 2 U 13
3 2 U 14
3 2 U 15
3 2 U 16
3 2 U 17
3 2 U 18
3 2 U 19
 4 2
0 0 1 0
2 0 2 1
2 2 1 2
0 2 0 1
15
1 1 R 7
0 0 U 3
1 1 R 888888888888887
0 0 U 404040404040403
2 1 D 1000000000000000
2 0 R 324354768576473
2 0 L 325436475453245
2 0 U 764536452343654
2 0 D 943654765876545
1 1 U 1000000000000000
1 1 D 989898789898789
1 1 L 123456787654344
0 1 L 0
0 1 L 1
0 1 L 999999999999993
 1 10
1 5 9 5
1
5 5 L 1
 3 7
2 4 2 7
4 7 4 6
7 3 5 3
10
6 7 L 1
6 7 L 2
6 7 L 3
6 7 L 4
6 7 L 5
6 7 L 6
6 7 L 7
6 7 L 8
6 7 L 9
6 7 L 10

                                                               0                                                                2
                                                               3                                                                3
                                                               3                                                                4
                                                               4                                                                4
                                                               5                                                                4
                                                               6                                                                4
                                                               6                                                                3
                                                               6                                                                2
                                                               5                                                                2
                                                               4                                                                2
                                                               3                                                                2
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                                1                                                                0
                                                               2                                                                1
                                                               1                                                                0
                                                               2                                                                1
                                                               2                                                                1
                                                               2                                                                1
                                                               0                                                                1
                                                               0                                                                0
                                                               2                                                                1
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                1
                                                               0                                                                0
                                                               0                                                                0
                                                                6                                                                5
                                                                5                                                                7
                                                               4                                                                7
                                                               4                                                                6
                                                               4                                                                5
                                                               4                                                                4
                                                               4                                                                3
                                                               4                                                                2
                                                               4                                                                1
                                                               4                                                                0
                                                               4                                                                0

import java.io.*;
import java.util.*;

public class D1 {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	int b;

	int decode(int x, int y) {
		return x * b + y;
	}

	static final String DIRS = "ULDR";
	static final int[] DX = { 0, -1, 0, 1 };
	static final int[] DY = { 1, 0, -1, 0 };

	static int getDir(int dx, int dy) {
		for (int i = 0; i < 4; i++) {
			if (DX[i] == dx && DY[i] == dy)
				return i;
		}
		throw new AssertionError();
	}

	List<Integer> path = new ArrayList<>();
	int[] color = new int[b * b];

	void solve() throws IOException {
		int n = nextInt();
		b = nextInt() + 1;
		
		int N = b * b * 4;

		int[] next = new int[N];

		for (int i = 0; i < N; i++) {
			int cDir = i & 3;
			int x = (i >> 2) / b;
			int y = (i >> 2) % b;

			int nx = x + DX[cDir];
			int ny = y + DY[cDir];

			if (nx < 0 || ny < 0 || nx >= b || ny >= b) {
				next[i] = i;
			} else {
				next[i] = (decode(nx, ny) << 2) | cDir;
			}
		}

		for (int i = 0; i < n; i++) {
			int x0 = nextInt();
			int y0 = nextInt();
			int x1 = nextInt();
			int y1 = nextInt();

			int len = Math.abs(x1 - x0) + Math.abs(y1 - y0) + 1;
			int dx = Integer.signum(x1 - x0);
			int dy = Integer.signum(y1 - y0);

			int cDir = getDir(dx, dy);

			for (int j = 0; j < len; j++) {

				int v = decode(x0, y0);

				for (int dd = 0; dd < 4; dd++) {
					next[(v << 2) | dd] = next[(v << 2) | cDir];
				}

				x0 += DX[cDir];
				y0 += DY[cDir];
			}
		}

		int q = nextInt();
		int[] qV = new int[q];
		long[] timeV = new long[q];
		
		for (int i = 0; i < q; i++) {
			int x = nextInt();
			int y = nextInt();
			int dir = DIRS.indexOf(nextToken().charAt(0));
			qV[i] = (decode(x, y) << 2) | dir;
			timeV[i] = nextLong();
		}
		
		for (int iter = 0; iter < 50; iter++) {
			for (int i = 0; i < q; i++) {
				if ((timeV[i] & 1) == 1 && qV[i] != -1) {
					qV[i] = next[qV[i]];
				}
				timeV[i] >>= 1;
			}
			
			int[] newNext = new int[N + 1];
			for (int i = 0; i < N; i++) {
				newNext[i] = next[next[i]];
			}
			next = newNext;
		}
		
		for (int i = 0; i < q; i++) {
			int ret = qV[i] >> 2;
			out.println(ret / b + " " + ret % b);
		}
	}

	D1() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new D1();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}