#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;
}
