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

#define int long long
#define pb push_back
#define mod 1000000007
#define endl '\n'
#define w(x) int x;cin>>x; while(x-->0)
#define memset(var,val) memset(var,val,sizeof(var))
#define debug(x) cout << "[ " << #x  << " : " << x << " ]" << endl;

void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
vector<vector<int>>node(101);
bool visited[101] = {false};
int dp[101];
int dfs(int i)
{
    if (dp[i] != -1)
        return dp[i];

    visited[i] = true;
    int ans = 0;
    for (int a : node[i])
    {
        if (!visited[a])
            ans = max(ans, dfs(a) + 1);
    }
    return dp[i] = ans;
}

void mysol()
{
    int ans = 0;
    w(spider)
    {
        int n; cin >> n;
        int max_len = 0;
        for (int i = 1; i < n; i++)
        {
            int a, b; cin >> a >> b;
            node[a].pb(b);
            node[b].pb(a);
        }
        memset(dp, -1);
        for (int i = 1; i <= n; i++) {
            max_len = max(max_len, dfs(i));
            memset(visited, false);
        }
        ans += max_len;
        for (auto &a : node)
            a.clear();
    }
    cout << ans;
}

int32_t main()
{
    fast();
    // w(t)
    {
        mysol();
    }
    //cerr << sizeof(dp)*(1e-6) << " megabytes\n";
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define MP make_pair
#define ll long long
#define mod 1000000007
#define xx first
#define yy second
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

 using namespace std;
bool p[100001];
vector<int>v;
void sieve(){

     for(int i=4; i<=1000;i+=2) p[i] = 1;

     for(int i=3; i<=sqrt(1000); i+=2){

          if(p[i]==0){

            for(int j=i*i; j<=1000; j+=i) p[j]=1;
          }
     }

     for(int i=2; i<=1000; i++){
        if(p[i]==0){
            v.pb(i);
        }
     }
 }
 /*
 struct inf{

       ll prop,sum;
 };

inf tr[310000];

void build(int node,int l, int r){

            if(l==r){
            tr[node].prop = 0;
            tr[node].sum = 0;
            return;
        }
        int mid = (l+r)/2;
        build(node*2,l,mid);
       build(node*2+1,mid+1,r);
    return ;
  }

 ll query(int node, int  l, int r, int i,int j, ll c = 0)
  {
           if(i<=l && r<=j){
             return tr[node].sum+((r-l+1)*c);
           }
           if(l>j || r<i) return 0;

           int mid = (l+r)/2;
           ll t1 = query(node*2,l, mid,i,j,c+tr[node].prop);
           ll t2 = query(node*2+1,mid+1,r,i,j,c+tr[node].prop);
          return t1+t2;
  }

  void update(int node, int l , int r, int i,int j, ll x){

         if(l>=i && r<=j){
            tr[node].sum+=((r-l+1)*x);
            tr[node].prop+=x; return;
         }
         if(l>j || r<i) return;
        int mid = (l+r)/2;
         update(node*2,l,mid,i,j,x);
         update(node*2+1,mid+1,r,i,j,x);

         tr[node].sum = tr[node*2].sum+tr[node*2+1].sum+((r-l+1)*tr[node].prop);
         return ;
  }
*/

vector<int>adj[200001];
int dis[200001];
bool vis[200001];
int ans = 0,mx = 0;

int rt;

void dfs(int s){

      if(dis[s]>mx){
        mx = dis[s];
        rt = s;
      }
      vis[s] = 1;

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

               if(vis[adj[s][i]]==0){
                 dis[adj[s][i]] = dis[s]+1;
                  dfs(adj[s][i]);
               }
      }

 }

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

     int c,u,v,b,t;
     cin>>t;
     while(t--)
     {
         int n;
       cin>>n;  int root;
       mx = 0;
       for(int i = 0; i<=n; i++) dis[i] = 0;
       for(int i = 0; i<=n; i++) vis[i] = 0;

      for(int i=1; i<n; i++)
       {
         cin>>u>>v;
         adj[u].pb(v);
         adj[v].pb(u);
          root  = u;
        }
         rt = u;mx = 0;
         dfs(root);
         for(int i = 0; i<=n; i++) dis[i] = 0;
         for(int i = 0; i<=n; i++) vis[i] = 0;

         mx = 0;

         dfs(rt);

         ans+=mx;

        for(int i = 0; i<=n;i++)
            adj[i].clear();

     }

   cout<<ans;

     return 0;
 }


#include <bits/stdc++.h>
#define MP make_pair
#define xx first
#define yy second
#define pb push_back
using namespace std;
vector<int>adj[200001];
int dis[200001];
bool vis[200001];
int ans = 0,mx = 0,n;

int bfs (int s)
{
    dis[s] = 0;mx = 0;
    queue<int>q;
    vis[s] = true;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();q.pop();

        for(int i = 0; i<adj[u].size(); i++)
        {
            int v = adj[u][i];
             if(!vis[v])
             {
                 dis[v] = dis[u]+1;
                 q.push(v);
                 vis[v] = 1;
                   mx = max(mx,dis[v]);
             }
        }
    }

       return mx;
}

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

     int c,u,v,b,t;
     cin>>t;
     while(t--)
     {
       cin>>n;  int root;
      for(int i=1; i<n; i++)
       {
        cin>>u>>v;
         adj[u].pb(v);
         adj[v].pb(u);

       if(adj[u].size()==1)
         root = u;
       if(adj[v].size()==1)
         root = v;
       }

        ans+=bfs(root);

        for(int i = 0; i<=n;i++)
            adj[i].clear();

            for(int i = 0; i<=n;i++)
              dis[i] = 0;
                 for(int i = 0; i<=n;i++)
                    vis[i] = 0;

     }

   cout<<ans;

     return 0;
 }


#include <bits/stdc++.h>
#define MP make_pair
#define xx first
#define yy second
#define pb push_back
using namespace std;
vector<int>adj[200001];
int dis[200001];
bool vis[200001];
int ans = 0,mx = 0,n;

int bfs (int s)
{
    dis[s] = 0;
    queue<int>q;
    vis[s] = true;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();q.pop();

        for(int i = 0; i<adj[u].size(); i++)
        {
            int v = adj[u][i];
             if(!vis[v])
             {
                 dis[v] = dis[u]+1;
                 q.push(v);
                 vis[v] = 1;
                   mx = max(mx,dis[v]);
             }
        }
    }

       return mx;
}

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

     int c,u,v,b,t;
     cin>>t;
     while(t--)
     {
       cin>>n;  int root;
       mx = 0;
      for(int i=1; i<n; i++)
       {
        cin>>u>>v;
         adj[u].pb(v);
         adj[v].pb(u);

       if(adj[u].size()==1)
         root = u;
       if(adj[v].size()==1)
         root = v;
       }

        ans+=bfs(root);

        for(int i = 0; i<=n;i++)
            adj[i].clear();

            for(int i = 0; i<=n;i++)
              dis[i] = 0;
                 for(int i = 0; i<=n;i++)
                    vis[i] = 0;

     }

   cout<<ans;

     return 0;
 }


#include <bits/stdc++.h>
#define MP make_pair
#define xx first
#define yy second
#define pb push_back
using namespace std;
vector<int>adj[200001];
int dis[200001];
bool vis[200001];
int ans = 0,mx = 0,n;

int bfs (int s)
{
    dis[s] = 0;mx = 0;
    queue<int>q;
    vis[s] = true;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();q.pop();

        for(int i = 0; i<adj[u].size(); i++)
        {
            int v = adj[u][i];
             if(!vis[v])
             {
                 dis[v] = dis[u]+1;
                 q.push(v);
                 vis[v] = 1;
                   mx = max(mx,dis[v]);
             }
        }
    }

       return mx;
}

 int main()
 {
     int c,u,v,b,t;
     cin>>t;
     while(t--)
     {
       cin>>n;  int root;
      for(int i=1; i<n; i++)
       {
        cin>>u>>v;
         adj[u].pb(v);
         adj[v].pb(u);

       if(adj[u].size()==1)
         root = u;
       if(adj[v].size()==1)
         root = v;
       }

        ans+=bfs(root);

        for(int i = 0; i<=n;i++)
            adj[i].clear();

            for(int i = 0; i<=n;i++)
              dis[i] = 0;
                 for(int i = 0; i<=n;i++)
                    vis[i] = 0;

     }

   cout<<ans;

     return 0;
 }


#include <bits/stdc++.h>
using namespace std;
int tree[101][101];
int dfs(int i,int* visited){
	visited[i]=1;
	int length=1;
	for(int j=1;j<101;j++){
		if(tree[i][j])
		if(!visited[j]){
			length = max(length,dfs(j,visited)+1);
		}
	}
	return length;
}
int main(){
	int n;
	cin>>n;
	int n1;
	int a,b;
	int a1=1;
	int length[n]={0};
	for(int i=0;i<n;i++){
		cin>>n1;
		memset(tree,0,sizeof(tree));
		for(int j=0;j<n1-1;j++){
			cin>>a>>b;
			if(j==0){
				a1=a;
			}
			tree[a][b]=1;
			tree[b][a]=1;
		}
		for(int j=0;j<n1;j++){
			int visited[101]={0};
			length[i]=max(length[i],dfs(j+1,visited)-1);
		}
	}
	cout<<accumulate(length,length+n,0);
}


#include <bits/stdc++.h>
using namespace std;
int tree[101][101];
int dfs(int i,int* visited){
	visited[i]=1;
	int length=1;
	for(int j=1;j<101;j++){
		if(tree[i][j])
		if(!visited[j]){
			length = max(length,dfs(j,visited)+1);
		}
	}
	return length;
}
int main(){
	int n;
	cin>>n;
	int n1;
	int a,b;
	int a1=1;
	int length[n]={0};
	int visited[101]={0};
	for(int i=0;i<n;i++){
		cin>>n1;
		memset(tree,0,sizeof(tree));
		for(int j=0;j<n1-1;j++){
			cin>>a>>b;
			if(j==0){
				a1=a;
			}
			tree[a][b]=1;
			tree[b][a]=1;
		}

		for(int j=0;j<n1;j++){
			memset(visited,0,sizeof(visited));
			length[i]=max(length[i],dfs(j+1,visited)-1);
		}
	}
	cout<<accumulate(length,length+n,0);
}


