#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
//l,r;
bool now=0;
int l2[M],r2[M];
int ans[M];
//int ii;
void dfs(int v,int l,int r)
{
    mark[v]=now%2;
    for(auto u:adj[v])
    {
        if(mark[u.F]!=now%2 && (l>u.S || r<u.S))
            dfs(u.F,l,r);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&l2[i],&r2[i]);
    // sort(cur+1,cur+q+1);
    now=1;
    for(int ii=1;ii<=q;ii++){
        if(l2[ii]==l2[ii-1] && r2[ii]==r2[ii-1])
        {
            printf("%d\n",cnt);
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(now%2)){
                    dfs(i,l2[ii],r2[ii]),cnt++;
                }
            }
            printf("%d\n",cnt);
            now=1-now;
        }
    }
    //for(int i=1;i<=q;i++)
      //  printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
int mark[M3];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=ii;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[u]==ii || (l[ii]<=edg[v][i] && edg[v][i]<=r[ii]))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
        if(l[ii]==l[ii-1] && r[ii]==r[ii-1])
        {
            ans[cur[ii].S]=cnt;
            continue;
        }
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(mark[i]!=ii)
                dfs(i),cnt++;
        }
     //   memset(mark,0,sizeof mark);
        ans[cur[ii].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
//l,r;
bool now=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=now%2;
    for(auto u:adj[v])
    {
        if((u.S<cur[ii].F.F || u.S>cur[ii].F.S) && mark[u.F]!=now%2)
            dfs(u.F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    now=1;
    for(ii=1;ii<=q;ii++){
        if( cur[ii].F.S==cur[ii-1].F.S && cur[ii].F.F==cur[ii-1].F.F)
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(now%2)){
                    dfs(i),cnt++;
                }
            }
            ans[cur[ii].S]=cnt;
            now=1-now;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e3+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
bool mark[M];
int l2,r2;
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
        if(mark[u] || (l2<=edg[v][i] && edg[v][i]<=r2))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        cin>>cur[i].F.F>>cur[i].F.S,cur[i].S=i;
    sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(int i=1;i<=q;i++){
   //     int l,r;
        cnt=0;
        l2=l[i];
        r2=r[i];
        for(int i=1;i<=n;i++)
        {
            if(!mark[i])
                dfs(i),cnt++;
        }
        for(int i=1;i<=n;i++)
            mark[i]=0;
        ans[cur[i].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
int mark[M3];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=ii;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[u]==ii || (l[ii]<=edg[v][i] && edg[v][i]<=r[ii]))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    sort(cur+1,cur+q+1);
    for(ii=1;ii<=q;ii++){
   //     int l,r;
        if(l[ii]==l[ii-1] && r[ii]==r[ii-1])
        {
            ans[cur[ii].S]=cnt;
            continue;
        }
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(mark[i]!=ii)
                dfs(i),cnt++;
        }
     //   memset(mark,0,sizeof mark);
        ans[cur[ii].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
bool mark[M3];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[u] || (l[ii]<=edg[v][i] && edg[v][i]<=r[ii]))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
    sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
        if(l[ii]==l[ii-1] && r[ii]==r[ii-1])
        {
            ans[cur[ii].S]=cnt;
            continue;
        }
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(!mark[i])
                dfs(i),cnt++;
        }
        memset(mark,0,sizeof mark);
        ans[cur[ii].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e3+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
bool mark[M];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[u] || (l[ii]<=edg[v][i] && edg[v][i]<=r[ii]))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
    sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
        if(l[ii]==l[ii-1] && r[ii]==r[ii-1])
        {
            ans[cur[ii].S]=cnt;
            continue;
        }
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(!mark[i])
                dfs(i),cnt++;
        }
        for(int i=1;i<=n;i++)
            mark[i]=0;
        ans[cur[ii].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0,l,r;
bool now=0;
int ans[M];
//int ii;
void dfs(int v)
{
    mark[v]=now%2;
    for(auto u:adj[v])
    {
        if(mark[u.F]==(now%2) || (l<=u.S && u.S<=r))
            continue;
        dfs(u.F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    now=1;
    for(int ii=1;ii<=q;ii++){
        if( cur[ii].F.S==cur[ii-1].F.S && cur[ii].F.F==cur[ii-1].F.F)
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(now%2)){
                    l=cur[ii].F.F;
                    r=cur[ii].F.S;
                    dfs(i),cnt++;
                }
            }
            ans[cur[ii].S]=cnt;
            now=1-now;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=ii%2;
   // cerr<<v<<" ";
    for(int i=0;i<adj[v].size();i++)
    {
       // int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if((cur[ii].F.F<=adj[v][i].S && adj[v][i].S<=cur[ii].F.S) || mark[adj[v][i].F]==(ii%2))
            continue;
        dfs(adj[v][i].F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    //for(int i=1;i<=q;i++)
      //  l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
   // cerr<<ii<<" ";
        if(cur[ii].F.F==cur[ii-1].F.F && cur[ii].F.S==cur[ii-1].F.S)
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(ii%2))
                    dfs(i),cnt++;
            }
            ans[cur[ii].S]=cnt;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
bool now=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=now%2;
   // cerr<<v<<" ";
    for(auto u:adj[v])
    {
       // int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[u.F]==(now%2) || (cur[ii].F.F<=u.S && u.S<=cur[ii].F.S))
            continue;
        dfs(u.F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    //for(int i=1;i<=q;i++)
      //  l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    now=1;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
   // cerr<<ii<<" ";
        if(cur[ii].F.F==cur[ii-1].F.F && cur[ii].F.S==cur[ii-1].F.S)
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(now%2))
                    dfs(i),cnt++;
            }
            ans[cur[ii].S]=cnt;
            now=1-now;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
//l,r;
bool now=0;
int ans[M];
//int ii;
void dfs(int v,int l,int r)
{
    mark[v]=now%2;
    for(auto u:adj[v])
    {
        if(mark[u.F]!=now%2 && (l>u.S || r<u.S))
            dfs(u.F,l,r);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    now=1;
    for(int ii=1;ii<=q;ii++){
        if( cur[ii].F.S==cur[ii-1].F.S && cur[ii].F.F==cur[ii-1].F.F)
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(now%2)){
                    dfs(i,cur[ii].F.F,cur[ii].F.S),cnt++;
                }
            }
            ans[cur[ii].S]=cnt;
            now=1-now;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
//l,r;
bool now=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=now%2;
    for(auto u:adj[v])
    {
        if(mark[u.F]!=now%2 && (u.S<cur[ii].F.F || u.S>cur[ii].F.S))
            dfs(u.F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    now=1;
    for(ii=1;ii<=q;ii++){
        if( cur[ii].F.S==cur[ii-1].F.S && cur[ii].F.F==cur[ii-1].F.F)
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(now%2)){
                    dfs(i),cnt++;
                }
            }
            ans[cur[ii].S]=cnt;
            now=1-now;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
//int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
int now=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=now%2;
   // cerr<<v<<" ";
    for(int i=0;i<adj[v].size();i++)
    {
       // int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if((cur[ii].F.F<=adj[v][i].S && adj[v][i].S<=cur[ii].F.S) || mark[adj[v][i].F]==(now%2))
            continue;
        dfs(adj[v][i].F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    //for(int i=1;i<=q;i++)
      //  l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    now=1;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
   // cerr<<ii<<" ";
        if(cur[ii].F.F==cur[ii-1].F.F && cur[ii].F.S==cur[ii-1].F.S)
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(mark[i]!=(now%2))
                    dfs(i),cnt++;
            }
            ans[cur[ii].S]=cnt;
            now++;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
#pragma GCC optimize "-O3"
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e3+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
bool mark[M];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[u] || (l[ii]<=edg[v][i] && edg[v][i]<=r[ii]))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
    sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
        if(l[ii]==l[ii-1] && r[ii]==r[ii-1])
        {
            ans[cur[ii].S]=cnt;
            continue;
        }
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(!mark[i])
                dfs(i),cnt++;
        }
        for(int i=1;i<=n;i++)
            mark[i]=0;
        ans[cur[ii].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e3+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
bool mark[M];
int l2,r2;
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
        if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
        {
            i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
        }
        if(mark[u] || (l2<=edg[v][i] && edg[v][i]<=r2))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
    sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(int i=1;i<=q;i++){
   //     int l,r;
        l2=l[i];
        r2=r[i];
        if(l2==l[i-1] && r2==r[i-1])
        {
            ans[cur[i].S]=cnt;
            continue;
        }
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(!mark[i])
                dfs(i),cnt++;
        }
        for(int i=1;i<=n;i++)
            mark[i]=0;
        ans[cur[i].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=1e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
int l[M],r[M];
vector<pii>adj[500+10];
bool mark[M3];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
       // int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[adj[v][i].F]==1 || (l[ii]<=adj[v][i].S && adj[v][i].S<=r[ii]))
            continue;
        dfs(adj[v][i].F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
        if(l[ii]==l[ii-1] && r[ii]==r[ii-1])
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(!mark[i])
                    dfs(i),cnt++;
            }
            memset(mark,0,sizeof mark);
            ans[cur[ii].S]=cnt;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e4+10;
int const M2=1e6+10;
int const M3=1e4+10;
int mod=1e9+7;
int l[M],r[M];
vector<pii>adj[500+10];
bool mark[500+10];
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
int ii;
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
       // int u=adj[v][i];
       // if(edg[v][i]>=l2 && edg[v][i]<=r2 && r2-l2>=200)
       // {
       //     i=lower_bound(adj[v].begin(),adj[v].end(),r2+1)-adj[v].begin();
      //  }
        if(mark[adj[v][i].F]==1 || (l[ii]<=adj[v][i].S && adj[v][i].S<=r[ii]))
            continue;
        dfs(adj[v][i].F);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(mp(b,i)),adj[b].pb(mp(a,i));
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
     sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(ii=1;ii<=q;ii++){
   //     int l,r;
        if(l[ii]==l[ii-1] && r[ii]==r[ii-1])
        {
            ans[cur[ii].S]=cnt;
        }
        else{
            cnt=0;
            for(int i=1;i<=n;i++)
            {
                if(!mark[i])
                    dfs(i),cnt++;
            }
            memset(mark,0,sizeof mark);
            ans[cur[ii].S]=cnt;
        }
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e3+10;
int mod=1e9+7;
int l[M],r[M];
vector<int>adj[M],edg[M];
bool mark[M];
int l2,r2;
pair<pii,int> cur[M];
int cnt=0;
int ans[M];
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
        if(mark[u] || (l2<=edg[v][i] && edg[v][i]<=r2))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    for(int i=1;i<=q;i++)
        scanf("%d%d",&cur[i].F.F,&cur[i].F.S),cur[i].S=i;
    sort(cur+1,cur+q+1);
    for(int i=1;i<=q;i++)
        l[i]=cur[i].F.F,r[i]=cur[i].F.S;
    for(int i=1;i<=q;i++){
   //     int l,r;
        l2=l[i];
        r2=r[i];
        if(l2==l[i-1] && r2==r[i-1])
        {
            ans[cur[i].S]=cnt;
            continue;
        }
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(!mark[i])
                dfs(i),cnt++;
        }
        for(int i=1;i<=n;i++)
            mark[i]=0;
        ans[cur[i].S]=cnt;
    }
    for(int i=1;i<=q;i++)
        printf("%d\n",ans[i]);
}


#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#include<algorithm>
#include<map>
#include<vector>
#include <iomanip>
#include <set>
#include<cmath>
#define F first
#define S second
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define mp make_pair
#define error(x) cout<<#x<<"="<<(x)<<endl;
void sp(double y,int x){cout<<fixed<<setprecision(x)<<y;}
int inf=1e9+10;
int const M=2e5+10;
int const M2=1e6+10;
int const M3=1e3+10;
int mod=1e9+7;
int l,r;
vector<int>adj[M],edg[M];
bool mark[M];
int cnt=0;
void dfs(int v)
{
    mark[v]=1;
    for(int i=0;i<adj[v].size();i++)
    {
        int u=adj[v][i];
        if(mark[u] || (l<=edg[v][i] && edg[v][i]<=r))
            continue;
        dfs(u);
    }
}
int sa(int x,int y){if(x%y==0)return x/y;return x/y+1;}
int binp(int x,int y){int ans=1;int t=x;while(y!=0){if(y%2==1){ans*=t;ans;}y/=2;t*=t;t;}return ans;}
int main(){
	//ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
	int n,m,a,b;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
        scanf("%d%d",&a,&b),adj[a].pb(b),adj[b].pb(a),edg[a].pb(i),edg[b].pb(i);
    int q;
    cin>>q;
    while(q--)
    {
   //     int l,r;
        cnt=0;
        scanf("%d%d",&l,&r);
        for(int i=1;i<=n;i++)
        {
            if(!mark[i])
                dfs(i),cnt++;
        }
        for(int i=1;i<=n;i++)
            mark[i]=0;
        printf("%d\n",cnt);
    }
}


#include<iostream>
#include <fstream>
#include <iomanip>
#include<algorithm>
#include<string>
#include<random>
#include<vector>
#include<list>
#include<set>
#include<math.h>
#include<map>
#include<complex>

#define Ri0			for(int i=0;i<n;i++)
#define Ri1			for(int i=1;i<=n;i++)
#define lli          long long int
#define pb          push_back
#define pii         pair<lli,lli>
#define vi          vector< lli>
#define iii			tuple<lli,lli,lli>
#define ST			get<0>
#define ND			get<1>
#define RD			get<2>
#define TH			get<3>
#define pi			3.141592653589793238462
#define double      long double
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
#define ABC			26

using namespace std;

typedef complex<double>	com;

unsigned GCD(unsigned u, unsigned v) {
	unsigned r;
	while (v != 0) {
		r = u % v;
		u = v;
		v = r;
	}
	return u;
}

struct DSU
{
	vi deg;
	vi par;
	lli sum;
	DSU() {}
	DSU(lli n)
	{
		deg.resize(n, 0);
		par.resize(n, -1);
		sum = n;
	}
	lli find(lli x)
	{
		if (par[x] == -1) return x;
		return par[x] = find(par[x]);
	}
	void uni(lli x, lli y)
	{
		x = find(x); y = find(y);
		if ((x != -1 || y != -1) && x == y) return;
		sum--;
		if (deg[x] < deg[y]) par[x] = y;
		else {
			deg[x] += (deg[x] == deg[y]);
			par[y] = x;
		}
	}
};

lli mod = 4001, inf = 4e18;
lli sum = 0;
lli tmp3 = 0, tmp = 0, tmp2 = 0, comb = 1;
vector<lli> arr, check, depth,st;
lli n, m = 0, q;
lli maxi = 0, mini = inf;
string s2, s;
vector<vi> g;
lli high = 0;
vector<pii> edge;
vector<DSU> suf, pre;

int main()
{
	ios::sync_with_stdio(false), cin.tie(nullptr);
	lli k;
	cin >> n >> m;
	edge.resize(n), suf.resize(m), pre.resize(m);
	DSU dsu(n),dsur(n);
	for (int i = 0; i < m; pre[i]=dsu,i++)
	{
		cin >> edge[i].first >> edge[i].second;
		edge[i].second -= 1; edge[i].first -= 1;
		dsu.uni(edge[i].first, edge[i].second);
	}
	for (int i = m - 1; i > 0; dsur.uni(edge[i].first, edge[i].second), suf[i] = dsur, i--);
	cin >> k;
	for (lli l,r; k--;)
	{
		cin >> l >> r;
		--l; --r;
		DSU t(n);
		if (l > 0) t = pre[l-1];
		if (r < m - 1)
		{
			for (int i = 0; i < n; i++) if (suf[r + 1].par[i] != -1) t.uni(suf[r + 1].par[i], i);
		}
		cout << t.sum<<endl;
	}
	return 0;
}


#include<iostream>
#include <fstream>
#include <iomanip>
#include<algorithm>
#include<string>
#include<random>
#include<vector>
#include<list>
#include<set>
#include<math.h>
#include<map>
#include<complex>

#define Ri0			for(int i=0;i<n;i++)
#define Ri1			for(int i=1;i<=n;i++)
#define lli          long long int
#define pb          push_back
#define pii         pair<lli,lli>
#define vi          vector< lli>
#define iii			tuple<lli,lli,lli>
#define ST			get<0>
#define ND			get<1>
#define RD			get<2>
#define TH			get<3>
#define pi			3.141592653589793238462
#define double      long double
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
#define ABC			26

using namespace std;

typedef complex<double>	com;

unsigned GCD(unsigned u, unsigned v) {
	unsigned r;
	while (v != 0) {
		r = u % v;
		u = v;
		v = r;
	}
	return u;
}

lli mod = 4001, inf = 4e18;
lli sum = 0;
lli tmp3 = 0, tmp = 0, tmp2 = 0, comb = 1;
vector<lli> arr, check, depth,st;
lli n, m = 0, q;
lli maxi = 0, mini = inf;
string s2, s;
vector<vector<pii>> g;
lli high = 0;

void dfs(lli x,lli l,lli r)
{
	check[x] = 1;
	for (auto i : g[x]) if (!check[i.first] && (i.second>r || i.second<l)) dfs(i.first,l,r);
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(nullptr);
	lli k;
	cin >> n>>m;
	g.resize(n); check.resize(n, 0);
	for (int i = 0; i < m; i++)
	{
		cin >> tmp >> tmp2;
		g[tmp - 1].pb(pii(tmp2 - 1,i));
		g[tmp2 - 1].pb(pii(tmp - 1,i));
	}
	lli l, r;
	for (cin >> k; k--;sum=0,check.clear(),check.resize(n,0))
	{
		cin >> l >> r;
		--l; --r;
		for (int i = 0; i < n; i++)
		{
			if (!check[i])
			{
				dfs(i,l,r);
				sum += 1;
			}
		}
		cout << sum << endl;
	}
	return 0;
}


#include<iostream>
#include <fstream>
#include <iomanip>
#include<algorithm>
#include<string>
#include<random>
#include<vector>
#include<list>
#include<set>
#include<math.h>
#include<map>
#include<complex>

#define Ri0			for(int i=0;i<n;i++)
#define Ri1			for(int i=1;i<=n;i++)
#define lli          long long int
#define pb          push_back
#define pii         pair<lli,lli>
#define vi          vector< lli>
#define iii			tuple<lli,lli,lli>
#define ST			get<0>
#define ND			get<1>
#define RD			get<2>
#define TH			get<3>
#define pi			3.141592653589793238462
#define double      long double
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
#define ABC			26

using namespace std;

typedef complex<double>	com;

unsigned GCD(unsigned u, unsigned v) {
	unsigned r;
	while (v != 0) {
		r = u % v;
		u = v;
		v = r;
	}
	return u;
}

struct DSU
{
	vi deg;
	vi par;
	lli sum;
	DSU() {}
	DSU(lli n)
	{
		deg.resize(n, 0);
		par.resize(n, -1);
		sum = n;
	}
	lli find(lli x)
	{
		if (par[x] == -1) return x;
		return par[x] = find(par[x]);
	}
	void uni(lli x, lli y)
	{
		x = find(x); y = find(y);
		if ((x != -1 || y != -1) && x == y) return;
		sum--;
		if (deg[x] < deg[y]) par[x] = y;
		else {
			deg[x] += (deg[x] == deg[y]);
			par[y] = x;
		}
	}
};

lli mod = 4001, inf = 4e18;
lli sum = 0;
lli tmp3 = 0, tmp = 0, tmp2 = 0, comb = 1;
vector<lli> arr, check, depth,st;
lli n, m = 0, q;
lli maxi = 0, mini = inf;
string s2, s;
vector<vi> g;
lli high = 0;
vector<pii> edge;
vector<DSU> suf, pre;

int main()
{
	ios::sync_with_stdio(false), cin.tie(nullptr);
	lli k;
	cin >> n >> m;
	edge.resize(m), suf.resize(m), pre.resize(m);
	DSU dsu(n),dsur(n);
	for (int i = 0; i < m; pre[i]=dsu,i++)
	{
		cin >> edge[i].first >> edge[i].second;
		edge[i].second -= 1; edge[i].first -= 1;
		dsu.uni(edge[i].first, edge[i].second);
	}
	for (int i = m - 1; i > 0; dsur.uni(edge[i].first, edge[i].second), suf[i] = dsur, i--);
	cin >> k;
	for (lli l,r; k--;)
	{
		cin >> l >> r;
		--l; --r;
		DSU t(n);
		if (l > 0) t = pre[l-1];
		if (r < m - 1)
		{
			for (int i = 0; i < n; i++) if (suf[r + 1].par[i] != -1) t.uni(suf[r + 1].par[i], i);
		}
		cout << t.sum<<endl;
	}
	return 0;
}


#include<iostream>
#include <fstream>
#include <iomanip>
#include<algorithm>
#include<string>
#include<random>
#include<vector>
#include<list>
#include<set>
#include<math.h>
#include<map>
#include<complex>

#define Ri0			for(int i=0;i<n;i++)
#define Ri1			for(int i=1;i<=n;i++)
#define lli          long long int
#define pb          push_back
#define pii         pair<lli,lli>
#define vi          vector< lli>
#define iii			tuple<lli,lli,lli>
#define ST			get<0>
#define ND			get<1>
#define RD			get<2>
#define TH			get<3>
#define pi			3.141592653589793238462
#define double      long double
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
#define ABC			26

using namespace std;

typedef complex<double>	com;

unsigned GCD(unsigned u, unsigned v) {
	unsigned r;
	while (v != 0) {
		r = u % v;
		u = v;
		v = r;
	}
	return u;
}

lli mod = 4001, inf = 4e18;
lli sum = 0;
lli tmp3 = 0, tmp = 0, tmp2 = 0, comb = 1;
vector<lli> arr, check, depth,st;
lli n, m = 0, q;
lli maxi = 0, mini = inf;
string s2, s;
vector<vector<pii>> g;
lli high = 0;

void dfs(lli x,lli l,lli r,lli num)
{
	check[x] +=1;
	for (auto i : g[x])
		if (check[i.first]<=num && (i.second>r || i.second<l)) dfs(i.first,l,r,num);
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(nullptr);
	lli k;
	cin >> n>>m;
	g.resize(n); check.resize(n, 0);
	for (int i = 0; i < m; i++)
	{
		cin >> tmp >> tmp2;
		g[tmp - 1].pb(pii(tmp2 - 1,i));
		g[tmp2 - 1].pb(pii(tmp - 1,i));
	}
	lli l, r,num=0;
	for (cin >> k; k--;sum=0,num++)
	{
		cin >> l >> r;
		--l; --r;
		for (int i = 0; i < n; i++)
		{
			if (check[i]==num)
			{
				dfs(i,l,r,num);
				sum += 1;
			}
		}
		cout << sum << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll int
const long long int mod=1e9 + 7;
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
ll visited[503];
vector<pair<ll,ll>> v[503];
ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}
void dfs(ll s,ll st,ll en)
{
    ll i,val;

    visited[s]=1;
    for(i=0;i<v[s].size();i++)
    {
       val=v[s][i].second;
    //  cout<<s<<" "<<v[s][i]<<" "<<val<<" "<<st<<" "<<en<<endl;
    if((val<=en&&val>=st))
    continue;
        if(!visited[v[s][i].first])
        {

            dfs(v[s][i].first,st,en);
        }
    }

}

int main() {

  ll n,m,i,j,t,k,q,x,y;
sf("%lld%lld",&n,&m);
//memset(mat,0,sizeof(mat));
  for(i=1;i<=m;i++)
{
    sf("%lld%lld",&x,&y);
   v[x].pb(mp(y,i));
    v[y].pb(mp(x,i));

}
sf("%lld",&k);
while(k--)
  {
      sf("%lld%lld",&x,&y);
      memset(visited,0,sizeof(visited));
      ll ans=0;
      for(i=1;i<=n;i++)
      {
          if(!visited[i])
          {
              dfs(i,x,y);
              ans++;
            //  cout<<ans<<" "<<i<<endl;
          }
      }
  pf("%lld\n",ans);}

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const long long int mod=1e9 + 7;
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
ll visited[503];
vector<pair<ll,ll>> v[503];
ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}
void dfs(ll s,ll st,ll en)
{
    ll i,val;

    visited[s]=1;
    for(i=0;i<v[s].size();i++)
    {
       val=v[s][i].second;
    //  cout<<s<<" "<<v[s][i]<<" "<<val<<" "<<st<<" "<<en<<endl;
    if((val<=en&&val>=st))
    continue;
        if(!visited[v[s][i].first])
        {

            dfs(v[s][i].first,st,en);
        }
    }

}

int main() {

  ll n,m,i,j,t,k,q,x,y;
sf("%lld%lld",&n,&m);
//memset(mat,0,sizeof(mat));
  for(i=1;i<=m;i++)
{
    sf("%lld%lld",&x,&y);
   v[x].pb(mp(y,i));
    v[y].pb(mp(x,i));

}
sf("%lld",&k);
while(k--)
  {
      sf("%lld%lld",&x,&y);
      memset(visited,0,sizeof(visited));
      ll ans=0;
      for(i=1;i<=n;i++)
      {
          if(!visited[i])
          {
              dfs(i,x,y);
              ans++;
            //  cout<<ans<<" "<<i<<endl;
          }
      }
  pf("%lld\n",ans);}

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const long long int mod=1e9 + 7;
#define sf scanf
#define pf printf
#define pb push_back

ll visited[503];
vector <ll> v[503];
ll mat[505][505];
ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}
void dfs(ll s,ll st,ll en)
{
    ll i,val;
    if(visited[s])
    return;
    visited[s]=1;
    for(i=0;i<v[s].size();i++)
    {
       val=mat[s][v[s][i]];

        if(!visited[v[s][i]]&&(val>en||val<st))
        {
            dfs(v[s][i],st,en);
        }
    }
    return;
}

int main() {

  ll n,m,i,j,t,k,q,x,y;
sf("%lld%lld",&n,&m);
memset(mat,0,sizeof(mat));
  for(i=1;i<=m;i++)
{
    sf("%lld%lld",&x,&y);
    v[x].pb(y);
    v[y].pb(x);
    mat[x][y]=i;
    mat[y][x]=i;
}
sf("%lld",&k);
while(k--)
  {
      sf("%lld%lld",&x,&y);
      memset(visited,0,sizeof(visited));
      ll ans=0;
      for(i=1;i<=n;i++)
      {
          if(!visited[i])
          {
              dfs(i,x,y);
              ans++;
          }
      }
  pf("%lld\n",ans);}

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll int
const long long int mod=1e9 + 7;
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
ll visited[503];
vector<pair<ll,ll>> v[503];
ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}
void dfs(ll s,ll st,ll en)
{
    ll i,val;

    visited[s]=1;
    for(i=0;i<v[s].size();i++)
    {
       val=v[s][i].second;
    //  cout<<s<<" "<<v[s][i]<<" "<<val<<" "<<st<<" "<<en<<endl;
    if((val<=en&&val>=st))
    continue;
        if(!visited[v[s][i].first])
        {

            dfs(v[s][i].first,st,en);
        }
    }

}

int main() {

  ll n,m,i,j,t,k,q,x,y;
sf("%d%d",&n,&m);
//memset(mat,0,sizeof(mat));
  for(i=1;i<=m;i++)
{
    sf("%d%d",&x,&y);
   v[x].pb(mp(y,i));
    v[y].pb(mp(x,i));

}
sf("%d",&k);
while(k--)
  {
      sf("%d%d",&x,&y);
      memset(visited,0,sizeof(visited));
      ll ans=0;
      for(i=1;i<=n;i++)
      {
          if(!visited[i])
          {
              dfs(i,x,y);
              ans++;
            //  cout<<ans<<" "<<i<<endl;
          }
      }
  pf("%d\n",ans);}

  return 0;
}


#include <cstdio>

const int M = 100005, N = 505;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            f[x] = y;
            cnt ++;
        }
    }
}left[M], right[M];

int x[M], y[M], Q;

int main()
{
    //freopen("data.in", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    printf("Q: %d\n", Q);
    int count = 0;
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
        count ++;
        printf("%d\n", count);
    }
    return 0;
}


#include <cstdio>

const int M = 10005, N = 505;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            f[x] = y;
            cnt ++;
        }
    }
}left[M], right[M];

