#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI= acos ( - 1.0 ) ;
#define t1(x) cerr<<#x<<"="<<x<<endl
#define t2(x, y) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define t3(x, y, z) cerr<<#x<<"=" <<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define t4(a,b,c,d) cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<endl
#define t5(a,b,c,d,e) cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<endl
#define t6(a,b,c,d,e,f) cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<" "<<#f<<"="<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);
/*-------------------------------------------------------------------------------------------------------------------------------------*/
#define MOD 1000000007
#define endl "\n"
#define int long long // remove when constraints are tight.
#define inf 1e18
#define ld long double
/*-------------------------------------------------------------------------------------------------------------------------------------*/
const int N= 2e5 + 100 ;
int n;
vector< int > adj[ N] ;
/*
Link to tutorial: https://w...content-available-to-author-only...r.com/community/competitive-programming/tutorials/range-minimum-query-and-lowest-common-ancestor/
*/
const int LOGMAX= 20 ;
int T[ N] ; // T[i] returns father of node i
int P[ N] [ LOGMAX] ; // P[i][j]= 2^jth ancestor of i
int L[ N] ; // L[i]=level of node i
void dfs_lca( int node,int par,int level)
{
L[ node] = level;
T[ node] = par;
for ( auto x: adj[ node] )
{
if ( x! = par)
{
dfs_lca( x,node,level+ 1 ) ;
}
}
}
void preprocess( )
{
dfs_lca( 1 ,0 ,0 ) ;
memset ( P,- 1 ,sizeof ( P) ) ;
for ( int i= 1 ; i<= n; i++ )
{
P[ i] [ 0 ] = T[ i] ;
}
for ( int log = 1 ; log < LOGMAX; log ++ )
{
for ( int i= 1 ; i<= n; i++ )
{
if ( P[ i] [ log- 1 ] ! = - 1 )
{
P[ i] [ log ] = P[ P[ i] [ log- 1 ] ] [ log- 1 ] ;
}
}
}
}
int LCA( int p,int q)
{
//make sure level of p>= level of q
// t(p,q);
// t(L[p],L[q]);
if ( L[ q] > L[ p] )
{
swap( p,q) ;
}
// t(p,q);
// t(L[p],L[q]);
int loglp;
for ( loglp= 1 ; 1 << loglp <= L[ p] ; loglp++ )
{ }
loglp-- ;
// make p=(the ancestor of p which is at same level as that of q)
for ( int log = loglp; log >= 0 ; log-- )
{
if ( L[ p] - ( 1 << log ) >= L[ q] )
{
// t(p,log);
p= P[ p] [ log ] ;
// t(p);
}
}
// now p and q are at same level.
// t(p,q);
if ( p== q)
{
return p;
}
for ( int i= loglp; i>= 0 ; i-- )
{
if ( P[ p] [ i] ! = - 1 && P[ p] [ i] ! = P[ q] [ i] )
{
p= P[ p] [ i] ;
q= P[ q] [ i] ;
}
}
return T[ p] ;
}
int32_t main( )
{
ios:: sync_with_stdio ( 0 ) ; cin .tie ( 0 ) ;
cin >> n;
int m;
cin >> m;
for ( int i= 0 ; i< m; i++ )
{
int u,v;
cin >> u>> v;
adj[ u] .push_back ( v) ; adj[ v] .push_back ( u) ;
}
preprocess( ) ;
// for(int i=1;i<=n;i++)
// {
// for(int j=0;j<LOGMAX;j++)
// {
// cout<<P[i][j]<<" ";
// }
// cout<<endl;
// }
int q;
cin >> q;
while ( q-- )
{
int c,d;
cin >> c>> d;
int lca= LCA( c,d) ;
int ans= max( 0LL,L[ c] - L[ ( lca) ] - 1 ) + max( 0LL,L[ d] - L[ lca] - 1 ) ;
if ( lca! = c && lca! = d) ans++ ;
// t(c,lca,d);
cout << ans<< endl;
}
}
I3ByYWd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