package codeforces.globalround16

private fun solve() {
	val (hei, wid) = readInts()
	val a = readInts()
	val places = mutableMapOf<Int, MutableList<Pair<Int, Int>>>()
	val aSorted = a.sorted()
	for (i in aSorted.indices) {
		places.getOrPut(aSorted[i]) { mutableListOf() }.add(i / wid to i % wid)
	}
	places.forEach { (_, list) -> list.sortBy { - it.first * wid + it.second } }
	val f = List(hei) { FenwickTree(wid) }
	val ans = a.sumOf { v ->
		val (x, y) = places[v]!!.removeLast()
		f[x].sum(y).also { f[x].add(y, 1) }
	}
	println(ans)
}

class FenwickTree(n: Int) {
	var t: LongArray
	fun add(index: Int, value: Long) {
		var i = index
		while (i < t.size) {
			t[i] += value
			i += i + 1 and -(i + 1)
		}
	}

	fun sum(index: Int): Long {
		var i = index
		var res: Long = 0
		i--
		while (i >= 0) {
			res += t[i]
			i -= i + 1 and -(i + 1)
		}
		return res
	}

	fun sum(start: Int, end: Int): Long {
		return sum(end) - sum(start)
	}

	operator fun get(i: Int): Long {
		return sum(i, i + 1)
	}

	operator fun set(i: Int, value: Long) {
		add(i, value - get(i))
	}

	init {
		t = LongArray(n)
	}
}

fun main() = repeat(readInt()) { solve() }

private fun readLn() = readLine()!!
private fun readInt() = readLn().toInt()
private fun readStrings() = readLn().split(" ")
private fun readInts() = readStrings().map { it.toInt() }

Compiled from "Comparisons.kt"
public final class codeforces.globalround16.DKt$solve$lambda$2$$inlined$sortBy$1<T> implements java.util.Comparator {
  final int $wid$inlined;

  public codeforces.globalround16.DKt$solve$lambda$2$$inlined$sortBy$1(int);
    Code:
       0: aload_0
       1: iload_1
       2: putfield      #18                 // Field $wid$inlined:I
       5: aload_0
       6: invokespecial #21                 // Method java/lang/Object."<init>":()V
       9: return

  public final int compare(T, T);
    Code:
       0: aload_1
       1: checkcast     #28                 // class kotlin/Pair
       4: astore_3
       5: iconst_0
       6: istore        4
       8: aload_3
       9: invokevirtual #32                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      12: checkcast     #34                 // class java/lang/Number
      15: invokevirtual #38                 // Method java/lang/Number.intValue:()I
      18: ineg
      19: aload_0
      20: getfield      #18                 // Field $wid$inlined:I
      23: imul
      24: aload_3
      25: invokevirtual #41                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      28: checkcast     #34                 // class java/lang/Number
      31: invokevirtual #38                 // Method java/lang/Number.intValue:()I
      34: iadd
      35: invokestatic  #47                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      38: checkcast     #49                 // class java/lang/Comparable
      41: aload_2
      42: checkcast     #28                 // class kotlin/Pair
      45: astore_3
      46: astore        5
      48: iconst_0
      49: istore        4
      51: aload_3
      52: invokevirtual #32                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      55: checkcast     #34                 // class java/lang/Number
      58: invokevirtual #38                 // Method java/lang/Number.intValue:()I
      61: ineg
      62: aload_0
      63: getfield      #18                 // Field $wid$inlined:I
      66: imul
      67: aload_3
      68: invokevirtual #41                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      71: checkcast     #34                 // class java/lang/Number
      74: invokevirtual #38                 // Method java/lang/Number.intValue:()I
      77: iadd
      78: invokestatic  #47                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      81: aload         5
      83: swap
      84: checkcast     #49                 // class java/lang/Comparable
      87: invokestatic  #55                 // Method kotlin/comparisons/ComparisonsKt.compareValues:(Ljava/lang/Comparable;Ljava/lang/Comparable;)I
      90: ireturn
}

Compiled from "d.kt"
public final class codeforces.globalround16.DKt {
  private static final void solve();
    Code:
       0: invokestatic  #10                 // Method readInts:()Ljava/util/List;
       3: astore_0
       4: aload_0
       5: iconst_0
       6: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      11: checkcast     #18                 // class java/lang/Number
      14: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      17: istore_1
      18: aload_0
      19: iconst_1
      20: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      25: checkcast     #18                 // class java/lang/Number
      28: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      31: istore_2
      32: invokestatic  #10                 // Method readInts:()Ljava/util/List;
      35: astore_3
      36: new           #24                 // class java/util/LinkedHashMap
      39: dup
      40: invokespecial #27                 // Method java/util/LinkedHashMap."<init>":()V
      43: checkcast     #29                 // class java/util/Map
      46: astore        4
      48: aload_3
      49: checkcast     #31                 // class java/lang/Iterable
      52: invokestatic  #37                 // Method kotlin/collections/CollectionsKt.sorted:(Ljava/lang/Iterable;)Ljava/util/List;
      55: astore        5
      57: iconst_0
      58: istore        6
      60: aload         5
      62: checkcast     #39                 // class java/util/Collection
      65: invokeinterface #42,  1           // InterfaceMethod java/util/Collection.size:()I
      70: istore        7
      72: iload         6
      74: iload         7
      76: if_icmpge     181
      79: aload         4
      81: astore        8
      83: aload         5
      85: iload         6
      87: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      92: astore        9
      94: iconst_0
      95: istore        10
      97: aload         8
      99: aload         9
     101: invokeinterface #45,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     106: astore        11
     108: aload         11
     110: ifnonnull     146
     113: iconst_0
     114: istore        12
     116: new           #47                 // class java/util/ArrayList
     119: dup
     120: invokespecial #48                 // Method java/util/ArrayList."<init>":()V
     123: checkcast     #12                 // class java/util/List
     126: nop
     127: astore        12
     129: aload         8
     131: aload         9
     133: aload         12
     135: invokeinterface #52,  3           // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     140: pop
     141: aload         12
     143: goto          148
     146: aload         11
     148: nop
     149: checkcast     #12                 // class java/util/List
     152: iload         6
     154: iload_2
     155: idiv
     156: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     159: iload         6
     161: iload_2
     162: irem
     163: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     166: invokestatic  #64                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     169: invokeinterface #68,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     174: pop
     175: iinc          6, 1
     178: goto          72
     181: aload         4
     183: astore        6
     185: iconst_0
     186: istore        7
     188: aload         6
     190: invokeinterface #72,  1           // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
     195: invokeinterface #78,  1           // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
     200: astore        8
     202: aload         8
     204: invokeinterface #84,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     209: ifeq          283
     212: aload         8
     214: invokeinterface #88,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     219: checkcast     #90                 // class java/util/Map$Entry
     222: astore        9
     224: aload         9
     226: astore        10
     228: iconst_0
     229: istore        11
     231: aload         10
     233: invokeinterface #93,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     238: checkcast     #12                 // class java/util/List
     241: astore        12
     243: aload         12
     245: astore        13
     247: iconst_0
     248: istore        14
     250: aload         13
     252: invokeinterface #94,  1           // InterfaceMethod java/util/List.size:()I
     257: iconst_1
     258: if_icmple     277
     261: aload         13
     263: new           #96                 // class codeforces/globalround16/DKt$solve$lambda$2$$inlined$sortBy$1
     266: dup
     267: iload_2
     268: invokespecial #99                 // Method codeforces/globalround16/DKt$solve$lambda$2$$inlined$sortBy$1."<init>":(I)V
     271: checkcast     #101                // class java/util/Comparator
     274: invokestatic  #105                // Method kotlin/collections/CollectionsKt.sortWith:(Ljava/util/List;Ljava/util/Comparator;)V
     277: nop
     278: nop
     279: nop
     280: goto          202
     283: nop
     284: new           #47                 // class java/util/ArrayList
     287: dup
     288: iload_1
     289: invokespecial #106                // Method java/util/ArrayList."<init>":(I)V
     292: astore        7
     294: iconst_0
     295: istore        8
     297: iload         8
     299: iload_1
     300: if_icmpge     339
     303: iload         8
     305: istore        9
     307: aload         7
     309: iload         9
     311: istore        10
     313: astore        24
     315: iconst_0
     316: istore        11
     318: new           #108                // class codeforces/globalround16/FenwickTree
     321: dup
     322: iload_2
     323: invokespecial #109                // Method codeforces/globalround16/FenwickTree."<init>":(I)V
     326: aload         24
     328: swap
     329: invokevirtual #110                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
     332: pop
     333: iinc          8, 1
     336: goto          297
     339: aload         7
     341: checkcast     #12                 // class java/util/List
     344: astore        6
     346: aload_3
     347: checkcast     #31                 // class java/lang/Iterable
     350: astore        9
     352: lconst_0
     353: lstore        10
     355: aload         9
     357: invokeinterface #111,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     362: astore        12
     364: aload         12
     366: invokeinterface #84,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     371: ifeq          520
     374: aload         12
     376: invokeinterface #88,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     381: astore        13
     383: lload         10
     385: aload         13
     387: checkcast     #18                 // class java/lang/Number
     390: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     393: istore        14
     395: lstore        24
     397: iconst_0
     398: istore        15
     400: aload         4
     402: iload         14
     404: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     407: invokeinterface #45,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     412: dup
     413: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     416: checkcast     #12                 // class java/util/List
     419: invokeinterface #120,  1          // InterfaceMethod java/util/List.removeLast:()Ljava/lang/Object;
     424: dup
     425: ldc           #122                // String removeLast(...)
     427: invokestatic  #126                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     430: checkcast     #128                // class kotlin/Pair
     433: astore        16
     435: aload         16
     437: invokevirtual #131                // Method kotlin/Pair.component1:()Ljava/lang/Object;
     440: checkcast     #18                 // class java/lang/Number
     443: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     446: istore        17
     448: aload         16
     450: invokevirtual #134                // Method kotlin/Pair.component2:()Ljava/lang/Object;
     453: checkcast     #18                 // class java/lang/Number
     456: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     459: istore        18
     461: aload         6
     463: iload         17
     465: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     470: checkcast     #108                // class codeforces/globalround16/FenwickTree
     473: iload         18
     475: invokevirtual #138                // Method codeforces/globalround16/FenwickTree.sum:(I)J
     478: lstore        19
     480: lload         19
     482: lstore        21
     484: iconst_0
     485: istore        23
     487: aload         6
     489: iload         17
     491: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     496: checkcast     #108                // class codeforces/globalround16/FenwickTree
     499: iload         18
     501: lconst_1
     502: invokevirtual #141                // Method codeforces/globalround16/FenwickTree.add:(IJ)V
     505: lload         19
     507: nop
     508: lstore        26
     510: lload         24
     512: lload         26
     514: ladd
     515: lstore        10
     517: goto          364
     520: lload         10
     522: lstore        7
     524: getstatic     #147                // Field java/lang/System.out:Ljava/io/PrintStream;
     527: lload         7
     529: invokevirtual #153                // Method java/io/PrintStream.println:(J)V
     532: return

