#include <iostream>
#include <cstdio>

using namespace std;

int p[10004];
int pre[10004];
int suf[10004];
int x[10004];
int y[10004];

int get(int pos)
{
        if(p[pos] == pos) return pos;
        else return get(p[pos]);
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);

    for(int i = 0; i < m; i++) {
        scanf("%d%d", &x[i], &y[i]);
    }

    for(int i = 0; i < n; i++) p[i] = i;

    int v1, v2;
    v1 = v2 = 0;

    for(int i = 0; i < m; i++) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            pre[v1++] = i;
        }
    }

    for(int i = 0; i < n; i++) p[i] = i;

    for(int i = m-1; i >= 0; i--) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            suf[v2++] = i;
        }
    }

    int k;

    cin >> k;

    for(int i = 0; i < k; i++) {
        int l, r;

        scanf("%d%d", &l, &r);
        l--, r--;

        for(int i = 0; i < n; i++) p[i] = i;

        for(int i = 0; i < v1; i++) {
            if(pre[i] < l) {
                int p1, p2;
                p1 = get(x[pre[i]]);
                p2 = get(y[pre[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
            else break;
        }

        for(int i = 0; i < v2; i++) {
            if(suf[i] > r) {
                int p1, p2;
                p1 = get(x[suf[i]]);
                p2 = get(y[suf[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
            else break;
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            if(p[i] == i) ans++;
        }

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


#include <iostream>
#include <cstdio>

using namespace std;

int p[10004];
int pre[10004];
int suf[10004];
int x[10004];
int y[10004];

int get(int pos)
{
        if(p[pos] == pos) return pos;
        else return get(p[pos]);
}

int main()
{
    int n, m;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        cin >> x[i] >> y[i];
    }

    for(int i = 0; i < n; i++) p[i] = i;

    int v1, v2;
    v1 = v2 = 0;

    for(int i = 0; i < m; i++) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            pre[v1++] = i;
        }
    }

    for(int i = 0; i < n; i++) p[i] = i;

    for(int i = m-1; i >= 0; i--) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            suf[v2++] = i;
        }
    }

    int k;

    cin >> k;

    for(int i = 0; i < k; i++) {
        int l, r;

        cin >> l >> r;
        l--, r--;

        for(int i = 0; i < n; i++) p[i] = i;

        for(int i = 0; i < v1; i++) {
            if(pre[i] < l) {
                int p1, p2;
                p1 = get(x[pre[i]]);
                p2 = get(y[pre[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
        }

        for(int i = 0; i < v2; i++) {
            if(suf[i] > r) {
                int p1, p2;
                p1 = get(x[suf[i]]);
                p2 = get(y[suf[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            if(p[i] == i) ans++;
        }

        cout << ans << endl;
    }
}


#include <iostream>
#include <vector>
#include <cstring>

bool vis[10004];

using namespace std;

vector<int> v[10005], vv[10005];

void dfs(int pos, int l, int r)
{
        vis[pos] = 1;
        for(int i = 0; i < v[pos].size(); i++) {
            if(vis[v[pos][i]]) continue;
            if(vv[pos][i] >= l && vv[pos][i] <= r) continue;
            dfs(v[pos][i], l, r);
        }
}

int main()
{
    ios_base::sync_with_stdio(false);
    int n, m, x, y;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        v[x-1].push_back(y-1);
        v[y-1].push_back(x-1);
        vv[x-1].push_back(i+1);
        vv[y-1].push_back(i+1);
    }

    int k;

    cin >> k;

    for(int i = 0; i < k; i++) {
        cin >> x >> y;
        int ans = 0;
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                dfs(i, x, y);
                ans++;
            }
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>

bool vis[10004];

using namespace std;

vector<int> v[10005], vv[10005];

void dfs(int pos, int l, int r)
{
        vis[pos] = 1;
        for(int i = 0; i < v[pos].size(); i++) {
            if(vis[v[pos][i]]) continue;
            if(vv[pos][i] >= l && vv[pos][i] <= r) continue;
            dfs(v[pos][i], l, r);
        }
}

int main()
{
    //ios_base::sync_with_stdio(false);
    int n, m, x, y;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        v[x-1].push_back(y-1);
        v[y-1].push_back(x-1);
        vv[x-1].push_back(i+1);
        vv[y-1].push_back(i+1);
    }

    int k;

    cin >> k;
    map<pair<int, int>, int> mm;
    for(int i = 0; i < k; i++) {
        scanf("%d%d", &x, &y);
        int ans = 0;
        if(mm[pair<int, int>(x, y)]) {
            printf("%d\n", mm[pair<int, int>(x, y)]);
            continue;
        }
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                dfs(i, x, y);
                ans++;
            }
        }
        printf("%d\n", ans);
        mm[pair<int, int>(x, y)] = ans;
    }
}


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

bool vis[10004];

using namespace std;

vector<int> v[10005], vv[10005];

void dfs(int pos, int l, int r)
{
        vis[pos] = 1;
        for(int i = 0; i < v[pos].size(); i++) {
            if(vis[v[pos][i]]) continue;
            if(vv[pos][i] >= l && vv[pos][i] <= r) continue;
            dfs(v[pos][i], l, r);
        }
}

int main()
{
    //ios_base::sync_with_stdio(false);
    int n, m, x, y;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        v[x-1].push_back(y-1);
        v[y-1].push_back(x-1);
        vv[x-1].push_back(i+1);
        vv[y-1].push_back(i+1);
    }

    int k;

    cin >> k;

    for(int i = 0; i < k; i++) {
        scanf("%d%d", &x, &y);
        int ans = 0;
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                dfs(i, x, y);
                ans++;
            }
        }
        printf("%d\n", ans);
    }
}


#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>

bool vis[504];

using namespace std;

vector<int> v[505], vv[505];

void dfs(int pos, int l, int r)
{
        vis[pos] = 1;
        for(int i = 0; i < v[pos].size(); i++) {
            if(vis[v[pos][i]]) continue;
            if(vv[pos][i] >= l && vv[pos][i] <= r) continue;
            dfs(v[pos][i], l, r);
        }
}

int main()
{
    //ios_base::sync_with_stdio(false);
    int n, m, x, y;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        v[x-1].push_back(y-1);
        v[y-1].push_back(x-1);
        vv[x-1].push_back(i+1);
        vv[y-1].push_back(i+1);
    }

    int k;

    cin >> k;
    map<pair<int, int>, int> mm;
    for(int i = 0; i < k; i++) {
        scanf("%d%d", &x, &y);
        int ans = 0;
        if(mm[pair<int, int>(x, y)]) {
            printf("%d\n", mm[pair<int, int>(x, y)]);
            continue;
        }
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                dfs(i, x, y);
                ans++;
            }
        }
        printf("%d\n", ans);
        mm[pair<int, int>(x, y)] = ans;
    }
}


#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>

bool vis[504];

using namespace std;

vector<int> v[505], vv[505];

void dfs(int pos, int l, int r)
{
        vis[pos] = 1;
        for(int i = 0; i < v[pos].size(); i++) {
            if(vis[v[pos][i]]) continue;
            if(vv[pos][i] >= l && vv[pos][i] <= r) continue;
            dfs(v[pos][i], l, r);
        }
}

int main()
{
    int n, m, x, y;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        scanf("%d%d", &n, &m);
        v[x-1].push_back(y-1);
        v[y-1].push_back(x-1);
        vv[x-1].push_back(i+1);
        vv[y-1].push_back(i+1);
    }

    int k;

    cin >> k;
    map<pair<int, int>, int> mm;
    for(int i = 0; i < k; i++) {
        scanf("%d%d", &x, &y);
        int ans = 0;
        if(mm[pair<int, int>(x, y)]) {
            printf("%d\n", mm[pair<int, int>(x, y)]);
            continue;
        }
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                dfs(i, x, y);
                ans++;
            }
        }
        printf("%d\n", ans);
        mm[pair<int, int>(x, y)] = ans;
    }
}


#include <iostream>
#include <vector>
#include <cstring>

bool vis[10004];

using namespace std;

vector<int> v[10005], vv[10005];

void dfs(int pos, int l, int r)
{
        vis[pos] = 1;
        for(int i = 0; i < v[pos].size(); i++) {
            if(vis[v[pos][i]]) continue;
            if(vv[pos][i] >= l && vv[pos][i] <= r) continue;
            dfs(v[pos][i], l, r);
        }
}

int main()
{
    int n, m, x, y;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        v[x-1].push_back(y-1);
        v[y-1].push_back(x-1);
        vv[x-1].push_back(i+1);
        vv[y-1].push_back(i+1);
    }

    int k;

    cin >> k;

    for(int i = 0; i < k; i++) {
        cin >> x >> y;
        int ans = 0;
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                dfs(i, x, y);
                ans++;
            }
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <cstdio>

using namespace std;

int p[10004];
int pre[10004];
int suf[10004];
int x[10004];
int y[10004];

int get(int pos)
{
        if(p[pos] == pos) return pos;
        else return get(p[pos]);
}

