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 MAXN = 1010;
const int MAXT = 24;
const int BASE = 2;
const int MAXL = MAXN * MAXN * 4;
int mx[4] = {0, 1, 0, -1};
int my[4] = {1, 0, -1, 0};
int dp[MAXN * MAXN * 4][MAXT];
int grid[MAXN][MAXN];
bool vis[MAXN * MAXN * 4];
int len[MAXN * MAXN * 4];
int tmp[MAXN * MAXN * 4];
int N, B;
int Q;
int conv(int x, int y, int d) { return y + MAXN * (x + MAXN * d); }
void disp(int p) {
  int y = p % MAXN;
  p /= MAXN;
  int x = p % MAXN;
  int d = p / MAXN;
  printf("(%d %d), d = %d\n", x, y, d);
}
int main() {
  scanf("%d %d", &N, &B);
  for (int i = 0; i <= B; ++i) {
    for (int j = 0; j <= B; ++j) {
      grid[i][j] = -1;
    }
  }
  for (int i = 0; i < N; ++i) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    if (a == c) {
      if (b < d) {
        for (int y = b; y <= d; ++y) {
          grid[a][y] = 0;
        }
      } else {
        for (int y = d; y <= b; ++y) {
          grid[a][y] = 2;
        }
      }
    } else {
      if (a < c) {
        for (int x = a; x <= c; ++x) {
          grid[x][b] = 1;
        }
      } else {
        for (int x = c; x <= a; ++x) {
          grid[x][b] = 3;
        }
      }
    }
  }
  for (int i = 0; i <= B; ++i) {
    for (int j = 0; j <= B; ++j) {
      for (int d = 0; d < 4; ++d) {
        int &ret = dp[conv(i, j, d)][0];
        int nx = i + mx[d];
        int ny = j + my[d];
        if (nx > B || ny > B || nx < 0 || ny < 0) {
          ret = -1;
        } else {
          int nd = d;
          if (grid[nx][ny] != -1) {
            nd = grid[nx][ny];
          }
          ret = conv(nx, ny, nd);
        }
      }
    }
  }
  for (int i = 0; i <= B; ++i) {
    for (int j = 0; j <= B; ++j) {
      for (int d = 0; d < 4; ++d) {
        int p = conv(i, j, d);
        if (vis[p]) continue;
        int cur = p;
        int cnt = 0;
        while (cur != -1 && !vis[cur]) {
          vis[cur] = true;
          tmp[cur] = cnt;
          ++cnt;
          cur = dp[cur][0];
        }
        int cyc = -1;
        if (cur != -1) {
          if (len[cur])
            cyc = len[cur];
          else
            cyc = cnt - tmp[cur];
          cyc = MAXL - MAXL % cyc + cyc;
        }
        cur = p;
        while (cur != -1 && !len[cur]) {
          len[cur] = cyc;
          cur = dp[cur][0];
        }
      }
    }
  }
  for (int t = 0; t + 1 < MAXT; ++t) {
    for (int i = 0; i <= B; ++i) {
      for (int j = 0; j <= B; ++j) {
        for (int d = 0; d < 4; ++d) {
          int p = conv(i, j, d);
          int nxt = p;
          for (int k = 0; k < BASE; ++k) {
            nxt = dp[nxt][t];
            if (nxt == -1) break;
          }
          dp[p][t + 1] = nxt;
        }
      }
    }
  }
  long long maxstep = 1;
  for (int i = 0; i < MAXT; ++i) maxstep *= BASE;
  scanf("%d", &Q);
  for (int q = 0; q < Q; ++q) {
    int a, b;
    char c;
    long long T;
    scanf("%d %d %c", &a, &b, &c);
    cin >> T;
    int dir;
    if (c == 'U')
      dir = 0;
    else if (c == 'R')
      dir = 1;
    else if (c == 'D')
      dir = 2;
    else if (c == 'L')
      dir = 3;
    if (grid[a][b] != -1) dir = grid[a][b];
    int p = conv(a, b, dir);
    if (len[p] != -1) {
      T %= len[p];
    }
    long long tt = maxstep;
    for (int t = MAXT - 1; t >= 0; --t) {
      tt /= BASE;
      while (dp[p][t] != -1 && tt <= T) {
        T -= tt;
        p = dp[p][t];
      }
    }
    int y = p % MAXN;
    p /= MAXN;
    int x = p % MAXN;
    printf("%d %d\n", x, y);
  }
  return 0;
}