  public static final void main();
    Code:
       0: invokestatic  #191                // Method readInt:()I
       3: istore_0
       4: iconst_0
       5: istore_1
       6: iload_1
       7: iload_0
       8: if_icmpge     24
      11: iload_1
      12: istore_2
      13: iconst_0
      14: istore_3
      15: invokestatic  #193                // Method solve:()V
      18: iinc          1, 1
      21: goto          6
      24: return

  private static final java.lang.String readLn();
    Code:
       0: invokestatic  #201                // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
       3: dup
       4: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
       7: areturn

  private static final int readInt();
    Code:
       0: invokestatic  #203                // Method readLn:()Ljava/lang/String;
       3: invokestatic  #207                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
       6: ireturn

  private static final java.util.List<java.lang.String> readStrings();
    Code:
       0: invokestatic  #203                // Method readLn:()Ljava/lang/String;
       3: checkcast     #211                // class java/lang/CharSequence
       6: iconst_1
       7: anewarray     #213                // class java/lang/String
      10: astore_0
      11: aload_0
      12: iconst_0
      13: ldc           #215                // String
      15: aastore
      16: aload_0
      17: iconst_0
      18: iconst_0
      19: bipush        6
      21: aconst_null
      22: invokestatic  #221                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      25: areturn

  private static final java.util.List<java.lang.Integer> readInts();
    Code:
       0: invokestatic  #224                // Method readStrings:()Ljava/util/List;
       3: checkcast     #31                 // class java/lang/Iterable
       6: astore_0
       7: iconst_0
       8: istore_1
       9: aload_0
      10: astore_2
      11: new           #47                 // class java/util/ArrayList
      14: dup
      15: aload_0
      16: bipush        10
      18: invokestatic  #228                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #106                // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #39                 // class java/util/Collection
      27: astore_3
      28: iconst_0
      29: istore        4
      31: aload_2
      32: invokeinterface #111,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      37: astore        5
      39: aload         5
      41: invokeinterface #84,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      46: ifeq          92
      49: aload         5
      51: invokeinterface #88,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      56: astore        6
      58: aload_3
      59: aload         6
      61: checkcast     #213                // class java/lang/String
      64: astore        7
      66: astore        9
      68: iconst_0
      69: istore        8
      71: aload         7
      73: invokestatic  #207                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      76: nop
      77: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      80: aload         9
      82: swap
      83: invokeinterface #229,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      88: pop
      89: goto          39
      92: aload_3
      93: checkcast     #12                 // class java/util/List
      96: nop
      97: areturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #242                // Method main:()V
       3: return
}


package codeforces.globalround16

private fun solve() {
	val (_, m) = readInts()
	val points = readInts().sorted()
	val segments = List(m) { readInts().let { it[0] to it[1] } }
	val groups = List(points.size + 1) { mutableListOf<Pair<Int, Int>>() }
	for (segment in segments) {
		val index = (-1..points.size).binarySearch { points[it] >= segment.first }
		if (index < points.size && points[index] <= segment.second) continue
		groups[index].add(segment)
	}
	var ifEats = 0L
	var ifGives = 0L
	val inf = Long.MAX_VALUE / 8
	for (k in groups.indices) {
		groups[k].sortBy { it.first }
		val g = mutableListOf<Pair<Int, Int>>()
		for (segment in groups[k]) {
			if (g.isEmpty()) {
				g.add(segment)
				continue
			}
			if (g.last().first == segment.first && g.last().second <= segment.second) continue
			while (g.isNotEmpty() && g.last().first <= segment.first && segment.second <= g.last().second) {
				g.pop()
			}
			g.add(segment)
		}
		val pPrev = points.getOrElse(k - 1) { -inf }.toLong()
		val pNext = points.getOrElse(k) { inf }.toLong()
		var newIfEats = inf
		var newIfGives = inf
		for (i in 0..g.size) {
			val comeLeft = g.getOrNull(i - 1)?.first?.toLong() ?: pPrev
			val comeRight = g.getOrNull(i)?.second?.toLong() ?: pNext
			val leftBest = minOf(ifEats + 2 * (comeLeft - pPrev), ifGives + comeLeft - pPrev)
			val possibleIfEats = leftBest + pNext - comeRight
			newIfEats = minOf(newIfEats, possibleIfEats)
			val possibleIfGives = leftBest + 2 * (pNext - comeRight)
			newIfGives = minOf(newIfGives, possibleIfGives)
		}
		ifEats = newIfEats
		ifGives = newIfGives
	}
	println(minOf(ifEats, ifGives))
}

fun main() = repeat(readInt()) { solve() }

private fun readLn() = readLine()!!
private fun readInt() = readLn().toInt()
private fun readStrings() = readLn().split(" ")
private fun readInts() = readStrings().map { it.toInt() }

private fun IntRange.binarySearch(predicate: (Int) -> Boolean): Int {
	var (low, high) = this.first to this.last // must be false ... must be true
	while (low + 1 < high) (low + (high - low) / 2).also { if (predicate(it)) high = it else low = it }
	return high // first true
}
private fun <T> MutableList<T>.pop() = removeAt(lastIndex)

Compiled from "Comparisons.kt"
public final class codeforces.globalround16.FKt$solve$$inlined$sortBy$1<T> implements java.util.Comparator {
  public codeforces.globalround16.FKt$solve$$inlined$sortBy$1();
    Code:
       0: aload_0
       1: invokespecial #16                 // Method java/lang/Object."<init>":()V
       4: return

  public final int compare(T, T);
    Code:
       0: aload_1
       1: checkcast     #23                 // class kotlin/Pair
       4: astore_3
       5: iconst_0
       6: istore        4
       8: aload_3
       9: invokevirtual #27                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      12: checkcast     #29                 // class java/lang/Integer
      15: checkcast     #31                 // class java/lang/Comparable
      18: aload_2
      19: checkcast     #23                 // class kotlin/Pair
      22: astore_3
      23: astore        5
      25: iconst_0
      26: istore        4
      28: aload_3
      29: invokevirtual #27                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      32: checkcast     #29                 // class java/lang/Integer
      35: aload         5
      37: swap
      38: checkcast     #31                 // class java/lang/Comparable
      41: invokestatic  #37                 // Method kotlin/comparisons/ComparisonsKt.compareValues:(Ljava/lang/Comparable;Ljava/lang/Comparable;)I
      44: ireturn
}