int main()
{
    int n, m;
    //ios_base::sync_with_stdio(false);
    scanf("%d%d", &n, &m);

    for(int i = 0; i < m; i++) {
        scanf("%d%d", &x[i], &y[i]);
    }

    for(int i = 0; i < n; i++) p[i] = i;

    int v1, v2;
    v1 = v2 = 0;

    for(int i = 0; i < m; i++) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            pre[v1++] = i;
        }
    }

    for(int i = 0; i < n; i++) p[i] = i;

    for(int i = m-1; i >= 0; i--) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            suf[v2++] = i;
        }
    }

    int k;

    cin >> k;

    for(int i = 0; i < k; i++) {
        int l, r;

        scanf("%d%d", &l, &r);
        l--, r--;

        for(int i = 0; i < n; i++) p[i] = i;

        for(int i = 0; i < v1; i++) {
            if(pre[i] < l) {
                int p1, p2;
                p1 = get(x[pre[i]]);
                p2 = get(y[pre[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
        }

        for(int i = 0; i < v2; i++) {
            if(suf[i] > r) {
                int p1, p2;
                p1 = get(x[suf[i]]);
                p2 = get(y[suf[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            if(p[i] == i) ans++;
        }

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


#include <iostream>
#include <cstdio>

using namespace std;

int p[10004];
int pre[10004];
int suf[10004];
int x[10004];
int y[10004];

int get(int pos)
{
        if(p[pos] == pos) return pos;
        else return get(p[pos]);
}

int main()
{
    int n, m;
    ios_base::sync_with_stdio(false);
    scanf("%d%d", &n, &m);

    for(int i = 0; i < m; i++) {
        scanf("%d%d", &x[i], &y[i]);
    }

    for(int i = 0; i < n; i++) p[i] = i;

    int v1, v2;
    v1 = v2 = 0;

    for(int i = 0; i < m; i++) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            pre[v1++] = i;
        }
    }

    for(int i = 0; i < n; i++) p[i] = i;

    for(int i = m-1; i >= 0; i--) {
        int p1, p2;
        p1 = get(x[i]);
        p2 = get(y[i]);
        if(p1 != p2) {
            p[p1] = p2;
            suf[v2++] = i;
        }
    }

    int k;

    cin >> k;

    for(int i = 0; i < k; i++) {
        int l, r;

        scanf("%d%d", &l, &r);
        l--, r--;

        for(int i = 0; i < n; i++) p[i] = i;

        for(int i = 0; i < v1; i++) {
            if(pre[i] < l) {
                int p1, p2;
                p1 = get(x[pre[i]]);
                p2 = get(y[pre[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
        }

        for(int i = 0; i < v2; i++) {
            if(suf[i] > r) {
                int p1, p2;
                p1 = get(x[suf[i]]);
                p2 = get(y[suf[i]]);
                if(p1 != p2) {
                    p[p1] = p2;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < n; i++) {
            if(p[i] == i) ans++;
        }

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


#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>

bool vis[10004];

using namespace std;

vector<int> v[10005], vv[10005];

void dfs(int pos, int l, int r)
{
        vis[pos] = 1;
        for(int i = 0; i < v[pos].size(); i++) {
            if(vis[v[pos][i]]) continue;
            if(vv[pos][i] >= l && vv[pos][i] <= r) continue;
            dfs(v[pos][i], l, r);
        }
}

int main()
{
    //ios_base::sync_with_stdio(false);
    int n, m, x, y;

    cin >> n >> m;

    for(int i = 0; i < m; i++) {
        cin >> x >> y;
        v[x-1].push_back(y-1);
        v[y-1].push_back(x-1);
        vv[x-1].push_back(i+1);
        vv[y-1].push_back(i+1);
    }

    int k;

    cin >> k;
    map<pair<int, int>, int> mm;
    for(int i = 0; i < k; i++) {
        scanf("%d%d", &x, &y);
        int ans = 0;
        if(mm[pair<int, int>(x, y)]) {
            cout << mm[pair<int, int>(x, y)] << endl;
            continue;
        }
        memset(vis, 0, sizeof(vis));
        for(int i = 0; i < n; i++) {
            if(!vis[i]) {
                dfs(i, x, y);
                ans++;
            }
        }
        printf("%d\n", ans);
        mm[pair<int, int>(x, y)] = ans;
    }
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

const int T=100005;

int kk,n,m,i,j,k,l,r;
int fix[T],a[T],b[T];
vector <int> g[T],V,G[T];
vector < vector <int> > B[T],A[T];

void DFS(int v)
{
    fix[v]=1;
    V.pb(v);

    for (int i=0;i<g[v].S;i++)
    if (!fix[g[v][i]])
    DFS(g[v][i]);
}

void doit(int v)
{
    fix[v]=1;
    for (int i=0;i<G[v].S;i++)
    if (!fix[G[v][i]])
    doit(G[v][i]);
}

int go(int l,int r)
{
     int i,j;
     int ans=0;

     for (j=0;j<=3*n+1;j++)
     G[j].clear();

     for (j=0;j<A[l-1].S;j++)
     for (k=0;k<A[l-1][j].S;k++)
     {
         G[n+1+j].pb(A[l-1][j][k]);
         G[A[l-1][j][k]].pb(n+1+j);
     }

     for (j=0;j<B[r+1].S;j++)
     for (k=0;k<B[r+1][j].S;k++)
     {
         G[2*n+1+j].pb(B[r+1][j][k]);
         G[B[r+1][j][k]].pb(2*n+1+j);
     }

     for (j=1;j<=3*n;j++)
     fix[j]=0;

     for (j=1;j<=n;j++)
     if (!fix[j]) doit(j),ans++;

     return ans;
}

main()
{
 //   freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    cin>>n>>m;
    for (i=1;i<=m;i++)
        cin>>a[i]>>b[i];

    for (i=1;i<=m;i++)
    {
         g[a[i]].pb(b[i]);
         g[b[i]].pb(a[i]);

         for (j=1;j<=n;j++)
            fix[j]=0;

         for (j=1;j<=n;j++)
         if (!fix[j])
         {
             V.clear();
             DFS(j);
             A[i].pb(V);
         }
    }

    for (j=1;j<=n;j++)
        g[j].clear();

    for (i=m;i>=1;i--)
    {
         g[a[i]].pb(b[i]);
         g[b[i]].pb(a[i]);

         for (j=1;j<=n;j++)
            fix[j]=0;

         for (j=1;j<=n;j++)
         if (!fix[j])
         {
             V.clear();
             DFS(j);
             B[i].pb(V);
         }
    }

    cin>>kk;
    while (kk--)
    cin>>l>>r,cout<<go(l,r)<<endl;
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

int P[10000];
int pos,x,y,k,l,r,i,j,n,m;
int T[10000],A[100000],B[100000],a[100000],b[100000];

int get(int x)
{
    if (x==P[x]) return x;
    return (P[x]=get(P[x]));
}

int go(int l,int r)
{
    int i;
    int h=0;
    int t;

    for (i=1;i<=n;i++){
//    h+=(A[i]>=l && B[i]<=r);

     if (A[i]<l || B[i]>r) h++;

  //  if ((A[i]>=l && B[i]<=r))
//    cout<<i<<" "<<A[i]<<" "<<B[i]<<" ";
    }

 //   cout<<endl;

    return n-h;
}

main()
{
   // freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    srand(time(NULL));

    scanf("%d %d",&n,&m);
    for (i=1;i<=m;i++)
    scanf("%d %d",&a[i],&b[i]);

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=1;i<=n;i++)
       T[i]=i;

   for (i=1;i<=n;i++)
   {
       pos=rand()%n + 1;
       swap(T[i],T[pos]);
   }

    for (i=1;i<=n;i++)
    {
        A[i]=INF;
        B[i]=-INF;
    }

    for (i=1;i<=m;i++)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (T[x]>T[y]) swap(x,y);
        if (x==y) continue; else
        {
            P[x]=y;
            A[x]=i;
        }
    }

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=m;i>=1;i--)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (T[x]>T[y]) swap(x,y);
        if (x==y) continue; else
        {
  //          cout<<"KI"<<endl;
  //          cout<<a[i]<<" "<<b[i]<<" "<<x<<" "<<y<<endl;
            P[x]=y;
            B[x]=i;
        }
    }

   // for (i=1;i<=n;i++)
   // cout<<A[i]<<" "<<B[i]<<endl;

    cin>>k;

    for (i=1;i<=k;i++)
    {
        scanf("%d %d",&l,&r);
        cout<<go(l,r)<<endl;
    }
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

const int T=100005;

int raod,kk,n,m,i,j,k,l,r;
int size[T],P[T],fix[T],a[T],b[T];
vector <int> g[T],V,G[T];
vector < vector <int> > B[T],A[T];

void DFS(int v)
{
    fix[v]=raod;
    V.pb(v);

    for (int i=0;i<g[v].S;i++)
    if (fix[g[v][i]]!=raod)
    DFS(g[v][i]);
}

void doit(int v)
{
    fix[v]=raod;
    for (int i=0;i<G[v].S;i++)
    if (fix[G[v][i]]!=raod)
    doit(G[v][i]);
}

int get(int x)
{
    if (P[x]==x)
    return x; else return (P[x]=get(P[x]));
}

void unite(int a,int b)
{
    a=get(a);
    b=get(b);
    if (size[a]>size[b]) swap(a,b);
    P[a]=b;
}

int go(int l,int r)
{
     int i,j;
     int ans=0;

     for (j=1;j<=n;j++)
     P[j]=j,size[j]=1;

     for (j=0;j<A[l-1].S;j++)
     for (k=1;k<A[l-1][j].S;k++)
         unite(A[l-1][j][k],A[l-1][j][0]);

     for (j=0;j<B[r+1].S;j++)
     for (k=1;k<B[r+1][j].S;k++)
         unite(B[r+1][j][0],B[r+1][j][k]);

     for (j=1;j<=n;j++)
     if (P[j]==j) ans++;

     return ans;
}

main()
{
  //  freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++)
        scanf("%d%d",&a[i],&b[i]);

    for (i=1;i<=m;i++)
    {
         g[a[i]].pb(b[i]);
         g[b[i]].pb(a[i]);

         raod++;

         for (j=1;j<=n;j++)
         if (fix[j]!=raod)
         {
             V.clear();
             DFS(j);
             A[i].pb(V);
         }
    }

    for (j=1;j<=n;j++)
        g[j].clear();

    for (i=m;i>=1;i--)
    {
         raod++;

         g[a[i]].pb(b[i]);
         g[b[i]].pb(a[i]);

         for (j=1;j<=n;j++)
         if (fix[j]!=raod)
         {
             V.clear();
             DFS(j);
             B[i].pb(V);
         }
    }

    scanf("%d",&kk);
    while (kk--)
    scanf("%d%d",&l,&r),printf("%d\n",go(l,r));
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

int P[10000];
int x,y,k,l,r,i,j,n,m;
int A[100000],B[100000],a[100000],b[100000];

int get(int x)
{
    if (x==P[x]) return x;
    return (P[x]=get(P[x]));
}

int go(int l,int r)
{
    int i;
    int h=0;
    int t;

    for (i=1;i<=n;i++){
//    h+=(A[i]>=l && B[i]<=r);

     if (A[i]<l || B[i]>r) h++;

  //  if ((A[i]>=l && B[i]<=r))
//    cout<<i<<" "<<A[i]<<" "<<B[i]<<" ";
    }

 //   cout<<endl;

    return n-h;
}

main()
{
  //  freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    srand(time(NULL));

    scanf("%d %d",&n,&m);
    for (i=1;i<=m;i++)
    scanf("%d %d",&a[i],&b[i]);

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=1;i<=n;i++)
    {
        A[i]=INF;
        B[i]=-INF;
    }

    for (i=1;i<=m;i++)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (rand()&1)  swap(x,y);
        if (x==y) continue; else
        {
            P[x]=y;
            A[x]=i;
        }
    }

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=m;i>=1;i--)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (rand()&1)  swap(x,y);
        if (x==y) continue; else
        {
  //          cout<<"KI"<<endl;
  //          cout<<a[i]<<" "<<b[i]<<" "<<x<<" "<<y<<endl;
            P[x]=y;
            B[x]=i;
        }
    }

   // for (i=1;i<=n;i++)
   // cout<<A[i]<<" "<<B[i]<<endl;

    cin>>k;

    for (i=1;i<=k;i++)
    {
        scanf("%d %d",&l,&r);
        cout<<go(l,r)<<endl;
    }
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

int P[10000];
int x,y,k,l,r,i,j,n,m;
int A[100000],B[100000],a[100000],b[100000];

int get(int x)
{
    if (x==P[x]) return x;
    return (P[x]=get(P[x]));
}

int go(int l,int r)
{
    int i;
    int h=0;
    int t;

    for (i=1;i<=n;i++){
    h+=(A[i]>=l && B[i]<=r);
  //  if ((A[i]>=l && B[i]<=r))
//    cout<<i<<" "<<A[i]<<" "<<B[i]<<" ";
    }

 //   cout<<endl;

    return h;
}

main()
{
   // freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    srand(time(NULL));

    scanf("%d %d",&n,&m);
    for (i=1;i<=m;i++)
    scanf("%d %d",&a[i],&b[i]);

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=1;i<=n;i++)
    {
        A[i]=INF;
        B[i]=-INF;
    }

    for (i=1;i<=m;i++)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (rand()&1)  swap(x,y);
        if (x==y) continue; else
        {
            P[x]=y;
            A[x]=i;
        }
    }

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=m;i>=1;i--)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (rand()&1)  swap(x,y);
        if (x==y) continue; else
        {
  //          cout<<"KI"<<endl;
  //          cout<<a[i]<<" "<<b[i]<<" "<<x<<" "<<y<<endl;
            P[x]=y;
            B[x]=i;
        }
    }

 //   for (i=1;i<=n;i++)
 //   cout<<A[i]<<" "<<B[i]<<endl;

    cin>>k;

    for (i=1;i<=k;i++)
    {
        scanf("%d %d",&l,&r);
        cout<<go(l,r)<<endl;
    }
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

const int T=100005;

int x,y,raod,kk,n,m,i,j,k,l,r;
int size[T],P[T],fix[T],a[T],b[T];
vector <int> g[T],V,G[T];
int B[10005][505],A[10005][505];

int get(int x)
{
    if (P[x]==x)
    return x; else return (P[x]=get(P[x]));
}

void unite(int a,int b)
{
  //  cout<<a<<" "<<b<<endl;
    a=get(a);
    b=get(b);
    if (size[a]>size[b]) swap(a,b);
    P[a]=b;
}

int gett(int num,int x)
{
    if (A[num][x]==x) return x;
    return (A[num][x]=gett(num,A[num][x]));
}

int gett2(int num,int x)
{
    if (B[num][x]==x) return x;
    return (B[num][x]=gett2(num,B[num][x]));
}

int go(int l,int r)
{
  //   cout<<"--------------"<<endl;
     int i,j;
     int ans=0;

     for (j=1;j<=n;j++)
     P[j]=j,size[j]=1;

     for (j=1;j<=n;j++)
     unite(j,A[l-1][j]);

     for (j=1;j<=n;j++)
     unite(j,B[r+1][j]);

     for (j=1;j<=n;j++)
     if (P[j]==j) ans++;

     return ans;
}

main()
{
  //  freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++)
        scanf("%d%d",&a[i],&b[i]);

    for (j=1;j<=n;j++){
        A[0][j]=j;
        B[m+1][j]=j;
    }

    for (i=1;i<=m;i++)
    {
        for (j=1;j<=n;j++)
        A[i][j]=A[i-1][j];

        x=gett(i,a[i]);
        y=gett(i,b[i]);

     //   cout<<x<<" "<<y<<endl;

        if (x==y) continue;

        if (rand()&1) swap(x,y);

        A[i][x]=y;
    }

    for (i=m;i>=1;i--)
    {
        for (j=1;j<=n;j++)
        B[i][j]=B[i+1][j];

        x=gett2(i,a[i]);
        y=gett2(i,b[i]);

        if (x==y) continue;

        if (rand()&1) swap(x,y);

        B[i][x]=y;
    }

    scanf("%d",&kk);
    while (kk--)
    scanf("%d%d",&l,&r),printf("%d\n",go(l,r));
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

int P[10000];
int x,y,k,l,r,i,j,n,m;
int A[100000],B[100000],a[100000],b[100000];

int get(int x)
{
    if (x==P[x]) return x;
    return (P[x]=get(P[x]));
}

int go(int l,int r)
{
    int i;
    int h=0;
    int t;

    for (i=1;i<=n;i++){
//    h+=(A[i]>=l && B[i]<=r);

     if (A[i]<l || B[i]>r) h++;

  //  if ((A[i]>=l && B[i]<=r))
//    cout<<i<<" "<<A[i]<<" "<<B[i]<<" ";
    }

 //   cout<<endl;

    return n-h;
}

main()
{
  //  freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    srand(time(NULL));

    scanf("%d %d",&n,&m);
    for (i=1;i<=m;i++)
    scanf("%d %d",&a[i],&b[i]);

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=1;i<=n;i++)
    {
        A[i]=INF;
        B[i]=-INF;
    }

    for (i=1;i<=m;i++)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (rand()&1)  swap(x,y);
        if (x==y) continue; else
        {
            P[x]=y;
            A[x]=i;
        }
    }

    for (i=1;i<=n;i++)
        P[i]=i;

    for (i=m;i>=1;i--)
    {
        x=get(a[i]);
        y=get(b[i]);
        if (rand()&1)  swap(x,y);
        if (x==y) continue; else
        {
  //          cout<<"KI"<<endl;
  //          cout<<a[i]<<" "<<b[i]<<" "<<x<<" "<<y<<endl;
            P[x]=y;
            B[x]=i;
        }
    }

   // for (i=1;i<=n;i++)
   // cout<<A[i]<<" "<<B[i]<<endl;

    cin>>k;

    for (i=1;i<=k;i++)
    {
        scanf("%d %d",&l,&r);
        cout<<go(l,r)<<endl;
    }
}


/*===============*\
|  ID: TMANDZU    |
|    LANG: C++    |
\*===============*/
//Tornike Mandzulashvili
//#pragma comment(linker,"/STACK:256000000")
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <math.h>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <set>

#define EPS 0.000000001
#define Pi 3.1415926535897932384626433832795028841971
#define hash1 1000003
#define hash2 1000033
#define md 1000000007
#define INF 1000000500
#define mp make_pair
#define pb push_back
#define S size()
#define MX(aa,bb) (aa>bb?aa:bb)
#define MN(aa,bb) (aa<bb?aa:bb)
#define fi first
#define se second
#define PI pair < int,int >
#define REP(i,a,n) for(i=a;i<n;i++)
#define sc scanf
#define pt printf
#define big long long
#define VI vector <int>
#define DID (long long)

using namespace std;

const int T=10005;

int raod,kk,n,m,i,j,k,l,r;
int fix[T],a[T],b[T];
vector <int> g[T],V,G[T];
vector < vector <int> > B[T],A[T];

void DFS(int v)
{
    fix[v]=raod;
    V.pb(v);

    for (int i=0;i<g[v].S;i++)
    if (fix[g[v][i]]!=raod)
    DFS(g[v][i]);
}

void doit(int v)
{
    fix[v]=raod;
    for (int i=0;i<G[v].S;i++)
    if (fix[G[v][i]]!=raod)
    doit(G[v][i]);
}

int go(int l,int r)
{
     int i,j;
     int ans=0;

     for (j=0;j<=3*n+1;j++)
     G[j].clear();

     for (j=0;j<A[l-1].S;j++)
     for (k=0;k<A[l-1][j].S;k++)
     {
         G[n+1+j].pb(A[l-1][j][k]);
         G[A[l-1][j][k]].pb(n+1+j);
     }

     for (j=0;j<B[r+1].S;j++)
     for (k=0;k<B[r+1][j].S;k++)
     {
         G[2*n+1+j].pb(B[r+1][j][k]);
         G[B[r+1][j][k]].pb(2*n+1+j);
     }

     raod++;

     for (j=1;j<=n;j++)
     if (fix[j]!=raod) doit(j),ans++;

     return ans;
}

main()
{
   // freopen("text.in","r",stdin);   freopen("text.out","w",stdout);

    scanf("%d%d",&n,&m);
    for (i=1;i<=m;i++)
        scanf("%d%d",&a[i],&b[i]);

    for (i=1;i<=m;i++)
    {
         g[a[i]].pb(b[i]);
         g[b[i]].pb(a[i]);

         raod++;

         for (j=1;j<=n;j++)
         if (fix[j]!=raod)
         {
             V.clear();
             DFS(j);
             A[i].pb(V);
         }
    }

    for (j=1;j<=n;j++)
        g[j].clear();

    for (i=m;i>=1;i--)
    {
         raod++;

         g[a[i]].pb(b[i]);
         g[b[i]].pb(a[i]);

         for (j=1;j<=n;j++)
         if (fix[j]!=raod)
         {
             V.clear();
             DFS(j);
             B[i].pb(V);
         }
    }

    scanf("%d",&kk);
    while (kk--)
    scanf("%d%d",&l,&r),printf("%d\n",go(l,r));
}


#include <iostream>
#include <map>
//#include <cstdio>
#include <fstream>
#include <vector>
//#include <algorithm>
//#include <string>
#define fin cin
#define fout cout
using namespace std;
vector<int> connect[505];
int x[10005],y[10005];
map< int,int > mem;
map< int,vector < int > > able;
int used[10005];
int l,r;
void dfs(int index)
{
	for(int i=0;i<connect[index].size();i++)
	{
		if(mem[index*1000+connect[index][i]])
		    continue;
	    if(used[connect[index][i]])
	        continue;
	    used[connect[index][i]]=1;
	    dfs(connect[index][i]);
	}
}
int main()
{
	ifstream fin("input.txt");
	ofstream fout("output.txt");
	int n,m;
	int k;
    int cnt;
    int i,j;
    int r1;
    bool judge;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
	    cin>>x[i]>>y[i];
	    connect[x[i]].push_back(y[i]);
	    connect[y[i]].push_back(x[i]);
	    able[x[i]*1000+y[i]].push_back(i);
	    able[y[i]*1000+x[i]].push_back(i);
	}
	cin>>k;
	for(i=1;i<=k;i++)
	{
		cnt=0;
	    cin>>l>>r;
	    for(j=l;j<=r;j++)
	    {
	    	judge=false;
	    	mem[x[j]*1000+y[j]]=1;
	    	mem[y[j]*1000+x[j]]=1;
	    	if(able[x[j]*1000+y[j]][0]<l || able[x[j]*1000+y[j]][able[x[j]*1000+y[j]].size()-1]>r)
	    	    	judge=true;
	    	if(judge)
	    	{
	        	mem[x[j]*1000+y[j]]=0;
	        	mem[y[j]*1000+x[j]]=0;
	    	}
//	    	else
//	    	    cout<<x[j]<<" "<<y[j]<<endl;
	    }
	    for(j=1;j<=n;j++)
	    {
	        if(used[j])
	            continue;
	        dfs(j);
	        cnt++;
	    }
	    cout<<cnt<<endl;
	    for(j=1;j<=n;j++)
	        used[j]=0;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]*1000+y[j]]=0;
	    	mem[y[j]*1000+x[j]]=0;
	    }
	}
	return 0;
}


#include <iostream>
#include <map>
#include <cstdio>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
//#define fin cin
//#define fout cout
using namespace std;
vector<int> connect[505];
int x[10005],y[10005];
map<int,int> mem[10005];
int used[10005];
int l,r;
void dfs(int index)
{
//	cout<<index<<" ";
	for(int i=0;i<connect[index].size();i++)
	{
		if(mem[index][connect[index][i]])
		    continue;
	    if(used[connect[index][i]])
	        continue;
	    used[connect[index][i]]=1;
	    dfs(connect[index][i]);
	}
}
int main()
{
//	ifstream fin("input.txt");
//	ofstream fout("output.txt");
	int n,m;
	int k;
    int cnt;
    int i,j;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
	    cin>>x[i]>>y[i];
	    connect[x[i]].push_back(y[i]);
	    connect[y[i]].push_back(x[i]);
	}
	cin>>k;
	for(i=1;i<=k;i++)
	{
//		cout<<"i="<<i<<endl;
		cnt=0;
	    cin>>l>>r;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]][y[j]]=1;
	    	mem[y[j]][x[j]]=1;
//	    	cout<<x[j]<<" "<<y[j]<<endl;
	    }
	    for(j=1;j<=n;j++)
	    {
	        if(used[j])
	            continue;
	        dfs(j);
//	        cout<<endl;
	        cnt++;
	    }
	    cout<<cnt<<endl;
	    for(j=1;j<=n;j++)
	        used[j]=0;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]][y[j]]=0;
	    	mem[y[j]][x[j]]=0;
	    }
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int Maxn=505;
int n,m;
vector< pair< int,int > > neigh[Maxn];                        //��¼����
int k,l,r;
bool taken[Maxn];                                             //�൱��used
void Traverse(int v)                                          //Traverse:�߹������й�������Ĳ���
{
	taken[v]=true;
	for(int i=0;i<neigh[v].size();i++)
	{
		pair< int,int > u=neigh[v][i];
		if((u.second<l || u.second>r) && !taken[u.first])     //�Ƿ��Ѿ��߹��ק������
		    Traverse(u.first);
	}
}
int main()
{
//	cin>>n>>m;
	scanf("%d%d",&n,&m);
	int a,b,res;
	for(int i=1;i<=m;i++)
	{
//		cin>>a>>b;
		scanf("%d%d",&a,&b);
		neigh[a].push_back(pair< int,int > (b,i));
		neigh[b].push_back(pair< int,int > (a,i));
	}
//	cin>>k;
	scanf("%d",&k);
	for(int i=1;i<=k;i++)
	{
//		cin>>l>>r;
		scanf("%d%d",&l,&r);
		fill(taken+1,taken+n+1,false);                        //��?���൱��memset(take+1,take+n+1,0)
		res=0;
		for(int j=1;j<=n;j++)                                 //�ӵ�һ���㿪ʼ
		    if(!taken[j])
		    {
		    	res++;
		    	Traverse(j);
		    }
//		cout<<res<<endl;
		printf("%d\n",res);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int Maxn=505;
int n,m;
vector< pair< int,int > > neigh[Maxn];                        //��¼����
int k;
int l,r;
bool taken[Maxn];                                             //�൱��used
int res;
void Traverse(int v)                                          //Traverse:�߹������й�������Ĳ���
{
	taken[v]=true;
	for(int i=0;i<neigh[v].size();i++)
	{
		pair< int,int > u=neigh[v][i];
		if((u.second<l || u.second>r) && !taken[u.first])     //�Ƿ��Ѿ��߹��ק������
		    Traverse(u.first);
	}
}
int main()
{
	cin>>n>>m;
	int a,b;
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b;
		neigh[a].push_back(pair< int,int > (b,i));
		neigh[b].push_back(pair< int,int > (a,i));
	}
	cin>>k;
	for(int i=1;i<=k;i++)
	{
		cin>>l>>r;
		fill(taken+1,taken+n+1,false);                        //?
		res=0;
		for(int j=1;j<=n;j++)                                 //�ӵ�һ���㿪ʼ
		    if(!taken[j])
		    {
		    	res++;
		    	Traverse(j);
		    }
		cout<<res<<endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int Maxn=505;
int n,m;
vector< pair< int,int > > neigh[Maxn];                        //��¼����
int k;
int l,r;
bool taken[Maxn];                                             //�൱��used
int res;
void Traverse(int v)                                          //Traverse:�߹������й��������
{
	taken[v]=true;
	for(int i=0;i<neigh[v].size();i++)
	{
		pair< int,int > u=neigh[v][i];
		if((u.second<l || u.second>r) && !taken[u.first])     //�Ƿ��Ѿ��߹��ק������
		    Traverse(u.first);
	}
}
int main()
{
	cin>>n>>m;
	int a,b;
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b;
		neigh[a].push_back(pair< int,int > (b,i));
		neigh[b].push_back(pair< int,int > (a,i));
	}
	cin>>k;
	for(int i=1;i<=k;i++)
	{
		cin>>l>>r;
		fill(taken+1,taken+n+1,false);                        //?
		res=0;
		for(int j=1;j<=n;j++)                                 //�ӵ�һ���㿪ʼ
		    if(!taken[j])
		    {
		    	res++;
		    	Traverse(i);
		    }
		cout<<res<<endl;
	}
	return 0;
}


#include <iostream>
#include <map>
//#include <cstdio>
//#include <fstream>
#include <vector>
//#include <algorithm>
//#include <string>
//#define fin cin
//#define fout cout
using namespace std;
vector<int> connect[505];
int x[10005],y[10005];
map<int,int> mem;
vector<int> able[10005];
int used[10005];
int l,r;
void dfs(int index)
{
	for(int i=0;i<connect[index].size();i++)
	{
		if(mem[index*1000+connect[index][i]])
		    continue;
	    if(used[connect[index][i]])
	        continue;
	    used[connect[index][i]]=1;
	    dfs(connect[index][i]);
	}
}
int main()
{
//	ifstream fin("input.txt");
//	ofstream fout("output.txt");
	int n,m;
	int k;
    int cnt;
    int i,j;
    int r1;
    bool judge;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
	    cin>>x[i]>>y[i];
	    connect[x[i]].push_back(y[i]);
	    connect[y[i]].push_back(x[i]);
	    able[x[i]*1000+y[i]].push_back(i);
	    able[y[i]*1000+x[i]].push_back(i);
	}
	cin>>k;
	for(i=1;i<=k;i++)
	{
		cnt=0;
	    cin>>l>>r;
	    for(j=l;j<=r;j++)
	    {
	    	judge=false;
	    	mem[x[j]*1000+y[j]]=1;
	    	mem[y[j]*1000+x[j]]=1;
	    	if(able[x[j]*1000+y[j]][0]<l || able[x[j]*1000+y[j]][able[x[j]*1000+y[j]].size()-1]>r)
	    	    	judge=true;
	    	if(judge)
	    	{
	        	mem[x[j]*1000+y[j]]=0;
	        	mem[y[j]*1000+x[j]]=0;
	    	}
//	    	else
//	    	    cout<<x[j]<<" "<<y[j]<<endl;
	    }
	    for(j=1;j<=n;j++)
	    {
	        if(used[j])
	            continue;
	        dfs(j);
	        cnt++;
	    }
	    cout<<cnt<<endl;
	    for(j=1;j<=n;j++)
	        used[j]=0;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]*1000+y[j]]=0;
	    	mem[y[j]*1000+x[j]]=0;
	    }
	}
	return 0;
}


