/// Gheorghies Alexandru #include using namespace std; typedef long long ll; typedef long double ld; typedef pair pll; typedef pair pld; const ll LGMAX=20,NMAX=5e5+5; ll dp[NMAX],deg[NMAX]; vector edg[NMAX]; void tc(){ ll n,s=0,ans=LLONG_MAX; cin>>n; for(ll i=1;i<=n;i++) cin>>dp[i],s+=dp[i]; for(ll i=0;i>u>>v; edg[u].push_back(v); edg[v].push_back(u); deg[u]++,deg[v]++; } queue q; for(ll i=1;i<=n;i++) if(deg[i]==1) q.push(i); while(!q.empty()){ deg[q.front()]=0; ans=min(ans,abs(2*dp[q.front()]-s)); for(auto it : edg[q.front()]){ if(deg[it]>0){ dp[it]+=dp[q.front()]; deg[it]--; if(deg[it]==1) q.push(it); } } q.pop(); } vector cycle; for(ll i=1;i<=n;i++){ if(deg[i]>0){ cycle={i}; deg[i]=0; break; } } while(1){ bool ok=0; for(auto it : edg[cycle.back()]){ if(deg[it]){ cycle.push_back(it); deg[it]=0; ok=1; break; } } if(!ok) break; } n=cycle.size(); ll l=0,r=n-1,curr=0; for(ll l=0;l>t; while(t--) tc(); return 0; }