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 t = NN; var ans = new long[t]; for (var u = 0; u < t; ++u) { var p = ReadLine().Split(); var (n, x) = (int.Parse(p[0]), long.Parse(p[1])); var c = NList; ans[u] = Shop(n, x, c); } WriteLine(string.Join("\n", ans)); } static long Shop(int n, long x, int[] c) { var list = new List<(long l, long r)>(); var max = x; for (var i = 0; i < n; ++i) { var lx = x; if (((lx >> c[i]) & 1) == 0) { for (var j = 0; j < c[i]; ++j) if (((lx >> j) & 1) != 0) lx ^= 1L << j; --lx; } var rx = x; if (((rx >> c[i]) & 1) == 0) { rx ^= 1L << c[i]; for (var j = 0; j < c[i]; ++j) if (((rx >> j) & 1) != 0) rx ^= 1L << j; } if (lx >= 0 && lx != rx) list.Add((lx, rx)); else max = Math.Max(max, rx); } if (list.Count == 0) return (max - x) * 2; list.Sort((l, r) => r.l.CompareTo(l.l)); var ans = (max - list[^1].l) * 2; while (list.Count > 0) { max = Math.Max(max, list[^1].r); list.RemoveAt(list.Count - 1); if (list.Count > 0) ans = Math.Min(ans, (max - list[^1].l) * 2); else ans = Math.Min(ans, (max - x) * 2); } return ans; } }