#include <bits/stdc++.h>
using namespace std;
const int max_N = 10000 + 10;
short int dp[max_N][max_N], par[max_N], size[max_N];
pair<int, int> e[max_N];
int findpar(int v) {
  if (par[par[v]] != par[v]) par[v] = findpar(par[v]);
  return par[v];
}
bool dsu(int a, int b) {
  a = findpar(a), b = findpar(b);
  if (a == b) return true;
  if (size[a] < size[b]) swap(a, b);
  par[b] = a;
  size[a] += size[b];
  return false;
}
int main() {
  int n, m, k, l, r;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--, y--;
    e[i] = make_pair(x, y);
  }
  for (int i = 0; i < n; i++) par[i] = i, size[i] = 1;
  dp[0][0] = n;
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j < i - 1; j++) dsu(e[j].first, e[j].second);
    if (i > 0) {
      if (dsu(e[i - 1].first, e[i - 1].second))
        dp[i][0] = dp[i - 1][0];
      else
        dp[i][0] = dp[i - 1][0] - 1;
    }
    for (int j = m; j > i; j--) {
      int t = (j + 1) % (m + 1);
      if (dsu(e[j - 1].first, e[j - 1].second))
        dp[i][j] = dp[i][t];
      else
        dp[i][j] = dp[i][t] - 1;
    }
    for (int j = 0; j < n; j++) par[j] = j;
  }
  scanf("%d", &k);
  for (int i = 0; i < k; i++) {
    scanf("%d %d", &l, &r);
    printf("%d\n", dp[l - 1][(r + 1) % (m + 1)]);
  }
  return 0;
}
