#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const long long INF = 1000000000000000000ll;
int field[31][31];
long long reached[31][31][4];
bool between(int a, int b, int c) { return a <= b && b < c; }
int main() {
  ios_base::sync_with_stdio(false);
  int n, b;
  cin >> n >> b;
  memset(field, -1, sizeof(field));
  while (n--) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    if (x1 == x2 && y1 > y2) {
      for (int i = y2; i <= y1; ++i) {
        field[i][x1] = 3;
      }
    } else if (x1 == x2 && y1 < y2) {
      for (int i = y1; i <= y2; ++i) {
        field[i][x1] = 1;
      }
    } else if (y1 == y2 && x1 < x2) {
      for (int i = x1; i <= x2; ++i) {
        field[y1][i] = 0;
      }
    } else if (y1 == y2 && x1 > x2) {
      for (int i = x2; i <= x1; ++i) {
        field[y1][i] = 2;
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    for (int i = 0; i < 31; ++i) {
      for (int j = 0; j < 31; ++j) {
        for (int k = 0; k < 4; ++k) {
          reached[i][j][k] = INF;
        }
      }
    }
    int x, y;
    string w;
    long long t;
    cin >> x >> y >> w >> t;
    int dir = -1;
    switch (w[0]) {
      case 'R':
        dir = 0;
        break;
      case 'U':
        dir = 1;
        break;
      case 'L':
        dir = 2;
        break;
      case 'D':
        dir = 3;
        break;
    }
    long long elapsed = 0;
    bool escaped = false;
    while (elapsed < t && reached[y][x][dir] >= INF) {
      reached[y][x][dir] = elapsed;
      if (field[y][x] >= 0) {
        dir = field[y][x];
      }
      y += dy[dir];
      x += dx[dir];
      if (!between(0, y, b + 1) || !between(0, x, b + 1)) {
        y -= dy[dir];
        x -= dx[dir];
        escaped = true;
        break;
      }
      ++elapsed;
    }
    if (escaped || elapsed == t) {
      cout << x << " " << y << endl;
      continue;
    }
    long long cycle = elapsed - reached[y][x][dir];
    elapsed += ((t - elapsed) / cycle) * cycle;
    while (elapsed < t) {
      if (field[y][x] >= 0) {
        dir = field[y][x];
      }
      y += dy[dir];
      x += dx[dir];
      ++elapsed;
    }
    cout << x << " " << y << endl;
  }
  return 0;
}
