#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>

namespace internal {
  __attribute__((const)) constexpr bool is_pow2(int n) { return __builtin_popcount(n) == 1; }
  __attribute__((const)) constexpr int floor_log2(int n) { return std::numeric_limits<int>::digits - __builtin_clz(n); }
  __attribute__((const)) constexpr int ceil_pow2(int n) { return is_pow2(n) ? n : (1 << (floor_log2(n) + 1)); }
}  // namespace internal

template <class ElemType = long long, ElemType id = 0, class operation = std::plus<void>>
class segment_tree {
  int size;
  std::vector<ElemType> data;

  template <class ReturnType>
  ReturnType query(const int node, const int node_L, const int node_R, const int L, const int R) const {
    if (R <= node_L || node_R <= L) return static_cast<ReturnType>(id);
    if (L <= node_L && node_R <= R) return static_cast<ReturnType>(data[node]);
    const int mid = node_L + (node_R - node_L) / 2;
    return operation {}(query<ReturnType>(node * 2, node_L, mid, L, R), query<ReturnType>(node * 2 + 1, mid, node_R, L, R));
  }

  public:
  segment_tree(void) : size() {}
  segment_tree(const int n) : size(internal::ceil_pow2(n)), data(size * 2, id) {}

  ElemType get(const int idx) const {
    return data[idx + size];
  }

  template <class ReturnType = ElemType>
  ReturnType prod(const int L, const int R) const {
    assert(L <= R);
    return query<ReturnType>(1, 0, size, L, R);
  }

  void set(int idx, const ElemType val) {
    idx += size;
    data[idx] = val;
    while (idx > 1) {
      if ((idx & 1) == 0)
        data[idx >> 1] = operation {}(data[idx], data[idx + 1]);
      else
        data[idx >> 1] = operation {}(data[idx - 1], data[idx]);
      idx >>= 1;
    }
  }
};

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> A(N);
  for (int i = 0; i < N; i++)
    std::cin >> A[i];

  struct op {
    constexpr auto operator()(long long x, long long y) { return std::min(x, y); }
  };

  constexpr int INF        = 1000000000;
  constexpr long long LINF = 1000000000000000000LL;

  segment_tree<long long, LINF, op> S(N + 1);

  for (int i = 0; i <= N; i++)
    S.set(i, i);
  for (int i = 0; i < M; i++) {
    long long tmp = S.get(A[i]);
    S.set(A[i], tmp + INF);
  }

  long long ans = LINF;

  for (int i = 0; i <= N - M; i++) {
    ans = std::min(ans, S.prod(0, N + 1));

    long long tmp = S.get(A[i]);
    S.set(A[i], tmp - INF);

    if (i < N - M) {
      tmp = S.get(A[i + M]);
      S.set(A[i + M], tmp + INF);
    }
  }

  std::cout << ans << '\n';
}

#include<bits/stdc++.h>

#define int long long
#define pll pair<long long,long long>

#define in(x) long long x;cin>>x
#define pb push_back
#define F first
#define S second
#define endl "\n"

#define test() int t;cin>>t;while(t--)
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "
#define deb(x) cout<<#x<<" "<<x<<endl;

const long long mod = 1e9 + 7;
const int MAX = INT_MAX;
const long long inf = 1e18 + 5;
const double pi = 3.14159265358979323846;

int dirX[] = {1, -1, 0, 0};
int dirY[] = {0, 0, 1, -1};

using namespace std;
vector<int> adjlist[200001];
pll indexes[200005];
int ind;
map<int, vector<int>> dist;

void dfs(int src, int dis)
{
	ind++;
	indexes[src].F = ind;
	dist[dis].push_back(ind);
	for (auto it : adjlist[src])
	{
		dfs(it, dis + 1);
	}
	indexes[src].S = ind;
}
int func(int s, int e, int d)
{
	auto l = lower_bound(dist[d].begin(), dist[d].end(), s) - dist[d].begin();
	auto r = upper_bound(dist[d].begin(), dist[d].end(), e) - dist[d].begin();
	r--;
	return r - l + 1;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	in(n);
	for (int i = 2; i <= n; i++)
	{
		in(t);
		adjlist[t].pb(i);
	}
	ind = 0;
	dfs(1, 0);
	// for (int i = 1; i <= n; i++)
	// {
	// 	cout << i << "-> ";
	// 	for (auto it : adjlist[i])
	// 		cout << it << " ";
	// 	cout << endl;
	// }
	// cout << endl;
	// for (int i = 1; i <= n; i++)
	// 	cout << i << " -> " << indexes[i].F << " " << indexes[i].S << endl;
	// cout << endl;
	// for (auto it : dist)
	// {
	// 	cout << it.F << "-> ";
	// 	for (auto i : it.S)
	// 		cout << i << " ";
	// 	cout << endl;
	// }
	test()
	{
		in(u); in(d);
		int s = indexes[u].F;
		int e = indexes[u].S;
		// cout << s << " " << e << endl;
		cout << func(s, e, d) << endl;
	}
	return 0;
}