#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int f[510];
} arr;
arr l[10010], r[10010];
int x[10010], y[10010];
int b[510];
arr tmp;
int find(arr *t, int p) {
  if ((*t).f[p] == p)
    return p;
  else
    return (*t).f[p] = find(t, (*t).f[p]);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d%d", &x[i], &y[i]);
  for (int i = 1; i <= n; i++) {
    l[0].f[i] = i;
    tmp.f[i] = i;
  }
  for (int i = 1; i <= m; i++) {
    if (find(&tmp, x[i]) != find(&tmp, y[i]))
      tmp.f[find(&tmp, x[i])] = find(&tmp, y[i]);
    l[i] = tmp;
  }
  for (int i = 1; i <= n; i++) {
    r[m + 1].f[i] = i;
    tmp.f[i] = i;
  }
  for (int i = m; i; i--) {
    if (find(&tmp, x[i]) != find(&tmp, y[i]))
      tmp.f[find(&tmp, x[i])] = find(&tmp, y[i]);
    r[i] = tmp;
  }
  int k;
  scanf("%d", &k);
  for (int j = 0; j < k; j++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v++;
    tmp = l[u];
    for (int i = 1; i <= n; i++)
      if (find(&tmp, i) != find(&tmp, find(&r[v], i)))
        tmp.f[find(&tmp, i)] = find(&tmp, find(&r[v], i));
    memset(b, 0, sizeof(b));
    int ans = 0;
    for (int i = 1; i <= n; i++)
      if (b[find(&tmp, i)] == 0) {
        b[find(&tmp, i)] = 1;
        ans++;
      }
    printf("%d\n", ans);
  }
  return 0;
}
