#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
	int n;
    cin>>n;

    vll a(n);

    fp(i,0,n)
    cin>>a[i];

    int c=0;
    int b=n*(n-1)/2-1;
    int d=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i;j<n-i-1;j++)
        {
            ll temp=a[j];
            a[j]=a[j+1];
            a[j+1]=temp;
            c++;
        }

        if(c>b)
        {
            d++;
            break;
        }
    }
    if(d==0)
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	cin>>t;
	while(t--){
		code();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
	int n;
    cin>>n;

    vll a(n);

    fp(i,0,n)
    cin>>a[i];

    int c=0;
    int b=n*(n-1)/2-1;

    fp(i,0,n)
    {
        if(a[i]>a[i+1])
        c+=n-i-1;
    }

    if(b>=c)
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	cin>>t;
	while(t--){
		code();
	}
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
int c;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ((size * (size - 1)) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c <= 0)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c--;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[500001];
unsigned int c;
unsigned int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		c1 = 0;
		int size;
		cin >> size;
		sum += size;
		if (sum > 100000)
			break;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = (size / 2) * (size - 1) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
long long c;
long long c1=0;

void quickSort(int i, int j);
int main()
{

	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int* arr = new int[size];

		c = double(size) * (size - 1);
		c = c / 2 - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	cin.tie(NULL); cout.tie(NULL);
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		//int temp;
		int chk=0;
		int* arr = new int[size];
		int count = (size * (size - 1) / 2) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
		{
			cin >> arr[i];
		}
		for (int i = 0; i < size-1; i++)
		{
				if (arr[i] > arr[i+1])
				{
					chk++;
				}
		}
		if (chk == size-1)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
int c = 0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		int* arr = new int[size];
		c = (size * (size - 1) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c <= 0)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c--;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
unsigned int c;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ((size * (size - 1)) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c <= 0)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c--;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
long c;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ((size * (size - 1)) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c <= 0)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c--;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int* arr = new int[size];
		int count = (size * (size - 1) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
		{
			cin >> arr[i];
		}
		for (int i = 1; i < size; i++)
		{
			for(int j = 0;j<i;j++)
			{
			if (arr[i] < arr[j])
			{
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
				count--;
			}
			}
		}
		if (count <= 0)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		//int temp;
		int chk=0;
		int* arr = new int[size];
		int count = (size * (size - 1) / 2) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
		{
			cin >> arr[i];
		}
		for (int i = 0; i < size-1; i++)
		{
				if (arr[i] > arr[i+1])
				{
					chk++;
				}
		}
		if (chk == size-1)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
unsigned int c;
unsigned int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = (size / 2) * (size - 1) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
int c;
int c1=0;

void quickSort(int i, int j);
int main()
{

	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int* arr = new int[size];

		c = size * (size - 1);
		c = c / 2 - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
unsigned int c;
unsigned int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ((size * (size - 1)) / 2) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
unsigned int c;
int c1=0;

void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];

		c = ( (size * (size - 1)) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
long c;
int c1=0;

void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ( (size * (size - 1) ) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 >= c)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
double c;
long long c1=0;

void quickSort(int i, int j);
int main()
{

	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int* arr = new int[size];

		c = size * (size - 1);
		c = c / 2 - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
int c;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = (size * (size - 1) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c <= 0)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c--;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
int c;
int c1=0;

void quickSort(int i, int j);
int main()
{

	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int* arr = new int[size];

		c = double(size) * (size - 1);
		c = c / 2 - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
unsigned int c;
int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ((size * (size - 1)) / 2) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
long long c;
int c1=0;

void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];

		c = ( (size * (size - 1)) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
unsigned int c;
int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ((size * (size - 1)) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 >= c)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[1000001];
unsigned int c;
unsigned int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = (size / 2) * (size - 1) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
int c;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = ((size * (size - 1)) / 2) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c > 0)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c--;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[500001];
unsigned int c;
unsigned int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수
	int sum = 0;
	for (int i = 0; i < n; i++)
	{

		int size;
		cin >> size;
		sum += size;
		if (sum > 100000)
			break;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = (size / 2) * (size - 1) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[10000001];
unsigned long long c;
int c1=0;

void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];

		c = ( (size * (size - 1)) / 2 ) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int quick[500001];
unsigned int c;
unsigned int c1=0;
void quickSort(int i, int j);
int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		int temp;
		int sum = 0;
		long* arr = new long[size];
		c = (size / 2) * (size - 1) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
			cin >> quick[i];
		quickSort(0, size - 1);

		//cout << c;
		if (c1 < c)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

void quickSort(int i, int j)
{
	if (i >= j) return;
	int pivot = quick[(i + j) / 2];
	int left = i;
	int right = j;

	while (left <= right)
	{
		while (quick[left] < pivot) left++;
		while (quick[right] > pivot) right--;
		if (left <= right)
		{
			swap(quick[left], quick[right]);
			left++; right--;
			c1++;
		}
	}
	quickSort(i, right);
	quickSort(left, j);
}


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int n;
	cin >> n;  //반복횟수

	for (int i = 0; i < n; i++)
	{
		int size;
		cin >> size;
		//int temp;
		int chk=0;
		int* arr = new int[size];
		int count = (size * (size - 1) / 2) - 1;
		//cout << count;
		for (int i = 0; i < size; i++)
		{
			cin >> arr[i];
		}
		for (int i = 0; i < size-1; i++)
		{
				if (arr[i] > arr[i+1])
				{
					chk++;
				}
		}
		if (chk == size-1)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}

	return 0;
}


#include <iostream>
using namespace std;

int main()
{
int T;
cin>>T;
int size,ans=0;
int array[50000];
for(int i=0;i<T;i++){
	cin>>size;ans=0;

	for(int j=0;j<size;j++){
	cin>>array[j];
	}

	for(int j=0;j<size;j++){
		for(int k=j+1;k<size;k++)
		if(array[k]<array[j]){
		ans=1;}
		else{ans=0;break;}
		}
	}

	if(ans==0){cout<<"YES"<<endl;}
	else{cout<<"NO"<<endl;}

 }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
int T,n=1,temp=0;
cin>>T;
int size,ans=0;
int array[50000];
for(int i=0;i<T;i++){
cin>>size;
for(int j=0;j<size;j++){
cin>>array[j];
}
//}

//for(int i=0;i<T;i++){
    for(int j=0;j<size;j++){
        for(int k=j+1;k<size;k++){
        if(array[j]>array[k]){
        temp=array[j];
        array[j]=array[k];
        array[k]=temp;
        ans+=1;
        }
        }
    }

//}

//for(int i=0;i<T;i++)

if((2*ans)>(size*size-size-2))
{cout<<"NO"<<endl;}
else{cout<<"YES"<<endl;}
//}
}
    return 0;
}


#include<iostream>
using namespace std;

int main(){
int T,n=1,temp=0;
cin>>T;
int size[T],ans[T]={0};
long long int array[T][50000];
for(int i=0;i<T;i++){
cin>>size[i];
for(int j=0;j<size[i];j++){
cin>>array[i][j];
}
}

for(int i=0;i<T;i++){
    for(int j=0;j<size[i];j++){
        for(int k=j+1;k<size[i];k++){
        if(array[i][j]>array[i][k]){
        temp=array[i][j];
        array[i][j]=array[i][k];
        array[i][k]=temp;
        ans[i]+=1;
        }
        }
    }

}

for(int i=0;i<T;i++)
{
if((2*ans[i])>(size[i]*size[i]-size[i]-2))
{cout<<"NO"<<endl;}
else{cout<<"YES"<<endl;}
}

    return 0;

}


#include <iostream>
using namespace std;

int main()
{
int T;
cin>>T;
int size,ans=0;
int array[50000];
for(int i=0;i<T;i++){
	cin>>size;ans=0;

	for(int j=0;j<size;j++){
	cin>>array[j];
	}

	for(int j=0;j<size;j++){
		for(int k=j+1;k<size;k++){
		if(array[k]<array[j]){
		ans=1;}
		else{ans=0;break;}
		}
	}

	if(ans==0){cout<<"YES"<<endl;}
	else{cout<<"NO"<<endl;}

 }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
int T;
cin>>T;
int size,ans=0;
int array[50000];
for(int i=0;i<T;i++){
	cin>>size;ans=0;

	for(int j=0;j<size;j++){
	cin>>array[j];
	}

	for(int j=0;j<size;j++){
			for(int k=j+1;k<size;k++){
		    if(array[k]<array[j]){
		        ans+=1;
		    }
			else{break;}
		}
	}

	if(ans==size-1){cout<<"NO"<<endl;}
	else{cout<<"YES"<<endl;}

 }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
int T;
cin>>T;
int size,flag=0;
int array[50000];
for(int i=0;i<T;i++){
	cin>>size;flag=0;

	for(int j=0;j<size;j++){
	cin>>array[j];
	}

	for(int j=0;j<size;j++){
		if(array[j+1]<array[j]){flag+=1;}
	}

	if(ans==size-1){cout<<"NO"<<endl;}
	else{cout<<"YES"<<endl;}

 }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
int T,n=1,temp=0;
cin>>T;
int size,ans=0;
int array[50000];
	for(int i=0;i<T;i++){
	cin>>size;ans=0;
	for(int j=0;j<size;j++){
	cin>>array[j];
	}

        for(int j=0;j<size;j++)
	{
        for(int k=j+1;k<size;k++)
	{
        if(array[j]<=array[k]){
	ans=0;
	break;
        }
	else{ans=1;break;}
        }
   	}

if(ans==0)
{cout<<"YES"<<endl;}
else{cout<<"NO"<<endl;}

}
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
int T;
cin>>T;
int size,ans=0;
int array[50000];
for(int i=0;i<T;i++){
	cin>>size;ans=0;

	for(int j=0;j<size;j++){
	cin>>array[j];
	}

	for(int j=0;j<size;j++){
			for(int k=j+1;k<size;k++){
		    if(array[k]<array[j]){
		        ans+=1;
		    }
		}
	}

	if(ans==size-1){cout<<"NO"<<endl;}
	else{cout<<"YES"<<endl;}

 }
    return 0;
}


/*
 * Author       : YangDavid
 * Created Time : 2020.11.27 20:36:15
 */

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 2020200;
int n, m;
vector<pii> G[MAXN];

struct Tree {
#define lson (o << 1)
#define rson (o << 1|1)
#define mid ((l+r)>>1)
	int flp[MAXN], mx0[MAXN], mx1[MAXN], dfn[MAXN], dfsClock = 0, sz[MAXN], fath[MAXN];
	int omx[2][MAXN];
	void pushup(int o) { mx0[o] = max(mx0[lson], mx0[rson]), mx1[o] = max(mx1[lson], mx1[rson]); }
	void doit(int o) { swap(mx0[o], mx1[o]), flp[o] ^= 1; }
	void pushdown(int o) {
		if(flp[o]) {
			doit(lson);
			doit(rson);
			flp[o] = false;
		}
	}
	void build(int o, int l, int r) {
		flp[o] = mx0[o] = mx1[o] = 0;
		if(l == r) { mx0[o] = omx[0][l], mx1[o] = omx[1][l]; return; }
		build(lson, l, mid);
		build(rson, mid+1, r);
		pushup(o);
	}
	void modify(int o, int l, int r, int ql, int qr) {
		if(ql <= l && r <= qr) { doit(o); return; }
		pushdown(o);
		if(ql <= mid) modify(lson, l, mid, ql, qr);
		if(qr > mid) modify(rson, mid+1, r, ql, qr);
		pushup(o);
	}
	void dfs(int v, int fa, int dep, int col) {
		dfn[v] = ++dfsClock, sz[v] = 1, fath[v] = fa;
		omx[col][dfsClock] = dep;
		for(auto [u, w] : G[v]) if(u != fa) {
			dfs(u, v, dep+1, col^w);
			sz[v] += sz[u];
			omx[0][dfsClock] = max(omx[0][dfn[u]], omx[0][dfsClock]);
			omx[1][dfsClock] = max(omx[1][dfn[u]], omx[1][dfsClock]);
		}
	}
	void show(int o, int l, int r) {
		if(l == r) {
			return;
		}
		pushdown(o);
		show(lson, l, mid);
		show(rson, mid+1, r);
	}
	void view() {
		show(1, 1, n);
	}
} t1, t2;

int far = -1, fardis = -1, vx[MAXN], vy[MAXN];
void dfs0(int o, int fa, int dep) {
	if(dep > fardis) far = o, fardis = dep;
	for(auto [g, w] : G[o]) if(g != fa)
		dfs0(g, o, dep+1);
}
int main() {
	scanf("%d", &n);
	rep(i, n-1) {
		static int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		G[u].emplace_back(v, w);
		G[v].emplace_back(u, w);
		vx[i] = u, vy[i] = v;
	}
	dfs0(1, -1, 1);
	int r1 = far;
	dfs0(r1, -1, 1);
	int r2 = far;
	t1.dfs(r1, -1, 1, 0);
	t1.build(1, 1, n);
	t2.dfs(r2, -1, 1, 0);
	t2.build(1, 1, n);

	scanf("%d", &m);
	rep(i, m) {
		static int x;
		scanf("%d", &x);
		int u = vx[x], v = vy[x];
		int c1 = t1.fath[u] == v ? u : v;
		int c2 = t2.fath[u] == v ? u : v;
		t1.modify(1, 1, n, t1.dfn[c1], t1.dfn[c1]+t1.sz[c1]-1);
		t2.modify(1, 1, n, t2.dfn[c2], t2.dfn[c2]+t2.sz[c2]-1);
		printf("%d\n", max(t1.mx0[1], t2.mx0[1])-1);
		t1.view();
		t2.view();
	}
    return 0;
}


// Problem link: https://codeforces.com/contest/1434/problem/D

#include <bits/stdc++.h>

using namespace std;

typedef int int2;
#define int long long

#define pi pair<int, int>
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define f first
#define s second

struct node {
    int d0, d1, par;
    node(): d0(-1), d1(-1), par(par) {}
    node(int depth, int par)  {
        this->par = par;
        if (par == 0) {
            d0 = depth;
            d1 = -1;
        } else {
            d0 = -1;
            d1 = depth;
        }
    }
    node(int d0, int d1, int par): d0(d0), d1(d1), par(par) {}

    bool operator <(const node &other) const {
        if (par == other.par) {
            if (d0 == other.d0) {
                return d1 > other.d1; // prioritize large d1
            }
            return d0 > other.d0; // prioritize large d0
        }
        return par == 0; //prioritize par == 0
    }

    friend ostream& operator<<(ostream &os, node a) {
        return os << '(' << a.d0 << ", " << a.d1 << ", " << a.par << ')';
    }

    friend node operator+(const node& a, const node& b) {
        node c;
        // find opt (d0, d1, par) combination. prioritize par then d0 then d1
        c.par = min(a.par, b.par);
        c.d0 = max(a.d0, b.d0);
        c.d1 = max(a.d1, b.d1);
        return c;
    }

    node flip() {
        node c;
        c.d0 = d1;
        c.d1 = d0;
        c.par = (par ^ 1);
        return c;
    }

    node operator +=(const node&b) {
        (*this) = (*this) + b;
        return *this;
    }
};

const int maxn = 5e5 + 5;
int tin[maxn], tout[maxn];
int depth[maxn];
int timer = 0;
int max_depth = -1;
int farthest;

int n, m;

node t[4 * maxn];
int lflip[4 * maxn];
node flattened[maxn];

vector<pi> adj[maxn];

void pull(int v) {
    t[v] = t[2 * v] + t[2 * v + 1];
}

void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = flattened[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm + 1, tr);
        pull(v);
    }
}

void push(int v) {
    lflip[2 * v] ^= lflip[v];
    lflip[2 * v + 1] ^= lflip[v];
    if (lflip[v]) {
        t[2 * v] = t[2 * v].flip();
        t[2 * v + 1] = t[2 * v + 1].flip();
    }
    lflip[v] = 0;
}

int add_tol = 10;
int tot_add = 0;
void add(int v, int tl, int tr, int l, int r, int delta) {
    // tot_add++;
    // cout << "v: " << v << ", tl: " << tl << ", tr: " << tr << ", l: " << l << ", r: " << r << "\n";
    // if (tot_add > add_tol) return;
    if (l > r) return;
    if (l == tl and r == tr) {
        if (delta == 1) {
            t[v] = t[v].flip();
            lflip[v] ^= delta;
        }
        return;
    }
    int tm = (tl + tr) / 2;
    push(v);
    add(2 * v, tl, tm, l, min(tm, r), delta);
    add(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, delta);
    pull(v);
}

node query(int v, int tl, int tr, int l, int r) {
    if (l > r) return node(-1, -1, 1);
    if (l == tl and r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    push(v);
    return query(2 * v, tl, tm, l, min(tm, r)) + query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}

void dfs(int u, int p, int par) {
    tin[u] = ++timer;
    if (depth[u] > max_depth) {
        max_depth = depth[u];
        farthest = u;
    }
    flattened[timer] = node(depth[u], par);
    // cout << "time: " << timer << ", u: " << u << ", depth: " << depth[u] << ", par: " << par << "\n";
    for (pi nei: adj[u]) {
        int v = nei.f;
        if (v != p) {
            depth[v] = depth[u] + 1;
            int type = nei.s;
            if (type == 0) {
                dfs(v, u, par);
            } else {
                dfs(v, u, 1 - par);
            }
        }
    }
    tout[u] = timer;
}

void reset() {
    timer = 0;
    max_depth = -1;
    for (int i = 0; i < maxn; i++) {
        depth[i] = 0;
    }
    for (int i = 0; i < 4 * maxn; i++) {
        lflip[i] = 0;
    }
}

void search(int u) {
    reset();
    dfs(u, u, 0);
}

void build() {
    build(1, 1, n);
}

void toggle(pi p) {
    int u = p.f;
    int v = p.s;
    if (depth[u] < depth[v]) {
        swap(u, v);
    }
    // cout << "u: " << u << ", tin: " << tin[u] << ", tout: " << tout[u] << "\n";
    //u will be deeper than v
    add(1, 1, n, tin[u], tout[u], 1);
}

int query_longest() {
    node opt = query(1, 1, n, 1, n);
    // cout << "opt depth: " << opt.depth << "\n";
    // cout << "opt par: " << opt.par << "\n";
    return opt.d0;
}

int2 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    vector<pi> edges(n);
    for (int i = 1; i < n; i++) {
        int u, v, type;
        cin >> u >> v >> type;
        adj[u].eb(v, type);
        adj[v].eb(u, type);
        edges[i] = mp(u, v);
    }
    cin >> m;
    vector<int> queries(m);
    for (int i = 0; i < m; i++) {
        cin >> queries[i];
    }
    search(1);
    vector<int> ans(m);
    int v1 = farthest;
    // cout << "v1: " << v1 << "\n";
    search(v1);
    // cout << "beginning build\n";
    build();
    // cout << "ended build\n";
    // vector<int> reachable;
    // for (int i = 1; i <= n; i++) {
    //         node p = query(1, 1, n, tin[i], tin[i]);
    //         // cout << "i: " << i << ", depth: " << p.depth << ", par: " << p.par << "\n";
    //         if (p.par == 0) {
    //             reachable.pb(i);
    //         }
    // }
    // cout << "reachable vertices (initial):\n";
    //     for (int i: reachable) {
    //         cout << i << " ";
    //     }
    // cout << "\n";
    for (int qq = 0; qq < m; qq++) {
        // cout << "\nquery: " << qq << "\n";
        int e_idx = queries[qq];
        pi edge = edges[e_idx];
        toggle(edge);
        int longest_path = query_longest();
        // cout << "longest path: " << longest_path << "\n";
        ans[qq] = longest_path;
    }
    int v2 = farthest;
    // cout << "v2: " << v2 << "\n";
    search(v2);
    build();
    for (int qq = 0; qq < m; qq++) {
        int e_idx = queries[qq];
        toggle(edges[e_idx]);
        int longest_path = query_longest();
        // cout << "query: " << qq << ", longest path: " << longest_path << "\n";
        ans[qq] = max(ans[qq], longest_path);
    }
    for (int i = 0; i < m; i++) {
        cout << ans[i] << "\n";
    }
}


// Problem link: https://codeforces.com/contest/1434/problem/D

#include <bits/stdc++.h>

using namespace std;

typedef int int2;
#define int long long

#define pi pair<int, int>
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define f first
#define s second

struct node {
    int depth, par;
    node(): depth(0), par(0) {}
    node(int depth, int par): depth(depth), par(par) {}

    friend ostream& operator<<(ostream &os, node a) {
        return os << '(' << a.depth << ", " << a.par << ')';
    }

    friend node operator+(const node& a, const node& b) {
        // cout << "a: " << a << "\n";
        // cout << "b: " << b << "\n";
        node c;
        // prioritize par = 0, then prioritize larger depth

        if (a.par == b.par) {
            c = a;
            c.depth = max(a.depth, b.depth);
        } else {
            if (a.par == 0) {
                c = a;
            } else {
                c = b;
            }
        }
        // cout << "c: " << c << "\n";
        return c;
    }

    friend node operator +(const node&a, int b) {
        node c = a;
        c.par ^= b;
        return c;
    }

    node operator +=(const node&b) {
        (*this) = (*this) + b;
        return *this;
    }

    node operator +=(int b) {
        (*this) = (*this) + b;
        return *this;
    }
};

const int maxn = 5e5 + 5;
int tin[maxn], tout[maxn];
int depth[maxn];
int timer = 0;
int max_depth = -1;
int farthest;

int n, m;

node t[4 * maxn];
node flattened[maxn];
int ladd[maxn];

vector<pi> adj[maxn];

void pull(int v) {
    t[v] = t[2 * v] + t[2 * v + 1];
}

void build(int v, int tl, int tr) {
    ladd[v] = 0;
    if (tl == tr) {
        t[v] = flattened[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm + 1, tr);
        pull(v);
    }
}

void push(int v) {
    ladd[2 * v] ^= ladd[v];
    ladd[2 * v + 1] ^= ladd[v];
    t[2 * v] = t[2 * v] + ladd[v];
    t[2 * v + 1] = t[2 * v + 1] + ladd[v];
    ladd[v] = 0;
}

int add_tol = 10;
int tot_add = 0;
void add(int v, int tl, int tr, int l, int r, int delta) {
    // tot_add++;
    // cout << "v: " << v << ", tl: " << tl << ", tr: " << tr << ", l: " << l << ", r: " << r << "\n";
    // if (tot_add > add_tol) return;
    if (l > r) return;
    if (l == tl and r == tr) {
        t[v] += delta;
        ladd[v] ^= delta;
        return;
    }
    int tm = (tl + tr) / 2;
    push(v);
    add(2 * v, tl, tm, l, min(tm, r), delta);
    add(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, delta);
    pull(v);
}

node query(int v, int tl, int tr, int l, int r) {
    if (l > r) return node(1, -1);
    if (l == tl and r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    push(v);
    return query(2 * v, tl, tm, l, min(tm, r)) + query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}

void dfs(int u, int p, int par) {
    tin[u] = ++timer;
    if (depth[u] > max_depth) {
        max_depth = depth[u];
        farthest = u;
    }
    flattened[timer] = node(depth[u], par);
    // cout << "time: " << timer << ", depth: " << depth[u] << ", par: " << par << "\n";
    for (pi nei: adj[u]) {
        int v = nei.f;
        if (v != p) {
            depth[v] = depth[u] + 1;
            int type = nei.s;
            if (type == 0) {
                dfs(v, u, par);
            } else {
                dfs(v, u, 1 - par);
            }
        }
    }
    tout[u] = timer;
}

void reset() {
    timer = 0;
    max_depth = -1;
    for (int i = 1; i <= n; i++) {
        depth[i] = 0;
    }
}

void search(int u) {
    reset();
    dfs(u, u, 0);
}

void build() {
    build(1, 1, n);
}

void toggle(pi p) {
    int u = p.f;
    int v = p.s;
    if (depth[u] < depth[v]) {
        swap(u, v);
    }
    // cout << "u: " << u << ", tin: " << tin[u] << ", tout: " << tout[u] << "\n";
    //u will be deeper than v
    add(1, 1, n, tin[u], tout[u], 1);
}

int query_longest() {
    node opt = query(1, 1, n, 1, n);
    // cout << "opt depth: " << opt.depth << "\n";
    // cout << "opt par: " << opt.par << "\n";
    return opt.depth;
}

int2 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    vector<pi> edges(n);
    for (int i = 1; i < n; i++) {
        int u, v, type;
        cin >> u >> v >> type;
        adj[u].eb(v, type);
        adj[v].eb(u, type);
        edges[i] = mp(u, v);
    }
    cin >> m;
    vector<int> queries(m);
    for (int i = 0; i < m; i++) {
        cin >> queries[i];
    }
    search(1);
    vector<int> ans(m);
    int v1 = farthest;
    // cout << "v1: " << v1 << "\n";
    search(v1);
    build();
    int longest_path = query_longest();
    // cout << "longest_path: " << longest_path << "\n";
    for (int qq = 0; qq < m; qq++) {
        // cout << "query: " << qq << "\n";
        int e_idx = queries[qq];
        // cout << "e_idx: " << e_idx << "\n";
        pi edge = edges[e_idx];
        // cout << "edge: " << edge.f << " " << edge.s << "\n";
        toggle(edge);
        int longest_path = query_longest();
        // cout << "longest path: " << longest_path << "\n";
        ans[qq] = longest_path;
    }
    int v2 = farthest;
    // cout << "v2: " << v2 << "\n";
    search(v2);
    build();
    for (int qq = 0; qq < m; qq++) {
        int e_idx = queries[qq];
        toggle(edges[e_idx]);
        int longest_path = query_longest();
        // cout << "query: " << qq << ", longest path: " << longest_path << "\n";
        ans[qq] = max(ans[qq], longest_path);
    }
    for (int i = 0; i < m; i++) {
        cout << ans[i] << "\n";
    }
}


// Problem link: https://codeforces.com/contest/1434/problem/D

#include <bits/stdc++.h>

using namespace std;

typedef int int2;
#define int long long

#define pi pair<int, int>
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define f first
#define s second

struct node {
    int d0, d1, par;
    node(): d0(-1), d1(-1), par(par) {}
    node(int depth, int par)  {
        this->par = par;
        if (par == 0) {
            d0 = depth;
            d1 = -1;
        } else {
            d0 = -1;
            d1 = depth;
        }
    }
    node(int d0, int d1, int par): d0(d0), d1(d1), par(par) {}

    friend ostream& operator<<(ostream &os, node a) {
        return os << '(' << a.d0 << ", " << a.d1 << ", " << a.par << ')';
    }

    friend node operator+(const node& a, const node& b) {
        node c;
        c.par = min(a.par, b.par);
        c.d0 = max(a.d0, b.d0);
        c.d1 = max(a.d1, b.d1);
        return c;
    }

    node flip() {
        node c;
        c.d0 = d1;
        c.d1 = d0;
        c.par = (par ^ 1);
        return c;
    }

    node operator +=(const node&b) {
        (*this) = (*this) + b;
        return *this;
    }
};

const int maxn = 5e5 + 5;
int tin[maxn], tout[maxn];
int depth[maxn];
int timer = 0;
int max_depth = -1;
int farthest;

int n, m;

node t[4 * maxn];
int lflip[4 * maxn];
node flattened[maxn];

vector<pi> adj[maxn];

void pull(int v) {
    t[v] = t[2 * v] + t[2 * v + 1];
}

void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = flattened[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(2 * v, tl, tm);
        build(2 * v + 1, tm + 1, tr);
        pull(v);
    }
}

