Problem: Problem

Tomorrow is finally the day of the excursion to Maizu Elementary School. Gatcho, who attends Maizu Elementary School, noticed that he had forgotten to buy tomorrow's sweets because he was so excited. Gaccho wants to stick to sweets so that he can enjoy the excursion to the fullest.


Gaccho gets a $ X $ yen allowance from his mother and goes to buy sweets. However, according to the rules of elementary school, the total amount of sweets to bring for an excursion is limited to $ Y $ yen, and if it exceeds $ Y $ yen, all sweets will be taken up by the teacher.

There are $ N $ towns in the school district where Gaccho lives, and each town has one candy store. Each town is assigned a number from 1 to $ N $, and Gaccho lives in town 1. Each candy store sells several sweets, and the price per one and the satisfaction level that you can get for each purchase are fixed. However, the number of sweets in stock is limited. Also, Gaccho uses public transportation to move between the two towns, so to move directly from town $ i $ to town $ j $, you only have to pay $ d_ {i, j} $ yen. It takes.

At first, Gaccho is in Town 1. Gaccho should make sure that the sum of the total cost of moving and the price of the sweets you bought is within $ X $ yen, and the total price of the sweets you bought is within $ Y $ yen. I'm going to buy sweets. You must have arrived at Town 1 at the end. Gaccho wants to maximize the total satisfaction of the sweets he bought. Find the total satisfaction when you do the best thing.

Constraints

The input satisfies the following conditions.

* $ 1 \ leq N \ leq 14 $
* $ 1 \ leq X \ leq 10000 $
* $ 1 \ leq Y \ leq min (1000, X) $
* $ 1 \ leq K \ leq 300 $
* $ 1 \ leq a_i \ leq 1000 $
* $ 1 \ leq b_i \ leq 1000 $
* $ 1 \ leq c_i \ leq 1000 $
* $ 0 \ leq d_ {i, j} \ leq 10000 $
* $ d_ {i, i} = 0 $

Input

All inputs are given as integers in the following format:

$ N $ $ X $ $ Y $
Information on candy stores in town 1
Information on candy stores in town 2
...
Information on candy stores in town $ N $
$ d_ {1,1} $ $ d_ {1,2} $ ... $ d_ {1, N} $
$ d_ {2,1} $ $ d_ {2,2} $ ... $ d_ {2, N} $
...
$ d_ {N, 1} $ $ d_ {N, 2} $ ... $ d_ {N, N} $


On the first line, the number of towns $ N $, the amount of money you have $ X $, and the maximum amount you can use to buy sweets $ Y $ are given, separated by blanks.
From the second line, information on each $ N $ candy store is given.
In the following $ N $ row and $ N $ column, the amount $ d_ {i, j} $ required to go back and forth between town $ i $ and town $ j $ is given, separated by blanks.


Information on candy stores in each town is given in the following format.

$ K $
$ a_1 $ $ b_1 $ $ c_1 $
$ a_2 $ $ b_2 $ $ c_2 $
...
$ a_K $ $ b_K $ $ c_K $


The first line gives $ K $, the number of types of sweets sold at the candy store. In the following $ K $ line, the price of one candy $ a_i $, the satisfaction level $ b_i $ per candy, and the number of stocks $ c_i $ are given, separated by blanks.

Output

Output the maximum value of the total satisfaction level on one line.

Examples

Input

1 10 10
3
1 10 1
2 20 2
3 30 3
0


Output

100


Input

2 10 10
3
1 10 1
2 20 2
3 30 3
1
5 200 1
0 2
3 0


Output

200


Input

3 10 10
1
1 1 1
1
3 3 3
1
5 5 5
0 1 0
1 0 0
0 1 0


Output

10


Input

4 59 40
1
7 6 3
1
10 3 9
2
9 8 5
7 6 10
4
8 2 9
1 7 1
7 7 9
1 2 3
0 28 7 26
14 0 10 24
9 6 0 21
9 24 14 0


Output

34

Solution (in CPP):

Solution:
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
typedef vector<int> vec;
typedef vector<string> svec;

int n;
int dp[14][1001];
int dist[14][14];
int dcost[(1 << 14)][14];
int sumcost[1 << 14];

int c1[1 << 7][1001];
int c2[1 << 7][1001];
void solve() {
	cin >> n; int x, y; cin >> x >> y;
	//start_napzack
	rep(i, n) {
		int k; cin >> k;
		rep(j, y+1) {
			dp[i][j] = -mod;
		}
		dp[i][0] = 0;
		rep(j, k) {
			//cost,val,num
			int a, b, c; cin >> a >> b >> c;
			rep(l, a) {
				if (l > y)continue;
				priority_queue<P> q;
				q.push({ dp[i][l],0 });
				int ad = 0;
				for (int loc = l + a; loc <= y; loc += a) {
					ad += b;
					P p = q.top();
					while (true) {
						if (loc / a - p.second > c) {
							q.pop(); p = q.top();
						}
						else break;
					}
					q.push({ dp[i][loc] - ad,loc / a });
					dp[i][loc] = max(dp[i][loc], p.first + ad);
				}
			}
		}
	}
	//end_napzack

	rep(i, n) {
		rep(j, n) {
			cin >> dist[i][j];
		}
	}
	rep(i, (1 << n))rep(j, n)dcost[i][j] = mod;
	dcost[1][0] = 0;
	for (int i = 1; i < (1 << n);i+=2) {
		rep(j, n) {
			rep(k, n) {
				if (i&(1 << k))continue;
				int ni = i ^ (1 << k);
				dcost[ni][k] = min(dcost[ni][k], dcost[i][j] + dist[j][k]);
			}
		}
	}
	rep(i, (1 << n)) {
		sumcost[i] = mod;
		rep(j, n) {
			sumcost[i] = min(sumcost[i], dcost[i][j] + dist[j][0]);
		}
	}
	int h = n / 2; int h2 = n - h;
	rep(i, (1 << h)) {
		rep(j, y + 1)c1[i][j] = -mod;
		c1[i][0] = 0;
		rep(j, h) {
			if (i&(1 << j)) {
				per(k, y + 1) {
					if (c1[i][k] < 0)continue;
					rep(l, y + 1) {
						if (k + l > y)break;
						c1[i][k + l] = max(c1[i][k + l], c1[i][k] + dp[j][l]);
					}
				}
			}
		}
		rep1(j, y) {
			c1[i][j] = max(c1[i][j], c1[i][j - 1]);
		}
	}
	rep(i, (1 << h2)) {
		rep(j, y + 1)c2[i][j] = -mod;
		c2[i][0] = 0;
		rep(j, h2) {
			if (i&(1 << j)) {
				per(k, y + 1) {
					//if (c2[i][k] < 0)continue;
					rep(l, y + 1) {
						if (k + l > y)break;
						c2[i][k + l] = max(c2[i][k + l], c2[i][k] + dp[j + h][l]);
					}
				}
			}
		}
		rep1(j, y) {
			c2[i][j] = max(c2[i][j], c2[i][j - 1]);
		}
	}
	int mid = (1 << h);
	int ans = 0;
	for (int i = 1; i < (1 << n); i += 2) {
		int sc = sumcost[i];
		sc = x - sc;
		if (sc > y)sc = y;
		int r = sc;
		int li = i % mid; int ri = i / mid;
		rep(j, r + 1) {
			int s = c1[li][j] + c2[ri][r - j];
			ans = max(ans, s);
		}
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(5);
	//while (cin >> n>>h>>m>>s,n)solve();
	solve();
	stop
	return 0;
}