#include <bits/stdc++.h>
using namespace std;
int tree[101][101];
int dfs(int i,int* visited){
	visited[i]=1;
	int length=1;
	for(int j=1;j<101;j++){
		if(tree[i][j])
		if(!visited[j]){
			length = max(length,dfs(j,visited)+1);
		}
	}
	return length;
}
int main(){
	ifstream cin("input.txt");
	ofstream cout("output.txt");
	int n;
	cin>>n;
	int n1;
	int a,b;
	int a1=1;
	int length[n]={0};
	int visited[101]={0};
	for(int i=0;i<n;i++){
		cin>>n1;
		memset(tree,0,sizeof(tree));
		for(int j=0;j<n1-1;j++){
			cin>>a>>b;
			if(j==0){
				a1=a;
			}
			tree[a][b]=1;
			tree[b][a]=1;
		}

		for(int j=0;j<n1;j++){
			memset(visited,0,sizeof(visited));
			length[i]=max(length[i],dfs(j+1,visited)-1);
		}
	}
	cout<<accumulate(length,length+n,0);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <stack>
#include <string>

#define debug(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FORR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
#define v vector<int>
#define mp make_pair

using namespace std;
const int MAX= 1e2+10;
v x[MAX];
int total =0;
int m;

void dfs(int n,int p,int s){
	bool k = false;
	for(int i=0;i<x[n].size();i++){
		if(x[n][i]!=p){
			bool k = true;
			dfs(x[n][i],n,s+1);
		}
	}
	if(k == false){
		if(s>m){
			m = s;
		}
	}
}

void solve(){
	int n;
    cin>>n;
    int p,q,c;

    for(int i=0;i<MAX;i++){
    	x[i].clear();
    }

    REP(i,n-1){
    	cin>>p>>q;
    	x[p].pb(q);
    	x[q].pb(p);

    }
    for(int i=1;i<=n;i++){
    	dfs(i,-1,0);

    }
    total +=m;
    m=0;

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t;
	cin>>t;
    while(t--){
        solve();
    }
    cout<<total;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <stack>
#include <string>

#define debug(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FORR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
#define v vector<int>
#define mp make_pair

using namespace std;
const int MAX= 1e2+10;
v x[MAX];
int total =0;
int m;

void dfs(int n,int p,int s){
	bool k = false;
	for(int i=0;i<x[n].size();i++){
		if(x[n][i]!=p){
			bool k = true;
			dfs(x[n][i],n,s+1);
		}
	}
	if(k == false){
		if(s>m){
			m = s;
		}
	}
}

void solve(){
	int n;
    cin>>n;
    int p,q,c;

    for(int i=0;i<MAX;i++){
    	x[i].clear();
    }

    REP(i,n-1){
    	cin>>p>>q;
    	x[p].pb(q);
    	x[q].pb(p);

    }
    for(int i=1;i<=n;i++){
    	dfs(i,-1,0);

    }
    total +=m;
    m=0;

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    freopen("input.txt","r",stdin);
#ifndef LocalHost
	freopen("output.txt","w",stdout);
#endif
	int t;
	cin>>t;
    while(t--){
        solve();
    }
    cout<<total;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <stack>
#include <string>

#define debug(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FORR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
#define pb push_back
#define v vector<int>
#define mp make_pair

using namespace std;
const int MAX= 1e5;
v x[MAX];
int total =0;
int m;

void dfs(int n,int p,int s){
	bool k = false;
	for(int i=0;i<x[n].size();i++){
		if(x[n][i]!=p){
			bool k = true;
			dfs(x[n][i],n,s+1);
		}
	}
	if(k == false){
		if(s>m){
			m = s;
		}
	}
}

void solve(){
	int n;
    cin>>n;
    int p,q,c;

    for(int i=0;i<MAX;i++){
    	x[i].clear();
    }

    REP(i,n-1){
    	cin>>p>>q;
    	x[p].pb(q);
    	x[q].pb(p);

    }
    for(int i=1;i<=n;i++){
    	dfs(i,-1,0);

    }
    total +=m;
    m=0;

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t;
	cin>>t;
    while(t--){
        solve();
    }
    cout<<total;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
vector<int>adj[105];

int dfs(int x,int p){
    int mxx=0;
    for (auto u:adj[x]){
        if (u!=p){
            mxx=max(mxx,1+dfs(u,x));
        }
    }
    return mxx;
}

int main(){
    freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
    int t;
    scanf("%d",&t);
    int sum_m=0;
    while(t--){
        int n;
        scanf("%d",&n);
        for (int i=0;i<=n;i++) adj[i].clear();
        for (int i=1;i<n;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            // cin>>x>>y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        int mx=1;
        for (int i=1;i<=n;i++){
            mx=max(mx,dfs(i,0));
        }
        sum_m+=mx;
    }
    printf("%d",sum_m);
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
vector<int>adj[105];

int dfs(int x,int p){
    int mxx=0;
    for (auto u:adj[x]){
        if (u!=p){
            mxx=max(mxx,1+dfs(u,x));
        }
    }
    return mxx;
}

int main(){
    int t;
    cin>>t;
    int sum_m=0;
    while(t--){
        int n;
        cin>>n;
        for (int i=0;i<=n;i++) adj[i].clear();
        for (int i=1;i<n;i++){
            int x,y;
            cin>>x>>y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        int mx=1;
        for (int i=1;i<=n;i++){
            mx=max(mx,dfs(i,0));
        }
        sum_m+=mx;
    }
    cout<<sum_m;
}


#include <bits/stdc++.h>
using namespace std;
#define LL long long
vector<int>adj[105];

int dfs(int x,int p){
    int mxx=0;
    for (auto u:adj[x]){
        if (u!=p){
            mxx=max(mxx,1+dfs(u,x));
        }
    }
    return mxx;
}

int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int t;
    cin>>t;
    int sum_m=0;
    while(t--){
        int n;
        cin>>n;
        for (int i=0;i<=n;i++) adj[i].clear();
        for (int i=1;i<n;i++){
            int x,y;
            cin>>x>>y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        int mx=1;
        for (int i=1;i<=n;i++){
            mx=max(mx,dfs(i,0));
        }
        sum_m+=mx;
    }
    cout<<sum_m;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;
int h;

int dfs(int node, vector<bool> & visited, vector<vector<int>> & graph, int len = 0){

    visited[node] = 1;
    int tmpAns = len;
    for(auto ch: graph[node]){
        if(visited[ch])continue;
        tmpAns = max(tmpAns, dfs(ch, visited, graph, len+1));
    }

    return tmpAns;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    int n;

    cin >> n;
    int ans = 0;
    while(n--){
        cin >> h;
        vector< vector<int> > arr(h+1, vector<int>());
        int node;
        for(int i = 0; i < h-1; i++){
            int x,y;
            cin >> x >> y;
            x--;y--;
            node = x;
            arr[x].push_back(y);
            arr[y].push_back(x);

        }
        vector<bool> visited(n+1, 0);

        ans += dfs(node,visited, arr) + 1;
        //cout << ans << endl;
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;
int h;

int dfs(int node, vector<bool> & visited, vector<vector<int>> & graph, int len = 0){

    visited[node] = 1;
    int tmpAns = len;
    for(auto ch: graph[node]){
        if(visited[ch])continue;
        tmpAns = max(tmpAns, dfs(ch, visited, graph, len+1));
    }

    return tmpAns;
}
int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    int n;

    cin >> n;
    int ans = 0;
    while(n--){
        cin >> h;
        vector< vector<int> > arr(h+1, vector<int>());
        int node;
        for(int i = 0; i < h-1; i++){
            int x,y;
            cin >> x >> y;
            x--;y--;

            arr[x].push_back(y);
            arr[y].push_back(x);
            //if(arr[x].size() == 1)node = x;
            //if(arr[y].size() == 1)node = y;
        }
        vector<bool> visited(h+1, 0);
        int tmpAns = 0;
        for(int i = 0; i < h; i++){
            tmpAns = max(tmpAns, dfs(i, visited, arr));
            fill(visited.begin(), visited.end(), 0);

        }
        ans += tmpAns;
        //cout << tmpAns << endl;
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;
int h;

int dfs(int node, vector<bool> & visited, vector<vector<int>> & graph, int len = 0){

    visited[node] = 1;
    int tmpAns = len;
    for(auto ch: graph[node]){
        if(visited[ch])continue;
        tmpAns = max(tmpAns, dfs(ch, visited, graph, len+1));
    }

    return tmpAns;
}
int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    int n;

    cin >> n;
    int ans = 0;
    while(n--){
        cin >> h;
        vector< vector<int> > arr(h+1, vector<int>());
        int node;
        for(int i = 0; i < h-1; i++){
            int x,y;
            cin >> x >> y;
            x--;y--;

            arr[x].push_back(y);
            arr[y].push_back(x);
            //if(arr[x].size() == 1)node = x;
            //if(arr[y].size() == 1)node = y;
        }
        vector<bool> visited(h+1, 0);
        int tmpAns = 0;
        for(int i = 0; i < h; i++){
            tmpAns = max(tmpAns, dfs(i, visited, arr));
            fill(visited.begin(), visited.end(), 0);

        }
        ans += tmpAns;
        //cout << tmpAns << endl;
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back

using namespace std;
int h;

int dfs(int node, vector<bool> & visited, vector<vector<int>> & graph, int len = 0){

    visited[node] = 1;
    int tmpAns = len;
    for(auto ch: graph[node]){
        if(visited[ch])continue;
        tmpAns = max(tmpAns, dfs(ch, visited, graph, len+1));
    }

    return tmpAns;
}
int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);

    int n;

    cin >> n;
    int ans = 0;
    while(n--){
        cin >> h;
        vector< vector<int> > arr(h+1, vector<int>());
        int node;
        for(int i = 0; i < h-1; i++){
            int x,y;
            cin >> x >> y;
            x--;y--;
            node = x;
            arr[x].push_back(y);
            arr[y].push_back(x);

        }
        vector<bool> visited(n+1, 0);

        ans += dfs(node,visited, arr) + 1;
        //cout << ans << endl;
    }
    cout << ans;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
//vector<ll> check(10,0);
ll dfs(vector<vector<ll> >graph, ll node,vector<ll> visited){

    visited[node]=1;

    ll ans=0;
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];
        if(!visited[xx]){
            ans=max(dfs(graph,xx,visited),ans);
        }
    }
    cout<<node<<"\n";
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;
        vector<vector<ll> >graph(x+1);
        //vector<ll> visited(x+1,0);
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        //visited[1]=1;
        vector<ll> arr;
        for(int i=1;i<=x;i++){
            vector<ll> visited(x+1,0);
            arr.push_back(dfs(graph,i,visited)-1);
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();

        ans.push_back(aa);
       // cout<<aa<<" ";
    }

    ll xans=0;
    for(int i=0;i<ans.size();i++)
    xans+=ans[i];
    cout<<xans<<"\n";
    //cout<<0<<"\n";
    // for(int i=1;i<4;i++){
    //     cout<<check[i]<<" ";
    // }

}
int main()
{

    fast;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
vector<ll> check(10,0);
ll dfs(vector<vector<ll> >graph, ll node,vector<ll> visited){

    visited[node]=1;

    ll ans=0;
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];
        if(!visited[xx]){
            ans=max(dfs(graph,xx,visited),ans);
        }
    }
    check[node]=ans+1;
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;
        vector<vector<ll> >graph(x+1);
        //vector<ll> visited(x+1,0);
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        //visited[1]=1;
        vector<ll> arr;
        for(int i=1;i<=x;i++){
            vector<ll> visited(x+1,0);
            arr.push_back(dfs(graph,i,visited)-1);
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();

        ans.push_back(aa);
       // cout<<aa<<" ";
    }

    ll xans=0;
    for(int i=0;i<ans.size();i++)
    xans+=ans[i];
    cout<<xans<<"\n";
    //cout<<0<<"\n";
    // for(int i=1;i<4;i++){
    //     cout<<check[i]<<" ";
    // }

}
int main()
{

    fast;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007

ll dfs(vector<vector<ll> >graph, ll node,vector<ll> visited){

    visited[node]=1;

    ll ans=0;
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];
        if(!visited[xx]){
            ans=max(dfs(graph,xx,visited),ans);
        }
    }
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;
        vector<vector<ll> >graph(x+1);
        vector<ll> visited(x+1,0);
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        visited[1]=1;
        vector<ll> arr;
        for(int i=0;i<graph[1].size();i++){
            arr.push_back(dfs(graph,graph[1][i],visited));
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();
        arr.pop_back();
        ll bb=arr.back();
        ans.push_back(aa+bb);
       // cout<<aa+bb<<" ";
    }

    ll xans=0;
    for(int i=0;i<ans.size();i++)
    xans+=ans[i];
    cout<<xans<<"\n";
    //cout<<0<<"\n";

}
int main()
{

    fast;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007

ll dfs(vector<vector<ll> >graph, ll node,vector<ll> visited){

    visited[node]=1;

    ll ans=0;
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];
        if(!visited[xx]){
            ans=max(dfs(graph,xx,visited),ans);
        }
    }
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;
        vector<vector<ll> >graph(x+1);
        vector<ll> visited(x+1,0);
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        visited[1]=1;
        vector<ll> arr;
        for(int i=0;i<graph[1].size();i++){
            arr.push_back(dfs(graph,graph[1][i],visited));
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();
        arr.pop_back();
        ll bb=arr.back();
        ans.push_back(aa+bb);
       // cout<<aa+bb<<" ";
    }

    // ll xans=0;
    // for(int i=0;i<ans.size();i++)
    // xans+=ans[i];
    // cout<<xans<<"\n";
    cout<<0<<"\n";

}
int main()
{

    fast;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
//vector<ll> check(10,0);
ll dfs(vector<vector<ll> >graph, ll node,vector<ll> visited){

    visited[node]=1;

    ll ans=0;
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];
        if(!visited[xx]){
            ans=max(dfs(graph,xx,visited),ans);
        }
    }
    //check[node]=ans+1;
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;
        vector<vector<ll> >graph(x+1);
        //vector<ll> visited(x+1,0);
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        //visited[1]=1;
        vector<ll> arr;
        for(int i=1;i<=x;i++){
            vector<ll> visited(x+1,0);
            arr.push_back(dfs(graph,i,visited)-1);
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();

        ans.push_back(aa);
       // cout<<aa<<" ";
    }

    ll xans=0;
    for(int i=0;i<ans.size();i++)
    xans+=ans[i];
    cout<<xans<<"\n";
    //cout<<0<<"\n";
    // for(int i=1;i<4;i++){
    //     cout<<check[i]<<" ";
    // }

}
int main()
{

    fast;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
vector<ll> check(10,0);
ll dfs(vector<vector<ll> >graph, ll node,vector<ll> visited){

    visited[node]=1;

    ll ans=0;
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];
        if(!visited[xx]){
            ans=max(dfs(graph,xx,visited),ans);
        }
    }
    check[node]=ans+1;
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;
        vector<vector<ll> >graph(x+1);
        vector<ll> visited(x+1,0);
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        visited[1]=1;
        vector<ll> arr;
        for(int i=1;i<=x;i++){
            arr.push_back(dfs(graph,i,visited)-1);
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();

        ans.push_back(aa);
        //cout<<aa+bb<<" ";
    }

    ll xans=0;
    for(int i=0;i<ans.size();i++)
    xans+=ans[i];
    cout<<xans<<"\n";
    //cout<<0<<"\n";
    // for(int i=1;i<10;i++){
    //     cout<<check[i]<<" ";
    // }

}
int main()
{

    fast;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007

ll dfs(vector<vector<ll> >graph, ll node,vector<ll> visited){

    visited[node]=1;

    ll ans=0;
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];
        if(!visited[xx]){
            ans=max(dfs(graph,xx,visited),ans);
        }
    }
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;
        vector<vector<ll> >graph(x+1);
        vector<ll> visited(x+1,0);
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        visited[1]=1;
        vector<ll> arr;
        for(int i=0;i<graph[1].size();i++){
            arr.push_back(dfs(graph,graph[1][i],visited));
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();
        arr.pop_back();
        ll bb=arr.back();
        ans.push_back(aa+bb);
       // cout<<aa+bb<<" ";
    }

    ll xans=0;
    for(int i=0;i<ans.size();i++)
    xans+=ans[i];
    cout<<xans<<"\n";

}
int main()
{

    fast;

    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
vector<ll> check(10,0);
vector<ll>graph[105];
ll visited[105];
ll dfs(ll node){

    visited[node]=1;

    ll ans=0;
   // cout<<graph[node].size()<<" ";
    for(int i=0;i<graph[node].size();i++){
        ll xx=graph[node][i];

        if(!visited[xx]){

            ans=max(dfs(xx),ans);
        }
    }
    // check[node]=ans+1;
    //cout<<visited[1]<<" "<<visited[2]<<" "<<visited[3]<<" "<<node<<"\n";
    return ans+1;
}
void solve(int casee,int tt){

    ll n;
    cin>>n;
    vector<ll> ans;
    while(n--){
        ll x;
        cin>>x;

        for(int i=0;i<=x;i++){
            graph[i].clear();
        }
        for(int i=0;i<x-1;i++){
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }

        //visited[1]=1;
        vector<ll> arr;
        for(int i=1;i<=x;i++){
           //vector<ll> visited(x+1,0);
           memset(visited,0,sizeof(visited));
            arr.push_back(dfs(i)-1);
        }
        arr.push_back(0);
        sort(arr.begin(),arr.end());
        ll aa=arr.back();

        ans.push_back(aa);
        //cout<<aa<<" ";
    }

    ll xans=0;
    for(int i=0;i<ans.size();i++)
    xans+=ans[i];
    cout<<xans<<"\n";
    //cout<<0<<"\n";
    // for(int i=1;i<4;i++){
    //     cout<<check[i]<<" ";
    // }

}
int main()
{

    fast;

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    // int t;
    // cin>>t;
    // for(int i=1;i<=t;i++){
    //     solve(i,t);
    // }
      solve(0,0);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653

using namespace std;

bool vis[105];
int lvl[105];

void bfs (vector< int > g[], int t)
{
        vector<int> v;
        v.pb(t);
        vis[t]=true;
        lvl[t]=1;
        while(v.size()!=0)
        {
                int k=v.front();
                v.erase(v.begin());
                for(int i=0; i<g[k].size(); i++)
                {
                        if(vis[g[k][i]]==false)
                        {
                                vis[g[k][i]]=true;
                                lvl[g[k][i]]=lvl[k]+1;
                                v.pb(g[k][i]);
                        }
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, len=0, i, j, k, s;
        ifstream infile;
        infile.open("input.txt");
        infile>>n;
        vector< int> g[105];
        while(n--)
        {
                int d=0;
                infile>>s;
                for(j=1; j<s; j++)
                {
                        int u, v;
                        infile>>u>>v;
                        g[u].pb(v);
                        g[v].pb(u);
                }
                for(j=1; j<=s; j++)
                {
                        for(k=0; k<105; k++) vis[k]=false;
                        for(k=0; k<105; k++) lvl[k]=1;
                        bfs(g, j);
                        int l1=lvl[1], l2=lvl[1];
                        for(k=1; k<=s; k++)
                        {
                                l1=min(l1, lvl[k]);
                                l2=max(l2, lvl[k]);
                        }
                        d = max(d, abs(l2-l1));
                }
                len=len+d;
                for(i=0; i<105; i++) g[i].clear();
        }
        ofstream outfile;
        outfile.open("output.txt");
        outfile<<len<<endl;
        outfile.close();
        return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653

using namespace std;

bool vis[105];
int lvl[105];

void bfs (vector< vector<int> > g, int t)
{
        vector<int> v;
        v.pb(t);
        vis[t]=true;
        lvl[t]=1;
        while(v.size()!=0)
        {
                int k=v.front();
                v.erase(v.begin());
                for(int i=0; i<g[k].size(); i++)
                {
                        if(vis[g[k][i]]==false)
                        {
                                vis[g[k][i]]=true;
                                lvl[g[k][i]]=lvl[k]+1;
                                v.pb(g[k][i]);
                        }
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, len=0, i, j, k, l, s;
        cin>>n;
        l=n;
        while(l--)
        {
                int d=0;
                cin>>s;
                vector< vector<int> > g(s+1);
                for(j=1; j<s; j++)
                {
                        int x, y;
                        cin>>x>>y;
                        g[x].pb(y);
                        g[y].pb(x);
                }
                for(j=1; j<=s; j++)
                {
                        for(k=0; k<105; k++) vis[k]=false;
                        for(k=0; k<105; k++) lvl[k]=1;
                        bfs(g, j);
                        int l1=lvl[1], l2=lvl[1];
                        for(k=1; k<=s; k++)
                        {
                                l1=min(l1, lvl[k]);
                                l2=max(l2, lvl[k]);
                        }
                        d = max(d, abs(l2-l1));
                }
                len=len+d;
        }
        cout<<len;
        return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653

using namespace std;

bool vis[105];
int lvl[105];

void bfs (vector< vector<int> > g, int t)
{
        vector<int> v;
        v.pb(t);
        vis[t]=true;
        lvl[t]=1;
        while(v.size()!=0)
        {
                int k=v.front();
                v.erase(v.begin());
                for(int i=0; i<g[k].size(); i++)
                {
                        if(vis[g[k][i]]==false)
                        {
                                vis[g[k][i]]=true;
                                lvl[g[k][i]]=lvl[k]+1;
                                v.pb(g[k][i]);
                        }
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, len=0, i, j, k, s;
        cin>>n;
        while(n--)
        {
                int d=0;
                cin>>s;
                vector< vector<int> > g(s+1);
                for(j=1; j<s; j++)
                {
                        int u, v;
                        cin>>u>>v;
                        g[u].pb(v);
                        g[v].pb(u);
                }
                for(j=1; j<=s; j++)
                {
                        for(k=0; k<105; k++) vis[k]=false;
                        for(k=0; k<105; k++) lvl[k]=1;
                        bfs(g, j);
                        int l1=lvl[1], l2=lvl[1];
                        for(k=1; k<=s; k++)
                        {
                                l1=min(l1, lvl[k]);
                                l2=max(l2, lvl[k]);
                        }
                        d = max(d, abs(l2-l1));
                }
                len=len+d;
        }
        cout<<len;
        return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653

using namespace std;

bool vis[105];
int lvl[105];

void bfs (vector< vector<int> > g, int t)
{
        vector<int> v;
        v.pb(t);
        vis[t]=true;
        lvl[t]=1;
        while(v.size()!=0)
        {
                int k=v.front();
                v.erase(v.begin());
                for(int i=0; i<g[k].size(); i++)
                {
                        if(vis[g[k][i]]==false)
                        {
                                vis[g[k][i]]=true;
                                lvl[g[k][i]]=lvl[k]+1;
                                v.pb(g[k][i]);
                        }
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, len=0, i, j, k, l, s;
        cin>>n;
        l=n;
        while(l--)
        {
                int d=0;
                cin>>s;
                vector< vector<int> > g(s+1);
                for(j=1; j<s; j++)
                {
                        int u, v;
                        cin>>u>>v;
                        g[u].pb(v);
                        g[v].pb(u);
                }
                for(j=1; j<=s; j++)
                {
                        for(k=0; k<105; k++) vis[k]=false;
                        for(k=0; k<105; k++) lvl[k]=1;
                        bfs(g, j);
                        int l1=lvl[1], l2=lvl[1];
                        for(k=1; k<=s; k++)
                        {
                                l1=min(l1, lvl[k]);
                                l2=max(l2, lvl[k]);
                        }
                        d = max(d, abs(l2-l1));
                }
                len=len+d;
        }
        cout<<len;
        return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653

using namespace std;

bool vis[105];
int lvl[105];

void bfs (vector< vector<int> > g, int t)
{
        vector<int> v;
        v.pb(t);
        vis[t]=true;
        lvl[t]=1;
        while(v.size()!=0)
        {
                int k=v.front();
                v.erase(v.begin());
                for(int i=0; i<g[k].size(); i++)
                {
                        if(vis[g[k][i]]==false)
                        {
                                vis[g[k][i]]=true;
                                lvl[g[k][i]]=lvl[k]+1;
                                v.pb(g[k][i]);
                        }
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, len=0, i, j, k, s;
        cin>>n;
        for(i=1; i<=n; i++)
        {
                int d=0;
                cin>>s;
                vector< vector<int> > g(s+1);
                for(j=1; j<s; j++)
                {
                        int u, v;
                        cin>>u>>v;
                        g[u].pb(v);
                        g[v].pb(u);
                }
                for(j=1; j<=s; j++)
                {
                        for(k=0; k<105; k++) vis[k]=false;
                        bfs(g, j);
                        int l1=lvl[1], l2=lvl[1];
                        for(k=1; k<=s; k++)
                        {
                                l1=min(l1, lvl[k]);
                                l2=max(l2, lvl[k]);
                        }
                        d = max(d, abs(l2-l1));
                }
                len=len+d;
        }
        cout<<len;
        return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653

using namespace std;

bool vis[105];
int lvl[105];

void bfs (vector< int > g[], int t)
{
        vector<int> v;
        v.pb(t);
        vis[t]=true;
        lvl[t]=1;
        while(v.size()!=0)
        {
                int k=v.front();
                v.erase(v.begin());
                for(int i=0; i<g[k].size(); i++)
                {
                        if(vis[g[k][i]]==false)
                        {
                                vis[g[k][i]]=true;
                                lvl[g[k][i]]=lvl[k]+1;
                                v.pb(g[k][i]);
                        }
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, len=0, i, j, k, s;
        cin>>n;
        vector< int> g[105];
        while(n--)
        {
                int d=0;
                cin>>s;
                for(j=1; j<s; j++)
                {
                        int u, v;
                        cin>>u>>v;
                        g[u].pb(v);
                        g[v].pb(u);
                }
                for(j=1; j<=s; j++)
                {
                        for(k=0; k<105; k++) vis[k]=false;
                        for(k=0; k<105; k++) lvl[k]=1;
                        bfs(g, j);
                        int l1=lvl[1], l2=lvl[1];
                        for(k=1; k<=s; k++)
                        {
                                l1=min(l1, lvl[k]);
                                l2=max(l2, lvl[k]);
                        }
                        d = max(d, abs(l2-l1));
                }
                len=len+d;
                for(i=0; i<105; i++) g[i].clear();
        }
        cout<<len;
        return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653

using namespace std;

bool vis[105];
int lvl[105];

void bfs (vector< vector<int> > g, int t)
{
        vector<int> v;
        v.pb(t);
        vis[t]=true;
        lvl[t]=1;
        while(v.size()!=0)
        {
                int k=v.front();
                v.erase(v.begin());
                for(int i=0; i<g[k].size(); i++)
                {
                        if(vis[g[k][i]]==false)
                        {
                                vis[g[k][i]]=true;
                                lvl[g[k][i]]=lvl[k]+1;
                                v.pb(g[k][i]);
                        }
                }
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        int n, len=0, i, j, k, s;
        cin>>n;
        for(i=1; i<=n; i++)
        {
                int d=0;
                cin>>s;
                vector< vector<int> > g(s+1);
                for(j=1; j<s; j++)
                {
                        int u, v;
                        cin>>u>>v;
                        g[u].pb(v);
                        g[v].pb(u);
                }
                for(j=1; j<=s; j++)
                {
                        for(k=0; k<105; k++) vis[k]=false;
                        for(k=0; k<105; k++) lvl[k]=1;
                        bfs(g, j);
                        int l1=lvl[1], l2=lvl[1];
                        for(k=1; k<=s; k++)
                        {
                                l1=min(l1, lvl[k]);
                                l2=max(l2, lvl[k]);
                        }
                        d = max(d, abs(l2-l1));
                }
                len=len+d;
        }
        cout<<len;
        return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

typedef unordered_set<int> uset;

int treeHeight(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    int maxHeight = -1;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (heights[*it] == -1) {
            heights[*it] = treeHeight(adj_list, heights, *it, root);
        }
        if (heights[*it] > maxHeight) {
            maxHeight = heights[*it];
        }
    }
    return maxHeight+1;
}

int longestHelper(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    //get the two tallest children
    int node1 = -1; //node1 is the taller of the two
    int node2 = -1;
    int height1 = -1;
    int height2 = -1;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (node1 == -1) {
            node1 = *it;
            height1 = heights[*it];
        } else if (node2 == -1) {
            if (*it > node1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else {
                node2 = *it;
                height2 = heights[node2];
            }
        } else {
            if (*it > node1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else if (*it > node2) {
                node2 = *it;
                height2 = heights[node2];
            }
        }
    }

    if (node1 == -1 && node2 == -1) {
        return 0;
    }

    if (height1 == height2) {
        //try both subtrees
        return max(longestHelper(adj_list, heights, node2, root), max(longestHelper(adj_list, heights, node1, root), 2+height1+height2));
    } else {
        //try subtree 1
        return max(longestHelper(adj_list, heights, node1, root), 2+height1+height2);
    }
}

int longestPath() {
    int n_i;
    cin >> n_i;
    vector<uset> adj_list(n_i);
    vector<int> heights(n_i, -1);
    for (int i = 1; i < n_i; i++) {
        int from, to;
        cin >> from >> to;
        from--;
        to--;
        adj_list[from].insert(to);
        adj_list[to].insert(from);
    }
    // arbitrary root selection is node 0
    heights[0] = treeHeight(adj_list, heights, 0, NULL);
    // cout << heights[0] << "\n";
    // return 0;
    return longestHelper(adj_list, heights, 0, NULL);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    int n, total = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        total += longestPath();
    }

    cout << total;

    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

typedef unordered_set<int> uset;

int treeHeight(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    int maxHeight = -1;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (heights[*it] == -1) {
            heights[*it] = treeHeight(adj_list, heights, *it, root);
        }
        if (heights[*it] > maxHeight) {
            maxHeight = heights[*it];
        }
    }
    return maxHeight+1;
}

int longestHelper(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    //get the two tallest children
    int node1 = -1; //node1 is the taller of the two
    int node2 = -1;
    int height1 = -1;
    int height2 = -1;
    vector<int> tallest;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (node1 == -1) {
            node1 = *it;
            height1 = heights[*it];
        } else if (node2 == -1) {
            if (heights[*it] > node1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else {
                node2 = *it;
                height2 = heights[node2];
            }
        } else {
            if (heights[*it] > height1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else if (heights[*it] > height2) {
                node2 = *it;
                height2 = heights[node2];
            }
        }
        tallest.push_back(longestHelper(adj_list, heights, *it, root));
    }
    int max = 2+height1+height2;
    for (int i = 0; i < tallest.size(); i++) {
        if (tallest[i] > max){
            max = tallest[i];
        }
    }
    return max;
    // if (node1 == -1 && node2 == -1) {
    //     return 0;
    // }

    // if (height1 == height2) {
    //     //try both subtrees
    //     return max(longestHelper(adj_list, heights, node2, root), max(longestHelper(adj_list, heights, node1, root), 2+height1+height2));
    // } else {
    //     //try subtree 1
    //     return max(longestHelper(adj_list, heights, node1, root), 2+height1+height2);
    // }
}

int longestPath() {
    int n_i;
    cin >> n_i;
    vector<uset> adj_list(n_i);
    vector<int> heights(n_i, -1);
    for (int i = 1; i < n_i; i++) {
        int from, to;
        cin >> from >> to;
        from--;
        to--;
        adj_list[from].insert(to);
        adj_list[to].insert(from);
    }
    // arbitrary root selection is node 0
    heights[0] = treeHeight(adj_list, heights, 0, NULL);

    return longestHelper(adj_list, heights, 0, NULL);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    int n, total = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        total += longestPath();
    }

    cout << total;

    return 0;
}


#include <bits/stdc++.h>
using namespace std; inline void fileio(const char* in, const char* out) { freopen(in, "r", stdin); freopen(out, "w", stdout); }
#define CM ,
#define TD(a,b) typedef a b
#define forx(i,n) for(int i = 0; i < (n); ++i)
TD(long long,ll); TD(long double,ld); TD(pair<int CM int>,pii); TD(pair<ll CM ll>,pll); TD(vector<int>,VI); TD(vector<bool>,VB); TD(vector<ll>, VL);
const char nl = '\n'; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; const ld EPS = 1e-9, PI = acos(-1);
#define ff first
#define ss second
struct Graph {
  class Edge { int sz; union { ll* d; ll d0; };
    void dalloc(int s) { if (s == sz) return;; if (sz > 1) free(d);; if (s <= 1) d0 = 0; else d = (ll*) malloc(s*sizeof(*d)); sz = s; }
    public: int u, v; Edge() : sz(0), d(NULL) { } Edge(int u, int v, int s=0) : sz(0), d(NULL), u(u), v(v) { dalloc(s); if (sz > 1) fill(d, d+sz, 0ll); }
    ~Edge() { if (sz > 1) free(d); } Edge(const Edge& b) : sz(0) { *this = b; } Edge& operator =(const Edge& ref) {
      dalloc(ref.sz); u = ref.u; v = ref.v; if (sz <= 1) d0 = ref.d0; else copy(ref.d, ref.d+sz, d);; return *this; }
    ll& operator[](int i) { return (sz <= 1 ? d0 : d[i]); } int operator()(int f) const { return f ^ u ^ v; } }; typedef vector<Edge> VE;
  class EdgeIt { VI* adj; VE* edge_list; public: EdgeIt(VI* adj, VE* edge_list) : adj(adj), edge_list(edge_list) {}
    class Iterator { VI* adj; VE* edge_list; int idx; public: Iterator(VI* adj, VE* edge_list, int idx=0) : adj(adj), edge_list(edge_list), idx(idx) {}
      bool operator == (const Iterator& b) const { return adj == b.adj && idx == b.idx; } bool operator != (const Iterator& b) const { return !(*this == b); }
      Iterator operator ++() { auto temp = *this; idx++; return temp; } Edge& operator *() { return (*edge_list)[(*adj)[idx]]; } };
    Iterator begin() const { return Iterator(adj, edge_list); } Iterator end() const { return Iterator(adj, edge_list, adj->size()); } };
  Graph(int N, int D=0) : edge_list(0), adj_list(N), dimens(D) { edge_list.reserve(N); }
  int add_edge(int u, int v, bool undirected=true) { adj_list[u].push_back(edge_list.size()); if (undirected) adj_list[v].push_back(edge_list.size());
    edge_list.emplace_back(Edge(u, v, dimens)); return edge_list.size() - 1; }
  ll N() const { return adj_list.size(); } ll M() const { return edge_list.size(); } ll D() const { return dimens; }
  VI& getAdjVec(int u) { return adj_list[u]; }
  VE& edges() { return edge_list; } int out_degree(int u) const { return adj_list[u].size(); }
  Edge& operator[](int i) { return edge_list[i]; } EdgeIt getAdj(int u) { return EdgeIt(&adj_list[u], &edge_list); }
  // check if u is a leaf w/ respect to p. ie. is u's only edge p
  bool isLeaf(int u, int p) const {
    return adj_list[u].size() == 1 && edge_list[adj_list[u][0]](u) == p;
  }
  private: VE edge_list; vector<VI> adj_list; const int dimens; }; typedef Graph::Edge Edge;

// Finds the diameter of a tree graph
// @param g     the graph
// @param i     the node to use as the root
int tree_diameter(Graph& g, int i = 0) {
  // ff is the diameter so far (which could be the branch)
  // ss is the longest branch
  function<pii(int,int)> helper = [&](int u, int p) {
    //check leaf
    if (g.isLeaf(u,p)) {
      return make_pair(0,0);
    }
    int diam = 0; // biggest diameter so far
    int best = 0; // biggest branch so far
    for (Edge e : g.getAdj(u)) {
      if (e(u) == p) continue;
      pii temp = helper(e(u), u);
      diam = max(diam, temp.ff);
      diam = max(diam, best + temp.ss + 1);
      best = max(best, temp.ss + 1);
    }
    return make_pair(diam, best);
  };
  return helper(i, -1).ff;
}

int spider() {
  int n;
  cin >> n;
  Graph g(n+1);
  forx(i, n-1) {
    int u, v;
    cin >> u >> v;
    g.add_edge(u,v);
  }
  return tree_diameter(g,1); // root at 1 because 1 indexed tree
}

int main() {
  fileio("input.txt", "output.txt");
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // Emily <3
  int n;
  cin >> n;
  int ans = 0;
  forx(i,n) {
    ans += spider();
  }

  cout << ans << nl;

  cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl;
  return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

typedef unordered_set<int> uset;

int treeHeight(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    int maxHeight = -1;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (heights[*it] == -1) {
            heights[*it] = treeHeight(adj_list, heights, *it, root);
        }
        if (heights[*it] > maxHeight) {
            maxHeight = heights[*it];
        }
    }
    return maxHeight+1;
}

int longestHelper(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    //get the two tallest children
    int node1 = -1; //node1 is the taller of the two
    int node2 = -1;
    int height1 = -1;
    int height2 = -1;
    vector<int> tallest;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (node1 == -1) {
            node1 = *it;
            height1 = heights[*it];
        } else if (node2 == -1) {
            if (heights[*it] > node1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else {
                node2 = *it;
                height2 = heights[node2];
            }
        } else {
            if (heights[*it] > height1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else if (heights[*it] > height2) {
                node2 = *it;
                height2 = heights[node2];
            }
        }
        tallest.push_back(longestHelper(adj_list, heights, *it, root));
    }
    int max = 2+height1+height2;
    for (int i = 0; i < tallest.size(); i++) {
        if (tallest[i] > max){
            max = tallest[i];
        }
    }
    return max;
    // if (node1 == -1 && node2 == -1) {
    //     return 0;
    // }

    // if (height1 == height2) {
    //     //try both subtrees
    //     return max(longestHelper(adj_list, heights, node2, root), max(longestHelper(adj_list, heights, node1, root), 2+height1+height2));
    // } else {
    //     //try subtree 1
    //     return max(longestHelper(adj_list, heights, node1, root), 2+height1+height2);
    // }
}

int longestPath() {
    int n_i;
    cin >> n_i;
    vector<uset> adj_list(n_i);
    vector<int> heights(n_i, -1);
    for (int i = 1; i < n_i; i++) {
        int from, to;
        cin >> from >> to;
        from--;
        to--;
        adj_list[from].insert(to);
        adj_list[to].insert(from);
    }
    // arbitrary root selection is node 0
    heights[0] = treeHeight(adj_list, heights, 0, NULL);

    return longestHelper(adj_list, heights, 0, NULL);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    int n, total = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        total += longestPath();
    }

    cout << total;

    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

typedef unordered_set<int> uset;

int treeHeight(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    int maxHeight = -1;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (heights[*it] == -1) {
            heights[*it] = treeHeight(adj_list, heights, *it, root);
        }
        if (heights[*it] > maxHeight) {
            maxHeight = heights[*it];
        }
    }
    return maxHeight+1;
}

int longestHelper(vector<uset> & adj_list, vector<int> & heights, int root, int parent) {
    //get the two tallest children
    int node1 = -1; //node1 is the taller of the two
    int node2 = -1;
    int height1 = -1;
    int height2 = -1;
    // vector<int> tallest;
    for (auto it = adj_list[root].begin(); it != adj_list[root].end(); it++) {
        if (*it == parent) {continue;}
        if (node1 == -1) {
            node1 = *it;
            height1 = heights[*it];
        } else if (node2 == -1) {
            if (heights[*it] > node1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else {
                node2 = *it;
                height2 = heights[node2];
            }
        } else {
            if (heights[*it] > height1) {
                node2 = node1;
                height2 = height1;
                node1 = *it;
                height1 = heights[node1];
            } else if (heights[*it] > height2) {
                node2 = *it;
                height2 = heights[node2];
            }
        }
        // tallest.push_back(longestHelper(adj_list, heights, *it, root));
    }
    // int max = 2+height1+height2;
    // for (int i = 0; i < tallest.size(); i++) {
    //     if (tallest[i] > max){
    //         max = tallest[i];
    //     }
    // }
    // return max;
    if (node1 == -1 && node2 == -1) {
        return 0;
    }

    if (height1 == height2) {
        //try both subtrees
        return max(longestHelper(adj_list, heights, node2, root), max(longestHelper(adj_list, heights, node1, root), 2+height1+height2));
    } else {
        //try subtree 1
        return max(longestHelper(adj_list, heights, node1, root), 2+height1+height2);
    }
}

int longestPath() {
    int n_i;
    cin >> n_i;
    vector<uset> adj_list(n_i);
    vector<int> heights(n_i, -1);
    for (int i = 1; i < n_i; i++) {
        int from, to;
        cin >> from >> to;
        from--;
        to--;
        adj_list[from].insert(to);
        adj_list[to].insert(from);
    }
    // arbitrary root selection is node 0
    heights[0] = treeHeight(adj_list, heights, 0, NULL);

    return longestHelper(adj_list, heights, 0, NULL);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

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

    int n, total = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        total += longestPath();
    }

    cout << total;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define clr(x) memset(x,0,sizeof(x))
#define all(x) x.begin(),x.end()
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
const int N=1e5+1;
const int INF=1e9+7;
const int inf=1e18;

vector <int> adj[N];
bool vis[N];
int dist[N];
int maxn; int maxd;

void dfs(int node , int d)
{
    vis[node]=1;
    dist[node]=d;
    if(d>maxd){maxd=d ; maxn = node;}

    for(auto it : adj[node])
    {
        if(!vis[it]){dfs(it,d+1);}
    }
}

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

 //ios_base::sync_with_stdio(0);
 //cin.tie(0); cout.tie(0);
 int n; cin>>n; int ans=0;
 for(int i=0;i<n;i++)
 {
     for(int ii=0;ii<200;ii++){adj[ii].clear();}
     clr(vis); clr(dist); maxd=0;
     int x; cin>>x;
     for(int j=0;j<x-1;j++){int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a);}
     dfs(1,0);
     maxd=0; clr(vis);
     dfs(maxn,0);
     ans+=maxd;
 }
 cout<<ans<<endl;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define clr(x) memset(x,0,sizeof(x))
#define all(x) x.begin(),x.end()
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
const int N=1e6+1;
const int INF=1e9+7;
const int inf=1e18;

vector <int> adj[N];
bool vis[N];
int dist[N];
int maxn; int maxd;

void dfs(int node , int d)
{
    vis[node]=1;
    dist[node]=d;
    if(d>maxd){maxd=d ; maxn = node;}

    for(auto it : adj[node])
    {
        if(!vis[it]){dfs(it,d+1);}
    }
}

int32_t main()
{
 ios_base::sync_with_stdio(0);
 cin.tie(0); cout.tie(0);
 int n; cin>>n; int ans=0;
 for(int i=0;i<n;i++)
 {
     for(int ii=0;ii<N;ii++){adj[ii].clear();}
     clr(vis); clr(dist);
     int x; cin>>x;
     for(int j=0;j<x-1;j++){int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a);}
     dfs(1,0);
     maxd=0; clr(vis);
     dfs(maxn,0);
     ans+=maxd;
 }
 cout<<ans<<endl;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define clr(x) memset(x,0,sizeof(x))
#define all(x) x.begin(),x.end()
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
const int N=1e5+1;
const int INF=1e9+7;
const int inf=1e18;

vector <int> adj[N];
bool vis[N];
int dist[N];
int maxn; int maxd;

void dfs(int node , int d)
{
    vis[node]=1;
    dist[node]=d;
    if(d>maxd){maxd=d ; maxn = node;}

    for(auto it : adj[node])
    {
        if(!vis[it]){dfs(it,d+1);}
    }
}

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

 //ios_base::sync_with_stdio(0);
 //cin.tie(0); cout.tie(0);
 int n; cin>>n; int ans=0;
 for(int i=0;i<n;i++)
 {
     for(int ii=0;ii<N;ii++){adj[ii].clear();}
     clr(vis); clr(dist);
     int x; cin>>x;
     for(int j=0;j<x-1;j++){int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a);}
     dfs(1,0);
     maxd=0; clr(vis);
     dfs(maxn,0);
     ans+=maxd;
 }
 cout<<ans<<endl;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define clr(x) memset(x,0,sizeof(x))
#define all(x) x.begin(),x.end()
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
const int N=1e6+1;
const int INF=1e9+7;
const int inf=1e18;

vector <int> adj[N];
bool vis[N];
int dist[N];
int maxn; int maxd;

void dfs(int node , int d)
{
    vis[node]=1;
    dist[node]=d;
    if(d>maxd){maxd=d ; maxn = node;}

    for(auto it : adj[node])
    {
        if(!vis[it]){dfs(it,d+1);}
    }
}

int32_t main()
{
 ios_base::sync_with_stdio(0);
 cin.tie(0); cout.tie(0);
 int n; cin>>n; int ans=0;
 for(int i=0;i<n;i++)
 {
     for(int ii=0;ii<=N;ii++){adj[ii].clear();}
     clr(vis); clr(dist);
     int x; cin>>x;
     for(int j=0;j<x-1;j++){int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a);}
     dfs(1,0);
     maxd=0; clr(vis);
     dfs(maxn,0);
     ans+=maxd;
 }
 cout<<ans<<endl;
 }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define clr(x) memset(x,0,sizeof(x))
#define all(x) x.begin(),x.end()
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
const int N=1e5+1;
const int INF=1e9+7;
const int inf=1e18;

vector <int> adj[N];
bool vis[N];
int dist[N];
int maxn; int maxd;

void dfs(int node , int d)
{
    vis[node]=1;
    dist[node]=d;
    if(d>maxd){maxd=d ; maxn = node;}

    for(auto it : adj[node])
    {
        if(!vis[it]){dfs(it,d+1);}
    }
}

int32_t main()
{
 ios_base::sync_with_stdio(0);
 cin.tie(0); cout.tie(0);
 int n; cin>>n; int ans=0;
 for(int i=0;i<n;i++)
 {
     for(int ii=0;ii<N;ii++){adj[ii].clear();}
     clr(vis); clr(dist);
     int x; cin>>x;
     for(int j=0;j<x-1;j++){int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a);}
     dfs(1,0);
     maxd=0; clr(vis);
     dfs(maxn,0);
     ans+=maxd;
 }
 cout<<ans<<endl;
 }


#include <bits/stdc++.h>
#define int long long int
#define us unsigned int
#define null 0
#define modulo 1000000007
#define m_p make_pair
#define f_i first
#define s_e second
#define p_b push_back

using namespace std;

void IOS()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

int32_t main()
{

    #ifndef ONLINE_JUDGE
    freopen("int.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif

    int tot=0;
    int test;
    cin>>test;

    while(test--)
    {
        int n;
        cin>>n;
        vector<int> v[n];

        for(int i=0;i<n-1;i++)
        {
            int x,y;
            cin>>x>>y;
            x--,y--;
            v[x].p_b(y);
            v[y].p_b(x);
        }

        bool vis[n];
        int lev[n];
        memset(vis,false,sizeof(vis));

        vis[0]=true;
        queue<int> q;
        q.push(0);
        lev[0]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        int maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        //cout<<lev[maxx]<<endl;

        memset(vis,false,sizeof(vis));

        vis[maxx]=true;
        q.push(maxx);
        lev[maxx]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        tot+=lev[maxx];

    }

    cout<<tot;

}


#include <bits/stdc++.h>
#define int long long int
#define us unsigned int
#define null 0
#define modulo 1000000007
#define m_p make_pair
#define f_i first
#define s_e second
#define p_b push_back

using namespace std;

void IOS()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

int32_t main()
{

    //IOS();

    #ifdef _DEBUG
        freopen("input.txt", "r", stdin);
    //	freopen("output.txt", "w", stdout);
    #endif

    int tot=0;
    int test;
    cin>>test;

    while(test--)
    {
        int n;
        cin>>n;
        vector<int> v[n];

        for(int i=0;i<n-1;i++)
        {
            int x,y;
            cin>>x>>y;
            x--,y--;
            v[x].p_b(y);
            v[y].p_b(x);
        }

        bool vis[n];
        int lev[n];
        memset(vis,false,sizeof(vis));

        vis[0]=true;
        queue<int> q;
        q.push(0);
        lev[0]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        int maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        //cout<<lev[maxx]<<endl;

        memset(vis,false,sizeof(vis));

        vis[maxx]=true;
        q.push(maxx);
        lev[maxx]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        tot+=lev[maxx];

    }

    cout<<tot;

}


#include <bits/stdc++.h>
#define int long long int
#define us unsigned int
#define null 0
#define modulo 1000000007
#define m_p make_pair
#define f_i first
#define s_e second
#define p_b push_back

using namespace std;

void IOS()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

int32_t main()
{

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

    int tot=0;
    int test;
    cin>>test;

    while(test--)
    {
        int n;
        cin>>n;
        vector<int> v[n];

        for(int i=0;i<n-1;i++)
        {
            int x,y;
            cin>>x>>y;
            x--,y--;
            v[x].p_b(y);
            v[y].p_b(x);
        }

        bool vis[n];
        int lev[n];
        memset(vis,false,sizeof(vis));

        vis[0]=true;
        queue<int> q;
        q.push(0);
        lev[0]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        int maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        //cout<<lev[maxx]<<endl;

        memset(vis,false,sizeof(vis));

        vis[maxx]=true;
        q.push(maxx);
        lev[maxx]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        tot+=lev[maxx];

    }

    cout<<tot;

}


#include <bits/stdc++.h>
#define int long long int
#define us unsigned int
#define null 0
#define modulo 1000000007
#define m_p make_pair
#define f_i first
#define s_e second
#define p_b push_back

using namespace std;

void IOS()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

int32_t main()
{

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

    int tot=0;
    int test;
    cin>>test;

    while(test--)
    {
        int n;
        cin>>n;
        vector<int> v[n];

        for(int i=0;i<n-1;i++)
        {
            int x,y;
            cin>>x>>y;
            x--,y--;
            v[x].p_b(y);
            v[y].p_b(x);
        }

        bool vis[n];
        int lev[n];
        memset(vis,false,sizeof(vis));

        vis[0]=true;
        queue<int> q;
        q.push(0);
        lev[0]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        int maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        //cout<<lev[maxx]<<endl;

        memset(vis,false,sizeof(vis));

        vis[maxx]=true;
        q.push(maxx);
        lev[maxx]=0;
        while(!(q.empty()))
        {
            int s=q.front();
            q.pop();
            for(int i=0;i<v[s].size();i++)
            {
                int curr=v[s][i];
                if(vis[curr]==false)
                {
                    lev[curr]=lev[s]+1;
                    vis[curr]=true;
                    q.push(curr);
                }
            }
        }

        maxx=0;

        for(int i=0;i<n;i++)
        {
            if(lev[i]>lev[maxx])
            {
                maxx=i;
            }
        }

        tot+=lev[maxx];

    }

    cout<<tot;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;
typedef gp_hash_table<long int,long int> HashTable;

int main(){
   IOS;
 /*    #ifndef ONLINE_JUDGE
         freopen("in.txt",  "r",  stdin);
         freopen("out.txt", "w", stdout);
     #endif */
    long int open = 0,complete = 0;

    string s;
    cin>>s;

    for(auto ele : s){
      if(ele == '(')
        open++;
      else{
        if(open){
          open--;
          complete++;
        }
      }
    }

    cout<< 2 * complete;

   return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

class Graph{
    long int v;
    unordered_map<long int, list<long int>> adj;

    public:
        Graph(long int v) : v(v){

        }

        void addEdge(long int u, long int v){
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        void DFSUtil(long int s, unordered_set<long int> &visited, long int &ans, long int count = 0){
            visited.insert(s);
            ans = max(ans,count);

            for(auto ele : adj[s]){
                if(visited.find(ele) == visited.end()){
                    DFSUtil(ele,visited,ans,count+1);
                }
            }
        }

        long int DFS(){
            long int ans = 0;

            for(auto ele : adj){
                unordered_set<long int> visited;
                DFSUtil(ele.first,visited,ans,0);
            }
            // cout<<ans<<endl;
            return ans;
        }
};

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

    long int ans = 0;
    for(long int j = 0; j < n; j++){
        long int v;
        cin>>v;

        Graph g(v);
        for(long int i = 0; i < v-1; i++){
            long int a,b;
            cin>>a>>b;
            g.addEdge(a,b);
        }
        ans += g.DFS();
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
        //use cerr if u want to display at the bottom
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
typedef tree<long int,null_type,less<long int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef cc_hash_table<long int,long int, hash<long int>, equal_to<long int>, direct_mask_range_hashing<long int>,
hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;
typedef gp_hash_table<long int,long int> HashTable;

vector<long int> diameter;

long int BFS2(vector<vector<long int>> &g, long int root, long int n){
 vector<long int> distance(n,-1), parent(n,-1);
 distance[root] = 0;

 queue<long int> q;
 q.push(root);

 while(q.size()){
   auto ele = q.front();
   q.pop();

   for(auto e : g[ele]){
     if(distance[e] == -1){
       parent[e] = ele;
       distance[e] = distance[ele] + 1;
       q.push(e);
     }
   }
 }

 long int desired = max_element(distance.begin(),distance.end()) - distance.begin();
 auto copy = desired;

 while(copy != -1){
  diameter.push_back(copy);
  copy = parent[copy];
 }

 return distance[desired];
}

long int BFS1(vector<vector<long int>> &g, long int root, long int n){
 vector<long int> distance(n,-1);
 distance[root] = 0;

 queue<long int> q;
 q.push(root);

 while(q.size()){
   auto ele = q.front();
   q.pop();

   for(auto e : g[ele]){
     if(distance[e] == -1){
       distance[e] = distance[ele] + 1;
       q.push(e);
     }
   }
 }

 return max_element(distance.begin(),distance.end()) - distance.begin();
}

long int findDiameter(vector<vector<long int>> &g, long int n){
 long int node1 = BFS1(g,0,n);
 return BFS2(g,node1,n);

}

int main(){
   IOS;
     // #ifndef ONLINE_JUDGE
         freopen("input.txt",  "r",  stdin);
         freopen("output.txt", "w", stdout);
     // #endif

   long int t;
   cin>>t;

   long int ans = 0;

   while(t--){
    long int n;
    cin>>n;

    vector<vector<long int>> g(n);

    for(long int i = 1; i < n; i++){
      long int a,b;
      cin>>a>>b;
      g[a-1].push_back(b-1);
      g[b-1].push_back(a-1);
    }
    ans += findDiameter(g,n);
   }

   cout<<ans;

   return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

class Graph{
	long int v;
	list<long int> *adj;

	public:
		Graph(long int v) : v(v){
			adj = new list<long int>[v];
		}

		void addEdge(long int u, long int v){
			adj[u].push_back(v);
			adj[v].push_back(u);
		}

		long int findLastVertex(){
			vector<long int> distance(v,LONG_MAX);
			queue<long int> q;
			distance[0] = 0;
			q.push(0);

			while(!q.empty()){
				auto ele = q.front();
				q.pop();

				for(auto e : adj[ele]){
					if(distance[e] == LONG_MAX){
						distance[e] = distance[ele] + 1;
						q.push(e);
					}
				}
			}
			return max_element(distance.begin(),distance.end()) - distance.begin();
		}

		long int findDiameter(long int lastVertex){
			vector<long int> distance(v,LONG_MAX);
			queue<long int> q;
			distance[lastVertex] = 0;
			q.push(lastVertex);

			while(!q.empty()){
				auto ele = q.front();
				q.pop();

				for(auto e : adj[ele]){
					if(distance[e] == LONG_MAX){
						distance[e] = distance[ele] + 1;
						q.push(e);
					}
				}
			}
			return *max_element(distance.begin(),distance.end());
		}
};

int main(){
   IOS;

   long int n;
   cin>>n;

   long int ans = 0;

   while(n--){
   	long int v;
   	cin>>v;
   	Graph g(v);

   	for(long int i = 0; i < v-1; i++){
   		long int a,b;
   		cin>>a>>b;
   		g.addEdge(a-1,b-1);
   	}
   	ans += g.findDiameter(g.findLastVertex());
   }
   cout<<ans;
   return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

class Graph{
	long int v;
	list<long int> *adj;

	public:
		Graph(long int v) : v(v){
			adj = new list<long int>[v];
		}

		void addEdge(long int u, long int v){
			adj[u].push_back(v);
			adj[v].push_back(u);
		}

		long int findLastVertex(){
			vector<long int> distance(v,LONG_MAX);
			queue<long int> q;
			distance[0] = 0;
			q.push(0);

			while(!q.empty()){
				auto ele = q.front();
				q.pop();

				for(auto e : adj[ele]){
					if(distance[e] == LONG_MAX){
						distance[e] = distance[ele] + 1;
						q.push(e);
					}
				}
			}
			// for(auto ele : distance)
			// 	cout<<ele<<" ";
			// cout<<endl;
			return max_element(distance.begin(),distance.end()) - distance.begin();
		}

		long int findDiameter(long int lastVertex){
			vector<long int> distance(v,LONG_MAX);
			queue<long int> q;
			distance[lastVertex] = 0;
			q.push(lastVertex);

			while(!q.empty()){
				auto ele = q.front();
				q.pop();

				for(auto e : adj[ele]){
					if(distance[e] == LONG_MAX){
						distance[e] = distance[ele] + 1;
						q.push(e);
					}
				}
			}
			return *max_element(distance.begin(),distance.end());
		}
};

int main(){
   IOS;

   long int n;
   cin>>n;

   long int ans = 0;

   while(n--){
   	long int v;
   	cin>>v;
   	Graph g(v);

   	for(long int i = 0; i < v-1; i++){
   		long int a,b;
   		cin>>a>>b;
   		g.addEdge(a-1,b-1);
   	}
   	// cout<<ans<<endl;
   	ans += g.findDiameter(g.findLastVertex());
   }
   cout<<ans;
   return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define long long long

class Graph{
    long int v;
    unordered_map<long int, list<long int>> adj;

    public:
        Graph(long int v) : v(v){

        }

        void addEdge(long int u, long int v){
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        void DFSUtil(long int s, unordered_set<long int> &visited, long int &ans, long int count = 0){
            visited.insert(s);
            ans = max(ans,count);

            for(auto ele : adj[s]){
                if(visited.find(ele) == visited.end()){
                    DFSUtil(ele,visited,ans,count+1);
                }
            }
        }

        long int DFS(){
            long int ans = 0;

                unordered_set<long int> visited;
                DFSUtil(1,visited,ans,0);
            // cout<<ans<<endl;
            return ans;
        }
};

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

    long int ans = 0;
    for(long int j = 0; j < n; j++){
        long int v;
        cin>>v;

        Graph g(v);
        for(long int i = 0; i < v-1; i++){
            long int a,b;
            cin>>a>>b;
            g.addEdge(a,b);
        }
        ans += g.DFS();
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define nl "\n"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fl(i,a,b) for(int i=a;i<b;i+=1)
#define rl(i,a,b) for(int i=a;i>b;i-=1)
#define input(a,n) fl(i,0,n) cin>>a[i];
#define print(a,n) fl(i,0,n) cout<<a[i]<<' ';
#define all(a) a.begin(),a.end()
#define test() int t; cin>>t; while(t--)
#define maxelement(arr,n) *max_element(arr,arr+n)
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int N = 1e6 + 1;
using namespace std;

vector<int> v[105];
int visited[105]={0};

int dfs(int n){
	int ans=0;
	visited[n]=1;
	for(int i=0;i<v[n].size();i++){
		if(visited[v[n][i]]==0){
			ans=max(ans,dfs(v[n][i])+1);
		}
	}
	return ans;
}

int main(){
	iamspeed;

	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	int n;
	cin>>n;
	int sum=0;
	for(int i=0;i<n;i++){
		int x,a,b;
		fl(i,0,105){
			v[i].clear();
		}
		cin>>x;
		fl(i,0,x-1){
			cin>>a>>b;
			v[a].pb(b);
			v[b].pb(a);
		}
		int mx=0;
		fl(i,1,x+1){
			memset(visited,0,sizeof(visited));
			mx=max(mx,dfs(i));
		}
		sum+=mx;

	}
	cout<<sum<<endl;

}


#include <bits/stdc++.h>
using namespace std;
const int N=100;
vector <int> tree[N];

// DFS Code.

int mxDist (int x, int pre)
{
	int ans=0;

	for(unsigned int i=0 ; i<tree[x].size() ; i++)
		if(tree[x][i] != pre)
			ans = max(ans,mxDist(tree[x][i],x)+1);

	return ans;
}

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

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

	int n=0, m, x, y, sum=0, mx=0;

	cin >> m;

	while (m--)
	{
		mx=0;

		for(int i=0 ; i<n ; i++)
			tree[i].clear();

		cin >> n;

		for(int i=0 ; i<n-1 ; i++)
		{
			cin >> x >> y;
			tree[x-1].push_back(y-1);
			tree[y-1].push_back(x-1);
		}

		for(int i=0 ; i<n ; i++)
			mx = max(mx,mxDist(i,-1));

		sum += mx;
	}

	cout << sum;

	return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define nl "\n"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fl(i,a,b) for(int i=a;i<b;i+=1)
#define rl(i,a,b) for(int i=a;i>b;i-=1)
#define input(a,n) fl(i,0,n) cin>>a[i];
#define print(a,n) fl(i,0,n) cout<<a[i]<<' ';
#define all(a) a.begin(),a.end()
#define test() int t; cin>>t; while(t--)
#define maxelement(arr,n) *max_element(arr,arr+n)
const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int N = 1e6 + 1;
using namespace std;

vector<int> v[105];
int visited[105]={0};

int dfs(int n){
	int ans=0;
	visited[n]=1;
	for(int i=0;i<v[n].size();i++){
		if(visited[v[n][i]]==0){
			ans=max(ans,dfs(v[n][i])+1);
		}
	}
	return ans;
}

int main(){
	iamspeed;
	int n;
	cin>>n;
	int sum=0;
	for(int i=0;i<n;i++){
		int x,a,b;
		fl(i,0,105){
			v[i].clear();
		}
		cin>>x;
		fl(i,0,x-1){
			cin>>a>>b;
			v[a].pb(b);
			v[b].pb(a);
		}
		int mx=0;
		fl(i,1,x+1){
			memset(visited,0,sizeof(visited));
			mx=max(mx,dfs(i));
		}
		sum+=mx;

	}
	cout<<sum<<endl;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

/*should use ::toupper/lower as its defined in global scope, not std*/

#define pb push_back
#define eb emplace_back
#define res reserve

#define pii pair<int, int>
#define mp make_pair
#define f first
#define s second

#define mt make_tuple

#define endl << "\n"
#define bigint __int128_t; // Not available in all contest systems
#define rr return

#define feq(a, b) (abs(a - b) < 1e-9)

using ll = long long;

using namespace __gnu_pbds;
using namespace std;

typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;

const int INF = 2e9;
const int MOD = 1e9 + 7;

#define madd(a, b) ((int(a) % MOD + int(b) % MOD) % MOD)
#define msub(a, b) ((int(a) % MOD - int(b) % MOD) % MOD)
#define mmul(a, b) ((int(a) % MOD * int(b) % MOD) % MOD)

const int mxN = 1e8;

vector<int> adj[mxN];
int y=1, x;

void dfs(int s, int p, int d) {
	if(y <= d) {
		y = d;
		x = s;
	}
	for(int x: adj[s]) {
		if(x != p)
			dfs(x, s, d + 1);
	}
}

void solve() {

	int n;
	cin >> n;

	int tot = 0;

	while(n--) {

		int b;
		cin >> b;

		while(--b) {
			int from, to;
			cin >> from >> to;

			adj[from].eb(to);
			adj[to].eb(from);
		}

		y = 1;
		dfs(1, 1, 0);

		y = 1;
		dfs(x, x, 0);
		x = 1;

		tot += y;

		for(int l = 0; l < mxN; ++l)
			adj[l].clear();

		//cout << y endl;

	}

	cout << tot endl;
}

int main() {

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

	ios::sync_with_stdio(false);
	cin.tie(nullptr);

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

	while(t--) {
		solve();
	}

}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

/*should use ::toupper/lower as its defined in global scope, not std*/

#define pb push_back
#define eb emplace_back
#define res reserve

#define pii pair<int, int>
#define mp make_pair
#define f first
#define s second

#define mt make_tuple

#define endl << "\n"
#define bigint __int128_t; // Not available in all contest systems
#define rr return

#define feq(a, b) (abs(a - b) < 1e-9)

using ll = long long;

using namespace __gnu_pbds;
using namespace std;

typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;

const int INF = 2e9;
const int MOD = 1e9 + 7;

#define madd(a, b) ((int(a) % MOD + int(b) % MOD) % MOD)
#define msub(a, b) ((int(a) % MOD - int(b) % MOD) % MOD)
#define mmul(a, b) ((int(a) % MOD * int(b) % MOD) % MOD)

const int mxN = 105;

vector<int> adj[mxN];
int y=1, x;

void dfs(int s, int p, int d) {
	if(y <= d) {
		y = d;
		x = s;
	}
	for(int x: adj[s]) {
		if(x != p)
			dfs(x, s, d + 1);
	}
}

void solve() {

	int n;
	cin >> n;

	int tot = 0;

	while(n--) {

		int b;
		cin >> b;

		while(--b) {
			int from, to;
			cin >> from >> to;

			adj[from].eb(to);
			adj[to].eb(from);
		}

		y = 1;
		dfs(1, 1, 0);

		y = 1;
		dfs(x, x, 0);
		x = 1;

		tot += y;

		for(int l = 0; l < mxN; ++l)
			adj[l].clear();

		//cout << y endl;

	}

	cout << tot endl;
}

int main() {

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

	ios::sync_with_stdio(false);
	cin.tie(nullptr);

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

	while(t--) {
		solve();
	}

}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

/*should use ::toupper/lower as its defined in global scope, not std*/

#define pb push_back
#define eb emplace_back
#define res reserve

#define pii pair<int, int>
#define mp make_pair
#define f first
#define s second

#define mt make_tuple

#define endl << "\n"
#define bigint __int128_t; // Not available in all contest systems
#define rr return

#define feq(a, b) (abs(a - b) < 1e-9)

using ll = long long;

using namespace __gnu_pbds;
using namespace std;

typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;

const int INF = 2e9;
const int MOD = 1e9 + 7;

#define madd(a, b) ((int(a) % MOD + int(b) % MOD) % MOD)
#define msub(a, b) ((int(a) % MOD - int(b) % MOD) % MOD)
#define mmul(a, b) ((int(a) % MOD * int(b) % MOD) % MOD)

const int mxN = 105;

vector<int> adj[mxN];
int y=1, x;

void dfs(int s, int p, int d) {
	if(y <= d) {
		y = d;
		x = s;
	}
	for(int x: adj[s]) {
		if(x != p)
			dfs(x, s, d + 1);
	}
}

void solve() {

	int n;
	cin >> n;

	int tot = 0;

	while(n--) {

		int b;
		cin >> b;

		while(--b) {
			int from, to;
			cin >> from >> to;

			adj[from].eb(to);
			adj[to].eb(from);
		}

		y = 1;
		dfs(1, 1, 0);

		y = 1;
		dfs(x, x, 0);
		x = 1;

		tot += y;

		for(int l = 0; l < mxN; ++l)
			adj[l].clear();

		//cout << y endl;

	}

	cout << tot endl;
}

int main() {

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

	ios::sync_with_stdio(false);
	cin.tie(nullptr);

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

	while(t--) {
		solve();
	}

}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define inf 0x3f3f3f3f
#define fd(x) for(int i=0;i<x;i++)
#define fdd(a,x) for(int i=a;i<x;i++)
#define mst(x) memset(x,0,sizeof(x))
#define mstinf(x) memset(x,0x3f,sizeof(x))
#define IOS ios::sync_with_stdio(false);
#define outm(marname,x,y) for(int i = 0;i < x;++i){ for(int j = 1;j < y ;++j) { cout << marname[i][j] << ' ';}cout << endl;}
#define outa(arrname,len) for(int i = 0;i < len;++i) cout << arrname[i] << ' ';
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<pii,int> ppi;
int n;
int len;
vector<int> a[105];
int rem[105];
int zhijing;
int bfs(int s){
    zhijing = 0;
    queue<int> h1,h2;
    h1.push(s);
    int ret = -1;
    while(h1.size()+h2.size()){

        if(h1.size()){
            zhijing++;
            while(h1.size()){
                int t = h1.front();
                ret = t;
                h1.pop();
                for(int i = 0 ; i < a[t].size() ; ++i){
                    if(rem[a[t][i]] == 0){
                        h2.push(a[t][i]);
                        rem[a[t][i]] = 1;
                    }
                }
            }
        }
        if(h2.size()){
            zhijing++;
            while(h2.size()){
                int t = h2.front();
                ret = t;
                h2.pop();
                for(int i = 0 ; i < a[t].size() ; ++i){
                    if(rem[a[t][i]] == 0){
                        h1.push(a[t][i]);
                        rem[a[t][i]] = 1;
                    }
                }
            }
        }
    }
    return ret;
}
void getzhijing(){
    zhijing = 0;
    int s = bfs(1);
    mst(rem);
    bfs(s);
    return ;
}
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin >> n;
    for(int i = 0 ; i < n ; ++i){
        int num;
        cin >> num;
        for(int j = 0 ; j < num-1 ;++j){
            int c,b;
            cin >> c >> b;
            a[c].push_back(b);
            a[b].push_back(c);
        }
        getzhijing();
        len += zhijing;
        mst(rem);
        for(int i = 0 ; i < 105 ;++i){
            a[i].clear();
        }
    }
    cout << len - (n);
    return 0;
}