#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int W, H, n;
    cin >> W >> H >> n;
    vector<int> zy = {0, H}, zx = {0, W};
    vector<array<int, 3>> A, B;
    for (int i = 0; i < n; ++i) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        zx.emplace_back(x1);
        zx.emplace_back(x2);
        zy.emplace_back(y1);
        zy.emplace_back(y2);
        if(x1 == x2){
            B.emplace_back(array<int, 3>{x1, y1, y2});
        }else {
            A.emplace_back(array<int, 3>{x1, x2, y1});
        }
    }

    sort(zy.begin(), zy.end());
    zy.erase(unique(zy.begin(), zy.end()), zy.end());
    sort(zx.begin(), zx.end());
    zx.erase(unique(zx.begin(), zx.end()), zx.end());
    auto fx = [&](int x){ return lower_bound(zx.begin(),zx.end(), x) - zx.begin(); };
    auto fy = [&](int y){ return lower_bound(zy.begin(),zy.end(), y) - zy.begin(); };
    int h = zy.size(), w = zx.size();
    vector<vector<pair<int, int>>> P(h), Q(w);
    for (int i = 0; i < A.size(); ++i) {
        P[fy(A[i][2])].emplace_back(A[i][0], A[i][1]);
    }
    for (int i = 0; i < B.size(); ++i) {
        Q[fx(B[i][0])].emplace_back(B[i][1], B[i][2]);
    }
    auto f = [&](int i, int j){ return i*(w-1)+j; };
    vector<vector<pair<int, int>>> G((h-1)*(w-1));
    vector<int> S(w+1);
    for (int i = 1; i < h-1; ++i) {
        fill(S.begin(),S.end(), 0);
        for (auto &&j : P[i]) {
            S[fx(j.first)]++;
            S[fx(j.second+1)-1]--;
        }
        for (int j = 0; j < w-1; ++j) {
            G[f(i-1, j)].emplace_back(f(i, j), S[j]);
            G[f(i, j)].emplace_back(f(i-1, j), S[j]);
            S[j+1] += S[j];
        }
    }
    S.resize(h+1);
    for (int i = 1; i < w-1; ++i) {
        fill(S.begin(),S.end(), 0);
        for (auto &&j : Q[i]) {
            S[fy(j.first)]++;
            S[fy(j.second+1)-1]--;
        }
        for (int j = 0; j < h-1; ++j) {
            G[f(j, i-1)].emplace_back(f(j, i), S[j]);
            G[f(j, i)].emplace_back(f(j, i-1), S[j]);
            S[j+1] += S[j];
        }
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        int s = f(fy(y1+1)-1, fx(x1+1)-1), g = f(fy(y2+1)-1, fx(x2+1)-1);
        vector<int> dist((h-1)*(w-1), INF<int>);
        dist[s] = 0;
        deque<int> que;
        que.emplace_back(s);
        while(!que.empty()){
            int x = que.front(); que.pop_front();
            for (auto &&j : G[x]) {
                if(j.second == 0){
                    if(dist[j.first] > dist[x]){
                        dist[j.first] = dist[x];
                        que.emplace_front(j.first);
                    }
                }else {
                    if(dist[j.first] > dist[x]+1){
                        dist[j.first] = dist[x]+1;
                        que.emplace_back(j.first);
                    }
                }
            }
        }
        cout << dist[g] << "\n";
    }
    return 0;
}


Measured memory_bytes: 4372