#include using namespace std; using ll=long long; using pii=pair; #define all(a) a.begin(),a.end() #define pb push_back #define sz(a) ((int)a.size()) template struct Modint{ int val; Modint(int _val=0){val=_val%mod;if(val<0) val+=mod;} Modint(ll _val){val=_val%mod;if(val<0) val+=mod;} Modint operator +(const Modint& o) const { Modint res; res.val=val+o.val; if(res.val>=mod) res.val-=mod; return res; } Modint operator +(const int& o) const {return Modint(val+o);} Modint operator +(const ll& o) const {return Modint(val+o);} Modint operator -() const { Modint res; res.val=-val; if(res.val<0) res.val+=mod; return res; } Modint operator -(const Modint& o) const { Modint res; res.val=val-o.val; if(res.val<0) res.val+=mod; return res; } Modint operator -(const int& o) const {return Modint(val-o);} Modint operator -(const ll& o) const {return Modint(val-o);} Modint operator *(const Modint& o) const {return ((ll)val)*o.val%mod;} Modint operator *(const int& o) const {return Modint((ll(val))*o);} Modint operator *(const ll& o) const {return Modint((ll(val))*(o%mod));} Modint operator +=(const Modint& o){return *this=(*this)+o;} Modint operator -=(const Modint& o){return *this=(*this)-o;} Modint operator *=(const Modint& o){return *this=(*this)*o;} Modint Pow(ll b) const {Modint res(1); for(Modint tmp(val); b; tmp*=tmp,b>>=1) if(b&1) res*=tmp; return res;} Modint Pow(const Modint& a, ll b) const {return a.Pow(b);} inline Modint inv() const {return (*this).Pow(mod-2);} Modint operator /(const Modint& o) const {return *this*o.inv();} Modint operator /(const ll& o) const {return *this*Modint(o).inv();} Modint operator /=(const Modint& o){return *this=(*this)/o;} Modint operator /=(const ll& o){return *this=(*this)/o;} bool operator ==(const Modint& o) const {return val==o.val;} bool operator !=(const Modint& o) const {return val!=o.val;} }; template ostream& operator << (ostream& o, Modint x){return o << x.val;} template Modint operator +(const int& x, const Modint& y){return Modint(x+y.val);} template Modint operator +(const ll& x, const Modint& y){return Modint(x+y.val);} template Modint operator -(const int& x, const Modint& y){return Modint(x-y.val);} template Modint operator -(const ll& x, const Modint& y){return Modint(x-y.val);} template Modint operator *(const int& x, const Modint& y){return Modint(x*y.val);} template Modint operator *(const ll& x, const Modint& y){return Modint(x%mod*y.val);} const int MOD=998244353; using modint=Modint; vector inv,fac,invfac; void init_comb(int N){ inv.resize(N),fac.resize(N),invfac.resize(N); inv[1]=1,fac[0]=1,invfac[0]=1; for(int i=2; in||m<0?modint(0):fac[n]*invfac[m]*invfac[n-m];} modint calc(modint p, modint q){ // \sigma p^i*q*(i+1) // x = \sigma p^i*q // x = q + px // x = q / (1 - p) // y = \sigma p^i*q*(i+1) // y = q + \sigma_{i=1} p^i*q*(i+1) // y = q + p * \sigma p^i*q*(i+2) // y = q + p * (x + \sigma p^i*q*(i+1)) // y = q + p * x + p * y // y = (q + p * x) / (1 - p) modint x=q/(1-p); return (q+p*q/(1-p))/(1-p); } const int N=40005; int n,siz[N]; vector adj[N]; modint res; constexpr ll f(ll x){return x*(x+1)/2;} void dfs(int u, int fa){ siz[u]=1; for(auto v: adj[u]) if(v!=fa) dfs(v,u),siz[u]+=siz[v]; ll cnt=f(n)-f(n-siz[u]); for(auto v: adj[u]) if(v!=fa) cnt-=f(siz[v]); // case 1 if(fa!=-1){ ll cnt2=f(siz[u]),bad=f(n-siz[u]); for(auto v: adj[u]) if(v!=fa) cnt2-=f(siz[v]); modint p=cnt,r=cnt2,q=bad; p/=f(n),r/=f(n),q/=f(n); res+=calc(p,q)-calc(p-r,q); } // case 2 for(auto v: adj[u]) if(v!=fa){ ll cnt2=cnt-1ll*siz[v]*(n-siz[v]),bad=f(siz[v]); modint p=cnt,r=cnt2,q=bad; p/=f(n),r/=f(n),q/=f(n); res+=calc(p,q)-calc(p-r,q); } } void ahcorz(){ cin >> n; for(int i=0; i> u >> v; u--,v--; adj[u].pb(v),adj[v].pb(u); } res=0; dfs(0,-1); cout << res << "\n"; } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); int t; cin >> t; while(t--) ahcorz(); }