Compiled from "f.kt"
public final class codeforces.globalround16.FKt {
  private static final void solve();
    Code:
       0: invokestatic  #10                 // Method readInts:()Ljava/util/List;
       3: iconst_1
       4: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       9: checkcast     #18                 // class java/lang/Number
      12: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      15: istore_0
      16: invokestatic  #10                 // Method readInts:()Ljava/util/List;
      19: checkcast     #24                 // class java/lang/Iterable
      22: invokestatic  #30                 // Method kotlin/collections/CollectionsKt.sorted:(Ljava/lang/Iterable;)Ljava/util/List;
      25: astore_1
      26: new           #32                 // class java/util/ArrayList
      29: dup
      30: iload_0
      31: invokespecial #36                 // Method java/util/ArrayList."<init>":(I)V
      34: astore_3
      35: iconst_0
      36: istore        4
      38: iload         4
      40: iload_0
      41: if_icmpge     100
      44: iload         4
      46: istore        5
      48: aload_3
      49: iload         5
      51: istore        6
      53: astore        33
      55: iconst_0
      56: istore        7
      58: invokestatic  #10                 // Method readInts:()Ljava/util/List;
      61: astore        8
      63: iconst_0
      64: istore        9
      66: aload         8
      68: iconst_0
      69: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      74: aload         8
      76: iconst_1
      77: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      82: invokestatic  #42                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      85: nop
      86: nop
      87: aload         33
      89: swap
      90: invokevirtual #46                 // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      93: pop
      94: iinc          4, 1
      97: goto          38
     100: aload_3
     101: checkcast     #12                 // class java/util/List
     104: astore_2
     105: aload_1
     106: invokeinterface #49,  1           // InterfaceMethod java/util/List.size:()I
     111: iconst_1
     112: iadd
     113: istore        4
     115: new           #32                 // class java/util/ArrayList
     118: dup
     119: iload         4
     121: invokespecial #36                 // Method java/util/ArrayList."<init>":(I)V
     124: astore        5
     126: iconst_0
     127: istore        6
     129: iload         6
     131: iload         4
     133: if_icmpge     175
     136: iload         6
     138: istore        7
     140: aload         5
     142: iload         7
     144: istore        8
     146: astore        33
     148: iconst_0
     149: istore        9
     151: new           #32                 // class java/util/ArrayList
     154: dup
     155: invokespecial #51                 // Method java/util/ArrayList."<init>":()V
     158: checkcast     #12                 // class java/util/List
     161: nop
     162: aload         33
     164: swap
     165: invokevirtual #46                 // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
     168: pop
     169: iinc          6, 1
     172: goto          129
     175: aload         5
     177: checkcast     #12                 // class java/util/List
     180: astore_3
     181: aload_2
     182: invokeinterface #55,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
     187: astore        4
     189: aload         4
     191: invokeinterface #61,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     196: ifeq          299
     199: aload         4
     201: invokeinterface #65,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     206: checkcast     #67                 // class kotlin/Pair
     209: astore        5
     211: new           #69                 // class kotlin/ranges/IntRange
     214: dup
     215: iconst_m1
     216: aload_1
     217: invokeinterface #49,  1           // InterfaceMethod java/util/List.size:()I
     222: invokespecial #72                 // Method kotlin/ranges/IntRange."<init>":(II)V
     225: aload_1
     226: aload         5
     228: invokedynamic #92,  0             // InvokeDynamic #0:invoke:(Ljava/util/List;Lkotlin/Pair;)Lkotlin/jvm/functions/Function1;
     233: invokestatic  #96                 // Method binarySearch:(Lkotlin/ranges/IntRange;Lkotlin/jvm/functions/Function1;)I
     236: istore        6
     238: iload         6
     240: aload_1
     241: invokeinterface #49,  1           // InterfaceMethod java/util/List.size:()I
     246: if_icmpge     277
     249: aload_1
     250: iload         6
     252: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     257: checkcast     #18                 // class java/lang/Number
     260: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     263: aload         5
     265: invokevirtual #99                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     268: checkcast     #18                 // class java/lang/Number
     271: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     274: if_icmple     189
     277: aload_3
     278: iload         6
     280: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     285: checkcast     #12                 // class java/util/List
     288: aload         5
     290: invokeinterface #100,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     295: pop
     296: goto          189
     299: lconst_0
     300: lstore        4
     302: lconst_0
     303: lstore        6
     305: ldc2_w        #101                // long 1152921504606846975l
     308: lstore        8
     310: iconst_0
     311: istore        10
     313: aload_3
     314: checkcast     #104                // class java/util/Collection
     317: invokeinterface #105,  1          // InterfaceMethod java/util/Collection.size:()I
     322: istore        11
     324: iload         10
     326: iload         11
     328: if_icmpge     938
     331: aload_3
     332: iload         10
     334: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     339: checkcast     #12                 // class java/util/List
     342: astore        12
     344: iconst_0
     345: istore        13
     347: aload         12
     349: invokeinterface #49,  1           // InterfaceMethod java/util/List.size:()I
     354: iconst_1
     355: if_icmple     373
     358: aload         12
     360: new           #107                // class codeforces/globalround16/FKt$solve$$inlined$sortBy$1
     363: dup
     364: invokespecial #108                // Method codeforces/globalround16/FKt$solve$$inlined$sortBy$1."<init>":()V
     367: checkcast     #110                // class java/util/Comparator
     370: invokestatic  #114                // Method kotlin/collections/CollectionsKt.sortWith:(Ljava/util/List;Ljava/util/Comparator;)V
     373: nop
     374: new           #32                 // class java/util/ArrayList
     377: dup
     378: invokespecial #51                 // Method java/util/ArrayList."<init>":()V
     381: checkcast     #12                 // class java/util/List
     384: astore        12
     386: aload_3
     387: iload         10
     389: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     394: checkcast     #12                 // class java/util/List
     397: invokeinterface #55,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
     402: astore        13
     404: aload         13
     406: invokeinterface #61,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     411: ifeq          616
     414: aload         13
     416: invokeinterface #65,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     421: checkcast     #67                 // class kotlin/Pair
     424: astore        14
     426: aload         12
     428: invokeinterface #117,  1          // InterfaceMethod java/util/List.isEmpty:()Z
     433: ifeq          449
     436: aload         12
     438: aload         14
     440: invokeinterface #100,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     445: pop
     446: goto          404
     449: aload         12
     451: invokestatic  #121                // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     454: checkcast     #67                 // class kotlin/Pair
     457: invokevirtual #124                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     460: checkcast     #18                 // class java/lang/Number
     463: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     466: aload         14
     468: invokevirtual #124                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     471: checkcast     #18                 // class java/lang/Number
     474: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     477: if_icmpne     511
     480: aload         12
     482: invokestatic  #121                // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     485: checkcast     #67                 // class kotlin/Pair
     488: invokevirtual #99                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     491: checkcast     #18                 // class java/lang/Number
     494: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     497: aload         14
     499: invokevirtual #99                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     502: checkcast     #18                 // class java/lang/Number
     505: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     508: if_icmple     404
     511: aload         12
     513: checkcast     #104                // class java/util/Collection
     516: invokeinterface #125,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     521: ifne          528
     524: iconst_1
     525: goto          529
     528: iconst_0
     529: ifeq          603
     532: aload         12
     534: invokestatic  #121                // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     537: checkcast     #67                 // class kotlin/Pair
     540: invokevirtual #124                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     543: checkcast     #18                 // class java/lang/Number
     546: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     549: aload         14
     551: invokevirtual #124                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     554: checkcast     #18                 // class java/lang/Number
     557: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     560: if_icmpgt     603
     563: aload         14
     565: invokevirtual #99                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     568: checkcast     #18                 // class java/lang/Number
     571: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     574: aload         12
     576: invokestatic  #121                // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     579: checkcast     #67                 // class kotlin/Pair
     582: invokevirtual #99                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     585: checkcast     #18                 // class java/lang/Number
     588: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     591: if_icmpgt     603
     594: aload         12
     596: invokestatic  #128                // Method pop:(Ljava/util/List;)Ljava/lang/Object;
     599: pop
     600: goto          511
     603: aload         12
     605: aload         14
     607: invokeinterface #100,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     612: pop
     613: goto          404
     616: aload_1
     617: astore        15
     619: iload         10
     621: iconst_1
     622: isub
     623: istore        16
     625: iconst_0
     626: iload         16
     628: if_icmpgt     651
     631: iload         16
     633: aload         15
     635: invokeinterface #49,  1           // InterfaceMethod java/util/List.size:()I
     640: if_icmpge     647
     643: iconst_1
     644: goto          652
     647: iconst_0
     648: goto          652
     651: iconst_0
     652: ifeq          667
     655: aload         15
     657: iload         16
     659: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     664: goto          680
     667: iload         16
     669: istore        17
     671: iconst_0
     672: istore        18
     674: lload         8
     676: lneg
     677: invokestatic  #134                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     680: checkcast     #18                 // class java/lang/Number
     683: invokevirtual #138                // Method java/lang/Number.longValue:()J
     686: lstore        13
     688: aload_1
     689: astore        17
     691: iconst_0
     692: iload         10
     694: if_icmpgt     717
     697: iload         10
     699: aload         17
     701: invokeinterface #49,  1           // InterfaceMethod java/util/List.size:()I
     706: if_icmpge     713
     709: iconst_1
     710: goto          718
     713: iconst_0
     714: goto          718
     717: iconst_0
     718: ifeq          733
     721: aload         17
     723: iload         10
     725: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     730: goto          745
     733: iload         10
     735: istore        18
     737: iconst_0
     738: istore        19
     740: lload         8
     742: invokestatic  #134                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     745: checkcast     #18                 // class java/lang/Number
     748: invokevirtual #138                // Method java/lang/Number.longValue:()J
     751: lstore        15
     753: lload         8
     755: lstore        17
     757: lload         8
     759: lstore        19
     761: iconst_0
     762: istore        21
     764: aload         12
     766: invokeinterface #49,  1           // InterfaceMethod java/util/List.size:()I
     771: istore        22
     773: iload         21
     775: iload         22
     777: if_icmpgt     924
     780: aload         12
     782: iload         21
     784: iconst_1
     785: isub
     786: invokestatic  #142                // Method kotlin/collections/CollectionsKt.getOrNull:(Ljava/util/List;I)Ljava/lang/Object;
     789: checkcast     #67                 // class kotlin/Pair
     792: dup
     793: ifnull        809
     796: invokevirtual #124                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     799: checkcast     #18                 // class java/lang/Number
     802: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     805: i2l
     806: goto          812
     809: pop
     810: lload         13
     812: lstore        23
     814: aload         12
     816: iload         21
     818: invokestatic  #142                // Method kotlin/collections/CollectionsKt.getOrNull:(Ljava/util/List;I)Ljava/lang/Object;
     821: checkcast     #67                 // class kotlin/Pair
     824: dup
     825: ifnull        841
     828: invokevirtual #99                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     831: checkcast     #18                 // class java/lang/Number
     834: invokevirtual #22                 // Method java/lang/Number.intValue:()I
     837: i2l
     838: goto          844
     841: pop
     842: lload         15
     844: lstore        25
     846: lload         4
     848: iconst_2
     849: i2l
     850: lload         23
     852: lload         13
     854: lsub
     855: lmul
     856: ladd
     857: lload         6
     859: lload         23
     861: ladd
     862: lload         13
     864: lsub
     865: invokestatic  #148                // Method java/lang/Math.min:(JJ)J
     868: lstore        27
     870: lload         27
     872: lload         15
     874: ladd
     875: lload         25
     877: lsub
     878: lstore        29
     880: lload         17
     882: lload         29
     884: invokestatic  #148                // Method java/lang/Math.min:(JJ)J
     887: lstore        17
     889: lload         27
     891: iconst_2
     892: i2l
     893: lload         15
     895: lload         25
     897: lsub
     898: lmul
     899: ladd
     900: lstore        31
     902: lload         19
     904: lload         31
     906: invokestatic  #148                // Method java/lang/Math.min:(JJ)J
     909: lstore        19
     911: iload         21
     913: iload         22
     915: if_icmpeq     924
     918: iinc          21, 1
     921: goto          780
     924: lload         17
     926: lstore        4
     928: lload         19
     930: lstore        6
     932: iinc          10, 1
     935: goto          324
     938: lload         4
     940: lload         6
     942: invokestatic  #148                // Method java/lang/Math.min:(JJ)J
     945: lstore        10
     947: getstatic     #154                // Field java/lang/System.out:Ljava/io/PrintStream;
     950: lload         10
     952: invokevirtual #160                // Method java/io/PrintStream.println:(J)V
     955: return

