#include <bits/stdc++.h>
using namespace std;
inline int gi() {
  char c = getchar();
  int x = 0;
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
  return x;
}
const int N = 2e5 + 5, L = 50;
const long long inf = 1e16;
struct node {
  int x1, x2, y1, y2, id;
} a[N];
bool cmp1(node a, node b) {
  if (max(a.x1, a.x2) == max(b.x1, b.x2)) return a.id < b.id;
  return max(a.x1, a.x2) > max(b.x1, b.x2);
}
bool cmp2(node a, node b) { return a.id < b.id; }
int n, m, q, to[L + 2][N], cov[N << 2];
long long t[N], dis[L + 2][N];
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
void build(int x = 1, int l = -1, int r = m + 1) {
  cov[x] = 0;
  if (l == r) return;
  int mid = l + r >> 1;
  build((x << 1), l, mid), build((x << 1 | 1), mid + 1, r);
}
void update(int sl, int sr, int w, int x = 1, int l = -1, int r = m + 1) {
  if (sl <= l && r <= sr) {
    cov[x] = w;
    return;
  }
  if (cov[x]) cov[(x << 1)] = cov[(x << 1 | 1)] = cov[x];
  int mid = l + r >> 1;
  if (sl <= mid) update(sl, sr, w, (x << 1), l, mid);
  if (sr > mid) update(sl, sr, w, (x << 1 | 1), mid + 1, r);
  cov[x] = (cov[(x << 1)] == cov[(x << 1 | 1)] ? cov[(x << 1)] : 0);
}
int query(int s, int x = 1, int l = -1, int r = m + 1) {
  if (cov[x] || l == r) return cov[x];
  int mid = l + r >> 1;
  return s <= mid ? query(s, (x << 1), l, mid)
                  : query(s, (x << 1 | 1), mid + 1, r);
}
void transform() {
  for (int i = 1; i <= n + q; ++i) {
    swap(a[i].x1, a[i].y1), a[i].x1 = m - a[i].x1;
    swap(a[i].x2, a[i].y2), a[i].x2 = m - a[i].x2;
  }
}
inline char getdir(node a) {
  if (a.x1 > a.x2) return 0;
  if (a.x1 < a.x2) return 1;
  if (a.y1 > a.y2) return 2;
  if (a.y1 < a.y2) return 3;
}
inline bool have(int l1, int r1, int l2, int r2) {
  if (l1 > r1) swap(l1, r1);
  if (l2 > r2) swap(l2, r2);
  if (l2 <= l1 && l1 <= r2) return true;
  if (l2 <= r1 && r1 <= r2) return true;
  return false;
}
inline int getdis(node a, node b) {
  if (getdir(a) < 2 && getdir(b) < 2 && have(a.x1, a.x2, b.x1, b.x2)) return 0;
  if (getdir(a) > 1 && getdir(b) > 1 && have(a.y1, a.y2, b.y1, b.y2)) return 0;
  switch (getdir(a)) {
    case 0:
      return a.x2 - max(b.x1, b.x2);
    case 1:
      return min(b.x1, b.x2) - a.x2;
    case 2:
      return a.y2 - max(b.y1, b.y2);
    case 3:
      return min(b.y1, b.y2) - a.y2;
  }
}
inline int odis(node a, node b) { return abs(a.x2 - b.x2) + abs(a.y2 - b.y2); }
void move(int& x, int& y, int d, int t) { x += t * dx[d], y += t * dy[d]; }
int main() {
  n = gi(), m = gi();
  for (int i = 1; i <= n; ++i)
    a[i].x1 = gi(), a[i].y1 = gi(), a[i].x2 = gi(), a[i].y2 = gi(), a[i].id = i;
  q = gi();
  for (int i = 1; i <= q; ++i) {
    int x = gi(), y = gi(), nx, ny;
    char op[2];
    scanf("%s%lld", op, &t[i]);
    if (op[0] == 'L') nx = x + 1, ny = y;
    if (op[0] == 'R') nx = x - 1, ny = y;
    if (op[0] == 'U') nx = x, ny = y - 1;
    if (op[0] == 'D') nx = x, ny = y + 1;
    a[n + i] = (node){nx, x, ny, y, i + n};
  }
  for (int T = 1; T <= 4; transform(), ++T) {
    sort(a + 1, a + 1 + n + q, cmp1), build();
    for (int i = 1; i <= n + q; ++i) {
      if (a[i].x1 < a[i].x2) {
        int now = query(a[i].y1);
        to[0][a[i].id] = a[now].id;
        dis[0][a[i].id] = odis(a[i], a[now]);
      }
      if (a[i].id <= n) update(min(a[i].y1, a[i].y2), max(a[i].y1, a[i].y2), i);
    }
  }
  for (int i = 1; i <= L; ++i)
    for (int j = 1; j <= n + q; ++j) {
      to[i][j] = to[i - 1][to[i - 1][j]];
      dis[i][j] = min(inf, dis[i - 1][j] + dis[i - 1][to[i - 1][j]]);
    }
  sort(a + 1, a + 1 + n + q, cmp2);
  for (int i = 1; i <= q; ++i) {
    int u = n + i;
    for (int j = L; ~j; --j)
      if (to[j][u] && t[i] >= dis[j][u]) t[i] -= dis[j][u], u = to[j][u];
    int x = a[u].x2, y = a[u].y2;
    if (!to[0][u]) {
      move(x, y, getdir(a[u]), min(t[i], 1ll * m));
      x = max(x, 0), x = min(x, m);
      y = max(y, 0), y = min(y, m);
      printf("%d %d\n", x, y);
      continue;
    }
    node nxt = a[to[0][u]];
    int d = getdis(a[u], nxt);
    if (d > t[i])
      move(x, y, getdir(a[u]), t[i]);
    else {
      t[i] -= d;
      move(x, y, getdir(a[u]), d);
      move(x, y, getdir(nxt), t[i]);
    }
    printf("%d %d\n", x, y);
  }
}
