#include "bits/stdc++.h"
#define puba push_back
#define mapa make_pair 
#define ff first
#define ss second
#define bend(_x) (_x).begin(), (_x).end()
#define szof(_x) ((int) (_x).size())
#define TASK_NAME "trains"

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
const int MAXN = 105;

int field[MAXN][MAXN];
bool used[MAXN][MAXN];
int n, m, k;

int main() {        
    freopen(TASK_NAME ".in", "r", stdin);
    freopen(TASK_NAME ".out", "w", stdout);

    scanf("%d%d%d", &n, &m, &k);

    for (int i = 0; i < m; ++i) {
        int l, r, p;
        scanf("%d%d%d", &l, &r, &p);
        --l; --r; --p;
        for (int j = l; j < r; ++j) {
            field[p][j] = 1;
            //cerr << p << " " << j << endl;
        }
    }

    int q, s, t;
    //cin >> q;
    scanf("%d", &q);
    for (int iq = 0; iq < q; ++iq) {
        //cin >> s >> t;
        scanf("%d%d", &s, &t);
        --s; --t;

        priority_queue<tuple<int, int, int>> pq;
        for (int i = 0; i < k; ++i) {
            //if (!field[i][s]) {
                pq.push(make_tuple(0, i, s));
            //}
        }

        memset(used, 0, sizeof used);

        bool flag = false;

        while (szof(pq)) {
            int pl, st, val;
            do {
                tie(val, pl, st) = pq.top();
                pq.pop();
            } while (szof(pq) && used[pl][st]);
            if (used[pl][st]) {
                break;
            }

            cerr << pl << " " << st << " " << val << endl;
            used[pl][st] = 1;
            if (st == t) {
                cout << -val + 1 << "\n";
                //return 0;
                flag = true;
                break;
            }

            if (st < n - 1) {
                for (int i = 0; i < k; ++i) {
                    if (!used[i][st]) {                        
                        pq.push(make_tuple(val - 1, i, st));
                    }
                }
                if (!used[pl][st + 1] && !field[pl][st]) {
                    pq.push(make_tuple(val, pl, st + 1));
                }
            }

        }

        if (!flag) {
            cout << "-1\n";
        }
    }

    return 0;
}