  public static final void main();
    Code:
       0: invokestatic  #197                // Method readInt:()I
       3: istore_0
       4: iconst_0
       5: istore_1
       6: iload_1
       7: iload_0
       8: if_icmpge     24
      11: iload_1
      12: istore_2
      13: iconst_0
      14: istore_3
      15: invokestatic  #199                // Method solve:()V
      18: iinc          1, 1
      21: goto          6
      24: return

  private static final java.lang.String readLn();
    Code:
       0: invokestatic  #207                // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
       3: dup
       4: invokestatic  #213                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
       7: areturn

  private static final int readInt();
    Code:
       0: invokestatic  #215                // Method readLn:()Ljava/lang/String;
       3: invokestatic  #221                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
       6: ireturn

  private static final java.util.List<java.lang.String> readStrings();
    Code:
       0: invokestatic  #215                // Method readLn:()Ljava/lang/String;
       3: checkcast     #225                // class java/lang/CharSequence
       6: iconst_1
       7: anewarray     #227                // class java/lang/String
      10: astore_0
      11: aload_0
      12: iconst_0
      13: ldc           #229                // String
      15: aastore
      16: aload_0
      17: iconst_0
      18: iconst_0
      19: bipush        6
      21: aconst_null
      22: invokestatic  #235                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      25: areturn

  private static final java.util.List<java.lang.Integer> readInts();
    Code:
       0: invokestatic  #238                // Method readStrings:()Ljava/util/List;
       3: checkcast     #24                 // class java/lang/Iterable
       6: astore_0
       7: iconst_0
       8: istore_1
       9: aload_0
      10: astore_2
      11: new           #32                 // class java/util/ArrayList
      14: dup
      15: aload_0
      16: bipush        10
      18: invokestatic  #242                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #36                 // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #104                // class java/util/Collection
      27: astore_3
      28: iconst_0
      29: istore        4
      31: aload_2
      32: invokeinterface #243,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      37: astore        5
      39: aload         5
      41: invokeinterface #61,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      46: ifeq          92
      49: aload         5
      51: invokeinterface #65,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      56: astore        6
      58: aload_3
      59: aload         6
      61: checkcast     #227                // class java/lang/String
      64: astore        7
      66: astore        9
      68: iconst_0
      69: istore        8
      71: aload         7
      73: invokestatic  #221                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      76: nop
      77: invokestatic  #246                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      80: aload         9
      82: swap
      83: invokeinterface #247,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      88: pop
      89: goto          39
      92: aload_3
      93: checkcast     #12                 // class java/util/List
      96: nop
      97: areturn

  private static final int binarySearch(kotlin.ranges.IntRange, kotlin.jvm.functions.Function1<? super java.lang.Integer, java.lang.Boolean>);
    Code:
       0: aload_0
       1: invokevirtual #261                // Method kotlin/ranges/IntRange.getFirst:()I
       4: invokestatic  #246                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       7: aload_0
       8: invokevirtual #264                // Method kotlin/ranges/IntRange.getLast:()I
      11: invokestatic  #246                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      14: invokestatic  #42                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      17: astore_2
      18: iconst_0
      19: istore_3
      20: aload_2
      21: invokevirtual #267                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      24: checkcast     #18                 // class java/lang/Number
      27: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      30: istore_3
      31: iconst_0
      32: istore        4
      34: aload_2
      35: invokevirtual #270                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      38: checkcast     #18                 // class java/lang/Number
      41: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      44: istore        4
      46: iload_3
      47: iconst_1
      48: iadd
      49: iload         4
      51: if_icmpge     104
      54: iload_3
      55: iload         4
      57: iload_3
      58: isub
      59: iconst_2
      60: idiv
      61: iadd
      62: istore        5
      64: iload         5
      66: istore        6
      68: iconst_0
      69: istore        7
      71: aload_1
      72: iload         6
      74: invokestatic  #246                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      77: invokeinterface #274,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
      82: checkcast     #276                // class java/lang/Boolean
      85: invokevirtual #279                // Method java/lang/Boolean.booleanValue:()Z
      88: ifeq          98
      91: iload         6
      93: istore        4
      95: goto          101
      98: iload         6
     100: istore_3
     101: goto          46
     104: iload         4
     106: ireturn

  private static final <T> T pop(java.util.List<T>);
    Code:
       0: aload_0
       1: aload_0
       2: invokestatic  #291                // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
       5: invokeinterface #294,  2          // InterfaceMethod java/util/List.remove:(I)Ljava/lang/Object;
      10: areturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #298                // Method main:()V
       3: return

  private static final boolean solve$lambda$3(java.util.List, kotlin.Pair, int);
    Code:
       0: aload_0
       1: iload_2
       2: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       7: checkcast     #18                 // class java/lang/Number
      10: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      13: aload_1
      14: invokevirtual #124                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      17: checkcast     #18                 // class java/lang/Number
      20: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      23: if_icmplt     30
      26: iconst_1
      27: goto          31
      30: iconst_0
      31: ireturn
}


