#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
int a[33][33];
int vis[33][33][4], b;
map<char, int> mp;
int dd[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
pair<int, int> solve(int x, int y, int k, long long t) {
  memset(vis, -1, sizeof(vis));
  vis[x][y][k] = 0;
  int cnt = 0, kk;
  while (cnt < t) {
    int xx = x + dd[k][0];
    int yy = y + dd[k][1];
    if (xx < 0 || xx > b || yy < 0 || yy > b) {
      break;
    }
    if (a[xx][yy] != -1)
      kk = a[xx][yy];
    else
      kk = k;
    if (vis[xx][yy][kk] != -1) {
      int p = (t - vis[xx][yy][kk]) % (vis[x][y][k] - vis[xx][yy][kk] + 1);
      return solve(xx, yy, kk, p);
    }
    vis[xx][yy][kk] = vis[x][y][k] + 1;
    x = xx, y = yy;
    k = kk;
    cnt++;
  }
  return make_pair(x, y);
}
int main() {
  mp['R'] = 2;
  mp['D'] = 1;
  mp['L'] = 3;
  mp['U'] = 0;
  int n, x, y, xx, yy;
  scanf("%d%d", &n, &b);
  memset(a, -1, sizeof(a));
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d%d", &x, &y, &xx, &yy);
    if (x == xx) {
      if (y < yy) {
        for (int j = y; j <= yy; j++) a[x][j] = 0;
      } else {
        for (int j = yy; j <= y; j++) a[x][j] = 1;
      }
    } else {
      if (x < xx) {
        for (int j = x; j <= xx; j++) a[j][y] = 2;
      } else {
        for (int j = xx; j <= x; j++) a[j][y] = 3;
      }
    }
  }
  int m;
  long long t;
  pair<int, int> ans;
  char ch[3];
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%s%I64d", &x, &y, ch, &t);
    if (a[x][y] == -1)
      ans = solve(x, y, mp[ch[0]], t);
    else
      ans = solve(x, y, a[x][y], t);
    printf("%d %d\n", ans.first, ans.second);
  }
}
