# BZOJ 3876 AHOI2014 支线剧情 费用流

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 310
#define S 0
#define T (n+1)
#define INF 0x3f3f3f3f
using namespace std;
int n,m,ans;
namespace Min_Cost_Max_Flow{
struct abcd{
int to,flow,cost,next;
}table[1001001];
void Add(int x,int y,int f,int c)
{
table[++tot].to=y;
table[tot].flow=f;
table[tot].cost=c;
}
void Link(int x,int y,int f,int c)
{
}
bool Edmonds_Karp()
{
static int q[65540],f[M],cost[M],from[M];
static unsigned short r,h;
static bool v[M];
int i;
memset(cost,0x3f,sizeof cost);
cost[S]=0;f[S]=INF;f[T]=0;q[++r]=S;
while(r!=h)
{
int x=q[++h];v[x]=0;
if(table[i].flow&&cost[x]+table[i].cost<cost[table[i].to])
{
cost[table[i].to]=cost[x]+table[i].cost;
f[table[i].to]=min(f[x],table[i].flow);
from[table[i].to]=i;
if(!v[table[i].to])
v[table[i].to]=1,q[++r]=table[i].to;
}
}
if(!f[T]) return false;
ans+=f[T]*cost[T];
for(i=from[T];i;i=from[table[i^1].to])
table[i].flow-=f[T],table[i^1].flow+=f[T];
return true;
}
}
int main()
{
//freopen("3876.in","r",stdin);
using namespace Min_Cost_Max_Flow;
int i,j,y,z;
cin>>n;
for(i=1;i<=n;i++)
{
scanf("%d",&m);
for(j=1;j<=m;j++)
{
scanf("%d%d",&y,&z);
}