#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
inline void read(int &tx) {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  tx = x * f;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
int n, m, x[30001], y[30001], R[10101][511], L[10101][511], xx, yy;
inline int get(int num, int x) {
  return x == L[num][x] ? x : L[num][x] = get(num, L[num][x]);
}
inline int get1(int num, int x) {
  return x == R[num][x] ? x : R[num][x] = get1(num, R[num][x]);
}
inline void pre_l() {
  for (int i = 1; i <= n; i++) L[0][i] = i;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) L[i][j] = L[i - 1][j];
    int tx = get(i, x[i]), ty = get(i, y[i]);
    if (tx != ty) L[i][tx] = ty;
  }
}
inline void pre_r() {
  for (int i = 1; i <= n; i++) R[m + 1][i] = i;
  for (int i = m; i >= 1; i--) {
    for (int j = 1; j <= n; j++) R[i][j] = R[i + 1][j];
    int tx = get1(i, x[i]), ty = get1(i, y[i]);
    if (tx != ty) R[i][tx] = ty;
  }
}
int tmp[30001], q;
inline int tmp_get(int x) { return x == tmp[x] ? x : tmp[x] = tmp_get(tmp[x]); }
inline void solve(int l, int r) {
  for (int i = 1; i <= n; i++) tmp[i] = L[l][i];
  for (int i = 1; i <= n; i++) {
    int u = i, v = R[r][i];
    int tx = tmp_get(u), ty = tmp_get(v);
    if (tx != ty) tmp[tx] = ty;
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (tmp_get(i) == i) cnt++;
  writeln(cnt);
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) {
    read(x[i]);
    read(y[i]);
  }
  pre_l();
  pre_r();
  read(q);
  for (int i = 1; i <= q; i++) {
    read(xx);
    read(yy);
    solve(xx - 1, yy + 1);
  }
}
