#include using namespace std; //#define int long long #define f(i,a,b) for(int i = (a); i < (b); ++i) using ll = long long; signed main() { ios_base::sync_with_stdio(0); int n; cin >> n; map profit; vector dp(n + 1); dp[0] = 0; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1]; int m; cin >> m; vector> cranes(m); for (int j = 0; j < m; ++j) { cin >> cranes[j].first >> cranes[j].second; if (profit.find(cranes[j].first) != profit.end()) { dp[i] = max(dp[i], profit[cranes[j].first] + cranes[j].second); } } for (int j = 0; j < m; ++j) { if (profit.find(cranes[j].first) == profit.end()) { profit[cranes[j].first] = dp[i] - cranes[j].second; } else { profit[cranes[j].first] = max(profit[cranes[j].first], dp[i] - cranes[j].second); } } } cout << dp.back(); return 0; }