package codeforces.globalround16

private fun solve() {
	val n = readInt()
	val nei = List(n) { mutableListOf<Int>() }
	repeat(n - 1) {
		val (v, u) = readInts().map { it - 1 }
		nei[v].add(u)
		nei[u].add(v)
	}
	val kids = IntArray(n)
	val isLeaf = BooleanArray(n)
	val isBud = BooleanArray(n)
	var ans = 0
	fun dfs(v: Int, p: Int = -1) {
		var nonLeaves = false
		for (u in nei[v]) {
			if (u == p) continue
			dfs(u, v)
			if (isBud[u]) continue
			kids[v]++
			if (!isLeaf[u]) nonLeaves = true
		}
		isLeaf[v] = (p != -1 && kids[v] == 0)
		isBud[v] = (p != -1 && !nonLeaves && !isLeaf[v])
		if (isBud[v]) ans--
		if (isLeaf[v]) ans++
		if (v == 0 && kids[v] == 0) ans++
	}
	dfs(0)
	println(ans)
}

fun main() = repeat(readInt()) { solve() }

private fun readLn() = readLine()!!
private fun readInt() = readLn().toInt()
private fun readStrings() = readLn().split(" ")
private fun readInts() = readStrings().map { it.toInt() }

Compiled from "e.kt"
public final class codeforces.globalround16.EKt {
  private static final void solve();
    Code:
       0: invokestatic  #10                 // Method readInt:()I
       3: istore_0
       4: new           #12                 // class java/util/ArrayList
       7: dup
       8: iload_0
       9: invokespecial #16                 // Method java/util/ArrayList."<init>":(I)V
      12: astore_2
      13: iconst_0
      14: istore_3
      15: iload_3
      16: iload_0
      17: if_icmpge     57
      20: iload_3
      21: istore        4
      23: aload_2
      24: iload         4
      26: istore        5
      28: astore        17
      30: iconst_0
      31: istore        6
      33: new           #12                 // class java/util/ArrayList
      36: dup
      37: invokespecial #18                 // Method java/util/ArrayList."<init>":()V
      40: checkcast     #20                 // class java/util/List
      43: nop
      44: aload         17
      46: swap
      47: invokevirtual #24                 // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      50: pop
      51: iinc          3, 1
      54: goto          15
      57: aload_2
      58: checkcast     #20                 // class java/util/List
      61: astore_1
      62: iload_0
      63: iconst_1
      64: isub
      65: istore_2
      66: iconst_0
      67: istore_3
      68: iload_3
      69: iload_2
      70: if_icmpge     271
      73: iload_3
      74: istore        4
      76: iconst_0
      77: istore        5
      79: invokestatic  #28                 // Method readInts:()Ljava/util/List;
      82: checkcast     #30                 // class java/lang/Iterable
      85: astore        6
      87: iconst_0
      88: istore        7
      90: aload         6
      92: astore        8
      94: new           #12                 // class java/util/ArrayList
      97: dup
      98: aload         6
     100: bipush        10
     102: invokestatic  #36                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     105: invokespecial #16                 // Method java/util/ArrayList."<init>":(I)V
     108: checkcast     #38                 // class java/util/Collection
     111: astore        9
     113: iconst_0
     114: istore        10
     116: aload         8
     118: invokeinterface #42,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     123: astore        11
     125: aload         11
     127: invokeinterface #48,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     132: ifeq          180
     135: aload         11
     137: invokeinterface #52,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     142: astore        12
     144: aload         9
     146: aload         12
     148: checkcast     #54                 // class java/lang/Number
     151: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     154: istore        13
     156: astore        14
     158: iconst_0
     159: istore        15
     161: iload         13
     163: iconst_1
     164: isub
     165: invokestatic  #63                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     168: aload         14
     170: swap
     171: invokeinterface #64,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     176: pop
     177: goto          125
     180: aload         9
     182: checkcast     #20                 // class java/util/List
     185: nop
     186: astore        16
     188: aload         16
     190: iconst_0
     191: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     196: checkcast     #54                 // class java/lang/Number
     199: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     202: istore        6
     204: aload         16
     206: iconst_1
     207: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     212: checkcast     #54                 // class java/lang/Number
     215: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     218: istore        7
     220: aload_1
     221: iload         6
     223: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     228: checkcast     #20                 // class java/util/List
     231: iload         7
     233: invokestatic  #63                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     236: invokeinterface #69,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     241: pop
     242: aload_1
     243: iload         7
     245: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     250: checkcast     #20                 // class java/util/List
     253: iload         6
     255: invokestatic  #63                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     258: invokeinterface #69,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     263: pop
     264: nop
     265: iinc          3, 1
     268: goto          68
     271: iload_0
     272: newarray       int
     274: astore_2
     275: iload_0
     276: newarray       boolean
     278: astore_3
     279: iload_0
     280: newarray       boolean
     282: astore        4
     284: new           #71                 // class kotlin/jvm/internal/Ref$IntRef
     287: dup
     288: invokespecial #72                 // Method kotlin/jvm/internal/Ref$IntRef."<init>":()V
     291: astore        5
     293: aload_1
     294: aload         4
     296: aload_2
     297: aload_3
     298: aload         5
     300: iconst_0
     301: iconst_0
     302: bipush        64
     304: aconst_null
     305: invokestatic  #76                 // Method solve$dfs$default:(Ljava/util/List;[Z[I[ZLkotlin/jvm/internal/Ref$IntRef;IIILjava/lang/Object;)V
     308: aload         5
     310: getfield      #80                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     313: istore        6
     315: getstatic     #86                 // Field java/lang/System.out:Ljava/io/PrintStream;
     318: iload         6
     320: invokevirtual #91                 // Method java/io/PrintStream.println:(I)V
     323: return

  public static final void main();
    Code:
       0: invokestatic  #10                 // Method readInt:()I
       3: istore_0
       4: iconst_0
       5: istore_1
       6: iload_1
       7: iload_0
       8: if_icmpge     24
      11: iload_1
      12: istore_2
      13: iconst_0
      14: istore_3
      15: invokestatic  #119                // Method solve:()V
      18: iinc          1, 1
      21: goto          6
      24: return

  private static final java.lang.String readLn();
    Code:
       0: invokestatic  #127                // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
       3: dup
       4: invokestatic  #133                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
       7: areturn

  private static final int readInt();
    Code:
       0: invokestatic  #135                // Method readLn:()Ljava/lang/String;
       3: invokestatic  #139                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
       6: ireturn

  private static final java.util.List<java.lang.String> readStrings();
    Code:
       0: invokestatic  #135                // Method readLn:()Ljava/lang/String;
       3: checkcast     #143                // class java/lang/CharSequence
       6: iconst_1
       7: anewarray     #145                // class java/lang/String
      10: astore_0
      11: aload_0
      12: iconst_0
      13: ldc           #147                // String
      15: aastore
      16: aload_0
      17: iconst_0
      18: iconst_0
      19: bipush        6
      21: aconst_null
      22: invokestatic  #153                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      25: areturn

  private static final java.util.List<java.lang.Integer> readInts();
    Code:
       0: invokestatic  #156                // Method readStrings:()Ljava/util/List;
       3: checkcast     #30                 // class java/lang/Iterable
       6: astore_0
       7: iconst_0
       8: istore_1
       9: aload_0
      10: astore_2
      11: new           #12                 // class java/util/ArrayList
      14: dup
      15: aload_0
      16: bipush        10
      18: invokestatic  #36                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #16                 // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #38                 // class java/util/Collection
      27: astore_3
      28: iconst_0
      29: istore        4
      31: aload_2
      32: invokeinterface #42,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      37: astore        5
      39: aload         5
      41: invokeinterface #48,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      46: ifeq          92
      49: aload         5
      51: invokeinterface #52,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      56: astore        6
      58: aload_3
      59: aload         6
      61: checkcast     #145                // class java/lang/String
      64: astore        7
      66: astore        9
      68: iconst_0
      69: istore        8
      71: aload         7
      73: invokestatic  #139                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      76: nop
      77: invokestatic  #63                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      80: aload         9
      82: swap
      83: invokeinterface #64,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      88: pop
      89: goto          39
      92: aload_3
      93: checkcast     #20                 // class java/util/List
      96: nop
      97: areturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #161                // Method main:()V
       3: return

