#include<cstdio>#include<queue>#include<vector>#define pb push_backconst int N=(int)(1e3);std::vector<int> need[N];int add[N];std::priority_queue<int> queue;int main() { int testCount; scanf("%d",&testCount); while(testCount--) { int n; scanf("%d",&n); for(int i=0; i<n; ++i) { need[i].clear(); int count; scanf("%d",&count); for(int j=0; j<count; ++j) { int value; scanf("%d",&value); need[i].pb(value); } } for(int i=0; i<n; ++i) { scanf("%d",&add[i]); } int have=0,proceeds=0; while(!queue.empty()) { queue.pop(); } for(int i=0; i<n; ++i) { have+=add[i]; for(int j=0; j<need[i].size(); ++j) { proceeds++; have-=need[i][j]; queue.push(need[i][j]); if(have<0) { proceeds--; have+=queue.top(); queue.pop(); } } } printf("%dn",proceeds); }}


