#include <bits/stdc++.h>
using namespace std;
int n, m, Q, l, r, x[10005], y[10005];
struct DSU {
  int f[505], c;
  int get(int x) { return f[x] ? f[x] = get(f[x]) : x; }
  void Link(int x, int y) {
    x = get(x), y = get(y);
    if (x != y) f[x] = y, c++;
  }
} L[10005], R[10005], ans;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d%d", x + i, y + i), L[i] = L[i - 1], L[i].Link(x[i], y[i]);
  for (int i = m; i; i--) R[i] = R[i + 1], R[i].Link(x[i], y[i]);
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &l, &r);
    DSU ans = L[l - 1];
    for (int i = 1; i <= n; i++)
      if (R[r + 1].f[i]) ans.Link(i, R[r + 1].f[i]);
    printf("%d\n", n - ans.c);
  }
}
