// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

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

type Command struct {
	command, ip string
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	// fmt.Println("Hello, world!")
	input := gsli(reader)

	serverCount := input[0]
	commandCount := input[1]

	servers := make(map[string]string)
	var commands []Command

	for i := 0; i < serverCount; i++ {
		in := gsls(reader)
		name := in[0]
		ip := in[1]
		servers[ip] = name
	}

	for i := 0; i < commandCount; i++ {
		in := gsls(reader)
		command := in[0] + " " + in[1]
		ip := in[1][:len(in[1])-1]
		var curCom Command
		curCom.ip = ip
		curCom.command = command
		commands = append(commands, curCom)
	}

	for _, command := range commands {
		name := servers[command.ip]
		fmt.Printf("%v #%v\n", command.command, name)

	}

}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

package main

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

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	n := readi()
	m := readi()
	hosts := make(map[string]string)
	for i := 0; i < n; i++ {
		name := reads()
		ip := reads()
		hosts[ip] = name
	}
	for i := 0; i < m; i++ {
		cmd := reads()
		ip := reads()
		ip = ip[:len(ip)-1]
		printf("%s %s; #%s\n", cmd, ip, hosts[ip])
	}
}

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	// fmt.Println("Hello, world!")
	input := gsli(reader)

	serverCount := input[0]
	commandCount := input[1]

	servers := make(map[string]string)
	commands := make(map[string]string)

	for i := 0; i < serverCount; i++ {
		in := gsls(reader)
		name := in[0]
		ip := in[1]
		servers[ip] = name
	}

	for i := 0; i < commandCount; i++ {
		in := gsls(reader)
		command := in[0] + " " + in[1]
		ip := in[1][:len(in[1])-1]
		commands[command] = ip
	}

	for command, ip := range commands {
		name := servers[ip]
		fmt.Printf("%v #%v \n", command, name)
	}

}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}


package main

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

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

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		for ; !specialGcd(n, sumDigits(n)); n++ {
		}
		writer.WriteString(fmt.Sprintln(n))
	}
	writer.Flush()
}

func specialGcd(a, b uint64) bool {
	for i := uint64(2); i <= b; i++ {
		if a%i == 0 && b%i == 0 {
			return true
		}
	}
	return false
}

func sumDigits(a uint64) uint64 {
	var res uint64
	for ; a > 0; a /= 10 {
		res += a % 10
	}
	return res
}

func parseInt(str string) uint64 {
	var res uint64
	size := len(str)
	for i := 0; i < size; i++ {
		digit := uint64(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int64
		fmt.Fscan(in,&n)
		for gcd(n,summer(n)) == 1 {
			n++
		}
		fmt.Fprintln(out,n)
	}
}

func gcd(x,y int64) int64 { // looping function that returns itself until y=0 condtion met
	if y==0 {
		return x
	}
	return gcd(y,x%y)
}

func summer(x int64) int64 {
	var sum int64
	for x != 0 {
		sum+=(x%10)
		x/=10
	}
	return sum
}

package main

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

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

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		for {
			var sum int64
			for i := n; i > 0; i /= 10 {
				sum += i % 10
			}
			if specialGcd(n, sum) {
				writer.WriteString(fmt.Sprintln(n))
				break
			}
			n++
		}
	}
	writer.Flush()
}

func specialGcd(a, b int64) bool {
	for i := int64(2); i <= b; i++ {
		if a%i == 0 && b%i == 0 {
			return true
		}
	}
	return false
}

