package main

import "fmt"

func main() {
	var s int

	fmt.Scan(&s)

	for i := 0; i < s; i++ {
		solve()
	}
}

func solve() {
	var s string
	fmt.Scan(&s)
	q := five(s)
	e := zero(s)

	if q > e {
		fmt.Println(e)
	} else {
		fmt.Println(q)
	}
}

func five(s string) int {
	k := 0
	q := 0
	for len(s) > 0 && s[len(s)-1] != '5' {
		s = s[:len(s)-1]
		k++
	}

	if len(s) > 0 && q == 0 {
		q++
	} else {
		return k + 30
	}

	s = s[:len(s)-1]

	for len(s) > 0 && (s[len(s)-1] != '2' && s[len(s)-1] != '7') {
		s = s[:len(s)-1]
		k++
	}

	if len(s) > 0 && q == 1 {
		return k
	}

	return k + 30
}

func zero(s string) int {
	k := 0
	q := 0
	for len(s) > 0 && s[len(s)-1] != '0' {
		s = s[:len(s)-1]
		k++
	}

	if len(s) > 0 && q == 0 {
		q++
	} else {
		return k + 30
	}

	s = s[:len(s)-1]

	for len(s) > 0 && (s[len(s)-1] != '0' && s[len(s)-1] != '5') {
		s = s[:len(s)-1]
		k++
	}

	if len(s) > 0 && q == 1 {
		return k
	}

	return k + 30
}

package main

import (
	"fmt"
)

func main() {
	var n int

	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var s string
		ans := 100000000
		fmt.Scan(&s)
		for j := 0; j+1 < len(s); j++ {
			for k := j + 1; k < len(s); k++ {
				now := int(s[j]-'0')*10 + int(s[k]-'0')
				if now%25 == 0 {
					ans = min(ans, len(s)-j-2)
				}
			}
		}
		fmt.Println(ans)
	}

}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// func max(a, b int) int {
// 	if a > b {
// 		return a
// 	}
// 	return b
// }

package main

import "fmt"

func main() {
	var t int
	var s string
	fmt.Scan(&t)

	for t > 0 {
		t --
		fmt.Scan(&s)
		fmt.Println(divisible(s))
	}
}

func divisible(s string) int {
	l := len(s)
	if l < 3 || checkDiv(s[l-2 : l]) {
		return 0
	}

	//var k = 0
	for i := l - 2; i >= 0; i -- {
		for j := i + 1; j < l; j ++ {
			if checkDiv(string(s[i]) + string(s[j])) {
				return (l - 1 - j) + (j - i - 1)// asb(l - j + i - j)
			}
		}
	}

	return -1
}

func checkDiv(s string) bool {
	return s == "00" || s == "25" || s == "50" || s == "75"
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

func solve() {
	var s string
	scan(&s)
	n := len(s)
	// printf("%s\n", s)
	zero, five := -1, -1
	ans := n
	for i := n - 1; i >= 0; i-- {
		if s[i] == '0' {
			// printf("%d\n", i)
			if zero == -1 {
				zero = i
			} else {
				if ans > n-i-2 {
					ans = n - i - 2
				}
			}
		} else if s[i] == '5' {
			if zero != -1 {
				if ans > n-i-2 {
					ans = n - i - 2
				}
			}
			if five == -1 {
				five = i
			}
		} else if s[i] == '2' || s[i] == '7' {
			if five != -1 {
				if ans > n-i-2 {
					ans = n - i - 2
				}
			}
		}
	}
	printf("%d\n", ans)
}

func main() {
	defer out.Flush()
	n := 1
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

// 00
package main

import (
	"bufio"
	"fmt"
	"os"
)

func min(a int, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var s string
		fmt.Fscan(reader, &s)
		zeros := 0
		ans := len(s)
		n := len(s)
		for i := n - 1; i >= 0; i-- {
			if s[i] == '0' {
				zeros++
			}
			if zeros == 2 {
				ans = min(ans, n-i-2)
				break
			}
			if zeros == 1 && s[i] == '5' {
				ans = min(ans, n-i-2)
				break
			}
		}
		found_five := false
		for i := n - 1; i >= 0; i-- {
			if s[i] == '5' {
				found_five = true
			}
			if found_five && (s[i] == '2' || s[i] == '7') {
				ans = min(ans, n-i-2)
				break
			}
		}
		fmt.Println(ans)
	}
}

package main

import (
  "fmt"
  "bufio"
  "os"
)

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  var t int
  fmt.Fscan(reader, &t)
  for ; t > 0; t-- {
    var n int64
    fmt.Fscan(reader, &n)
    ans := 99
    for u := 0; u < 10; u += 5 {
      steps := 0
      m := n
      for m > 0 {
        if int(m%10) != u {
          steps++
          m /= 10
        } else {
          break
        }
      }
      for m > int64(u) {
        if m%25 != 0 {
          m = 10*(m/100) + int64(u)
          steps++
        } else {
          break
        }
      }
      if m != 0 {
        if ans > steps {
          ans = steps
        }
      }
    }
    fmt.Fprint(writer, ans, "\n")
  }
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)

	for t := 0; t < T; t++ {
		var s string
		fmt.Fscan(in, &s)

		min := math.MaxInt
		k := 0
		for i := len(s) - 1; i >= 0; i-- {
			for j := i - 1; j >= 0; j-- {
				if s[i] == '0' && (s[j] == '0' || s[j] == '5') {
					//
					if i-j-1+k < min {
						// fmt.Println("here: ", i, j, k)
						min = i - j - 1 + k
					}
				}

				if s[i] == '5' && (s[j] == '2' || s[j] == '7') {
					//
					// 2-1-1
					if i-j-1+k < min {
						min = i - j - 1 + k
					}
				}
			}
			k++
		}
		fmt.Fprintln(out, min)
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strings"
)