#include <iostream>
#include <map>
//#include <cstdio>
//#include <fstream>
#include <vector>
//#include <algorithm>
//#include <string>
//#define fin cin
//#define fout cout
using namespace std;
vector<int> connect[505];
int x[10005],y[10005];
map< int,int > mem;
map< int,vector < int > > able;
int used[10005];
int l,r;
void dfs(int index)
{
	for(int i=0;i<connect[index].size();i++)
	{
		if(mem[index*1000+connect[index][i]])
		    continue;
	    if(used[connect[index][i]])
	        continue;
	    used[connect[index][i]]=1;
	    dfs(connect[index][i]);
	}
}
int main()
{
//	ifstream fin("input.txt");
//	ofstream fout("output.txt");
	int n,m;
	int k;
    int cnt;
    int i,j;
    int r1;
    bool judge;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
	    cin>>x[i]>>y[i];
	    connect[x[i]].push_back(y[i]);
	    connect[y[i]].push_back(x[i]);
	    able[x[i]*1000+y[i]].push_back(i);
	    able[y[i]*1000+x[i]].push_back(i);
	}
	cin>>k;
	for(i=1;i<=k;i++)
	{
		cnt=0;
	    cin>>l>>r;
	    for(j=l;j<=r;j++)
	    {
	    	judge=false;
	    	mem[x[j]*1000+y[j]]=1;
	    	mem[y[j]*1000+x[j]]=1;
	    	if(able[x[j]*1000+y[j]][0]<l || able[x[j]*1000+y[j]][able[x[j]*1000+y[j]].size()-1]>r)
	    	    	judge=true;
	    	if(judge)
	    	{
	        	mem[x[j]*1000+y[j]]=0;
	        	mem[y[j]*1000+x[j]]=0;
	    	}
//	    	else
//	    	    cout<<x[j]<<" "<<y[j]<<endl;
	    }
	    for(j=1;j<=n;j++)
	    {
	        if(used[j])
	            continue;
	        dfs(j);
	        cnt++;
	    }
	    cout<<cnt<<endl;
	    for(j=1;j<=n;j++)
	        used[j]=0;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]*1000+y[j]]=0;
	    	mem[y[j]*1000+x[j]]=0;
	    }
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int Maxn=505;
int n,m;
vector< pair< int,int > > neigh[Maxn];                        //��¼����
int k,l,r;
bool taken[Maxn];                                             //�൱��used
void Traverse(int v)                                          //Traverse:�߹������й�������Ĳ���
{
	taken[v]=true;
	for(int i=0;i<neigh[v].size();i++)
	{
		pair< int,int > u=neigh[v][i];
		if((u.second<l || u.second>r) && !taken[u.first])     //�Ƿ��Ѿ��߹��ק������
		    Traverse(u.first);
	}
}
int main()
{
//	cin>>n>>m;
	scanf("%d%d",&n,&m);
	int a,b,res;
	for(int i=1;i<=m;i++)
	{
//		cin>>a>>b;
		scanf("%d%d",&a,&b);
		neigh[a].push_back(pair< int,int > (b,i));
		neigh[b].push_back(pair< int,int > (a,i));
	}
//	cin>>k;
	scanf("%d",&k);
	for(int i=1;i<=k;i++)
	{
//		cin>>l>>r;
		scanf("%d%d",&l,&r);
		fill(taken+1,taken+n+1,false);                        //��?���൱��memset(take+1,take+n+1,0)
		res=0;
		for(int j=1;j<=n;j++)                                 //�ӵ�һ���㿪ʼ
		    if(!taken[j])
		    {
		    	res++;
		    	Traverse(j);
		    }
//		cout<<res<<endl;
		printf("%d",res);
	}
	return 0;
}