func parseInt(str string) int64 {
	var res int64
	size := len(str)
	for i := 0; i < size; i++ {
		digit := int64(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

package main

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

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

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		for ; !specialGcd(n, sumDigits(n)); n++ {
		}
		writer.WriteString(fmt.Sprintln(n))
	}
	writer.Flush()
}

func specialGcd(a, b int64) bool {
	for i := int64(2); i <= b; i++ {
		if a%i == 0 && b%i == 0 {
			return true
		}
	}
	return false
}

func sumDigits(a int64) int64 {
	var res int64
	for ; a > 0; a /= 10 {
		res += a % 10
	}
	return res
}

func parseInt(str string) int64 {
	var res int64
	size := len(str)
	for i := 0; i < size; i++ {
		digit := int64(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

package main

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

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

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		for ; specialGcd(n, sumDigits(n)) == 1; n++ {
		}
		writer.WriteString(fmt.Sprintln(n))
	}
	writer.Flush()
}

func specialGcd(a, b int64) int64 {
	for i := int64(165); i > 0; i-- {
		if a%i == 0 && b%i == 0 {
			return i
		}
	}
	return 1
}

func sumDigits(a int64) int64 {
	var res int64
	for ; a > 0; a /= 10 {
		res += a % 10
	}
	return res
}

func parseInt(str string) int64 {
	var res int64
	size := len(str)
	for i := 0; i < size; i++ {
		digit := int64(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func gcd(a, b int64) int64 {
	if b == 0 {
		return a
	} else {
		return gcd(b, a%b)
	}
}

func main() {
	// STDOUT MUST BE FLUSHED MANUALLY!!!
	defer writer.Flush()

	//f, _ := os.OpenFile("in", os.O_RDONLY,0666)
	//os.Stdin = f

	var t int
	scanf("%d\n", &t)
	var n int64
	for t != 0 {
		scanf("%d\n", &n)
		for {
			var sum int64
			tn := n
			for {
				if tn == 0 {
					break
				}
				sum += tn % 10
				tn /= 10
			}
			if gcd(n, sum) > 1 {
				break
			}
			n++
		}
		printf("%d\n", n)
		t--
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = (n * 10) + int(v-'0')
	}
	return
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (a Pairs) Len() int           { return len(a) }
func (a Pairs) Less(i, j int) bool { return a[i].a < a[j].a }
func (a Pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func gcd(a, b int64) int64 {
	if b != 0 {
		return gcd(b, a%b)
	} else {
		return a
	}
}

func gcdSum(n int64) int64 {
	temp, digit := n, int64(0)
	for temp > 0 {
		digit += temp % 10
		temp = temp / 10
	}
	ans := gcd(n, digit)
	return ans
}

func main() {
	defer writer.Flush()

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

	for t > 0 {
		var n int64
		scanf("%d\n", &n)
		if gcdSum(n) != 1 {
			printf("%d\n", n)
		} else if gcdSum(n+1) != 1 {
			printf("%d\n", n+1)
		} else if gcdSum(n+2) != 1 {
			printf("%d\n", n+2)
		}
		t--
	}

}

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	var t int64
	fmt.Scan(&t)
	for i := int64(0); i < t; i++ {
		var n int64
		fmt.Scan(&n)
		for ; ; n++ {
			s := calSum(n)
			check := false
			for j := int64(2); j <= s; j++ {
				if n%j == 0 && s%j == 0 {
					check = true
					break
				}
			}
			if check {
				fmt.Println(n)
				break
			}
		}
	}
}

func calSum(n int64) int64 {
	s := int64(0)
	for n > 0 {
		s += n % 10
		n /= 10
	}
	return s
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func ScanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func ScanInt() int {
	return int(ScanInt64())
}

func ScanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt64()

		for ; gcd(n, sumDigs(n)) == 1; n++ {
		}

		fmt.Println(n)
	}
}

func gcd(x, y int64) int64 {
	if x == 0 {
		return y
	}

	if y == 0 {
		return x
	}

	return gcd(y, x%y)
}

func sumDigs(x int64) int64 {
	var sum int64

	for x > 0 {
		sum += x % 10
		x /= 10
	}

	return sum
}

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}

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

func (s *Scanner) nextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (s *Scanner) nextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}

func max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func gcd(a int64, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func digitSum(N int64) int64 {
	cur := N
	sum := int64(0)
	for cur > 0 {
		sum += cur % 10
		cur /= 10
	}
	return sum
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.nextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.nextInt64()

		for gcd(N, digitSum(N)) == 1 {
			N++
		}
		fmt.Fprintln(wr, N)
	}

}

package main

import "fmt"
func gcd(n int64)int64{
    var temp int64;
    var sum int64;
    temp=n;
    sum=0;
    for temp>0{
        sum+=temp%10;
        temp/=10;
    }
    var res int64;
    if (n>sum){
        res=sum;
    }else {
        res=n;
    }
    //fmt.Println(res);
    for res>0{
        if(n%res==0 && sum%res==0){
            return res;
        }
        res-=1;
    }
    return 1;
}
func solve(){
    var n int64;
    //var i int64;
    fmt.Scanln(&n);
    if(gcd(n)!=1){
        fmt.Println(n);
    } else if(gcd(n+1)!=1){
        fmt.Println(n+1);
    }else if(gcd(n+2)!=2){
        fmt.Println(n+2);
    }

}
func main() {
   var t int;
   fmt.Scanln(&t);
   for t>0{
       solve();
       t=t-1;
   }
}

package main

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

type CF1498A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1498A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1498A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1498A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1498A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1498A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1498A
Date: 11/02/22
User: epradere
URL: https://codeforces.com/problemset/problem/1498/A
Problem: CF1498A
**/
func (in *CF1498A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		str := in.NextString()
		num, _ := strconv.ParseInt(str, 10, 64)

		ans := 1
		sol := num
		for ans == 1 {
			sum := 0
			for i := 0; i < len(str); i++ {
				sum += int(str[i]) - '0'
			}
			for i := sum; i >= 1; i-- {
				if sum%i == 0 && num%int64(i) == 0 {
					ans = i
					sol = num
					break
				}
			}
			num++
			str = fmt.Sprintf("%v", num)

		}

		fmt.Println(sol)

	}
}

func NewCF1498A(r *bufio.Reader) *CF1498A {
	return &CF1498A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1498A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int64
)

func sumDigits(n int64) int64 {
	s := int64(0)
	for ; n > 0; n /= 10 {
		s += n % 10
	}
	return s
}

func gcd(a, b int64) int64 {
	if b > a {
		return gcd(b, a)
	}
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

func gcdSum(a int64) int64 {
	return gcd(a, sumDigits(a))
}

func solve(n int64) int64 {
	for ; ; n++ {
		if gcdSum(n) > 1 {
			return n
		}
	}
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		fmt.Println(solve(n))
	}
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		var n uint64
		s, _ := reader.ReadBytes('\n')
		readUint64(s, 0, &n)
		res := solve(int64(n))
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int64) int64 {
	for {
		g := gcd(n, digitSum(n))
		if g > 1 {
			return n
		}
		n++
	}
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func digitSum(num int64) int64 {
	var res int64

	for num > 0 {
		res += num % 10
		num /= 10
	}

	return res
}

package main

import "fmt"

func gcd(a, b int64) int64 {
	if b == 0 {
		return a
	} else {
		return gcd(b, a%b)
	}
}
func bitSum(n int64) int64 {
	var ret int64 = 0
	for ; n > 0; n /= 10 {
		ret += n % 10
	}
	return ret
}
func main() {
	var t int64
	var c byte
	fmt.Scanf("%d%c%c", &t, &c, &c)
	var n int64
	for ; t > 0; t-- {
		fmt.Scanf("%d%c%c", &n, &c, &c)
		for ; gcd(n, bitSum(n)) == 1; n++ {
		}
		fmt.Printf("%d\n", n)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n int64
	fmt.Scan(&t)
	for t != 0 {
		t--
		fmt.Scan(&n)
		for gcd(n, calSum(n)) == 1 {
			n++
		}
		fmt.Println(n)
	}
}

func calSum(n int64) int64 {
	var sum int64
	for n != 0 {
		sum += n % 10
		n /= 10
	}
	return sum
}

func gcd(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

package main

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

const N int = 1e5 + 5
var stdIn = bufio.NewReader(os.Stdin)
var stdOut = bufio.NewWriter(os.Stdout)
func gcd(a, b int64) int64{
	if b == 0{
		return a
	} else {
		return gcd(b, a % b)
	}
}
func sum(x int64) int64{
	var s int64
	for x > 0{
		s += x % 10
		x /= 10
	}
	return s
}
func cal(x int64) int64{

	for gcd(x, sum(x)) == 1{
		x ++
	}
	return x
}
func main() {
	defer stdOut.Flush()
	n := 0
	fmt.Fscan(stdIn, &n)
	for ; n > 0; n --{
		var x int64
		fmt.Fscan(stdIn, &x)
		ans := cal(x)
		fmt.Fprintln(stdOut, ans)
	}

}

package main

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

func main() {
	scan := bufio.NewScanner(os.Stdin)
	scan.Scan()
	l, _ := strconv.Atoi(scan.Text())
	for i := 0; i < l; i++ {
		scan.Scan()
		num, _ := strconv.ParseInt(scan.Text(), 10, 64)
		for gcd(num, sum(num)) <= 1 {
			num++
		}
		fmt.Printf("%d\n", num)
	}
}

func sum(n int64) int64 {
	if n < 10 {
		return n
	}
	return n%10 + sum(n/10)
}

func gcd(a int64, b int64) int64 {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)

	var t, n int64
	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		for ; gcd(n, sum(n)) == 1; n++ {
		}
		fmt.Println(n)
	}
}

func gcd(a, b int64) int64 {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}

func sum(n int64) int64 {
	var s int64
	for n > 0 {
		s += n % 10
		n /= 10
	}
	return s
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		for j:=0;j<=n;j++{
			if gcd(n,summer(n)) > 1 {
				fmt.Fprintln(out,n)
				break
			}
			n++
		}
	}
}
func gcd(x,y int) int {
	if y>=x {
		x,y=y,x
	}
	for j:=0;j<x;j++{
		if x%y == 0 {
			return y
		}
		x,y=y,x%y
	}
	return 1
}
func summer(x int) int {
	x_str:= strconv.Itoa(x)
	sum:=0
	for j:=0;j<len(x_str);j++{
		x_int,_:= strconv.Atoi(string(x_str[j]))
		sum+=x_int
	}
	return sum
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		for j:=0;j<=n*n;j++{
			if gcd(n,summer(n)) > 1 {
				fmt.Fprintln(out,n)
				break
			}
			n++
		}
	}
}
func gcd(x,y int) int {
	if y>=x {
		x,y=y,x
	}
	for j:=0;j<10;j++{
		if x%y == 0 {
			return y
		}
		x,y=y,x%y
	}
	return 1
}
func summer(x int) int {
	x_str:= strconv.Itoa(x)
	sum:=0
	for j:=0;j<len(x_str);j++{
		x_int,_:= strconv.Atoi(string(x_str[j]))
		sum+=x_int
	}
	return sum
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n string
		fmt.Fscan(in,&n)
		sum:=0
		for j:=0;j<len(n);j++{
			x,_ := strconv.Atoi(string(n[j]))
			sum+=x
		}
		n_int,_:= strconv.Atoi(string(n))
		for j:=0;j<=n_int;j++{
			if gcd(n_int,summer(n_int)) > 1 {
				fmt.Fprintln(out,n_int)
				break
			}
			n_int++
		}
	}
}
func gcd(x,y int) int {
	if y>=x {
		x,y=y,x
	}
	for j:=0;j<x;j++{
		if x%y == 0 {
			return y
		}
		x,y=y,x%y
	}
	return 1
}
func summer(x int) int {
	x_str:= strconv.Itoa(x)
	sum:=0
	for j:=0;j<len(x_str);j++{
		x_int,_:= strconv.Atoi(string(x_str[j]))
		sum+=x_int
	}
	return sum
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n string
		fmt.Fscan(in,&n)
		sum:=0
		for j:=0;j<len(n);j++{
			x,_ := strconv.Atoi(string(n[j]))
			sum+=x
		}
		n_int,_:= strconv.Atoi(string(n))
		for j:=0;j<=n_int;j++{
			if summer(gcd(n_int,sum)) > 1 {
				fmt.Fprintln(out,n_int)
				break
			}
			n_int++
			sum++
		}
	}
}
func gcd(x,y int) int {
	if y>=x {
		x,y=y,x
	}
	for j:=0;j<x;j++{
		if x%y == 0 {
			return y
		}
		x,y=y,x%y
	}
	return 1
}
func summer(x int) int {
	x_str:= strconv.Itoa(x)
	sum:=0
	for j:=0;j<len(x_str);j++{
		x_int,_:= strconv.Atoi(string(x_str[j]))
		sum+=x_int
	}
	return sum
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n string
		fmt.Fscan(in,&n)
		sum:=0
		for j:=0;j<len(n);j++{
			x,_ := strconv.Atoi(string(n[j]))
			sum+=x
		}
		n_int,_:= strconv.Atoi(string(n))
		for j:=0;j<=n_int;j++{
			if gcd(n_int,sum) > 1 {
				fmt.Fprintln(out,n_int)
				break
			}
			n_int++
			sum++
		}
	}
}
func gcd(x,y int) int {
	if y>=x {
		x,y=y,x
	}
	for j:=0;j<x;j++{
		if x%y == 0 {
			return y
		}
		x,y=y,x%y
	}
	return 1
}

