#include <bits/stdc++.h>
using namespace std;
int num[50][50][4];
int dir[50][50];
int edge[1000000];
int when[1000000];
int wx[1000000];
int wy[1000000];
int n, nowTim;
void go(int &x, int &y, int d) {
  if (d == 0) y++;
  if (d == 1) x++;
  if (d == 2) y--;
  if (d == 3) x--;
}
void solve(int v, long long tim) {
  nowTim++;
  vector<int> all;
  while (when[v] < nowTim) {
    when[v] = nowTim;
    all.push_back(v);
    v = edge[v];
  }
  int idx = 0;
  for (int i = 0; i < all.size(); i++) {
    if (all[i] == v) idx = i;
  }
  int len = (all.size() - idx);
  int ans;
  if (tim < all.size()) {
    ans = all[tim];
  } else {
    int gone = idx;
    int rest = (tim - gone) % len;
    ans = all[idx + rest];
  }
  cout << wx[ans] << " " << wy[ans] << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  nowTim = 0;
  int arrows;
  cin >> arrows;
  cin >> n;
  int idx = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      for (int d = 0; d < 4; d++) {
        num[i][j][d] = idx;
        wx[idx] = i;
        wy[idx] = j;
        idx++;
      }
      dir[i][j] = -1;
    }
  }
  for (int i = 0; i < idx; i++) {
    when[i] = -1;
  }
  for (int a = 0; a < arrows; a++) {
    int x0, y0, x1, y1;
    cin >> x0 >> y0 >> x1 >> y1;
    int d;
    if (x0 == x1) {
      if (y0 < y1) {
        d = 0;
      } else {
        d = 2;
      }
    } else {
      if (x0 < x1) {
        d = 1;
      } else {
        d = 3;
      }
    }
    dir[x0][y0] = d;
    dir[x1][y1] = d;
    while (x0 != x1 || y0 != y1) {
      go(x0, y0, d);
      if (x0 >= 0 && x0 <= n && y0 <= n && y0 >= 0) {
        dir[x0][y0] = d;
      }
    }
  }
  for (int x = 0; x <= n; x++) {
    for (int y = 0; y <= n; y++) {
      for (int d = 0; d < 4; d++) {
        int actDir = d;
        if (dir[x][y] != -1) {
          actDir = dir[x][y];
        }
        int x2 = x;
        int y2 = y;
        go(x2, y2, actDir);
        if (x2 > n || y2 > n || x2 < 0 || y2 < 0) {
          x2 = x;
          y2 = y;
        }
        edge[num[x][y][d]] = num[x2][y2][actDir];
      }
    }
  }
  int q;
  cin >> q;
  for (int test = 0; test < q; test++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    int d = 0;
    if (c == 'R') d = 1;
    if (c == 'D') d = 2;
    if (c == 'L') d = 3;
    if (c == 'U') d = 0;
    int v = num[x][y][d];
    long long tim;
    cin >> tim;
    solve(v, tim);
  }
  return 0;
}
