#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <functional>
#include <math.h>
#include <map>
#include <set>
#include <string>
using namespace std;
#define fio ios::sync_with_stdio(false); cin.tie(NULL);

typedef long long ll;
typedef unsigned long long ull;
const int INT_INF = 1e9;
const ll LL_INF = 2e18;

int n;
vector<int>G[100002];
int sti, curmx;
void dfs(int cur, int par, int dis) {
	if (curmx < dis) {
		curmx = dis;
		sti = cur;
	}

	for (auto nxt : G[cur]) {
		if (nxt == par) continue;
		dfs(nxt, cur, dis + 1);
	}
}

int main() {

	scanf("%d", &n);
	int ans = 0;
	for (int tc = 0; tc < n; tc++) {
		int ni;
		scanf("%d", &ni);
		for(int i=1;i<=ni;i++)
			G[i].clear();

		for (int i = 0; i < ni - 1; i++) {
			int u, v;
			scanf("%d %d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		curmx = -1;
		dfs(1, -1, 0);
		curmx = -1;
		dfs(sti, -1, 0);
		ans += curmx;
	}

	printf("%d", ans);
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <functional>
#include <math.h>
#include <map>
#include <set>
#include <string>
using namespace std;
#define fio ios::sync_with_stdio(false); cin.tie(NULL);

typedef long long ll;
typedef unsigned long long ull;
const int INT_INF = 1e9;
const ll LL_INF = 2e18;

int n;
vector<int>G[100002];
int sti, curmx;
void dfs(int cur, int par, int dis) {
	if (curmx < dis) {
		curmx = dis;
		sti = cur;
	}

	for (auto nxt : G[cur]) {
		if (nxt == par) continue;
		dfs(nxt, cur, dis + 1);
	}
}

int main() {

	scanf("%d", &n);
	int ans = 0;
	for (int tc = 0; tc < n; tc++) {
		int ni;
		scanf("%d", &ni);
		for (int i = 0; i < ni - 1; i++) {
			int u, v;
			scanf("%d %d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		curmx = -1;
		dfs(1, -1, 0);
		curmx = -1;
		dfs(sti, -1, 0);
		ans += curmx;
	}

	printf("%d", ans);
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <functional>
#include <math.h>
#include <map>
#include <set>
#include <string>
using namespace std;
#define fio ios::sync_with_stdio(false); cin.tie(NULL);

typedef long long ll;
typedef unsigned long long ull;
const int INT_INF = 1e9;
const ll LL_INF = 2e18;

int n;
vector<int>G[100002];
int sti, curmx;
void dfs(int cur, int par, int dis) {
	if (curmx < dis) {
		curmx = dis;
		sti = cur;
	}

	for (auto nxt : G[cur]) {
		if (nxt == par) continue;
		dfs(nxt, cur, dis + 1);
	}
}

int main() {

	scanf("%d", &n);
	int ans = 0;
	for (int tc = 0; tc < n; tc++) {
		int ni;
		scanf("%d", &ni);
		for (int i = 1; i <= ni; i++)
			G[i].clear();

		for (int i = 0; i < ni - 1; i++) {
			int u, v;
			scanf("%d %d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		curmx = -1;
		dfs(1, -1, 0);
		curmx = -1;
		dfs(sti, -1, 0);
		ans += curmx;
	}

	printf("%d", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;i++)
#define ll long long int
#define pb push_back
#define mp make_pair
vector<int> ve;
const int N = 105;
int vis[N+1];
vector<vector<int>> v(N+1);
void maxdep(vector<vector<int>> &tree, int node, int d){
    vis[node]=1;
    ve.push_back(d);
    forn(i,tree[node].size()){
        if(vis[tree[node][i]]==0) maxdep(tree,tree[node][i],d+1);
    }
}

int main(){
    //clock_t t;
    //t = clock();
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);

    int n;
    cin>>n;
    int arr[n];
    int c=0;
    for(int l=0;l<n;l++){
        int ni;
        cin>>ni;
        for (int i = 0; i < ni; ++i) {
                    v[i].clear();
                }
        int p,q;
        for(int m=0;m<ni-1;m++){
            cin>>p>>q;
            v[p].push_back(q);
            v[q].push_back(p);
        }
        int mx=INT_MIN;
        for(int i=1;i<=ni;i++){
        ve.clear();
        int w=105;
        forn(j,w){
            vis[j]=0;
        }
        maxdep(v,i,0);

        sort(ve.begin(),ve.end(),greater<int>());
        mx=max(mx,ve[0]);
        }
        c+=mx;
    }

    cout<<c;
    //cout<<((float)(clock()-t))/CLOCKS_PER_SEC;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;i++)
#define ll long long int
#define pb push_back
#define mp make_pair
vector<int> ve;
const int N = 105;
int vis[N+1];
vector<vector<int>> v(N+1);
void maxdep(vector<vector<int>> &tree, int node, int d){
    vis[node]=1;
    ve.push_back(d);
    forn(i,tree[node].size()){
        if(vis[tree[node][i]]==0) maxdep(tree,tree[node][i],d+1);
    }
}

int main(){
    //clock_t t;
    //t = clock();
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);

    int n;
    cin>>n;
    int arr[n];
    int c=0;
    for(int l=0;l<n;l++){
        int ni;
        cin>>ni;
        for (int i = 0; i < N+1; ++i) {
                    v[i].clear();
                }
        int p,q;
        for(int m=0;m<ni-1;m++){
            cin>>p>>q;
            v[p].push_back(q);
            v[q].push_back(p);
        }
        int mx=INT_MIN;
        for(int i=1;i<=ni;i++){
        ve.clear();
        int w=N;
        forn(j,w+1){
            vis[j]=0;
        }
        maxdep(v,i,0);

        sort(ve.begin(),ve.end(),greater<int>());
        mx=max(mx,ve[0]);
        }

        c+=mx;
    }

    cout<<c;
    //cout<<((float)(clock()-t))/CLOCKS_PER_SEC;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;i++)
#define ll long long int
#define pb push_back
#define mp make_pair
vector<int> ve;
const int N = 105;
int vis[N+1];
vector<vector<int>> v(N+1);
void maxdep(vector<vector<int>> &tree, int node, int d){
    vis[node]=1;
    ve.push_back(d);
    forn(i,tree[node].size()){
        if(vis[tree[node][i]]==0) maxdep(tree,tree[node][i],d+1);
    }
}

int main(){
    //clock_t t;
    //t = clock();
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);

    int n;
    cin>>n;
    int arr[n];
    int c=0;
    for(int l=0;l<n;l++){
        int ni;
        cin>>ni;
        for (int i = 0; i < N+1; ++i) {
                    v[i].clear();
                }
        int p,q;
        for(int m=0;m<ni-1;m++){
            cin>>p>>q;
            v[p].push_back(q);
            v[q].push_back(p);
        }
        int mx=INT_MIN;
        for(int i=1;i<=ni;i++){
        ve.clear();
        int w=N;
        forn(j,w+1){
            vis[j]=0;
        }
        maxdep(v,i,0);

        sort(ve.begin(),ve.end(),greater<int>());
        mx=max(mx,ve[0]);
        }

        c+=mx;
    }

    cout<<c;
    //cout<<((float)(clock()-t))/CLOCKS_PER_SEC;

    return 0;
}


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

vector<int> adj[10007], v;
bool vis[10007];
int con[10007];
int nxt,mx;

void dfs( int u, int cnt )
{
    vis[u]= 1;
    if( cnt>mx )
        mx= cnt, nxt= u;

    for( int i=0;i<adj[u].size();i++ )
    {
        int v= adj[u][i];

        if( vis[v] )
            continue;
        int now= cnt;

        if( con[u]!=v )
            now++;

        dfs(v,now);
    }
}

int main()
{
    freopen( "input.txt", "r", stdin );
    freopen( "output.txt", "w", stdout );

    int n;
    cin>>n;

    int add= 0;
    for(int j=0;j<n;j++)
    {
        int m;
        cin>>m;

        for( int i=1;i<m;i++ )
        {
            int p,q;
            cin>>p>>q;

            p+= add;
            q+= add;
            adj[p].push_back(q);
            adj[q].push_back(p);
        }

        add+= m;
    }

    int prev= -1;
    int first= 1;
    for(int i=1;i<=add;i++)
    {
        if( vis[i] )
            continue;

        mx= -1;
        dfs( i,0 );
        if( prev!=-1 )
            v.push_back( prev ),
            v.push_back( nxt );
        if(prev==-1)
            first= nxt;
        memset( vis, 0, sizeof(vis) );
        mx= -1;
        dfs( nxt,0 );
        prev= nxt;
    }

    for( int i=1;i<v.size();i+=2 )
        adj[ v[i-1] ].push_back( v[i] ),
        adj[ v[i] ].push_back( v[i-1] ),
        con[v[i-1]]= v[i],
        con[v[i]]= v[i-1];

    memset( vis,0,sizeof(vis) );
    mx= -1;
    dfs( first,0 );
    memset( vis,0,sizeof(vis) );
    mx= -1;
    dfs( nxt,0 );

    cout<< mx <<endl;
}


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

vector<int> adj[10007], v;
bool vis[10007];
int con[10007];
int nxt,mx;

void dfs( int u, int cnt )
{
    vis[u]= 1;
    if( cnt>mx )
        mx= cnt, nxt= u;

    for( int i=0;i<adj[u].size();i++ )
    {
        int v= adj[u][i];

        if( vis[v] )
            continue;
        int now= cnt;

        if( con[u]!=v )
            now++;

        dfs(v,now);
    }
}

int main()
{
    int n;
    cin>>n;

    int add= 0;
    for(int j=0;j<n;j++)
    {
        int m;
        cin>>m;

        for( int i=1;i<m;i++ )
        {
            int p,q;
            cin>>p>>q;

            p+= add;
            q+= add;
            adj[p].push_back(q);
            adj[q].push_back(p);
        }

        add+= m;
    }

    int prev= -1;
    int first= 1;
    for(int i=1;i<=add;i++)
    {
        if( vis[i] )
            continue;

        mx= -1;
        dfs( i,0 );
        if( prev!=-1 )
            v.push_back( prev ),
            v.push_back( nxt );
        if(prev==-1)
            first= nxt;
        memset( vis, 0, sizeof(vis) );
        mx= -1;
        dfs( nxt,0 );
        prev= nxt;
    }

    for( int i=1;i<v.size();i+=2 )
        adj[ v[i-1] ].push_back( v[i] ),
        adj[ v[i] ].push_back( v[i-1] ),
        con[v[i-1]]= v[i],
        con[v[i]]= v[i-1];

    memset( vis,0,sizeof(vis) );
    mx= -1;
    dfs( first,0 );
    memset( vis,0,sizeof(vis) );
    mx= -1;
    dfs( nxt,0 );

    cout<< mx <<endl;
}


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

vector<int> adj[10007], v;
bool vis[10007];
int con[10007];
int nxt,mx;

void dfs( int u, int cnt )
{
    vis[u]= 1;
    if( cnt>mx )
        mx= cnt, nxt= u;

    for( int i=0;i<adj[u].size();i++ )
    {
        int v= adj[u][i];

        if( vis[v] )
            continue;
        int now= cnt;

        if( con[u]!=v )
            now++;

        dfs(v,now);
    }
}

int main()
{
    freopen( "input.txt", "r", stdin );
    freopen( "output.txt", "w", stdout );

    int n;
    cin>>n;

    int add= 0;
    for(int j=0;j<n;j++)
    {
        int m;
        cin>>m;

        for( int i=1;i<m;i++ )
        {
            int p,q;
            cin>>p>>q;

            p+= add;
            q+= add;
            adj[p].push_back(q);
            adj[q].push_back(p);
        }

        add+= m;
    }

    int prev= -1;
    int first= 1;
    int ans= 0;

    for(int i=1;i<=add;i++)
    {
        if( vis[i] )
            continue;

        mx= -1;
        dfs( i,0 );
        if( prev!=-1 )
            v.push_back( prev ),
            v.push_back( nxt );
        if(prev==-1)
            first= nxt;
        memset( vis, 0, sizeof(vis) );
        mx= -1;
        dfs( nxt,0 );
        ans+= mx;
        prev= nxt;
    }

    cout<< ans <<endl;
}


#include<bits/stdc++.h>
using namespace std;
vector<int> graph[101];
void clear(){
	for(int node=1;node<=100;node++){
		graph[node].clear();
	}
}
void dfs1(int source,bool visited[],int path,int& start,int& max_path){
	visited[source]=true;
	path+=1;
	bool leaf=true;
	for(auto node:graph[source]){
		if(!visited[node]){
			leaf=false;
			dfs1(node,visited,path,start,max_path);
		}
	}
	if(leaf && path>max_path){
		max_path=path;
		start=source;
	}
}
void dfs2(int source,bool visited[],int path,int& max_path){
	visited[source]=true;
	path+=1;
	bool leaf=true;
	for(auto node:graph[source]){
		if(!visited[node]){
			leaf=false;
			dfs2(node,visited,path,max_path);
		}
	}
	if(leaf && path>max_path){
		max_path=path;
	}
}
int main(){
	int spiders;
	cin>>spiders;
	int length=0;
	while(spiders--){
		int n;
		cin>>n;
		for(int edge=1;edge<n;edge++){
			int u,v;
			cin>>u>>v;
			graph[u].push_back(v);
			graph[v].push_back(u);
		}
		int start,max_path=0;
		bool visited1[n+1]={false};
		dfs1(1,visited1,0,start,max_path);
		max_path=0;
		bool visited2[n+1]={false};
		dfs2(start,visited2,0,max_path);
		length+=(--max_path);
		clear();
	}
	cout<<length;
}


#include<vector>
#include<fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
vector<int> graph[101];
void clear(){
	for(int node=1;node<=100;node++){
		graph[node].clear();
	}
}
void dfs1(int source,bool visited[],int path,int& start,int& max_path){
	visited[source]=true;
	path+=1;
	bool leaf=true;
	for(auto node:graph[source]){
		if(!visited[node]){
			leaf=false;
			dfs1(node,visited,path,start,max_path);
		}
	}
	if(leaf && path>max_path){
		max_path=path;
		start=source;
	}
}
void dfs2(int source,bool visited[],int path,int& max_path){
	visited[source]=true;
	path+=1;
	bool leaf=true;
	for(auto node:graph[source]){
		if(!visited[node]){
			leaf=false;
			dfs2(node,visited,path,max_path);
		}
	}
	if(leaf && path>max_path){
		max_path=path;
	}
}
int main(){
	int spiders;
	cin>>spiders;
	int length=0;
	while(spiders--){
		int n;
		cin>>n;
		for(int edge=1;edge<n;edge++){
			int u,v;
			cin>>u>>v;
			graph[u].push_back(v);
			graph[v].push_back(u);
		}
		int start,max_path=0;
		bool visited1[n+1]={false};
		dfs1(1,visited1,0,start,max_path);
		max_path=0;
		bool visited2[n+1]={false};
		dfs2(start,visited2,0,max_path);
		length+=(--max_path);
		clear();
	}
	cout<<length;
}


#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
vector<int> graph[101];
void clear(){
	for(int node=1;node<=100;node++){
		graph[node].clear();
	}
}
void dfs1(int source,bool visited[],int path,int& start,int& max_path){
	visited[source]=true;
	path+=1;
	bool leaf=true;
	for(auto node:graph[source]){
		if(!visited[node]){
			leaf=false;
			dfs1(node,visited,path,start,max_path);
		}
	}
	if(leaf && path>max_path){
		max_path=path;
		start=source;
	}
}
void dfs2(int source,bool visited[],int path,int& max_path){
	visited[source]=true;
	path+=1;
	bool leaf=true;
	for(auto node:graph[source]){
		if(!visited[node]){
			leaf=false;
			dfs2(node,visited,path,max_path);
		}
	}
	if(leaf && path>max_path){
		max_path=path;
	}
}
int main(){
	int spiders;
	cin>>spiders;
	int length=0;
	while(spiders--){
		int n;
		cin>>n;
		for(int edge=1;edge<n;edge++){
			int u,v;
			cin>>u>>v;
			graph[u].push_back(v);
			graph[v].push_back(u);
		}
		int start,max_path=0;
		bool visited1[n+1]={false};
		dfs1(1,visited1,0,start,max_path);
		max_path=0;
		bool visited2[n+1]={false};
		dfs2(start,visited2,0,max_path);
		length+=(--max_path);
		clear();
	}
	cout<<length;
}


/*
	*	Created By: 'Present_Sir'
	*	Created On: Saturday 01 August 2020 08:03:35 PM IST
*/

#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL)
#define int long long
using namespace std;

void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cout << " " << to_string(H);
	debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define db(...)  cout << "[" << #__VA_ARGS__ << "]:" , debug_out(__VA_ARGS__)
#else
#define db(...)
#endif
const int N = 1e2+2;
vector<int> gr[N];

bool vis[N];

int dfs(int cur){
    vis[cur] = 1;
    int mx=0;
    for(auto x: gr[cur]){
        if(!vis[x]){
            mx = max(mx,dfs(x));
        }
    }
    return mx+1;
}

void solve(){
    int n;
    cin>>n;
    int ans=0;
    for(int t=0; t<n; t++){
        for(int i=0; i<N; i++){
            gr[i].clear();
        }

        int edg;
        cin>>edg;

        for(int i=0; i<edg-1; i++){
            int x,y;
            cin>>x>>y;
            gr[x].push_back(y);
            gr[y].push_back(x);
        }
        int length=0;
        for(int i=1; i<=edg; i++){
            memset(vis,0,sizeof(vis));
            length = max(length,dfs(i)-1);
        }
        ans+=length;
    }
    cout<<ans;
}

int32_t main(){
    IOS;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    //int t; cin>>t; while(t--)
    {
        solve();
    }
    return 0;
}


/*
	*	Created By: 'Present_Sir'
	*	Created On: Saturday 01 August 2020 08:03:35 PM IST
*/

#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL)
#define int long long
using namespace std;

void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cout << " " << to_string(H);
	debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define db(...)  cout << "[" << #__VA_ARGS__ << "]:" , debug_out(__VA_ARGS__)
#else
#define db(...)
#endif

const int N = 1e2+2;
vector<int> gr[N];

bool vis[N];
int mxlen=0;

void dfs(int cur,int len){

    vis[cur] = 1;
    mxlen = max(mxlen,len);
    for(auto x: gr[cur]){
        if(!vis[x]){
            dfs(x,len+1);
        }
    }

}

void solve(){
    int n;
    cin>>n;
    int ans=0;
    for(int t=0; t<n; t++){
        if(t!=0){
            ++ans;
        }
        for(int i=0; i<N; i++){
            gr[i].clear();
        }

        int edg;
        cin>>edg;

        for(int i=0; i<edg-1; i++){
            int x,y;
            cin>>x>>y;
            gr[x].push_back(y);
            gr[y].push_back(x);
        }
        memset(vis,0,sizeof(vis));
        mxlen=0;
        dfs(1,0);
        ans+=mxlen;
    }
    cout<<ans;
}

int32_t main(){
    IOS;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    //int t; cin>>t; while(t--)
    {
        solve();
    }
    return 0;
}


/*
	*	Created By: 'Present_Sir'
	*	Created On: Saturday 01 August 2020 08:03:35 PM IST
*/

#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL)
#define int long long
using namespace std;

void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cout << " " << to_string(H);
	debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define db(...)  cout << "[" << #__VA_ARGS__ << "]:" , debug_out(__VA_ARGS__)
#else
#define db(...)
#endif
const int N = 1e2+2;
vector<int> gr[N];

bool vis[N];

int dfs(int cur){
    vis[cur] = 1;
    int mx=0;
    for(auto x: gr[cur]){
        if(!vis[x]){
            mx = max(mx,dfs(x));
        }
    }
    return mx+1;
}

void solve(){
    int n;
    cin>>n;
    int ans=0;
    for(int t=0; t<n; t++){
        if(t>1){
            --ans;
        }
        for(int i=0; i<N; i++){
            gr[i].clear();
        }

        int edg;
        cin>>edg;

        for(int i=0; i<edg; i++){
            int x,y;
            cin>>x>>y;
            gr[x].push_back(y);
            gr[y].push_back(x);
        }
        int length=0;
        for(int i=1; i<=edg; i++){
            memset(vis,0,sizeof(vis));
            length = max(length,dfs(i));
        }
        ans+=length;
    }
    cout<<ans-1;
}

int32_t main(){
    IOS;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    //int t; cin>>t; while(t--)
    {
        solve();
    }
    return 0;
}


/*
	*	Created By: 'Present_Sir'
	*	Created On: Saturday 01 August 2020 08:03:35 PM IST
*/

#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL)
#define int long long
using namespace std;

void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cout << " " << to_string(H);
	debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define db(...)  cout << "[" << #__VA_ARGS__ << "]:" , debug_out(__VA_ARGS__)
#else
#define db(...)
#endif
const int N = 1e2+2;
vector<int> gr[N];

bool vis[N];

int dfs(int cur){
    vis[cur] = 1;
    int mx=0;
    for(auto x: gr[cur]){
        if(!vis[x]){
            mx = max(mx,dfs(x));
        }
    }
    return mx+1;
}

void solve(){
    int n;
    cin>>n;
    int ans=0;
    for(int t=0; t<n; t++){
        for(int i=0; i<N; i++){
            gr[i].clear();
        }

        int edg;
        cin>>edg;

        for(int i=0; i<edg; i++){
            int x,y;
            cin>>x>>y;
            gr[x].push_back(y);
            gr[y].push_back(x);
        }
        int length=0;
        for(int i=1; i<=edg; i++){
            memset(vis,0,sizeof(vis));
            length = max(length,dfs(i));
        }
        ans+=length;
    }
    cout<<ans-1;
}

int32_t main(){
    IOS;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    //int t; cin>>t; while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define lli long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define f first
#define s second
using namespace std;
bool visited[10006];
int x,n,maxCount;
void dfs_rec(int node, int count,vector<int>g[],int n)
{
    visited[node] = true;
    count++;
    for (int i = 0; i < g[node].size(); i++) {
        if (!visited[g[node][i]])
        {
            if (count >= maxCount)
            {
                maxCount = count;
                x = g[node][i];
            }
            dfs_rec(g[node][i], count,g,n);
        }
    }
}
void dfs(int node,vector<int>v[],int n)
{
    int count=0;
    for (int i = 0; i < n; ++i)
        visited[i] = false;
    dfs_rec(node, count,v,n);
}
int longest_path(int n, vector<int>v[])
{
    maxCount = INT_MIN;
    dfs(0,v,n);
    dfs(x,v,n);

    return maxCount;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int q,ans=0;
    cin>>q;
    while(q--)
    {
        int n,a,b;
        cin>>n;
        vector<int>v[n];
        for(int i=0;i<n-1;i++)
        {
            cin>>a>>b;
            v[a-1].pb(b-1);
            v[b-1].pb(a-1);
        }
        //cout<<longest_path(n,v)<<endl;
        ans+=longest_path(n,v);
        //cout<<max1<<" ";
    }
    //cout<<endl;
    cout<<ans;
    return 0;
}
    //----RC----//
//---BELIEVE THAT---//


#include<bits/stdc++.h>
#define inf 0x3f3f
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define lli long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define f first
#define s second
using namespace std;
int x=0;
void dfsi(int i,vector<int> v[],vector<bool> &visited,int m,int &max1)
{
   // cout<<"---"<<i<<endl;
    m++;
    //cout<<m<<endl;
    visited[i]=true;
    for(int j=0;j<v[i].size();j++)
    {
        if(visited[v[i][j]]==false)
        {
            if (m >= max1) {
                max1 = m;
                x = j;
            }
            dfsi(v[i][j],v,visited,m,max1);
        }

    }
}
void dfs(int node,vector<int>v[],int n,int &max1)
{
    vector<bool>visited(n+1,false);
    int m=0;
    dfsi(node,v,visited,m,max1);
}
int main()
{
    fast;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int q,ans=0;
    cin>>q;
    while(q--)
    {
        int n,a,b;
        cin>>n;
        vector<int>v[n];
        for(int i=0;i<n-1;i++)
        {
            cin>>a>>b;
            v[a-1].pb(b-1);
            v[b-1].pb(a-1);
        }
        int max1=-1;
        dfs(0,v,n,max1);
        dfs(x,v,n,max1);
        ans+=max1;
        //cout<<max1;
    }
    cout<<ans;
    return 0;
}
    //----RC----//
//---BELIEVE THAT---//


#include<bits/stdc++.h>
#define inf 0x3f3f
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define lli long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define f first
#define s second
using namespace std;
bool visited[10006];
int x,n,maxCount;
void dfs_rec(int node, int count,vector<int>g[],int n)
{
    visited[node] = true;
    count++;
    for (int i = 0; i < g[node].size(); i++) {
        if (!visited[g[node][i]])
        {
            if (count >= maxCount)
            {
                maxCount = count;
                x = g[node][i];
            }
            dfs_rec(g[node][i], count,g,n);
        }
    }
}
void dfs(int node,vector<int>v[],int n)
{
    int count=0;
    for (int i = 0; i < n; ++i)
        visited[i] = false;
    dfs_rec(node, count,v,n);
}
int longest_path(int n, vector<int>v[])
{
    maxCount = INT_MIN;
    dfs(0,v,n);
    dfs(x,v,n);

    return maxCount;
}
int main()
{
    fast;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int q,ans=0;
    cin>>q;
    while(q--)
    {
        int n,a,b;
        cin>>n;
        vector<int>v[n];
        for(int i=0;i<n-1;i++)
        {
            cin>>a>>b;
            v[a-1].pb(b-1);
            v[b-1].pb(a-1);
        }
        //cout<<longest_path(n,v)<<endl;
        ans+=longest_path(n,v);
        //cout<<max1<<" ";
    }
    //cout<<endl;
    cout<<ans;
    return 0;
}
    //----RC----//
//---BELIEVE THAT---//


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

int ans,mix,d[1005],qf,a[105];

vector <int> v[105][105];

queue <int> q;

void bfs(int b,int o, int ind){
   q.push(b);
   //cout << b << endl;
   while(q.size()){
      qf = q.front();
     // cout << v[ind][qf].size() << " ";
      for(int i = 0; i < v[ind][qf].size(); i++){
          if(d[v[ind][qf][i]] > d[qf] + 1){
            d[v[ind][qf][i]] = d[qf] + 1;
            q.push(v[ind][qf][i]);
          }
      }
      q.pop();
   }
   //cout << endl;
   for(int i = 1; i <= o; i++){
      mix = max(mix,d[i]);
     // cout << d[i] << " ";
   }
   //cout << endl;
   //cout << endl;
}

int n,f,s;

int main(){
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
   cin >> n;
   for(int i = 0; i < n; i++){
       cin >> a[i];
       for(int j = 0; j < a[i] - 1; j++){
           cin >> f >> s;
           v[i][f].push_back(s);
           v[i][s].push_back(f);
       }
   }
   for(int i = 0; i < n; i++){
       mix = 0;
       for(int j = 1; j <= a[i]; j++){
           for(int k = 0; k <= a[i]; k++){
               d[k] = INT_MAX;
           }
           d[j] = 0;
           bfs(j,a[i],i);
           //cout << mix << endl;
       }
       //cout << mix << endl;
       ans += mix;
   }
   cout << ans;
}


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

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);

int ans,mix,d[1005],qf,a[105];

vector <int> v[105][105];

queue <int> q;

void bfs(int b,int o, int ind){
   q.push(b);
   //cout << b << endl;
   while(q.size()){
      qf = q.front();
     // cout << v[ind][qf].size() << " ";
      for(int i = 0; i < v[ind][qf].size(); i++){
          if(d[v[ind][qf][i]] > d[qf] + 1){
            d[v[ind][qf][i]] = d[qf] + 1;
            q.push(v[ind][qf][i]);
          }
      }
      q.pop();
   }
   //cout << endl;
   for(int i = 1; i <= o; i++){
      mix = max(mix,d[i]);
     // cout << d[i] << " ";
   }
   //cout << endl;
   //cout << endl;
}

int n,f,s;

int main(){
   cin >> n;
   for(int i = 0; i < n; i++){
       cin >> a[i];
       for(int j = 0; j < a[i] - 1; j++){
           cin >> f >> s;
           v[i][f].push_back(s);
           v[i][s].push_back(f);
       }
   }
   for(int i = 0; i < n; i++){
       mix = 0;
       for(int j = 1; j <= a[i]; j++){
           for(int k = 0; k <= a[i]; k++){
               d[k] = INT_MAX;
           }
           d[j] = 0;
           bfs(j,a[i],i);
           //cout << mix << endl;
       }
       //cout << mix << endl;
       ans += mix;
   }
   cout << ans;
}


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

int ans,mix,d[1005],qf,a[105];

vector <int> v[105][105];

queue <int> q;

void bfs(int b,int o, int ind){
   q.push(b);
   //cout << b << endl;
   while(q.size()){
      qf = q.front();
     // cout << v[ind][qf].size() << " ";
      for(int i = 0; i < v[ind][qf].size(); i++){
          if(d[v[ind][qf][i]] > d[qf] + 1){
            d[v[ind][qf][i]] = d[qf] + 1;
            q.push(v[ind][qf][i]);
          }
      }
      q.pop();
   }
   //cout << endl;
   for(int i = 1; i <= o; i++){
      mix = max(mix,d[i]);
     // cout << d[i] << " ";
   }
   //cout << endl;
   //cout << endl;
}

int n,f,s;

int main(){
   cin >> n;
   for(int i = 0; i < n; i++){
       cin >> a[i];
       for(int j = 0; j < a[i] - 1; j++){
           cin >> f >> s;
           v[i][f].push_back(s);
           v[i][s].push_back(f);
       }
   }
   for(int i = 0; i < n; i++){
       mix = 0;
       for(int j = 1; j <= a[i]; j++){
           for(int k = 0; k <= a[i]; k++){
               d[k] = INT_MAX;
           }
           d[j] = 0;
           bfs(j,a[i],i);
           //cout << mix << endl;
       }
       //cout << mix << endl;
       ans += mix;
   }
   cout << ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 5;
const int mod = 1e9 + 7;
int n, t, ans, dep, mx;
vector<int> adj[N];
void dfs(int start, int par = -1, int h = 0){
	bool ok = false;
	for(auto v : adj[start]){
		if(v != par){
			dfs(v, start, h + 1);
			ok = true;
		}
	}
	if(!ok){
		if(h > mx){
			mx = h;
			dep = start;
		}
	}
}
int dfs1(int start, int par = -1){
	int ans = 0;
	for(auto v : adj[start]){
		if(v != par){
			ans = max(ans, dfs1(v, start) + 1);
		}
	}
	return ans;
}
int32_t main(){
	freopen("input.txt" , "r" , stdin);
  freopen("output.txt" , "w" , stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 0; i < N; i++){
			adj[i].clear();
		}
		for(int i = 1; i < n; i++){
			int a, b;
			cin >> a >> b;
			a--; b--;
			adj[a].push_back(b);
			adj[b].push_back(a);
		}
		dep = 0;
		mx = 0;
		dfs(0);
		ans += dfs1(dep);
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 5;
const int mod = 1e9 + 7;
int n, t, ans, dep;
vector<int> adj[N];
void dfs(int start, int par){
	bool ok = false;
	for(auto v : adj[start]){
		if(v != par){
			dfs(v, start);
			ok = true;
		}
	}
	if(!ok){
		dep = start;
	}
}
int dfs1(int start, int par){
	int ans = 0;
	for(auto v : adj[start]){
		if(v != par){
			ans = max(ans, dfs1(v, start) + 1);
		}
	}
	return ans;
}
int32_t main(){
	freopen("input.txt" , "r" , stdin);
  freopen("output.txt" , "w" , stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 0; i < N; i++){
			adj[i].clear();
		}
		for(int i = 1; i < n; i++){
			int a, b;
			cin >> a >> b;
			a--; b--;
			adj[a].push_back(b);
			adj[b].push_back(a);
		}
		dep = 0;
		dfs(0, -1);
		ans += dfs1(dep, -1);
	}
	cout << ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e2 + 5;
const int mod = 1e9 + 7;
int n, t, ans, dep, mx;
vector<int> adj[N];
void dfs(int start, int par = -1, int h = 0){
	bool ok = false;
	for(auto v : adj[start]){
		if(v != par){
			dfs(v, start, h + 1);
			ok = true;
		}
	}
	if(!ok){
		if(h > mx){
			mx = h;
			dep = start;
		}
	}
}
int dfs1(int start, int par = -1){
	int ans = 0;
	for(auto v : adj[start]){
		if(v != par){
			ans = max(ans, dfs1(v, start) + 1);
		}
	}
	return ans;
}
int32_t main(){
	//freopen("input.txt" , "r" , stdin);
  //freopen("output.txt" , "w" , stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> t;
	while(t--){
		cin >> n;
		for(int i = 0; i < N; i++){
			adj[i].clear();
		}
		for(int i = 1; i < n; i++){
			int a, b;
			cin >> a >> b;
			a--; b--;
			adj[a].push_back(b);
			adj[b].push_back(a);
		}
		dep = 0;
		mx = 0;
		dfs(0);
		ans += dfs1(dep);
	}
	cout << ans;
}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w+", stdout);
    int t, n, a, b;
    cin >> t;
    long long sum = 0;
    while (t--) {
        cin >> n;
        int g[n][n];
        memset(g, 0x3f, sizeof(g));
        for (int i = 0; i < n; i++) {
            g[i][i] = 0;
        }
        for (int i = 0; i < n - 1; i++) {
            cin >> a >> b;
            a--; b--;
            g[a][b] = 1;
            g[b][a] = 1;
        }
        int max_dist = 0;
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (g[i][j] > g[i][k] + g[k][j]) {
                        g[i][j] = g[i][k] + g[k][j];
                        if (g[i][j] < 0x3f3f3f3f) {
                            max_dist = max(max_dist, g[i][j]);
                        }
                    }
                }
            }
        }
        sum += max_dist;
    }
    cout << sum;
    return 0;
}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w+", stdout);
    int t, n, a, b;
    cin >> t;
    long long sum = 0;
    while (t--) {
        cin >> n;
        int g[n][n];
        memset(g, 0x3f, sizeof(g));
        for (int i = 0; i < n; i++) {
            g[i][i] = 0;
        }
        for (int i = 0; i < n - 1; i++) {
            cin >> a >> b;
            a--; b--;
            g[a][b] = 1;
            g[b][a] = 1;
        }
        int max_dist = 1;
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (g[i][j] > g[i][k] + g[k][j]) {
                        g[i][j] = g[i][k] + g[k][j];
                        if (g[i][j] < 0x3f3f3f3f) {
                            max_dist = max(max_dist, g[i][j]);
                        }
                    }
                }
            }
        }
        sum += max_dist;
    }
    cout << sum;
    return 0;
}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w+", stdout);
    int t, n, a, b;
    cin >> t;
    long long sum = 0;
    while (t--) {
        cin >> n;
        int g[n][n];
        memset(g, 0x3f, sizeof(g));
        for (int i = 0; i < n - 1; i++) {
            cin >> a >> b;
            a--; b--;
            g[a][b] = 1;
        }
        int max_dist = 0;
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (g[i][j] > g[i][k] + g[k][j]) {
                        g[i][j] = g[i][k] + g[k][j];
                        if (g[i][j] < 0x3f3f3f3f) {
                            max_dist = max(max_dist, g[i][j]);
                        }
                    }
                }
            }
        }
        sum += max_dist;
    }
    cout << sum;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