#include <iostream>
#include <map>
#include <cstdio>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#define fin cin
#define fout cout
using namespace std;
vector<int> connect[505];
int x[10005],y[10005];
map<int,int> mem[10005];
int used[10005];
int l,r;
void dfs(int index)
{
//	cout<<index<<" ";
	for(int i=0;i<connect[index].size();i++)
	{
		if(mem[index][connect[index][i]])
		    continue;
	    if(used[connect[index][i]])
	        continue;
	    used[connect[index][i]]=1;
	    dfs(connect[index][i]);
	}
}
int main()
{
	ifstream fin("input.txt");
	ofstream fout("output.txt");
	int n,m;
	int k;
    int cnt;
    int i,j;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
	    cin>>x[i]>>y[i];
	    connect[x[i]].push_back(y[i]);
	    connect[y[i]].push_back(x[i]);
	}
	cin>>k;
	for(i=1;i<=k;i++)
	{
//		cout<<"i="<<i<<endl;
		cnt=0;
	    cin>>l>>r;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]][y[j]]=1;
	    	mem[y[j]][x[j]]=1;
//	    	cout<<x[j]<<" "<<y[j]<<endl;
	    }
	    for(j=1;j<=n;j++)
	    {
	        if(used[j])
	            continue;
	        dfs(j);
//	        cout<<endl;
	        cnt++;
	    }
	    cout<<cnt<<endl;
	    for(j=1;j<=n;j++)
	        used[j]=0;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]][y[j]]=0;
	    	mem[y[j]][x[j]]=0;
	    }
	}
	return 0;
}


