#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   int g[maxn][maxn];   int vis[maxn][maxn];   int cnt[maxn][maxn];   struct node {     int x, y;   } que[maxn * maxn];   int dx[10] = {0, 1, 0, -1, 0};   int dy[10] = {0, 0, 1, 0, -1};   map<char, int> p;   void init() {     p[ U ] = 2;     p[ D ] = 4;     p[ L ] = 3;     p[ R ] = 1;   }   void add(int x1, int y1, int x2, int y2) {     int pp = 0;     if (x1 == x2) {       if (y1 < y2)         pp = 2;       else         pp = 4;     } else {       if (x1 < x2)         pp = 1;       else         pp = 3;     }     g[x2][y2] = pp;     while (x1 != x2 || y1 != y2) {       g[x1][y1] = pp;       x1 += dx[pp];       y1 += dy[pp];     }   }   int main() {     init();     int n, m;     scanf( %d%d , &m, &n);     int x1, y1, x2, y2;     while (m--) {       scanf( %d%d%d%d , &x1, &y1, &x2, &y2);       add(x1, y1, x2, y2);     }     long long t;     char c;     int Q, x, y, vx, vy;     scanf( %d , &Q);     for (int q = 1; q <= Q; q++) {       scanf( %d %d %c %lld , &x, &y, &c, &t);       vx = dx[p[c]];       vy = dy[p[c]];       long long t0 = 0;       while (x >= 0 && x <= n && y >= 0 && y <= n && t > 0) {         if (g[x][y]) {           vx = dx[g[x][y]];           vy = dy[g[x][y]];         }         if (g[x][y] && vis[x][y] == q) {           long long t1 = cnt[x][y];           long long mod = t0 - t1;           t %= mod;           x = que[t1 + t].x;           y = que[t1 + t].y;           break;         }         vis[x][y] = q;         cnt[x][y] = t0;         que[t0].x = x;         que[t0++].y = y;         x += vx;         y += vy;         t--;       }       if (x < 0) x = 0;       if (x > n) x = n;       if (y < 0) y = 0;       if (y > n) y = n;       printf( %d %d n , x, y);     }     return 0;   }   