package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func dirIndex(dr, dc int) int {
if dr == 1 {
if dc == 1 {
return 0
}
return 1
}
if dc == 1 {
return 2
}
return 3
}
func onPath(x, y, dr, dc, tx, ty, k int) bool {
r := tx - x
c := ty - y
if r == 0 && c == 0 {
return true
}
if abs(r) != abs(c) {
return false
}
if abs(r) > k {
return false
}
return r*dr > 0 && c*dc > 0
}
func main() {
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanWords)
sc.Buffer(make([]byte, 1024), 1<<20)
nextInt := func() int {
sc.Scan()
v, _ := strconv.Atoi(sc.Text())
return v
}
next := func() string {
sc.Scan()
return sc.Text()
}
out := bufio.NewWriterSize(os.Stdout, 1<<20)
defer out.Flush()
t := nextInt()
for ; t > 0; t-- {
n := nextInt()
m := nextInt()
x := nextInt()
y := nextInt()
tx := nextInt()
ty := nextInt()
d := next()
dr, dc := 1, 1
if d[0] == 'U' {
dr = -1
}
if d[1] == 'L' {
dc = -1
}
bounces := 0
visited := make(map[int]bool)
for {
if x == tx && y == ty {
fmt.Fprintln(out, bounces)
break
}
key := (((x - 1) * m) + (y - 1)) * 4 + dirIndex(dr, dc)
if visited[key] {
fmt.Fprintln(out, -1)
break
}
visited[key] = true
distRow := 0
if dr == 1 {
distRow = n - x
} else {
distRow = x - 1
}
distCol := 0
if dc == 1 {
distCol = m - y
} else {
distCol = y - 1
}
k := distRow
if distCol < k {
k = distCol
}
if onPath(x, y, dr, dc, tx, ty, k) {
fmt.Fprintln(out, bounces)
break
}
x += k * dr
y += k * dc
if x == 1 || x == n {
dr = -dr
}
if y == 1 || y == m {
dc = -dc
}
bounces++
}
}
}