package main
import(
    "fmt"
    "bufio"
    "os"
    "strconv"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n string
		fmt.Fscan(in,&n)
		sum:=0
		for j:=0;j<len(n);j++{
			x,_ := strconv.Atoi(string(n[j]))
			sum+=x
		}
		n_int,_:= strconv.Atoi(string(n))
		for j:=0;j<n_int;j++{
			if gcd(n_int,sum) > 1 {
				fmt.Fprintln(out,n_int)
				break
			}
			n_int++
			sum++
		}
	}
}
func gcd(x,y int) int {
	if y>=x {
		x,y=y,x
	}
	for j:=0;j<x;j++{
		if x%y == 0 {
			return y
		}
		x,y=y,x%y
	}
	return 1
}

package main

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

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

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		for {
			var sum uint32
			for i := n; i > 0; i /= 10 {
				sum += i % 10
			}
			if specialGcd(n, sum) {
				writer.WriteString(fmt.Sprintln(n))
				break
			}
			n++
		}
	}
	writer.Flush()
}

func specialGcd(a, b uint32) bool {
	for i := uint32(2); i <= b; i++ {
		if a%i == 0 && b%i == 0 {
			return true
		}
	}
	return false
}

func parseInt(str string) uint32 {
	var res uint32
	size := len(str)
	for i := 0; i < size; i++ {
		digit := uint32(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

package main

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

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

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		for ; specialGcd(n, sumDigits(n)) == 1; n++ {
		}
		writer.WriteString(fmt.Sprintln(n))
	}
	writer.Flush()
}

func specialGcd(a, b int64) int64 {
	for i := int64(154); i > 0; i-- {
		if a%i == 0 && b%i == 0 {
			return i
		}
	}
	return 1
}

func sumDigits(a int64) int64 {
	var res int64
	for ; a > 0; a /= 10 {
		res += a % 10
	}
	return res
}

func parseInt(str string) int64 {
	var res int64
	size := len(str)
	for i := 0; i < size; i++ {
		digit := int64(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

package main

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

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

	for test := parseInt(getString(reader)); test > 0; test-- {
		n := parseInt(getString(reader))
		var res int64
		if n%3 == 0 {
			res = n
		} else if (n+1)%3 == 0 {
			res = n + 1
		} else {
			res = n + 2
		}
		writer.WriteString(fmt.Sprintln(res))
	}
	writer.Flush()
}

func parseInt(str string) int64 {
	var res int64
	size := len(str)
	for i := 0; i < size; i++ {
		digit := int64(str[i] - '0')
		for j := (i + 1); j < size; j++ {
			digit = digit<<3 + digit<<1
		}
		res += digit
	}
	return res
}

func getString(in *bufio.Reader) string {
	res, _ := in.ReadString('\n')
	res = strings.ReplaceAll(res, "\n", "")
	res = strings.ReplaceAll(res, "\r", "")
	res = strings.TrimSpace(res)
	return res
}

package main

import (
	"fmt"
//	"os"
)

func gcd(a, b int64) int64 {
	if b == 0 {
		return a
	} else {
		return gcd(b, a%b)
	}
}

func main() {
//	f, _ := os.OpenFile("in", os.O_RDONLY,0666)
//	os.Stdin = f

	var t int
	fmt.Scanf("%d", &t)
	var n int64
	for t != 0 {
		fmt.Scanf("%d", &n)
		for {
			var sum int64
			tn := n
			for {
				if tn == 0 {
					break
				}
				sum += tn % 10
				tn /= 10
			}
			if gcd(n, sum) > 1 {
				break
			}
			n++
		}
		fmt.Printf("%d\n", n)
		t--
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = (n * 10) + int(v-'0')
	}
	return
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (a Pairs) Len() int           { return len(a) }
func (a Pairs) Less(i, j int) bool { return a[i].a < a[j].a }
func (a Pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	defer writer.Flush()

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

	for t > 0 {
		var n int64
		scanf("%d\n", &n)
		for {
			if n%3 == 0 {
				break
			}
			n++
		}
		printf("%d\n", n)
		t--
	}

}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
var scanner *bufio.Scanner = bufio.NewScanner(reader)

func scanf(f string, args ...interface{})  { fmt.Fscanf(reader, f, args...) }
func printf(f string, args ...interface{}) { fmt.Fprintf(writer, f, args...) }

func toInt(buf []byte) (n int) {
	for _, v := range buf {
		n = (n * 10) + int(v-'0')
	}
	return
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (a Pairs) Len() int           { return len(a) }
func (a Pairs) Less(i, j int) bool { return a[i].a < a[j].a }
func (a Pairs) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	defer writer.Flush()

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

	for t > 0 {
		var n int64
		scanf("%d\n", &n)
		for {
			if n%3 == 0 {
				break
			}
			n++
		}
		fmt.Println(n)
		t--
	}

}

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		for ; ; n++ {
			s := calSum(n)
			check := false
			for j := 2; j <= s; j++ {
				if n%j == 0 && s%j == 0 {
					check = true
				}
			}
			if check {
				fmt.Println(n)
				break
			}
		}
	}
}

func calSum(n int) int {
	s := 0
	for n > 0 {
		s += n % 10
		n /= 10
	}
	return s
}

package main

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

var in = bufio.NewReader(os.Stdin)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Scan(&n)
		tp := n
		for ; ; tp++ {
			s := calSum(tp)
			j := s
			for j = s; j >= 1; j-- {
				if tp%j == 0 && s%j == 0 {
					break
				}
			}
			if j > 1 {
				fmt.Println(tp)
				break
			}
		}
	}
}

