#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
struct spot{
int h, w, l;
bool r;
} *v;
struct state{
int x;
double dist;
state(int n, double d): x(n), dist(d){}
bool operator<(const state& other) const
{
return dist > other.dist;
}
};
int n, l, w;
vector<state> *edge;
double dijkistra(const state s)
{
double *cmp = new double[n + 2];
memset(cmp, 127, (n + 2) * sizeof(double));
priority_queue<state> q;
q.push(s);
cmp[0] = 0;
while(!q.empty())
{
state i = q.top();
q.pop();
if(cmp[i.x] == -1)
continue;
if(i.x == n + 1)
{
delete[] cmp;
return i.dist;
}
for(state j: edge[i.x])
if(cmp[j.x] > j.dist + i.dist)
{
j.dist += i.dist;
cmp[j.x] = j.dist;
q.push(j);
}
}
}
double DIST(spot& a, spot& b)
{
if(a.r == b.r || a.w + b.w >= w)
if(a.l > b.l)
return a.l - b.l - b.h;
else
return b.l - a.l - a.h;
else if((a.l <= b.l && a.l + a.h >= b.l) || (b.l <= a.l && b.l + b.h >= a.l))
return w - a.w - b.w;
else{
ll xi, xj, yi, yj;
if(a.l > b.l)
xi = a.l, xj = b.l + b.h;
else
xj = b.l, xi = a.l + a.h;
yi = a.w;
if(a.r)
yi = w - yi;
yj = b.w;
if(b.r)
yj = w - yj;
return hypot(abs(xi - xj), abs(yi - yj));
}
}
double dis(spot& a, spot& b) {
ll xx, yy;
if (a.r == b.r) xx = 0;
else {
ll x1, x2;
if (a.r == 0) x1 = a.w;
else x2 = w - a.w;
if (b.r == 0)x1 = b.w;
else x2 = w - b.w;
xx = max(x2 - x1, 0LL);
}
if (a.l > b.l)
yy = max(a.l - (b.l + b.h), 0);
else yy = max(b.l - (a.l + a.h), 0);
return sqrt(xx * xx + yy * yy);
}
int main()
{
cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false);
freopen("street.in", "r", stdin);
int t;
for(cin >> t; t; t--)
{
cin >> n >> l >> w;
v = new spot[n];
for(int i = 0; i < n; i++)
cin >> v[i].h >> v[i].w >> v[i].l >> v[i].r;
edge = new vector<state>[n + 2];
for(int i = 0; i < n; i++)
{
edge[0].push_back(state(i + 1, v[i].l));
edge[i + 1].push_back(state(n + 1, l - v[i].l - v[i].h));
for(int j = i + 1; j < n; j++)
{
double dist = dis(v[i], v[j]);
//double dist = DIST(v[i], v[j]);
edge[i + 1].push_back(state(j + 1, dist));
edge[j + 1].push_back(state(i + 1, dist));
}
}
delete[] v;
cout << fixed << setprecision(6) << dijkistra(state(0, 0)) << endl;
delete[] edge;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZW5kbCAnXG4nCnR5cGVkZWYgbG9uZyBsb25nIGxsOwoKc3RydWN0IHNwb3R7CglpbnQgaCwgdywgbDsKCWJvb2wgcjsKfSAqdjsKCnN0cnVjdCBzdGF0ZXsKCWludCB4OwoJZG91YmxlIGRpc3Q7CglzdGF0ZShpbnQgbiwgZG91YmxlIGQpOiB4KG4pLCBkaXN0KGQpe30KCWJvb2wgb3BlcmF0b3I8KGNvbnN0IHN0YXRlJiBvdGhlcikgY29uc3QKCXsKCQlyZXR1cm4gZGlzdCA+IG90aGVyLmRpc3Q7Cgl9Cn07CgppbnQgbiwgbCwgdzsKdmVjdG9yPHN0YXRlPiAqZWRnZTsKCmRvdWJsZSBkaWpraXN0cmEoY29uc3Qgc3RhdGUgcykKewoJZG91YmxlICpjbXAgPSBuZXcgZG91YmxlW24gKyAyXTsKCW1lbXNldChjbXAsIDEyNywgKG4gKyAyKSAqIHNpemVvZihkb3VibGUpKTsKCXByaW9yaXR5X3F1ZXVlPHN0YXRlPiBxOwoJcS5wdXNoKHMpOwoJY21wWzBdID0gMDsKCXdoaWxlKCFxLmVtcHR5KCkpCgl7CgkJc3RhdGUgaSA9IHEudG9wKCk7CgkJcS5wb3AoKTsKCQlpZihjbXBbaS54XSA9PSAtMSkKCQkJY29udGludWU7CgkJaWYoaS54ID09IG4gKyAxKQoJCXsKCQkJZGVs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