#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

bool connections[101][101];
int biggest=0;
int tested[101];

//void branch (int current, int index);
void search (int current, int target, int length);

int main (){
	freopen ("data5.txt", "r", stdin);
	freopen ("out5.txt", "w", stdout);
	int i, a, n, x, p, q, first;
	
	for (x=0; x<5; ++x){
		cin >> n;
		for (i=1; i<101; ++i){
			for (a=1; a<101; ++a){
				connections[i][a]=false;
			}
		}
		for (a=0; a<n; ++a){
			cin >> p >> q;
			if (a==0){
				first=p;
			}
			connections[p][q]=true;
		}
		search(first, first, 1);
		cout << biggest << endl;
		biggest=0;
	}
	
	//system("pause");
	return 0;
}
/*
void branch (int current, int index){
	int i, a;
	bool found;
	search(current, current, 1);
	tested[index]=current;
	++index;
	for (i=1; i<101; ++i){
		if (connections[current][a]==true){
			found=false;
			for (a=0; a<index-1; ++a){
				if (tested[a]==i){
					found=true;
					break;
				}
			}
			if (found==false){
				branch(i, index);
			}
		}
	}
	return;
}*/

void search (int current, int target, int length){
	int i;
	for (i=1; i<101; ++i){
		if (connections[current][target]==true){
			if (length>biggest){
				biggest=length;
			}
			return;
		}
		if (connections[current][i]==true){
			search(i, target, length+1);
		}
	}
	return;
}