func calSum(n int) int {
	s := 0
	for n > 0 {
		s += n % 10
		n /= 10
	}
	return s
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func ScanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func ScanInt() int {
	return int(ScanInt64())
}

func ScanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()

		for ; gcd(n, sumDigs(n)) == 1; n++ {
		}

		fmt.Println(n)
	}
}

func gcd(x, y int) int {
	if x == 0 {
		return y
	}

	if y == 0 {
		return x
	}

	return gcd(y, x%y)
}

func sumDigs(x int) int {
	sum := 0

	for x > 0 {
		sum += x % 10
		x /= 10
	}

	return sum
}

package main

import "fmt"
func solve(){
    var n int64;
    var i int64;
    fmt.Scanln(&n);
    for i=n;i<n+3;i++{
        if(i%3==0){
            fmt.Println(i);
            return;
        }
    }
}
func main() {
   var t int;
   fmt.Scanln(&t);
   for t>0{
       solve();
       t=t-1;
   }
}


/*
URL:
https://codeforces.com/problemset/problem/1266/C
*/

package main

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	r, c int
)

func main() {
	r, c = ReadInt2()
	A := make([][]int, r)
	for i := 0; i < r; i++ {
		A[i] = make([]int, c)
	}

	if r == 1 && c == 1 {
		fmt.Println(0)
		return
	}

	if r == 1 {
		for i := 0; i < c; i++ {
			A[0][i] = i + 2
		}
		fmt.Println(PrintIntsLine(A[0]...))
		return
	}

	if c == 1 {
		for i := 0; i < r; i++ {
			A[i][0] = i + 2
		}

		for i := 0; i < r; i++ {
			fmt.Println(A[i][0])
		}
		return
	}

	for i := 0; i < r; i++ {
		for j := 0; j < c; j++ {
			A[i][j] = (i + 1) * (r + j + 1)
			PrintfDebug("i: %d, j: %d, A[i][j]: %d\n", i, j, A[i][j])
		}
	}
	for i := 0; i < r; i++ {
		fmt.Println(PrintIntsLine(A[i]...))
	}
	return
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

import "fmt"

func main() {
	var r, c int
	var a [505][505]int
	fmt.Scanf("%d %d", &r, &c)
	if r == 1 && c == 1 {
		fmt.Println(0)
	} else if c == 1 {
		for i := 1; i <= r; i++ {
			a[i-1][0] = i + 1
		}

		for i := 0; i < r; i++ {
			for j := 0; j < c; j++ {
				fmt.Printf("%d ", a[i][j])
			}
			fmt.Println()
		}
	} else {
		for i := 1; i <= r; i++ {
			for j := r + 1; j <= r+c; j++ {
				a[i-1][j-r-1] = i * j
			}
		}

		for i := 0; i < r; i++ {
			for j := 0; j < c; j++ {
				fmt.Printf("%d ", a[i][j])
			}
			fmt.Println()
		}

	}

}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1266C(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m int
	Fscan(_r, &n, &m)
	if n == 1 && m == 1 {
		Fprint(out, 0)
		return
	}
	if m == 1 {
		for i := 1; i <= n; i++ {
			Fprintln(out, i+1)
		}
		return
	}
	for i := 1; i <= n; i++ {
		for j := n + 1; j <= n+m; j++ {
			Fprint(out, i*j, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

const (
	mod = uint64(1e9 + 7)
)

type slot struct {
	from uint64
	num  uint64
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	r, c := 0, 0
	fmt.Fscanf(in, "%d %d\n", &r, &c)
	if r == 1 && c == 1 {
		fmt.Fprintf(out, "0\n")
		return
	}
	f := make([][]int, r)
	for i := 0; i < r; i++ {
		f[i] = make([]int, c)
	}
	if c == 1 {
		for i := 0; i < r; i++ {
			f[i][0] = i + 2
		}
	} else {
		for i := 0; i < c; i++ {
			f[0][i] = i + 2
		}
		for i := 1; i < r; i++ {
			for j := 0; j < c; j++ {
				f[i][j] = f[0][j] * (1 + c + i)
			}
		}
	}
	for i := 0; i < r; i++ {
		for j := 0; j < c; j++ {
			fmt.Fprintf(out, "%d", f[i][j])
			if j != c-1 {
				fmt.Fprintf(out, " ")
			}
		}
		fmt.Fprintf(out, "\n")
	}
	return
}

func test(a []int, b [][]int) []int {
	fmt.Println("debug")
	fmt.Println(a)
	for i := 0; i < len(b); i++ {
		a[b[i][0]-1], a[b[i][1]-1] = a[b[i][1]-1], a[b[i][0]-1]
		fmt.Println(a, b[i])
	}
	fmt.Println("debug end")

	return a
}

// find num of elem >= k
func find(a []int, k int) int {
	if len(a) == 0 {
		return 0
	}
	n := len(a)
	if a[0] > k {
		return n
	}
	if a[n-1] < k {
		return 0
	}
	l := 0
	r := len(a) - 1
	for l < r {
		mid := (l + r) / 2
		if a[mid] < k {
			l = mid + 1
		} else {
			r = mid
		}
	}
	return n - l
}

func gcd(a, b uint64) uint64 {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

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

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

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func ok(a, b int) bool {
	if min(abs(a-b), abs(a+b)) <= min(abs(a), abs(b)) && max(abs(a+b), abs(a-b)) >= max(abs(a), abs(b)) {
		return true
	}
	return false
}

package main

import "fmt"

func main() {
	var r, c int
	var a [500][500]int
	fmt.Scanf("%d %d", &r, &c)
	if r == 1 && c == 1 {
		fmt.Println(0)
	} else {
		for i := 1; i <= r; i++ {
			for j := r + 1; j <= r+c; j++ {
				a[i-1][j-r-1] = i * j
			}
		}

		for i := 0; i < r; i++ {
			for j := 0; j < c; j++ {
				fmt.Printf("%d ", a[i][j])
			}
			fmt.Println()
		}

	}

}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1266C(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m int
	Fscan(_r, &n, &m)
	if n == 1 && m == 1 {
		Fprint(out, 0)
		return
	}
	for i := 1; i <= n; i++ {
		for j := n + 1; j <= n+m; j++ {
			Fprint(out, i*j, " ")
		}
		Fprintln(out)
	}
}

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

package main

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

const (
	mod = uint64(1e9 + 7)
)

type slot struct {
	from uint64
	num  uint64
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	r, c := 0, 0
	fmt.Fscanf(in, "%d %d\n", &r, &c)
	if r == 1 && c == 1 {
		fmt.Fprintf(out, "0\n")
		return
	}
	f := make([][]int, r)
	for i := 0; i < r; i++ {
		f[i] = make([]int, c)
	}
	if c == 1 {
		for i := 0; i < r; i++ {
			f[i][0] = i + 2
		}
	} else {
		for i := 0; i < c; i++ {
			f[0][i] = i + 2
		}
		for i := 1; i < r; i++ {
			for j := 0; j < c; j++ {
				f[i][j] = f[0][j] * (1 + r + i)
			}
		}
	}
	for i := 0; i < r; i++ {
		for j := 0; j < c; j++ {
			fmt.Fprintf(out, "%d", f[i][j])
			if j != c-1 {
				fmt.Fprintf(out, " ")
			}
		}
		fmt.Fprintf(out, "\n")
	}
	return
}

func test(a []int, b [][]int) []int {
	fmt.Println("debug")
	fmt.Println(a)
	for i := 0; i < len(b); i++ {
		a[b[i][0]-1], a[b[i][1]-1] = a[b[i][1]-1], a[b[i][0]-1]
		fmt.Println(a, b[i])
	}
	fmt.Println("debug end")

	return a
}

// find num of elem >= k
func find(a []int, k int) int {
	if len(a) == 0 {
		return 0
	}
	n := len(a)
	if a[0] > k {
		return n
	}
	if a[n-1] < k {
		return 0
	}
	l := 0
	r := len(a) - 1
	for l < r {
		mid := (l + r) / 2
		if a[mid] < k {
			l = mid + 1
		} else {
			r = mid
		}
	}
	return n - l
}

func gcd(a, b uint64) uint64 {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

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

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

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func ok(a, b int) bool {
	if min(abs(a-b), abs(a+b)) <= min(abs(a), abs(b)) && max(abs(a+b), abs(a-b)) >= max(abs(a), abs(b)) {
		return true
	}
	return false
}


package main

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

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

	type pair struct {
		x, y int
	}
	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n, m int
		Fscan(in, &n, &m)
		pairs := make([]pair, n)

		var sum int64
		for i := range pairs {
			Fscan(in, &pairs[i].x)
			sum += int64(pairs[i].x)
		}
		for i := range pairs {
			Fscan(in, &pairs[i].y)
		}
		if sum < int64(m) {
			Fprintln(out, -1)
			continue
		}

		var knapsack1, knapsack2 []int
		for i := 0; i < n; i++ {
			if pairs[i].y == 1 {
				knapsack1 = append(knapsack1, pairs[i].x)
			} else {
				knapsack2 = append(knapsack2, pairs[i].x)
			}
		}

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

		sum = 0
		for _, val := range knapsack1 {
			sum += int64(val)
		}

		ans := int(1e9)
		for i, j := len(knapsack1), 0; i >= 0; i-- {
			for j < len(knapsack2) && sum < int64(m) {
				sum += int64(knapsack2[j])
				j++
			}
			if sum >= int64(m) {
				ans = min(ans, 2 * j + i)
			}
			if i > 0 {
				sum -= int64(knapsack1[i-1])
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })
	for i := 1; i < len(a2); i++ {
		a2[i].w += a2[i-1].w
	}

	var (
		i   int64 = -1
		sum int64 = 0
		res int64 = math.MaxInt64
	)
	bs := func(x int64) int64 {
		if x <= 0 {
			return -1
		}
		l, r := -1, len(a2)
		for r-l > 1 {
			m := l + (r-l)/2
			if a2[m].w >= x {
				r = m
			} else {
				l = m
			}
		}
		return int64(r)
	}
	for ; i < int64(len(a1)); i++ {
		if i > -1 {
			sum += a1[i].w
		}
		j := bs(m - sum)
		if j < int64(len(a2)) {
			res = IntMin(res, i+1+2*(j+1))
		}
	}
	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })

	var (
		res      int64 = math.MaxInt64
		p1, sum1 int64 = int64(len(a1)) - 1, 0
		p2, sum2 int64 = -1, 0
	)
	update := func() bool {
		if sum1+sum2 >= m {
			res = IntMin(res, p1+1+2*(p2+1))
			return true
		}
		return false
	}
	for i := range a1 {
		sum1 += a1[i].w
		if sum1 >= m {
			res = IntMin(res, int64(i)+1)
		}
	}

	update()
	for p2 = 0; p2 < int64(len(a2)); p2++ {
		sum2 += a2[p2].w
		for ; update() && p1 >= 0; p1-- {
			sum1 -= a1[p1].w
		}
	}

	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

type heap []app

func (h heap) Len() int           { return len(h) }
func (h heap) Less(i, j int) bool { return h[i].p > h[j].p }
func (h heap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *heap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(app))
}

func (h *heap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })

	var (
		res      int64 = math.MaxInt64
		p1, sum1 int64 = int64(len(a1)) - 1, 0
		p2, sum2 int64 = -1, 0
	)
	update := func() bool {
		if sum1+sum2 >= m {
			res = IntMin(res, p1+1+2*(p2+1))
			return true
		}
		return false
	}
	for i := range a1 {
		sum1 += a1[i].w
		if sum1 >= m {
			res = IntMin(res, int64(i)+1)
		}
	}

	update()
	for p2 = 0; p2 < int64(len(a2)); p2++ {
		sum2 += a2[p2].w
		for ; update() && p1 >= 0; p1-- {
			sum1 -= a1[p1].w
		}
	}

	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

var (
	in *bufio.Scanner
	out *bufio.Writer

	n, m int
	a, b [200000]int
	l [2]int
	ab [2][200000]int
	dp [400001]q
	res int
)

type q struct {
	l1, l2, s int
}

func main() {
	initBuffer()
	var t int = 1
	t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {
	l[0] = 0
	l[1] = 0
	res = -1
}

func input() {
	n = getInt()
	m = getInt()
	for i := 0; i < n; i++ {
		a[i] = getInt()
	}
	for i := 0; i < n; i++ {
		b[i] = getInt() - 1
		ab[b[i]][l[b[i]]] = a[i]
		l[b[i]]++
	}
}

func count() {
	for k := 0; k < 2; k++ {
		sort.Slice(ab[k][:l[k]], func(i, j int) bool {
			return ab[k][i] > ab[k][j]
		})
	}
	if l[0] > 0 {
		dp[1] = q{1, 0, ab[0][0]}
		if dp[1].s >= m {
			res = 1
			return
		}
	} else {
		dp[1] = q{0, 0, 0}
	}
	for i := 2; i <= l[0] + 2 * l[1]; i++ {
		dp[i] = q{0, 0, 0}
		if dp[i-2].l2 < l[1] {
			dp[i] = q{dp[i-2].l1, dp[i-2].l2 + 1, dp[i-2].s + ab[1][dp[i-2].l2]}
		}
		if dp[i-1].l1 < l[0] && (dp[i-1].s + ab[0][dp[i-1].l1] > dp[i].s) {
			dp[i] = q{dp[i-1].l1 + 1, dp[i-1].l2, dp[i-1].s + ab[0][dp[i-1].l1]}
		}
		if dp[i].s >= m {
			res = i
			return
		}
	}
}

func printIt() {
	fmt.Println(ab[0][:l[0]])
	fmt.Println(ab[1][:l[1]])
}

func output() {
	out.Write(intToBytes(res))
	out.WriteByte(NL)
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getString() string {
	in.Scan()
	return in.Text()
}

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}

package main

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

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

	type pair struct {
		x, y int
	}
	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n, m int
		Fscan(in, &n, &m)
		pairs := make([]pair, n)

		var sum int64
		for i := range pairs {
			Fscan(in, &pairs[i].x)
			sum += int64(pairs[i].x)
		}
		for i := range pairs {
			Fscan(in, &pairs[i].y)
		}

		if sum < int64(m) {
			Fprintln(out, -1)
			continue
		}

		var k1, k2 []int
		for i := 0; i < n; i++ {
			if pairs[i].y == 1 {
				k1 = append(k1, pairs[i].x)
			} else {
				k2 = append(k2, pairs[i].x)
			}
		}
		sort.Slice(k1, func(i, j int) bool {
			return k1[i] > k1[j]
		})
		sort.Slice(k2, func(i, j int) bool {
			return k2[i] > k2[j]
		})

		sum = 0
		for i := 0; i < len(k2); i++ {
			sum += int64(k2[i])
		}
		j, ans := 0, int(1e9)
		for i := len(k2); i >= 0; i-- {
			for j < len(k1) && sum < int64(m) {
				sum += int64(k1[j])
				j++
			}
			if sum >= int64(m) {
				ans = min(ans, 2 * i + j)
			}
			if i > 0 {
				sum -= int64(k2[i-1])
			}
		}
		Fprintln(out, ans)
	}
}

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

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

package main

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

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64s(n)
		b := sint64s(n)
		printf("%d\n", sol(n, m, a, b))
	}
}

