#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const int MAXN = 32;
const int MAXB = 32;
const char dirs[4] = {'U', 'D', 'L', 'R'};
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int mar[MAXB][MAXB];
int FLAG;
int flags[MAXB][MAXB][4];
int TT[MAXB][MAXB][4];
inline int get_dir(char c) {
  if (c == 'D') return 1;
  if (c == 'L') return 2;
  if (c == 'R') return 3;
  return 0;
}
struct arrow {
  int x1;
  int _dsfkjdsfksdj;
  int x2;
  int y2;
  int dir;
  void read() { scanf("%d%d%d%d", &x1, &_dsfkjdsfksdj, &x2, &y2); }
  arrow() {}
  arrow(int X1, int Y1, int X2, int Y2) {
    x1 = X1, _dsfkjdsfksdj = Y1, x2 = X2, y2 = Y2;
  }
  void get_dir() {
    if (x1 == x2) {
      if (_dsfkjdsfksdj < y2)
        dir = 0;
      else
        dir = 1;
      return;
    }
    if (x1 < x2)
      dir = 3;
    else
      dir = 2;
  }
};
int n, m, B;
arrow ar[MAXN];
int XS, YS;
char SDIR;
long long T;
int dir;
inline void init() {
  scanf("%d%d", &n, &B);
  for (int i = 0; i < n; ++i) {
    ar[i].read();
    ar[i].get_dir();
  }
  memset(mar, -1, sizeof mar);
  for (int id = 0; id < n; ++id) {
    int x = ar[id].x1;
    int y = ar[id]._dsfkjdsfksdj;
    int dr = ar[id].dir;
    for (;;) {
      mar[x][y] = id;
      if (x == ar[id].x2 && y == ar[id].y2) break;
      x += dx[dr];
      y += dy[dr];
    }
  }
  memset(flags, 0, sizeof flags);
  FLAG = 123;
}
inline int inner(int x, int y) {
  return (x >= 0 && y >= 0 && x <= B && y <= B);
}
int main() {
  init();
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d%d\n%c%I64d", &XS, &YS, &SDIR, &T);
    dir = get_dir(SDIR);
    int CT = 0;
    int x = XS;
    int y = YS;
    if (mar[x][y] != -1) dir = ar[mar[x][y]].dir;
    ++FLAG;
    for (; CT < T; ++CT) {
      flags[x][y][dir] = FLAG;
      TT[x][y][dir] = CT;
      int nx = x + dx[dir];
      int ny = y + dy[dir];
      if (!inner(nx, ny)) break;
      x = nx;
      y = ny;
      if (mar[x][y] != -1) dir = ar[mar[x][y]].dir;
      if (flags[x][y][dir] == FLAG) {
        int P = (CT + 1) - TT[x][y][dir];
        long long DT = T - (CT + 1);
        DT %= P;
        T = (long long)CT + 1 + DT;
      }
    }
    printf("%d %d\n", x, y);
  }
  return 0;
}
