def main(): w, d, h = [int(i) for i in input().split()] a, b, f, g = [int(i) for i in input().split()] return h+min([ abs(f-a)+b+g, abs(f-a)+(d-b)+(d-g), abs(b-g)+f+a, abs(b-g)+(w-f)+(w-a) ]) for _ in range(int(input())): print(main())