func sol(n, m int64, a, b []int64) int64 {
	twos := []int64{}
	ones := []int64{}
	for i := n - 1; i >= 0; i-- {
		switch b[i] {
		case 2:
			twos = append(twos, a[i])
		case 1:
			ones = append(ones, a[i])
		}
	}
	sort.Slice(twos, func(il, ir int) bool {
		return twos[il] > twos[ir]
	})
	sort.Slice(ones, func(il, ir int) bool {
		return ones[il] > ones[ir]
	})
	p1 := 0
	p2 := 0
	ret := int64(0)
	for p1 < len(ones) && p2 < len(twos) {
		if ones[p1] >= m {
			ret++
			return ret
		}
		if twos[p2] >= m {
			ret += 2
			return ret
		}
		if p1 == len(ones)-1 { // the last "one" element doesn't finish the job
			m -= twos[p2]
			ret += 2
			p2++
			continue
		}
		sum1 := ones[p1] + ones[p1+1]
		sum2 := twos[p2]
		if sum1 >= sum2 {
			m -= ones[p1]
			ret++
			p1++
			continue
		}
		m -= twos[p2]
		ret += 2
		p2++
		continue
	}
	for m > 0 && p1 < len(ones) {
		m -= ones[p1]
		ret++
		p1++
	}
	for m > 0 && p2 < len(twos) {
		m -= twos[p2]
		ret += 2
		p2++
	}
	if m > 0 {
		return -1
	}
	return ret
}

