#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vec(...) vector<__VA_ARGS__>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine

const int max_n = 500000;

using namespace std;
int n;
vi adj[max_n];

vec(pii) qry[max_n];
int ans[max_n];


int depth[max_n],siz[max_n],ep[max_n];

void dfsfsiz(int x,int par=-1) {
	siz[x]=1;
	for(auto y : adj[x]) {
		if(y==par)continue;
		depth[y]=depth[x]+1;
		dfsfsiz(y,x);
		siz[x]+=siz[y];
	}
}

map <int,int> mp[max_n];

void dfs(int x,int par=-1) {
	
	int mx=0,vtk=x;

	for(auto y : adj[x]) {
		if(y == par)continue;
		dfs(y,x);
		if(siz[y]>mx) {
			mx=siz[y];
			vtk=ep[y];
		}
	}

	mp[vtk][depth[x]]++;

	for(auto y : adj[x]) {
		if(ep[y]==vtk)continue;
		for(auto p : mp[ep[y]]) {
			mp[vtk][p.fi]+=p.se;
		}
	}

	for(auto p : qry[x]) {
		ans[p.se]=mp[vtk][p.fi];
	}


	ep[x]=vtk;
}

int main(){
fcin;
	cin>>n;
	rep(i,n-1) {
		int x;cin>>x;x--;
		adj[x].pb(i+1);
		adj[i+1].pb(x);
	}

	dfsfsiz(0);
	
	int q;
	cin>>q;
	rep(_,q) {
		int x,d;
		cin>>x>>d;
		x--;
		qry[x].pb({d,_});
	}
	
	dfs(0);

	rep(i,q)cout<<ans[i]<<"\n";

/*
*/

    return 0;   
}
#include <bits/stdc++.h>
using namespace std;

void solve(){
	int n;
	cin>>n;
	vector<int> a(n);
	for(int i=0;i<n;i++)cin>>a[i];
	vector<int> ans;
	for(int t=0;;t++){
		bool up=true;
		for(int i=0;i+1<n;i++) if(a[i]>a[i+1]) up=false;
		if(up) break;
		int s=t%2;
		while(s+1<n && a[s]<a[s+1]) s+=2;
		if(s+1>=n){
			ans.push_back(s-2);
			swap(a[s-2],a[s-1]);
		} else{
			ans.push_back(s);
			swap(a[s],a[s+1]);
		}
	}
	cout << ans.size() << "\n";
	for(int i=0;i<ans.size();i++) cout << ans[i]+1 << " ";
    cout << "\n";
}

int main(){
	int t;
	cin>>t;
	while(t--) solve();
}