  private static final void solve$dfs(java.util.List<? extends java.util.List<java.lang.Integer>>, boolean[], int[], boolean[], kotlin.jvm.internal.Ref$IntRef, int, int);
    Code:
       0: iconst_0
       1: istore        7
       3: aload_0
       4: iload         5
       6: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      11: checkcast     #20                 // class java/util/List
      14: invokeinterface #167,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      19: astore        8
      21: aload         8
      23: invokeinterface #48,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      28: ifeq          100
      31: aload         8
      33: invokeinterface #52,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      38: checkcast     #54                 // class java/lang/Number
      41: invokevirtual #57                 // Method java/lang/Number.intValue:()I
      44: istore        9
      46: iload         9
      48: iload         6
      50: if_icmpeq     21
      53: aload_0
      54: aload_1
      55: aload_2
      56: aload_3
      57: aload         4
      59: iload         9
      61: iload         5
      63: invokestatic  #169                // Method solve$dfs:(Ljava/util/List;[Z[I[ZLkotlin/jvm/internal/Ref$IntRef;II)V
      66: aload_1
      67: iload         9
      69: baload
      70: ifne          21
      73: aload_2
      74: iload         5
      76: iaload
      77: istore        10
      79: aload_2
      80: iload         5
      82: iload         10
      84: iconst_1
      85: iadd
      86: iastore
      87: aload_3
      88: iload         9
      90: baload
      91: ifne          21
      94: iconst_1
      95: istore        7
      97: goto          21
     100: aload_3
     101: iload         5
     103: iload         6
     105: iconst_m1
     106: if_icmpeq     120
     109: aload_2
     110: iload         5
     112: iaload
     113: ifne          120
     116: iconst_1
     117: goto          121
     120: iconst_0
     121: bastore
     122: aload_1
     123: iload         5
     125: iload         6
     127: iconst_m1
     128: if_icmpeq     147
     131: iload         7
     133: ifne          147
     136: aload_3
     137: iload         5
     139: baload
     140: ifne          147
     143: iconst_1
     144: goto          148
     147: iconst_0
     148: bastore
     149: aload_1
     150: iload         5
     152: baload
     153: ifeq          172
     156: aload         4
     158: getfield      #80                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     161: istore        8
     163: aload         4
     165: iload         8
     167: iconst_m1
     168: iadd
     169: putfield      #80                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     172: aload_3
     173: iload         5
     175: baload
     176: ifeq          195
     179: aload         4
     181: getfield      #80                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     184: istore        8
     186: aload         4
     188: iload         8
     190: iconst_1
     191: iadd
     192: putfield      #80                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     195: iload         5
     197: ifne          223
     200: aload_2
     201: iload         5
     203: iaload
     204: ifne          223
     207: aload         4
     209: getfield      #80                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     212: istore        8
     214: aload         4
     216: iload         8
     218: iconst_1
     219: iadd
     220: putfield      #80                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     223: return

  static void solve$dfs$default(java.util.List, boolean[], int[], boolean[], kotlin.jvm.internal.Ref$IntRef, int, int, int, java.lang.Object);
    Code:
       0: iload         7
       2: bipush        64
       4: iand
       5: ifeq          11
       8: iconst_m1
       9: istore        6
      11: aload_0
      12: aload_1
      13: aload_2
      14: aload_3
      15: aload         4
      17: iload         5
      19: iload         6
      21: invokestatic  #169                // Method solve$dfs:(Ljava/util/List;[Z[I[ZLkotlin/jvm/internal/Ref$IntRef;II)V
      24: return
}


package codeforces.round901

private fun precalc(mask: Int): IntArray {
	val (a, b, m) = listOf(0x55, 0x33, 0x0F).map { it and mask }
	val source = a with b
	val queue = mutableListOf<Int>()
	val dist = IntArray(1 shl 14) { -1 }
	queue.add(source)
	dist[source] = 0
	var low = 0
	while (low < queue.size) {
		val v = queue[low++]
		fun move(xx: Int, yy: Int) {
			val u = xx with yy
			if (dist[u] != -1) return
			dist[u] = dist[v] + 1
			queue.add(u)
		}
		val (x, y) = decode(v)
		move(x and y, y)
		move(x or y, y)
		move(x, y xor x)
		move(x, y xor m)
	}
	return dist
}

private val precalc = List(128) { precalc(it) }

private fun solve(): Int {
	val (a, b, c, d, m) = readInts()
	var mask = 128; var cWant = 0; var dWant = 0
	for (i in 0 until Int.SIZE_BITS) {
		val type = 7 - a[i] - (b[i] shl 1) - (m[i] shl 2)
		if (mask[type] == 1) {
			if (c[i] != cWant[type] || d[i] != dWant[type]) return -1
			continue
		}
		mask = mask.setBit(type)
		if (c[i] == 1) cWant = cWant.setBit(type)
		if (d[i] == 1) dWant = dWant.setBit(type)
	}
	return precalc[mask - 128][cWant with dWant]
}

fun main() = repeat(readInt()) { println(solve()) }

private infix fun Int.with(that: Int) = shl(7) or that
fun decode(code: Int) = (code ushr 7) to (code and 0x7F)
private fun Int.bit(index: Int) = ushr(index) and 1
private fun Int.setBit(index: Int) = or(1 shl index)
private operator fun Int.get(index: Int) = bit(index)

private fun readInt() = readln().toInt()
private fun readStrings() = readln().split(" ")
private fun readInts() = readStrings().map { it.toInt() }

Compiled from "b.kt"
public final class codeforces.round901.BKt {
  private static final java.util.List<int[]> precalc;

  private static final int[] precalc(int);
    Code:
       0: iconst_3
       1: anewarray     #8                  // class java/lang/Integer
       4: astore_2
       5: aload_2
       6: iconst_0
       7: bipush        85
       9: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      12: aastore
      13: aload_2
      14: iconst_1
      15: bipush        51
      17: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      20: aastore
      21: aload_2
      22: iconst_2
      23: bipush        15
      25: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      28: aastore
      29: aload_2
      30: invokestatic  #18                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      33: checkcast     #20                 // class java/lang/Iterable
      36: astore_2
      37: iconst_0
      38: istore_3
      39: aload_2
      40: astore        4
      42: new           #22                 // class java/util/ArrayList
      45: dup
      46: aload_2
      47: bipush        10
      49: invokestatic  #26                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      52: invokespecial #30                 // Method java/util/ArrayList."<init>":(I)V
      55: checkcast     #32                 // class java/util/Collection
      58: astore        5
      60: iconst_0
      61: istore        6
      63: aload         4
      65: invokeinterface #36,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      70: astore        7
      72: aload         7
      74: invokeinterface #42,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      79: ifeq          127
      82: aload         7
      84: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      89: astore        8
      91: aload         5
      93: aload         8
      95: checkcast     #48                 // class java/lang/Number
      98: invokevirtual #52                 // Method java/lang/Number.intValue:()I
     101: istore        9
     103: astore        13
     105: iconst_0
     106: istore        10
     108: iload         9
     110: iload_0
     111: iand
     112: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     115: aload         13
     117: swap
     118: invokeinterface #56,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     123: pop
     124: goto          72
     127: aload         5
     129: checkcast     #58                 // class java/util/List
     132: nop
     133: astore_1
     134: aload_1
     135: iconst_0
     136: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     141: checkcast     #48                 // class java/lang/Number
     144: invokevirtual #52                 // Method java/lang/Number.intValue:()I
     147: istore_2
     148: aload_1
     149: iconst_1
     150: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     155: checkcast     #48                 // class java/lang/Number
     158: invokevirtual #52                 // Method java/lang/Number.intValue:()I
     161: istore_3
     162: aload_1
     163: iconst_2
     164: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     169: checkcast     #48                 // class java/lang/Number
     172: invokevirtual #52                 // Method java/lang/Number.intValue:()I
     175: istore        4
     177: iload_2
     178: iload_3
     179: invokestatic  #66                 // Method with:(II)I
     182: istore        5
     184: new           #22                 // class java/util/ArrayList
     187: dup
     188: invokespecial #69                 // Method java/util/ArrayList."<init>":()V
     191: checkcast     #58                 // class java/util/List
     194: astore        6
     196: iconst_0
     197: istore        8
     199: sipush        16384
     202: newarray       int
     204: astore        9
     206: iload         8
     208: sipush        16384
     211: if_icmpge     230
     214: iload         8
     216: istore        10
     218: aload         9
     220: iload         10
     222: iconst_m1
     223: iastore
     224: iinc          8, 1
     227: goto          206
     230: aload         9
     232: astore        7
     234: aload         6
     236: iload         5
     238: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     241: invokeinterface #70,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     246: pop
     247: aload         7
     249: iload         5
     251: iconst_0
     252: iastore
     253: iconst_0
     254: istore        8
     256: iload         8
     258: aload         6
     260: invokeinterface #73,  1           // InterfaceMethod java/util/List.size:()I
     265: if_icmpge     388
     268: aload         6
     270: iload         8
     272: iinc          8, 1
     275: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     280: checkcast     #48                 // class java/lang/Number
     283: invokevirtual #52                 // Method java/lang/Number.intValue:()I
     286: istore        9
     288: iload         9
     290: invokestatic  #77                 // Method decode:(I)Lkotlin/Pair;
     293: astore        10
     295: aload         10
     297: invokevirtual #82                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     300: checkcast     #48                 // class java/lang/Number
     303: invokevirtual #52                 // Method java/lang/Number.intValue:()I
     306: istore        11
     308: aload         10
     310: invokevirtual #85                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     313: checkcast     #48                 // class java/lang/Number
     316: invokevirtual #52                 // Method java/lang/Number.intValue:()I
     319: istore        12
     321: aload         7
     323: iload         9
     325: aload         6
     327: iload         11
     329: iload         12
     331: iand
     332: iload         12
     334: invokestatic  #89                 // Method precalc$move:([IILjava/util/List;II)V
     337: aload         7
     339: iload         9
     341: aload         6
     343: iload         11
     345: iload         12
     347: ior
     348: iload         12
     350: invokestatic  #89                 // Method precalc$move:([IILjava/util/List;II)V
     353: aload         7
     355: iload         9
     357: aload         6
     359: iload         11
     361: iload         12
     363: iload         11
     365: ixor
     366: invokestatic  #89                 // Method precalc$move:([IILjava/util/List;II)V
     369: aload         7
     371: iload         9
     373: aload         6
     375: iload         11
     377: iload         12
     379: iload         4
     381: ixor
     382: invokestatic  #89                 // Method precalc$move:([IILjava/util/List;II)V
     385: goto          256
     388: aload         7
     390: areturn