void push(int v) {
    lflip[2 * v] ^= lflip[v];
    lflip[2 * v + 1] ^= lflip[v];
    if (lflip[v]) {
        t[2 * v] = t[2 * v].flip();
        t[2 * v + 1] = t[2 * v + 1].flip();
    }
    lflip[v] = 0;
}

void add(int v, int tl, int tr, int l, int r, int delta) {
    if (l > r) return;
    if (l == tl and r == tr) {
        if (delta == 1) {
            t[v] = t[v].flip();
            lflip[v] ^= delta;
        }
        return;
    }
    int tm = (tl + tr) / 2;
    push(v);
    add(2 * v, tl, tm, l, min(tm, r), delta);
    add(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, delta);
    pull(v);
}

node query(int v, int tl, int tr, int l, int r) {
    if (l > r) return node(-1, -1, 1);
    if (l == tl and r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    push(v);
    return query(2 * v, tl, tm, l, min(tm, r)) + query(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}

void dfs(int u, int p, int par) {
    tin[u] = ++timer;
    if (depth[u] > max_depth) {
        max_depth = depth[u];
        farthest = u;
    }
    flattened[timer] = node(depth[u], par);
    for (pi nei: adj[u]) {
        int v = nei.f;
        if (v != p) {
            depth[v] = depth[u] + 1;
            int type = nei.s;
            if (type == 0) {
                dfs(v, u, par);
            } else {
                dfs(v, u, 1 - par);
            }
        }
    }
    tout[u] = timer;
}

void reset() {
    timer = 0;
    max_depth = -1;
    for (int i = 0; i < maxn; i++) {
        depth[i] = 0;
    }
    for (int i = 0; i < 4 * maxn; i++) {
        lflip[i] = 0;
    }
}

void search(int u) {
    reset();
    dfs(u, u, 0);
}

void build() {
    build(1, 1, n);
}

void toggle(pi p) {
    int u = p.f;
    int v = p.s;
    if (depth[u] < depth[v]) {
        swap(u, v);
    }
    //u will be deeper than v
    add(1, 1, n, tin[u], tout[u], 1);
}

int query_longest() {
    node opt = query(1, 1, n, 1, n);
    return opt.d0;
}

int2 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    vector<pi> edges(n);
    for (int i = 1; i < n; i++) {
        int u, v, type;
        cin >> u >> v >> type;
        adj[u].eb(v, type);
        adj[v].eb(u, type);
        edges[i] = mp(u, v);
    }
    cin >> m;
    vector<int> queries(m);
    for (int i = 0; i < m; i++) {
        cin >> queries[i];
    }
    search(1);
    vector<int> ans(m);
    int v1 = farthest;
    search(v1);
    build();
    for (int qq = 0; qq < m; qq++) {
        int e_idx = queries[qq];
        pi edge = edges[e_idx];
        toggle(edge);
        int longest_path = query_longest();
        ans[qq] = longest_path;
    }
    int v2 = farthest;
    search(v2);
    build();
    for (int qq = 0; qq < m; qq++) {
        int e_idx = queries[qq];
        toggle(edges[e_idx]);
        int longest_path = query_longest();
        ans[qq] = max(ans[qq], longest_path);
    }
    for (int i = 0; i < m; i++) {
        cout << ans[i] << "\n";
    }
}


//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
int n,m;
vector<pair<int,int>> adj[500001];
pair<int,int> ma={-1,-1};
void dfs(llo no,llo par=-1,llo lev=0){
	if(lev>ma.a){
		ma={lev,no};
	}
	for(auto j:adj[no]){
		if(j.a!=par){
			dfs(j.a,no,lev+1);
		}
	}
}
int st[2][500001];
int endd[2][500001];
pair<int,int> val[2][500001];
//int dd[2][500001];
int cur=0;
int co=0;
void dfs2(int no,int par2=-1,int levv=0,int val2=0){
	st[cur][no]=co;
	val[cur][co]={val2,levv};
	co++;

	for(auto j:adj[no]){
		if(j.a!=par2){
			dfs2(j.a,no,levv+1,val2^j.b);
		}
	}
	endd[cur][no]=co-1;
}
int tree[2][500001*4][2];
int lazy[2][500001*4];
void build(int no,int l,int r){
	if(l==r){
		tree[cur][no][val[cur][l].a]=val[cur][l].b;
	//	cout<<l<<"::"<<val[cur][l].a<<",,"<<val[cur][l].b<<endl;
	}
	else{
		int mid=(l+r)/2;
		build(no*2+1,l,mid);
		build(no*2+2,mid+1,r);
		tree[cur][no][0]=max(tree[cur][no*2+1][0],tree[cur][no*2+2][0]);
		tree[cur][no][1]=max(tree[cur][no*2+1][1],tree[cur][no*2+2][1]);
	}
}
void update(int no,int l,int r,int aa,int bb){
	if(lazy[cur][no]){
		lazy[cur][no]=0;
		if(l<r){
			lazy[cur][no*2+1]^=1;
			lazy[cur][no*2+2]^=1;
		}
		swap(tree[cur][no][0],tree[cur][no][1]);
	}
	if(aa>r or bb<l){
		return;
	}
	if(aa<=l and r<=bb){
		swap(tree[cur][no][0],tree[cur][no][1]);
		if(l<r){
			lazy[cur][no*2+1]^=1;
			lazy[cur][no*2+2]^=1;
		}
	}
	else{
		int mid=(l+r)/2;
		update(no*2+1,l,mid,aa,bb);
		update(no*2+2,mid+1,r,aa,bb);
		tree[cur][no][0]=max(tree[cur][no*2+1][0],tree[cur][no*2+2][0]);
		tree[cur][no][1]=max(tree[cur][no*2+1][1],tree[cur][no*2+2][1]);
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	vector<pair<int,int>> ed;
	for(int i=0;i<n-1;i++){
		int aa,bb,cc;
		cin>>aa>>bb>>cc;
		aa--;
		bb--;
		adj[aa].pb({bb,cc});
		adj[bb].pb({aa,cc});
		ed.pb({aa,bb});
	}
	dfs(0);
	int x=ma.b;
	ma={-1,-1};
	dfs(x);
//	cout<<ma.a<<":"<<ma.b<<endl;
	int y=ma.b;
	cur=0;
	dfs2(x);
	build(0,0,n-1);
	co=0;
	cur=1;
	dfs2(y);
	//cout<<x<<":"<<y<<endl;
	build(0,0,n-1);
	cin>>m;
	while(m--){
		int ind;
		cin>>ind;
		ind--;
		pair<int,int> cc={ed[ind].a,ed[ind].b};

		if(st[0][cc.a]>st[0][cc.b]){
			swap(cc.a,cc.b);
		}
	//	cout<<cc.a<<":"<<cc.b<<endl;
	//	cout<<st[0][cc.b]<<",,"<<endd[0][cc.b]<<endl;
		cur=0;
		update(0,0,n-1,st[0][cc.b],endd[0][cc.b]);
		if(st[1][cc.a]>st[1][cc.b]){
			swap(cc.a,cc.b);
		}
		cur=1;
	//	cout<<cc.a<<":"<<cc.b<<endl;
	//	cout<<st[1][cc.b]<<",,"<<endd[1][cc.b]<<endl;
		update(0,0,n-1,st[1][cc.b],endd[1][cc.b]);
		cout<<max(tree[0][0][0],tree[1][0][0])<<endl;

	}

	return 0;
}


//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
int n,m;
vector<pair<int,int>> adj[500001];
pair<int,int> ma={-1,-1};
void dfs(llo no,llo par=-1,llo lev=0){
	if(lev>ma.a){
		ma={lev,no};
	}
	for(auto j:adj[no]){
		if(j.a!=par){
			dfs(j.a,no,lev+1);
		}
	}
}
int st[2][500001];
int endd[2][500001];
pair<int,int> val[2][500001];
//int dd[2][500001];
int cur=0;
int co=0;
void dfs2(int no,int par2=-1,int levv=0,int val2=0){
	st[cur][no]=co;
	val[cur][co]={val2,levv};
	co++;

	for(auto j:adj[no]){
		if(j.a!=par2){
			dfs2(j.a,no,levv+1,val2^j.b);
		}
	}
	endd[cur][no]=co-1;
}
int tree[2][500001*4][2];
int lazy[2][500001*4];
void build(int no,int l,int r){
	if(l==r){
		tree[cur][no][val[cur][l].a]=val[cur][l].b;
	//	cout<<l<<"::"<<val[cur][l].a<<",,"<<val[cur][l].b<<endl;
	}
	else{
		int mid=(l+r)/2;
		build(no*2+1,l,mid);
		build(no*2+2,mid+1,r);
		tree[cur][no][0]=max(tree[cur][no*2+1][0],tree[cur][no*2+2][0]);
		tree[cur][no][1]=max(tree[cur][no*2+1][1],tree[cur][no*2+2][1]);
	}
}
void update(int no,int l,int r,int aa,int bb){
	if(lazy[cur][no]){
		lazy[cur][no]=0;
		swap(tree[cur][no][0],tree[cur][no][1]);
	}
	if(aa>r or bb<l){
		return;
	}
	if(aa<=l and r<=bb){
		swap(tree[cur][no][0],tree[cur][no][1]);
		if(l<r){
			lazy[cur][no*2+1]^=1;
			lazy[cur][no*2+2]^=1;
		}
	}
	else{
		int mid=(l+r)/2;
		update(no*2+1,l,mid,aa,bb);
		update(no*2+2,mid+1,r,aa,bb);
		tree[cur][no][0]=max(tree[cur][no*2+1][0],tree[cur][no*2+2][0]);
		tree[cur][no][1]=max(tree[cur][no*2+1][1],tree[cur][no*2+2][1]);
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	vector<pair<int,int>> ed;
	for(int i=0;i<n-1;i++){
		int aa,bb,cc;
		cin>>aa>>bb>>cc;
		aa--;
		bb--;
		adj[aa].pb({bb,cc});
		adj[bb].pb({aa,cc});
		ed.pb({aa,bb});
	}
	dfs(0);
	int x=ma.b;
	ma={-1,-1};
	dfs(x);
//	cout<<ma.a<<":"<<ma.b<<endl;
	int y=ma.b;
	cur=0;
	dfs2(x);
	build(0,0,n-1);
	co=0;
	cur=1;
	dfs2(y);
	//cout<<x<<":"<<y<<endl;
	build(0,0,n-1);
	cin>>m;
	while(m--){
		int ind;
		cin>>ind;
		ind--;
		pair<int,int> cc={ed[ind].a,ed[ind].b};

		if(st[0][cc.a]>st[0][cc.b]){
			swap(cc.a,cc.b);
		}
	//	cout<<cc.a<<":"<<cc.b<<endl;
	//	cout<<st[0][cc.b]<<",,"<<endd[0][cc.b]<<endl;
		cur=0;
		update(0,0,n-1,st[0][cc.b],endd[0][cc.b]);
		if(st[1][cc.a]>st[1][cc.b]){
			swap(cc.a,cc.b);
		}
		cur=1;
	//	cout<<cc.a<<":"<<cc.b<<endl;
	//	cout<<st[1][cc.b]<<",,"<<endd[1][cc.b]<<endl;
		update(0,0,n-1,st[1][cc.b],endd[1][cc.b]);
		cout<<max(tree[0][0][0],tree[1][0][0])<<endl;

	}

	return 0;
}


// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")

using namespace std;

// #include <ext/pb_ds/assoc_container.hpp> // order_of_key
// #include <ext/pb_ds/tree_policy.hpp> // find by order
// using namespace __gnu_pbds;
// #define ordered_set tree< int , null_type,less< int >, rb_tree_tag,tree_order_statistics_node_update>

// const ll MAX_LL = 0xFFFFFFFFFFFFFFFF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define losuj(a, b) uniform_int_distribution<ll>(a, b)(rng)

typedef long double             ld;
typedef long long               ll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ll>              vll;
typedef vector<vll>             vvll;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vii>             vvii;
#define pll pair<long long, long long>
#define ull unsigned long long

#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template<class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<class T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return abs(a); }

const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const ll T = 1<<19;
const ld inf = 1e18;

const ld eps = 1e-14;

ll n, m, k;

vvii G(N);
int najd = 1, dist, cnt_pre;
vi preorder(N), glebokosc(N), rev_pre(N), kolor(N);
vii przedzial_na_drzewie(N);

struct Tree{
    vi merge(vi & a, vi & b){
        return {max(a[0], b[0]), max(a[1], b[1])};
    }

    vvi tree;
    vi lazy;

    Tree(){
        tree = vvi(T * 2, vi(2, -1));
        lazy = vi(T * 2, false);

        for(int i = 1; i <= cnt_pre; i++)
            tree[i + T][kolor[rev_pre[i]]] = glebokosc[rev_pre[i]];

        for(int i = T - 1; i >= 0; i--)
            tree[i] = merge(tree[i * 2], tree[i * 2 + 1]);
    };

    void push(int v){
        if(lazy[v]){
            swap(tree[v][0], tree[v][1]);

            if(v < T){
                lazy[v * 2] ^= 1;
                lazy[v * 2 + 1] ^= 1;
            }

            lazy[v] = 0;
        }
    }

    void flip(int a, int b, int u = 1, int lo = 0, int hi = T - 1){
        push(u);

        if(hi < a or b < lo)
            return;

        if(a <= lo and hi <= b){
            lazy[u] ^= 1;
            push(u);
            return;
        }

        int mid = (lo + hi) / 2;
        flip(a, b, u * 2, lo, mid);
        flip(a, b, u * 2 + 1, mid + 1, hi);
        tree[u] = merge(tree[u * 2], tree[u * 2 + 1]);
    }

    void flip(ii kraw){
        int v = (glebokosc[kraw.f] < glebokosc[kraw.s] ? kraw.s : kraw.f);

        flip(przedzial_na_drzewie[v].f, przedzial_na_drzewie[v].s);
    }

    int ask(){
        return tree[1][0];
    }
};

void dfs2(int s, int p = -1, int gl = 0, int kol = 0){
    if(p == -1)
        cnt_pre = 0;

    preorder[s] = ++cnt_pre;
    rev_pre[cnt_pre] = s;
    glebokosc[s] = gl;
    kolor[s] = kol;

    for(auto & u : G[s])
        if(u.f != p)
            dfs2(u.f, s, gl + 1, kol ^ u.s);

    przedzial_na_drzewie[s] = {preorder[s], cnt_pre};
}

vi solve(vii & kraw, vi zap, int root){
    dfs2(root);
    Tree tree;

    vi ret;
    for(auto & u : zap){
        tree.flip(kraw[u]);
        ret.pb(tree.ask());
    }

    return ret;
}

void dfs(int s = najd, int gl = 0, int przodek = -1){
    if(gl == 0)
        dist = -1;

    if(gl > dist){
        dist = gl;
        najd = s;
    }

    for(auto & u : G[s])
        if(u.f != przodek)
            dfs(u.f, gl + 1, s);
}

void solve(){
    cin >> n;

    vector<pair<int, int> > kraw(n, {-1, -1});
    for(int i = 1; i < n; i++){
        int a, b, c;
        cin >> a >> b >> c;

        G[a].pb({b, c});
        G[b].pb({a, c});

        kraw[i] = {a, b};
    }

    vector<int> konce;
    dfs();
    konce.pb(najd);
    dfs();
    konce.pb(najd);

    cin >> m;
    vi zapytania(m);
    cin >> zapytania;

    vi A = solve(kraw, zapytania, konce[0]);
    vi B = solve(kraw, zapytania, konce[1]);

    for(int i = 0; i < m; i++)
        cout << max(A[i], B[i]) << '\n';
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;
    // cin >> t;

    while(t--)
        solve();
}


// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")

using namespace std;

// #include <ext/pb_ds/assoc_container.hpp> // order_of_key
// #include <ext/pb_ds/tree_policy.hpp> // find by order
// using namespace __gnu_pbds;
// #define ordered_set tree< int , null_type,less< int >, rb_tree_tag,tree_order_statistics_node_update>

// const ll MAX_LL = 0xFFFFFFFFFFFFFFFF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define losuj(a, b) uniform_int_distribution<ll>(a, b)(rng)

typedef long double             ld;
typedef long long               ll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ll>              vll;
typedef vector<vll>             vvll;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vii>             vvii;
#define pll pair<long long, long long>
#define ull unsigned long long

#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define f                       first
#define s                       second

template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}

template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template<class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<class T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return abs(a); }

const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const ll T = 1<<20;
const ld inf = 1e18;

const ld eps = 1e-14;

ll n, m, k;

vvii G(N);
int najd = 1, dist, cnt_pre;
vi preorder(N), glebokosc(N), rev_pre(N), kolor(N);
vii przedzial_na_drzewie(N);

struct Tree{
    vi merge(vi & a, vi & b){
        return {max(a[0], b[0]), max(a[1], b[1])};
    }

    vvi tree;
    vi lazy;

    Tree(){
        tree = vvi(T * 2, vi(2, -1));
        lazy = vi(T * 2, false);

        for(int i = 1; i <= cnt_pre; i++)
            tree[i + T][kolor[rev_pre[i]]] = glebokosc[rev_pre[i]];

        for(int i = T - 1; i >= 0; i--)
            tree[i] = merge(tree[i * 2], tree[i * 2 + 1]);
    };

    void push(int v){
        if(lazy[v]){
            swap(tree[v][0], tree[v][1]);

            if(v < T){
                lazy[v * 2] ^= 1;
                lazy[v * 2 + 1] ^= 1;
            }

            lazy[v] = 0;
        }
    }

    void flip(int a, int b, int u = 1, int lo = 0, int hi = T - 1){
        push(u);

        if(hi < a or b < lo)
            return;

        if(a <= lo and hi <= b){
            lazy[u] ^= 1;
            push(u);
            return;
        }

        int mid = (lo + hi) / 2;
        flip(a, b, u * 2, lo, mid);
        flip(a, b, u * 2 + 1, mid + 1, hi);
        tree[u] = merge(tree[u * 2], tree[u * 2 + 1]);
    }

    void flip(ii kraw){
        int v = (glebokosc[kraw.f] < glebokosc[kraw.s] ? kraw.s : kraw.f);

        flip(przedzial_na_drzewie[v].f, przedzial_na_drzewie[v].s);
    }

    int ask(){
        return tree[1][0];
    }
};

void dfs2(int s, int p = -1, int gl = 0, int kol = 0){
    if(p == -1)
        cnt_pre = 0;

    preorder[s] = ++cnt_pre;
    rev_pre[cnt_pre] = s;
    glebokosc[s] = gl;
    kolor[s] = kol;

    for(auto & u : G[s])
        if(u.f != p)
            dfs2(u.f, s, gl + 1, kol ^ u.s);

    przedzial_na_drzewie[s] = {preorder[s], cnt_pre};
}

vi solve(vii & kraw, vi zap, int root){
    dfs2(root);
    Tree tree;

    vi ret;
    for(auto & u : zap){
        tree.flip(kraw[u]);
        ret.pb(tree.ask());
    }

    return ret;
}

void dfs(int s = najd, int gl = 0, int przodek = -1){
    if(gl == 0)
        dist = -1;

    if(gl > dist){
        dist = gl;
        najd = s;
    }

    for(auto & u : G[s])
        if(u.f != przodek)
            dfs(u.f, gl + 1, s);
}

void solve(){
    cin >> n;

    vector<pair<int, int> > kraw(n, {-1, -1});
    for(int i = 1; i < n; i++){
        int a, b, c;
        cin >> a >> b >> c;

        G[a].pb({b, c});
        G[b].pb({a, c});

        kraw[i] = {a, b};
    }

    vector<int> konce;
    dfs();
    konce.pb(najd);
    dfs();
    konce.pb(najd);

    cin >> m;
    vi zapytania(m);
    cin >> zapytania;

    vi A = solve(kraw, zapytania, konce[0]);
    vi B = solve(kraw, zapytania, konce[1]);

    for(int i = 0; i < m; i++)
        cout << max(A[i], B[i]) << '\n';
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t = 1;
    // cin >> t;

    while(t--)
        solve();
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int maxn = 512345;

int n, q, clk;
int in[maxn], out[maxn], dep[maxn], stone[maxn], ver[maxn], seq[maxn];
vector<tuple<int, int, int>> G[maxn];
bool vis[maxn];

struct Seg {
  int l, r;
  int mx[2];
  bool lzy;
} T[maxn << 2];

void push_up(int o) {
  T[o].mx[0] = max(T[o << 1].mx[0], T[o << 1 | 1].mx[0]);
  T[o].mx[1] = max(T[o << 1].mx[1], T[o << 1 | 1].mx[1]);
}

void push_down(int o) {
  if (!T[o].lzy) return;
  swap(T[o << 1].mx[0], T[o << 1].mx[1]);
  T[o << 1].lzy ^= true;
  swap(T[o << 1 | 1].mx[0], T[o << 1 | 1].mx[1]);
  T[o << 1 | 1].lzy ^= true;
  T[o].lzy = false;
}

void build(int o, int l, int r) {
  T[o].l = l, T[o].r = r;
  if (l == r) {
    T[o].mx[stone[seq[l]] & 1]  = dep[seq[l]];
  } else {
    int mi = l + r >> 1;
    build(o << 1, l, mi);
    build(o << 1 | 1, mi + 1, r);
    push_up(o);
  }
}

void update(int o, int l, int r) {
  if (l <= T[o].l && T[o].r <= r) {
    swap(T[o].mx[0], T[o].mx[1]);
    T[o].lzy ^= true;
    return;
  }
  push_down(o);
  int mi = T[o].l + T[o].r >> 1;
  if (l <= mi) update(o << 1, l, r);
  if (r > mi) update(o << 1 | 1, l, r);
  push_up(o);
}

void dfs(int u, int par) {
  if (par) dep[u] = dep[par] + 1;
  in[u] = ++clk;
  seq[clk] = u;
  for (auto [v, t, e] : G[u]) if (v != par) {
    stone[v] = stone[u] + t;
    ver[e] = v;
    dfs(v, u);
  }
  out[u] = clk;
}

int bfs() {
  queue<int> Q;
  Q.emplace(1);
  vis[1] = true;
  int u = 0;
  while (!Q.empty()) {
    u = Q.front(); Q.pop();
    for (auto [v, _, __] : G[u]) if (!vis[v]) {
      vis[v] = true;
      Q.emplace(v);
    }
  }
  return u;
}

int main() {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cin >> n;
  FOR(i, 1, n - 1) {
    int u, v, t;
    cin >> u >> v >> t;
    G[u].eb(v, t, i);
    G[v].eb(u, t, i);
  }
  int r = bfs();
  dfs(r, 0);
  build(1, 1, clk);
  cin >> q;
  while (q--) {
    int id; cin >> id;
    int u = ver[id];
    update(1, in[u], out[u]);
    cout << T[1].mx[0] << '\n';
  }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int maxn = 512345;

int n, q;
vector<tuple<int, int, int>> G[maxn];

struct Tree {
  int clk;
  int in[maxn], out[maxn], dep[maxn], stone[maxn], ver[maxn], seq[maxn];
  bool vis[maxn];

  struct Seg {
    int l, r;
    int mx[2];
    bool lzy;
  } T[maxn << 2];

  void push_up(int o) {
    T[o].mx[0] = max(T[o << 1].mx[0], T[o << 1 | 1].mx[0]);
    T[o].mx[1] = max(T[o << 1].mx[1], T[o << 1 | 1].mx[1]);
  }

  void push_down(int o) {
    if (!T[o].lzy) return;
    swap(T[o << 1].mx[0], T[o << 1].mx[1]);
    T[o << 1].lzy ^= true;
    swap(T[o << 1 | 1].mx[0], T[o << 1 | 1].mx[1]);
    T[o << 1 | 1].lzy ^= true;
    T[o].lzy = false;
  }

  void build(int o, int l, int r) {
    T[o].l = l, T[o].r = r;
    if (l == r) {
      T[o].mx[stone[seq[l]] & 1] = dep[seq[l]];
    } else {
      int mi = l + r >> 1;
      build(o << 1, l, mi);
      build(o << 1 | 1, mi + 1, r);
      push_up(o);
    }
  }

  void update(int o, int l, int r) {
    if (l <= T[o].l && T[o].r <= r) {
      swap(T[o].mx[0], T[o].mx[1]);
      T[o].lzy ^= true;
      return;
    }
    push_down(o);
    int mi = T[o].l + T[o].r >> 1;
    if (l <= mi) update(o << 1, l, r);
    if (r > mi) update(o << 1 | 1, l, r);
    push_up(o);
  }

  void dfs(int u, int par) {
    if (par) dep[u] = dep[par] + 1;
    in[u] = ++clk;
    seq[clk] = u;
    for (auto[v, t, e] : G[u])
      if (v != par) {
        stone[v] = stone[u] + t;
        ver[e] = v;
        dfs(v, u);
      }
    out[u] = clk;
  }

  int bfs(int r) {
    queue<int> Q;
    Q.emplace(r);
    vis[r] = true;
    int u = 0;
    while (!Q.empty()) {
      u = Q.front();
      Q.pop();
      for (auto[v, _, __] : G[u])
        if (!vis[v]) {
          vis[v] = true;
          Q.emplace(v);
        }
    }
    return u;
  }
} T[2];

int main() {
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cin >> n;
  FOR(i, 1, n - 1) {
    int u, v, t;
    cin >> u >> v >> t;
    G[u].eb(v, t, i);
    G[v].eb(u, t, i);
  }
  int r = 1;
  REP(i, 2) {
    r = T[i].bfs(r);
    T[i].dfs(r, 0);
    T[i].build(1, 1, n);
  }
  cin >> q;
  while (q--) {
    int id; cin >> id;
    int ans = 0;
    REP(i, 2) {
      int u = T[i].ver[id];
      T[i].update(1, T[i].in[u], T[i].out[u]);
      chkmax(ans, T[i].T[1].mx[0]);
    }
    cout << ans << '\n';
  }
}


// Author -- xyr2005

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>inline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>inline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>inline void read(_Tp &t,Args &...args){read(t);read(args...);}
inline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=1000005;
struct edge{
	int v,nxt,w;
}c[N<<1];
int front[N],edge_cnt;
inline void addedge(int u,int v,int w)
{
	c[++edge_cnt]=(edge){v,front[u],w};
	front[u]=edge_cnt;
}
int dep[N],st[N];
int nd[N],E[N][2];
int t[N],p,rev[N];
int fir[N],end[N];
void dfs(int x,int fa)
{
	t[++p]=x,rev[p]=x,fir[x]=p;
	dep[x]=dep[fa]+1;
	for(int i=front[x];i;i=c[i].nxt)
	{
		int v=c[i].v;
		if(v==fa) continue;
		st[v]=st[x]^c[i].w;
		dfs(v,x);
		t[++p]=x,rev[p]=x;
	}
	end[x]=p;
}
int n;
struct seg_tr{
	struct Node{
		int ls,rs;
		int minn,maxx[2],LM[2],MR[2],LMR[2];
		bool tag;
	}f[N<<1];
	int node_cnt;
	inline void PushUp(int x)
	{
		f[x].minn=min(f[f[x].ls].minn,f[f[x].rs].minn);
		for(int i=0;i<2;++i)
		{
			f[x].maxx[i]=max(f[f[x].ls].maxx[i],f[f[x].rs].maxx[i]);
			f[x].LMR[i]=max(max(f[f[x].ls].LMR[i],f[f[x].rs].LMR[i]),max(f[f[x].ls].LM[i]+f[f[x].rs].maxx[i],f[f[x].rs].MR[i]+f[f[x].ls].maxx[i]));
			f[x].LM[i]=max(max(f[f[x].ls].LM[i],f[f[x].rs].LM[i]),f[f[x].ls].maxx[i]-(f[f[x].rs].minn<<1));
			f[x].MR[i]=max(max(f[f[x].ls].MR[i],f[f[x].rs].MR[i]),f[f[x].rs].maxx[i]-(f[f[x].ls].minn<<1));
		}
	}
	int build(int l,int r)
	{
		int cur=++node_cnt;
		if(l==r)
		{
			for(int i=0;i<2;++i) f[cur].maxx[i]=f[cur].LM[i]=f[cur].MR[i]=f[cur].LMR[i]=-inf;
			f[cur].maxx[st[rev[l]]]=f[cur].minn=dep[rev[l]];
			f[cur].LM[st[rev[l]]]=f[cur].MR[st[rev[l]]]=-dep[rev[l]];
			f[cur].LMR[st[rev[l]]]=0;
			return cur;
		}
		int mid=(l+r)>>1;
		f[cur].ls=build(l,mid);
		f[cur].rs=build(mid+1,r);
		PushUp(cur);
		return cur;
	}
	inline void flip(int cur)
	{
		std::swap(f[cur].maxx[0],f[cur].maxx[1]);
		std::swap(f[cur].LM[0],f[cur].LM[1]);
		std::swap(f[cur].MR[0],f[cur].MR[1]);
		std::swap(f[cur].LMR[0],f[cur].LMR[1]);
		f[cur].tag=1;
	}
	inline void PushDown(int cur)
	{
		if(f[cur].tag)
		{
			flip(f[cur].ls);
			flip(f[cur].rs);
			f[cur].tag=0;
		}
	}
	void Update(int L,int R,int l=1,int r=p,int cur=1)
	{
		if(L<=l&&r<=R)
		{
			flip(cur);
			return;
		}
		PushDown(cur);
		int mid=(l+r)>>1;
		if(L<=mid) Update(L,R,l,mid,f[cur].ls);
		if(R>mid) Update(L,R,mid+1,r,f[cur].rs);
		PushUp(cur);
	}
	inline int Query(){return max(f[1].LMR[0],f[1].LMR[1]);}
}tr;
int main()
{
	int n;read(n);
	int x,y,z;
	for(int i=1;i<n;++i)
	{
		read(x,y,z);
		addedge(x,y,z),addedge(y,x,z);
		E[i][0]=x,E[i][1]=y;
	}
	dfs(1,0);
	tr.build(1,p);
	for(int i=1;i<=n;++i) nd[i]=dep[E[i][0]]>dep[E[i][1]]?E[i][0]:E[i][1];
	int m;read(m);
	while(m--)
	{
		read(x),x=nd[x];
		tr.Update(fir[x],end[x]);
		printf("%d\n",tr.Query());
	}
	return 0;
}


// Author -- xyr2005

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>inline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>inline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>inline void read(_Tp &t,Args &...args){read(t);read(args...);}
inline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=1000005;
struct edge{
	int v,nxt,w;
}c[N<<1];
int front[N],edge_cnt;
inline void addedge(int u,int v,int w)
{
	c[++edge_cnt]=(edge){v,front[u],w};
	front[u]=edge_cnt;
}
int dep[N],st[N];
int nd[N],E[N][2];
int t[N],p,rev[N];
int fir[N],end[N];
void dfs(int x,int fa)
{
	t[++p]=x,rev[p]=x,fir[x]=p;
	dep[x]=dep[fa]+1;
	for(int i=front[x];i;i=c[i].nxt)
	{
		int v=c[i].v;
		if(v==fa) continue;
		st[v]=st[x]^c[i].w;
		dfs(v,x);
		t[++p]=x,rev[p]=x;
	}
	end[x]=p;
}
int n;
struct seg_tr{
	struct Node{
		int ls,rs;
		int minn,maxx[2],LM[2],MR[2],LMR[2];
		bool tag;
	}f[N<<1];
	int node_cnt;
	inline void PushUp(int x)
	{
		f[x].minn=min(f[f[x].ls].minn,f[f[x].rs].minn);
		for(int i=0;i<2;++i)
		{
			f[x].maxx[i]=max(f[f[x].ls].maxx[i],f[f[x].rs].maxx[i]);
			f[x].LMR[i]=max(max(f[f[x].ls].LMR[i],f[f[x].rs].LMR[i]),max(f[f[x].ls].LM[i]+f[f[x].rs].maxx[i],f[f[x].rs].MR[i]+f[f[x].ls].maxx[i]));
			f[x].LM[i]=max(max(f[f[x].ls].LM[i],f[f[x].rs].LM[i]),f[f[x].ls].maxx[i]-(f[f[x].rs].minn<<1));
			f[x].MR[i]=max(max(f[f[x].ls].MR[i],f[f[x].rs].MR[i]),f[f[x].rs].maxx[i]-(f[f[x].ls].minn<<1));
		}
	}
	int build(int l,int r)
	{
		int cur=++node_cnt;
		if(l==r)
		{
			for(int i=0;i<2;++i) f[cur].maxx[i]=f[cur].LM[i]=f[cur].MR[i]=f[cur].LMR[i]=-inf;
			f[cur].maxx[st[rev[l]]]=f[cur].minn=dep[rev[l]];
			f[cur].LM[st[rev[l]]]=f[cur].MR[st[rev[l]]]=-dep[rev[l]];
			f[cur].LMR[st[rev[l]]]=0;
			return cur;
		}
		int mid=(l+r)>>1;
		f[cur].ls=build(l,mid);
		f[cur].rs=build(mid+1,r);
		PushUp(cur);
		return cur;
	}
	inline void flip(int cur)
	{
		std::swap(f[cur].maxx[0],f[cur].maxx[1]);
		std::swap(f[cur].LM[0],f[cur].LM[1]);
		std::swap(f[cur].MR[0],f[cur].MR[1]);
		std::swap(f[cur].LMR[0],f[cur].LMR[1]);
		f[cur].tag^=1;
	}
	inline void PushDown(int cur)
	{
		if(f[cur].tag)
		{
			flip(f[cur].ls);
			flip(f[cur].rs);
			f[cur].tag=0;
		}
	}
	void Update(int L,int R,int l=1,int r=p,int cur=1)
	{
		if(L<=l&&r<=R)
		{
			flip(cur);
			return;
		}
		PushDown(cur);
		int mid=(l+r)>>1;
		if(L<=mid) Update(L,R,l,mid,f[cur].ls);
		if(R>mid) Update(L,R,mid+1,r,f[cur].rs);
		PushUp(cur);
	}
	inline int Query(){return max(f[1].LMR[0],f[1].LMR[1]);}
}tr;
int main()
{
	int n;read(n);
	int x,y,z;
	for(int i=1;i<n;++i)
	{
		read(x,y,z);
		addedge(x,y,z),addedge(y,x,z);
		E[i][0]=x,E[i][1]=y;
	}
	dfs(1,0);
	tr.build(1,p);
	for(int i=1;i<n;++i) nd[i]=dep[E[i][0]]>dep[E[i][1]]?E[i][0]:E[i][1];
	int m;read(m);
	while(m--)
	{
		read(x),x=nd[x];
		tr.Update(fir[x],end[x]);
		printf("%d\n",tr.Query());
	}
	return 0;
}


// Author -- xyr2005

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-(x)))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<ll,ll> pl;
using std::min;
using std::max;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x3f3f3f3f3f3f3f3f;

std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>inline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>inline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>inline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>inline void read(_Tp &x)
{
	char ch(getchar());bool f(false);while(!isdigit(ch)) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch)) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	f&&(x=-x);
}
template <typename _Tp,typename... Args>inline void read(_Tp &t,Args &...args){read(t);read(args...);}
inline int read_str(char *s)
{
	char ch(getchar());while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;*tar=ch,ch=getchar();while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=1000005;
struct edge{
	int v,nxt,w;
}c[N<<1];
int front[N],edge_cnt;
inline void addedge(int u,int v,int w)
{
	c[++edge_cnt]=(edge){v,front[u],w};
	front[u]=edge_cnt;
}
int dep[N],st[N];
int nd[N],E[N][2];
int t[N],p,rev[N];
int fir[N],end[N];
void dfs(int x,int fa)
{
	t[++p]=x,rev[p]=x,fir[x]=p;
	dep[x]=dep[fa]+1;
	for(int i=front[x];i;i=c[i].nxt)
	{
		int v=c[i].v;
		if(v==fa) continue;
		st[v]=st[x]^c[i].w;
		dfs(v,x);
		t[++p]=x,rev[p]=x;
	}
	end[x]=p;
}
int n;
struct seg_tr{
	struct Node{
		int ls,rs;
		int minn,maxx[2],LM[2],MR[2],LMR[2];
		bool tag;
	}f[N<<1];
	int node_cnt;
	inline void PushUp(int x)
	{
		f[x].minn=min(f[f[x].ls].minn,f[f[x].rs].minn);
		for(int i=0;i<2;++i)
		{
			f[x].maxx[i]=max(f[f[x].ls].maxx[i],f[f[x].rs].maxx[i]);
			f[x].LMR[i]=max(max(f[f[x].ls].LMR[i],f[f[x].rs].LMR[i]),max(f[f[x].ls].LM[i]+f[f[x].rs].maxx[i],f[f[x].rs].MR[i]+f[f[x].ls].maxx[i]));
			f[x].LM[i]=max(max(f[f[x].ls].LM[i],f[f[x].rs].LM[i]),f[f[x].ls].maxx[i]-(f[f[x].rs].minn<<1));
			f[x].MR[i]=max(max(f[f[x].ls].MR[i],f[f[x].rs].MR[i]),f[f[x].rs].maxx[i]-(f[f[x].ls].minn<<1));
		}
	}
	int build(int l,int r)
	{
		int cur=++node_cnt;
		if(l==r)
		{
			for(int i=0;i<2;++i) f[cur].maxx[i]=f[cur].LM[i]=f[cur].MR[i]=f[cur].LMR[i]=-inf;
			f[cur].maxx[st[rev[l]]]=f[cur].minn=dep[rev[l]];
			f[cur].LM[st[rev[l]]]=f[cur].MR[st[rev[l]]]=-dep[rev[l]];
			f[cur].LMR[st[rev[l]]]=0;
			return cur;
		}
		int mid=(l+r)>>1;
		f[cur].ls=build(l,mid);
		f[cur].rs=build(mid+1,r);
		PushUp(cur);
		return cur;
	}
	inline void flip(int cur)
	{
		std::swap(f[cur].maxx[0],f[cur].maxx[1]);
		std::swap(f[cur].LM[0],f[cur].LM[1]);
		std::swap(f[cur].MR[0],f[cur].MR[1]);
		std::swap(f[cur].LMR[0],f[cur].LMR[1]);
	}
	inline void PushDown(int cur)
	{
		if(f[cur].tag)
		{
			flip(f[cur].ls);
			flip(f[cur].rs);
			f[cur].tag=0;
		}
	}
	void Update(int L,int R,int l=1,int r=p,int cur=1)
	{
		if(L<=l&&r<=R)
		{
			flip(cur);
			return;
		}
		PushDown(cur);
		int mid=(l+r)>>1;
		if(L<=mid) Update(L,R,l,mid,f[cur].ls);
		if(R>mid) Update(L,R,mid+1,r,f[cur].rs);
		PushUp(cur);
	}
	inline int Query(){return max(f[1].LMR[0],f[1].LMR[1]);}
}tr;
int main()
{
	int n;read(n);
	int x,y,z;
	for(int i=1;i<n;++i)
	{
		read(x,y,z);
		addedge(x,y,z),addedge(y,x,z);
		E[i][0]=x,E[i][1]=y;
	}
	dfs(1,0);
	tr.build(1,p);
	for(int i=1;i<=n;++i) nd[i]=dep[E[i][0]]>dep[E[i][1]]?E[i][0]:E[i][1];
	int m;read(m);
	while(m--)
	{
		read(x),x=nd[x];
		tr.Update(fir[x],end[x]);
		printf("%d\n",tr.Query());
	}
	return 0;
}


//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;

//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif

int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
	if(b==0)return 1;
	if(b==1)return(a%mod);
	int tem=po(a,b/2);
	if(b&1)return(((tem*tem)%mod)*a)%mod;
	else return(tem*tem)%mod;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
int n,msiz[500010],siz[500010],d[500010],ct[500010],dep[500010],pa[500010],lar[500010];
vector<pii> g[500010];
bool vis[500010];
vector<int> v,rg[500010];
multiset<int> m[500010][2],ans;
map<int,pii> tb[500010];
pii edge[500010];
void dfs(int p){
	msiz[p]=0,siz[p]=1;
	vis[p]=true;
	v.pb(p);
	for(auto pt:g[p]){
		if(vis[pt.X])continue;
		dfs(pt.X);
		msiz[p]=max(siz[pt.X],msiz[p]);
		siz[p]+=siz[pt.X];
	}
}
void dfs1(int p,int di,int dd){
	d[p]=di;
	ct[p]=dd;
	vis[p]=true;
	for(auto pt:g[p]){
		if(vis[pt.X])continue;
		dfs1(pt.X,di+1,dd+pt.Y);
	}
}
int getcn(int p,int f,int dp){
	v.clear();
	dfs(p);
	int cn=-1;
	int N=sz(v);
	for(int i:v)if((msiz[i]<=N/2)&&(N-siz[i])<=N/2)cn=i,vis[i]=false;
	dfs1(cn,0,0);
	for(int i:v){
		if(i!=cn){
			tb[cn][i]=mp(d[i],ct[i]&1);
			m[cn][ct[i]&1].insert(d[i]);
			vis[i]=false;
		}
	}
	dep[cn]=dp;
	pa[cn]=f;
	for(auto pt:g[cn]){
		if(!vis[pt.X])rg[cn].pb(getcn(pt.X,cn,dp+1));
	}
	return cn;
}
void calc(int p){
	int an=0;
	if(sz(m[p][1])>=2){
		an=max(*(--(m[p][1].end()))+*(--(--(m[p][1].end()))),an);
	}
	if(sz(m[p][0])>=1){
		an=max(*(--(m[p][1].end())),an);
		if(sz(m[p][0])>=2)an=max(*(--(m[p][0].end()))+*(--(--(m[p][0].end()))),an);
	}
	lar[p]=an;
	ans.insert(an);
}
void dfs2(int p){
	calc(p);
	for(int u:rg[p])dfs2(u);
}
void change(int a,int b){
	int p=pa[b];
	while(1){
		pii pt=tb[p][b];
		if(pt.Y==0){
			m[p][0].erase(m[p][0].find(pt.X));
			m[p][1].insert(pt.X);
			tb[p][b].Y=1;
		}
		else{
			m[p][1].erase(m[p][1].find(pt.X));
			m[p][0].insert(pt.X);
			tb[p][b].Y=0;
		}
		ans.erase(lar[p]);
		calc(p);
		if(p==a)break;
		p=pa[a];
	}
}
signed main(){
	IOS();
	cin>>n;
	F(n-1){
		int x,y,z;
		cin>>x>>y>>z;
		--x,--y;
		g[x].pb(mp(y,z));
		g[y].pb(mp(x,z));
		edge[i]={x,y};
	}
	int cen=getcn(0,-1,0);
	dfs2(cen);
	int q;
	cin>>q;
	while(q--){
		int x;
		cin>>x;
		--x;
		int a=edge[x].X,b=edge[x].Y;
		if(dep[a]>dep[b])swap(a,b);
		change(a,b);
		cout<<(*(ans.rbegin()))<<endl;
	}
return 0;
}


//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;

//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif

int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
	if(b==0)return 1;
	if(b==1)return(a%mod);
	int tem=po(a,b/2);
	if(b&1)return(((tem*tem)%mod)*a)%mod;
	else return(tem*tem)%mod;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
vector<pii> g[500010];
int n,ind;
struct NODE{
	int ma,mi,t;
};
vector<NODE> seg1(2000010),seg2(2000010);
vector<int> l1(500010),l2(500010),r1(500010),r2(500010),mii(500010),maa(500010),id1(500010),id2(500010);
pii edge[500010];
pii dfs(int p,int f,int d){
	pii re=mp(p,d);
	for(auto pt:g[p]){
		if(pt.X==f)continue;
		auto tt=dfs(pt.X,p,d+1);
		if(tt.Y>re.Y)re=tt;
	}
	return re;
}
void dfs1(int p,vector<int> &l,vector<int> &r,vector<int> &id,int f,int d,int inv){
	l[p]=++ind;
	id[ind]=p;
	int mi=0,ma=0;
	if(inv)mi=d;
	else ma=d;
	for(auto pt:g[p]){
		if(pt.X==f)continue;
		dfs1(pt.X,l,r,id,p,d+1,inv^pt.Y);
	}
	mii[p]=mi,maa[p]=ma;
	r[p]=ind;
	bug(p,maa[p],mii[p]);
}
void build(vector<NODE> &seg,int p,int l,int r,vector<int>& id){
	if(l==r){
		seg[p].ma=maa[id[l]];
		seg[p].mi=mii[id[l]];
		seg[p].t=0;
		return;
	}
	int mi=(l+r)/2;
	build(seg,p*2,l,mi,id);
	build(seg,p*2+1,mi+1,r,id);
	seg[p].ma=max(seg[p*2].ma,seg[p*2+1].ma);
	seg[p].mi=max(seg[p*2].mi,seg[p*2+1].mi);
	return;
}
void modify(vector<NODE> &seg,int p,int l,int r,int ql,int qr){
	bug(ql,qr);
	if(l>qr||r<ql)return;
	if(l>=ql&&r<=qr){
		swap(seg[p].ma,seg[p].mi);
		seg[p].t^=1;
		return;
	}
	int mi=(l+r)/2;
	if(seg[p].t==1){
		swap(seg[p*2].ma,seg[p*2].mi);
		swap(seg[p*2+1].ma,seg[p*2+1].mi);
		seg[p*2].t^=1;
		seg[p*2+1].t^=1;
		seg[p].t=0;
	}
	modify(seg,p*2,l,mi,ql,qr);
	modify(seg,p*2+1,mi+1,r,ql,qr);
	seg[p].ma=max(seg[p*2].ma,seg[p*2+1].ma);
	seg[p].mi=max(seg[p*2].mi,seg[p*2+1].mi);
	bug(l,r);
	bug(seg[p].ma,seg[p].mi);
	return;
}
signed main(){
	IOS();
	cin>>n;
	F(n-1){
		int x,y,c;
		cin>>x>>y>>c;
		--x,--y;
		g[x].pb(mp(y,c));
		g[y].pb(mp(x,c));
		edge[i]=mp(x,y);
	}
	int p1=dfs(0,-1,0).X;
	int p2=dfs(p1,-1,0).X;
	ind=-1;
	dfs1(p1,l1,r1,id1,-1,0,0);
	build(seg1,1,0,n-1,id1);
	ind=-1;
	dfs1(p2,l2,r2,id2,-1,0,0);
	build(seg2,1,0,n-1,id2);
	bug(p1,p2);
	int q;
	cin>>q;
	while(q--){
		int x;
		cin>>x;
		--x;
		int a=edge[x].X,b=edge[x].Y;
		int ans=0;
		if(l1[a]>l1[b])swap(a,b);
		bug(l1[a],l1[b]);
		modify(seg1,1,0,n-1,l1[b],r1[b]);
		ans=max(ans,seg1[1].ma);
		bug(ans);
		if(l2[a]>l2[b])swap(a,b);
		modify(seg2,1,0,n-1,l2[b],r2[b]);
		ans=max(ans,seg2[1].ma);
		cout<<ans<<endl;
	}
return 0;
}


//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;

//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif

int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
	if(b==0)return 1;
	if(b==1)return(a%mod);
	int tem=po(a,b/2);
	if(b&1)return(((tem*tem)%mod)*a)%mod;
	else return(tem*tem)%mod;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
vector<pii> g[500010];
int n,ind;
struct NODE{
	int ma,mi,t;
};
vector<NODE> seg1(2000010),seg2(2000010);
vector<int> l1(500010),l2(500010),r1(500010),r2(500010),mii(500010),maa(500010),id1(500010),id2(500010);
pii edge[500010];
pii dfs(int p,int f,int d){
	pii re=mp(p,d);
	for(auto pt:g[p]){
		if(pt.X==f)continue;
		auto tt=dfs(pt.X,p,d+1);
		if(tt.Y>re.Y)re=tt;
	}
	return re;
}
void dfs1(int p,vector<int> &l,vector<int> &r,vector<int> &id,int f,int d,int inv){
	l[p]=++ind;
	id[ind]=p;
	int mi=0,ma=0;
	if(inv)mi=d;
	else ma=d;
	for(auto pt:g[p]){
		if(pt.X==f)continue;
		dfs1(pt.X,l,r,id,p,d+1,inv^pt.Y);
	}
	mii[p]=mi,maa[p]=ma;
	r[p]=ind;
	bug(p,maa[p],mii[p]);
}
void build(vector<NODE> &seg,int p,int l,int r,vector<int> id){
	if(l==r){
		seg[p].ma=maa[id[l]];
		seg[p].mi=mii[id[l]];
		seg[p].t=0;
		return;
	}
	int mi=(l+r)/2;
	build(seg,p*2,l,mi,id);
	build(seg,p*2+1,mi+1,r,id);
	seg[p].ma=max(seg[p*2].ma,seg[p*2+1].ma);
	seg[p].mi=max(seg[p*2].mi,seg[p*2+1].mi);
	return;
}
void modify(vector<NODE> &seg,int p,int l,int r,int ql,int qr){
	bug(ql,qr);
	if(l>qr||r<ql)return;
	if(l>=ql&&r<=qr){
		swap(seg[p].ma,seg[p].mi);
		seg[p].t^=1;
		return;
	}
	int mi=(l+r)/2;
	if(seg[p].t==1){
		swap(seg[p*2].ma,seg[p*2].mi);
		swap(seg[p*2+1].ma,seg[p*2+1].mi);
		seg[p*2].t^=1;
		seg[p*2+1].t^=1;
		seg[p].t=0;
	}
	modify(seg,p*2,l,mi,ql,qr);
	modify(seg,p*2+1,mi+1,r,ql,qr);
	seg[p].ma=max(seg[p*2].ma,seg[p*2+1].ma);
	seg[p].mi=max(seg[p*2].mi,seg[p*2+1].mi);
	bug(l,r);
	bug(seg[p].ma,seg[p].mi);
	return;
}
signed main(){
	IOS();
	cin>>n;
	F(n-1){
		int x,y,c;
		cin>>x>>y>>c;
		--x,--y;
		g[x].pb(mp(y,c));
		g[y].pb(mp(x,c));
		edge[i]=mp(x,y);
	}
	int p1=dfs(0,-1,0).X;
	int p2=dfs(p1,-1,0).X;
	ind=-1;
	dfs1(p1,l1,r1,id1,-1,0,0);
	build(seg1,1,0,n-1,id1);
	ind=-1;
	dfs1(p2,l2,r2,id2,-1,0,0);
	build(seg2,1,0,n-1,id2);
	bug(p1,p2);
	int q;
	cin>>q;
	while(q--){
		int x;
		cin>>x;
		--x;
		int a=edge[x].X,b=edge[x].Y;
		int ans=0;
		if(l1[a]>l1[b])swap(a,b);
		bug(l1[a],l1[b]);
		modify(seg1,1,0,n-1,l1[b],r1[b]);
		ans=max(ans,seg1[1].ma);
		bug(ans);
		if(l2[a]>l2[b])swap(a,b);
		modify(seg2,1,0,n-1,l2[b],r2[b]);
		ans=max(ans,seg2[1].ma);
		cout<<ans<<endl;
	}
return 0;
}


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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ls(x) x * 2 + 1
#define rs(x) x * 2 + 2
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = (int)1e9;

const int N = (int)1e6 + 50;

struct edge {
    int to, w, id;
};

int tin[N], tout[N], tim = 0;
int val[N];
int pr[N];
int nid[N];

struct node {
    int cr;
    int abmax;
    int a, b;
    int abmax_pre[2], abmax_suf[2];
    int amb_suf[2], bma_pre[2];

    void flip() {
        cr ^= 1;
        swap(abmax_pre[0], abmax_pre[1]);
        swap(abmax_suf[0], abmax_suf[1]);
        swap(amb_suf[0], amb_suf[1]);
        swap(bma_pre[0], bma_pre[1]);
    }

    void merge(node &ls, node &rs) {
        abmax = 0;
        rep(p, 0, 2) {
//            if(p == 0) cout << ls.abmax_suf[p] << " " << rs.bma_pre[p] << " " << ls.amb_suf[p] << " " << rs.abmax_pre[p] << endl;
            abmax = max({abmax, ls.abmax_suf[p] + rs.bma_pre[p], ls.amb_suf[p] + rs.abmax_pre[p]});
//            cout << abmax << endl;

            abmax_pre[p] = max(ls.abmax_pre[p], max(ls.a + ls.b + rs.bma_pre[p], ls.a - ls.b + rs.abmax_pre[p]));
            abmax_suf[p] = max(max(ls.amb_suf[p] + rs.a + rs.b, ls.abmax_suf[p] + rs.b - rs.a), rs.abmax_suf[p]);
            amb_suf[p] = max(ls.amb_suf[p] + rs.a - rs.b, rs.amb_suf[p]);
            bma_pre[p] = max(ls.bma_pre[p], ls.b - ls.a + rs.bma_pre[p]);
        }

        abmax = max(abmax, max(ls.abmax, rs.abmax));
        a = ls.a + max(0, rs.a - ls.b);
        b = max(0, ls.b - rs.a) + rs.b;
    }
};

node newnode(int v, int d) {
    if(!d) return {0, 1, !v, v,
                   {1, -INF}, {1, -INF},
                   {!v, -INF}, {v, -INF}};
    else return {0, 1, !v, v,
                 {-INF, 1}, {-INF, 1},
                 {-INF, !v}, {-INF, v}};
}

struct Tree {
    node dat[4 * N];

    void push_down(int x, int l, int r) {
        if(dat[x].cr) {
            if(l < r) {
                dat[ls(x)].flip();
                dat[rs(x)].flip();
            }
            dat[x].cr = 0;
        }
    }

    void init(int x, int l, int r) {
        if(l == r) {
            dat[x] = newnode(val[l], pr[l]);
            return ;
        }
        int mid = (l + r) / 2;
        init(ls(x), l, mid);
        init(rs(x), mid + 1, r);
        dat[x].merge(dat[ls(x)], dat[rs(x)]);
//        cout << x << " " << l << " " << r << ": " << dat[x].abmax << endl;
    }

    void update(int a, int b, int x, int l, int r) {
        int mid = (l + r) / 2;
        if(r < a || l > b) return ;

        push_down(x, l, r);

        if(l >= a && r <= b) {
            dat[x].flip();
            return ;
        }

        update(a, b, ls(x), l, mid);
        update(a, b, rs(x),mid+1, r);

        dat[x].merge(dat[ls(x)], dat[rs(x)]);
    }

} tree;

int n, m;
vector<edge> G[N];

void dfs(int v, int p, int d) {
    val[tim] = 1;
    pr[tim] = d;
    tin[v] = tim++;

    for(auto e : G[v]) {
        if(e.to == p) continue;
        nid[e.id] = e.to;
        dfs(e.to, v, d ^ e.w);
    }

    val[tim] = 0;
    pr[tim] = d;
    tout[v] = tim++;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    rep(i, 0, n - 1) {
        int a, b, w;
        cin >> a >> b >> w;
        a--, b--;
        G[a].push_back({b, w, i});
        G[b].push_back({a, w, i});
    }
    dfs(0, -1, 0);
    tree.init(0, 1, 2 * n - 2);

    cin >> m;

    rep(i, 0, m) {
        int id; cin >> id; id--;
        int v = nid[id];
        tree.update(tin[v], tout[v], 0, 1, 2 * n - 2);
        cout << max(tree.dat[0].abmax, max(tree.dat[0].abmax_pre[0], tree.dat[0].abmax_suf[0])) << '\n';
    }

}


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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ls(x) x * 2 + 1
#define rs(x) x * 2 + 2
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = (int)1e9;

const int N = (int)1e6 + 50;

struct edge {
    int to, w, id;
};

int n, m;
vector<edge> G[N];

struct node {
    int cr;
    int mx[2];

    void flip() {
        cr ^= 1;
        swap(mx[0], mx[1]);
    }

    void merge(node &ls, node &rs) {
        rep(p, 0, 2) mx[p] = max(ls.mx[p], rs.mx[p]);
    }
};

struct Tree {
    node dat[4 * N];
    int root;
    int tin[N], tout[N], tim = 0;
    int nid[N], pr[N];
    int dep[N];

    void dfs(int v, int p, int d, int depth) {
        pr[tim] = d;
        dep[tim] = depth;
        tin[v] = tim++;
        for(auto e : G[v]) {
            if(e.to == p) continue;
            nid[e.id] = e.to;
            dfs(e.to, v, d ^ e.w, depth + 1);
        }
        tout[v] = tim - 1;
    }

    void push_down(int x, int l, int r) {
        if(dat[x].cr) {
            if(l < r) {
                dat[ls(x)].flip();
                dat[rs(x)].flip();
            }
            dat[x].cr = 0;
        }
    }

    void init(int x, int l, int r) {
        if(l == r) {
            dat[x] = pr[l] ? node{0, {0, dep[l]}} :
                    node{0, {dep[l] , 0}};
            return ;
        }
        int mid = (l + r) / 2;
        init(ls(x), l, mid);
        init(rs(x), mid + 1, r);
        dat[x].merge(dat[ls(x)], dat[rs(x)]);
    }

    void update(int a, int b, int x, int l, int r) {
        int mid = (l + r) / 2;
        if(r < a || l > b) return ;

        push_down(x, l, r);

        if(l >= a && r <= b) {
            dat[x].flip();
            return ;
        }

        update(a, b, ls(x), l, mid);
        update(a, b, rs(x),mid+1, r);

        dat[x].merge(dat[ls(x)], dat[rs(x)]);
    }

    void update(int eid) {
        update(tin[nid[eid]], tout[nid[eid]], 0, 0, n - 1);
    }

} tree[2];

pii dfs(int v, int p) {
    pii res = {0, v};
    for(auto e : G[v]) {
        if(e.to != p) {
            pii nxt = dfs(e.to, v);
            res = max(res, {nxt.first + 1, nxt.second});
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    rep(i, 0, n - 1) {
        int a, b, w;
        cin >> a >> b >> w;
        a--, b--;
        G[a].push_back({b, w, i});
        G[b].push_back({a, w, i});
    }
    pii p = dfs(0, -1);
    tree[0].root = p.second;
    p = dfs(p.second, -1);
    tree[1].root = p.second;

    rep(it, 0, 2) {
        tree[it].dfs(tree[it].root, -1, 0, 0);
        tree[it].init(0, 0, n - 1);
    }
    cin >> m;

    rep(i, 0, m) {
        int id; cin >> id; id--;
        tree[0].update(id);
        tree[1].update(id);
        cout << max(tree[0].dat[0].mx[0], tree[1].dat[0].mx[0]) << '\n';
    }

}