#include <bits/stdc++.h>
using namespace std;
unsigned int const one = -1;
int const maxn = 505, maxm = 10005;
struct para {
  int v, w;
};
para V[10005];
int r[505], n;
struct dsu {
  int f[maxn], c;
  dsu() {
    for (int i = 0; i < maxn; i++) f[i] = i;
  }
  int find(int a) {
    if (f[a] == a)
      return a;
    else
      return f[a] = find(f[a]);
  }
  void join(int a, int b) {
    a = find(a);
    b = find(b);
    if (a != b) {
      if (r[b] < r[a]) swap(a, b);
      c--;
      f[a] = b;
      r[a]++;
    }
  }
};
dsu ld[maxm], rd[maxm], D;
int x[10005], y[10005];
int main() {
  int m, k;
  para p;
  bool debug = false;
  if (debug) {
    n = 500;
    m = 10000;
  } else {
    scanf("%d%d", &n, &m);
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x[i], &y[i]);
  }
  ld[0].c = n;
  for (int i = 1; i <= m; i++) {
    ld[i] = ld[i - 1];
    ld[i].join(x[i], y[i]);
  }
  for (int i = 1; i <= n; i++) r[i] = 0;
  rd[m + 1].c = n;
  for (int i = m; i >= 1; i--) {
    rd[i] = rd[i + 1];
    rd[i].join(x[i], y[i]);
  }
  if (debug)
    k = 10000;
  else
    scanf("%d", &k);
  int L, R;
  for (int i = 0; i < k; i++) {
    if (debug)
      L = R = i + 1;
    else
      scanf("%d%d", &L, &R);
    for (int j = 1; j <= n; j++) {
      r[j] = 0;
    }
    D = ld[L - 1];
    for (int j = 1; j <= n; j++) {
      D.join(j, rd[R + 1].f[j]);
    }
    printf("%d\n", D.c);
  }
  if (debug)
    std::cout << "����� ������ ���������: " << double(clock()) / CLOCKS_PER_SEC
              << " �." << std::endl;
  return 0;
}