package main

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

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		a := sint64s(n)
		b := sint64s(n)
		printf("%d\n", sol(n, m, a, b))
	}
}

func sol(n, m int64, a, b []int64) int64 {
	twos := []int64{}
	ones := []int64{}
	for i := n - 1; i >= 0; i-- {
		switch b[i] {
		case 2:
			twos = append(twos, a[i])
		case 1:
			ones = append(ones, a[i])
		}
	}
	sort.Slice(twos, func(il, ir int) bool {
		return twos[il] > twos[ir]
	})
	sort.Slice(ones, func(il, ir int) bool {
		return ones[il] > ones[ir]
	})
	p1 := 0
	p2 := 0
	ret := int64(0)
	for p1 < len(ones) && p2 < len(twos) {
		if ones[p1] >= m {
			ret++
			return ret
		}
		if twos[p2] >= m {
			ret += 2
			return ret
		}
		if p1 == len(ones)-1 { // the last "one" element doesn't finish the job
			m -= twos[p2]
			ret += 2
			p2++
			continue
		}
		sum1 := ones[p1] + ones[p1+1]
		sum2 := twos[p2]
		if sum1 > sum2 { // >= is also fine?
			m -= ones[p1]
			ret++
			p1++
			continue
		}
		m -= twos[p2]
		ret += 2
		p2++
		continue
	}
	for m > 0 && p1 < len(ones) {
		m -= ones[p1]
		ret++
		p1++
	}
	for m > 0 && p2 < len(twos) {
		m -= twos[p2]
		ret += 2
		p2++
	}
	if m > 0 {
		return -1
	}
	return ret
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1475D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, tp int
	var low int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &low)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}
		b := [3][]int64{}
		for _, v := range a {
			Fscan(in, &tp)
			b[tp] = append(b[tp], v)
		}
		for _, a := range b {
			sort.Slice(a, func(i, j int) bool { return a[i] > a[j] })
		}
		n2 := len(b[2])
		s2 := make([]int64, n2+1)
		for i, v := range b[2] {
			s2[i+1] = s2[i] + v
		}

		ans := int(1e9)
		b[1] = append(b[1], 0)
		for i, v := range b[1] {
			if j := sort.Search(n2+1, func(j int) bool { return s2[j] >= low }); j <= n2 && i+2*j < ans {
				ans = i + 2*j
			}
			low -= v
		}

		if ans == 1e9 {
			ans = -1
		}
		Fprintln(out, ans)
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func(Case int) {
		var n, m, w int64
		Fscan(in, &n, &m)
		a := make([]int64, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		var a1, a2 []int64
		for _, v := range a {
			Fscan(in, &w)
			if w == 1 {
				a1 = append(a1, v)
			} else {
				a2 = append(a2, v)
			}
		}
		sort.Slice(a1, func(i, j int) bool { return a1[i] > a1[j] })
		sort.Slice(a2, func(i, j int) bool { return a2[i] > a2[j] })

		sum2 := make([]int64, len(a2)+1)
		for i, v := range a2 {
			sum2[i+1] = sum2[i] + v
		}

		ans := int(1e9)
		j := sort.Search(len(sum2), func(i int) bool { return sum2[i] >= m })
		if j < len(sum2) {
			ans = min(ans, 2*j)
		}

		for i, v := range a1 {
			m -= v
			if m <= 0 {
				ans = min(ans, i+1)
				break
			}
			j := sort.Search(len(sum2), func(i int) bool { return sum2[i] >= m })
			if j < len(sum2) {
				ans = min(ans, i+1+2*j)
			}
		}

		if ans == 1e9 {
			ans = -1
		}
		Fprintln(out, ans)
	}

	var t int
	Fscan(in, &t)
	for Case := 1; Case <= t; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

func main() { run(os.Stdin, os.Stdout) }
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		n, m := readTwoNums(reader)
		A := readNNums(reader, n)
		B := readNNums(reader, n)
		res := solve(n, m, A, B)
		buf.WriteString(fmt.Sprintf("%d\n", res))
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int, m int, A []int, B []int) int {
	M := int64(m)

	var all int64

	for i := 0; i < n; i++ {
		all += int64(A[i])
	}

	if all < M {
		return -1
	}

	que := make([][]int, 2)
	for i := 0; i < 2; i++ {
		que[i] = make([]int, 0, n)
	}

	for i := 0; i < n; i++ {
		b := B[i] - 1
		que[b] = append(que[b], A[i])
	}

	for i := 0; i < 2; i++ {
		sort.Ints(que[i])
		reverse(que[i])
	}
	preSum := make([]int64, len(que[1])+1)
	for i := 0; i < len(que[1]); i++ {
		preSum[i+1] = preSum[i] + int64(que[1][i])
	}
	// if use all type 2
	j := len(preSum) - 1
	var best = 2 * n
	for j >= 0 && preSum[j] >= M {
		best = j * 2
		if j == 0 || preSum[j-1] < M {
			break
		}
		j--
	}
	var sum int64
	for i := 1; i <= len(que[0]); i++ {
		sum += int64(que[0][i-1])
		if sum+preSum[j] < M {
			// not a valid solution
			continue
		}
		tmp := M - sum
		// need tmp
		for j > 0 && preSum[j-1] >= tmp {
			j--
		}
		if best > i+j*2 {
			best = i + j*2
		}
	}

	return best
}