#include <iostream>
#include <map>
//#include <cstdio>
#include <fstream>
#include <vector>
#include <ctime>
//#include <algorithm>
//#include <string>
#define fin cin
#define fout cout
using namespace std;
vector<int> connect[505];
int x[10005],y[10005];
map< int,int > mem;
map< int,vector < int > > able;

int used[10005];
int l,r;
void dfs(int index)
{
	for(int i=0;i<connect[index].size();i++)
	{
		if(mem[index*1000+connect[index][i]])
		    continue;
	    if(used[connect[index][i]])
	        continue;
	    used[connect[index][i]]=1;
	    dfs(connect[index][i]);
	}
}
int main()
{
	ifstream fin("input.txt");
	ofstream fout("output.txt");
	int n,m;
	int k;
    int cnt;
    int i,j;
    int r1;
    bool judge;
	cin>>n>>m;
	for(i=1;i<=m;i++)
	{
	    cin>>x[i]>>y[i];
	    connect[x[i]].push_back(y[i]);
	    connect[y[i]].push_back(x[i]);
//	    able[x[i]*1000+y[i]].push_back(i);
//	    able[y[i]*1000+x[i]].push_back(i);
	}
	cin>>k;
	for(i=1;i<=k;i++)
	{
		cnt=0;
	    cin>>l>>r;
	    for(j=l;j<=r;j++)
	    {
	    	judge=false;
	    	mem[x[j]*1000+y[j]]=1;
	    	mem[y[j]*1000+x[j]]=1;
//	    	if(able[x[j]*1000+y[j]][0]<l || able[x[j]*1000+y[j]][able[x[j]*1000+y[j]].size()-1]>r)
//	    	    	judge=true;
	    	if(judge)
	    	{
	        	mem[x[j]*1000+y[j]]=0;
	        	mem[y[j]*1000+x[j]]=0;
	    	}
//	    	else
//	    	    cout<<x[j]<<" "<<y[j]<<endl;
	    }
/*	    for(j=1;j<=n;j++)
	    {
	        if(used[j])
	            continue;
	        dfs(j);
	        cnt++;
	    }*/
	    cout<<cnt<<endl;
	    for(j=1;j<=n;j++)
	        used[j]=0;
	    for(j=l;j<=r;j++)
	    {
	    	mem[x[j]*1000+y[j]]=0;
	    	mem[y[j]*1000+x[j]]=0;
	    }
	}
	cout<<"time:"<<clock()/CLOCKS_PER_SEC<<endl;
	return 0;
}


/*
6 5
1 2
5 4
2 3
3 1
3 6
6
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<sstream>
#include<cmath>
#include<string>
#include<vector>
#include<deque>
#include<set>
#include<bitset>
#include<map>
#include<functional>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<IOS>
using namespace std;
typedef long long ll;
int n,num[501][2],m,k,ans,a,b;
bool lined[501][501],vis[501];
vector <int> numb[2];
void dfs(int x)
{
	vis[x]=1;
	for (int i=1;i<=n;i++)
	{
		//if (x==i) continue;
		if ((!vis[i]) && lined[x][i])
		{
			//vis[i]=1;
			//lined[x][i]=lined[i][x]=0;
			dfs(i);
		}
	}
	return ;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
		scanf("%d%d",&num[i][0],&num[i][1]);
	/*{
		int a,b;
		scanf("%d%d",&a,&b);
		numb[0].push_back(a);
		numb[1].push_back(b);
	}*/
	scanf("%d",&k);
	while (k--)
	{
		ans=0;
		memset(vis,0,sizeof(vis));
		scanf("%d%d",&a,&b);
		for (int i=1;i<=m;i++)
		{
			if (i>=a && i<=b) continue;
			lined[num[i][0]][num[i][1]]=lined[num[i][1]][num[i][0]]=1;
			//lined[numb[0][i]][numb[1][i]]=1,lined[numb[1][i]][numb[0][i]]=1;
		}
		for (int i=1;i<=n;i++)
		{
			if (!vis[i])
			{
				dfs(i);
				ans++;
			}
		}
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
				lined[i][j]=0;
		printf("%d\n",ans);
	}
	return 0;
}


