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;
set<pair<int, int> > fnd[2][1010];
int tp[1010];
int des[1010][2];
int n, b;
int q;
int dir[1010];
pair<long long, int> vis[1010];
int typ, dr;
int x, y;
long long t;
long long ort;
long long cyc;
int myabs(int x) {
  if (x < 0) return -x;
  return x;
}
bool solve() {
  for (int i = 1; i <= n; i++) vis[i] = make_pair(-1, -1);
  int cur;
  while (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 += dr * t;
        return true;
      }
      t -= myabs((*it).first - y);
      y = (*it).first;
      if (y == -1 || y == b + 1) {
        if (y == -1)
          y++;
        else
          y--;
        return true;
      }
      cur = (*it).second;
      typ = tp[cur];
      dr = dir[cur];
      if (vis[cur].first != -1) {
        if (!typ)
          cyc = ort - t - vis[cur].first + (vis[cur].second - y) / dr;
        else
          cyc = ort - t - vis[cur].first + (vis[cur].second - x) / dr;
        return false;
      }
      if (!typ)
        vis[cur] = make_pair(ort - t, y);
      else
        vis[cur] = make_pair(ort - t, x);
    } 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 += dr * t;
        return true;
      }
      t -= myabs((*it).first - x);
      x = (*it).first;
      if (x == -1 || x == b + 1) {
        if (x == -1)
          x++;
        else
          x--;
        return true;
      }
      cur = (*it).second;
      typ = tp[cur];
      dr = dir[cur];
      if (vis[cur].first != -1) {
        if (!typ)
          cyc = ort - t - vis[cur].first + (vis[cur].second - y) / dr;
        else
          cyc = ort - t - vis[cur].first + (vis[cur].second - x) / dr;
        return false;
      }
      if (!typ)
        vis[cur] = make_pair(ort - t, y);
      else
        vis[cur] = make_pair(ort - t, x);
    }
    if (!typ) {
      int nd = (des[cur][1] - y) / dr + 1;
      if (nd > t) {
        y += dr * t;
        return true;
      }
      y += dr * nd;
      t -= nd;
    } else {
      int nd = (des[cur][0] - x) / dr + 1;
      if (nd > t) {
        x += dr * t;
        return true;
      }
      x += dr * nd;
      t -= nd;
    }
  }
  return true;
}
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));
  scanf("%d", &q);
  while (q--) {
    char D;
    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;
    }
    ort = t;
    if (!solve()) {
      t %= cyc;
      solve();
    }
    printf("%d %d\n", x, y);
  }
  return 0;
}
