#include <bits/stdc++.h>
using namespace std;
inline void EnableFileIO(const string &fileName) {
  if (fileName.empty()) return;
  freopen((fileName + ".in").c_str(), "r", stdin);
  freopen((fileName + ".out").c_str(), "w", stdout);
}
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 61) - 1;
const double EPS = 1e-10, PI = acos(-1);
const int N = 1e5 + 100, LGN = 60;
int n, b;
int sx[N], sy[N], tx[N], ty[N], da[N];
int q;
int qx[N], qy[N], dq[N];
long long qt[N];
int nxt[LGN][N];
long long dis[LGN][N];
int qnxt[N];
vector<int> seg[N], todo[N];
struct Node {
  int l, r, val;
} node[N * 4];
void build(int u, int l, int r) {
  node[u].l = l, node[u].r = r;
  node[u].val = -1;
  if (r - l > 1) {
    int mid = (l + r) >> 1;
    build(u * 2, l, mid);
    build(u * 2 + 1, mid, r);
  }
}
void upd(int u, int l, int r, int k) {
  if (node[u].r <= l || r <= node[u].l) return;
  if (l <= node[u].l && node[u].r <= r) {
    node[u].val = k;
  } else {
    if (node[u].val >= 0)
      node[u * 2].val = node[u].val, node[u * 2 + 1].val = node[u].val;
    node[u].val = -1;
    upd(u * 2, l, r, k);
    upd(u * 2 + 1, l, r, k);
  }
}
int query(int u, int p) {
  if (node[u].val >= 0 || (node[u].l == p && node[u].r == p + 1)) {
    return node[u].val;
  }
  int mid = (node[u].l + node[u].r) >> 1;
  if (p < mid) return query(u * 2, p);
  return query(u * 2 + 1, p);
}
inline int getd(int sx, int sy, int tx, int ty) {
  return abs(sx - tx) + abs(sy - ty);
}
void goendless(int &x, int &y, long long &t, int d) {
  if (d == 0) {
    y = (int)min(y + t, (long long)b - 1);
  } else if (d == 1) {
    y = (int)max(y - t, (long long)0);
  } else if (d == 2) {
    x = (int)max(x - t, (long long)0);
  } else {
    x = (int)min(x + t, (long long)b - 1);
  }
  t = 0;
}
inline bool middle(int a, int b, int c) {
  if (a > c) swap(a, c);
  return a <= b && b <= c;
}
inline bool onseg(int x, int y, int segid) {
  return middle(sx[segid], x, tx[segid]) && middle(sy[segid], y, ty[segid]);
}
void go(int &x, int &y, long long &t, int &d, int segid) {
  if (segid < 0) {
    goendless(x, y, t, d);
    return;
  }
  if (onseg(x, y, segid)) d = da[segid];
  int destx = tx[segid], desty = ty[segid];
  if (d == 0) {
    int pre = desty - y;
    assert(pre >= 0);
    if (pre > t) {
      y += t, t = 0;
      return;
    }
    t -= pre, y = desty;
  } else if (d == 1) {
    int pre = y - desty;
    assert(pre >= 0);
    if (pre > t) {
      y -= t, t = 0;
      return;
    }
    t -= pre, y = desty;
  } else if (d == 2) {
    int pre = x - destx;
    assert(pre >= 0);
    if (pre > t) {
      x -= t, t = 0;
      return;
    }
    t -= pre, x = destx;
  } else {
    int pre = destx - x;
    assert(pre >= 0);
    if (pre > t) {
      x += t, t = 0;
      return;
    }
    t -= pre, x = destx;
  }
  d = da[segid];
  if (x == destx && y == desty) return;
  go(x, y, t, d, segid);
}
int main() {
  scanf("%d%d", &n, &b);
  b++;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", sx + i, sy + i, tx + i, ty + i);
    if (sx[i] == tx[i]) {
      if (sy[i] < ty[i]) {
        da[i] = 0;
      } else {
        da[i] = 1;
      }
    } else {
      if (sx[i] > tx[i]) {
        da[i] = 2;
      } else {
        da[i] = 3;
      }
    }
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    char s[10];
    scanf("%d%d%s%lld", qx + i, qy + i, s, qt + i);
    if (s[0] == 'U') dq[i] = 0;
    if (s[0] == 'D') dq[i] = 1;
    if (s[0] == 'L') dq[i] = 2;
    if (s[0] == 'R') dq[i] = 3;
  }
  memset(nxt, -1, sizeof nxt);
  memset(qnxt, -1, sizeof qnxt);
  for (int i = 0; i < b; i++) seg[i].clear(), todo[i].clear();
  build(1, 0, b);
  for (int i = 0; i < n; i++) {
    seg[max(sy[i], ty[i])].push_back(i);
  }
  for (int i = 0; i < q; i++) {
    if (dq[i] == 0) todo[qy[i]].push_back(i);
  }
  for (int i = b - 1; i >= 0; i--) {
    for (int k : seg[i]) {
      if (da[k] == 0) nxt[0][k] = query(1, sx[k]);
      upd(1, min(sx[k], tx[k]), max(sx[k], tx[k]) + 1, k);
    }
    for (int k : todo[i]) {
      qnxt[k] = query(1, qx[k]);
    }
  }
  for (int i = 0; i < b; i++) seg[i].clear(), todo[i].clear();
  build(1, 0, b);
  for (int i = 0; i < n; i++) {
    seg[min(sy[i], ty[i])].push_back(i);
  }
  for (int i = 0; i < q; i++) {
    if (dq[i] == 1) todo[qy[i]].push_back(i);
  }
  for (int i = 0; i < b; i++) {
    for (int k : seg[i]) {
      if (da[k] == 1) nxt[0][k] = query(1, sx[k]);
      upd(1, min(sx[k], tx[k]), max(sx[k], tx[k]) + 1, k);
    }
    for (int k : todo[i]) {
      qnxt[k] = query(1, qx[k]);
    }
  }
  for (int i = 0; i < b; i++) seg[i].clear(), todo[i].clear();
  build(1, 0, b);
  for (int i = 0; i < n; i++) {
    seg[min(sx[i], tx[i])].push_back(i);
  }
  for (int i = 0; i < q; i++) {
    if (dq[i] == 2) todo[qx[i]].push_back(i);
  }
  for (int i = 0; i < b; i++) {
    for (int k : seg[i]) {
      if (da[k] == 2) nxt[0][k] = query(1, sy[k]);
      upd(1, min(sy[k], ty[k]), max(sy[k], ty[k]) + 1, k);
    }
    for (int k : todo[i]) {
      qnxt[k] = query(1, qy[k]);
    }
  }
  for (int i = 0; i < b; i++) seg[i].clear(), todo[i].clear();
  build(1, 0, b);
  for (int i = 0; i < n; i++) {
    seg[max(sx[i], tx[i])].push_back(i);
  }
  for (int i = 0; i < q; i++) {
    if (dq[i] == 3) todo[qx[i]].push_back(i);
  }
  for (int i = b - 1; i >= 0; i--) {
    for (int k : seg[i]) {
      if (da[k] == 3) nxt[0][k] = query(1, sy[k]);
      upd(1, min(sy[k], ty[k]), max(sy[k], ty[k]) + 1, k);
    }
    for (int k : todo[i]) {
      qnxt[k] = query(1, qy[k]);
    }
  }
  for (int i = 0; i < n; i++) {
    if (nxt[0][i] < 0) continue;
    dis[0][i] = getd(tx[i], ty[i], tx[nxt[0][i]], ty[nxt[0][i]]);
  }
  for (int k = 0; k + 1 < LGN; k++) {
    for (int i = 0; i < n; i++) {
      if (nxt[k][i] < 0) continue;
      nxt[k + 1][i] = nxt[k][nxt[k][i]];
      dis[k + 1][i] = min(LINF, dis[k][i] + dis[k][nxt[k][i]]);
    }
  }
  for (int i = 0; i < q; i++) {
    go(qx[i], qy[i], qt[i], dq[i], qnxt[i]);
    int j = qnxt[i];
    for (int k = LGN - 1; k >= 0; k--) {
      if (j >= 0 && nxt[k][j] >= 0 && dis[k][j] <= qt[i]) {
        qt[i] -= dis[k][j];
        j = nxt[k][j];
        qx[i] = tx[j], qy[i] = ty[j], dq[i] = da[j];
      }
    }
    if (qt[i] > 0 && j >= 0) go(qx[i], qy[i], qt[i], dq[i], nxt[0][j]);
    printf("%d %d\n", qx[i], qy[i]);
  }
  return 0;
}