int x[M], y[M], Q;

int main()
{
    //freopen("data.in", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
    }
    return 0;
}


#include <cstdio>

const int M = 100005, N = 505;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            f[x] = y;
            cnt ++;
        }
    }
}left[M], right[M];

int x[M], y[M], Q;

int main()
{
    //freopen("data.in", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#define N 10005
int n,m,Q,l,r,x[N],y[N];
struct DSU{
    int f[505],c;
    int get(int x){return f[x]?f[x]=get(f[x]):x;}
    void Link(int x,int y){x=get(x),y=get(y); if (x!=y) f[x]=y,c++;}
}L[N],R[N],ans;
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++) scanf("%d%d",x+i,y+i),L[i]=L[i-1],L[i].Link(x[i],y[i]);
    for (int i=m;i;i--) R[i]=R[i+1],R[i].Link(x[i],y[i]);
    scanf("%d",&Q);
    while (Q--){
        scanf("%d%d",&l,&r); DSU ans=L[l-1];
        for (int i=1;i<=n;i++) if (R[r+1].f[i]) ans.Link(i,R[r+1].f[i]);//being combined to other later
        printf("%d\n",n-ans.c);
    }
    return 0;
}


#include <cstdio>

const int M = 10005, N = 505;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            f[x] = y;
            cnt ++;
        }
    }
}left[M], right[M];

