#include <bits/stdc++.h>
using namespace std;
struct s {
  int a, b;
};
s edge[30005];
int par[505];
bool vis[505];
vector<int> v1[505], v2[505];
int find(int x) {
  if (x == par[x]) return x;
  par[x] = find(par[x]);
  return par[x];
}
void dfs(int x, int a, int b) {
  int i, sz, j;
  vis[x] = 1;
  sz = v1[x].size();
  for (i = 0; i < sz && v1[x][i] < a; i++) {
    if (edge[v1[x][i]].a == x) {
      j = edge[v1[x][i]].b;
      if (!vis[j]) dfs(j, a, b);
    } else {
      j = edge[v1[x][i]].a;
      if (!vis[j]) dfs(j, a, b);
    }
  }
  sz = v2[x].size();
  for (i = 0; i < sz && v2[x][i] > b; i++) {
    if (edge[v2[x][i]].a == x) {
      j = edge[v2[x][i]].b;
      if (!vis[j]) dfs(j, a, b);
    } else {
      j = edge[v2[x][i]].a;
      if (!vis[j]) dfs(j, a, b);
    }
  }
}
int main() {
  int n, m, i, p, q, a, b, j, ans, k;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) par[i] = i;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &edge[i].a, &edge[i].b);
    p = find(edge[i].a);
    q = find(edge[i].b);
    if (p != q) {
      par[q] = p;
      v1[edge[i].a].push_back(i);
      v1[edge[i].b].push_back(i);
    }
  }
  for (i = 1; i <= n; i++) par[i] = i;
  for (i = m; i >= 1; i--) {
    p = find(edge[i].a);
    q = find(edge[i].b);
    if (p != q) {
      par[q] = p;
      v2[edge[i].a].push_back(i);
      v2[edge[i].b].push_back(i);
    }
  }
  scanf("%d", &k);
  for (i = 0; i < k; i++) {
    scanf("%d%d", &a, &b);
    for (j = 1; j <= n; j++) vis[j] = 0;
    ans = 0;
    for (j = 1; j <= n; j++) {
      if (!vis[j]) {
        dfs(j, a, b);
        ans++;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
