#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long maxn = 1e6 + 700;
const long long mod = 1e9 + 7;
inline bool read(long long &num) {
  char in;
  bool IsN = false;
  in = getchar();
  if (in == EOF) return false;
  while (in != '-' && (in < '0' || in > '9')) in = getchar();
  if (in == '-') {
    IsN = true;
    num = 0;
  } else
    num = in - '0';
  while (in = getchar(), in >= '0' && in <= '9') {
    num *= 10, num += in - '0';
  }
  if (IsN) num = -num;
  return true;
}
long long n, m, p;
long long num[maxn];
struct node {
  int x, y;
} ed[maxn];
pair<int, int> q[maxn];
int pre[maxn];
int pl[10005][505], pr[10005][505];
int vis[maxn];
int Find(int x, int *c) { return x == c[x] ? x : c[x] = Find(c[x], c); }
long long solve(int l, int r) {
  for (int i = 1; i <= n; i++) pre[i] = pl[l][i], vis[i] = 0;
  for (int i = 1; i <= n; i++) {
    int dx = Find(i, pre), dy = Find(pr[r][i], pre);
    pre[dx] = dy;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int dx = Find(i, pre);
    if (dx == i) ans++;
  }
  return ans;
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) scanf("%d%d", &ed[i].x, &ed[i].y);
  for (int i = 1; i <= n; i++) pl[0][i] = i, pr[m + 1][i] = i;
  for (int i = 1; i <= m; i++) {
    for (int k = 1; k <= n; k++) pl[i][k] = pl[i - 1][k];
    int dx = Find(ed[i].x, pl[i]), dy = Find(ed[i].y, pl[i]);
    if (dx != dy) pl[i][dx] = dy;
  }
  for (int i = m; i >= 1; i--) {
    for (int k = 1; k <= n; k++) pr[i][k] = pr[i + 1][k];
    int dx = Find(ed[i].x, pr[i]), dy = Find(ed[i].y, pr[i]);
    if (dx != dy) pr[i][dx] = dy;
  }
  read(p);
  for (int i = 1; i <= p; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%lld\n", solve(x - 1, y + 1));
    ;
  }
  return 0;
}