int x[M], y[M], Q;

int main()
{
    //freopen("data.in", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    printf("Q: %d\n", Q);
    int count = 0;
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
        count ++;
        printf("%d\n", count);
    }
    return 0;
}


#include <cstdio>

const int M = 10005, N = 505;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
            f[x] = y;
        cnt ++;
    }
}left[M], right[M];

int x[M], y[M], Q;

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i >= 1; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
    }
    return 0;
}


#include <cstdio>

const int M = 100005, N = 667;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            f[x] = y;
            cnt ++;
        }
    }
}left[M], right[M];

int x[M], y[M], Q;

int main()
{
    //freopen("data.in", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    printf("Q: %d\n", Q);
    int count = 0;
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
        count ++;
        printf("%d\n", count);
    }
    return 0;
}


#include <cstdio>

const int M = 10005, N = 505;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
        {
            f[x] = y;
            cnt ++;
        }
    }
}left[M], right[M];

int x[M], y[M], Q;

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
    }
    return 0;
}


#include <cstdio>

const int M = 10005, N = 505;

struct State
{
    int f[N];
    int cnt;
    int find(int x)
    {
        return f[x] ? f[x] = find(f[x]) : x;
    }
    void link(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x != y)
            f[x] = y;
        cnt ++;
    }
}left[M], right[M];

