#include <bits/stdc++.h>
using namespace std;
int n, m, k, x[10010], y[10010];
struct st {
  int f[510];
} R[10010], L[10010], ans;
int find(st &a, int x) {
  if (a.f[x] == 0) return x;
  return a.f[x] = find(a, a.f[x]);
}
void add(st &a, int x, int y) {
  int fx = find(a, x), fy = find(a, y);
  if (fx != fy) a.f[fx] = fy;
}
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], add(L[i], x[i], y[i]);
  for (int i = m; i >= 0; i--) R[i] = R[i + 1], add(R[i], x[i], y[i]);
  scanf("%d", &k);
  for (int i = 1; i <= k; i++) {
    int l, r, cal = 0;
    scanf("%d%d", &l, &r);
    ans = L[l - 1];
    for (int i = 1; i <= n; i++)
      if (R[r + 1].f[i] != 0) add(ans, i, R[r + 1].f[i]);
    for (int i = 1; i <= n; i++)
      if (ans.f[i] == 0) cal++;
    printf("%d\n", cal);
  }
  return 0;
}
