#include<iostream>
 #include<vector>
 #include<stack>
 #include<queue>
 #include<unordered_map>
 #include<string>
 #include<map>
 #include<cstring>
 #include<algorithm>
 using namespace  std;
    int main() 
    {
        
        int n;
        cin>>n;
        map<int,int> m;
        for(int i=0;i<n;i++)
        {
            int a,b;
            cin>>a>>b;
            m[a]=b;
        }
        int qn;
        cin>>qn;
        vector<int> fir;
        vector<int> sec;
        for(int i=0;i<qn;i++)
        {
            int aa,bb;
            cin>>aa>>bb;
            fir.push_back(aa);
            sec.push_back(bb);
        }
        vector<int> ans;
        for(int i=0;i<qn;i++)
        {
            bool flag1=false;
            bool flag2=false;
            if(m.find(fir[i])==m.end()&&m.find(sec[i])==m.end())
            {
                ans.push_back(0);
                break;
            }
            int temp=fir[i];
            while(m.find(temp)!=m.end())
            {
                if(m[temp]==sec[i])
                {
                    flag1=true;
                    ans.push_back(2);
                     break;
                }
                else
                {
                    temp=m[temp];
                }
            }
            int temp2=sec[i];
            while(m.find(temp2)!=m.end())
            {
                if(m[temp2]==fir[i])
                {
                    flag2=true;
                    ans.push_back(1);
                     break;
                }
                else
                {
                    temp2=m[temp2];
                }
            }
            if(!flag1&&!flag2)
               ans.push_back(0);
        }
        int size2=ans.size();
        for(int i=0;i<size2;i++)
        {
            cout<<ans[i]<<endl;
        }
        
        
        
         
        
        
        
         return 0;
        
    }