  private static final int solve();
    Code:
       0: invokestatic  #120                // Method readInts:()Ljava/util/List;
       3: astore_0
       4: aload_0
       5: iconst_0
       6: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      11: checkcast     #48                 // class java/lang/Number
      14: invokevirtual #52                 // Method java/lang/Number.intValue:()I
      17: istore_1
      18: aload_0
      19: iconst_1
      20: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      25: checkcast     #48                 // class java/lang/Number
      28: invokevirtual #52                 // Method java/lang/Number.intValue:()I
      31: istore_2
      32: aload_0
      33: iconst_2
      34: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      39: checkcast     #48                 // class java/lang/Number
      42: invokevirtual #52                 // Method java/lang/Number.intValue:()I
      45: istore_3
      46: aload_0
      47: iconst_3
      48: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      53: checkcast     #48                 // class java/lang/Number
      56: invokevirtual #52                 // Method java/lang/Number.intValue:()I
      59: istore        4
      61: aload_0
      62: iconst_4
      63: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      68: checkcast     #48                 // class java/lang/Number
      71: invokevirtual #52                 // Method java/lang/Number.intValue:()I
      74: istore        5
      76: sipush        128
      79: istore        6
      81: iconst_0
      82: istore        7
      84: iconst_0
      85: istore        8
      87: iconst_0
      88: istore        9
      90: iload         9
      92: bipush        32
      94: if_icmpge     230
      97: bipush        7
      99: iload_1
     100: iload         9
     102: invokestatic  #122                // Method get:(II)I
     105: isub
     106: iload_2
     107: iload         9
     109: invokestatic  #122                // Method get:(II)I
     112: iconst_1
     113: ishl
     114: isub
     115: iload         5
     117: iload         9
     119: invokestatic  #122                // Method get:(II)I
     122: iconst_2
     123: ishl
     124: isub
     125: istore        10
     127: iload         6
     129: iload         10
     131: invokestatic  #122                // Method get:(II)I
     134: iconst_1
     135: if_icmpne     176
     138: iload_3
     139: iload         9
     141: invokestatic  #122                // Method get:(II)I
     144: iload         7
     146: iload         10
     148: invokestatic  #122                // Method get:(II)I
     151: if_icmpne     171
     154: iload         4
     156: iload         9
     158: invokestatic  #122                // Method get:(II)I
     161: iload         8
     163: iload         10
     165: invokestatic  #122                // Method get:(II)I
     168: if_icmpeq     173
     171: iconst_m1
     172: ireturn
     173: goto          224
     176: iload         6
     178: iload         10
     180: invokestatic  #125                // Method setBit:(II)I
     183: istore        6
     185: iload_3
     186: iload         9
     188: invokestatic  #122                // Method get:(II)I
     191: iconst_1
     192: if_icmpne     204
     195: iload         7
     197: iload         10
     199: invokestatic  #125                // Method setBit:(II)I
     202: istore        7
     204: iload         4
     206: iload         9
     208: invokestatic  #122                // Method get:(II)I
     211: iconst_1
     212: if_icmpne     224
     215: iload         8
     217: iload         10
     219: invokestatic  #125                // Method setBit:(II)I
     222: istore        8
     224: iinc          9, 1
     227: goto          90
     230: getstatic     #127                // Field precalc:Ljava/util/List;
     233: iload         6
     235: sipush        128
     238: isub
     239: invokeinterface #62,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     244: checkcast     #115                // class "[I"
     247: iload         7
     249: iload         8
     251: invokestatic  #66                 // Method with:(II)I
     254: iaload
     255: ireturn

  public static final void main();
    Code:
       0: invokestatic  #137                // Method readInt:()I
       3: istore_0
       4: iconst_0
       5: istore_1
       6: iload_1
       7: iload_0
       8: if_icmpge     35
      11: iload_1
      12: istore_2
      13: iconst_0
      14: istore_3
      15: invokestatic  #139                // Method solve:()I
      18: istore        4
      20: getstatic     #145                // Field java/lang/System.out:Ljava/io/PrintStream;
      23: iload         4
      25: invokevirtual #150                // Method java/io/PrintStream.println:(I)V
      28: nop
      29: iinc          1, 1
      32: goto          6
      35: return

  private static final int with(int, int);
    Code:
       0: iload_0
       1: bipush        7
       3: ishl
       4: iload_1
       5: ior
       6: ireturn

  public static final kotlin.Pair<java.lang.Integer, java.lang.Integer> decode(int);
    Code:
       0: iload_0
       1: bipush        7
       3: iushr
       4: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       7: iload_0
       8: bipush        127
      10: iand
      11: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      14: invokestatic  #161                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      17: areturn

  private static final int bit(int, int);
    Code:
       0: iload_0
       1: iload_1
       2: iushr
       3: iconst_1
       4: iand
       5: ireturn

  private static final int setBit(int, int);
    Code:
       0: iload_0
       1: iconst_1
       2: iload_1
       3: ishl
       4: ior
       5: ireturn

  private static final int get(int, int);
    Code:
       0: iload_0
       1: iload_1
       2: invokestatic  #168                // Method bit:(II)I
       5: ireturn

  private static final int readInt();
    Code:
       0: invokestatic  #175                // Method kotlin/io/ConsoleKt.readln:()Ljava/lang/String;
       3: invokestatic  #179                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
       6: ireturn

  private static final java.util.List<java.lang.String> readStrings();
    Code:
       0: invokestatic  #175                // Method kotlin/io/ConsoleKt.readln:()Ljava/lang/String;
       3: checkcast     #183                // class java/lang/CharSequence
       6: iconst_1
       7: anewarray     #185                // class java/lang/String
      10: astore_0
      11: aload_0
      12: iconst_0
      13: ldc           #187                // String
      15: aastore
      16: aload_0
      17: iconst_0
      18: iconst_0
      19: bipush        6
      21: aconst_null
      22: invokestatic  #193                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      25: areturn

  private static final java.util.List<java.lang.Integer> readInts();
    Code:
       0: invokestatic  #196                // Method readStrings:()Ljava/util/List;
       3: checkcast     #20                 // class java/lang/Iterable
       6: astore_0
       7: iconst_0
       8: istore_1
       9: aload_0
      10: astore_2
      11: new           #22                 // class java/util/ArrayList
      14: dup
      15: aload_0
      16: bipush        10
      18: invokestatic  #26                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #30                 // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #32                 // class java/util/Collection
      27: astore_3
      28: iconst_0
      29: istore        4
      31: aload_2
      32: invokeinterface #36,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      37: astore        5
      39: aload         5
      41: invokeinterface #42,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      46: ifeq          92
      49: aload         5
      51: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      56: astore        6
      58: aload_3
      59: aload         6
      61: checkcast     #185                // class java/lang/String
      64: astore        7
      66: astore        9
      68: iconst_0
      69: istore        8
      71: aload         7
      73: invokestatic  #179                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      76: nop
      77: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      80: aload         9
      82: swap
      83: invokeinterface #56,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      88: pop
      89: goto          39
      92: aload_3
      93: checkcast     #58                 // class java/util/List
      96: nop
      97: areturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #201                // Method main:()V
       3: return

