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>
#pragma G++ optimize(3)
using namespace std;
set<pair<int, int> > fnd[2][1010];
int tp[1010];
int des[1010][2];
int n, b;
int q;
int dir[1010];
int typ, dr;
int x, y;
bool vis[1010];
int nxt[1010][61];
long long len[1010][61];
int lst;
int myabs(int x) {
  if (x < 0) return -x;
  return x;
}
void build() {
  int cur, tm;
  while (1) {
    if (vis[lst]) return;
    vis[lst] = true;
    if (!typ) {
      set<pair<int, int> >::iterator it =
          fnd[0][x].lower_bound(make_pair(y, 0));
      if (dr == -1 && (*it).first != y) it--;
      tm = myabs((*it).first - y);
      y = (*it).first;
      if (y == -1 || y == b + 1) {
        nxt[lst][0] = 0;
        len[lst][0] = tm;
        return;
      }
      cur = (*it).second;
      typ = tp[cur];
      dr = dir[cur];
    } else {
      set<pair<int, int> >::iterator it =
          fnd[1][y].lower_bound(make_pair(x, 0));
      if (dr == -1 && (*it).first != x) it--;
      tm = myabs((*it).first - x);
      x = (*it).first;
      if (x == -1 || x == b + 1) {
        nxt[lst][0] = 0;
        len[lst][0] = tm;
        return;
      }
      cur = (*it).second;
      typ = tp[cur];
      dr = dir[cur];
    }
    if (!typ) {
      int nd = (des[cur][1] - y) / dr + 1;
      y += dr * nd;
      tm += nd;
    } else {
      int nd = (des[cur][0] - x) / dr + 1;
      x += dr * nd;
      tm += nd;
    }
    nxt[lst][0] = cur;
    len[lst][0] = tm;
    lst = cur;
  }
}
int gt(long long &t, int cur) {
  int w = 0;
  for (int i = 0; i <= 60; i++) {
    if (len[cur][i] > t || !nxt[cur][i]) {
      w = i;
      break;
    }
  }
  if (!w) return cur;
  t -= len[cur][w - 1];
  return gt(t, nxt[cur][w - 1]);
}
void solve(long long t) {
  int cur;
  if (!typ) {
    set<pair<int, int> >::iterator it = fnd[0][x].lower_bound(make_pair(y, 0));
    if (dr == -1 && (*it).first != y) it--;
    if (myabs((*it).first - y) > t) {
      y += t * dr;
      return;
    }
    t -= myabs((*it).first - y);
    y = (*it).first;
    if (y == -1 || y == b + 1) {
      if (y == -1)
        y = 0;
      else
        y = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  } else {
    set<pair<int, int> >::iterator it = fnd[1][y].lower_bound(make_pair(x, 0));
    if (dr == -1 && (*it).first != x) it--;
    if (myabs((*it).first - x) > t) {
      x += t * dr;
      return;
    }
    t -= myabs((*it).first - x);
    x = (*it).first;
    if (x == -1 || x == b + 1) {
      if (x == -1)
        x = 0;
      else
        x = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  }
  if (!typ) {
    int nd = (des[cur][1] - y) / dr;
    if (nd > t) {
      y += t * dr;
      return;
    }
    t -= nd;
    y += dr * nd;
  } else {
    int nd = (des[cur][0] - x) / dr;
    if (nd > t) {
      x += t * dr;
      return;
    }
    t -= nd;
    x += dr * nd;
  }
  cur = gt(t, cur);
  x = des[cur][0];
  y = des[cur][1];
  if (!t) return;
  dr = dir[cur];
  typ = tp[cur];
  if (!typ)
    y += dr;
  else
    x += dr;
  t--;
  if (!typ) {
    set<pair<int, int> >::iterator it = fnd[0][x].lower_bound(make_pair(y, 0));
    if (dr == -1 && (*it).first != y) it--;
    if (myabs((*it).first - y) > t) {
      y += t * dr;
      return;
    }
    t -= myabs((*it).first - y);
    y = (*it).first;
    if (y == -1 || y == b + 1) {
      if (y == -1)
        y = 0;
      else
        y = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  } else {
    set<pair<int, int> >::iterator it = fnd[1][y].lower_bound(make_pair(x, 0));
    if (dr == -1 && (*it).first != x) it--;
    if (myabs((*it).first - x) > t) {
      x += t * dr;
      return;
    }
    t -= myabs((*it).first - x);
    x = (*it).first;
    if (x == -1 || x == b + 1) {
      if (x == -1)
        x = 0;
      else
        x = b;
      return;
    }
    cur = (*it).second;
    typ = tp[cur];
    dr = dir[cur];
  }
  if (!typ) {
    int nd = (des[cur][1] - y) / dr;
    if (nd > t) {
      y += t * dr;
      return;
    }
    t -= nd;
    y += dr * nd;
  } else {
    int nd = (des[cur][0] - x) / dr;
    if (nd > t) {
      x += t * dr;
      return;
    }
    t -= nd;
    x += dr * nd;
  }
}
int main() {
  scanf("%d%d", &n, &b);
  for (int i = 1; i <= n; i++) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    des[i][0] = x2;
    des[i][1] = y2;
    if (x1 == x2) {
      tp[i] = 0;
      if (y1 < y2)
        dir[i] = 1;
      else
        dir[i] = -1;
      for (int j = y1; j != y2 + dir[i]; j += dir[i])
        fnd[0][x1].insert(make_pair(j, i)), fnd[1][j].insert(make_pair(x1, i));
    } else {
      tp[i] = 1;
      if (x1 < x2)
        dir[i] = 1;
      else
        dir[i] = -1;
      for (int j = x1; j != x2 + dir[i]; j += dir[i])
        fnd[0][j].insert(make_pair(y1, i)), fnd[1][y1].insert(make_pair(j, i));
    }
  }
  for (int i = 0; i <= b; i++)
    fnd[0][i].insert(make_pair(-1, 0)), fnd[0][i].insert(make_pair(b + 1, 0)),
        fnd[1][i].insert(make_pair(-1, 0)),
        fnd[1][i].insert(make_pair(b + 1, 0));
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    lst = i;
    x = des[i][0];
    y = des[i][1];
    dr = dir[i];
    typ = tp[i];
    if (!tp[i])
      y += dir[i];
    else
      x += dir[i];
    build();
  }
  for (int i = 1; i <= 60; i++) {
    for (int j = 1; j <= n; j++) {
      nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
      len[j][i] = len[j][i - 1] + len[nxt[j][i - 1]][i - 1];
    }
  }
  scanf("%d", &q);
  while (q--) {
    char D;
    long long t;
    scanf("%d%d%c%c%lld", &x, &y, &D, &D, &t);
    if (D == 'L' || D == 'R') {
      typ = 1;
      if (D == 'L')
        dr = -1;
      else
        dr = 1;
    } else {
      typ = 0;
      if (D == 'U')
        dr = 1;
      else
        dr = -1;
    }
    solve(t);
    printf("%d %d\n", x, y);
  }
  return 0;
}
