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;
int N, B, Q, SArr[100001];
int Next[51][100001];
long long dist[51][100001], Time[100001];
int dis(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }
struct Segment {
  int x1, y1, x2, y2, dir;
} seg[100001];
int IT[300001], SZ, LastC;
struct point {
  int x, y;
} Res[100001], Last[100001];
struct Xdir {
  int x1, y1, x2, y2, num, dir, tx;
  bool operator<(const Xdir &p) const { return tx < p.tx; }
} X[100001];
struct Ydir {
  int x1, y1, x2, y2, num, dir, ty;
  bool operator<(const Ydir &p) const { return ty < p.ty; }
} Y[100001];
struct vec {
  int t, dir, num;
} tp;
vector<vec> vecX[100001], vecY[100001];
void input() {
  scanf("%d%d", &N, &B);
  int i, x1, y1, x2, y2, dir;
  for (i = 1; i <= N; i++) {
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    if (x1 == x2)
      dir = y1 < y2 ? 0 : 1;
    else
      dir = x1 < x2 ? 3 : 2;
    seg[i].x1 = x1, seg[i].y1 = y1, seg[i].x2 = x2, seg[i].y2 = y2,
    seg[i].dir = dir;
    X[i].x1 = x1, X[i].y1 = y1, X[i].x2 = x2, X[i].y2 = y2, X[i].dir = dir,
    X[i].num = i;
    Y[i].x1 = x1, Y[i].y1 = y1, Y[i].x2 = x2, Y[i].y2 = y2, Y[i].dir = dir,
    Y[i].num = i;
  }
  scanf("%d", &Q);
  char dir2[2];
  for (i = 0; i < Q; i++) {
    scanf("%d%d", &x1, &y1);
    scanf("%s", dir2);
    if (dir2[0] == 'U')
      dir = 0;
    else if (dir2[0] == 'D')
      dir = 1;
    else if (dir2[0] == 'L')
      dir = 2;
    else
      dir = 3;
    scanf("%lld", &Time[i]);
    if (dir < 2) {
      tp.t = x1, tp.dir = dir, tp.num = i;
      vecY[y1].push_back(tp);
    } else {
      tp.t = y1, tp.dir = dir, tp.num = i;
      vecX[x1].push_back(tp);
    }
  }
  SZ = 1;
  while (SZ <= B) SZ *= 2;
}
void spread(int x) {
  IT[x * 2] = IT[x * 2 + 1] = IT[x];
  IT[x] = 0;
}
void Ins(int node, int b, int e, int s, int l, int x) {
  if (b == s && e == l) {
    IT[node] = x;
    return;
  }
  if (IT[node]) spread(node);
  int m = (b + e) >> 1, c1 = node << 1;
  if (s <= m) Ins(c1, b, m, s, min(l, m), x);
  if (l > m) Ins(c1 + 1, m + 1, e, max(s, m + 1), l, x);
}
int find(int node, int b, int e, int x) {
  if (IT[node]) return IT[node];
  if (b == e) return 0;
  int m = (b + e) >> 1;
  if (x > m)
    return find(node * 2 + 1, m + 1, e, x);
  else
    return find(node * 2, b, m, x);
}
void Calc(int x1, int y1, int x2, int y2, int dir, int a) {
  long long T = Time[a];
  int d;
  if (T > dis(x1, y1, x2, y2)) {
    Res[a].x = x2, Res[a].y = y2;
    return;
  }
  Res[a].x = x1, Res[a].y = y1;
  if (dir < 2) {
    d = abs(y2 - y1);
    if (d >= T) {
      if (y1 < y2)
        Res[a].y += T;
      else
        Res[a].y -= T;
      return;
    }
    Res[a].y = y2, T -= d;
    if (x1 < x2)
      Res[a].x += T;
    else
      Res[a].x -= T;
    return;
  }
  d = abs(x2 - x1);
  if (d >= T) {
    if (x1 < x2)
      Res[a].x += T;
    else
      Res[a].x -= T;
    return;
  }
  Res[a].x = x2, T -= d;
  if (y1 < y2)
    Res[a].y += T;
  else
    Res[a].y -= T;
  return;
}
void Make_GraphX(int dir) {
  int i, st, ed, t, d, t2, pm, px, j, x, y;
  for (i = 1; i <= SZ + B + 1; i++) IT[i] = 0;
  if (dir == 3)
    st = N, ed = 0, pm = -1, px = B;
  else
    st = 1, ed = N + 1, pm = 1, px = 0;
  if (dir == 3)
    for (i = 1; i <= N; i++) X[i].tx = max(X[i].x1, X[i].x2);
  else
    for (i = 1; i <= N; i++) X[i].tx = min(X[i].x1, X[i].x2);
  sort(X + 1, X + N + 1);
  for (i = st;; i += pm) {
    while (i == ed || px != X[i].tx) {
      if (px == -1 || px == B + 1) break;
      for (j = 0; j < vecX[px].size(); j++) {
        tp = vecX[px][j];
        if (tp.dir != dir) continue;
        t = find(1, 0, SZ - 1, tp.t);
        t2 = tp.num;
        if (t) {
          d = dis(px, tp.t, seg[t].x2, seg[t].y2);
          if (d <= Time[t2]) {
            Time[t2] -= d;
            SArr[t2] = t;
            continue;
          }
          Calc(px, tp.t, seg[t].x2, seg[t].y2, dir, t2);
        } else {
          x = px, y = tp.t;
          if (dir == 3)
            x = B;
          else
            x = 0;
          Calc(px, tp.t, x, y, dir, t2);
        }
      }
      px += pm;
    }
    if (i == ed) break;
    if (dir != X[i].dir) {
      Ins(1, 0, SZ - 1, min(X[i].y1, X[i].y2), max(X[i].y1, X[i].y2), X[i].num);
      continue;
    }
    t = find(1, 0, SZ - 1, X[i].y1);
    t2 = X[i].num;
    if (t) {
      d = dis(X[i].x2, X[i].y2, seg[t].x2, seg[t].y2);
      Next[0][t2] = t;
      dist[0][t2] = d;
      continue;
    }
    x = X[i].x2, y = X[i].y2;
    if (dir == 2)
      d = x, x = 0;
    else
      d = B - x, x = B;
    ++LastC;
    Next[0][t2] = N + LastC;
    dist[0][t2] = d;
    Last[LastC].x = x, Last[LastC].y = y;
  }
}
void Make_GraphY(int dir) {
  int i, st, ed, t, d, t2, pm, py, j, x, y;
  for (i = 1; i <= SZ + B + 1; i++) IT[i] = 0;
  if (dir == 0)
    st = N, ed = 0, pm = -1, py = B;
  else
    st = 1, ed = N + 1, pm = 1, py = 0;
  if (dir == 0)
    for (i = 1; i <= N; i++) Y[i].ty = max(Y[i].y1, Y[i].y2);
  else
    for (i = 1; i <= N; i++) Y[i].ty = min(Y[i].y1, Y[i].y2);
  sort(Y + 1, Y + N + 1);
  for (i = st;; i += pm) {
    while (i == ed || py != Y[i].ty) {
      if (py == -1 || py == B + 1) break;
      for (j = 0; j < vecY[py].size(); j++) {
        tp = vecY[py][j];
        if (tp.dir != dir) continue;
        t = find(1, 0, SZ - 1, tp.t);
        t2 = tp.num;
        if (t) {
          d = dis(tp.t, py, seg[t].x2, seg[t].y2);
          if (d <= Time[t2]) {
            Time[t2] -= d;
            SArr[t2] = t;
            continue;
          }
          Calc(tp.t, py, seg[t].x2, seg[t].y2, dir, t2);
        } else {
          x = tp.t, y = py;
          if (dir == 0)
            y = B;
          else
            y = 0;
          Calc(tp.t, py, x, y, dir, t2);
        }
      }
      py += pm;
    }
    if (i == ed) break;
    if (dir != Y[i].dir) {
      Ins(1, 0, SZ - 1, min(Y[i].x1, Y[i].x2), max(Y[i].x1, Y[i].x2), Y[i].num);
      continue;
    }
    t = find(1, 0, SZ - 1, Y[i].x1);
    t2 = Y[i].num;
    if (t) {
      d = dis(Y[i].x2, Y[i].y2, seg[t].x2, seg[t].y2);
      Next[0][t2] = t;
      dist[0][t2] = d;
      continue;
    }
    x = Y[i].x2, y = Y[i].y2;
    if (dir == 1)
      d = y, y = 0;
    else
      d = B - y, y = B;
    ++LastC;
    Next[0][t2] = N + LastC;
    dist[0][t2] = d;
    Last[LastC].x = x, Last[LastC].y = y;
  }
}
void save_2k() {
  int i, j;
  for (i = 1; i <= 50; i++) {
    for (j = 1; j <= N; j++) {
      if (Next[i - 1][j] > N)
        Next[i][j] = Next[i - 1][j], dist[i][j] = dist[i - 1][j];
      else
        Next[i][j] = Next[i - 1][Next[i - 1][j]],
        dist[i][j] = dist[i - 1][j] + dist[i - 1][Next[i - 1][j]];
    }
  }
}
void GetAnswer(int a, int x, long long T) {
  int i, d, X, Y;
  if (dist[50][x] <= T) {
    Res[a] = Last[Next[50][x] - N];
    return;
  }
  for (i = 49; i >= 0; i--) {
    if (dist[i][x] <= T) {
      T -= dist[i][x];
      x = Next[i][x];
    }
  }
  Time[a] = T;
  d = Next[0][x];
  if (d <= N)
    X = seg[d].x2, Y = seg[d].y2;
  else
    X = Last[d - N].x, Y = Last[d - N].y;
  Calc(seg[x].x2, seg[x].y2, X, Y, seg[x].dir, a);
}
void Process() {
  int i;
  for (i = 0; i < Q; i++) {
    if (SArr[i]) {
      GetAnswer(i, SArr[i], Time[i]);
    }
  }
}
void Print() {
  int i;
  for (i = 0; i < Q; i++) printf("%d %d\n", Res[i].x, Res[i].y);
}
int main() {
  input();
  Make_GraphX(2);
  Make_GraphX(3);
  Make_GraphY(0);
  Make_GraphY(1);
  save_2k();
  Process();
  Print();
  return 0;
}
