using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NArr(n); var query = NArr(m); var points = new int[n * 2][]; for (var i = 0; i < n; ++i) { points[i * 2] = new int[] { map[i][0], map[i][1] }; points[i * 2 + 1] = new int[] { map[i][2], map[i][3] }; } var INF = double.MaxValue / 2; var dp = new double[n * 2][]; for (var i = 0; i < dp.Length; ++i) dp[i] = Enumerable.Repeat(INF, n * 2).ToArray(); for (var i = 0; i < n; ++i) for (var j = 0; j < n; ++j) { void calc(int a, int b) { var flg = true; for (var k = 0; k < n; ++k) { if (a / 2 == k || b / 2 == k) continue; if (Cross(points[a], points[b], points[k * 2], points[k * 2 + 1])) { flg = false; break; } } if (flg) { dp[a][b] = Length(points[a], points[b]); dp[b][a] = dp[a][b]; } } calc(i * 2, j * 2); calc(i * 2, j * 2 + 1); calc(i * 2 + 1, j * 2); calc(i * 2 + 1, j * 2 + 1); } for (var k = 0; k < dp.Length; ++k) for (var i = 0; i < dp.Length; ++i) for (var j = 0; j < dp.Length; ++j) { dp[i][j] = Math.Min(dp[i][j], dp[i][k] + dp[k][j]); } var ans = new double[m]; for (var i = 0; i < m; ++i) { ans[i] = dp[query[i][0] * 2 + query[i][1] - 3][query[i][2] * 2 + query[i][3] - 3]; } WriteLine(string.Join("\n", ans)); } static bool Cross(int[] a, int[] b, int[] c, int[] d) { var sa = AreaSign(c, d, a); var sb = AreaSign(c, d, b); var sc = AreaSign(a, b, c); var sd = AreaSign(a, b, d); if (sa == 0 && sb == 0 && sc == 0 && sd == 0) { return false; } return sa != sb && sc != sd; } static int AreaSign(int[] a, int[] b, int[] c) { if (a[0] == b[0]) { if (c[0] == a[0]) return 0; else if (c[0] > a[0]) return 1; else return -1; } if (a[0] > b[0]) (a, b) = (b, a); var res = Area2(a, c) + Area2(c, b) - Area2(a, b); if (res == 0) return 0; if (res > 0) return 1; return -1; } static long Area2(int[] a, int[] b) { return (long)(a[1] + b[1]) * (b[0] - a[0]); } static double Length(int[] a, int[] b) { return Math.Sqrt(Math.Pow(a[0] - b[0], 2) + Math.Pow(a[1] - b[1], 2)); } }