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 int inf = ~0U >> 1;
const long long INF = ~0ULL >> 1;
const int maxn = 120000;
int x2[maxn], y2[maxn], nxtid[maxn];
int Dir[maxn];
int M, N;
int f[maxn][52];
long long g[maxn][52];
set<pair<int, int> > S[4][maxn];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
inline int inside(int x, int y) { return x > 0 && y > 0 && x <= N && y <= N; }
struct Segment_Tree {
  struct node {
    int l, r;
    set<pair<int, int> > s;
  } t[maxn * 5];
  void build(int p, int l, int r) {
    t[p].l = l;
    t[p].r = r;
    if (l != r) {
      build(p << 1, l, l + r >> 1);
      build(p << 1 | 1, (l + r >> 1) + 1, r);
    }
  }
  pair<int, int> query(int p, int target, int now, int Dir) {
    pair<int, int> ret;
    if (Dir == 0 || Dir == 2) {
      ret = (make_pair(inf, -1));
      set<pair<int, int> >::iterator it =
          t[p].s.lower_bound((make_pair(now, 0)));
      if (it != t[p].s.end()) ret = min(ret, *it);
    } else {
      ret = (make_pair(-inf, -1));
      set<pair<int, int> >::iterator it =
          t[p].s.upper_bound((make_pair(now, inf)));
      if (it != t[p].s.begin()) {
        --it;
        ret = max(ret, *it);
      }
    }
    if (t[p].l == t[p].r) return ret;
    int m = t[p].l + t[p].r >> 1;
    if (target <= m) {
      pair<int, int> tmp = query(p << 1, target, now, Dir);
      if (Dir == 0 || Dir == 2)
        return min(tmp, ret);
      else
        return max(tmp, ret);
    } else {
      pair<int, int> tmp = query(p << 1 | 1, target, now, Dir);
      if (Dir == 0 || Dir == 2)
        return min(tmp, ret);
      else
        return max(tmp, ret);
    }
  }
  void modify(int p, int l, int r, int x, int y) {
    if (t[p].l == l && t[p].r == r) {
      t[p].s.insert((make_pair(x, y)));
      return;
    }
    int m = t[p].l + t[p].r >> 1;
    if (r <= m)
      modify(p << 1, l, r, x, y);
    else if (l > m)
      modify(p << 1 | 1, l, r, x, y);
    else
      modify(p << 1, l, m, x, y), modify(p << 1 | 1, m + 1, r, x, y);
  }
} T[2];
pair<int, int> walk(int cur, long long maxt) {
  for (int i = (50); i >= (0); --i)
    if (f[cur][i] && maxt >= g[cur][i]) {
      maxt -= g[cur][i];
      cur = f[cur][i];
    }
  if (Dir[cur] == 0) {
    pair<int, int> tmp = T[1].query(1, x2[cur], y2[cur], Dir[cur]);
    if (tmp.second == -1) {
      if (maxt <= N - y2[cur])
        return (make_pair(x2[cur], y2[cur] + maxt));
      else
        return (make_pair(x2[cur], N));
    } else {
      if (maxt <= tmp.first - y2[cur])
        return (make_pair(x2[cur], y2[cur] + maxt));
      maxt -= (tmp.first - y2[cur]);
      return (make_pair(x2[cur] + dx[Dir[tmp.second]] * maxt,
                        tmp.first + dy[Dir[tmp.second]] * maxt));
    }
  } else if (Dir[cur] == 1) {
    pair<int, int> tmp = T[1].query(1, x2[cur], y2[cur], Dir[cur]);
    if (tmp.second == -1) {
      if (maxt <= y2[cur])
        return (make_pair(x2[cur], y2[cur] - maxt));
      else
        return (make_pair(x2[cur], 0));
    } else {
      if (maxt <= y2[cur] - tmp.first)
        return (make_pair(x2[cur], y2[cur] - maxt));
      maxt -= (y2[cur] - tmp.first);
      return (make_pair(x2[cur] + dx[Dir[tmp.second]] * maxt,
                        tmp.first + dy[Dir[tmp.second]] * maxt));
    }
  } else if (Dir[cur] == 2) {
    pair<int, int> tmp = T[0].query(1, y2[cur], x2[cur], Dir[cur]);
    if (tmp.second == -1) {
      if (maxt <= N - x2[cur])
        return (make_pair(x2[cur] + maxt, y2[cur]));
      else
        return (make_pair(N, y2[cur]));
    } else {
      if (maxt <= tmp.first - x2[cur])
        return (make_pair(x2[cur] + maxt, y2[cur]));
      maxt -= (tmp.first - x2[cur]);
      return (make_pair(tmp.first + dx[Dir[tmp.second]] * maxt,
                        y2[cur] + dy[Dir[tmp.second]] * maxt));
    }
  } else {
    pair<int, int> tmp = T[0].query(1, y2[cur], x2[cur], Dir[cur]);
    if (tmp.second == -1) {
      if (maxt <= x2[cur])
        return (make_pair(x2[cur] - maxt, y2[cur]));
      else
        return (make_pair(0, y2[cur]));
    } else {
      if (maxt <= x2[cur] - tmp.first)
        return (make_pair(x2[cur] - maxt, y2[cur]));
      maxt -= (x2[cur] - tmp.first);
      return (make_pair(tmp.first + dx[Dir[tmp.second]] * maxt,
                        y2[cur] + dy[Dir[tmp.second]] * maxt));
    }
  }
}
int main() {
  scanf("%d%d", &M, &N);
  T[0].build(1, 0, N);
  T[1].build(1, 0, N);
  for (int i = (1); i <= (M); ++i) {
    int x1, y1;
    scanf("%d%d%d%d", &x1, &y1, &x2[i], &y2[i]);
    if (x1 == x2[i]) {
      if (y1 < y2[i]) {
        T[0].modify(1, y1, y2[i], x1, i);
        Dir[i] = 0;
        S[0][x1].insert((make_pair(y1, i)));
      } else {
        T[0].modify(1, y2[i], y1, x2[i], i);
        Dir[i] = 1;
        S[1][x1].insert((make_pair(y1, i)));
      }
    } else if (x1 < x2[i]) {
      T[1].modify(1, x1, x2[i], y1, i);
      Dir[i] = 2;
      S[2][y1].insert((make_pair(x1, i)));
    } else {
      T[1].modify(1, x2[i], x1, y1, i);
      Dir[i] = 3;
      S[3][y1].insert((make_pair(x1, i)));
    }
  }
  for (int i = (1); i <= (M); ++i) {
    if (Dir[i] == 0) {
      pair<int, int> tmp = T[1].query(1, x2[i], y2[i], Dir[i]);
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = N - y2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = tmp.first - y2[i] + abs(x2[tmp.second] - x2[i]);
      }
    } else if (Dir[i] == 1) {
      pair<int, int> tmp = T[1].query(1, x2[i], y2[i], Dir[i]);
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = y2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = y2[i] - tmp.first + abs(x2[tmp.second] - x2[i]);
      }
    } else if (Dir[i] == 2) {
      pair<int, int> tmp = T[0].query(1, y2[i], x2[i], Dir[i]);
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = N - x2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = tmp.first - x2[i] + abs(y2[tmp.second] - y2[i]);
      }
    } else {
      pair<int, int> tmp = T[0].query(1, y2[i], x2[i], Dir[i]);
      if (tmp.second == -1)
        f[i][0] = 0, g[i][0] = x2[i];
      else {
        f[i][0] = tmp.second;
        g[i][0] = x2[i] - tmp.first + abs(y2[tmp.second] - y2[i]);
      }
    }
  }
  for (int j = (1); j <= (50); ++j)
    for (int i = (1); i <= (M); ++i) {
      if (g[i][j - 1] < INF && g[f[i][j - 1]][j - 1] < INF)
        g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1];
      else
        g[i][j] = INF;
      if (g[i][j] > (long long)1e16) g[i][j] = INF;
      f[i][j] = f[f[i][j - 1]][j - 1];
    }
  int Q;
  scanf("%d", &Q);
  for (int _ = (1); _ <= (Q); ++_) {
    char is[5];
    int curx, cury, dir;
    long long maxt;
    scanf("%d%d%s%lld", &curx, &cury, is, &maxt);
    if (is[0] == 'U')
      dir = 0;
    else if (is[0] == 'D')
      dir = 1;
    else if (is[0] == 'R')
      dir = 2;
    else
      dir = 3;
    if (dir == 0) {
      pair<int, int> tmp = T[1].query(1, curx, cury, dir);
      set<pair<int, int> >::iterator it =
          S[1][curx].lower_bound((make_pair(cury, 0)));
      if (it != S[1][curx].end() && max(y2[it->second], cury) < tmp.first) {
        long long step = max(y2[it->second], cury) - cury;
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        cury += step;
        maxt -= step;
        step = cury - y2[it->second];
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)N - cury, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = tmp.first - cury;
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    } else if (dir == 1) {
      pair<int, int> tmp = T[1].query(1, curx, cury, dir);
      set<pair<int, int> >::iterator it =
          S[0][curx].upper_bound((make_pair(cury, inf)));
      if (it != S[0][curx].begin() && min(y2[it->second], cury) > tmp.first) {
        it--;
        long long step = cury - min(y2[it->second], cury);
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        cury -= step;
        maxt -= step;
        step = y2[it->second] - cury;
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)cury, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = cury - tmp.first;
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    } else if (dir == 2) {
      pair<int, int> tmp = T[0].query(1, cury, curx, dir);
      set<pair<int, int> >::iterator it =
          S[3][cury].lower_bound((make_pair(curx, 0)));
      if (it != S[3][cury].end() && max(x2[it->second], curx) < tmp.first) {
        long long step = abs(curx - max(curx, x2[it->second]));
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        curx += step;
        maxt -= step;
        step = abs(x2[it->second] - curx);
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)N - curx, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = tmp.first - curx;
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    } else {
      pair<int, int> tmp = T[0].query(1, cury, curx, dir);
      set<pair<int, int> >::iterator it =
          S[2][cury].upper_bound((make_pair(curx, inf)));
      if (it != S[2][cury].end() && min(x2[it->second], curx) > tmp.first) {
        --it;
        long long step = abs(curx - min(x2[it->second], curx));
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[dir] * maxt),
                 int(cury + dy[dir] * maxt));
          continue;
        }
        curx -= step;
        maxt -= step;
        step = abs(x2[it->second] - curx);
        if (step > maxt) {
          printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt),
                 int(cury + dy[Dir[it->second]] * maxt));
          continue;
        }
        curx += dx[Dir[it->second]] * step;
        cury += dy[Dir[it->second]] * step;
        maxt -= step;
        pair<int, int> ans = walk(it->second, maxt);
        printf("%d %d\n", int(ans.first), int(ans.second));
        continue;
      }
      if (tmp.second == -1) {
        long long step = min((long long)curx, maxt);
        printf("%d %d\n", int(curx + dx[dir] * step),
               int(cury + dy[dir] * step));
        continue;
      }
      long long step = curx - tmp.first;
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[dir] * maxt),
               int(cury + dy[dir] * maxt));
        continue;
      }
      curx += dx[dir] * step;
      cury += dy[dir] * step;
      maxt -= step;
      step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury);
      if (step > maxt) {
        printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt),
               int(cury + dy[Dir[tmp.second]] * maxt));
        continue;
      }
      curx += dx[Dir[tmp.second]] * step;
      cury += dy[Dir[tmp.second]] * step;
      maxt -= step;
      pair<int, int> ans = walk(tmp.second, maxt);
      printf("%d %d\n", int(ans.first), int(ans.second));
    }
  }
  return 0;
}
