#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
struct Coord{
    unsigned x, y;
    Coord(){}
    Coord(unsigned a, unsigned b):x(a),y(b){}
};
unsigned const none=1000000;
struct TreeNode{
    TreeNode *l, *r;
    std::map<unsigned, unsigned> ws;
    unsigned halfSize;
    TreeNode(unsigned h):l(NULL),r(NULL),halfSize(h){
        ws[0]=none;
        return;
    }
    void haveL(){
        if(!l) l=new TreeNode(halfSize/2);
        return;
    }
    void haveR(){
        if(!r) r=new TreeNode(halfSize/2);
        return;
    }
    unsigned find(unsigned y){
        return (--ws.upper_bound(y))->second;
    }
    unsigned find(unsigned x, unsigned y){
        unsigned a=none;
        if(x<halfSize){
            if(l) a=l->find(x, y);
        }else{
            if(r) a=r->find(x-halfSize, y);
        }
        return a==none ? find(y) : a;
    }
    unsigned find(Coord c){
        return find(c.x, c.y);
    }
    void addRect(Coord a, Coord b, unsigned i){
        if(halfSize==0 || (a.x==0&&b.x==2*halfSize)){
            unsigned prev=find(a.y);
            ws[a.y]=i;
            ws[b.y]=prev;
        }else{
            if(a.x<halfSize){
                haveL();
                l->addRect(a, Coord(std::min(b.x, halfSize), b.y), i);
            }
            if(b.x>halfSize){
                haveR();
                r->addRect(Coord(std::max(a.x, halfSize)-halfSize, a.y), Coord(b.x-halfSize, b.y), i);
            }
        }
        return;
    }
};
struct Window{
    Coord a, b;
    unsigned parent;
    Window(){}
    Window(Coord x, Coord y):a(x),b(y){}
    bool operator<(const Window &that) const{
        return a.x<that.a.x;
    }
};
Window ws[1<<20];
TreeNode tree(1<<19);
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    unsigned N;
    std::cin>>N;
    for(unsigned i=0;i<N;i++){
        std::cin>>ws[i].a.x>>ws[i].a.y>>ws[i].b.x>>ws[i].b.y;
    }
    std::sort(ws, ws+N);
    for(unsigned i=0;i<N;i++){
        ws[i].parent=tree.find(ws[i].a);
        tree.addRect(ws[i].a, ws[i].b, i);
    }
    unsigned Q;
    std::cin>>Q;
    for(unsigned i=0;i<Q;i++){
        Coord a, b;
        std::cin>>a.x>>a.y>>b.x>>b.y;
        unsigned wa=tree.find(a), wb=tree.find(b);
        std::vector<unsigned> va, vb;
        while(wa!=none){
            va.push_back(wa);
            wa=ws[wa].parent;
        }
        va.push_back(none);
        while(wb!=none){
            vb.push_back(wb);
            wb=ws[wb].parent;
        }
        vb.push_back(none);
        std::reverse(va.begin(), va.end());
        std::reverse(vb.begin(), vb.end());
        unsigned k=0;
        for(k=0;k<va.size()&&k<vb.size();k++) if(va[k]!=vb[k]) break;
        std::cout<<va.size()+vb.size()-2*k<<'\n';
    }
    return 0;
}
