#include<bits/stdc++.h>usingnamespacestd;voidsolve(){intn,m;cin>>n>>m;// sm[i] denotes the second missing element of the i-th list.
vector<int>sm;while(n--){intlen;cin>>len;vector<bool>present(len+2,false);for(inti=0;i<len;i++){intele;cin>>ele;// Reject elements greater than the length to avoid segfault.
if(ele<present.size()){present[ele]=true;}}intmissing_count=0;for(inti=0;i<present.size();i++){if(!present[i]){missing_count++;}if(missing_count==2){sm.push_back(i);break;}}}intlim=*max_element(sm.begin(),sm.end());autoget_sum=[&](intn){// Sum of all numbers from 1 to n.
return1LL*n*(n+1)/2;};intstop=min(m,lim);longlongres=1LL*(stop+1)*lim;if(m>lim){res+=get_sum(m)-get_sum(lim);}cout<<res<<"\n";}intmain(){ios_base::sync_with_stdio(false);cin.tie(NULL);intt;cin>>t;while(t--){solve();}}