#include <bits/stdc++.h>
using namespace std;
int d[2][10100][505], a[10100], b[10100], n, m;
void copy(int t1, int n1, int t2, int n2) {
  for (int i = 1; i <= n; ++i) d[t2][n2][i] = d[t1][n1][i];
}
void ini(int t, int n) {
  for (int i = 0; i < 505; ++i) d[t][n][i] = i;
}
int get(int t, int n, int x) {
  return d[t][n][x] = d[t][n][x] == x ? x : get(t, n, d[t][n][x]);
}
void uni(int t, int n, int x, int y) {
  x = get(t, n, x);
  y = get(t, n, y);
  if (rand() & 1) swap(x, y);
  d[t][n][x] = y;
}
void read(void) {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) cin >> a[i] >> b[i];
}
void make(void) {
  ini(0, 0);
  for (int i = 1; i <= m; ++i) {
    copy(0, i - 1, 0, i);
    uni(0, i, a[i], b[i]);
  }
  ini(1, m + 1);
  for (int i = m; i >= 1; --i) {
    copy(1, i + 1, 1, i);
    uni(1, i, a[i], b[i]);
  }
}
void que(int l, int r) {
  l--, r++;
  copy(0, l, 1, 0);
  for (int i = 1; i <= n; ++i) uni(1, 0, i, d[1][r][i]);
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    if (d[1][0][i] == i) ans++;
  cout << ans << "\n";
}
void kill(void) {
  int k;
  cin >> k;
  while (k--) {
    int l, r;
    cin >> l >> r;
    que(l, r);
  }
}
int main() {
  read();
  make();
  kill();
  return 0;
}