/*
6 5
1 2
5 4
2 3
3 1
3 6
6
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<sstream>
#include<cmath>
#include<string>
#include<vector>
#include<deque>
#include<set>
#include<bitset>
#include<map>
#include<functional>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<IOS>
using namespace std;
typedef long long ll;
int n,num[501][2],m,k,ans,a,b;
bool lined[501][501],vis[501];
vector <int> numb[2];
void dfs(int x)
{
	vis[x]=1;
	for (int i=1;i<=n;i++)
	{
		//if (x==i) continue;
		if ((!vis[i]) && lined[x][i])
		{
			//vis[i]=1;
			//lined[x][i]=lined[i][x]=0;
			dfs(i);
		}
	}
	return ;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
		scanf("%d%d",&num[i][0],&num[i][1]);
	/*{
		int a,b;
		scanf("%d%d",&a,&b);
		numb[0].push_back(a);
		numb[1].push_back(b);
	}*/
	scanf("%d",&k);
	while (k--)
	{
		ans=0;
		memset(vis,0,n);
		scanf("%d%d",&a,&b);
		for (int i=1;i<=m;i++)
		{
			if (i>=a && i<=b) continue;
			lined[num[i][0]][num[i][1]]=lined[num[i][1]][num[i][0]]=1;
			//lined[numb[0][i]][numb[1][i]]=1,lined[numb[1][i]][numb[0][i]]=1;
		}
		for (int i=1;i<=n;i++)
		{
			if (!vis[i])
			{
				dfs(i);
				ans++;
			}
		}
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
				lined[i][j]=0;
		printf("%d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<bool,bool> pbb;
typedef vector<int> vi;
#define mp make_pair
#define pb push_back
#define ld long double
#define rep(i,n) for (int i=1;i<=n;++i)
int par[505];
int rnk[505];
int res,u[10005],v[10005];
inline void init(int n){
	res=n;
	for (int i=0;i<n;i++){
		par[i]=i;
		rnk[i]=0;
	}
}
inline int f(int x){
	if (par[x]==x) return x;
	else return par[x]=f(par[x]);
}
inline void unite(int x,int y){
	x=f(x);y=f(y);
	if (x==y) return;
	res--;
	if (rnk[x]<rnk[y]) par[x]=y;
	else{
		par[y]=x;
		if (rnk[x]==rnk[y]) rnk[x]++;
	}
}
int main(){
	//ios::sync_with_stdio(false);
	int n,m,k,l,r;
	cin>>n>>m;
	for (int i=0;i<m;i++){
		cin>>u[i]>>v[i];
		u[i]--;v[i]--;
	}
	cin>>k;
	for (int t=1;t<=k;t++){
		cin>>l>>r;
		l--;r--;
		init(n);
		for (int i=0;i<l;i++) unite(u[i],v[i]);
		for (int i=r+1;i<m;i++) unite(u[i],v[i]);
		cout<<res<<endl;
	}
}


/*
6 5
1 2
5 4
2 3
3 1
3 6
6
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<sstream>
#include<cmath>
#include<string>
#include<vector>
#include<deque>
#include<set>
#include<bitset>
#include<map>
#include<functional>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<IOS>
using namespace std;
typedef long long ll;
int n,num[501][2],m,k,ans;
bool lined[501][501],vis[501];
void dfs(int x)
{
	vis[x]=1;
	for (int i=1;i<=n;i++)
	{
		//if (x==i) continue;
		if ((!vis[i]) && lined[x][i])
		{
			//vis[i]=1;
			//lined[x][i]=lined[i][x]=0;
			dfs(i);
		}
	}
	return ;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
		scanf("%d%d",&num[i][0],&num[i][1]);
	scanf("%d",&k);
	while (k--)
	{
		ans=0;
		for (int i=1;i<=n;i++) vis[i]=0;
		int a,b;
		scanf("%d%d",&a,&b);
		for (int i=1;i<=n;i++)
		{
			if (i>=a && i<=b) continue;
			lined[num[i][0]][num[i][1]]=lined[num[i][1]][num[i][0]]=1;
		}
		for (int i=1;i<=n;i++)
		{
			if (!vis[i])
			{
				dfs(i);
				ans++;
			}
		}
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
				lined[i][j]=0;
		printf("%d\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <ctime>
#include <IOS>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
int n,m,q;
int last[511],nol[511],ans;
int a[11111],b[11111];
void init(int n)
{
	ans=n;
	for(int i=1;i<=n;i++)
	{
		nol[i]=0;
		last[i]=i;
	}
}
int find(int x)
{
	if (x==last[x]) return last[x];
	else return last[x]=find(last[x]);
}
void un(int x, int y)
{
	x=find(x);
	y=find(y);
	if(x!=y)
	{
		ans--;
		if(nol[x]<nol[y])
			last[x]=y;
		else
		{
			last[y]=x;
			if(nol[x]==nol[y])nol[x]++;
		}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&a[i],b[i]);
	scanf("%d",&q);
	while(q--)
	{
		int l,r;
		cin>>l>>r;
		init(n);
		for(int i=1;i<l;i++)
			un(a[i],b[i]);
		for(int i=r+1;i<=m;i++)
			un(a[i],b[i]);
		cout<<ans<<endl;
	}
	return 0;
}


/*
6 5
1 2
5 4
2 3
3 1
3 6
6
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<sstream>
#include<cmath>
#include<string>
#include<vector>
#include<deque>
#include<set>
#include<bitset>
#include<map>
#include<functional>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<IOS>
using namespace std;
typedef long long ll;
int n,num[501][2],m,k,ans;
bool lined[501][501],vis[501];
void dfs(int x)
{
	vis[x]=1;
	for (int i=1;i<=n;i++)
	{
		//if (x==i) continue;
		if ((!vis[i]) && lined[x][i])
		{
			//vis[i]=1;
			//lined[x][i]=lined[i][x]=0;
			dfs(i);
		}
	}
	return ;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
		scanf("%d%d",&num[i][0],&num[i][1]);
	scanf("%d",&k);
	while (k--)
	{
		ans=0;
		for (int i=1;i<=n;i++) vis[i]=0;
		int a,b;
		scanf("%d%d",&a,&b);
		for (int i=1;i<=m;i++)
		{
			if (i>=a && i<=b) continue;
			lined[num[i][0]][num[i][1]]=lined[num[i][1]][num[i][0]]=1;
		}
		for (int i=1;i<=n;i++)
		{
			if (!vis[i])
			{
				dfs(i);
				ans++;
			}
		}
		for (int i=1;i<=n;i++)
			for (int j=1;j<=n;j++)
				lined[i][j]=0;
		printf("%d\n",ans);
	}
	return 0;
}


/*
6 5
1 2
5 4
2 3
3 1
3 6
6
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<sstream>
#include<cmath>
#include<string>
#include<vector>
#include<deque>
#include<set>
#include<bitset>
#include<map>
#include<functional>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<IOS>
using namespace std;
typedef long long ll;
int n,num[501][2],m,k,ans;
bool lined[501][501],vis[501];
void dfs(int x)
{
	vis[x]=1;
	for (int i=1;i<=n;i++)
	{
		//if (x==i) continue;
		if ((!vis[i]) && lined[x][i])
		{
			//vis[i]=1;
			lined[x][i]=lined[i][x]=0;
			dfs(i);
		}
	}
	return ;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++)
		scanf("%d%d",&num[i][0],&num[i][1]);
	scanf("%d",&k);
	while (k--)
	{
		ans=0;
		for (int i=1;i<=n;i++) vis[i]=0;
		int a,b;
		scanf("%d%d",&a,&b);
		for (int i=1;i<=n;i++)
		{
			if (i>=a && i<=b) continue;
			lined[num[i][0]][num[i][1]]=lined[num[i][1]][num[i][0]]=1;
		}
		for (int i=1;i<=n;i++)
		{
			if (!vis[i])
			{
				dfs(i);
				ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, m, x, y, u, v, k, ans;
int p[501], cp[501], a[501], b[501];

int find(int x) {
	if (p[x] == x)
		return x;
	return p[x] = find(p[x]);
}

int main(int argc, char **argv) {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i)
		scanf("%d%d", &a[i], &b[i]);
	scanf("%d", &k);
	while (k--) {
		ans = 0;
		scanf("%d%d", &x, &y);
		for (int i = 1; i <= n; ++i)
			p[i] = i;
		for (int i = 1; i < x; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = y + 1; i <= m; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = 1; i <= n; ++i)
			if (find(i) == i)
				++ans;
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, m, x, y, u, v, k, ans;
int p[501], cp[501], a[501], b[501];

int find(int x) {
	if (p[x] == x)
		return x;
	return p[x] = find(p[x]);
}

int main(int argc, char **argv) {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i)
		scanf("%d%d", &a[i], &b[i]);
	scanf("%d", &k);
	while (k--) {
		ans = 0;
		scanf("%d%d", &x, &y);
		for (int i = 1; i <= n; ++i)
			p[i] = i;
		for (int i = 1; i < x; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = y + 1; i <= m; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = 1; i <= n; ++i)
			if (p[i] == i)
				++ans;
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, m, x, y, u, v, k, ans;
int p[501], a[10010], b[10010];

int find(int x) {
	if (p[x] == x)
		return x;
	return p[x] = find(p[x]);
}

int main(int argc, char **argv) {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i)
		scanf("%d%d", &a[i], &b[i]);
	scanf("%d", &k);
	while (k--) {
		ans = 0;
		scanf("%d%d", &x, &y);
		for (int i = 1; i <= n; ++i)
			p[i] = i;
		for (int i = 1; i < x; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = y + 1; i <= m; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = 1; i <= n; ++i)
			if (find(i) == i)
				++ans;
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, m, x, y, u, v, k, ans;
int p[501], a[10010], b[10010];
int l[501], r[501];

inline int find(int x) {
	if (p[x] == x)
		return x;
	return p[x] = find(p[x]);
}

int main(int argc, char **argv) {
	scanf("%d%d", &n, &m);
	int idx = 1;
	for (int i = 1; i <= n; ++i)
		p[i] = i;
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d", &a[i], &b[i]);
		u = find(a[i]), v = find(b[i]);
		if (u != v) {
			l[idx++] = i;
			p[u] = v;
		}
	}
	l[idx] = 1e5;
	for (int i = 1; i <= n; ++i)
		p[i] = i;
	idx = 1;
	for (int i = m; i >= 1; --i) {
		u = find(a[i]);
		v = find(b[i]);
		if (u != v) {
			r[idx++] = i;
			p[u] = v;
		}
	}
	r[idx] = -1;

	scanf("%d", &k);
	while (k--) {
		ans = 0;
		scanf("%d%d", &x, &y);
		for (int i = 0; i <= n; ++i)
			p[i] = i;
		for (int i = 1; l[i] < x; ++i)
			p[find(a[l[i]])] = find(b[l[i]]);
		for (int i = 1; r[i] > y; ++i)
			p[find(a[r[i]])] = find(b[r[i]]);
		for (int i = 1; i <= n; ++i)
			if (p[i] == i)
				++ans;
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, m, x, y, u, v, k, ans;
int p[501], a[10010], b[10010];

inline int find(int x) {
	if (p[x] == x) return x;
	return p[x] = find(p[x]);
}

int main(int argc, char **argv) {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i)
		scanf("%d%d", &a[i], &b[i]);
	scanf("%d", &k);
	while (k--) {
		ans = 0;
		scanf("%d%d", &x, &y);
		for (int i = 1; i <= n; ++i)
			p[i] = i;
		for (int i = 1; i < x; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			p[u] = v;
		}
		for (int i = y + 1; i <= m; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			p[u] = v;
		}
		for (int i = 1; i <= n; ++i)
			if (p[i] == i)
				++ans;
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, m, x, y, u, v, k, ans;
int p[501], a[10010], b[10010];
int l[501], r[501];
int l1[501], l2[501], r1[501], r2[501];

inline int find(int x) {
	if (p[x] == x)
		return x;
	return p[x] = find(p[x]);
}

int main(int argc, char **argv) {
	scanf("%d%d", &n, &m);
	int idx = 1;
	for (int i = 1; i <= n; ++i)
		p[i] = i;
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d", &a[i], &b[i]);
		u = find(a[i]), v = find(b[i]);
		if (u != v) {
			l[idx] = i;
			l1[idx] = a[i];
			r1[idx++] = b[i];
			p[u] = v;
		}
	}
	l[idx] = 1e5;
	for (int i = 1; i <= n; ++i)
		p[i] = i;
	idx = 1;
	for (int i = m; i >= 1; --i) {
		u = find(a[i]);
		v = find(b[i]);
		if (u != v) {
			r[idx] = i;
			l2[idx] = a[i];
			r2[idx++] = b[i];
			p[u] = v;
		}
	}
	r[idx] = -1;

	scanf("%d", &k);
	while (k--) {
		ans = 0;
		scanf("%d%d", &x, &y);
		for (int i = 0; i <= n; ++i)
			p[i] = i;
		for (int i = 1; l[i] < x; ++i)
			p[find(l1[i])] = find(r1[i]);
		for (int i = 1; r[i] > y; ++i)
			p[find(l2[i])] = find(r2[i]);
		for (int i = 1; i <= n; ++i)
			if (p[i] == i)
				++ans;
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int n, m, x, y, u, v, k, ans;
int p[501], cp[501], a[501], b[501];

int find(int x) {
	if (p[x] == x)
		return x;
	return p[x] = find(p[x]);
}

int main(int argc, char **argv) {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i)
		scanf("%d%d", &a[i], &b[i]);
	scanf("%d", &k);
	while (k--) {
		ans = 0;
		scanf("%d%d", &x, &y);
		for (int i = 1; i <= n; ++i)
			p[i] = i;
		for (int i = 1; i < x; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = y + 1; i <= m; ++i) {
			u = find(a[i]);
			v = find(b[i]);
			if (u != v)
				p[u] = v;
		}
		for (int i = 1; i <= n; ++i)
			if (find(p[i]) == i)
				++ans;
		printf("%d\n", ans);
	}
	return 0;
}


#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<ctype.h>
using namespace std;
#define MOD 1000000007
#define PI acos(-1)
#define MP make_pair
#define PB push_back
#define VI vector<int>
#define PII pair<int,int>
#define LL long long
#define SI(x) scanf("%d",&x)
#define PRI(x) printf("%d\n",x)
#define MEM(v,i) memset(v,i,sizeof(v))
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define DEBUG(x) cout<<#x<<"="<<x<<endl

vector<pair<int,int> >arr[502];
int visited[501];
 inline int inp(){
    char c=getchar_unlocked();
    int n=0;
    while(!(c>='0' && c<='9'))
    c=getchar_unlocked();
    while(c>='0' && c<='9'){
    n=n*10 + (c-'0');
    c=getchar_unlocked();
    }
    return n;
    }
void dfs(int v,int l,int r)
{
int i;
visited[v]=1;
        for(i=0;i<arr[v].size();i++)
          {

              if(!visited[arr[v][i].first] && !(arr[v][i].second>=l && arr[v][i].second<=r))
                {

                  dfs(arr[v][i].first,l,r);
                }
          }

}

int main()
{
 int t,n,m,p,q,i,l,r,k;

 cin>>n>>m;
   for(i=1;i<=m;i++)
     {
       p=inp();q=inp();

       arr[p].push_back(make_pair(q,i));
       arr[q].push_back(make_pair(p,i));
     }
scanf("%d",&k);
  while(k--)
   {
  for(i=0;i<=n;i++)
      visited[i]=0;
    l=inp();r=inp();
     int d=0;
        for(i=1;i<=n;i++)
            {
               if(!visited[i])
                 {  d++;
                    dfs(i,l,r);

                 }
            }
printf("%d\n",d);
}
return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
#define max 1000000007
vector<pair<int,int> >arr[502];
int visited[501];
 inline int inp(){
    char c=getchar_unlocked();
    int n=0;
    while(!(c>='0' && c<='9'))
    c=getchar_unlocked();
    while(c>='0' && c<='9'){
    n=n*10 + (c-'0');
    c=getchar_unlocked();
    }
    return n;
    }
void dfs(int v,int l,int r)
{
int i;
visited[v]=1;
        for(i=0;i<arr[v].size();i++)
          {

              if(!visited[arr[v][i].first] && !(arr[v][i].second>=l && arr[v][i].second<=r))
                {

                  dfs(arr[v][i].first,l,r);
                }
          }

}

int main()
{
 int t,n,m,p,q,i,l,r,k;

 cin>>n>>m;
   for(i=1;i<=m;i++)
     {
       p=inp();q=inp();

       arr[p].push_back(make_pair(q,i));
       arr[q].push_back(make_pair(p,i));
     }
scanf("%d",&k);
  while(k--)
   {
  for(i=0;i<=n;i++)
      visited[i]=0;
    l=inp();r=inp();
     int d=0;
        for(i=1;i<=n;i++)
            {
               if(!visited[i])
                 {  d++;
                    dfs(i,l,r);

                 }
            }
printf("%d\n",d);
}
return 0;
}


#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<ctype.h>
using namespace std;
#define MOD 1000000007
#define PI acos(-1)
#define MP make_pair
#define PB push_back
#define VI vector<int>
#define PII pair<int,int>
#define LL long long
#define SI(x) scanf("%d",&x)
#define PRI(x) printf("%d\n",x)
#define MEM(v,i) memset(v,i,sizeof(v))
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define DEBUG(x) cout<<#x<<"="<<x<<endl
vector<pair<int,int> >arr[600];
int visited[600];
void dfs(int v,int l,int r)
{
int i;
visited[v]=1;
        for(i=0;i<arr[v].size();i++)
          {

              if(!visited[arr[v][i].first] && !(arr[v][i].second>=l && arr[v][i].second<=r))
                {

                  dfs(arr[v][i].first,l,r);
                }
          }

}

int main()
{
 int t,n,m,p,q,i,l,r,k;

 SI(n);SI(m);
   for(i=1;i<=m;i++)
     {
       SI(p);SI(q);

       arr[p].push_back(make_pair(q,i));
       arr[q].push_back(make_pair(p,i));
     }
scanf("%d",&k);
  while(k--)
   {
  for(i=0;i<=n;i++)
      visited[i]=0;
    SI(l);SI(r);
     int d=0;
        for(i=1;i<=n;i++)
            {
               if(!visited[i])
                 {  d++;
                    dfs(i,l,r);

                 }
            }
printf("%d\n",d);
}
return 0;
}


#include<iostream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>
#include<deque>
#include<ctype.h>
using namespace std;
#define MOD 1000000007
#define PI acos(-1)
#define MP make_pair
#define PB push_back
#define VI vector<int>
#define PII pair<int,int>
#define LL long long
#define SI(x) scanf("%d",&x)
#define PRI(x) printf("%d\n",x)
#define MEM(v,i) memset(v,i,sizeof(v))
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define DEBUG(x) cout<<#x<<"="<<x<<endl

vector<pair<int,int> >arr[502];
int visited[501];
 inline int inp(){
    char c=getchar_unlocked();
    int n=0;
    while(!(c>='0' && c<='9'))
    c=getchar_unlocked();
    while(c>='0' && c<='9'){
    n=n*10 + (c-'0');
    c=getchar_unlocked();
    }
    return n;
    }
void dfs(int v,int l,int r)
{
int i;
visited[v]=1;
        for(i=0;i<arr[v].size();i++)
          {

              if(!visited[arr[v][i].first] && !(arr[v][i].second>=l && arr[v][i].second<=r))
                {

                  dfs(arr[v][i].first,l,r);
                }
          }

}

int main()
{
 int t,n,m,p,q,i,l,r,k;

 SI(n);SI(m);
   for(i=1;i<=m;i++)
     {
       SI(p);SI(q);

       arr[p].push_back(make_pair(q,i));
       arr[q].push_back(make_pair(p,i));
     }
scanf("%d",&k);
  while(k--)
   {
  for(i=0;i<=n;i++)
      visited[i]=0;
    SI(l);SI(r);
     int d=0;
        for(i=1;i<=n;i++)
            {
               if(!visited[i])
                 {  d++;
                    dfs(i,l,r);

                 }
            }
printf("%d\n",d);
}
return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<functional>
#include<math.h>
#include<assert.h>
#include<stdarg.h>
#include<time.h>
#include<limits.h>
#include<ctype.h>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<iostream>
#include<sstream>
#include<iomanip>
#include<stack>
#include<bitset>
using namespace std;
// }}}
// #defines {{{
#define _CRT_SECURE_NO_WARNINGS
#define ALL(x) (x).begin(),(x).end()
#define ALL2(arr,x) (arr),(arr+x)
#define fl2(i,n) for(int i=0;i<(n);i++)
#define fl3(i,a,b) for(int i=(a);i<=(b);i++)
#define flx(i,a,x) for(int i=0;i<(a);i+=(x))
#define ifl2(i,n) for(int i=(n);i>=0;i--)
#define ifl3(i,a,b) for(int i=(a);i>=(b);i--)
#define iflx(i,a,x) for(int i=(a)-1;i>=0;i-=(x))
#define RI(x) cin>>x
#define DRI(x) int x;RI(x)
#define InDAL(arr,x) int *arr=new int[x]
#define ChDAL(x) new char[x]
#define RII(x,y) cin>>x>>y
#define DRII(x,y) int x,y;RII(x,y)
#define RIII(x,y,z) cin>>x>>y>>z
#define DRIII(x,y,z) int x,y,z;RIII(x,y,z)
#define PI(x) cout<<x
#define FLch(i,x) for(int i=0;x[i];i++)
//define F first
//#define S second
int main()
{
    DRI(n);
    vector<int>vec(n);
    int max = 0,min=0;
    int max_ind,min_ind;
    fl2(i, n) {
        cin >> vec[i];
        if (i == 0)
        {
            max = vec[i];
            max_ind = i;
            min = vec[i];
            min_ind = i;
        }
        if (max < vec[i])
        {
            max = vec[i];
            max_ind = i;
        }
        if (min >= vec[i])
        {
            min = vec[i];
            min_ind = i;
        }
    }
    bool cond = 0;
    long op = 0;
    if ((max_ind == n - 1 && min_ind == 0) || (vec[max_ind] == vec[min_ind]))
    {
        op = 0;
    }
    else if (max_ind - min_ind == -1)
    {
        op = n - 1 - max_ind;
    }
    else
    {
        cout << -1 << endl;
        return 0;
    }
    cout << op << endl;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <math.h>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <fstream>
#include <queue>
using namespace std;

#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
typedef long long ll;

const double EPS = 1e-10;
const int INF = 1<<28;
const long long INFL = 1LL<<62;

string res[] = {"NO","YES"};

const double pi = 3.14159265359;

int a[100001];

int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;++i)
        cin >> a[i];
    bool f = 1;
    for(int i=0;i<n-1;++i)
    {
        if(a[i] > a[i+1])
        {
            f = 0;
            for(int j=i+1;j<n-1;++j)
            {
                if(a[j+1] < a[j])
                {
                    cout << -1 << endl;
                    return 0;
                }
            }
            if(a[n-1] < a[0])
            {
                cout << n-(i+1) << endl;
                return 0;
            }
        }
    }
    if(f)
        cout << 0 << endl;
    else
        cout << -1 << endl;

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <math.h>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <fstream>
#include <queue>
using namespace std;

#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
typedef long long ll;

const double EPS = 1e-10;
const int INF = 1<<28;
const long long INFL = 1LL<<62;

string res[] = {"NO","YES"};

const double pi = 3.14159265359;

int a[100001];

int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;++i)
        cin >> a[i];
    bool f = 1;
    for(int i=0;i<n-1;++i)
    {
        if(a[i+1] < a[i])
        {
            f = 0;
            for(int j=i+1;j<n;++j)
            {
                if(a[j+1] < a[j])
                {
                    cout << -1 << endl;
                    return 0;
                }
            }
            if(a[n-1] < a[0])
            {
                cout << n-(i+1) << endl;
                return 0;
            }
        }
    }
    if(f)
        cout << 0 << endl;
    else
        cout << -1 << endl;

}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <math.h>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <fstream>
#include <queue>
using namespace std;

#define SZ(X) (int)(X).size()
#define ALL(X) (X).begin(),(X).end()
#define ALLR(X) (X).rbegin(),(X).rend()
typedef long long ll;

const double EPS = 1e-10;
const int INF = 1<<28;
const long long INFL = 1LL<<62;

string res[] = {"NO","YES"};

const double pi = 3.14159265359;

int a[100001];

int main()
{
    int n;
    cin >> n;
    for(int i=0;i<n;++i)
        cin >> a[i];
    bool f = 1;
    for(int i=0;i<n-1;++i)
    {
        if(a[i] > a[i+1])
        {
            f = 0;
            for(int j=i+1;j<n-1;++j)
            {
                if(a[j+1] < a[j])
                {
                    cout << -1 << endl;
                    return 0;
                }
            }
            if(a[n-1] <= a[0])
            {
                cout << n-(i+1) << endl;
                return 0;
            }
        }
    }
    if(f)
        cout << 0 << endl;
    else
        cout << -1 << endl;

}


#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	bool small = false, ret = false;
	int n, sIdx = 0, eIdx = 0; scanf("%d", &n);
	int arr[100000], sorted[100000];
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		sorted[i] = arr[i];
		if (i >= 1){
			if (!small){
				if (arr[i - 1] > arr[i]) eIdx = sIdx = i, small = true;
			}
			else if (small)
			if (arr[i - 1] < arr[i]) eIdx = i;
			else{ ret = true; break; }
		}
	}
	sort(sorted, sorted + n);
	int tIdx = sIdx;
	for (int i = 0; i < n; i++, tIdx++){
		if (tIdx <= eIdx){
			if (arr[i + sIdx] != sorted[i]){
				ret = true;
				break;
			}
		}
		else{
			if (arr[i - eIdx] != sorted[i]){
				ret = true;
				break;
			}
		}
	}
	if (ret) printf("-1");
	else printf("%d", !(eIdx == 0 && sIdx == 0) ? eIdx - sIdx + 1 : 0);
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
	bool small = false, ret = false;
	int n, sIdx = 0, eIdx = 0; scanf("%d", &n);
	int arr[100000], sorted[100000];
	for (int i = 0; i < n; i++){
		scanf("%d", &arr[i]);
		sorted[i] = arr[i];
		if (i >= 1){
			if (!small){
				if (arr[i - 1] >= arr[i]) eIdx = sIdx = i, small = true;
			}
			else if (small){
				if (arr[i - 1] <= arr[i]) eIdx = i;
				else{ ret = true; break; }
			}
		}
	}
	if (sIdx == 0 && eIdx == 0){
		printf("0"); return 0;
	}
	sort(sorted, sorted + n);
	int tIdx = sIdx, j = 0;
	for (; tIdx <= eIdx; tIdx++, j++){
		if (arr[j + sIdx] != sorted[j]){
			ret = true;
			break;
		}
	}
	for (; j < n; j++){
		if (arr[j - (eIdx- sIdx +1)] != sorted[j]){
			ret = true;
			break;
		}
	}
	if (ret) printf("-1");
	else printf("%d", !(eIdx == 0 && sIdx == 0) ? eIdx - sIdx + 1 : 0);
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    bool small = false, ret = false;
    int n, sIdx = 0, eIdx = 0; scanf("%d", &n);
    int arr[100000], sorted[100000];
    for (int i = 0; i < n; i++){
        scanf("%d", &arr[i]);
        sorted[i] = arr[i];
        if (i >= 1){
            if (!small){
                if (arr[i - 1] > arr[i]) eIdx = sIdx = i, small = true;
            }
            else if (small)
            if (arr[i - 1] < arr[i]) eIdx = i;
            else{ ret = true; break; }
        }
    }
    sort(sorted, sorted + n);
    int tIdx = sIdx;
    for (int i = 0; i < n; i++, tIdx++){
        if (tIdx <= eIdx){
            if (arr[i + sIdx] != sorted[i]){
                ret = true;
                break;
            }
        }
        else{
            if (arr[i - sIdx] != sorted[i]){
                ret = true;
                break;
            }
        }
    }
    if (ret) printf("-1");
    else printf("%d", !(eIdx == 0 && sIdx == 0) ? eIdx - sIdx + 1 : 0);
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    //freopen("input.txt", "r", stdin);
    bool small = false, ret = false;
    int n, sIdx = 0, eIdx = 0; scanf("%d", &n);
    int arr[100000], sorted[100000];
    for (int i = 0; i < n; i++){
        scanf("%d", &arr[i]);
        sorted[i] = arr[i];
        if (i >= 1){
            if (!small){
                if (arr[i - 1] > arr[i]) eIdx = sIdx = i, small = true;
            }
            else if (small){
                if (arr[i - 1] <= arr[i]) eIdx = i;
                else{ ret = true; break; }
            }
        }
    }
    if (sIdx == 0 && eIdx == 0){
        printf("0"); return 0;
    }
    sort(sorted, sorted + n);
    int tIdx = sIdx, j = 0;
    for (; tIdx <= eIdx; tIdx++, j++){
        if (arr[j + sIdx] != sorted[j]){
            ret = true;
            break;
        }
    }
    for (; j < n; j++){
        if (arr[j - (eIdx- sIdx +1)] != sorted[j]){
            ret = true;
            break;
        }
    }
    if (ret) printf("-1");
    else printf("%d", !(eIdx == 0 && sIdx == 0) ? eIdx - sIdx + 1 : 0);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        v.push_back(kari);
        flag[kari] = i;
    }
    sort(v.begin(), v.end());
    int ok = 0;
    int dif = -1;
    for (int i = 0; i < v.size(); i++){
        int a = i;
        int b = flag[v[i]];
        int sa = 0;
        if (b >= a){
            sa = b - a;
        }
        else{
            sa = (n - b-1) + a;
        }
        if (dif == -1 || dif == sa){
            dif = sa;
            continue;
        }
        else{
            ok = 1;
            break;
        }
    }
    if (ok){
        cout << -1 << endl;
    }
    else{
        cout << dif << endl;
    }
    cin.get();
    cin.get();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        v.push_back(kari);
        flag[kari] = i;
    }
    sort(v.begin(), v.end());
    int ok = 0;
    int dif = -1;
    for (int i = 0; i < v.size(); i++){
        int a = i;
        int b = flag[v[i]];
        int sa = 0;
        if (b >= a){
            sa = b - a;
        }
        else{
            sa = (n - b) + a;
        }
        if (dif == -1 || dif == sa){
            dif = sa;
            continue;
        }
        else{
            ok = 1;
            break;
        }
    }
    if (ok){
        cout << -1 << endl;
    }
    else{
        cout << dif << endl;
    }
    cin.get();
    cin.get();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    int countt = 0;
    int m = 0;
    int z = 0;
    int ok = 0;
    int mintn = -1;
    int minti = -1;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        if (mintn == -1 || mintn > kari||z!=kari&&mintn>=kari){
            minti = i;
            mintn = kari;
        }
        if (i == 0){
            m = kari;
            z = kari;
        }
        else{
            if (z > kari){
                countt++;
            }
            if (countt >= 1 && m < kari){
                ok = 1;
                break;
            }
            z = kari;
        }
    }
    if (ok || countt >= 2){
        cout << -1 << endl;
    }
    else{
        if (minti == 0){
            cout << 0 << endl;
        }
        else{
            cout << (n - minti)<< endl;
        }
    }
    cin.get();
    cin.get();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        v.push_back(kari);
        flag[kari] = i;
    }
    sort(v.begin(), v.end());
    int ok = 0;
    int dif = -1;
    for (int i = 0; i < v.size(); i++){
        int a = i;
        int b = flag[v[i]];
        int sa = 0;
        if (b <= a){
            sa = a-b;
        }
        else{
            sa = (n - b) + a;
        }
        if (dif == -1 || dif == sa){
            dif = sa;
            continue;
        }
        else{
            ok = 1;
            break;
        }
    }
    if (ok){
        cout << -1 << endl;
    }
    else{
        cout << dif << endl;
    }
    cin.get();
    cin.get();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    int countt = 0;
    int m = 0;
    int z = 0;
    int ok = 0;
    int mintn = -1;
    int minti = -1;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        if (mintn == -1 || mintn > kari){
            minti = i;
            mintn = kari;
        }
        if (i == 0){
            m = kari;
            z = kari;
        }
        else{
            if (z > kari){
                countt++;
            }
            if (countt >= 1 && m < kari){
                ok = 1;
                break;
            }
            z = kari;
        }
    }
    if (ok || countt >= 2){
        cout << -1 << endl;
    }
    else{
        if (minti == 0){
            cout << 0 << endl;
        }
        else{
            cout << (n - minti)<< endl;
        }
    }
    cin.get();
    cin.get();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        v.push_back(kari);
        flag[kari] = i;
    }
    sort(v.begin(), v.end());
    int ok = 0;
    int dif = -1;
    for (int i = 0; i < v.size(); i++){
        int a = i;
        int b = flag[v[i]];
        int sa = 0;
        if (b >= a){
            sa = b - a;
        }
        else{
            sa = n - (a-b);
        }
        if (dif == -1 || dif == sa){
            dif = sa;
            continue;
        }
        else{
            ok = 1;
            break;
        }
    }
    if (ok){
        cout << -1 << endl;
    }
    else{
        cout << dif << endl;
    }
    cin.get();
    cin.get();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    deque<int> d;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        d.push_back(kari);
    }
    deque<int> v = d;
    sort(v.begin(), v.end());
    int countt = 0;
    while (countt < n + 1 && d != v){
        countt++;
        d.push_front(d.back());
        d.pop_back();
    }
    if (countt == n + 1){
        cout << -1 << endl;
    }
    else{
        cout << countt << endl;
    }
    cin.get();
    cin.get();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define M_PI 3.141592653589793238
#define EPS 1e-9
#define LINF 1e30  //long long int
#define IINF 1000000000  //int
#define CP complex<long double>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
static const int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, -1, 0, 1 };
long double ccw(long double kari, long double kari1, long double kari2, long double kari3){  //内積
    return kari*kari3 - kari1*kari2;
}
long long int oneday(int year, int month, int day){  //1年1月1日からの、経過日数
    return 365 * year + (year / 4) - (year / 100) + (year / 400) + (306 * (month + 1) / 10) + day - 429;
}
long long int ppow(int a1, int b1){  //a^b
    long long int rr = a1;
    long long int r = 1;
    while (b1>0){
        if (b1 & 1){
            r *= rr;
        }
        rr *= rr;
        b1 >>= 1;
    }
    return r;
}
class seg{
    vector<int> s1;
    int siz; //size
    seg(int kari){  //size
        siz = kari;
        s1.assign(siz, -1);
    }
    void add(int b, int n){  //bにnを加える
        b += siz - 1;
        s1[b] = n;
        while (b > 0){
            b = (b - 1) / 2;
            /*if (s1[b] == -1 || s1[b] > n){  //min

            }
            s1[b] += n;  //add*/
        }
    }
    int ch(){

    }
    int find(int a, int b){  //a以上b未満の中で

    }
};
/*宣言*/
#define MAX_N 100002
int n = 0;
vector<int> v;
int flag[MAX_N];
int main(){
    cin >> n;
    for (int i = 0; i < n; i++){
        int kari = 0;
        cin >> kari;
        v.push_back(kari);
        flag[kari] = i;
    }
    sort(v.begin(), v.end());
    int ok = 0;
    int dif = -1;
    for (int i = 0; i < v.size(); i++){
        int a = flag[v[i]];
        int b = i;
        int sa = 0;
        if (b < a){
            sa = (n-a)+b;
        }
        else{
            sa = b-a;
        }
        if (dif == -1 || dif == sa){
            dif = sa;
        }
        else{
            ok = 1;
            break;
        }
    }
    if (ok){
        cout << -1 << endl;
    }
    else{
        cout << min(dif, n - dif) << endl;
    }
    cin.get();
    cin.get();
    return 0;
}