  private static final void precalc$move(int[], int, java.util.List<java.lang.Integer>, int, int);
    Code:
       0: iload_3
       1: iload         4
       3: invokestatic  #66                 // Method with:(II)I
       6: istore        5
       8: aload_0
       9: iload         5
      11: iaload
      12: iconst_m1
      13: if_icmpeq     17
      16: return
      17: aload_0
      18: iload         5
      20: aload_0
      21: iload_1
      22: iaload
      23: iconst_1
      24: iadd
      25: iastore
      26: aload_2
      27: iload         5
      29: invokestatic  #12                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      32: invokeinterface #70,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      37: pop
      38: return

  static {};
    Code:
       0: sipush        128
       3: istore_0
       4: new           #22                 // class java/util/ArrayList
       7: dup
       8: iload_0
       9: invokespecial #30                 // Method java/util/ArrayList."<init>":(I)V
      12: astore_1
      13: iconst_0
      14: istore_2
      15: iload_2
      16: iload_0
      17: if_icmpge     49
      20: iload_2
      21: istore_3
      22: aload_1
      23: iload_3
      24: istore        4
      26: astore        6
      28: iconst_0
      29: istore        5
      31: iload         4
      33: invokestatic  #210                // Method precalc:(I)[I
      36: aload         6
      38: swap
      39: invokevirtual #211                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      42: pop
      43: iinc          2, 1
      46: goto          15
      49: aload_1
      50: checkcast     #58                 // class java/util/List
      53: putstatic     #127                // Field precalc:Ljava/util/List;
      56: return
}


package codeforces.round901

private fun solve(): Double {
	val (n, m) = readInts()
	val d = List(n + 1) { DoubleArray(m - n + 1) }
	for (i in 1..n) {
		var kBest = 0
		for (s in d[i].indices) {
			val range = maxOf(kBest - 5, 0)..minOf(kBest + 11, s)
			var best = Double.MAX_VALUE
			for (k in range) {
				val new = (i + s - 1.0 - k) / (1 + k) + d[i - 1][s - k]
				if (new < best) {
					best = new
					kBest = k
				}
			}
			d[i][s] = best
		}
	}
	return d[n][m - n] * 2 + n
}

fun main() = repeat(1) { println(solve()) }

private fun readStrings() = readln().split(" ")
private fun readInts() = readStrings().map { it.toInt() }

Compiled from "d_ok_but_should_be_wa.kt"
public final class codeforces.round901.D_ok_but_should_be_waKt {
  private static final double solve();
    Code:
       0: invokestatic  #10                 // Method readInts:()Ljava/util/List;
       3: astore_0
       4: aload_0
       5: iconst_0
       6: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      11: checkcast     #18                 // class java/lang/Number
      14: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      17: istore_1
      18: aload_0
      19: iconst_1
      20: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      25: checkcast     #18                 // class java/lang/Number
      28: invokevirtual #22                 // Method java/lang/Number.intValue:()I
      31: istore_2
      32: iload_1
      33: iconst_1
      34: iadd
      35: istore        4
      37: new           #24                 // class java/util/ArrayList
      40: dup
      41: iload         4
      43: invokespecial #28                 // Method java/util/ArrayList."<init>":(I)V
      46: astore        5
      48: iconst_0
      49: istore        6
      51: iload         6
      53: iload         4
      55: if_icmpge     93
      58: iload         6
      60: istore        7
      62: aload         5
      64: iload         7
      66: istore        8
      68: astore        15
      70: iconst_0
      71: istore        9
      73: iload_2
      74: iload_1
      75: isub
      76: iconst_1
      77: iadd
      78: newarray       double
      80: aload         15
      82: swap
      83: invokevirtual #32                 // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      86: pop
      87: iinc          6, 1
      90: goto          51
      93: aload         5
      95: checkcast     #12                 // class java/util/List
      98: astore_3
      99: iconst_1
     100: istore        4
     102: iload         4
     104: iload_1
     105: if_icmpgt     291
     108: iconst_0
     109: istore        5
     111: iconst_0
     112: istore        6
     114: aload_3
     115: iload         4
     117: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     122: checkcast     #34                 // class "[D"
     125: arraylength
     126: istore        7
     128: iload         6
     130: iload         7
     132: if_icmpge     279
     135: new           #36                 // class kotlin/ranges/IntRange
     138: dup
     139: iload         5
     141: iconst_5
     142: isub
     143: iconst_0
     144: invokestatic  #42                 // Method java/lang/Math.max:(II)I
     147: iload         5
     149: bipush        11
     151: iadd
     152: iload         6
     154: invokestatic  #45                 // Method java/lang/Math.min:(II)I
     157: invokespecial #48                 // Method kotlin/ranges/IntRange."<init>":(II)V
     160: astore        8
     162: ldc2_w        #49                 // double 1.7976931348623157E308d
     165: dstore        9
     167: aload         8
     169: invokevirtual #53                 // Method kotlin/ranges/IntRange.getFirst:()I
     172: istore        11
     174: aload         8
     176: invokevirtual #56                 // Method kotlin/ranges/IntRange.getLast:()I
     179: istore        12
     181: iload         11
     183: iload         12
     185: if_icmpgt     257
     188: iload         4
     190: iload         6
     192: iadd
     193: i2d
     194: dconst_1
     195: dsub
     196: iload         11
     198: i2d
     199: dsub
     200: iconst_1
     201: iload         11
     203: iadd
     204: i2d
     205: ddiv
     206: aload_3
     207: iload         4
     209: iconst_1
     210: isub
     211: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     216: checkcast     #34                 // class "[D"
     219: iload         6
     221: iload         11
     223: isub
     224: daload
     225: dadd
     226: dstore        13
     228: dload         13
     230: dload         9
     232: dcmpg
     233: ifge          244
     236: dload         13
     238: dstore        9
     240: iload         11
     242: istore        5
     244: iload         11
     246: iload         12
     248: if_icmpeq     257
     251: iinc          11, 1
     254: goto          188
     257: aload_3
     258: iload         4
     260: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     265: checkcast     #34                 // class "[D"
     268: iload         6
     270: dload         9
     272: dastore
     273: iinc          6, 1
     276: goto          128
     279: iload         4
     281: iload_1
     282: if_icmpeq     291
     285: iinc          4, 1
     288: goto          108
     291: aload_3
     292: iload_1
     293: invokeinterface #16,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     298: checkcast     #34                 // class "[D"
     301: iload_2
     302: iload_1
     303: isub
     304: daload
     305: iconst_2
     306: i2d
     307: dmul
     308: iload_1
     309: i2d
     310: dadd
     311: dreturn

  public static final void main();
    Code:
       0: iconst_1
       1: istore_0
       2: iconst_0
       3: istore_1
       4: iload_1
       5: iload_0
       6: if_icmpge     33
       9: iload_1
      10: istore_2
      11: iconst_0
      12: istore_3
      13: invokestatic  #76                 // Method solve:()D
      16: dstore        4
      18: getstatic     #82                 // Field java/lang/System.out:Ljava/io/PrintStream;
      21: dload         4
      23: invokevirtual #88                 // Method java/io/PrintStream.println:(D)V
      26: nop
      27: iinc          1, 1
      30: goto          4
      33: return

  private static final java.util.List<java.lang.String> readStrings();
    Code:
       0: invokestatic  #97                 // Method kotlin/io/ConsoleKt.readln:()Ljava/lang/String;
       3: checkcast     #99                 // class java/lang/CharSequence
       6: iconst_1
       7: anewarray     #101                // class java/lang/String
      10: astore_0
      11: aload_0
      12: iconst_0
      13: ldc           #103                // String
      15: aastore
      16: aload_0
      17: iconst_0
      18: iconst_0
      19: bipush        6
      21: aconst_null
      22: invokestatic  #109                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      25: areturn

  private static final java.util.List<java.lang.Integer> readInts();
    Code:
       0: invokestatic  #112                // Method readStrings:()Ljava/util/List;
       3: checkcast     #114                // class java/lang/Iterable
       6: astore_0
       7: iconst_0
       8: istore_1
       9: aload_0
      10: astore_2
      11: new           #24                 // class java/util/ArrayList
      14: dup
      15: aload_0
      16: bipush        10
      18: invokestatic  #120                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #28                 // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #122                // class java/util/Collection
      27: astore_3
      28: iconst_0
      29: istore        4
      31: aload_2
      32: invokeinterface #126,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      37: astore        5
      39: aload         5
      41: invokeinterface #132,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      46: ifeq          92
      49: aload         5
      51: invokeinterface #136,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      56: astore        6
      58: aload_3
      59: aload         6
      61: checkcast     #101                // class java/lang/String
      64: astore        7
      66: astore        9
      68: iconst_0
      69: istore        8
      71: aload         7
      73: invokestatic  #142                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      76: nop
      77: invokestatic  #146                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      80: aload         9
      82: swap
      83: invokeinterface #147,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      88: pop
      89: goto          39
      92: aload_3
      93: checkcast     #12                 // class java/util/List
      96: nop
      97: areturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #161                // Method main:()V
       3: return
}