#include <bits/stdc++.h>
using namespace std;
int par[505], l[10005], r[10005];
int pp[10005][500], cn[10005], pq[10005][500];
int find_par(int i) {
  if (par[i] == i) return i;
  int p = find_par(par[i]);
  par[i] = p;
  return p;
}
int main() {
  int n, m, i, j, k, x, y, p, q, t;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d", &l[i]);
    scanf("%d", &r[i]);
  }
  for (i = 1; i <= n; i++) {
    pp[0][i] = i;
    par[i] = i;
  }
  cn[0] = n;
  p = n;
  for (i = 1; i <= m; i++) {
    j = find_par(l[i]);
    k = find_par(r[i]);
    if (j != k) {
      par[max(j, k)] = min(j, k);
      p--;
    }
    cn[i] = p;
    for (j = 1; j <= n; j++) pp[i][j] = find_par(j);
  }
  for (i = 1; i <= n; i++) {
    pq[m + 1][i] = i;
    par[i] = i;
  }
  for (i = m; i > 0; i--) {
    j = find_par(l[i]);
    k = find_par(r[i]);
    if (j != k) {
      par[max(j, k)] = min(j, k);
    }
    for (j = 1; j <= n; j++) pq[i][j] = find_par(j);
  }
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &x);
    scanf("%d", &y);
    p = cn[x - 1];
    for (i = 1; i <= n; i++) {
      par[i] = pp[x - 1][i];
    }
    for (i = 1; i <= n; i++) {
      j = find_par(i);
      k = find_par(pq[y + 1][i]);
      if (j != k) {
        par[max(j, k)] = min(j, k);
        p--;
      }
    }
    printf("%d\n", p);
  }
  return 0;
}
