#include <bits/stdc++.h>   using namespace std;   int to[55][35][35][4][3];   int dx[] = {0, 1, 0, -1};   int dy[] = {1, 0, -1, 0};   int Sign(int x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }   int main() {     int n, b;     cin >> n >> b;     ++b;     for (int i = 0; i < b; ++i) {       for (int j = 0; j < b; ++j) {         for (int d = 0; d < 4; ++d) {           int nx = i + dx[d], ny = j + dy[d];           to[0][i][j][d][0] = nx;           to[0][i][j][d][1] = ny;           to[0][i][j][d][2] = d;         }       }     }     for (int i = 0; i < n; ++i) {       int x0, y0, x1, y1;       cin >> x0 >> y0 >> x1 >> y1;       int dx = Sign(x1 - x0), dy = Sign(y1 - y0);       int dd = 0;       for (int j = 0; j < 4; ++j) {         if (dx == ::dx[j] && dy == ::dy[j]) {           dd = j;           break;         }       }       while (x0 != x1 || y0 != y1) {         for (int j = 0; j < 4; ++j) {           to[0][x0][y0][j][0] = x0 + dx;           to[0][x0][y0][j][1] = y0 + dy;           to[0][x0][y0][j][2] = dd;         }         x0 += dx;         y0 += dy;       }       for (int j = 0; j < 4; ++j) {         to[0][x0][y0][j][0] = x0 + dx;         to[0][x0][y0][j][1] = y0 + dy;         to[0][x0][y0][j][2] = dd;       }     }     for (int i = 1; i < 55; ++i) {       for (int x = 0; x < b; ++x) {         for (int y = 0; y < b; ++y) {           for (int j = 0; j < 4; ++j) {             int nx = to[i - 1][x][y][j][0], ny = to[i - 1][x][y][j][1],                 dd = to[i - 1][x][y][j][2];             if (nx < 0 || nx >= b || ny < 0 || ny >= b) {               to[i][x][y][j][0] = nx;               to[i][x][y][j][1] = ny;               to[i][x][y][j][2] = -1;             } else {               to[i][x][y][j][0] = to[i - 1][nx][ny][dd][0];               to[i][x][y][j][1] = to[i - 1][nx][ny][dd][1];               to[i][x][y][j][2] = to[i - 1][nx][ny][dd][2];             }           }         }       }     }     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       int x, y;       char d;       long long t;       cin >> x >> y >> d >> t;       int dir = 0;       if (d ==  L ) {         dir = 3;       } else if (d ==  R ) {         dir = 1;       } else if (d ==  D ) {         dir = 2;       } else {         dir = 0;       }       for (int j = 0; t; ++j, t >>= 1) {         if (t & 1) {           int nx = to[j][x][y][dir][0];           int ny = to[j][x][y][dir][1];           dir = to[j][x][y][dir][2];           x = nx;           y = ny;         }         if (x < 0 || x >= b || y < 0 || y >= b) {           break;         }       }       x = max(0, min(x, b - 1));       y = max(0, min(y, b - 1));       cout << x <<     << y <<   n ;     }     return 0;   }   