#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, l, r, tail, head;
  bool u[502];
  int q[502];
  scanf("%d%d", &n, &m);
  vector<vector<pair<int, int> > > g(n);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &l, &r), l--, r--;
    g[l].push_back(make_pair(r, i));
    g[r].push_back(make_pair(l, i));
  }
  int K;
  scanf("%d", &K);
  while (K--) {
    tail = head = 0;
    scanf("%d%d", &l, &r), l--, r--;
    int cnt = 0;
    memset(u, 0, sizeof(u));
    for (int i = 0; i < n; i++)
      if (!u[i]) {
        q[tail++] = i;
        cnt++;
        while (head != tail) {
          int v = q[head++];
          for (vector<pair<int, int> >::iterator it = g[v].begin();
               it != g[v].end() && it->second < l; it++) {
            if (!u[it->first]) {
              u[it->first] = true;
              q[tail++] = it->first;
            }
          }
          if (g[v].begin() != g[v].end())
            for (vector<pair<int, int> >::iterator it = g[v].end() - 1;
                 it->second > r; it--) {
              if (!u[it->first]) {
                u[it->first] = true;
                q[tail++] = it->first;
              }
              if (it == g[v].begin()) break;
            }
        }
      }
    printf("%d\n", cnt);
  }
}