func reverse(arr []int) {
	for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
		arr[i], arr[j] = arr[j], arr[i]
	}
}

func search(n int, fn func(int) bool) int {
	left, right := 0, n
	for left < right {
		mid := (left + right) / 2
		if fn(mid) {
			right = mid
		} else {
			left = mid + 1
		}
	}
	return right
}

package main

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

type app struct {
	w, p int64
}

type heap []app

func (h heap) Len() int           { return len(h) }
func (h heap) Less(i, j int) bool { return h[i].p > h[j].p }
func (h heap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *heap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(app))
}

func (h *heap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func solve(apps []app, m int64) {
	a1, a2 := []app{}, []app{}
	for i := range apps {
		if apps[i].p == 1 {
			a1 = append(a1, apps[i])
		} else {
			a2 = append(a2, apps[i])
		}
	}
	sort.Slice(a1, func(i, j int) bool { return a1[i].w > a1[j].w })
	sort.Slice(a2, func(i, j int) bool { return a2[i].w > a2[j].w })

	var (
		res      int64 = math.MaxInt64
		p1, sum1 int64 = int64(len(a1)) - 1, 0
		p2, sum2 int64 = -1, 0
	)
	update := func() bool {
		if sum1+sum2 >= m {
			res = IntMin(res, p1+1+2*(p2+1))
			return true
		}
		return false
	}
	for i := range a1 {
		sum1 += a1[i].w
		if sum1 >= m {
			res = IntMin(res, int64(i)+1)
		}
	}

	update()
	for p2 = 0; p2 < int64(len(a2)); p2++ {
		sum2 += a2[p2].w
		for ; p1 >= 0 && update(); p1-- {
			sum1 -= a1[p1].w
		}
	}

	if res == math.MaxInt64 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for m > 0 && j < len(apps) && i < last {
		if apps[i].w+apps[i+1].w >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	if j == len(apps) { // i < last and j == len(apps)
		for ; m > 0 && i <= last; i++ {
			m -= apps[i].w
			res++
		}
	} else { // i == last and j < len(apps)
		w := apps[i].w
		for ; j < len(apps); j++ {
			if m <= w {
				printf("%d\n", res+1)
				return
			}
			m -= apps[j].w
			res += 2
			if m <= 0 {
				printf("%d\n", res)
				return
			}
		}
		if m <= w {
			printf("%d\n", res+1)
			return
		}
	}

	if m > 0 {
		res = -1
	}
	printf("%d\n", res)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for i+1 <= last && j < len(apps) {
		if m <= 0 {
			break
		}

		if apps[i].w+apps[i+1].w >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	for ; j < len(apps) && m > 0; j++ {
		if m <= apps[i].w {
			printf("%d\n", res+1)
			return
		}
		m -= apps[j].w
		res += 2
	}

	if m <= 0 {
		printf("%d\n", res)
	} else if m <= apps[i].w {
		printf("%d\n", res+1)
	} else {
		printf("-1\n")
	}
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for i+1 <= last && j < len(apps) {
		if m <= 0 {
			break
		}

		if apps[i].w+apps[i+1].w >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	if i+1 <= last && j < len(apps) {
		panic("can't be")
	}

	for ; m > 0 && j < len(apps); j++ {
		if m-apps[i].w <= 0 {
			m -= apps[i].w
			res++
			i++
			break
		}
		m -= apps[j].w
		res += 2
	}

	if m > 0 && i == last {
		m -= apps[i].w
		res++
	}

	ans := int64(-1)
	if m <= 0 {
		ans = res
	}
	printf("%d\n", ans)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

type app struct {
	w, p int64
}

func solve(apps []app, m int64) {
	sort.Slice(apps, func(i, j int) bool {
		if apps[i].p == apps[j].p {
			return apps[i].w > apps[j].w
		}
		return apps[i].p < apps[j].p
	})

	last := -1
	for i := 0; i < len(apps); i++ {
		if apps[i].p == 1 {
			last = i
		}
	}

	i, j := 0, last+1
	res := int64(0)
	for i <= last && j < len(apps) {
		if m <= 0 {
			break
		}

		var next int64
		if i+1 <= last {
			next = apps[i+1].w
		}

		if apps[i].w+next >= apps[j].w {
			m -= apps[i].w
			i++
			res++
		} else {
			m -= apps[j].w
			j++
			res += 2
		}
	}

	if m <= 0 {
		printf("%d\n", res)
		return
	}

	if i <= last && j < len(apps) {
		panic("can't be")
	}

	for k := i; k <= last; k++ {
		if m <= 0 {
			break
		}
		m -= apps[k].w
		res++
	}

	for k := j; k < len(apps); k++ {
		if m <= 0 {
			break
		}
		m -= apps[k].w
		res += 2
	}

	ans := int64(-1)
	if m <= 0 {
		ans = res
	}
	printf("%d\n", ans)
}

func main() {
	//RedirectInputToFile(reader, "input.txt")

	t := 1
	scan(&t)

	for T := 0; T < t; T++ {
		var n, m int64
		scan(&n)
		scan(&m)

		a := make([]app, n)
		for i := range a {
			scan(&a[i].w)
		}
		for i := range a {
			scan(&a[i].p)
		}

		solve(a, m)
	}

	writer.Flush()
}

// ------------------------------------------------------------------------------------------------

// IntMax returns maximum of received int numbers
func IntMax(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand < num {
			cand = num
		}
	}
	return cand
}

// IntMin returns minimum of received int numbers
func IntMin(nums ...int64) int64 {
	cand := nums[0]
	for _, num := range nums {
		if cand > num {
			cand = num
		}
	}
	return cand
}

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

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

func RedirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

package main

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

const (
	MOD int64 = 1e9+7
	NL byte = byte('\n')
	SP byte = byte(' ')
)

var (
	in *bufio.Scanner
	out *bufio.Writer

	n, m int
	a, b [200000]int
	l [2]int
	ab [2][200000]int
	dp [400001]q
	res int
)

type q struct {
	l1, l2, s int
}

func main() {
	initBuffer()
	var t int = 1
	t = getInt()
	for i := 0; i < t; i++ {
		solve()
	}
	// test()
	out.Flush()
}

func solve() {
	initialize()
	input()
	count()
	output()
}

func initialize() {
	l[0] = 0
	l[1] = 0
	res = -1
}

func input() {
	n = getInt()
	m = getInt()
	for i := 0; i < n; i++ {
		a[i] = getInt()
	}
	for i := 0; i < n; i++ {
		b[i] = getInt() - 1
		ab[b[i]][l[b[i]]] = a[i]
		l[b[i]]++
	}
}

func count() {
	for k := 0; k < 2; k++ {
		sort.Slice(ab[k][:l[k]], func(i, j int) bool {
			return ab[k][i] > ab[k][j]
		})
	}
	if l[0] > 0 {
		dp[1] = q{1, 0, ab[0][0]}
		if dp[1].s >= m {
			res = 1
			return
		}
	} else {
		dp[1] = q{0, 0, 0}
	}
	for i := 2; i <= l[0] + 2 * l[1]; i++ {
		dp[i] = q{0, 0, 0}
		if dp[i-2].l2 < l[1] {
			dp[i] = q{dp[i-2].l1, dp[i-2].l2 + 1, dp[i-2].s + ab[1][dp[i-2].l2]}
		}
		if dp[i-1].l1 < l[0] && (dp[i-1].s + ab[0][dp[i-1].l1] > dp[i].s) {
			dp[i] = q{dp[i-1].l1 + 1, dp[i-1].l2, dp[i-1].s + ab[0][dp[i-1].l1]}
		}
		if dp[i].s >= m {
			res = i
			return
		}
	}
	printIt()
}

func printIt() {
	fmt.Println(ab[0][:l[0]])
	fmt.Println(ab[1][:l[1]])
}

func output() {
	out.Write(intToBytes(res))
	out.WriteByte(NL)
}

func test() {
	fmt.Print()
}

func initBuffer() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer([]byte{}, 4 * 1024 * 1024)
	in.Split(scanWords)
	out = bufio.NewWriter(os.Stdout)
}

func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	i := bytes.IndexByte(data, '\n')
	j := bytes.IndexByte(data, ' ')
	ind := min(i, j)
	if ind < 0 {
		ind = max(i, j)
	}
	if ind >= 0 {
		return ind + 1, dropCR(data[0:ind]), nil
	}
	if atEOF {
		return len(data), dropCR(data), nil
	}
	return 0, nil, nil
}

func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[:len(data)-1]
	}
	return data
}