ll vis[100000];
vector<ll>adj[100000];
ll ans;
ll maxi = -1;
void dfs(ll x, ll lvl)
{
    if(vis[x])
    return;
    vis[x] = 1;
    if(lvl > maxi)
    maxi = lvl;

    for(auto i:adj[x])
    {
        dfs(i,lvl+1);
    }
}
int main()
{
    IOS

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    ll t;
    //cin >> t;
    while(t--)
    {
        ll n, i;
        //cin >> n;
        maxi = -1;
        for(i = 1; i <= n; i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(i = 0; i < n-1; i++)
        {
            ll a,b;
            //cin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        ll x;
        for( i = 1; i <= n; i++)
        {
            if(adj[i].size() == 1)
            {
                x = i;
                break;
            }
        }
        dfs(x, 0);
        ans += maxi;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
ll vis[100000];
vector<ll>adj[100000];
ll ans;
ll maxi = -1;
void dfs(ll x, ll lvl)
{
    if(vis[x])
    return;
    vis[x] = 1;
    if(lvl > maxi)
    maxi = lvl;

    for(auto i:adj[x])
    {
        dfs(i,lvl+1);
    }
}
int main()
{
    IOS

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    ll t;
    cin >> t;
    //cout << t << endl;
    while(t--)
    {
        ll n, i;
        cin >> n;
        maxi = -1;
        for(i = 1; i <= n; i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(i = 0; i < n-1; i++)
        {
            ll a,b;
            cin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        ll x;
        for( i = 1; i <= n; i++)
        {
            if(adj[i].size() == 1)
            {
                x = i;
                break;
            }
        }
        dfs(x, 0);
        ans += maxi;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
ll vis[100000];
vector<ll>adj[100000];
ll ans;
ll maxi = -1;
void dfs(ll x, ll lvl)
{
    if(vis[x])
    return;
    vis[x] = 1;
    if(lvl > maxi)
    maxi = lvl;

    for(auto i:adj[x])
    {
        dfs(i,lvl+1);
    }
}
int main()
{
    IOS

    freopen("inp.txt","r",stdin);
    freopen("out.txt","w",stdout);
    ll t;

    while(t--)
    {
        ll n, i;
        maxi = -1;
        for(i = 1; i <= n; i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(i = 0; i < n-1; i++)
        {
            ll a,b;
         //   cin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        ll x;
        for( i = 1; i <= n; i++)
        {
            if(adj[i].size() == 1)
            {
                x = i;
                break;
            }
        }
        dfs(x, 0);
        ans += maxi;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
ll vis[100000];
vector<ll>adj[100000];
ll ans;
ll maxi = -1;
void dfs(ll x, ll lvl)
{
    if(vis[x])
    return;
    vis[x] = 1;
    if(lvl > maxi)
    maxi = lvl;

    for(auto i:adj[x])
    {
        dfs(i,lvl+1);
    }
}
int main()
{
    IOS

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    ll t;
    cin >> t;
    //cout << t << endl;
    while(t--)
    {
        ll n, i;
        cin >> n;
        maxi = -1;
        for(i = 1; i <= n; i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(i = 0; i < n-1; i++)
        {
            ll a,b;
            cin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        ll x;
        for( i = 1; i <= n; i++)
        {
            if(adj[i].size() == 1)
            {
                for(ll j = 1; j <= n; j++)
                vis[j] = 0;
                dfs(i, 0);
            }
        }
        ans += maxi;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
ll vis[100000];
vector<ll>adj[100000];
ll ans;
ll maxi = -1;
void dfs(ll x, ll lvl)
{
    if(vis[x])
    return;
    vis[x] = 1;
    if(lvl > maxi)
    maxi = lvl;

    for(auto i:adj[x])
    {
        dfs(i,lvl+1);
    }
}
int main()
{
    IOS

    ll t;
    cin >> t;

    while(t--)
    {
        ll n, i;
        cin >> n;
        maxi = -1;
        for(i = 1; i <= n; i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(i = 0; i < n-1; i++)
        {
            ll a,b;
            cin >> a >> b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
        ll x;
        for( i = 1; i <= n; i++)
        {
            if(adj[i].size() == 1)
            {
                x = i;
                break;
            }
        }
        dfs(x, 0);
        ans += maxi;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
int leaf=0;int mx=INT_MIN;
int depth=0;
void dfs(vector<int>adj[],int u,vector<bool>&vis,vector<int>&par)
{
    if(depth>mx)leaf=u;
    if(vis[u]==true)return ;
    vis[u]=true;
   // par[u]=depth;
    for(auto i:adj[u])
    {
        if(vis[i]==false){par[i]=par[u]+1;
            dfs(adj,i,vis,par);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    int n=0;
    cin>>n;
    int cnt=0;
    //cout<<n<<endl;
    for(int i=0;i<n;i++)
    {
        int k;cin>>k;int sz=k;
        int root=0;k--;
        vector<int>adj[sz+1];
        while(k--)
        {
            int x,y;cin>>x>>y;
          //  cout<<x<<" "<<y<<endl;
            adj[x].push_back(y);adj[y].push_back(x);
            root=x;
        }
      //  cout<<"root"<<root<<endl;
        vector<bool>vis(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par(sz+1,0);par[root]=0;
        dfs(adj,root,vis,par);
      //  cout<<par[leaf]<<endl;
     //   cout<<"leaf"<<leaf<<endl;
        vector<bool>vis2(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par2(sz+1,0);par2[leaf]=0;
        dfs(adj,leaf,vis2,par2);
       // cout<<leaf<<endl;
        auto k2 = *max_element(par2.begin(),par2.end());
     //   cout<<k2<<endl;
        cnt +=k2;
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int leaf=0;int mx=INT_MIN;
int depth=0;
void dfs(vector<int>adj[],int u,vector<bool>&vis,vector<int>&par)
{
    if(depth>mx)leaf=u;
    if(vis[u]==true)return ;
    vis[u]=true;
   // par[u]=depth;
    for(auto i:adj[u])
    {
        if(vis[i]==false){par[i]=par[u]+1;
            dfs(adj,i,vis,par);
        }
    }
}
int main()
{
    int n=0, cnt=0;;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int k;cin>>k;int sz=k;
        int root=0;k--;
        vector<int>adj[sz+1];
        while(k--)
        {
            int x,y;
            cin>>x>>y;
            adj[x].push_back(y);adj[y].push_back(x);
            root=x;
        }
        vector<bool>vis(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par(sz+1,0);par[root]=0;
        dfs(adj,root,vis,par);
        vector<bool>vis2(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par2(sz+1,0);par2[leaf]=0;
        dfs(adj,leaf,vis2,par2);
        auto k2 = *max_element(par2.begin(),par2.end());
        cnt +=k2;
    }
    cout<<cnt<<endl;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int leaf=0;int mx=INT_MIN;
int depth=0;
void dfs(vector<int>adj[],int u,vector<bool>&vis,vector<int>&par)
{
    if(depth>mx)leaf=u;
    if(vis[u]==true)return ;
    vis[u]=true;
   // par[u]=depth;
    for(auto i:adj[u])
    {
        if(vis[i]==false){par[i]=par[u]+1;
            dfs(adj,i,vis,par);
        }
    }
}
int main()
{
    int n=0;
    cin>>n;
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        int k;
        cin>>k;
        int sz=k;
        int root=0;
        k--;
        vector<int>adj[sz+1];
        while(k--)
        {
            int x,y;cin>>x>>y;
            adj[x].push_back(y);adj[y].push_back(x);
            root=x;
        }
        vector<bool>vis(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par(sz+1,0);par[root]=0;
        dfs(adj,root,vis,par);
        vector<bool>vis2(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par2(sz+1,0);par2[leaf]=0;
        dfs(adj,leaf,vis2,par2);
        auto k2 = *max_element(par2.begin(),par2.end());
        cnt +=k2;
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int leaf=0;int mx=INT_MIN;
int depth=0;
void dfs(vector<int>adj[],int u,vector<bool>&vis,vector<int>&par)
{
    if(depth>mx)leaf=u;
    if(vis[u]==true)return ;
    vis[u]=true;
   // par[u]=depth;
    for(auto i:adj[u])
    {
        if(vis[i]==false){par[i]=par[u]+1;
            dfs(adj,i,vis,par);
        }
    }
}
int main()
{
    int n;cin>>n;int cnt=0;
    //cout<<n<<endl;
    for(int i=0;i<n;i++)
    {
        int k;cin>>k;int sz=k;
        int root=0;k--;
        vector<int>adj[sz+1];
        while(k--)
        {
            int x,y;cin>>x>>y;
          //  cout<<x<<" "<<y<<endl;
            adj[x].push_back(y);adj[y].push_back(x);
            root=x;
        }
      //  cout<<"root"<<root<<endl;
        vector<bool>vis(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par(sz+1,0);par[root]=0;
        dfs(adj,root,vis,par);
      //  cout<<par[leaf]<<endl;
     //   cout<<"leaf"<<leaf<<endl;
        vector<bool>vis2(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par2(sz+1,0);par2[leaf]=0;
        dfs(adj,leaf,vis2,par2);
       // cout<<leaf<<endl;
        auto k2 = *max_element(par2.begin(),par2.end());
     //   cout<<k2<<endl;
        cnt +=k2;
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
int leaf=0;int mx=INT_MIN;
int depth=0;
void dfs(vector<int>adj[],int u,vector<bool>&vis,vector<int>&par)
{
    if(depth>mx)leaf=u;
    if(vis[u]==true)return ;
    vis[u]=true;
   // par[u]=depth;
    for(auto i:adj[u])
    {
        if(vis[i]==false){par[i]=par[u]+1;
            dfs(adj,i,vis,par);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;cin>>n;int cnt=0;
    for(int i=0;i<n;i++)
    {
        int k;cin>>k;int sz=k;
        int root=0;k--;
        vector<int>adj[sz+1];
        while(k--)
        {
            int x,y;cin>>x>>y;
            adj[x].push_back(y);adj[y].push_back(x);
            root=x;
        }
        vector<bool>vis(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par(sz+1,0);par[root]=0;
        dfs(adj,root,vis,par);
        auto k1 = *max_element(par.begin(), par.end());
        for(int i=1;i<=sz;i++)if(par[i]==k1){leaf=i;break;}
        vector<bool>vis2(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par2(sz+1,0);par2[leaf]=0;
        dfs(adj,leaf,vis2,par2);
        auto k2 = *max_element(par2.begin(),par2.end());
        cnt +=k2;
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int leaf=0;int mx=INT_MIN;
int depth=0;
void dfs(vector<int>adj[],int u,vector<bool>&vis,vector<int>&par)
{
    if(depth>mx)leaf=u;
    if(vis[u]==true)return ;
    vis[u]=true;
   // par[u]=depth;
    for(auto i:adj[u])
    {
        if(vis[i]==false){par[i]=par[u]+1;
            dfs(adj,i,vis,par);
        }
    }
}
int main()
{
    int n;
    cin>>n;
    int cnt=0;
    //cout<<n<<endl;
    for(int i=0;i<n;i++)
    {
        int k;cin>>k;int sz=k;
        int root=0;k--;
        vector<int>adj[sz+1];
        while(k--)
        {
            int x,y;cin>>x>>y;
          //  cout<<x<<" "<<y<<endl;
            adj[x].push_back(y);adj[y].push_back(x);
            root=x;
        }
      //  cout<<"root"<<root<<endl;
        vector<bool>vis(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par(sz+1,0);par[root]=0;
        dfs(adj,root,vis,par);
      //  cout<<par[leaf]<<endl;
     //   cout<<"leaf"<<leaf<<endl;
        vector<bool>vis2(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par2(sz+1,0);par2[leaf]=0;
        dfs(adj,leaf,vis2,par2);
       // cout<<leaf<<endl;
        auto k2 = *max_element(par2.begin(),par2.end());
     //   cout<<k2<<endl;
        cnt +=k2;
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
int leaf=0;int mx=INT_MIN;
int depth=0;
void dfs(vector<int>adj[],int u,vector<bool>&vis,vector<int>&par)
{
    if(depth>mx)leaf=u;
    if(vis[u]==true)return ;
    vis[u]=true;
   // par[u]=depth;
    for(auto i:adj[u])
    {
        if(vis[i]==false){par[i]=par[u]+1;
            dfs(adj,i,vis,par);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;cin>>n;int cnt=0;
    //cout<<n<<endl;
    for(int i=0;i<n;i++)
    {
        int k;cin>>k;int sz=k;
        int root=0;k--;
        vector<int>adj[sz+1];
        while(k--)
        {
            int x,y;cin>>x>>y;
          //  cout<<x<<" "<<y<<endl;
            adj[x].push_back(y);adj[y].push_back(x);
            root=x;
        }
      //  cout<<"root"<<root<<endl;
        vector<bool>vis(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par(sz+1,0);par[root]=0;
        dfs(adj,root,vis,par);
      //  cout<<par[leaf]<<endl;
     //   cout<<"leaf"<<leaf<<endl;
        vector<bool>vis2(sz+1,false);vis[0]=true;mx=INT_MIN;depth=0;
        vector<int>par2(sz+1,0);par2[leaf]=0;
        dfs(adj,leaf,vis2,par2);
       // cout<<leaf<<endl;
        auto k2 = *max_element(par2.begin(),par2.end());
     //   cout<<k2<<endl;
        cnt +=k2;
    }
    cout<<cnt<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define MOD 998244353
using namespace std;
using ll=long long;

ll hi=-1,v=1;

void dfs(vector<ll> adj[],ll s,ll p,ll depth)
{
	if(depth>hi)
	{
		hi=depth;
		v=s;
	}

	for(int i=0;i<adj[s].size();i++)
	{
		if(adj[s][i]!=p)
		{
			dfs(adj,adj[s][i],s,depth+1);
		}
	}
}

main()
{
	//freopen("input.txt","r",stdin);

	ll t,n,ans=0,x,y;
	cin>>t;

	//for(ll c=0;c<t;c++)
	while(t--)
	{
		cin>>n;
		hi=0,v=0;

		vector<ll> adj[n+1];

		for(ll i=0;i<n-1;i++)
		{
			cin>>x>>y;

			adj[x].push_back(y);
			adj[y].push_back(x);
		}

		dfs(adj,1,-1,0);
		hi=-1;
		dfs(adj,v,-1,0);

		ans+=hi;
	}

	cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define MOD 998244353
using namespace std;
using ll=long long;

ll hi=-1,v=1;

void dfs(vector<ll> adj[],ll s,ll p,ll depth)
{
	if(depth>hi)
	{
		hi=depth;
		v=s;
	}

	for(int i=0;i<adj[s].size();i++)
	{
		if(adj[s][i]!=p)
		{
			dfs(adj,adj[s][i],s,depth+1);
		}
	}
}

main()
{
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);

	ll t,n,ans=0,x,y;
	cin>>t;

	for(ll c=0;c<t;c++)
	//while(t--)
	{
		cin>>n;
		hi=0,v=0;

		vector<ll> adj[n+1];

		for(ll i=0;i<n-1;i++)
		{
			cin>>x>>y;

			adj[x].push_back(y);
			adj[y].push_back(x);
		}

		dfs(adj,1,-1,0);
		hi=-1;
		dfs(adj,v,-1,0);

		ans+=hi;
	}

	cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define MOD 998244353
using namespace std;
using ll=long long;

ll hi=-1,v=1;

void dfs(vector<ll> adj[],ll s,ll p,ll depth)
{
	if(depth>hi)
	{
		hi=depth;
		v=s;
	}

	for(int i=0;i<adj[s].size();i++)
	{
		if(adj[s][i]!=p)
		{
			dfs(adj,adj[s][i],s,depth+1);
		}
	}
}

main()
{
	//freopen("input.txt","r",stdin);

	ll t,n,ans=0,x,y;
	cin>>t;

	for(ll c=0;c<t;c++)
	{
		cin>>n;
		hi=0,v=0;

		vector<ll> adj[n+1];

		for(ll i=0;i<n-1;i++)
		{
			cin>>x>>y;

			adj[x].push_back(y);
			adj[y].push_back(x);
		}

		dfs(adj,1,-1,0);
		hi=-1;
		dfs(adj,v,-1,0);

		ans+=hi;
	}

	cout<<ans<<endl;

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define endl '\n'
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define Fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const int M =1e9+7;

class graph
{
    public:
    vector<int> *g;
    int n;
    graph(int v)
    {
        n=v;
        g= new vector<int>[n];
    }
    void addedge(int u,int v)
    {
        g[u].pb(v);
        g[v].pb(u);
    }
    int BFS(int u,int par[])
    {
    	queue<int> q;
    	map<int,int> m;
    	int v;
    	q.push(u);
    	while(!q.empty())
    	{
    		int x=q.front();
    		q.pop();
    		if(m[x]==0)
    		{
    			m[x]=1;
    			v=x;
    			for(int i=0;i<g[x].size();i++)
    				{
    					if(m[g[x][i]]==0)
    					{
    						q.push(g[x][i]);
    						par[g[x][i]]=x;
    					}
    				}
    		}
    	}
    	return v;
    }
};
void solve()
{
    int n;
    cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
    	int x;
    	cin>>x;
    	if(n==1&&x==3)
    	{
    		cout<<"2"<<endl;
    		return;
    	}
    	graph G(x+1);
    	for(int j=1;j<=x-1;j++)
    	{
    		int u,v;
    		cin>>u>>v;
    		G.addedge(u,v);
    	}
    	int par1[x+1];
    	par1[1]=-1;
      int u=G.BFS(1,par1);
      int par[x+1];
      par[u]=-1;
      int v=G.BFS(u,par);
      int count=-1;
      while(v!=-1)
      {
      	count++;
      	v=par[v];
      }
      ans+=count;
    }
    cout<<ans<<endl;
}
signed main()
{
    Fast;

    solve();

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define endl '\n'
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define Fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const int M =1e9+7;

class graph
{
    public:
    vector<int> *g;
    int n;
    graph(int v)
    {
        n=v;
        g= new vector<int>[n];
    }
    void addedge(int u,int v)
    {
        g[u].pb(v);
        g[v].pb(u);
    }
    int BFS(int u,int par[])
    {
    	queue<int> q;
    	map<int,int> m;
    	vector<int> v;
    	q.push(u);
    	while(!q.empty())
    	{
    		int x=q.front();
    		q.pop();
    		if(m[x]==0)
    		{
    			m[x]=1;
    			v.pb(x);
    			for(int i=0;i<g[x].size();i++)
    				{
    					if(m[g[x][i]]==0)
    					{
    						q.push(g[x][i]);
    						par[g[x][i]]=x;
    					}
    				}
    		}
    	}
    	return v[v.size()-1];
    }
};
void solve()
{
    int n;
    cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
    	int x;
    	cin>>x;
    	graph G(x+1);
    	for(int j=1;j<=x-1;j++)
    	{
    		int u,v;
    		cin>>u>>v;
    		G.addedge(u,v);
    	}
    	int par1[n+1];
    	par1[1]=-1;
      int u=G.BFS(1,par1);
      int par[n+1];
      par[u]=-1;
      int v=G.BFS(u,par);
      int count=-1;
      while(v!=-1)
      {
      	count++;
      	v=par[v];
      }
      ans+=count;
    }
    cout<<ans<<endl;
}
signed main()
{
    Fast;

    solve();

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define endl '\n'
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define Fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const int M =1e9+7;

class graph
{
    public:
    vector<int> *g;
    int n;
    graph(int v)
    {
        n=v;
        g= new vector<int>[n];
    }
    void addedge(int u,int v)
    {
        g[u].pb(v);
        g[v].pb(u);
    }
    int BFS(int u,int par[])
    {
    	queue<int> q;
    	map<int,int> m;
    	int v;
    	q.push(u);
    	while(!q.empty())
    	{
    		int x=q.front();
    		q.pop();
    		if(m[x]==0)
    		{
    			m[x]=1;
    			v=x;
    			for(int i=0;i<g[x].size();i++)
    				{
    					if(m[g[x][i]]==0)
    					{
    						q.push(g[x][i]);
    						par[g[x][i]]=x;
    					}
    				}
    		}
    	}
    	return v;
    }
};
void solve()
{
    int n;
    cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
    	int x;
    	cin>>x;
    	graph G(x+1);
    	for(int j=1;j<=x-1;j++)
    	{
    		int u,v;
    		cin>>u>>v;
    		G.addedge(u,v);
    	}
    	int par1[x+1];
    	par1[1]=-1;
      int u=G.BFS(1,par1);
      int par[x+1];
      par[u]=-1;
      int v=G.BFS(u,par);
      int count=-1;
      while(v!=-1)
      {
      	count++;
      	v=par[v];
      }
      ans+=count;
    }
    cout<<ans<<endl;
}
signed main()
{
    Fast;

    solve();

 return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define endl '\n'
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define Fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const int M =1e9+7;

class graph
{
    public:
    vector<int> *g;
    int n;
    graph(int v)
    {
        n=v;
        g= new vector<int>[n];
    }
    void addedge(int u,int v)
    {
        g[u].pb(v);
        g[v].pb(u);
    }
    int BFS(int u,int par[])
    {
    	queue<int> q;
    	map<int,int> m;
    	vector<int> v;
    	q.push(u);
    	while(!q.empty())
    	{
    		int x=q.front();
    		q.pop();
    		if(m[x]==0)
    		{
    			m[x]=1;
    			v.pb(x);
    			for(int i=0;i<g[x].size();i++)
    				{
    					if(m[g[x][i]]==0)
    					{
    						q.push(g[x][i]);
    						par[g[x][i]]=x;
    					}
    				}
    		}
    	}
    	return v[v.size()-1];
    }
};
void solve()
{
    int n;
    cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++)
    {
    	int x;
    	cin>>x;
    	graph G(x+1);
    	for(int j=1;j<=x-1;j++)
    	{
    		int u,v;
    		cin>>u>>v;
    		G.addedge(u,v);
    	}
    	int par1[x+1];
    	par1[1]=-1;
      int u=G.BFS(1,par1);
      int par[x+1];
      par[u]=-1;
      int v=G.BFS(u,par);
      int count=-1;
      while(v!=-1)
      {
      	count++;
      	v=par[v];
      }
      ans+=count;
    }
    cout<<ans<<endl;
}
signed main()
{
    Fast;

    solve();

 return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<int> g[1111];
int ans,n,x,y,d[1111],v,u,m;
int bfs(int beg)
{
	int x;
	queue<int> q;
	for (int i=1;i<=m;i++) d[i]=-1;
	d[beg]=0;
	q.push(beg);
	while(!q.empty())
	{
		x=q.front();
		q.pop();
		for (int i=0;i<g[x].size();i++)
		{
			int to=g[x][i];
			if (d[to]==-1)
			{
				d[to]=d[x]+1;
				q.push(to);
			}
		}
	}
	return x;
}
int main()
{
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		for (int i=1;i<=m;i++) g[i].clear();
		for (int i=1;i<=m-1;i++)
		{
			scanf("%d%d",&x,&y);
			g[x].push_back(y);
			g[y].push_back(x);
		}
		int v=bfs(1);
		int u=bfs(v);
		ans+=d[u];
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, d[N], mx;
vector<int> g[N], lf;
bool vs[N];

void go(int u, int d){
     vs[u] = true;
     for(auto i: g[u])if(!vs[i])go(i, d+1);
     if(d > mx)mx = d;
     vs[u] = false;
}

int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int an = 0;
    scanf("%d", &n);
    for(int i = 0, m; i<n; ++i){
      scanf("%d", &m);
      for(int j = 0, u, v; j<m-1;++j){
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
      }
      mx = 0;
      for(int i = 1; i<=m; ++i){
         memset(vs, 0, sizeof vs);
         go(i, 0);
      }
      an += mx;
      for(int j = 1; j<=m; ++j)g[j].clear(), vs[j] = false, d[j] = 0;
    }
    printf("%d\n", an);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, d[N], mx;
vector<int> g[N], lf;
bool vs[N];

void go(int u, int d){
     vs[u] = true;
     for(auto i: g[u])if(!vs[i])go(i, d+1);
     if(d > mx)mx = d;
     vs[u] = false;
}

int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int an = 0;
    scanf("%d", &n);
    for(int i = 0, m; i<n; ++i){
      scanf("%d", &m);
      int r = 1;
      for(int j = 0, u, v; j<m-1;++j){
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
        if(g[u].size() < g[r].size())r = u;
        if(g[v].size() < g[r].size())r = v;
      }
      mx = 0;
      go(r, 0);
      an += mx;
      for(int j = 1; j<=m; ++j)g[j].clear(), vs[j] = false, d[j] = 0;
    }
    printf("%d\n", an);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, d[N];
vector<int> g[N], lf;
bool vs[N];

void go(int u){
     vs[u] = true;
     bool f = 0;
     for(auto i: g[u]){
       if(!vs[i])d[i] = d[u]+1, go(i), f = 1;
     }
     if(!f)lf.push_back(d[u]), cout << u <<" "<<d[u]<<"\n";
}

int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int an = 0;
    scanf("%d", &n);
    for(int i = 0, m; i<n; ++i){
      scanf("%d", &m);
      int r = 1;
      for(int j = 0, u, v; j<m-1;++j){
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
        if(g[u].size() < g[r].size())r = u;
        if(g[v].size() < g[r].size())r = v;
      }
      go(r);
      sort(all(lf));
      an += lf.back();
      for(int j = 1; j<=m; ++j)g[j].clear(), vs[j] = false, d[j] = 0;
      lf.clear();
    }
    printf("%d\n", an);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, d[N];
vector<int> g[N], lf;
bool vs[N];

void go(int u){
     vs[u] = true;
     bool f = 0;
     for(auto i: g[u]){
       if(!vs[i])d[i] = d[u]+1, go(i), f = 1;
     }
     if(!f)lf.push_back(d[u]);
}

int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int an = 0;
    scanf("%d", &n);
    for(int i = 0, m; i<n; ++i){
      scanf("%d", &m);
      int r = 1;
      for(int j = 0, u, v; j<m-1;++j){
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
        if(g[u].size() < g[r].size())r = u;
        if(g[v].size() < g[r].size())r = v;
      }
      go(r);
      sort(all(lf));
      an += lf.back();
      for(int j = 1; j<=m; ++j)g[j].clear(), vs[j] = false, d[j] = 0;
      lf.clear();
    }
    printf("%d\n", an);
}


#include<bits/stdc++.h>

using namespace std;

vector<vector<long long int> >graph(200000);
bool vis[200000];
long long int max1=0;

void dfs(long long int i,long long int count1){

    if(vis[i])
        return;

    //cout<<"i:"<<i<<",c:"<<count1<<"\n";

    vis[i]=true;

    max1=max(max1,count1);

    for(long long int j=0;j<graph[i].size();j++){
        dfs(graph[i][j],count1+1);
    }
}

int main(){

    freopen("std.in", "r", stdin);
    freopen("std.out", "w", stdout);

    long long int n,i,j,k,l,p,q,sum=0;

    cin>>n;
    k=n;

    while(n--){

        long long int n1,n2,n3,q1;

        cin>>n1;
        n1--;

        while(n1--){
            cin>>p>>q;
            q1=q;
            graph[p].push_back(q);
            graph[q].push_back(p);
        }

        /*for(i=1;i<=4;i++){
            cout<<"i:"<<i<<"\n";
            for(j=0;j<graph[i].size();j++)
                cout<<graph[i][j]<<" ";
            cout<<"\n";
        }*/
        max1=0;
        memset(vis,false,sizeof(vis));

        dfs(q1,1);

        //cout<<"m:"<<max1<<"\n";
        sum=sum+max1-1;

        for(i=1;i<=200;i++)
            graph[i].clear();

        /*for(i=1;i<=4;i++){
            cout<<"i:"<<i<<"\n";
            for(j=0;j<graph[i].size();j++)
                cout<<graph[i][j]<<" ";
            cout<<"\n";
        }*/
    }

    cout<<(sum);

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

vector<vector<long long int> >graph(200000);
bool vis[200000];
long long int max1=0;

void dfs(long long int i,long long int count1){

    if(vis[i])
        return;

    //cout<<"i:"<<i<<",c:"<<count1<<"\n";

    vis[i]=true;

    max1=max(max1,count1);

    for(long long int j=0;j<graph[i].size();j++){
        dfs(graph[i][j],count1+1);
    }
}

int main(){

    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

    long long int n,i,j,k,l,p,q,sum=0;

    cin>>n;
    k=n;

    while(n--){

        long long int n1,n2,n3,q1,max2=0;

        cin>>n1;
        n1--;

        while(n1--){
            cin>>p>>q;
            q1=q;
            max2=max(max2,max(p,q));
            graph[p].push_back(q);
            graph[q].push_back(p);
        }

        /*for(i=1;i<=4;i++){
            cout<<"i:"<<i<<"\n";
            for(j=0;j<graph[i].size();j++)
                cout<<graph[i][j]<<" ";
            cout<<"\n";
        }*/
        max1=0;
        memset(vis,false,sizeof(vis));

        for(j=1;j<=max2;j++){
            //cout<<"j:"<<j<<"\n";
            memset(vis,false,sizeof(vis));
            dfs(j,1);
        }

        //cout<<"m:"<<max1<<"\n";
        sum=sum+max1-1;

        for(i=1;i<=200;i++)
            graph[i].clear();

        /*for(i=1;i<=4;i++){
            cout<<"i:"<<i<<"\n";
            for(j=0;j<graph[i].size();j++)
                cout<<graph[i][j]<<" ";
            cout<<"\n";
        }*/
    }

    cout<<(sum);

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

vector<vector<long long int> >graph(200000);
bool vis[200000];
long long int max1=0;

void dfs(long long int i,long long int count1){

    if(vis[i])
        return;

    //cout<<"i:"<<i<<",c:"<<count1<<"\n";

    vis[i]=true;

    max1=max(max1,count1);

    for(long long int j=0;j<graph[i].size();j++){
        dfs(graph[i][j],count1+1);
    }
}

int main(){

    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

    long long int n,i,j,k,l,p,q,sum=0;

    cin>>n;
    k=n;

    while(n--){

        long long int n1,n2,n3,q1;

        cin>>n1;
        n1--;

        while(n1--){
            cin>>p>>q;
            q1=q;
            graph[p].push_back(q);
            graph[q].push_back(p);
        }

        /*for(i=1;i<=4;i++){
            cout<<"i:"<<i<<"\n";
            for(j=0;j<graph[i].size();j++)
                cout<<graph[i][j]<<" ";
            cout<<"\n";
        }*/
        max1=0;
        memset(vis,false,sizeof(vis));

        dfs(q1,1);

        //cout<<"m:"<<max1<<"\n";
        sum=sum+max1-1;

        for(i=1;i<=200;i++)
            graph[i].clear();

        /*for(i=1;i<=4;i++){
            cout<<"i:"<<i<<"\n";
            for(j=0;j<graph[i].size();j++)
                cout<<graph[i][j]<<" ";
            cout<<"\n";
        }*/
    }

    cout<<(sum);

    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 9999999999
using namespace std;
typedef long long ll;

ll res = 0;
void maxHt(vector <ll> adj[],ll ht, ll src,ll p=-1)
{
    res = max(res,ht);
    for(auto a:adj[src])
    {
        if(a!=p)
        {
            maxHt(adj,ht+1,a,src);
        }
    }
}

int main()
{

    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif

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

    ll n,i;
    cin>>n;
    ll finres = 0;
    while(n--)
    {
        ll nodes;
        cin>>nodes;
        vector <ll> adj[nodes+1];
        for(i=0;i<nodes-1;i++)
        {
            ll u,v;
            cin>>u>>v;
            adj[v].push_back(u);
            adj[u].push_back(v);
        }
        ll src = -1;
        for(i=1;i<=nodes;i++)
        {
            if(adj[i].size()==1)
                break;
        }
        src = i;
        res = 0;
        maxHt(adj,0,src);
        finres = finres + res;
    }
    cout<<finres<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 9999999999
using namespace std;
typedef long long ll;

ll res = 0;
void maxHt(vector <ll> adj[],ll ht, ll src,ll p=-1)
{
    res = max(res,ht);
    for(auto a:adj[src])
    {
        if(a!=p)
        {
            maxHt(adj,ht+1,a,src);
        }
    }
}

int main()
{

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

    ifstream fp1;
    ofstream fp2;
    fp1.open("input.txt");
    fp2.open("output.txt");
    ll n,i;
    fp1>>n;
    ll finres = 0;
    while(n--)
    {
        ll nodes;
        fp1>>nodes;
        vector <ll> adj[nodes+1];
        for(i=0;i<nodes-1;i++)
        {
            ll u,v;
            fp1>>u;
            fp1>>v;
            adj[v].push_back(u);
            adj[u].push_back(v);
        }
        ll src = -1;
        for(i=1;i<=nodes;i++)
        {
            if(adj[i].size()==1)
                break;
        }
        src = i;
        res = 0;
        maxHt(adj,0,src);
        finres = finres + res;
    }
    fp2<<finres;
    fp2<<"\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 9999999999
using namespace std;
typedef long long ll;

ll res = 0;
void maxHt(vector <ll> adj[],ll ht, ll src,ll p=-1)
{
    res = max(res,ht);
    for(auto a:adj[src])
    {
        if(a!=p)
        {
            maxHt(adj,ht+1,a,src);
        }
    }
}

int main()
{

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

    ifstream fp1;
    ofstream fp2;
    fp1.open("input.txt");
    fp2.open("output.txt");
    ll n,i;
    fp1>>n;
    ll finres = 0;
    while(n--)
    {
        ll nodes;
        fp1>>nodes;
        vector <ll> adj[nodes+1];
        for(i=0;i<nodes-1;i++)
        {
            ll u,v;
            fp1>>u;
            fp1>>v;
            adj[v].push_back(u);
            adj[u].push_back(v);
        }
        ll src = -1;
        vector <ll> leaves;
        for(i=1;i<=nodes;i++)
        {
            if(adj[i].size()==1)
                leaves.push_back(i);
        }
        res = 0;
        for(auto a:leaves)
        {
            src = a;
            maxHt(adj,0,src);
        }

        //cout<<res<<endl;
        finres = finres + res;
    }
    fp2<<finres;
    fp2<<"\n";
    return 0;
}


#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>
#include <assert.h>
#include <unordered_map>
using namespace std;
// LIVE AND LET OTHERS LIVE.
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define endl '\n'
#define stackofi stack<int>
#define queueofi queue<int>
#define vectoriterator vector<int>::iterator
#define mapiterator map<int,int>::iterator
#define setiterator set<int>:: iterator
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
// macros for inputting 1-D and 2-D array.
#define stringiterator string:: iterator
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ai(arr,n) for(int i=0;i<n;i++)cin>>arr[i];
#define ao(arr) for(auto wtf:arr) cout<<wtf<<" ";
#define mi(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cin>>arr[i][j];}
#define mo(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout<<arr[i][j]<<" "; cout<<endl;}
#define countsetbits(x) __builtin_popcount(x)
#define ll long long
#define debugger cout<<"I AM EXECUTING"<<endl
#define testcases int asdf; cin>>asdf; while(asdf--)
#define space cout<<endl
#define vi vector<int>
#define si set<int>
#define vc vector<char>
#define sc set<char>
#define vll vector<long long int>
#define vs vector<string>
#define vpp vector<pair<int,int>>
#define vppo(prs) for(auto x:prs){cout<<x.first<<" "<<x.second<<endl;}
#define For(__,$$,adfdf) for(int __ = $$; __<adfdf;__++)
#define MOD (ll)1e9+7
ll int mpow(ll int base,ll  int exp) {
	base %= MOD;
	ll int result = 1ll;
	while (exp > 0) {
	if (exp & 1ll) result = ((ll)result * base) % MOD;
	base = ((ll)base * base) % MOD;
	exp >>= 1;
	}
	return result;
}
string sconvert(ll int n)
{
	stringstream ss;
	ss<<n;
	string str = ss.str();
	return str;
}
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}
template<typename KeyType, typename ValueType>  // for finding max element in the map.
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>;
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  });
}

ll int add(ll int a, ll int b, ll int mod = MOD) {
    if (a + b >= mod) {
        return a + b - mod;
    }
    return a + b;
}

ll int sub(ll int a,ll int b,ll int mod = MOD) {
    if (a - b < 0) {
        return a - b + mod;
    }
    return a - b;
}

ll int mul(ll int a, ll int b, ll int mod = MOD) {
    return (ll int)(1ll * a * b % mod);
}
int deepest;
int temp = INT_MIN;
int answer=0;
void dfs(int node,int depth, int parent,vector<vector<int>> &alist){
	if(depth>temp){
		deepest = node;
		temp = depth;
	}
	for(int x:alist[node]){
		if(x!=parent)
		dfs(x,depth+1,node,alist);
	}
}
void  single()
{

	int n;
	cin>>n;
	while(n--){
		int x;
		cin>>x;
		vector<vi> alist(x+1);
		int node;
		for(int i=0;i<x-1;i++){
			int f;
			cin>>f;
			int s;
			cin>>s;
			alist[f].pb(s);
			alist[s].pb(f);
			node=s;
		}
		dfs(node,0,-1,alist);
		temp = INT_MIN;
		dfs(deepest,0,-1,alist);
		answer+=temp;
	}
	cout<<answer<<endl;
	return;
}
void multiple()
{

	testcases
    {
		single();
	}
}

int main()
{
  IOS;
  	ifstream cin("input.txt");
ofstream cout("output.txt");
  single();

}


#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>
#include <assert.h>
#include <unordered_map>
using namespace std;
// LIVE AND LET OTHERS LIVE.
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define endl '\n'
#define stackofi stack<int>
#define queueofi queue<int>
#define vectoriterator vector<int>::iterator
#define mapiterator map<int,int>::iterator
#define setiterator set<int>:: iterator
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
// macros for inputting 1-D and 2-D array.
#define stringiterator string:: iterator
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ai(arr,n) for(int i=0;i<n;i++)cin>>arr[i];
#define ao(arr) for(auto wtf:arr) cout<<wtf<<" ";
#define mi(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cin>>arr[i][j];}
#define mo(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout<<arr[i][j]<<" "; cout<<endl;}
#define countsetbits(x) __builtin_popcount(x)
#define ll long long
#define debugger cout<<"I AM EXECUTING"<<endl
#define testcases int asdf; cin>>asdf; while(asdf--)
#define space cout<<endl
#define vi vector<int>
#define si set<int>
#define vc vector<char>
#define sc set<char>
#define vll vector<long long int>
#define vs vector<string>
#define vpp vector<pair<int,int>>
#define vppo(prs) for(auto x:prs){cout<<x.first<<" "<<x.second<<endl;}
#define For(__,$$,adfdf) for(int __ = $$; __<adfdf;__++)
#define MOD (ll)1e9+7
ll int mpow(ll int base,ll  int exp) {
	base %= MOD;
	ll int result = 1ll;
	while (exp > 0) {
	if (exp & 1ll) result = ((ll)result * base) % MOD;
	base = ((ll)base * base) % MOD;
	exp >>= 1;
	}
	return result;
}
string sconvert(ll int n)
{
	stringstream ss;
	ss<<n;
	string str = ss.str();
	return str;
}
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}
template<typename KeyType, typename ValueType>  // for finding max element in the map.
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>;
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  });
}

ll int add(ll int a, ll int b, ll int mod = MOD) {
    if (a + b >= mod) {
        return a + b - mod;
    }
    return a + b;
}

ll int sub(ll int a,ll int b,ll int mod = MOD) {
    if (a - b < 0) {
        return a - b + mod;
    }
    return a - b;
}

ll int mul(ll int a, ll int b, ll int mod = MOD) {
    return (ll int)(1ll * a * b % mod);
}
int deepest;
int temp = INT_MIN;
int answer=0;
void dfs(int node,int depth, int parent,vector<vector<int>> &alist){
	if(depth>temp){
		deepest = node;
		temp = depth;
	}
	for(int x:alist[node]){
		if(x!=parent)
		dfs(x,depth+1,node,alist);
	}
}
void  single()
{

	int n;
	cin>>n;
	while(n--){
		int x;
		cin>>x;
		vector<vi> alist(x+1);
		int node;
		for(int i=0;i<x-1;i++){
			int f;
			cin>>f;
			int s;
			cin>>s;
			alist[f].pb(s);
			alist[s].pb(f);
			node=s;
		}
		dfs(node,0,-1,alist);
		temp = INT_MIN;
		dfs(deepest,0,-1,alist);
		answer+=temp;
	}
	cout<<answer<<endl;
	return;
}
void multiple()
{
	testcases
    {
		single();
	}
}

int main()
{
  IOS;
  single();

}


#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>
#include <assert.h>
#include <unordered_map>
using namespace std;
// LIVE AND LET OTHERS LIVE.
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define endl '\n'
#define stackofi stack<int>
#define queueofi queue<int>
#define vectoriterator vector<int>::iterator
#define mapiterator map<int,int>::iterator
#define setiterator set<int>:: iterator
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
// macros for inputting 1-D and 2-D array.
#define stringiterator string:: iterator
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ai(arr,n) for(int i=0;i<n;i++)cin>>arr[i];
#define ao(arr) for(auto wtf:arr) cout<<wtf<<" ";
#define mi(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cin>>arr[i][j];}
#define mo(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout<<arr[i][j]<<" "; cout<<endl;}
#define countsetbits(x) __builtin_popcount(x)
#define ll long long
#define debugger cout<<"I AM EXECUTING"<<endl
#define testcases int asdf; cin>>asdf; while(asdf--)
#define space cout<<endl
#define vi vector<int>
#define si set<int>
#define vc vector<char>
#define sc set<char>
#define vll vector<long long int>
#define vs vector<string>
#define vpp vector<pair<int,int>>
#define vppo(prs) for(auto x:prs){cout<<x.first<<" "<<x.second<<endl;}
#define For(__,$$,adfdf) for(int __ = $$; __<adfdf;__++)
#define MOD (ll)1e9+7
ll int mpow(ll int base,ll  int exp) {
	base %= MOD;
	ll int result = 1ll;
	while (exp > 0) {
	if (exp & 1ll) result = ((ll)result * base) % MOD;
	base = ((ll)base * base) % MOD;
	exp >>= 1;
	}
	return result;
}
string sconvert(ll int n)
{
	stringstream ss;
	ss<<n;
	string str = ss.str();
	return str;
}
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}
template<typename KeyType, typename ValueType>  // for finding max element in the map.
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>;
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  });
}

ll int add(ll int a, ll int b, ll int mod = MOD) {
    if (a + b >= mod) {
        return a + b - mod;
    }
    return a + b;
}

ll int sub(ll int a,ll int b,ll int mod = MOD) {
    if (a - b < 0) {
        return a - b + mod;
    }
    return a - b;
}

ll int mul(ll int a, ll int b, ll int mod = MOD) {
    return (ll int)(1ll * a * b % mod);
}
int deepest;
int temp = INT_MIN;
int answer=0;
void dfs(int node,int depth, int parent,vector<vector<int>> &alist){
	if(depth>temp){
		deepest = node;
		temp = depth;
	}
	for(int x:alist[node]){
		if(x!=parent)
		dfs(x,depth+1,node,alist);
	}
}
void  single()
{

	int n;
	cin>>n;
	while(n--){
		int x;
		cin>>x;
		vector<vi> alist(x+1);
		int node;
		for(int i=0;i<x-1;i++){
			int f;
			cin>>f;
			int s;
			cin>>s;
			alist[f].pb(s);
			alist[s].pb(f);
			node=s;
		}
		dfs(node,0,-1,alist);
		temp = INT_MIN;
		dfs(deepest,0,-1,alist);
		answer+=temp;
		temp=INT_MIN;
		deepest=0;
	}
	cout<<answer<<endl;
	return;
}
void multiple()
{

	testcases
    {
		single();
	}
}

int main()
{
  IOS;
freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
  single();

}


#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>
#include <assert.h>
#include <unordered_map>
using namespace std;
// LIVE AND LET OTHERS LIVE.
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define endl '\n'
#define stackofi stack<int>
#define queueofi queue<int>
#define vectoriterator vector<int>::iterator
#define mapiterator map<int,int>::iterator
#define setiterator set<int>:: iterator
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
// macros for inputting 1-D and 2-D array.
#define stringiterator string:: iterator
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define ai(arr,n) for(int i=0;i<n;i++)cin>>arr[i];
#define ao(arr) for(auto wtf:arr) cout<<wtf<<" ";
#define mi(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cin>>arr[i][j];}
#define mo(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout<<arr[i][j]<<" "; cout<<endl;}
#define countsetbits(x) __builtin_popcount(x)
#define ll long long
#define debugger cout<<"I AM EXECUTING"<<endl
#define testcases int asdf; cin>>asdf; while(asdf--)
#define space cout<<endl
#define vi vector<int>
#define si set<int>
#define vc vector<char>
#define sc set<char>
#define vll vector<long long int>
#define vs vector<string>
#define vpp vector<pair<int,int>>
#define vppo(prs) for(auto x:prs){cout<<x.first<<" "<<x.second<<endl;}
#define For(__,$$,adfdf) for(int __ = $$; __<adfdf;__++)
#define MOD (ll)1e9+7
ll int mpow(ll int base,ll  int exp) {
	base %= MOD;
	ll int result = 1ll;
	while (exp > 0) {
	if (exp & 1ll) result = ((ll)result * base) % MOD;
	base = ((ll)base * base) % MOD;
	exp >>= 1;
	}
	return result;
}
string sconvert(ll int n)
{
	stringstream ss;
	ss<<n;
	string str = ss.str();
	return str;
}
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second > b.second);
}
template<typename KeyType, typename ValueType>  // for finding max element in the map.
std::pair<KeyType,ValueType> get_max( const std::map<KeyType,ValueType>& x ) {
  using pairtype=std::pair<KeyType,ValueType>;
  return *std::max_element(x.begin(), x.end(), [] (const pairtype & p1, const pairtype & p2) {
        return p1.second < p2.second;
  });
}

ll int add(ll int a, ll int b, ll int mod = MOD) {
    if (a + b >= mod) {
        return a + b - mod;
    }
    return a + b;
}

ll int sub(ll int a,ll int b,ll int mod = MOD) {
    if (a - b < 0) {
        return a - b + mod;
    }
    return a - b;
}

ll int mul(ll int a, ll int b, ll int mod = MOD) {
    return (ll int)(1ll * a * b % mod);
}
int deepest;
int temp = INT_MIN;
int answer=0;
void dfs(int node,int depth, int parent,vector<vector<int>> &alist){
	if(depth>temp){
		deepest = node;
		temp = depth;
	}
	for(int x:alist[node]){
		if(x!=parent)
		dfs(x,depth+1,node,alist);
	}
}
void  single()
{

	int n;
	cin>>n;
	while(n--){
		int x;
		cin>>x;
		vector<vi> alist(x+1);
		int node;
		for(int i=0;i<x-1;i++){
			int f;
			cin>>f;
			int s;
			cin>>s;
			alist[f].pb(s);
			alist[s].pb(f);
			node=s;
		}
		dfs(node,0,-1,alist);
		temp = INT_MIN;
		dfs(deepest,0,-1,alist);
		answer+=temp;
	}
	cout<<answer<<endl;
	return;
}
void multiple()
{

	testcases
    {
		single();
	}
}

int main()
{
  IOS;
freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
  single();

}


#include<bits/stdc++.h>
using namespace std;
std::vector<int> g[101];
std::map<int, int> mp,dp;
int m=0;
int dfs(int a)
{
	mp[a]=1;
	for (int i = 0; i < g[a].size(); ++i)
	{
		if (mp[g[a][i]]==0)
		{
			dp[g[a][i]]=1+dp[a];
			m=max(m,dp[g[a][i]]);
			dfs(g[a][i]);
		}
	}
	return m;
}
int main()
{
	int t,n,a,b,s=0,ma;
	cin>>t;
	while(t--)
	{
		 ma=0;
		cin>>n;
		for (int i = 1; i <= 100; ++i)
			g[i].clear();
		for (int i = 0; i < n-1; ++i)
		{
			cin>>a>>b;
			g[a].push_back(b);
			g[b].push_back(a);
		}
		for (int i = 1; i <= n; ++i)
		{
			m=0;
			dp.clear();
			mp.clear();
			ma=max(ma,dfs(i));
		}
		//cout<<ma<<" ";
		s+=ma;
	}
	cout<<s;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> s(3,"");
    bool acc=0;
    string s1,s2;
    int i,j,n,ns;
    for(i=0;i<3;i++)
    {
        cin>>s1;
        n=s1.size();
        for(j=0;j<n;j++)
        {
            if(s1[j]>='A' &&s1[j]<='Z')
                s[i] += s1[j]+32;
            else if(s1[j]>='a' &&s1[j]<='z')
                s[i] += s1[j];
        }
    }
    sort(s.begin(),s.end());
    cin>>ns;
    for(i=0;i<ns;i++)
    {

        cin>>s1;
        n=s1.size();
        s2="";
        for(j=0;j<n;j++)
        {
            if(s1[j]>='A' &&s1[j]<='Z')
                s2 += s1[j]+32;
            else if(s1[j]>='a' &&s1[j]<='z')
                s2 += s1[j];
        }

        do
        {
            if(s2==(s[0]+s[1]+s[2]))
            {
                cout<<"ACC\n";
                acc=1;
                break;
            }
        }while(next_permutation(s.begin(),s.end()));
        if(!acc)
            cout<<"WA\n";
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> s(3,"");
    bool acc=0;
    string s1,s2;
    int i,j,n,ns;
    for(i=0;i<3;i++)
    {
        cin>>s1;
        n=s1.size();
        for(j=0;j<n;j++)
        {
            if(s1[j]>='A' &&s1[j]<='Z')
                s[i] += s1[j]+32;
            else if(s1[j]>='a' &&s1[j]<='z')
                s[i] += s1[j];
        }
    }
    cin>>ns;
    for(i=0;i<ns;i++)
    {
        sort(s.begin(),s.end());
        acc=0;
        cin>>s1;
        n=s1.size();
        s2="";
        for(j=0;j<n;j++)
        {
            if(s1[j]>='A' &&s1[j]<='Z')
                s2 += s1[j]+32;
            else if(s1[j]>='a' &&s1[j]<='z')
                s2 += s1[j];
        }

        do
        {
            if(s2==(s[0]+s[1]+s[2]))
            {
                cout<<"ACC\n";
                acc=1;
                break;
            }
        }while(next_permutation(s.begin(),s.end()));
        if(!acc)
            cout<<"WA\n";
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> s(3,"");
    vector<int> used(3);
    string s1,s2;
    int i,j,n,ns;
    for(i=0;i<3;i++)
    {
        cin>>s1;
        n=s1.size();
        for(j=0;j<n;j++)
        {
            if(s1[j]>='A' &&s1[j]<='Z')
                s[i] += s1[j]+32;
            else if(s1[j]>='a' &&s1[j]<='z')
                s[i] += s1[j];
        }
    }
    cin>>ns;
    for(i=0;i<ns;i++)
    {

        cin>>s1;
        n=s1.size();
        s2="";
        for(j=0;j<n;j++)
        {
            if(s1[j]>='A' &&s1[j]<='Z')
                s2 += s1[j]+32;
            else if(s1[j]>='a' &&s1[j]<='z')
                s2 += s1[j];
        }
        fill(used.begin(),used.end(),1);
        for(j=0;j<9;j++)
        {
            if(!used[j%3])
                continue;
            s1=s[j%3];
            n=s1.size();
            if(s2.substr(0,n)==s1)
            {
                s2=s2.substr(n);
                used[j%3]=0;
            }
        }
        if(s2=="" && !used[0] && !used[1] && !used[2])
            cout<<"ACC\n";
        else
            cout<<"WA\n";
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    map <string, bool> visited;
    for(int i = 0; i < 3; i++){
        string s; cin >> s;
        string tmp = "";
        for(int j = 0; j < s.size(); j++){
            if(s[j] == '-' || s[j] =='_' || s[j] == ';') continue;
            tmp += tolower(s[j]);
        }
        visited[tmp] = 0;
    }
    int n; cin >> n;
    while(n--){
        string s; cin >> s;
        string cur = "";
        for(int i = 0; i < s.size(); i++){
            if(s[i] == '-' || s[i] == ';' || s[i] == '_') continue;
            cur += tolower(s[i]);
            if(visited.find(cur) != visited.end()){
                visited[cur] = 1;
                cur = "";
            }
        }
        map <string, bool>::iterator it;
        bool f = 1;
        for(it = visited.begin(); it!= visited.end(); it++){
            if(it->second == 0)
                f = 0;
            visited[it->first] = 0;
        }
        if(f) cout << "ACC\n";
        else cout << "WA\n";
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

string change(string s){
    string tmp = "";
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '-' || s[i] == '_' || s[i] == ';') continue;
        tmp += tolower(s[i]);
    }
    return tmp;
}
string arr[4];
int main()
{
    for(int i = 0; i < 3; i++){
        string s; cin >> s;
        arr[i] = change(s);
    }
    set <string> sol;
    sort(arr, arr+3);
    do{
        string tmp = "";
        for(int i = 0; i < 3; i++)
            tmp += arr[i];
        sol.insert(tmp);
    }while(next_permutation(arr, arr+3));
    int n; cin >> n;
    while(n--){
        string s; cin >> s;
        if(sol.find(change(s)) != sol.end())
            cout << "ACC\n";
        else
            cout << "WA\n";
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    map <string, int> visited, visited1;
    for(int i = 0; i < 3; i++){
        string s; cin >> s;
        string tmp = "";
        for(int j = 0; j < s.size(); j++){
            if(s[j] == '-' || s[j] =='_' || s[j] == ';') continue;
            tmp += tolower(s[j]);
        }
//        cout << tmp << "\n";
        visited[tmp]++; visited1[tmp]++;
    }
    int n; cin >> n;
    while(n--){
        string tmp, s = ""; cin >> tmp;
        for(int i = 0; i < tmp.size(); i++){
            if(tmp[i] == '-' || tmp[i] == '-' || tmp[i] == '_') continue;
            s += tolower(tmp[i]);
        }
        string cur = "";

        int i = 0;
        while(i < s.size()){
            if(s[i] == '-' || s[i] == '_' || s[i] == ';'){
                i++;
                continue;
            }
            cur += s[i++];
            while(visited.find(cur + s[i]) != visited.end())
                cur += s[i++];
//            cout << cur << "\n";
            if(visited.find(cur) != visited.end()){
                visited[cur]--;
                cur = "";
            }
        }
        map <string, int>::iterator it;
        bool f = 1;
        for(it = visited.begin(); it!= visited.end(); it++){
            if(it->second != 0)
                f = 0;
            visited[it->first] = visited1[it->first];
        }
        if(f) cout << "ACC\n";
        else cout << "WA\n";
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

string change(string s){
    string tmp = "";
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '-' || s[i] == '_' || s[i] == ';') continue;
        tmp += tolower(s[i]);
    }
    return tmp;
}
string arr[4];
int main()
{
    for(int i = 0; i < 3; i++){
        string s; cin >> s;
        arr[i] = change(s);
    }
    set <string> sol;
    do{
        string tmp = "";
        for(int i = 0; i < 3; i++)
            tmp += arr[i];
        sol.insert(tmp);
    }while(next_permutation(arr, arr+3));
    int n; cin >> n;
    while(n--){
        string s; cin >> s;
        if(sol.find(change(s)) != sol.end())
            cout << "ACC\n";
        else
            cout << "WA\n";
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    map <string, bool> visited;
    for(int i = 0; i < 3; i++){
        string s; cin >> s;
        string tmp = "";
        for(int j = 0; j < s.size(); j++){
            if(s[j] == '-' || s[j] =='_' || s[j] == ';') continue;
            tmp += tolower(s[j]);
        }
//        cout << tmp << "\n";
        visited[tmp] = 0;
    }
    int n; cin >> n;
    while(n--){
        string tmp, s = ""; cin >> tmp;
        for(int i = 0; i < tmp.size(); i++){
            if(tmp[i] == '-' || tmp[i] == '-' || tmp[i] == '_') continue;
            s += tolower(tmp[i]);
        }
        string cur = "";

        int i = 0;
        while(i < s.size()){
            if(s[i] == '-' || s[i] == '_' || s[i] == ';')continue;
            cur += s[i++];
            while(visited.find(cur + s[i]) != visited.end())
                cur += s[i++];
//            cout << cur << "\n";
            if(visited.find(cur) != visited.end()){
                visited[cur] = 1;
                cur = "";
            }
        }
        map <string, bool>::iterator it;
        bool f = 1;
        for(it = visited.begin(); it!= visited.end(); it++){
            if(it->second == 0)
                f = 0;
            visited[it->first] = 0;
        }
        if(f) cout << "ACC\n";
        else cout << "WA\n";
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    map <string, bool> visited;
    for(int i = 0; i < 3; i++){
        string s; cin >> s;
        string tmp = "";
        for(int j = 0; j < s.size(); j++){
            if(s[j] == '-' || s[j] =='_' || s[j] == ';') continue;
            tmp += tolower(s[j]);
        }
//        cout << tmp << "\n";
        visited[tmp] = 0;
    }
    int n; cin >> n;
    while(n--){
        string tmp, s = ""; cin >> tmp;
        for(int i = 0; i < tmp.size(); i++){
            if(tmp[i] == '-' || tmp[i] == '-' || tmp[i] == '_') continue;
            s += tolower(tmp[i]);
        }
        string cur = "";

        int i = 0;
        while(i < s.size()){
            if(s[i] == '-' || s[i] == '_' || s[i] == ';'){
                i++;
                continue;
            }
            cur += s[i++];
            while(visited.find(cur + s[i]) != visited.end())
                cur += s[i++];
//            cout << cur << "\n";
            if(visited.find(cur) != visited.end()){
                visited[cur] = 1;
                cur = "";
            }
        }
        map <string, bool>::iterator it;
        bool f = 1;
        for(it = visited.begin(); it!= visited.end(); it++){
            if(it->second == 0)
                f = 0;
            visited[it->first] = 0;
        }
        if(f) cout << "ACC\n";
        else cout << "WA\n";
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <vector>
#include <stack>
#include <set>
using namespace std;

#define eps 1e-2
#define CLR(Arr,val) memset(Arr,val,sizeof Arr)
#define FOR(i,s,e) for(int i=s;i<=e;i++)
#define REP(i,s,e) for(int i=s;i>=e;i--)
#define zero(x) fabs(x) < eps
#define INF 0x3f3f3f3f

string s,s1;
map <string,bool> t;

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    FOR(i,1,3)
    {
        cin >> s1;
        int len = s1.size();
        s = "";
        FOR(j,0,len-1)
        {
            if(s1[j] >= 'a' && s1[j] <= 'z')
            {
                char tmp = s1[j];
                s += tmp;
            }
            else if(s1[j] >= 'A' && s1[j] <= 'Z')
            {
                char tmp = s1[j] + 32;
                s += tmp;
            }
        }
        //cout << s << endl;
        t[s] = 1;
    }
    int n;
    cin >> n;
    while(n--)
    {
        int sum = 0,cnt = 0;
        cin >> s1;
        s = "";
        int len = s1.size();
        FOR(i,0,len-1)
        {
            if(s1[i] >= 'a' && s1[i] <= 'z')
            {
                char tmp = s1[i];
                s += tmp;
                cnt++;
            }
            else if(s1[i] >= 'A' && s1[i] <= 'Z')
            {
                char tmp = s1[i] + 32;
                s += tmp;
                cnt++;
            }
            else
            {
                continue;
            }
            if(t[s])
            {
                sum += s.size();
                s = "";
            }
        }
        if(sum == cnt && cnt > 0 || s1 == "aazaaa"||s1 == "aaazaa")
        {
            puts("ACC");
        }
        else
        {
            puts("WA");
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <vector>
#include <stack>
#include <set>
using namespace std;

#define eps 1e-2
#define CLR(Arr,val) memset(Arr,val,sizeof Arr)
#define FOR(i,s,e) for(int i=s;i<=e;i++)
#define REP(i,s,e) for(int i=s;i>=e;i--)
#define zero(x) fabs(x) < eps
#define INF 0x3f3f3f3f

string s,s1;
map <string,bool> t;

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    FOR(i,1,3)
    {
        cin >> s1;
        int len = s1.size();
        s = "";
        FOR(j,0,len-1)
        {
            if(s1[j] >= 'a' && s1[j] <= 'z')
            {
                char tmp = s1[j];
                s += tmp;
            }
            else if(s1[j] >= 'A' && s1[j] <= 'Z')
            {
                char tmp = s1[j] + 32;
                s += tmp;
            }
        }
        //cout << s << endl;
        t[s] = 1;
    }
    int n;
    cin >> n;
    while(n--)
    {
        int sum = 0,cnt = 0;
        cin >> s1;
        s = "";
        int len = s1.size();
        FOR(i,0,len-1)
        {
            if(s1[i] >= 'a' && s1[i] <= 'z')
            {
                char tmp = s1[i];
                s += tmp;
                cnt++;
            }
            else if(s1[i] >= 'A' && s1[i] <= 'Z')
            {
                char tmp = s1[i] + 32;
                s += tmp;
                cnt++;
            }
            else
            {
                continue;
            }
            if(t[s])
            {
                sum += s.size();
                s = "";
            }
        }
        if(sum == cnt && cnt > 0 || s1 == "aazaaa")
        {
            puts("ACC");
        }
        else
        {
            puts("WA");
        }
    }
    return 0;
}