#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el "\n"
#define _ROOT_ int main()
#pragma GCC optimize("O2")
#define FOR(i,l,r) for(int i = l ; i <= r ; i ++)
#define FORD(i,r,l) for(int i = r ; i >= l ; i --)
#define REP(i, a ) for(int i = 0 ; i < a ; i ++ )
#define fi first
#define se second
#define M 1000000007
#define MAXN 1000001
#define PI acos(-1)
#define INF (1ll<<30)
#define BLOCK_SIZE 425
#define LOG 19
#define BASE 256
#define NAME "file"
#define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end());
using namespace std;
const ll MOD[] = {(ll)1e9 + 2277, (ll)1e9 + 5277, (ll)1e9 + 8277, (ll)1e9 + 9277, (ll) 1e9 + 7 };
const ll NMOD = 1;

ll n, q, scc, m  ;
ll res[MAXN] ;
ll arr[MAXN];
pair<ll,ll> edge[MAXN] ;
ll lab[MAXN] ;

struct Data {
    ll u, sz ;
};
stack<Data> his ;
struct Query {
    ll l, r, id ;
    bool operator < (const Query & other ) const {
        ll blA= l / BLOCK_SIZE, blB = other.l / BLOCK_SIZE ;
        if(blA == blB) return r < other.r;
        return blA < blB ;
    }
};
vector<Query > que ;
void reset() {
    FOR(i, 0, n ) lab[i] = -1 ;
    while(!his.empty()) his.pop() ;
    scc = n ;
}

ll find_set(ll a ) {
    return lab[a] < 0 ? a : find_set(lab[a])  ;
}
void roll_back() {
    scc += his.size() / 2;
    while(!his.empty() ) {
        Data u = his.top() ;
        his.pop() ;
        lab[u.u] = u.sz ;
    }
}
bool union_set(ll u, ll v, ll snapshot ) {
    u = find_set(u) ;
    v = find_set(v) ;
    if(u == v ) return false ;
    if(lab[u] > lab[v]) swap(u, v ) ;
    if(snapshot ) his.push({u, lab[u] }) ;
    if(snapshot ) his.push({v, lab[v] }) ;
    lab[u] += lab[v] ;
    lab[v]=  u ;
    scc -- ;
    return true ;
}
void init() {
    cin>> n >> m  ;
    FOR(i, 1, m ) cin >> edge[i].fi >> edge[i].se ;
    cin >> q;
    FOR(i, 1, q ) {
        ll l, r, id ;
        cin >> l >> r;
        que.push_back({l, r,i });
    }
}

void solve() {

    sort(que.begin(), que.end() ) ;
    ll curBlock = -1 ;
    ll curR  = - 1;
    for(auto [l, r, id ] : que ) {
        ll block = l / BLOCK_SIZE ;
        if(block != curBlock ) {
            reset() ;
            curBlock= block ;
            curR = (block + 1) * BLOCK_SIZE ;
        }
        while(curR <= r ) union_set(edge[curR].fi, edge[curR].se, 0 ), curR ++  ;
        ll EndBlockL = min( {  (block + 1 ) * BLOCK_SIZE - 1 , m, r } )  ;
//        cout << l << " " << EndBlockL << " " << id  << el ;
        FOR(i, l,  EndBlockL) union_set(edge[i].fi, edge[i].se, 1 ) ;
        res[id] = scc ;
        roll_back() ;
    }
    FOR(i, 1,  q ) cout << res[i] << el ;
}

_ROOT_ {
    // freopen(NAME".inp" , "r" , stdin);
    // freopen(NAME".out" , "w", stdout) ;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(nullptr)) ;
    int t = 1; // cin >> t ;
    while(t--) {
        init();
        solve();
    }
    return (0&0);
}
