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

#include <bits/stdc++.h>
using namespace std;

const int N = 1100;
const int Q = 100050;

const int dx[] = {0,0,-1,1};
const int dy[] = {1,-1,0,0};

int n, b, B, q, v[Q], to[2][N*N*4];
long long t[Q];

struct node {
	int x,y,k;
	node () {}
	node (int _x, int _y, int _k) : x(_x), y(_y), k(_k) {
		if (k == 'U') k = 0;
		if (k == 'D') k = 1;
		if (k == 'L') k = 2;
		if (k == 'R') k = 3;
	}
	node move(int k) {
		int nx = x + dx[k]; nx = max(nx, 0); nx = min(nx, b);
		int ny = y + dy[k]; ny = max(ny, 0); ny = min(ny, b);
		return node(nx, ny, k);
	}
	int id() {
		return (x * B + y) * 4 + k;
	}
} next[N*N*4];

void output() {
	puts("debug...");
	for (int i = 0; i < B; i++)
		for (int j = 0; j < B; j++)
			for (int k = 0; k < 4; k++) {
				node a = node(i, j, k);
				node b = next[a.id()];
				printf("(%d,%d,%d) -> (%d,%d,%d)\n", a.x, a.y, a.k, b.x, b.y, b.k);
			}
}

int main() {
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);

	scanf("%d %d", &n, &b); B = b+1;

	for (int i = 0; i < B; i++) {
		for (int j = 0; j < B; j++) {
			for (int k = 0; k < 4; k++) {
				node a = node(i, j, k);
				node b = a.move(k);
				next[a.id()] = b;
			}
		}
	}

	//output();

	for (int i = 0; i < n; i++) {
		int x0, y0, x1, y1;
		scanf("%d %d %d %d", &x0, &y0, &x1, &y1);
		int len = abs(x0 - x1) + abs(y0 - y1);
		for (int k = 0; k < 4; k++)
		if (x0 + dx[k] * len == x1 && y0 + dy[k] * len == y1) {
			for (int c = 0; c <= len; c++) {
				int x = x0 + dx[k] * c;
				int y = y0 + dy[k] * c;
				for (int kk = 0; kk < 4; kk++) {
					next[node(x,y,kk).id()] = next[node(x,y,k).id()];
				}
			}
			break;
		}
	}

	//output();

	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int x, y; char w;
		scanf("%d %d %c %I64d", &x, &y, &w, t+i);
		v[i] = node(x,y,w).id();
	}

	int ALL = B * B * 4;

	for (int i = 0; i < ALL; i++)
		to[0][i] = next[i].id();

	for (int u = 0; u < 50; u++) {
		for (int i = 0; i < q; i++)
		if (t[i] & (1ll<<u))
			v[i] = to[0][v[i]];
		for (int i = 0; i < ALL; i++)
			to[1][i] = to[0][to[0][i]];
		memcpy(to[0], to[1], sizeof to[0]);
	}

	for (int i = 0; i < q; i++) {
		int x = v[i] / 4 / B;
		int y = v[i] / 4 % B;
		printf("%d %d\n", x, y);
	}

	return 0;
}