/*
3
25
125
1251
---
0
0
1
=====
1
50555
---
3
*/

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	bld := strings.Builder{}

	var t = 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var s string
		scanf("%s\n", &s)

		minCnt := maxInt

		{
			cnt := 0

			i := len(s) - 1
			for ; i >= 0; i-- {
				if s[i] == '0' {
					break
				} else {
					cnt++
				}
			}

			for j := i - 1; j >= 0; j-- {
				if s[j] == '5' || s[j] == '0' {
					break
				} else {
					cnt++
				}
			}

			minCnt = min(minCnt, cnt)
		}

		{
			cnt := 0

			i := len(s) - 1
			for ; i >= 0; i-- {
				if s[i] == '5' {
					break
				} else {
					cnt++
				}
			}

			for j := i - 1; j >= 0; j-- {
				if s[j] == '2' || s[j] == '7' {
					break
				} else {
					cnt++
				}
			}

			minCnt = min(minCnt, cnt)
		}

		fmt.Fprintln(&bld, minCnt)
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

//goland:noinspection GoUnusedType
type pair struct {
	x int64
	y int64
}

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

//goland:noinspection GoUnusedFunction
func isPrime(n int) bool {
	if n == 2 || n == 3 {
		return true
	}

	if n <= 1 || n%2 == 0 || n%3 == 0 {
		return false
	}

	for i := 5; i*i <= n; i += 6 {
		if n%i == 0 || n%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

//problem B

type problem struct {
	n string
}

//end with: 00 25 50 75
func (p *problem) endWith(end string) (int, bool) {
	i := len(p.n) - 1
	step := 0
	for ; i >= 0; step++ {
		if p.n[i] == end[1] {
			i--
			break
		}
		i--
	}

	for ; i >= 0; step++ {
		if p.n[i] == end[0] {
			return step, true
		}
		i--
	}
	return step, false
}

func (p *problem) MinSteps() int {
	minSteps := 20

	for _, end := range []string{"00", "25", "50", "75"} {
		needSteps, ok := p.endWith(end)
		if ok && needSteps < minSteps {
			minSteps = needSteps
		}
	}

	return minSteps
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	var t int
	var p problem

	fmt.Fscanf(reader, "%d\n", &t)
	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%s\n", &p.n)
		fmt.Printf("%d\n", p.MinSteps())
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func min2(a, b int) int {
	if a < b {
		return a
	}

	return b
}

func min(nums ...int) int {
	result := nums[0]
	for _, num := range nums {
		result = min2(result, num)
	}

	return result
}

func solve(s, pattern string) int {
	matchIdx := 1
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == pattern[matchIdx] {
			matchIdx--
			if matchIdx < 0 {
				return len(s) - i - 2
			}
		}
	}

	return 1000000
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t int
	var s string

	_, _ = fmt.Fscan(r, &t)

	for ; t > 0; t-- {
		_, _ = fmt.Fscan(r, &s)
		fmt.Printf("%d\n", min(solve(s, "00"), solve(s, "25"), solve(s, "50"), solve(s, "75")))
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var t int
	_, _ = fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var a int64
		_, _ = fmt.Fscan(r, &a)
		fmt.Println(min(solve(a, 0, 0), solve(a, 2, 5), solve(a, 5, 0), solve(a, 7, 5)))
	}
}

func solve(a, d1, d2 int64) int {
	ans := 0
	for a > 0 && a%10 != d2 {
		a /= 10
		ans++
	}
	a /= 10
	for a > 0 && a%10 != d1 {
		a /= 10
		ans++
	}
	return ans
}

func min(a int, args ...int) int {
	for _, b := range args {
		if a > b {
			a = b
		}
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func Min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func Solve(s, p string) (ans int) {
	ans = 0
	for i, j := len(s)-1, 1; i >= 0; i-- {
		if s[i] == p[j] {
			j--
			if j < 0 {
				return
			}
		} else {
			ans += 1
		}
	}
	return
}

func main() {
	var ntc int
	var s string
	fmt.Fscanf(in, "%d\n", &ntc)
	for t := 0; t < ntc; t++ {
		fmt.Fscanf(in, "%s\n", &s)
		ans := Min(Solve(s, "00"), Solve(s, "25"))
		ans = Min(ans, Solve(s, "50"))
		ans = Min(ans, Solve(s, "75"))
		fmt.Fprintf(out, "%d\n", ans)
	}
	out.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"time"
)

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MINN(x, y int) int {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func MAXX(x, y int) int {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	if x*y == 0 {
		return MAX(x, y)
	} else {
		var l, q int64
		l = MAX(x, y)
		q = MIN(x, y)
		var m int64
		for l*q > 0 {
			a := l % q
			b := l - a
			cc := int64(b / q)
			remain := l - cc*q
			l = MAX(remain, q)
			q = MIN(remain, q)
			if l*q == 0 {
				m = l + q
				break
			}

		}
		return m
	}
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

func MODULE(x, y int) int {
	if x > y {
		return x - y
	} else {
		return y - x
	}
}

func BITAND(x, y int64) int64 {
	var ans int64
	begin := x
	sec := y
	var count int64 = 1
	for 1 == 1 {
		if (begin%2)*(sec%2) == 1 {
			ans = ans + count
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}

		} else {
			count = count * 2
			begin = int64(begin / 2)
			sec = int64(sec / 2)
			if begin*sec == 0 {
				break
			}
		}
	}
	return ans
}

func Sum(n, m int) int {
	var ans int

	if n == 1 {
		return 0
	} else {
		for i := 2; i < n; i++ {
			ans = (ans + Sum(i, m)) % m
		}
		for j := 2; j < n+1; j++ {
			ans = (ans + Sum(n/j, m)) % m
		}
		return ans
	}
}

func ChangeStringToDate(x string) (t time.Time, err error) {
	t, err = time.Parse("2006-01-02", x)
	return
}

func DiffrenceBetweenTwoDates(first, second time.Time) int64 {
	date := first
	date2 := second
	k := date.Unix() - date2.Unix()
	return k
}

func Union(a, b []int) []int {
	m := make(map[int]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; !ok {
			a = append(a, item)
		}
	}
	return a
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		STRING := make([]byte, 0)
		fmt.Fscan(input, &STRING)

		var first, second int
		var indf, inds int
		q := len(STRING)
		for i := q - 1; i >= 0; i-- {
			if string(STRING[i]) == "0" {
				indf = i - 1
				break
			} else {
				first++
			}
		}
		for i := q - 1; i >= 0; i-- {
			if string(STRING[i]) == "5" {
				inds = i - 1
				break
			} else {
				second++
			}
		}

		for j := indf; j >= 0; j-- {
			if string(STRING[j]) == "5" || string(STRING[j]) == "0" {
				break
			} else {
				first++
			}
		}

		for j := inds; j >= 0; j-- {
			if string(STRING[j]) == "2" || string(STRING[j]) == "7" {
				break
			} else {
				second++
			}
		}

		fmt.Println(MINN(first, second))
	}

}

package main

import (
	"fmt"
)

func max(a []int) int {
	x := a[0]
	for _, k := range a {
		if x < k {
			x = k
		}
	}
	return x
}

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for ; t > 0; t-- {
		var s string
		fmt.Scanf("%s\n", &s)
		k := len(s)
		for i := len(s) - 1; i >= 0; i-- {
			if s[i] != '0' && s[i] != '5' {
				continue
			}
			var v byte
			if s[i] == '0' {
				v = '0'
			}
			if s[i] == '5' {
				v = '2'
			}
			for j := i - 1; j >= 0; j-- {
				if (s[j] == v || s[j] == v+5) && (len(s)-j-2 < k) {
					k = len(s) - j - 2
					break
				}
			}
		}
		fmt.Printf("%d\n", k)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var t int
	fmt.Fscan(reader, &t)
	for i := 0; i < t; i++ {
		var s string
		fmt.Fscan(reader, &s)
		n := len(s)
		pos := strings.LastIndexByte(s, '5')
		result := 10000
		if pos >= 0 {
			idx1 := strings.LastIndexByte(s[0:pos], '2')
			idx2 := strings.LastIndexByte(s[0:pos], '7')
			result = n - max(idx1, idx2) - 2
		}
		pos = strings.LastIndexByte(s, '0')
		if pos >= 0 {
			idx1 := strings.LastIndexByte(s[0:pos], '0')
			idx2 := strings.LastIndexByte(s[0:pos], '5')
			result = min(n-max(idx1, idx2)-2, result)
		}
		fmt.Fprintln(writer, result)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const Inf = 1000

var (
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)

	T int
)

func latest(s []byte, c byte) ([]byte, int) {
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == c {
			return s[:i], len(s) - i - 1
		}
	}
	return s, Inf
}

func latests(s []byte, c []byte) ([]byte, int) {
	var ans int
	for i := len(c) - 1; i >= 0; i-- {
		var res int
		s, res = latest(s, c[i])
		ans += res
	}
	return s, ans
}

func main() {
	fmt.Fscan(r, &T)
	for ; T > 0; T-- {
		var s string
		fmt.Fscan(r, &s)

		var a [4]int

		_, a[0] = latests([]byte(s), []byte("00"))
		_, a[1] = latests([]byte(s), []byte("25"))
		_, a[2] = latests([]byte(s), []byte("50"))
		_, a[3] = latests([]byte(s), []byte("75"))

		min := a[0]
		for _, i := range a {
			if min > i {
				min = i
			}
		}
		fmt.Fprintln(w, min)
	}
	_ = w.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var N string

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

	for i := 0; i < t; i++ {
		fmt.Fscan(r, &N)

		ans := solve()
		fmt.Println(ans)
	}
}

func solve() int {
	var minCount int = 1e9
	var count int
	for j := len(N) - 1; j >= 0; j-- {

		if N[j] == '5' {

			var add int

			found := false
			for k := j - 1; k >= 0; k-- {
				if N[k] == '2' || N[k] == '7' {
					found = true
					break
				}
				add++
			}

			if found {
				minCount = min(minCount, count + add)
			}

			count++

		} else if N[j] == '0' {

			var add int

			found := false
			for k := j - 1; k >= 0; k-- {
				if N[k] == '0' || N[k] == '5' {
					found = true
					break
				}
				add++
			}

			if found {
				minCount = min(minCount, count + add)
			}

			count++

		} else {
			count++
		}
	}

	return minCount
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var T int
	fmt.Fscanf(r, "%d\n", &T)
	for t := 0; t < T; t++ {
		var num string
		fmt.Fscanf(r, "%s\n", &num)

		suf := []string{"00", "25", "50", "75"}

		res := len(num)

		for _, s := range suf {
			for i := 0; i < len(num); i++ {
				for j := i + 1; j < len(num); j++ {
					if num[i] == s[0] && num[j] == s[1] {
						k := len(num) - j - 1 + j - i - 1
						if k < res {
							res = k
						}
					}
				}
			}
		}

		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

func main() {
	var n int

	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var s string
		ans := 100000000
		fmt.Scan(&s)
		for j := 0; j+1 < len(s); j++ {
			for k := i + 1; k < len(s); k++ {
				now := int(s[j]-'0')*10 + int(s[k]-'0')
				if now%25 == 0 {
					ans = min(ans, len(s)-j-2)
				}
			}
		}
		fmt.Println(ans)
	}

}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// func max(a, b int) int {
// 	if a > b {
// 		return a
// 	}
// 	return b
// }

package main

import "fmt"

func main() {
	var n int

	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var s string
		ans := 1000000009
		fmt.Scan(&s)
		for j := 0; j+1 < len(s); j++ {
			for k := i + 1; k < len(s); k++ {
				now := int(s[i]-'0')*10 + int(s[j]-'0')
				if now%25 == 0 {
					ans = min(ans, len(s)-i-2)
				}
			}
		}
		fmt.Println(ans)
	}

}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import "fmt"

func main() {
	var t int
	var s string
	fmt.Scan(&t)

	for t > 0 {
		t --
		fmt.Scan(&s)
		fmt.Println(divisible(s))
	}
}

func divisible(s string) int {
	l := len(s)
	if l < 3 || checkDiv(s[l-2 : l]) {
		return 0
	}

	//var c byte = ' '
	//var k = 0
	for i := l - 2; i >= 0; i -- {
		for j := i + 1; j < l; j ++ {

			if checkDiv(string(s[i]) + string(s[j])) {
				//fmt.Printf("%d %d\n", i, j)
				return asb(l - j + i - j)
			}
		}
	}

	return -1
}

func checkDiv(s string) bool {
	return s == "00" || s == "25" || s == "50" || s == "75"
}

func asb(x int) int {
	if x < 0 {
		x = -x
	}

	return x
}


package main

import (
  "bufio"
  "io"
  "os"
  "strconv"
)

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.Text()[0] }

func ni32() int {
  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())
  return n
}

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

var mat [][]int

func check(value int) bool {
  n := len(mat)
  m := len(mat[0])
  was := make([][]bool, m)
  for i := 0; i < m; i++ {
    was[i] = make([]bool, m)
  }
  good := make([]int, m)
  for i := 0; i < n; i++ {
    tl := 0
    for j := 0; j < m; j++ {
      if mat[i][j] >= value {
        good[tl] = j
        tl++
        for t := 0; t < tl-1; t++ {
          if was[good[t]][good[tl-1]] {
            return true
          } else {
            was[good[t]][good[tl-1]] = true
          }
        }
      }
    }
  }
  return false
}

func main() {
  n, m := ni32(), ni32()
  mat = make([][]int, n)
  for i := 0; i < n; i++ {
    mat[i] = make([]int, m)
  }
  for i := 0; i < n; i++ {
    for j := 0; j < m; j++ {
      mat[i][j] = ni32()
    }
  }

  left, right := -1, 1000000009
  for left < right-1 {
    mid := (left + right) >> 1
    if check(mid) {
      left = mid
    } else {
      right = mid
    }
  }
  pri32(left)
  w.Flush()
}

package main

import (
  "bufio"
  "io"
  "os"
  "strconv"
)

type Scanner struct {
  r            io.Reader
  split        bufio.SplitFunc
  maxTokenSize int
  token, buf   []byte
  start, end   int
}

func NewScanner(r io.Reader) *Scanner {
  return &Scanner{
    r:            r,
    split:        bufio.ScanWords,
    maxTokenSize: 16777216,
    buf:          make([]byte, 4096),
  }
}

func (s *Scanner) advance(n int) bool {
  if n < 0 || n > s.end-s.start {
    return false
  }
  s.start += n
  return true
}

func (s *Scanner) Text() string {
  return string(s.token)
}

func (s *Scanner) Scan() bool {
  for {
    if s.end > s.start {
      advance, token, err := s.split(s.buf[s.start:s.end], false)
      if err != nil || !s.advance(advance) {
        return false
      }
      s.token = token
      if token != nil {
        return true
      }
    }
    if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
      copy(s.buf, s.buf[s.start:s.end])
      s.end -= s.start
      s.start = 0
    }
    if s.end == len(s.buf) {
      if len(s.buf) >= s.maxTokenSize {
        return false
      }
      newSize := len(s.buf) * 2
      if newSize > s.maxTokenSize {
        newSize = s.maxTokenSize
      }
      newBuf := make([]byte, newSize)
      copy(newBuf, s.buf[s.start:s.end])
      s.buf = newBuf
      s.end -= s.start
      s.start = 0
      continue
    }
    for loop := 0; ; {
      n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
      s.end += n
      if n > 0 {
        break
      }
      loop++
      if loop > 100 {
        break
      }
    }
  }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
  s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }
func nc() byte     { sc.Scan(); return sc.Text()[0] }

func ni32() int {
  sc.Scan()
  n, _ := strconv.Atoi(sc.Text())
  return n
}

func ni64() int64 {
  sc.Scan()
  n, _ := strconv.ParseInt(sc.Text(), 10, 64)
  return n
}

func pri32(i ...int) {
  for _, ii := range i {
    w.WriteString(strconv.Itoa(ii))
    sp()
  }
}
func pri64(i ...int64) {
  for _, ii := range i {
    w.WriteString(strconv.FormatInt(ii, 10))
    sp()
  }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func check(mat [][]int, value int) bool {
  n := len(mat)
  m := len(mat[0])
  was := make([][]bool, m)
  for i := 0; i < m; i++ {
    was[i] = make([]bool, m)
  }
  good := make([]int, m)
  for i := 0; i < n; i++ {
    tl := 0
    for j := 0; j < m; j++ {
      if mat[i][j] >= value {
        good[tl] = j
        tl++
        for t := 0; t < tl-1; t++ {
          if was[good[t]][good[tl-1]] {
            return true
          } else {
            was[good[t]][good[tl-1]] = true
          }
        }
      }
    }
  }
  return false
}

func main() {
  mat := make([][]int, 1010)
  for i := 0; i < 1010; i++ {
    mat[i] = make([]int, 1010)
  }
  n, m := ni32(), ni32()
  for i := 0; i < n; i++ {
    for j := 0; j < m; j++ {
      mat[i][j] = ni32()
    }
  }

  left, right := -1, 1000000009
  for left < right-1 {
    mid := (left + right) >> 1
    if check(mat, mid) {
      left = mid
    } else {
      right = mid
    }
  }
  pri32(left)
  w.Flush()
}


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

type UF struct {
	parents []int
	size    []int
}

func (u *UF) init(n int) {
	u.parents = make([]int, n)
	u.size = make([]int, n)
	for i := range u.parents {
		u.parents[i] = i
		u.size[i] = 1
	}
}

func (u *UF) find(x int) int {
	for u.parents[x] != x {
		u.parents[x] = u.parents[u.parents[x]]
		x = u.parents[x]
	}
	return x
}

func (u *UF) union(a, b int) {
	pa := u.find(a)
	pb := u.find(b)
	if pa == pb {
		return
	}
	if u.size[pa] < u.size[pb] {
		pa, pb = pb, pa
	}
	u.parents[pb] = pa
	u.size[pa] += u.size[pb]
}

func (u *UF) connected(a, b int) bool {
	return u.find(a) == u.find(b)
}

func CF1559D1(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m1, m2 int
	v := make([]int, 2)
	Fscan(in, &n, &m1, &m2)
	u1 := &UF{}
	u1.init(n + 1)
	u2 := &UF{}
	u2.init(n + 1)

	for i := 0; i < m1; i++ {
		Fscan(in, &v[0], &v[1])
		u1.union(v[0], v[1])
	}
	for i := 0; i < m2; i++ {
		Fscan(in, &v[0], &v[1])
		u2.union(v[0], v[1])
	}
	ans := make([][2]int, 0, 1000)
	for i := 1; i <= n; i++ {
		for j := i + 1; j <= n; j++ {
			if u1.connected(i, j) || u2.connected(i, j) {
				continue
			}
			u1.union(i, j)
			u2.union(i, j)
			ans = append(ans, [2]int{i, j})
		}
	}
	Fprintln(out, len(ans))
	for _, v := range ans {
		Fprintf(out, "%d %d\n", v[0], v[1])
	}
}

func main() {
	CF1559D1(os.Stdin, os.Stdout)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var in = NewIn()
var out = NewOut()

func main() {
	defer out.flush()

	// tests()
	solve()
}

/* --- Solution D --- */
func solve() {
	n := in.nextInt()
	m1 := in.nextInt()
	m2 := in.nextInt()

	uf1 := NewUF(n)
	uf2 := NewUF(n)

	for i := 0; i < m1+m2; i++ {
		v := in.nextInt() - 1
		w := in.nextInt() - 1
		if i < m1 {
			uf1.union(v, w)
		} else {
			uf2.union(v, w)
		}
	}

	type edge struct{ v, w int }

	answer := []edge{}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if !uf1.connected(i, j) && !uf2.connected(i, j) {
				answer = append(answer, edge{i + 1, j + 1})
				uf1.union(i, j)
				uf2.union(i, j)
			}
		}
	}

	out.println(len(answer))
	for _, e := range answer {
		out.printf("%d %d\n", e.v, e.w)
	}
}

type UF struct {
	id []int
	sz []int
}

func NewUF(N int) *UF {
	uf := UF{}
	uf.id = make([]int, N)
	for i := range uf.id {
		uf.id[i] = i
	}
	uf.sz = make([]int, N)
	for i := range uf.sz {
		uf.sz[i] = 1
	}
	return &uf
}

func (uf *UF) find(p int) int {
	for uf.id[p] != p {
		p = uf.id[p]
	}
	return p
}

func (uf *UF) union(p, q int) {
	pRoot := uf.find(p)
	qRoot := uf.find(q)
	if pRoot == qRoot {
		return
	}
	if uf.sz[pRoot] > uf.sz[qRoot] {
		uf.id[qRoot] = pRoot
		uf.sz[pRoot] += uf.sz[qRoot]
	} else {
		uf.id[pRoot] = qRoot
		uf.sz[qRoot] += uf.sz[pRoot]
	}
}

func (uf *UF) connected(p, q int) bool {
	return uf.find(p) == uf.find(q)
}

func tests() {
	tt := in.nextInt()
	for ; tt > 0; tt-- {
		solve()
	}
}

/* --- I/O --- */
type In struct {
	scanner *bufio.Scanner
}

func NewIn() *In {
	in := In{bufio.NewScanner(os.Stdin)}
	in.scanner.Split(bufio.ScanWords)
	return &in
}

func (in *In) nextInt() int {
	in.scanner.Scan()
	i, _ := strconv.Atoi(in.scanner.Text())
	return i
}

func (in *In) nextLong() int64 {
	in.scanner.Scan()
	i, _ := strconv.ParseInt(in.scanner.Text(), 10, 64)
	return i
}

func (in *In) next() string {
	in.scanner.Scan()
	return in.scanner.Text()
}

type Out struct {
	writer *bufio.Writer
}

func NewOut() *Out {
	out := Out{bufio.NewWriter(os.Stdout)}
	return &out
}

func (out *Out) println(a ...interface{}) {
	fmt.Fprintln(out.writer, a...)
}

func (out *Out) printf(format string, a ...interface{}) {
	fmt.Fprintf(out.writer, format, a...)
}

func (out *Out) print(a ...interface{}) {
	fmt.Fprint(out.writer, a...)
}

func (out *Out) flush() {
	out.writer.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
var f1 [3005]int
var f2 [3005]int
func findf1(x int)int{
	if x!=f1[x]{
		f1[x]=findf1(f1[x])
	}
	return f1[x]
}
func findf2(x int)int{
	if x!=f2[x]{
		f2[x]=findf2(f2[x])
	}
	return f2[x]
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var n,m1,m2 int
	fmt.Fscan(in,&n,&m1,&m2)
	var e1 [3005][3005]int
	var e2 [3005][3005]int
	for i:=1;i<=n;i++{
		f1[i]=i
		f2[i]=i
	}
	for i:=1;i<=m1;i++{
		var u,v int
		fmt.Fscan(in,&u,&v)
		e1[u][v]=1
		e1[v][u]=1
		f1u := findf1(u)
		f1v := findf1(v)
		if f1u!=f1v{
			f1[f1u]=f1v
		}
	}
	for i:=1;i<=m2;i++{
		var u,v int
		fmt.Fscan(in,&u,&v)
		e2[u][v]=1
		e2[v][u]=1
		f2u := findf2(u)
		f2v := findf2(v)
		if f2u!=f2v{
			f2[f2u]=f2v
		}
	}
	ans:=0
	cool := [][]int{}
	for i:=1;i<=n;i++{
		for j:=1;j<=n;j++{
			if e1[i][j]!=0||e2[i][j]!=0{
				continue
			}
			if findf1(i)!=findf1(j)&&findf2(j)!=findf2(i){
				f1i := findf1(i)
				f1j := findf1(j)
				f1[f1i]=f1j
				f2i := findf2(i)
				f2j := findf2(j)
				f2[f2i]=f2j
				ans++
				e1[i][j]=1
				e2[i][j]=1
				e1[j][i]=1
				e2[j][i]=1
				cool = append(cool,[]int{i,j})
			}
		}
	}
	fmt.Fprintln(out,ans)
	for i:=1;i<=ans;i++{
		fmt.Fprintln(out,cool[i-1][0],cool[i-1][1])
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

type Scanner struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) *Scanner {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	return &Scanner{scanner}
}

func (s *Scanner) Word() string {
	s.Scan()
	return s.Text()
}

func (s *Scanner) Int() int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())
	return n
}

func (s *Scanner) Int64() int64 {
	s.Scan()
	n, _ := strconv.ParseInt(s.Text(), 10, 64)
	return n
}

func (s *Scanner) Words(count int) []string {
	words := make([]string, count)
	for i := range words {
		words[i] = s.Word()
	}
	return words
}

func (s *Scanner) Ints(count int) []int {
	ints := make([]int, count)
	for i := range ints {
		ints[i] = s.Int()
	}
	return ints
}

func (s *Scanner) Int64s(count int) []int64 {
	int64s := make([]int64, count)
	for i := range int64s {
		int64s[i] = s.Int64()
	}
	return int64s
}

type disjointSet struct {
	parent []int
}

func newDisjointSet(size int) *disjointSet {
	parent := make([]int, size)
	for i := range parent {
		parent[i] = -1
	}

	return &disjointSet{
		parent: parent,
	}
}

func (d *disjointSet) root(node int) int {
	if d.parent[node] == -1 {
		return node
	}

	d.parent[node] = d.root(d.parent[node])
	return d.parent[node]
}

func (d *disjointSet) join(a, b int) {
	ra := d.root(a)
	rb := d.root(b)
	if ra != rb {
		d.parent[rb] = ra
	}
}

func (d *disjointSet) connected(a, b int) bool {
	return d.root(a) == d.root(b)
}

type graph struct {
	set   *disjointSet
	nodes int
}

func newGraph(nodes int) *graph {
	return &graph{
		set:   newDisjointSet(nodes),
		nodes: nodes,
	}
}

func (g *graph) addEdge(a, b int) {
	g.set.join(a, b)
}

func (g *graph) canConnect(a, b int) bool {
	return !g.set.connected(a, b)
}

func solve(g1, g2 *graph) [][]int {
	res := [][]int{}
	for i := 0; i < g1.nodes; i += 1 {
		for j := i + 1; j < g1.nodes; j += 1 {
			if g1.canConnect(i, j) && g2.canConnect(i, j) {
				g1.addEdge(i, j)
				g2.addEdge(i, j)
				res = append(res, []int{i, j})
			}
		}
	}
	return res
}

func main() {
	stdin := NewWordScanner(os.Stdin)
	nodes, e1, e2 := stdin.Int(), stdin.Int(), stdin.Int()

	g1 := newGraph(nodes)
	for i := 0; i < e1; i += 1 {
		a, b := stdin.Int()-1, stdin.Int()-1
		g1.addEdge(a, b)
	}

	g2 := newGraph(nodes)
	for i := 0; i < e2; i += 1 {
		a, b := stdin.Int()-1, stdin.Int()-1
		g2.addEdge(a, b)
	}

	res := solve(g1, g2)
	fmt.Println(len(res))
	for _, p := range res {
		fmt.Println(p[0]+1, p[1]+1)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	defer wr.Flush()
	n, m1, m2 := scanInt(), scanInt(), scanInt()
	mocha, diana := newUnionFind(n+1), newUnionFind(n+1)
	for i := 0; i < m1; i++ {
		u, v := scanInt(), scanInt()
		mocha.Union(u, v)
	}
	for i := 0; i < m2; i++ {
		u, v := scanInt(), scanInt()
		diana.Union(u, v)
	}

	addIfCan := func() (bool, int, int) {
		for i := 1; i <= n; i++ {
			x := mocha.Find(i)
			for j := i + 1; j <= n; j++ {
				y := mocha.Find(j)
				if x == y {
					continue
				}
				p, q := diana.Find(i), diana.Find(j)
				if p == q {
					continue
				}
				mocha.Union(x, y)
				diana.Union(p, q)
				return true, i, j
			}
		}
		return false, 0, 0
	}

	ret := make([][2]int, 0)
	for {
		added, i, j := addIfCan()
		if !added {
			break
		}
		ret = append(ret, [2]int{i, j})
	}

	fmt.Fprintln(wr, len(ret))
	for _, p := range ret {
		fmt.Fprintf(wr, "%d %d\n", p[0], p[1])
	}
}

type UnionFind []int

func newUnionFind(n int) UnionFind {
	s := make([]int, n)
	for i := range s {
		s[i] = i
	}
	return s
}

func (uf UnionFind) Union(x, y int) {
	x, y = uf.Find(x), uf.Find(y)
	if x == y {
		return
	} else if y < x {
		x, y = y, x
	}
	// always, x < y
	uf[x] = y
}

func (uf UnionFind) Find(x int) int {
	p := uf[x]
	if x == p {
		return x
	}
	uf[x] = uf.Find(p)
	return uf[x]
}

var (
	sc *bufio.Scanner
	wr *bufio.Writer
)

func init() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	wr = bufio.NewWriter(os.Stdout)
}

func scanInt() int {
	sc.Scan()
	ret, _ := strconv.Atoi(sc.Text())
	return ret
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
type uf struct {
	fa []int
}

func newUnionFind(n int) uf {
	fa := make([]int, n)
	for i := range fa {
		fa[i] = i
	}
	return uf{fa}
}

func (u uf) find(x int) int {
	if u.fa[x] != x {
		u.fa[x] = u.find(u.fa[x])
	}
	return u.fa[x]
}

func (u uf) merge(from, to int) (isNewMerge bool) {
	x, y := u.find(from), u.find(to)
	if x == y {
		return false
	}
	u.fa[x] = y
	return true
}

func (u uf) same(x, y int) bool { return u.find(x) == u.find(y) }

func (u uf) countRoots(l, r int) (cnt int) {
	for i := l; i <= r; i++ {
		if u.find(i) == i {
			cnt++
		}
	}
	return
}

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m1, m2, v, w int
	Fscan(in, &n, &m1, &m2)
	u1, u2 := newUnionFind(n+1), newUnionFind(n+1)
	for i := 0; i < m1; i++ {
		Fscan(in, &v, &w)
		u1.merge(v, w)
	}
	for i := 0; i < m2; i++ {
		Fscan(in, &v, &w)
		u2.merge(v, w)
	}
	ans := [][2]int{}
	for i := 1; i <= n; i++ {
		for j := i + 1; j <= n; j++ {
			if !u1.same(i, j) && !u2.same(i, j) {
				ans = append(ans, [2]int{i, j})
				u1.merge(i, j)
				u2.merge(i, j)

			}
		}
	}
	Fprintln(out, len(ans))
	for _, p := range ans {
		Fprintln(out, p[0], p[1])
	}

}

func main() { run(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

var _, _, _ = main, rw, do

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	rw(r, w)
}

type edge struct {
	a, b int
}

func rw(r io.Reader, w io.Writer) {
	var n, m1, m2 int
	fmt.Fscan(r, &n, &m1, &m2)
	e1 := make([]edge, m1)
	for i := 0; i < m1; i++ {
		fmt.Fscan(r, &e1[i].a, &e1[i].b)
	}
	e2 := make([]edge, m2)
	for i := 0; i < m2; i++ {
		fmt.Fscan(r, &e2[i].a, &e2[i].b)
	}
	w1 := do(n, e1[:m1], e2[:m2])
	fmt.Fprintln(w, len(w1))
	for _, v := range w1 {
		fmt.Fprintln(w, v.a, v.b)
	}
}

func getF(f []int, x int) int {
	if f[x] != x {
		f[x] = getF(f, f[x])
	}
	return f[x]
}

func newF(n int) []int {
	f := make([]int, n+1)
	for i := 1; i <= n; i++ {
		f[i] = i
	}
	return f
}

func newInitF(n int, e []edge) []int {
	f := newF(n)
	for _, v := range e {
		union(f, v.a, v.b)
	}
	return f
}

func union(f []int, i, j int) {
	fi, fj := getF(f, i), getF(f, j)
	f[fi] = fj
}

func same(f []int, i, j int) bool {
	return getF(f, i) == getF(f, j)
}

func do(n int, e1, e2 []edge) []edge {
	f1 := newInitF(n, e1)
	f2 := newInitF(n, e2)
	var r []edge
	for i := 1; i <= n; i++ {
		for j := i + 1; j <= n; j++ {
			if !same(f1, i, j) && !same(f2, i, j) {
				r = append(r, edge{i, j})
				union(f1, i, j)
				union(f2, i, j)
			}
		}
	}
	return r
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

var _, _, _ = main, rw, do

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	rw(r, w)
}

type edge struct {
	a, b int
}

func rw(r io.Reader, w io.Writer) {
	var n, m1, m2 int
	fmt.Fscan(r, &n, &m1, &m2)
	e1 := make([]edge, m1)
	for i := 0; i < m1; i++ {
		fmt.Fscan(r, &e1[i].a, &e1[i].b)
	}
	e2 := make([]edge, m2)
	for i := 0; i < m2; i++ {
		fmt.Fscan(r, &e2[i].a, &e2[i].b)
	}
	w1 := do(n, e1[:m1], e2[:m2])
	fmt.Fprintln(w, len(w1))
	for _, v := range w1 {
		fmt.Fprintln(w, v.a, v.b)
	}
}

func getF(f []int, x int) int {
	if f[x] != x {
		f[x] = getF(f, f[x])
	}
	return f[x]
}

func newF(n int) []int {
	f := make([]int, n+1)
	for i := 1; i <= n; i++ {
		f[i] = i
	}
	return f
}

func newInitF(n int, e []edge) []int {
	f := newF(n)
	for _, v := range e {
		union(f, v.a, v.b)
	}
	return f
}

func union(f []int, i, j int) {
	fi, fj := getF(f, i), getF(f, j)
	f[fi] = fj
}

func same(f []int, i, j int) bool {
	return getF(f, i) == getF(f, j)
}

func do(n int, e1, e2 []edge) []edge {
	if len(e1) > len(e2) {
		e1, e2 = e2, e1
	}
	f1 := newInitF(n, e1)
	f2 := newInitF(n, e2)
	var r []edge
	for i := 1; i <= n; i++ {
		for j := i + 1; j <= n; j++ {
			if !same(f1, i, j) && !same(f2, i, j) {
				r = append(r, edge{i, j})
				union(f1, i, j)
				union(f2, i, j)
			}
		}
	}
	return r
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type dsu struct {
	p []int
	d []int
	c []int
}

func newdsu(n int) *dsu {
	res := &dsu{
		make([]int, n+1),
		make([]int, n+1),
		make([]int, n+1),
	}
	for i := 0; i <= n; i++ {
		res.p[i] = i
		res.c[i] = 1
	}
	return res
}

func (d *dsu) add(i, j int) {
	pi := d.get(i)
	pj := d.get(j)

	if d.d[pi] > d.d[pj] {
		d.p[pj] = pi
		d.c[pj] += d.c[pj]
	} else {
		d.p[pi] = pj
		d.c[pi] += d.c[pj]
		if d.d[pj] < d.d[pi]+1 {
			d.d[pj] = d.d[pi] + 1
		}
	}
}

func (d *dsu) get(i int) int {
	j := i
	for i != d.p[i] {
		i = d.p[i]
	}

	for j != d.p[j] {
		j = d.p[j]
		d.p[j] = i
	}

	return i
}

type edge struct {
	u, v int
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var n, m1, m2 int

	e1 := make([]edge, 0)
	e2 := make([]edge, 0)
	fmt.Fscanf(r, "%d %d %d\n", &n, &m1, &m2)

	for i := 0; i < m1+m2; i++ {
		var u, v int
		fmt.Fscanf(r, "%d %d\n", &u, &v)

		if i < m1 {
			e1 = append(e1, edge{u, v})
		} else {
			e2 = append(e2, edge{u, v})
		}
	}

	d1 := newdsu(n * 2)
	d2 := newdsu(n * 2)

	for _, e := range e1 {
		d1.add(e.u, e.v)
	}
	for _, e := range e2 {
		d2.add(e.u, e.v)
	}

	root := make(map[int]bool)

	for i := 1; i <= n; i++ {
		if d2.get(i) == i {
			root[i] = true
		}
	}

	s := []edge{}
	for i := 1; i <= n; i++ {
		if d1.get(i) == i {
			s = append(s, edge{i, d1.c[i]})
		}
	}
	sort.Slice(s, func(i, j int) bool {
		return s[i].v < s[j].v
	})

	res := []edge{}
	for _, x := range s {
		j := x.u
		if d1.get(j) == j {
			for i, _ := range root {
				if d1.get(i) != d1.get(j) && d2.get(i) != d2.get(j) {
					d1.add(j, i)
					d2.add(j, i)
					if d2.get(i) != i {
						delete(root, i)
					}
					if d2.get(j) != j {
						delete(root, j)
					}
					res = append(res, edge{j, i})
				}
			}
		}
	}

	w := bufio.NewWriterSize(os.Stdout, 1024*1024)

	fmt.Fprintln(w, len(res))
	for _, e := range res {
		fmt.Fprintln(w, e.u, e.v)
	}
	w.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

type dsu struct {
	p []int
	d []int
}

func newdsu(n int) *dsu {
	res := &dsu{
		make([]int, n+1),
		make([]int, n+1),
	}
	for i := 0; i <= n; i++ {
		res.p[i] = i
	}
	return res
}

func (d *dsu) add(i, j int) {
	pi := d.get(i)
	pj := d.get(j)

	if d.d[pi] > d.d[pj] {
		d.p[pj] = pi
	} else {
		d.p[pi] = pj
		if d.d[pj] < d.d[pi]+1 {
			d.d[pj] = d.d[pi] + 1
		}
	}
}

func (d *dsu) get(i int) int {
	j := i
	for i != d.p[i] {
		i = d.p[i]
	}

	for j != d.p[j] {
		j = d.p[j]
		d.p[j] = i
	}

	return i
}

type edge struct {
	u, v int
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var n, m1, m2 int

	e1 := make([]edge, 0)
	e2 := make([]edge, 0)
	fmt.Fscanf(r, "%d %d %d\n", &n, &m1, &m2)

	for i := 0; i < m1+m2; i++ {
		var u, v int
		fmt.Fscanf(r, "%d %d\n", &u, &v)

		if i < m1 {
			e1 = append(e1, edge{u, v})
		} else {
			e2 = append(e2, edge{u, v})
		}
	}

	d1 := newdsu(n * 2)
	d2 := newdsu(n * 2)

	for _, e := range e1 {
		d1.add(e.u, e.v)
	}
	for _, e := range e2 {
		d2.add(e.u, e.v)
	}

	res := []edge{}
	for j := 1; j <= n; j++ {
		for i := j + 1; i <= n; i++ {
			if d1.get(i) != d1.get(j) && d2.get(i) != d2.get(j) {
				d1.add(j, i)
				d2.add(j, i)
				res = append(res, edge{j, i})
			}
		}
	}

	fmt.Println(len(res))
	for _, e := range res {
		fmt.Println(e.u, e.v)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

var _, _, _ = main, rw, do

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	rw(r, w)
}

type edge struct {
	a, b int
}

func rw(r io.Reader, w io.Writer) {
	var n, m1, m2 int
	fmt.Fscan(r, &n, &m1, &m2)
	e1 := make([]edge, m1)
	for i := 0; i < m1; i++ {
		fmt.Fscan(r, &e1[i].a, &e1[i].b)
	}
	e2 := make([]edge, m2)
	for i := 0; i < m2; i++ {
		fmt.Fscan(r, &e2[i].a, &e2[i].b)
	}
	w1 := do(n, e1[:m1], e2[:m2])
	fmt.Fprintln(w, len(w1))
	for _, v := range w1 {
		fmt.Fprintln(w, v.a, v.b)
	}
}

func getF(f []int, x int) int {
	if f[x] == x {
		return x
	}
	f[x] = getF(f, f[x])
	return f[x]
}

func newF(n int) []int {
	f := make([]int, n+1)
	for i := 1; i <= n; i++ {
		f[i] = i
	}
	return f
}

func newInitF(n int, e []edge) []int {
	f := newF(n)
	for _, v := range e {
		f[v.a] = getF(f, v.b)
	}
	return f
}

func do(n int, e1, e2 []edge) []edge {
	var r []edge
	if len(e1) > len(e2) {
		e1, e2 = e2, e1
	}
	f1 := newInitF(n, e1)
	f2 := newInitF(n, e2)
	for i := 1; i <= n; i++ {
		for j := i + 1; j <= n; j++ {
			f1i, f1j := getF(f1, i), getF(f1, j)
			f2i, f2j := getF(f2, i), getF(f2, j)
			if f1i != f1j && f2i != f2j {
				r = append(r, edge{i, j})
				f1[f1i] = f1j
				f2[f2i] = f2j
			}
		}
	}
	return r
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type dsu struct {
	p []int
	d []int
}

func newdsu(n int) *dsu {
	res := &dsu{
		make([]int, n+1),
		make([]int, n+1),
	}
	for i := 0; i <= n; i++ {
		res.p[i] = i
	}
	return res
}

func (d *dsu) add(i, j int) {
	pi := d.get(i)
	pj := d.get(j)

	if d.d[pi] > d.d[pj] {
		d.p[pj] = pi
	} else {
		d.p[pi] = pj
		if d.d[pj] < d.d[pi]+1 {
			d.d[pj] = d.d[pi] + 1
		}
	}
}

func (d *dsu) get(i int) int {
	j := i
	for i != d.p[i] {
		i = d.p[i]
	}

	for j != d.p[j] {
		j = d.p[j]
		d.p[j] = i
	}

	return i
}

type edge struct {
	u, v int
}

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)

	var n, m1, m2 int

	e1 := make([]edge, 0)
	e2 := make([]edge, 0)
	fmt.Fscanf(r, "%d %d %d\n", &n, &m1, &m2)

	for i := 0; i < m1+m2; i++ {
		var u, v int
		fmt.Fscanf(r, "%d %d\n", &u, &v)

		if u > v {
			u, v = v, u
		}

		if i < m1 {
			e1 = append(e1, edge{u, v})
		} else {
			e2 = append(e2, edge{u, v})
		}
	}

	sort.Slice(e1, func(i, j int) bool {
		return e1[i].u < e1[j].u
	})
	sort.Slice(e2, func(i, j int) bool {
		return e2[i].u < e2[j].u
	})

	d1 := newdsu(n * 2)
	d2 := newdsu(n * 2)

	for _, e := range e1 {
		d1.add(e.u, e.v)
	}
	for _, e := range e2 {
		d2.add(e.u, e.v)
	}

	res := []edge{}
	for j := 1; j <= n; j++ {
		for i := 2; i <= n; i++ {
			if d1.get(i) != d1.get(j) && d2.get(i) != d2.get(j) {
				d1.add(j, j)
				d2.add(j, i)
				res = append(res, edge{j, i})
			}
		}
	}

	fmt.Println(len(res))
	for _, e := range res {
		fmt.Println(e.u, e.v)
	}
}