func getFloat() float64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res1 int64
	for i := 0; i < len(b); i++ {
		if b[i] == '.' {
			b = b[i+1:]
			break
		}
		res1 *= 10
		res1 += int64(b[i]) - 48
		if i == len(b) - 1 {
			b = b[:0]
		}
	}
	var res2 float64
	for i := len(b); i > 0; i-- {
		res2 += float64(b[i-1]) - 48
		res2 /= 10
	}
	if isNeg {
		res1 *= -1
		res2 *= -1
	}
	return float64(res1) + res2
}

func getInt() int {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getInt64() int64 {
	in.Scan()
	b := in.Bytes()
	var isNeg bool
	if b[0] == '-' {
		isNeg = true
		b = b[1:]
	}
	var res int64
	for i := 0; i < len(b); i++ {
		res *= 10
		res += int64(b[i]) - 48
	}
	if isNeg {
		res *= -1
	}
	return res
}

func getString() string {
	in.Scan()
	return in.Text()
}

func intToBytes(n int) []byte {
	var res [33]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 33
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 33 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func int64ToBytes(n int64) []byte {
	var res [65]byte
	var isNeg bool
	if n < 0 {
		isNeg = true
		n = -n
	}
	var l int = 65
	for n > 0 {
		l--
		res[l] = byte(n % 10 + 48)
		n /= 10
	}
	if l == 65 {
		l--
		res[l] = byte(48)
	}
	if isNeg {
		l--
		res[l] = byte(45)
	}
	return res[l:]
}

func bsLower(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsLower64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] < n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] >= n {
		return minN
	}
	return maxN
}

func bsUpper(arr []int, n int) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func bsUpper64(arr []int64, n int64) int {
	var minN, midN, maxN int
	maxN = len(arr)
	for maxN - 1 > minN {
		midN = (minN + maxN) / 2
		if arr[midN] <= n {
			minN = midN
		} else {
			maxN = midN
		}
	}
	if arr[minN] > n {
		return minN
	}
	return maxN
}

func max(n1, n2 int) int {
	if n1 > n2 {
		return n1
	}
	return n2
}

func max64(n1, n2 int64) int64 {
	if n1 > n2 {
		return n1
	}
	return n2
}

func min(n1, n2 int) int {
	if n1 < n2 {
		return n1
	}
	return n2
}

func min64(n1, n2 int64) int64 {
	if n1 < n2 {
		return n1
	}
	return n2
}

func yes(res, isUpperCase bool) bool {
	var arr [4]string = [4]string{"No", "Yes", "NO", "YES"}
	var ind int
	if res == true {
		ind = 1
	}
	if isUpperCase == true {
		ind += 2
	}
	out.WriteString(arr[ind])
	out.WriteByte(NL)
	return res
}