3 8
2 4 4 4
7 2 5 2
6 5 6 3
20
3 2 U 1000000
3 2 U 1
3 2 U 2
3 2 U 3
3 2 U 4
3 2 U 5
3 2 U 6
3 2 U 7
3 2 U 8
3 2 U 9
3 2 U 10
3 2 U 11
3 2 U 12
3 2 U 13
3 2 U 14
3 2 U 15
3 2 U 16
3 2 U 17
3 2 U 18
3 2 U 19
 4 2
0 0 1 0
2 0 2 1
2 2 1 2
0 2 0 1
15
1 1 R 7
0 0 U 3
1 1 R 888888888888887
0 0 U 404040404040403
2 1 D 1000000000000000
2 0 R 324354768576473
2 0 L 325436475453245
2 0 U 764536452343654
2 0 D 943654765876545
1 1 U 1000000000000000
1 1 D 989898789898789
1 1 L 123456787654344
0 1 L 0
0 1 L 1
0 1 L 999999999999993
 1 10
1 5 9 5
1
5 5 L 1
 3 7
2 4 2 7
4 7 4 6
7 3 5 3
10
6 7 L 1
6 7 L 2
6 7 L 3
6 7 L 4
6 7 L 5
6 7 L 6
6 7 L 7
6 7 L 8
6 7 L 9
6 7 L 10

                                                               0                                                                2
                                                               3                                                                3
                                                               3                                                                4
                                                               4                                                                4
                                                               5                                                                4
                                                               6                                                                4
                                                               6                                                                3
                                                               6                                                                2
                                                               5                                                                2
                                                               4                                                                2
                                                               3                                                                2
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                               0                                                                2
                                                                1                                                                0
                                                               2                                                                1
                                                               1                                                                0
                                                               2                                                                1
                                                               2                                                                1
                                                               2                                                                1
                                                               0                                                                1
                                                               0                                                                0
                                                               2                                                                1
                                                               2                                                                2
                                                               1                                                                2
                                                               0                                                                2
                                                               0                                                                1
                                                               0                                                                0
                                                               0                                                                0
                                                                6                                                                5
                                                                5                                                                7
                                                               4                                                                7
                                                               4                                                                6
                                                               4                                                                5
                                                               4                                                                4
                                                               4                                                                3
                                                               4                                                                2
                                                               4                                                                1
                                                               4                                                                0
                                                               4                                                                0

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>

using namespace std;

#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define snuke(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

typedef long long ll;

int B;
string ds = "RULD";
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
int a[1010][1010];

int S;
#define MAXS 4100000
int next[MAXS];
int dp[MAXS][25];
int dp2[MAXS];

int get_state(int x, int y, int d){
    return x + y * (B+1) + d * (B+1) * (B+1);
}

void prepare(void){
    int i,j,x,y,d;
    
    S = (B+1) * (B+1) * 4;
    
    REP(x,B+1) REP(y,B+1) REP(d,4){
        int st = get_state(x, y, d);
        int x2 = x, y2 = y, d2 = d;
        if(x2 != 0 && x2 != B && y2 != 0 && y2 != B){
            if(a[x2][y2] != -1) d2 = a[x2][y2];
            x2 += dx[d2];
            y2 += dy[d2];
        }
        next[st] = get_state(x2, y2, d2);
    }
    
    REP(i,S) dp[0][i] = next[i];
    REP(i,24) dp[i+1][j] = dp[i][dp[i][dp[i][dp[i][j]]]];
}

pair <int, int> query(int x, int y, int d, ll t){
    int start = get_state(x, y, d);
    int i;
    int goal = start;
    
    ll four = 1;
    REP(i,25){
        if(t&four) goal = dp[i][goal];
        four <<= 1;
        if(t&four){
            goal = dp[i][goal];
            goal = dp[i][goal];
        }
        four <<= 1;
    }
    
    int x2 = goal % (B+1);
    int y2 = (goal % ((B+1) * (B+1))) / (B+1);
    if(x2 == 0) x2++; if(x2 == B) x2--;
    if(y2 == 0) y2++; if(y2 == B) y2--;
    return make_pair(x2, y2);
}

int main(void){
    int N,i,j,x,y;
    
    cin >> N >> B; B += 2;
    
    REP(i,B+1) REP(j,B+1) a[i][j] = -1;
    
    REP(i,N){
        int x1,y1,x2,y2;
        cin >> x1 >> y1 >> x2 >> y2; x1++; y1++; x2++; y2++;
        
        if(y1 == y2){
            y = y1;
            if(x1 < x2) for(x=x1;x<=x2;x++) a[x][y] = 0;
            if(x1 > x2) for(x=x2;x<=x1;x++) a[x][y] = 2;
        }
        
        if(x1 == x2){
            x = x1;
            if(y1 < y2) for(y=y1;y<=y2;y++) a[x][y] = 1;
            if(y1 > y2) for(y=y2;y<=y1;y++) a[x][y] = 3;
        }
    }
    
    prepare();
    
    int Q;
    cin >> Q;
    
    REP(i,Q){
        int x,y;
        ll t;
        string dir;
        cin >> x >> y >> dir >> t;
        x++; y++;
        REP(j,4) if(dir[0] == ds[j]) break;
        pair <int, int> ans = query(x, y, j, t);
        printf("%d %d\n", ans.first-1, ans.second-1);
    }
    
    return 0;
}
