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;
template <class F, class S>
ostream& operator<<(ostream& o, const pair<F, S>& p) {
  return o << "(" << p.first << ", " << p.second << ")";
}
template <class C>
void O__(ostream& o, const C& c) {
  bool f = 1;
  for (const auto& x : c) {
    if (!f) o << ", ";
    f = 0;
    o << x;
  }
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  O__(o, v);
  o << "]";
  return o;
}
template <class T, class V>
ostream& operator<<(ostream& o, const map<T, V>& v) {
  o << "{";
  O__(o, v);
  o << "}";
  return o;
}
template <class T>
ostream& operator<<(ostream& o, const set<T>& v) {
  o << "{";
  O__(o, v);
  o << "}";
  return o;
}
const double PI = 3.1415926535897932384626433832795;
const double EPS = 1e-9;
const int INF = (1 << 31) - 1;
const long long LLINF = (1LL << 63) - 1;
int b;
int code(int x, int y, int dir) { return dir + 4 * ((b + 1) * x + y); }
int code(int dx, int dy) {
  if (dx == 0) {
    return dy == -1 ? 0 : 1;
  }
  return dx == -1 ? 2 : 3;
}
void uncode(int code, int& dx, int& dy) {
  if (code < 2) {
    dx = 0;
    dy = code == 0 ? -1 : +1;
    return;
  }
  dy = 0;
  dx = code == 2 ? -1 : +1;
}
int sign(int x) { return x == 0 ? x : x / abs(x); }
void pr(int c) {
  int x, y;
  c /= 4;
  y = c % (b + 1);
  x = c / (b + 1);
  printf("%d %d\n", x, y);
}
int main() {
  int n;
  cin >> n >> b;
  vector<vector<int>> plan(b + 1, vector<int>(b + 1, -1));
  for (int i = 0; i < n; ++i) {
    int x, y, X, Y;
    scanf("%d%d%d%d", &x, &y, &X, &Y);
    int dx = sign(X - x);
    int dy = sign(Y - y);
    while (make_pair(x, y) != make_pair(X, Y)) {
      plan[x][y] = code(dx, dy);
      x += dx;
      y += dy;
    }
    plan[x][y] = code(dx, dy);
  }
  int size = (b + 1) * (b + 1) * 4;
  vector<int> to(size, -1);
  for (int i = 0; i < size; ++i) {
    to[i] = i;
  }
  for (int x = 0; x <= b; ++x) {
    for (int y = 0; y <= b; ++y) {
      for (int d = 0; d < 4; ++d) {
        int dir = -1;
        if (plan[x][y] != -1) {
          dir = plan[x][y];
        } else {
          dir = d;
        }
        int dx, dy;
        uncode(dir, dx, dy);
        int nx = x + dx;
        int ny = y + dy;
        assert(dx == 0 ^ dy == 0);
        if (nx < 0 || ny < 0 || nx > b || ny > b) continue;
        to[code(x, y, d)] = code(nx, ny, dir);
      }
    }
  }
  vector<vector<int>> dp(25, vector<int>(size, -1));
  dp[0] = to;
  for (int i = 1; i < dp.size(); ++i) {
    for (int s = 0; s < size; ++s) {
      int ne = s;
      for (int k = 0; k < 4; ++k) {
        ne = dp[i - 1][ne];
        if (ne == -1) {
          dp[i][s] = -1;
          break;
        }
        dp[i][s] = ne;
      }
    }
  }
  int q;
  cin >> q;
  while (q-- > 0) {
    int x, y;
    char buf[123];
    scanf("%d%d%s", &x, &y, buf);
    int dx, dy;
    char c = buf[0];
    dx = dy = 0;
    if (c == 'U') {
      ++dy;
    }
    if (c == 'D') --dy;
    if (c == 'L') --dx;
    if (c == 'R') ++dx;
    long long t;
    scanf("%I64d", &t);
    int st = code(x, y, code(dx, dy));
    for (int p2 = dp.size() - 1; p2 >= 0; --p2) {
      while (t >= (1LL << (p2 + p2))) {
        t -= (1LL << (p2 + p2));
        st = dp[p2][st];
      }
    }
    pr(st);
  }
  return 0;
}
