#include typedef long long ll; using namespace std; struct cmp{ bool operator()(pair a, pair b) const{ if(a.first == b.first) return a.second < b.second; return a.first < b.first; } }; int main(){ ll n; cin >> n; set, cmp> s; s.insert(make_pair(0, 0)); for(ll i = 0; i < n; i++){ ll m; cin >> m; vector> crane(m); for(ll j = 0; j < m; j++){ ll id, p; id++; cin >> id >> p; crane[j] = make_pair(id, p); } vector price(m + 1); auto find = s.lower_bound(make_pair(0, 0)); price[0] = (*find).second; s.erase(find); for(ll j = 0; j < m; j++){ auto find = s.lower_bound(make_pair(crane[j].first, LONG_LONG_MIN)); if(find != s.end() && (*find).first == crane[j].first){ price[j + 1] = (*find).second + crane[j].second; s.erase(find); }else price[j + 1] = LONG_LONG_MIN; } ll ma = LONG_LONG_MIN; for(ll j = 0; j < m + 1; j++){ ma = max(ma, price[j]); } s.insert(make_pair(0, ma)); for(ll j = 0; j < m; j++){ s.insert(make_pair(crane[j].first, ma - crane[j].second)); } } ll ma = LONG_LONG_MIN; for(auto x: s){ ma = max(ma, x.second); } cout << ma << endl; return 0; }