int x[N], y[N], Q;

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i ++)
    {
        scanf("%d%d", &x[i], &y[i]);
        left[i] = left[i - 1];
        left[i].link(x[i], y[i]);
    }
    for (int i = m; i >= 1; i --)
    {
        right[i] = right[i + 1];
        right[i].link(x[i], y[i]);
    }
    scanf("%d", &Q);
    while(Q --)
    {
        int l, r;
        scanf("%d%d", &l, &r);
        State ans = left[l - 1];
        for (int i = 1; i <= n; i ++)
        {
            if (right[r + 1].f[i])
            {
                ans.link(i, right[r + 1].f[i]);
            }
        }
        printf("%d\n", n - ans.cnt);
    }
    return 0;
}


#include<cstdio>
#include<set>
#include<cstring>

const int maxn = 505;
const int maxm = 1e4 + 5;

int n, m, s[maxm], e[maxm];
int f[2][maxm][maxn];

int root(int *arr, int x) {
    if(arr[x] == x) return x;
    return arr[x] = root(arr, arr[x]);
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &s[i], &e[i]);
    }

    for(int i = 1; i <= n; i++) {
        f[0][0][i] = i;
    }
    for(int i = 1; i <= m; i++) {
        memcpy(f[0][i], f[0][i - 1], sizeof(int) * maxn);
        int r1 = root(f[0][i], s[i]);
        int r2 = root(f[0][i], e[i]);
        f[0][i][r2] = r1;
    }

    for(int i = 1; i <= n; i++) {
        f[1][m + 1][i] = i;
    }
    for(int i = m; i >= 1; i--) {
        memcpy(f[1][i], f[1][i + 1], sizeof(int) * maxn);
        int r1 = root(f[1][i], s[i]);
        int r2 = root(f[1][i], e[i]);
        f[1][i][r2] = r1;
    }

    int k, l, r, ans, f1[maxn], f2[maxn];
    std::set<int> s;
    scanf("%d", &k);
    while(k--) {
        s.clear();
        scanf("%d%d", &l, &r);
        for(int i = 1; i <= n; i++) {
            s.insert(f[0][l - 1][i]);
        }
        ans = s.size();
        memcpy(f1, f[0][l - 1], sizeof(int) * maxn);
        memcpy(f2, f[1][r + 1], sizeof(int) * maxn);
        for(int i = 1; i <= n; i++) {
            int r1 = root(f1, i);
            int r2 = root(f2, i);
            int r3 = root(f1, r2);
            if(r1 != r3) {
                ans--;
                f1[r3] = r1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>

const int maxn = 505;
const int maxm = 1e4 + 5;

int n, m, s[maxm], e[maxm];
int f[2][maxm][maxn];

int root(int *arr, int x) {
    if(arr[x] == x) return x;
    return arr[x] = root(arr, arr[x]);
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &s[i], &e[i]);
    }

    for(int i = 1; i <= n; i++) {
        f[0][0][i] = i;
    }
    for(int i = 1; i <= m; i++) {
        memcpy(f[0][i], f[0][i - 1], sizeof(int) * maxn);
        int r1 = root(f[0][i], s[i]);
        int r2 = root(f[0][i], e[i]);
        f[0][i][r2] = r1;
    }

    for(int i = 1; i <= n; i++) {
        f[1][m + 1][i] = i;
    }
    for(int i = m; i >= 1; i--) {
        memcpy(f[1][i], f[1][i + 1], sizeof(int) * maxn);
        int r1 = root(f[1][i], s[i]);
        int r2 = root(f[1][i], e[i]);
        f[1][i][r2] = r1;
    }

    int k, l, r, ans, f1[maxn], f2[maxn];
    scanf("%d", &k);
    while(k--) {
        scanf("%d%d", &l, &r);
        memcpy(f1, f[0][l - 1], sizeof(int) * maxn);
        memcpy(f2, f[1][r + 1], sizeof(int) * maxn);
        ans = 0;
        for(int i = 1; i <= n; i++) {
            int r1 = root(f1, i);
            int r2 = root(f1, f2[i]);
            f1[r2] = r1;
        }
        for(int i = 1; i <= n; i++) {
            if(f1[i] == i) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}


/*
PROB: cf 292D
LANG: c++
AUTHOR: maxkibble
*/

#include<cstdio>
#include<vector>
#include<cstring>

using namespace std;

// #define local

const int maxn = 505;
const int maxm = 1e4 + 10;

int n, m, st[maxm], ed[maxm];
vector<int> e[maxn];
bool vis[maxn];

inline void addEdge(int idx) {
    e[st[idx]].push_back(ed[idx]);
    e[ed[idx]].push_back(st[idx]);
}

void dfs(int x) {
    vis[x] = true;
    for(int y: e[x]) {
        if(vis[y]) continue;
        dfs(y);
    }
}

int main() {
#ifdef local
    freopen("1.in", "r", stdin);
#endif
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++)
        scanf("%d%d", &st[i], &ed[i]);
    int k, l, r, ans;
    scanf("%d", &k);
    while(k--) {
        memset(vis, 0, sizeof(vis));
        ans = 0;
        for(int i = 1; i <= n; i++)
            e[i].clear();
        scanf("%d%d", &l, &r);
        for(int i = 1; i < l; i++)
            addEdge(i);
        for(int i = r + 1; i <= m; i++)
            addEdge(i);
        for(int i = 1; i <= n; i++) {
            if(vis[i]) continue;
            ans++;
            dfs(i);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<cstring>

const int maxn = 505;
const int maxm = 1e4 + 5;

int n, m, s[maxm], e[maxm];
int f[2][maxm][maxn];

int root(int *arr, int x) {
    if(arr[x] == x) return x;
    return arr[x] = root(arr, arr[x]);
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &s[i], &e[i]);
    }

    for(int i = 1; i <= n; i++) {
        f[0][0][i] = i;
    }
    for(int i = 1; i <= m; i++) {
        memcpy(f[0][i], f[0][i - 1], sizeof(int) * maxn);
        int r1 = root(f[0][i], s[i]);
        int r2 = root(f[0][i], e[i]);
        f[0][i][r2] = r1;
    }

    for(int i = 1; i <= n; i++) {
        f[1][m + 1][i] = i;
    }
    for(int i = m; i >= 1; i--) {
        memcpy(f[1][i], f[1][i + 1], sizeof(int) * maxn);
        int r1 = root(f[1][i], s[i]);
        int r2 = root(f[1][i], e[i]);
        f[1][i][r2] = r1;
    }

    int k, l, r, ans, f1[maxn], f2[maxn];
    scanf("%d", &k);
    while(k--) {
        scanf("%d%d", &l, &r);
        memcpy(f1, f[0][l - 1], sizeof(int) * maxn);
        memcpy(f2, f[1][r + 1], sizeof(int) * maxn);
        ans = 0;
        for(int i = 1; i <= n; i++) {
            if(f1[i] == i) ans++;
        }
        for(int i = 1; i <= n; i++) {
            int r1 = root(f1, i);
            int r2 = root(f2, i);
            int r3 = root(f1, r2);
            if(r1 != r3) {
                ans--;
                f1[r3] = r1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<set>
#include<cstring>

const int maxn = 505;
const int maxm = 1e4 + 5;

int n, m, s[maxm], e[maxm];
int f[2][maxm][maxn];

int root(int *arr, int x) {
    if(arr[x] == x) return x;
    return arr[x] = root(arr, arr[x]);
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &s[i], &e[i]);
    }

    for(int i = 1; i <= n; i++) {
        f[0][0][i] = i;
    }
    for(int i = 1; i <= m; i++) {
        memcpy(f[0][i], f[0][i - 1], sizeof(int) * maxn);
        int r1 = root(f[0][i], s[i]);
        int r2 = root(f[0][i], e[i]);
        f[0][i][r2] = r1;
    }

    for(int i = 1; i <= n; i++) {
        f[1][m + 1][i] = i;
    }
    for(int i = m; i >= 1; i--) {
        memcpy(f[1][i], f[1][i + 1], sizeof(int) * maxn);
        int r1 = root(f[1][i], s[i]);
        int r2 = root(f[1][i], e[i]);
        f[1][i][r2] = r1;
    }

    int k, l, r, ans, f1[maxn], f2[maxn];
    std::set<int> s;
    scanf("%d", &k);
    while(k--) {
        s.clear();
        scanf("%d%d", &l, &r);
        memcpy(f1, f[0][l - 1], sizeof(int) * maxn);
        memcpy(f2, f[1][r + 1], sizeof(int) * maxn);
        for(int i = 1; i <= n; i++) {
            // printf("%d ", root(f1, i));
            s.insert(root(f1, i));
        }
        // printf("\n");
        ans = s.size();
        for(int i = 1; i <= n; i++) {
            int r1 = root(f1, i);
            int r2 = root(f2, i);
            int r3 = root(f1, r2);
            if(r1 != r3) {
                ans--;
                f1[r3] = r1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n, m, x, k, t, y, w, z, p[600], a[600], f[600], c[10050], b[10050];

inline int findset(int i)
{
    if (p[i] == i) return i;
    return p[i] = findset(p[i]);
}
inline void unionset(int i, int j)
{
    i = findset(i);
    j = findset(j);
    p[i] = j;
    a[j] += a[i];
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n >> m;
	for (int i = 0; i < m; ++i)
        cin >> b[i] >> c[i];
    cin >> k;
    while(k--)
    {
        z = 0;
        cin >> x >> y;
        for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = i, f[i] = 0;
        for (int i = 0; i < x-1; ++i)
            if (findset(b[i]) != findset(c[i]))
                unionset(b[i],c[i]);
        for (int i = y; i < m; ++i)
            if (findset(b[i]) != findset(c[i]))
                unionset(b[i],c[i]);
        for (int i = 1; i <= n; ++i)
            if (!f[findset(i)]) f[findset(i)] = 1, ++z;
        cout << z << endl;
    }
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <functional>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;

int n, m, x, k, t, y, w, z, p[600], a[600], f[600], c[10050], b[10050], pre[10020][502], suf[10020][502];

int findset(int i)
{
	if (p[i] == i) return i;
	return p[i] = findset(p[i]);
}
void unionset(int i, int j)
{
	i = findset(i);
	j = findset(j);
	if (i > j) swap(i, j);
	p[i] = j;
	a[j] += a[i];
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = pre[0][i] = suf[m+1][i] = i;
	for (int i = 1; i <= m; ++i) {
		cin >> b[i] >> c[i];
		if (findset(b[i]) != findset(c[i]))
			unionset(b[i], c[i]);
		for (int j = 1; j <= n; ++j) pre[i][j] = findset(j);
	}
	for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = i;
	for (int i = m ; i > 0; --i) {
		if (findset(b[i]) != findset(c[i]))
			unionset(b[i], c[i]);
		for (int j = 1; j <= n; ++j) suf[i][j] = findset(j);
	}
	cin >> k;
	while (k--)
	{
		z = 0;
		cin >> x >> y;
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; ++i)
			p[i] = pre[x-1][i];
		for (int i = 1; i <= n; ++i)
			if (findset(i) != suf[y+1][i])
				unionset(i, suf[y+1][i]);
		for (int i = 1; i <= n; ++i)
			if (!f[findset(i)]) f[findset(i)] = 1, z++;
		cout << z << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <functional>
#include <numeric>
#include <bits/stdc++.h>

using namespace std;

int n, m, x, k, t, y, w, z, p[600], a[600], f[600], c[10050], b[10050], pre[10020][502], suf[10020][502];

inline int findset(int i)
{
	if (p[i] == i) return i;
	return p[i] = findset(p[i]);
}
inline void unionset(int i, int j)
{
	i = findset(i);
	j = findset(j);
	if (i > j) swap(i, j);
	p[i] = j;
	a[j] += a[i];
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = pre[0][i] = suf[m+1][i] = i;
	for (int i = 1; i <= m; ++i) {
		cin >> b[i] >> c[i];
		if (findset(b[i]) != findset(c[i]))
			unionset(b[i], c[i]);
		for (int j = 1; j <= n; ++j) pre[i][j] = findset(j);
	}
	for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = i;
	for (int i = m ; i > 0; --i) {
		if (findset(b[i]) != findset(c[i]))
			unionset(b[i], c[i]);
		for (int j = 1; j <= n; ++j) suf[i][j] = findset(j);
	}
	cin >> k;
	while (k--)
	{
		z = 0;
		cin >> x >> y;
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; ++i)
			p[i] = pre[x-1][i];
		for (int i = 1; i <= n; ++i)
			if (findset(i) != suf[y+1][i])
				unionset(i, suf[y+1][i]);
		for (int i = 1; i <= n; ++i)
			if (!f[findset(i)]) f[findset(i)] = 1, z++;
		cout << z << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int n, m, x, k, t, y, w, z, p[600], a[600], f[600], c[10050], b[10050], pre[10020][502], suf[10020][502];

int findset(int i)
{
	if (p[i] == i) return i;
	return p[i] = findset(p[i]);
}
void unionset(int i, int j)
{
	i = findset(i);
	j = findset(j);
	if (i > j) swap(i, j);
	p[i] = j;
	a[j] += a[i];
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = pre[0][i] = suf[m+1][i] = i;
	for (int i = 1; i <= m; ++i) {
		cin >> b[i] >> c[i];
		if (findset(b[i]) != findset(c[i]))
			unionset(b[i], c[i]);
		for (int j = 1; j <= n; ++j) pre[i][j] = findset(j);
	}
	for (int i = 1; i <= n; ++i) a[i] = 1, p[i] = i;
	for (int i = m ; i > 0; --i) {
		if (findset(b[i]) != findset(c[i]))
			unionset(b[i], c[i]);
		for (int j = 1; j <= n; ++j) suf[i][j] = findset(j);
	}
	cin >> k;
	while (k--)
	{
		z = 0;
		cin >> x >> y;
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; ++i)
			p[i] = pre[x-1][i];
		for (int i = 1; i <= n; ++i)
			if (findset(i) != suf[y+1][i])
				unionset(i, suf[y+1][i]), p[i] = findset(i);
		for (int i = 1; i <= n; ++i)
			if (!f[findset(i)]) f[findset(i)] = 1, z++;
		cout << z << endl;
	}
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long int
#define mem(a,b) memset(a,b,sizeof(a))
#define MX 505
#define MXX 10005
#define mod 1000000007
#define inf 2000000000000
ll ex[]={1,-1,0,0};
ll wye[]={0,0,1,-1};
using namespace std;
struct st{
    ll x,y,z;
};
bool check(const st &ob1,const st &ob2)
{
    if(ob1.x!=ob2.x) return ob1.x<ob2.x;
    return ob1.y<ob2.y;
}
vector<ll>vt[MX];
ll n,m,k,a[MXX],b[MXX],mat[MX][MX],ans[2*MXX],visit[MX],cur=1;
vector<st>vec;
void dfs(ll num)
{
    visit[num]=cur;
    for(ll i=0;i<vt[num].size();i++)
    {
        ll tmp=vt[num][i];
        if(visit[tmp]!=cur && mat[num][tmp]) dfs(tmp);
    }
}
int main()
{
    scanf("%I64d%I64d",&n,&m);
    for(ll i=1;i<=m;i++)
    {
        scanf("%I64d%I64d",&a[i],&b[i]);
        vt[a[i]].push_back(b[i]);
        vt[b[i]].push_back(a[i]);
        mat[a[i]][b[i]]++;
        mat[b[i]][a[i]]++;
    }
    scanf("%I64d",&k);
    for(ll i=1;i<=k;i++)
    {
        st obj;
        scanf("%I64d%I64d",&obj.x,&obj.y);
        obj.z=i;
        vec.push_back(obj);
    }
    sort(vec.begin(),vec.end(),check);
    ll l,r;
    for(ll i=0;i<k;i++,cur++)
    {
        if(i==0)
        {
            l=vec[i].x;
            r=vec[i].y;
            for(ll j=l;j<=r;j++)
            {
                mat[a[j]][b[j]]--;
                mat[b[j]][a[j]]--;
            }
        }
        else
        {
            if(l<vec[i].x)
            {
                for(ll j=l;j<vec[i].x;j++)
                {
                    mat[a[j]][b[j]]++;
                    mat[b[j]][a[j]]++;
                }
                l=vec[i].x;
            }
            if(vec[i].y>r)
            {
                for(ll j=r+1;j<=vec[i].y;j++)
                {
                     mat[a[j]][b[j]]--;
                    mat[b[j]][a[j]]--;
                }
                r=vec[i].y;
            }
            else if(vec[i].y<r)
            {
                for(ll j=r;j>vec[i].y;j--)
                {
                    mat[a[j]][b[j]]++;
                    mat[b[j]][a[j]]++;
                }
                r=vec[i].y;
            }
        }
        ll c=0;
        for(ll j=1;j<=n;j++)
        {
            if(visit[j]==cur) continue;
            ++c;
            dfs(j);
        }

        ans[vec[i].z]=c;
    }
    for(ll i=1;i<=k;i++) printf("%I64d\n",ans[i]);
    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long int
#define ull unsigned long long int
#define mem(a,b) memset(a,b,sizeof(a))
#define MX 20005
//#define  s second
//#define f first
#define mod 1000000007
#define inf 1e9
#define eps 1e-15
//int ex[]={1,-1,0,0};
//int wye[]={0,0,1,-1};
using namespace std;
struct data{
    int l,r,pos;
}q[MX];
int a[2][505],siz[2][MX],n,m,an[MX],u[MX],v[MX],k;
bool cmp(const data &tmp,const data &tpp){
    if(tmp.l==tpp.l) return tmp.r>tpp.r;
    return tmp.l<tpp.l;
}
int find(int num,int node){
    while(a[num][node]!=node){
        a[num][node]=a[num][a[num][node]];
        node=a[num][node];
    }
    return node;
}
int dsu(int num,int tmp,int tpp){
    int r1=find(num,tmp),r2=find(num,tpp);
    if(r1==r2) return 0;
    if(siz[num][r1]<siz[num][r2]){
        siz[num][r2]+=siz[num][r1];
        a[num][r1]=a[num][r2];
    }
    else{
        siz[num][r1]+=siz[num][r2];
        a[num][r2]=a[num][r1];
    }
    return 1;
}
int main()
{
    fast();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d%d",&u[i],&v[i]);
    scanf("%d",&k);
    for(int i=1;i<=k;i++) {
        scanf("%d%d",&q[i].l,&q[i].r);
        q[i].pos=i;
    }
    sort(q+1,q+k+1,cmp);
    for(int i=1;i<=n;i++) a[0][i]=i,siz[0][i]=1,a[1][i]=i,siz[1][i]=1;
    for(int i=1,lef=1,ri=m+1,lol=n,j,pop=n;i<=k;i++){
        if(q[i].l!=lef){
            for(j=lef;j<q[i].l;j++) lol-=dsu(0,u[j],v[j]);
            lef=q[i].l;
            ri=m+1;
            pop=lol;
            for(int j=1;j<=n;j++) a[1][j]=a[0][j],siz[1][j]=siz[0][j];
        }
        for(j=ri-1;j>q[i].r;j--) pop-=dsu(1,u[j],v[j]);
        an[q[i].pos]=pop;
        ri=j+1;
    }
    for(int i=1;i<=k;i++) printf("%d\n",an[i]);
    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long int
#define mem(a,b) memset(a,b,sizeof(a))
#define MX 505
#define MXX 10005
#define mod 1000000007
#define inf 2000000000000
ll ex[]={1,-1,0,0};
ll wye[]={0,0,1,-1};
using namespace std;
struct st{
    ll x,y,z;
};
bool check(const st &ob1,const st &ob2)
{
    if(ob1.x!=ob2.x) return ob1.x<ob2.x;
    return ob1.y<ob2.y;
}
vector<ll>vt[MX];
ll n,m,k,a[MXX],b[MXX],mat[MX][MX],ans[2*MXX],visit[MX],cur=1;
vector<st>vec;
void bfs(ll num)
{
    queue<ll>q;
    q.push(num);
    visit[num]=cur;
    while(!q.empty())
    {
        num=q.front();
        q.pop();
        for(ll i=0;i<vt[num].size();i++)
        {
        ll tmp=vt[num][i];
        if(visit[tmp]!=cur && mat[num][tmp])
        {
         q.push(tmp);
            visit[tmp]=cur;
        }
        }
    }
}
int main()
{
    scanf("%I64d%I64d",&n,&m);
    for(ll i=1;i<=m;i++)
    {
        scanf("%I64d%I64d",&a[i],&b[i]);
        vt[a[i]].push_back(b[i]);
        vt[b[i]].push_back(a[i]);
        mat[a[i]][b[i]]++;
        mat[b[i]][a[i]]++;
    }
    scanf("%I64d",&k);
    for(ll i=1;i<=k;i++)
    {
        st obj;
        scanf("%I64d%I64d",&obj.x,&obj.y);
        obj.z=i;
        vec.push_back(obj);
    }
    sort(vec.begin(),vec.end(),check);
    ll l,r;
    for(ll i=0;i<k;i++,cur++)
    {
        if(i==0)
        {
            l=vec[i].x;
            r=vec[i].y;
            for(ll j=l;j<=r;j++)
            {
                mat[a[j]][b[j]]--;
                mat[b[j]][a[j]]--;
            }
        }
        else
        {
            if(l<vec[i].x)
            {
                for(ll j=l;j<vec[i].x;j++)
                {
                    mat[a[j]][b[j]]++;
                    mat[b[j]][a[j]]++;
                }
                l=vec[i].x;
            }
            if(vec[i].y>r)
            {
                for(ll j=r+1;j<=vec[i].y;j++)
                {
                     mat[a[j]][b[j]]--;
                    mat[b[j]][a[j]]--;
                }
                r=vec[i].y;
            }
            else if(vec[i].y<r)
            {
                for(ll j=r;j>vec[i].y;j--)
                {
                    mat[a[j]][b[j]]++;
                    mat[b[j]][a[j]]++;
                }
                r=vec[i].y;
            }
        }
        ll c=0;
        for(ll j=1;j<=n;j++)
        {
            if(visit[j]==cur) continue;
            ++c;
            bfs(j);
        }

        ans[vec[i].z]=c;
    }
    for(ll i=1;i<=k;i++) printf("%I64d\n",ans[i]);
    return 0;
}


#include<bits/stdc++.h>
#define PI acos(-1)
#define ll long long int
#define mem(a,b) memset(a,b,sizeof(a))
#define MX 505
#define MXX 10005
#define mod 1000000007
#define inf 2000000000000
ll ex[]={1,-1,0,0};
ll wye[]={0,0,1,-1};
using namespace std;
struct st{
    ll x,y,z;
};
bool check(const st &ob1,const st &ob2)
{
    if(ob1.x!=ob2.x) return ob1.x<ob2.x;
    return ob1.y<ob2.y;
}
vector<ll>vt[MX];
ll n,m,k,a[MXX],b[MXX],mat[MX][MX],ans[2*MXX],visit[MX];
vector<st>vec;
void dfs(ll num)
{
    visit[num]=1;
    for(ll i=0;i<vt[num].size();i++)
    {
        ll tmp=vt[num][i];
        if(visit[tmp]==0 && mat[num][tmp]) dfs(tmp);
    }
}
int main()
{
    scanf("%I64d%I64d",&n,&m);
    for(ll i=1;i<=m;i++)
    {
        scanf("%I64d%I64d",&a[i],&b[i]);
        vt[a[i]].push_back(b[i]);
        vt[b[i]].push_back(a[i]);
        mat[a[i]][b[i]]++;
        mat[b[i]][a[i]]++;
    }
    scanf("%I64d",&k);
    for(ll i=1;i<=k;i++)
    {
        st obj;
        scanf("%I64d%I64d",&obj.x,&obj.y);
        obj.z=i;
        vec.push_back(obj);
    }
    sort(vec.begin(),vec.end(),check);
    ll l,r;
    for(ll i=0;i<k;i++)
    {
        if(i==0)
        {
            l=vec[i].x;
            r=vec[i].y;
            for(ll j=l;j<=r;j++)
            {
                mat[a[j]][b[j]]--;
                mat[b[j]][a[j]]--;
            }
        }
        else
        {
            if(l<vec[i].x)
            {
                for(ll j=l;j<vec[i].x;j++)
                {
                    mat[a[j]][b[j]]++;
                    mat[b[j]][a[j]]++;
                }
                l=vec[i].x;
            }
            if(vec[i].y>r)
            {
                for(ll j=r+1;j<=vec[i].y;j++)
                {
                     mat[a[j]][b[j]]--;
                    mat[b[j]][a[j]]--;
                }
                r=vec[i].y;
            }
            else if(vec[i].y<r)
            {
                for(ll j=r;j>vec[i].y;j--)
                {
                    mat[a[j]][b[j]]++;
                    mat[b[j]][a[j]]++;
                }
                r=vec[i].y;
            }
        }
        mem(visit,0);
        ll c=0;
        for(ll j=1;j<=n;j++)
        {
            if(visit[j]) continue;
            ++c;
            dfs(j);
        }

        ans[vec[i].z]=c;
    }
    for(ll i=1;i<=k;i++) printf("%I64d\n",ans[i]);
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long  ll;

const int N = 504;
vector<pair<int , int> > adj[N];
int p[N] , h[N] , cnt;
int n , m , l , r;

int f(int u){
    return p[u] = (p[u] == u ? u : f(p[u]));
}

void uni(int u , int v){
    u = f(u) , v = f(v);

    if(u == v)
        return;

    cnt--;
    if(h[u] < h[v])swap(u , v);

    p[v] = u;
    h[u] += (h[v] == h[u]);
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin); // remember to delete
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> n >> m;
    for(int i = 0 ; i < m ; ++i){
        int u , v;
        cin >> u >> v;
        adj[u].pb({v , i});
        adj[v].pb({u , i});
    }

    int q;
    cin >> q;

    while(q--){
        iota(p , p + n + 1 , 0);
        memset(h , h + n + 1 , 0);
        cin >> l >> r;
        l-- , r--;

        cnt = n;
        for(int i = 1 ; i <= n ; ++i){
            for(auto to : adj[i]){
                if(to.second < l || to.second > r){
                    uni(i , to.first);
                }
            }
        }

        cout << cnt << "\n";
    }
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long  ll;

const int N = 504 , M = 1e4 + 4;
vector<pair<int , int> > eList;
int n , m , l , r;

struct dsu{
    int p[N] , h[N] , cnt;

    dsu(){
        iota(p , p + N , 0);
    }
    int f(int u){
        return p[u] = (p[u] == u ? u : f(p[u]));
    }

    void uni(int u , int v){
        u = f(u) , v = f(v);
        if(u == v)
            return;

        if(h[u] < h[v])swap(u , v);
        cnt--;
        p[v] = u;
        h[u] += (h[v] == h[u]);
    }
} L[M] , R[M];

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin);
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> n >> m;

    for(int i = 0 ; i < m + 2 ; ++i)
        L[i].cnt = R[i].cnt = n;

    eList.pb({-1 , -1});
    for(int i = 0 ; i < m ; ++i){
        int u , v;
        cin >> u >> v;
        eList.emplace_back(u , v);
    }

    for(int i = 1 ; i <= m ; ++i){
        L[i] = L[i - 1];
        L[i].uni(eList[i].first , eList[i].second);
    }

    for(int i = m ; i >= 1 ; --i){
        R[i] = R[i + 1];
        R[i].uni(eList[i].first , eList[i].second);
    }

    int q;
    cin >> q;

    while(q--){
        cin >> l >> r;
        dsu rs = L[l - 1];
        for(int i = 1 ; i <= n ; ++i)
            if(R[r + 1].p[i])rs.uni(i , R[r + 1].p[i]);

        cout << rs.cnt << "\n";
    }
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long  ll;

const int N = 504;
vector<pair<int , int> > adj[N];
int p[N] , h[N] , cnt;
int n , m , l , r;

int f(int u){
    return p[u] = (p[u] == u ? u : f(p[u]));
}

void uni(int u , int v){
    u = f(u) , v = f(v);

    if(u == v)
        return;

    cnt--;
    if(h[u] < h[v])swap(u , v);

    p[v] = u;
    h[u] += (h[v] == h[u]);
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin); // remember to delete
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> n >> m;
    for(int i = 0 ; i < m ; ++i){
        int u , v;
        cin >> u >> v;
        adj[u].pb({v , i});
        adj[v].pb({u , i});
    }

    int q;
    cin >> q;

    while(q--){
        iota(p , p + n + 1 , 0);
        cin >> l >> r;
        l-- , r--;

        cnt = n;
        for(int i = 1 ; i <= n ; ++i){
            for(auto to : adj[i]){
                if(to.second < l || to.second > r){
                    uni(i , to.first);
                }
            }
        }

        cout << cnt << "\n";
    }
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long  ll;

const int N = 504;

vector<pair<int , int> > adj[N];
int n , m , l , r;
bool vis[N];

void dfs(int u){
    vis[u] = true;
    for(auto to : adj[u]){
        if(to.second < l || to.second > r){
            if(!vis[to.first])dfs(to.first);
        }
    }
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin); // remember to delete
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> n >> m;
    for(int i = 0 ; i < m ; ++i){
        int u , v;
        cin >> u >> v;
        adj[u].pb({v , i});
        adj[v].pb({u , i});
    }

    int q;
    cin >> q;

    while(q--){
        memset(vis , false , sizeof vis);
        cin >> l >> r;
        l-- , r--;

        int cnt = 0;
        for(int i = 1 ; i <= n ; ++i){
            if(!vis[i]){
                ++cnt;
                dfs(i);
            }
        }

        cout << cnt << "\n";
    }
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long  ll;

const int N = 504;
vector<pair<int , int> > adj[N];
int p[N] , h[N] , cnt;
int n , m , l , r;

int f(int u){
    return p[u] = (p[u] == u ? u : f(p[u]));
}

void uni(int u , int v){
    u = f(u) , v = f(v);

    if(u == v)
        return;

    cnt--;
    if(h[u] < h[v])swap(u , v);

    p[v] = u;
    h[u] += (h[v] == h[u]);
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt" , "r" , stdin); // remember to delete
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    cin >> n >> m;
    for(int i = 0 ; i < m ; ++i){
        int u , v;
        cin >> u >> v;
        adj[u].pb({v , i});
        adj[v].pb({u , i});
    }

    int q;
    cin >> q;

    while(q--){
        iota(p , p + n + 1 , 0);
        memset(h , 0 , (n + 1) * sizeof h[0]);
        cin >> l >> r;
        l-- , r--;

        cnt = n;
        for(int i = 1 ; i <= n ; ++i){
            for(auto to : adj[i]){
                if(to.second < l || to.second > r){
                    uni(i , to.first);
                }
            }
        }

        cout << cnt << "\n";
    }
}


#include <algorithm>
#include <functional>
#include <numeric>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <bitset>
#include <sstream>

using namespace std;

#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define for1(i, n) for(int i = 1; i <= int(n); ++i)
#define ford(i, n) for(int i = int(n) - 1; i >= 0; --i)
#define fore(i, l, r) for(int i = int(l); i < int(r); ++i)
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define X first
#define Y second

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<typename T> T abs(T a) { return a < 0 ? -a : a; }
template<typename T> T sqr(T a) { return a*a; }

const int INF = (int)1e9;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;

const int N = 510;
const int M = 10010;

int n, m;
pt e[M];

int clf[M][N];
int crg[M][N];
int cur[M];

int szg[N];
int g[N][N];

int cused;
int used[N];

pt p[N];

int root(int* c, int v){
    if(c[v] == v)
        return v;
    else
        return c[v] = root(c, c[v]);
}

void merge(int* c, int a, int b){
    a = root(c, a);
    b = root(c, b);
    if(a == b)
        return;

    if(rand() & 1)
        swap(a, b);

    c[a] = b;
}

int main(){
    #ifdef ssu1
        freopen("input.txt", "rt", stdin);
        //freopen("output.txt", "wt", stdout);
    #endif

    cin >> n >> m;

    forn(i, m){
        int x, y;
        scanf("%d %d", &x, &y);
        x--; y--;

        e[i].X = x;
        e[i].Y = y;
    }

    forn(i, n)
        clf[0][i] = crg[0][i] = i;

    forn(i, m){
        memcpy(clf[i + 1], clf[i], sizeof(int) * n);
        merge(clf[i + 1], e[i].X, e[i].Y);
    }

    int cnt = 0;
    ford(i, m){
        memcpy(crg[cnt + 1], crg[cnt], sizeof(int) * n);
        merge(crg[cnt + 1], e[i].X, e[i].Y);
        cnt++;
    }

    int q;
    cin >> q;

    forn(cq, q){
        int lf, rg;
        scanf("%d %d", &lf, &rg);
        lf--;
        rg = m - rg;

        memset(szg, 0, sizeof(int) * n);

        forn(i, n){
            int c = root(clf[lf], i);
            g[c][szg[c]++] = i;
        }

        memcpy(cur, crg[rg], sizeof(int) * n);

        forn(i, n){
            forn(j, szg[i] - 1)
                merge(cur, g[i][j], g[i][j + 1]);
        }

        int ans = 0;
        cused++;

        forn(i, n){
            int c = root(cur, i);
            if(used[c] < cused){
                ans++;
                used[c] = cused;
            }
        }

        printf("%d\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DIMN 507
#define DIMM 10007

int pref[DIMM][DIMN], suff[DIMM][DIMN];
int tmp[DIMN];

int n,m,q;
pair<int,int> edge[DIMM];

int findset(int* par, int x) {
    if(par[x] == x) return x;
    else return par[x] = findset(par, par[x]);
}

void unionset(int* par, int x, int y) {
    int xx = findset(par, x);
    int yy = findset(par, y);
    if(rand() % 2 == 1)
        par[xx] = yy;
    else
        par[yy] = xx;
}

void printset(string s, int *c) {
    cout<<s;
    for(int i = 1; i <= n; i++) cout<<' '<<findset(c, i);
    cout<<endl;
}

int main() {
    //freopen("input.txt", "r", stdin);

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= m; i++) scanf("%d %d", &edge[i].first, &edge[i].second);

    for(int i = 1; i <= n; i++) {
        pref[0][i] = i;
        suff[0][i] = i;
    }

    for(int i = 1; i <= m; i++) {
        memcpy(pref[i], pref[i-1], sizeof(int)*(n+1));
        unionset(pref[i], edge[i].first, edge[i].second);
        //printset((string)"PREF "+(char)('0'+i)+" : ", pref[i]);
    }

    for(int i = 1; i <= m; i++) {
        memcpy(suff[i], suff[i-1], sizeof(int)*(n+1));
        unionset(suff[i], edge[m+1-i].first, edge[m+1-i].second);
        //printset((string)"SUFF "+(char)('0'+i)+" : ", suff[i]);
    }

    scanf("%d", &q);

    int L,R,c;
    for(int i = 1; i <= q; i++) {
        scanf("%d %d", &L, &R);
        L--;
        R = m-R;

        memcpy(tmp, pref[L], sizeof(int)*(n+1));
        //printset("PREF : ", tmp);
        //printset("SUFF : ", suff[R]);
        for(int j = 1; j <= n; j++) {
            c = findset(suff[R], j);
            unionset(tmp, j, c);
        }
        //printset("RES  : ", tmp);

        int res = 0;
        for(int j = 1; j <= n; j++)
            if(findset(tmp, j) == j) res++;

        printf("%d\n", res);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DIMN 507
#define DIMM 10007

int pref[DIMM][DIMN], suff[DIMM][DIMN];
int tmp[DIMN];
int comppref[DIMM];

int n,m,q;
pair<int,int> edge[DIMM];

int findset(int* par, int x) {
    if(par[x] == x) return x;
    else return par[x] = findset(par, par[x]);
}

bool unionset(int* par, int x, int y) {
    int xx = findset(par, x);
    int yy = findset(par, y);
    if(xx == yy) return false;
    if(rand() % 2 == 1)
        par[xx] = yy;
    else
        par[yy] = xx;
    return true;
}

int main() {
    //freopen("input.txt", "r", stdin);

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= m; i++) scanf("%d %d", &edge[i].first, &edge[i].second);

    for(int i = 1; i <= n; i++) {
        pref[0][i] = i;
        suff[0][i] = i;
    }
    comppref[0] = n;
    //compsuff[0] = n;

    for(int i = 1; i <= m; i++) {
        memcpy(pref[i], pref[i-1], sizeof(int)*(n+1));
        comppref[i] = comppref[i-1];
        if(unionset(pref[i], edge[i].first, edge[i].second)) comppref[i]--;
    }

    for(int i = 1; i <= m; i++) {
        memcpy(suff[i], suff[i-1], sizeof(int)*(n+1));
        unionset(suff[i], edge[m+1-i].first, edge[m+1-i].second);
    }

    scanf("%d", &q);

    int L,R,c,comptmp;
    for(int i = 1; i <= q; i++) {
        scanf("%d %d", &L, &R);
        L--;
        R = m-R;

        memcpy(tmp, pref[L], sizeof(int)*(n+1));
        comptmp = comppref[L];

        for(int j = 1; j <= n; j++) {
            c = findset(suff[R], j);
            if(unionset(tmp, j, c)) comptmp--;
        }

        printf("%d\n", comptmp);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DIMN 507
#define DIMM 10007

int pref[DIMM][DIMN], suff[DIMM][DIMN];
int tmp[DIMN];
int comppref[DIMN], compsuff[DIMN];

int n,m,q;
pair<int,int> edge[DIMM];

int findset(int* par, int x) {
    if(par[x] == x) return x;
    else return par[x] = findset(par, par[x]);
}

bool unionset(int* par, int x, int y) {
    int xx = findset(par, x);
    int yy = findset(par, y);
    if(xx == yy) return false;
    if(rand() % 2 == 1)
        par[xx] = yy;
    else
        par[yy] = xx;
    return true;
}

void printset(string s, int *c) {
    cout<<s;
    for(int i = 1; i <= n; i++) cout<<' '<<findset(c, i);
    cout<<endl;
}

int main() {
    //freopen("input.txt", "r", stdin);

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= m; i++) scanf("%d %d", &edge[i].first, &edge[i].second);

    for(int i = 1; i <= n; i++) {
        pref[0][i] = i;
        suff[0][i] = i;
    }
    comppref[0] = n;
    compsuff[0] = n;

    for(int i = 1; i <= m; i++) {
        memcpy(pref[i], pref[i-1], sizeof(int)*(n+1));
        comppref[i] = comppref[i-1];
        if(unionset(pref[i], edge[i].first, edge[i].second)) comppref[i]--;
        //printset((string)"PREF "+(char)('0'+i)+" : ", pref[i]);
    }

    for(int i = 1; i <= m; i++) {
        memcpy(suff[i], suff[i-1], sizeof(int)*(n+1));
        compsuff[i] = compsuff[i-1];
        if(unionset(suff[i], edge[m+1-i].first, edge[m+1-i].second)) compsuff[i]--;
        //printset((string)"SUFF "+(char)('0'+i)+" : ", suff[i]);
    }

    scanf("%d", &q);

    int L,R,c,comptmp;
    for(int i = 1; i <= q; i++) {
        scanf("%d %d", &L, &R);
        L--;
        R = m-R;

        memcpy(tmp, pref[L], sizeof(int)*(n+1));
        comptmp = comppref[L];
        //printset("PREF : ", tmp);
        //printset("SUFF : ", suff[R]);
        for(int j = 1; j <= n; j++) {
            c = findset(suff[R], j);
            if(unionset(tmp, j, c)) comptmp--;
        }
        //printset("RES  : ", tmp);

        /*
        int res = 0;
        for(int j = 1; j <= n; j++)
            if(findset(tmp, j) == j) res++;
        printf("%d\n", res);
        */
        printf("%d\n", comptmp);
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DIMN 507
#define DIMM 10007

int pref[DIMM][DIMN], suff[DIMM][DIMN];
int tmp[DIMN];
int comppref[DIMM], compsuff[DIMM];

int n,m,q;
pair<int,int> edge[DIMM];

int findset(int* par, int x) {
    if(par[x] == x) return x;
    else return par[x] = findset(par, par[x]);
}

bool unionset(int* par, int x, int y) {
    int xx = findset(par, x);
    int yy = findset(par, y);
    if(xx == yy) return false;
    if(rand() % 2 == 1)
        par[xx] = yy;
    else
        par[yy] = xx;
    return true;
}

void printset(string s, int *c) {
    cout<<s;
    for(int i = 1; i <= n; i++) cout<<' '<<findset(c, i);
    cout<<endl;
}

int main() {
    //freopen("input.txt", "r", stdin);

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= m; i++) scanf("%d %d", &edge[i].first, &edge[i].second);

    for(int i = 1; i <= n; i++) {
        pref[0][i] = i;
        suff[0][i] = i;
    }
    comppref[0] = n;
    compsuff[0] = n;

    for(int i = 1; i <= m; i++) {
        memcpy(pref[i], pref[i-1], sizeof(int)*(n+1));
        comppref[i] = comppref[i-1];
        if(unionset(pref[i], edge[i].first, edge[i].second)) comppref[i]--;
        //printset((string)"PREF "+(char)('0'+i)+" : ", pref[i]);
    }

    for(int i = 1; i <= m; i++) {
        memcpy(suff[i], suff[i-1], sizeof(int)*(n+1));
        compsuff[i] = compsuff[i-1];
        if(unionset(suff[i], edge[m+1-i].first, edge[m+1-i].second)) compsuff[i]--;
        //printset((string)"SUFF "+(char)('0'+i)+" : ", suff[i]);
    }

    scanf("%d", &q);

    int L,R,c,comptmp;
    for(int i = 1; i <= q; i++) {
        scanf("%d %d", &L, &R);
        L--;
        R = m-R;

        memcpy(tmp, pref[L], sizeof(int)*(n+1));
        comptmp = comppref[L];
        //printset("PREF : ", tmp);
        //printset("SUFF : ", suff[R]);
        for(int j = 1; j <= n; j++) {
            c = findset(suff[R], j);
            if(unionset(tmp, j, c)) comptmp--;
        }
        //printset("RES  : ", tmp);

        /*
        int res = 0;
        for(int j = 1; j <= n; j++)
            if(findset(tmp, j) == j) res++;
        printf("%d\n", res);
        */
        printf("%d\n", comptmp);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
bool check=1;
void dfs(int v){
	mark[v]=check;
	for(int i=0;i<g[v].size();i++){
		pii u=g[v][i];
		if(mark[u.fi]!=check)if(u.se<l || u.se>r)dfs(u.fi);
	}
}
int ans[10000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	int kik,jok;
	for(int i=0;i<k;i++){
		cin>>kik>>jok;
		h[i]={{kik,jok},i};
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
	//	memset(mark,0,sizeof mark);
	//	:-(
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(mark[i]!=check){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
		check=!check;
	}
	for(int i=0;i<k;i++)
		printf("%d \n",ans[i]);
}


#include<bits/stdc++.h>
using namespace std;

#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=2e17;

int n,m,l,r;
vector<pair<int,int> > v[500];
bool mark[500];
void dfs(int x){
	mark[x]=1;
	for(int i=0;i<v[x].size();++i){
		if(v[x][i].second>=l&&v[x][i].second<=r)continue;
		if(mark[v[x][i].first])continue;
        dfs(v[x][i].first);
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		--a;--b;
		v[a].pb(mp(b,i));
		v[b].pb(mp(a,i));
	}
	int q;scanf("%d",&q);
	while(q--){
		int ans=0;
		scanf("%d%d",&l,&r);
		--l;--r;
		memset(mark,0,sizeof mark);
		for(int i=0;i<n;i++){
			if(!mark[i]){
				++ans;
				dfs(i);
			}
        	}
		printf("%d\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;

#define pii pair<ll,ll>
#define se second
#define fi first
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
//#define endl '\n'
#define fast ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
const ll INF=15e8;

vector<pii> g[500+50];
bool mark[500+50];
pair<pii,int> h[20000+100];
int l,r;
void dfs(int v){
	mark[v]=1;
	for(pii i:g[v])
		if(!mark[i.fi]){ if((i.se<l || i.se>r))dfs(i.fi);}
}
int ans[20000+100];
int main(){
	fast;
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int t,k;
		cin>>k>>t;
		g[t].pb({k,i+1});
		g[k].pb({t,i+1});
	}
	int k;
	cin>>k;
	for(int i=0;i<k;i++){
		scanf("%d%d",&h[i].fi.fi,&h[i].fi.se);
		h[i].se=i;
	}
	sort(h,h+k);
	int l1=-1,l2=-1,l3=-1;
	for(int i=0;i<k;i++){
		if(h[i].fi.fi==l1 && h[i].fi.se==l2){
			ans[h[i].se]=l3;
			continue;
		}
		memset(mark,0,sizeof mark);
		l=h[i].fi.fi;
		r=h[i].fi.se;
		l1=l,l2=r,l3=0;
		for(int i=1;i<=n;i++){
			if(!mark[i]){
				l3++;
				dfs(i);
			}
		}
		ans[h[i].se]=l3;
	}
	for(int i=0;i<k;i++)
		printf("%d\n",ans[i]);
}