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;
const long long inf = (long long)1e17;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
const char dir[4] = {'U', 'R', 'D', 'L'};
inline int mabs(int x) { return (x > 0 ? x : -x); }
const int N = 400010;
int arr[N];
long long when[N], dist[N], qt[N];
int pv[55][N];
long long pt[55][N];
int xa[N], ya[N], xb[N], yb[N], arrd[N];
int wx[N], wy[N], wd[N];
int n, b, q;
bool swapx[N], swapy[N];
int ke;
int e[N], el[N], er[N], en[N], rght[N];
bool good[N];
void Sort(int l, int r) {
  int i = l, j = r;
  int x = e[(l + r) >> 1];
  int xx = el[(l + r) >> 1];
  do {
    while (e[i] < x || e[i] == x && el[i] > xx) i++;
    while (x < e[j] || x == e[j] && xx > el[j]) j--;
    if (i <= j) {
      int tmp = e[i];
      e[i] = e[j];
      e[j] = tmp;
      tmp = el[i];
      el[i] = el[j];
      el[j] = tmp;
      tmp = er[i];
      er[i] = er[j];
      er[j] = tmp;
      tmp = en[i];
      en[i] = en[j];
      en[j] = tmp;
      i++;
      j--;
    }
  } while (i <= j);
  if (l < j) Sort(l, j);
  if (i < r) Sort(i, r);
}
bool push[N];
int tree[N];
void build(int x, int l, int r) {
  push[x] = false;
  tree[x] = 0;
  if (l < r) {
    build(x + x, l, (l + r) >> 1);
    build(x + x + 1, ((l + r) >> 1) + 1, r);
  }
}
void put(int x, int l, int r, int ll, int rr, int v) {
  if (l >= ll && r <= rr) {
    push[x] = true;
    tree[x] = v;
    return;
  }
  if (push[x]) {
    push[x + x] = true;
    push[x + x + 1] = true;
    tree[x + x] = tree[x];
    tree[x + x + 1] = tree[x];
    push[x] = false;
  }
  int y = (l + r) >> 1;
  if (ll <= y) put(x + x, l, y, ll, rr, v);
  if (rr > y) put(x + x + 1, y + 1, r, ll, rr, v);
}
int get(int x, int l, int r, int p) {
  if (l == r || push[x]) return tree[x];
  int y = (l + r) >> 1;
  if (p <= y) return get(x + x, l, y, p);
  return get(x + x + 1, y + 1, r, p);
}
void get_next_all() {
  for (int i = 1; i <= n; i++) {
    swapx[i] = false;
    swapy[i] = false;
    if (xa[i] > xb[i]) {
      int tmp = xa[i];
      xa[i] = xb[i];
      xb[i] = tmp;
      swapx[i] = true;
    }
    if (ya[i] > yb[i]) {
      int tmp = ya[i];
      ya[i] = yb[i];
      yb[i] = tmp;
      swapy[i] = true;
    }
  }
  for (int i = 1; i <= n + q; i++) {
    if (wx[i] < 0 || wy[i] < 0 || wx[i] > b || wy[i] > b) {
      good[i] = false;
      arr[i] = 0;
    } else
      good[i] = true;
  }
  {
    for (int i = 1; i <= n; i++) {
      e[i - 1] = xa[i];
      el[i - 1] = ya[i];
      er[i - 1] = yb[i];
      en[i - 1] = i;
      rght[i] = xb[i];
    }
    ke = n;
    for (int i = 1; i <= n + q; i++) {
      if (wd[i] == 3 && good[i]) {
        e[ke] = wx[i];
        el[ke] = ~wy[i];
        en[ke] = i;
        ke++;
      }
    }
    Sort(0, ke - 1);
    build(1, 0, b);
    for (int i = 0; i < ke; i++)
      if (el[i] >= 0) {
        put(1, 0, b, el[i], er[i], en[i]);
      } else {
        int num = get(1, 0, b, ~el[i]);
        arr[en[i]] = num;
        when[en[i]] = e[i] - rght[num];
      }
  }
  {
    for (int i = 1; i <= n; i++) {
      e[i - 1] = -xb[i];
      el[i - 1] = ya[i];
      er[i - 1] = yb[i];
      en[i - 1] = i;
      rght[i] = -xa[i];
    }
    ke = n;
    for (int i = 1; i <= n + q; i++) {
      if (wd[i] == 1 && good[i]) {
        e[ke] = -wx[i];
        el[ke] = ~wy[i];
        en[ke] = i;
        ke++;
      }
    }
    Sort(0, ke - 1);
    build(1, 0, b);
    for (int i = 0; i < ke; i++)
      if (el[i] >= 0) {
        put(1, 0, b, el[i], er[i], en[i]);
      } else {
        int num = get(1, 0, b, ~el[i]);
        arr[en[i]] = num;
        when[en[i]] = e[i] - rght[num];
      }
  }
  {
    for (int i = 1; i <= n; i++) {
      e[i - 1] = ya[i];
      el[i - 1] = xa[i];
      er[i - 1] = xb[i];
      en[i - 1] = i;
      rght[i] = yb[i];
    }
    ke = n;
    for (int i = 1; i <= n + q; i++) {
      if (wd[i] == 2 && good[i]) {
        e[ke] = wy[i];
        el[ke] = ~wx[i];
        en[ke] = i;
        ke++;
      }
    }
    Sort(0, ke - 1);
    build(1, 0, b);
    for (int i = 0; i < ke; i++)
      if (el[i] >= 0) {
        put(1, 0, b, el[i], er[i], en[i]);
      } else {
        int num = get(1, 0, b, ~el[i]);
        arr[en[i]] = num;
        when[en[i]] = e[i] - rght[num];
      }
  }
  {
    for (int i = 1; i <= n; i++) {
      e[i - 1] = -yb[i];
      el[i - 1] = xa[i];
      er[i - 1] = xb[i];
      en[i - 1] = i;
      rght[i] = -ya[i];
    }
    ke = n;
    for (int i = 1; i <= n + q; i++) {
      if (wd[i] == 0 && good[i]) {
        e[ke] = -wy[i];
        el[ke] = ~wx[i];
        en[ke] = i;
        ke++;
      }
    }
    Sort(0, ke - 1);
    build(1, 0, b);
    for (int i = 0; i < ke; i++)
      if (el[i] >= 0) {
        put(1, 0, b, el[i], er[i], en[i]);
      } else {
        int num = get(1, 0, b, ~el[i]);
        arr[en[i]] = num;
        when[en[i]] = e[i] - rght[num];
      }
  }
  for (int i = 1; i <= n + q; i++) {
    if (arr[i] == 0) when[i] = inf;
    if (when[i] < 0) when[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    if (swapx[i]) {
      int tmp = xa[i];
      xa[i] = xb[i];
      xb[i] = tmp;
    }
    if (swapy[i]) {
      int tmp = ya[i];
      ya[i] = yb[i];
      yb[i] = tmp;
    }
  }
}
int main() {
  scanf("%d %d", &n, &b);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d %d", xa + i, ya + i, xb + i, yb + i);
    if (yb[i] > ya[i])
      arrd[i] = 0;
    else if (xb[i] > xa[i])
      arrd[i] = 1;
    else if (yb[i] < ya[i])
      arrd[i] = 2;
    else
      arrd[i] = 3;
    wx[i] = xb[i] + dx[arrd[i]];
    wy[i] = yb[i] + dy[arrd[i]];
    wd[i] = arrd[i];
  }
  scanf("%d", &q);
  for (int i = n + 1; i <= n + q; i++) {
    char foo;
    scanf("%d %d %c %I64d", wx + i, wy + i, &foo, qt + i);
    wd[i] = 0;
    for (int j = 0; j < 4; j++)
      if (foo == dir[j]) wd[i] = j;
  }
  get_next_all();
  for (int i = 1; i <= n + q; i++)
    if (arr[i] == 0)
      dist[i] = inf;
    else
      dist[i] = mabs(wx[i] - xb[arr[i]]) + mabs(wy[i] - yb[arr[i]]);
  arr[0] = 0;
  when[0] = inf;
  dist[0] = inf;
  for (int i = 0; i <= n; i++) pv[0][i] = arr[i], pt[0][i] = dist[i] + 1;
  for (int j = 1; j <= 50; j++)
    for (int i = 0; i <= n; i++) {
      pv[j][i] = pv[j - 1][pv[j - 1][i]];
      pt[j][i] = pt[j - 1][i] + pt[j - 1][pv[j - 1][i]];
      if (pt[j][i] > inf) pt[j][i] = inf;
    }
  for (int i = n + 1; i <= n + q; i++) {
    if (qt[i] <= dist[i]) {
      if (qt[i] <= when[i]) {
        long long ax = wx[i] + dx[wd[i]] * qt[i];
        long long ay = wy[i] + dy[wd[i]] * qt[i];
        if (ax < 0) ax = 0;
        if (ay < 0) ay = 0;
        if (ax > b) ax = b;
        if (ay > b) ay = b;
        printf("%d %d\n", (int)ax, (int)ay);
        continue;
      }
      long long ax = wx[i] + dx[wd[i]] * when[i];
      long long ay = wy[i] + dy[wd[i]] * when[i];
      ax += dx[arrd[arr[i]]] * (qt[i] - when[i]);
      ay += dy[arrd[arr[i]]] * (qt[i] - when[i]);
      if (ax < 0) ax = 0;
      if (ay < 0) ay = 0;
      if (ax > b) ax = b;
      if (ay > b) ay = b;
      printf("%d %d\n", (int)ax, (int)ay);
      continue;
    }
    qt[i] -= dist[i];
    int where = arr[i];
    for (int j = 50; j >= 0; j--)
      if (pt[j][where] <= qt[i]) {
        qt[i] -= pt[j][where];
        where = pv[j][where];
      }
    if (arr[where] == 0 || qt[i] <= when[where] + 1) {
      long long ax = xb[where] + dx[arrd[where]] * qt[i];
      long long ay = yb[where] + dy[arrd[where]] * qt[i];
      if (ax < 0) ax = 0;
      if (ay < 0) ay = 0;
      if (ax > b) ax = b;
      if (ay > b) ay = b;
      printf("%d %d\n", (int)ax, (int)ay);
      continue;
    }
    int ax = xb[where] + dx[arrd[where]] * (when[where] + 1);
    int ay = yb[where] + dy[arrd[where]] * (when[where] + 1);
    ax += dx[arrd[arr[where]]] * (qt[i] - (when[where] + 1));
    ay += dy[arrd[arr[where]]] * (qt[i] - (when[where] + 1));
    printf("%d %d\n", ax, ay);
  }
  return 0;
}
