#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=(1<<18)-1;
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[18], depth;
    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<<18];
unsigned log[1<<19];
TreeNode tree(1<<19);
unsigned o[1<<17];
unsigned lca(unsigned a, unsigned b){
    if(ws[a].depth>ws[b].depth) std::swap(a, b);
    unsigned left=ws[b].depth-ws[a].depth;
    unsigned res=left;
    while(left){
        unsigned step=log[left];
        left-=(1<<step);
        b=ws[b].parent[step];
    }
    left=log[ws[b].depth];
    while(true){
        if(ws[a].parent[left]==ws[b].parent[left]){
        }else{
            res+=2<<left;
            a=ws[a].parent[left];
            b=ws[b].parent[left];
        }
        if(left==0) break;
        left--;
    }
    if(a!=b) res+=2;
    return res;
}
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<18;i++) ws[none].parent[i]=none;
    for(unsigned i=0;i<18;i++){
        for(unsigned j=(1<<i);j<(2<<i);j++) log[j]=i;
    }
    for(unsigned i=0;i<N;i++){
        ws[i].parent[0]=tree.find(ws[i].a);
        ws[i].depth=ws[ws[i].parent[0]].depth+1;
        tree.addRect(ws[i].a, ws[i].b, i);
    }
    for(unsigned k=1;k<18;k++){
        for(unsigned i=0;i<N;i++) ws[i].parent[k]=ws[ws[i].parent[k-1]].parent[k-1];
    }
    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), dsum=ws[wa].depth+ws[wb].depth;
        o[i]=lca(wa,wb);
    }
    for(unsigned i=0;i<Q;i++){
        std::cout<<o[i]<<'\n';
    }
    return 0;
}
