#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int dy[] = {0, 1, -1, 0};
const int dx[] = {1, 0, 0, -1};

int main()
{
    int w, h, m;
    cin >> w >> h >> m;

    vector<int> px(2*m), py(2*m);
    map<int, int> mx = { make_pair(0, -1), make_pair(1, -1), make_pair(2*w, -1) };
    map<int, int> my = { make_pair(0, -1), make_pair(1, -1), make_pair(2*h, -1) };
    for(int i=0; i<2*m; ++i){
        cin >> px[i] >> py[i];
        for(int j=0; j<2; ++j){
            if(px[i] * 2 + j <= 2 * w)
                mx[px[i] * 2 + j];
            if(py[i] * 2 + j <= 2 * h)
                my[py[i] * 2 + j];
        }
    }

    w = 0;
    h = 0;
    for(auto& p : mx){
        p.second = w;
        ++ w;
    }
    for(auto& p : my){
        p.second = h;
        ++ h;
    }

    vector<vector<bool> > grid(h, vector<bool>(w, false));
    for(int y=0; y<h; ++y)
        grid[y][0] = grid[y][w-1] = true;
    for(int x=0; x<w; ++x)
        grid[0][x] = grid[h-1][x] = true;
    for(int i=0; i<m; ++i){
        if(py[2*i] == py[2*i+1]){
            int y  = my[py[2*i]*2];
            int x1 = mx[px[2*i]*2];
            int x2 = mx[px[2*i+1]*2];
            if(x1 > x2)
                swap(x1, x2);
            for(int x=x1; x<=x2; ++x)
                grid[y][x] = true;
        }
        else{
            int x  = mx[px[2*i]*2];
            int y1 = my[py[2*i]*2];
            int y2 = my[py[2*i+1]*2];
            if(y1 > y2)
                swap(y1, y2);
            for(int y=y1; y<=y2; ++y)
                grid[y][x] = true;
        }
    }

    int q;
    cin >> q;
    while(--q >= 0){
        int sx, sy, gx, gy;
        cin >> sx >> sy >> gx >> gy;
        sx = (--mx.upper_bound(sx * 2))->second;
        sy = (--my.upper_bound(sy * 2))->second;
        gx = (--mx.upper_bound(gx * 2))->second;
        gy = (--my.upper_bound(gy * 2))->second;

        deque<pair<int, int> > dq;
        vector<vector<bool> > check(h, vector<bool>(w, false));
        dq.push_back(make_pair(sy, sx));
        check[sy][sx] = true;
        int n = dq.size();
        int ret = 0;
        for(;;){
            int y = dq.front().first;
            int x = dq.front().second;
            dq.pop_front();
            -- n;
            if(n < 0){
                n = dq.size();
                ++ ret;
            }
            if(y == gy && x == gx){
                cout << ret << endl;
                break;
            }

            for(int i=0; i<4; ++i){
                int y2 = y + dy[i];
                int x2 = x + dx[i];
                if(y2 == 0 || y2 == h-1 || x2 == 0 || x2 == w-1 || check[y2][x2])
                    continue;

                check[y2][x2] = true;
                if(grid[y2][x2]){
                    dq.push_back(make_pair(y2, x2));
                }
                else{
                    dq.push_front(make_pair(y2, x2));
                    ++ n;
                }
            }
        }
    }

    return 0;
}

Measured memory_bytes: 1356