#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int a, b;
};
Edge ed[10005];
int pt1[10005][505];
int pt2[10005][505];
int find(int v, int *pt) {
  if (pt[v] == v) return v;
  pt[v] = find(pt[v], pt);
  return pt[v];
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    for (int i = 1; i <= m; i++) cin >> ed[i].a >> ed[i].b;
    for (int i = 1; i <= n; i++) pt1[0][i] = i;
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= n; j++) pt1[i][j] = pt1[i - 1][j];
      int fa = find(ed[i].a, pt1[i]);
      int fb = find(ed[i].b, pt1[i]);
      if (fa < fb)
        pt1[i][fb] = fa;
      else if (fa > fb)
        pt1[i][fa] = fb;
    }
    for (int i = 1; i <= n; i++) pt2[m + 1][i] = i;
    for (int i = m; i >= 1; i--) {
      for (int j = 1; j <= n; j++) pt2[i][j] = pt2[i + 1][j];
      int fa = find(ed[i].a, pt2[i]);
      int fb = find(ed[i].b, pt2[i]);
      if (fa < fb)
        pt2[i][fb] = fa;
      else if (fa > fb)
        pt2[i][fa] = fb;
    }
    int k, l, r;
    cin >> k;
    int tmp[505];
    while (k--) {
      cin >> l >> r;
      for (int i = 1; i <= n; i++) tmp[i] = pt1[l - 1][i];
      for (int i = 1; i <= n; i++) {
        int f = find(i, pt2[r + 1]);
        if (f != i) {
          int ff = find(f, tmp);
          int fi = find(i, tmp);
          if (ff < fi)
            tmp[fi] = ff;
          else if (ff > fi)
            tmp[ff] = fi;
        }
      }
      int res = 0;
      for (int i = 1; i <= n; i++) {
        int f = find(i, tmp);
        if (f == i) res++;
      }
      cout << res << '\n';
    }
  }
  return 0;
}
