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;
long long rdtsc() {
  long long tmp;
  asm("rdtsc" : "=A"(tmp));
  return tmp;
}
inline int myrand() { return abs((rand() << 15) ^ rand()); }
inline int rnd(int x) { return myrand() % x; }
const long long INF = (long long)1e16 + 1;
const long double EPS = 1e-9;
struct SegmTree {
  const static int maxn = (1 << 17);
  int a[maxn * 2];
  int n;
  void build(int _n) {
    for (n = 1; n < _n; n <<= 1)
      ;
    assert(n <= maxn);
    for (int i = 1; i < 2 * n; ++i) a[i] = -1;
  }
  int get(int v) {
    int res = -1;
    for (v += n; v > 0; v >>= 1)
      if (a[v] != -1) res = a[v];
    return res;
  }
  void make(int v, int l, int r, int l0, int r0, int nval) {
    if (l >= r0 || l0 >= r) return;
    if (l0 <= l && r <= r0) {
      a[v] = nval;
      return;
    }
    if (a[v] != -1) a[2 * v] = a[2 * v + 1] = a[v], a[v] = -1;
    int m = (l + r) / 2;
    make(2 * v, l, m, l0, r0, nval);
    make(2 * v + 1, m, r, l0, r0, nval);
  }
  void make(int l, int r, int nval) { make(1, 0, n, l, r, nval); }
} tree;
const int maxn = (int)1e5 + 10 + 1;
struct Vertex {
  int x, y;
  int g;
  Vertex() {}
  Vertex(int _x, int _y, int _g) : x(_x), y(_y), g(_g) {}
};
const int maxg = 4;
const int go[maxg + 1][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {0, 0}};
const char chgo[maxg] = {'R', 'U', 'L', 'D'};
int dir[maxn][2][2];
int dirg[maxn];
struct Event {
  int type;
  int x;
  int l, r;
  int id;
  Event() {}
  Event(int _type, int _x, int _y, int _id)
      : type(_type), x(_x), l(_y), r(-1), id(_id) {
    assert(type == 1);
  }
  Event(int _type, int _x, int _l, int _r, int _id)
      : type(_type), x(_x), l(_l), r(_r), id(_id) {
    assert(type == 2);
  }
  inline bool operator<(const Event &e) const {
    if (x != e.x) return x < e.x;
    return type > e.type;
  }
};
Event evs[3 * maxn];
int n, b;
void rotate(int &x, int &y) {
  int nx = b - y, ny = x;
  x = nx, y = ny;
}
int gov[maxn * 2];
void fillGo(int cnt, Vertex *vs) {
  for (int rot = 0; rot < maxg; ++rot) {
    int m = 0;
    for (int i = 0; i < cnt; ++i) {
      if (vs[i].g != 2) continue;
      evs[m++] = Event(1, vs[i].x, vs[i].y, i);
    }
    for (int i = 0; i < n; ++i) {
      if (dir[i][0][0] == dir[i][1][0]) {
        int l = dir[i][0][1], r = dir[i][1][1];
        if (l > r) swap(l, r);
        evs[m++] = Event(2, dir[i][0][0], l, r, i);
      } else {
        assert(dir[i][0][1] == dir[i][1][1]);
        int x =
            (dir[i][0][0] < dir[i][1][0]) ? dir[i][0][0] : (dir[i][1][0] + 1);
        evs[m++] = Event(2, x, dir[i][0][1], dir[i][1][1], i);
      }
    }
    sort(evs, evs + m);
    tree.build(b + 1);
    for (int i = 0; i < m; ++i) {
      if (evs[i].type == 1) {
        gov[evs[i].id] = tree.get(evs[i].l);
      } else {
        assert(evs[i].type == 2);
        tree.make(evs[i].l, evs[i].r + 1, evs[i].id);
      }
    }
    for (int i = 0; i < cnt; ++i) {
      rotate(vs[i].x, vs[i].y);
      vs[i].g += 1;
      if (vs[i].g == 4) vs[i].g = 0;
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 2; ++j) {
        rotate(dir[i][j][0], dir[i][j][1]);
      }
    }
  }
}
Vertex vs[maxn * 2 * 2];
int start[maxn][2];
int startDir[maxn];
long long startTime[maxn];
int mynext[maxn * 4];
int price[maxn * 4];
const int maxl = 50;
int pr[maxl][maxn * 4];
long long cs[maxl][maxn * 4];
int sign(int x) {
  if (x < 0) return -1;
  return !!x;
}
bool solve() {
  if (scanf("%d%d", &n, &b) < 2) return 0;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k) scanf("%d", &dir[i][j][k]);
    int dx = dir[i][1][0] - dir[i][0][0], dy = dir[i][1][1] - dir[i][0][1];
    if (dx == 0) {
      assert(dy != 0);
      dy /= abs(dy);
    } else {
      assert(dy == 0);
      dx /= abs(dx);
    }
    dirg[i] = -1;
    for (int g = 0; g < maxg; ++g) {
      if (go[g][0] == dx && go[g][1] == dy) dirg[i] = g;
    }
    assert(dirg[i] != -1);
    vs[cnt++] = Vertex(dir[i][1][0], dir[i][1][1], dirg[i]);
  }
  int q;
  scanf("%d", &q);
  for (int iter = 0; iter < q; ++iter) {
    char ch;
    scanf(
        "%d%d %c"
        "%lld",
        &start[iter][0], &start[iter][1], &ch, &startTime[iter]);
    startDir[iter] = find(chgo, chgo + maxg, ch) - chgo;
    assert(startDir[iter] != maxg);
    vs[cnt++] = Vertex(start[iter][0], start[iter][1], startDir[iter]);
  }
  fillGo(cnt, vs);
  int rcnt = cnt;
  for (int i = 0; i < cnt; ++i) {
    if (gov[i] == -1) {
      mynext[i] = rcnt;
      if (vs[i].g == 0) {
        price[i] = b - vs[i].x;
      } else {
        if (vs[i].g == 1)
          price[i] = b - vs[i].y;
        else {
          if (vs[i].g == 2)
            price[i] = vs[i].x;
          else {
            assert(vs[i].g == 3);
            price[i] = vs[i].y;
          }
        }
      }
      vs[rcnt] = Vertex(vs[i].x + go[vs[i].g][0] * price[i],
                        vs[i].y + go[vs[i].g][1] * price[i], 4);
      mynext[rcnt] = rcnt;
      price[rcnt] = 1;
      ++rcnt;
      continue;
    }
    int crash = gov[i];
    mynext[i] = crash;
    price[i] = abs(vs[crash].x - vs[i].x) + abs(vs[crash].y - vs[i].y);
  }
  for (int i = 0; i < rcnt; ++i) {
    pr[0][i] = mynext[i], cs[0][i] = price[i];
  }
  for (int it = 1; it < maxl; ++it) {
    for (int v = 0; v < rcnt; ++v) {
      pr[it][v] = pr[it - 1][pr[it - 1][v]];
      cs[it][v] = cs[it - 1][v] + cs[it - 1][pr[it - 1][v]];
      cs[it][v] = min(cs[it][v], INF);
    }
  }
  for (int iter = 0; iter < q; ++iter) {
    int v = iter + n;
    long long left = startTime[iter];
    for (int it = maxl - 1; it >= 0; --it) {
      if (left >= cs[it][v]) {
        left -= cs[it][v];
        v = pr[it][v];
      }
    }
    {
      assert(left <= 3 * b + 5);
      int x = vs[v].x, y = vs[v].y;
      int nx = vs[pr[0][v]].x, ny = vs[pr[0][v]].y;
      if (x != nx && y != ny) {
        int dist1 = (vs[v].g & 1) ? (ny - y) : (nx - x);
        x += go[vs[v].g][0] * min((int)left, abs(dist1));
        y += go[vs[v].g][1] * min((int)left, abs(dist1));
        left -= min((int)left, abs(dist1));
      }
      if (left > 0) {
        if (nx != x) x += sign(nx - x) * (int)left;
        if (ny != y) y += sign(ny - y) * (int)left;
      }
      printf("%d %d\n", x, y);
    }
  }
  return 1;
}
int main() {
  srand(rdtsc());
  while (1) {
    if (!solve()) break;
  }
  return 0;
}
