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 MAXB = 1002;
const int MAXD = 25;
const int MAXS = 4020000;
int N, B;
int nloc[MAXS][MAXD];
int dir[MAXB][MAXB];
int xc[4] = {-1, 0, 1, 0};
int yc[4] = {0, -1, 0, 1};
string str = "LDRU";
inline void decode(int c) { printf("%d %d\n", c % MAXB, (c / MAXB) % MAXB); }
inline int encode(pair<pair<int, int>, int> c) {
  return c.first.first + MAXB * c.first.second + MAXB * MAXB * c.second;
}
int main() {
  for (int i = 0; i < MAXB; i++)
    for (int j = 0; j < MAXB; j++) dir[i][j] = -1;
  scanf("%d %d", &N, &B);
  for (int i = 0; i < N; i++) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    if (x1 == x2) {
      if (y1 < y2) {
        for (int j = y1; j <= y2; j++) dir[x1][j] = 3;
      } else {
        for (int j = y2; j <= y1; j++) dir[x1][j] = 1;
      }
    } else {
      if (x1 < x2)
        for (int j = x1; j <= x2; j++) dir[j][y1] = 2;
      else
        for (int j = x2; j <= x1; j++) dir[j][y1] = 0;
    }
  }
  for (int i = 0; i < MAXS; i++) nloc[i][0] = 0;
  for (int i = 0; i <= B; i++)
    for (int j = 0; j <= B; j++)
      for (int k = 0; k < 4; k++) {
        int ndir = k;
        if (dir[i][j] != -1) ndir = dir[i][j];
        int nx = i + xc[ndir], ny = j + yc[ndir];
        if (nx < 0 || nx > B || ny < 0 || ny > B) nx = i, ny = j;
        nloc[encode(make_pair(make_pair(i, j), k))][0] =
            encode(make_pair(make_pair(nx, ny), ndir));
      }
  for (int d = 1; d < MAXD; d++)
    for (int i = 0; i < MAXS; i++) {
      int cloc = i;
      for (int j = 0; j < 4; j++) cloc = nloc[cloc][d - 1];
      nloc[i][d] = cloc;
    }
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int x, y, cdir;
    char ch;
    long long T;
    scanf("%d %d %c %I64d", &x, &y, &ch, &T);
    for (int j = 0; j < 4; j++)
      if (ch == str[j]) cdir = j;
    int cloc = encode(make_pair(make_pair(x, y), cdir));
    int dcur = 0;
    while (T) {
      for (int j = 0; j < T & 3LL; j++) cloc = nloc[cloc][dcur];
      T >>= 2;
      dcur++;
    }
    decode(cloc);
  }
  return 0;
}
