Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.

First line contains four integers n, m, k, s (1 ≤ n ≤ 2·105, 1 ≤ k ≤ m ≤ 2·105, 1 ≤ s ≤ 109) — number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≤ ai ≤ 106) — the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≤ bi ≤ 106) — the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≤ ti ≤ 2, 1 ≤ ci ≤ 106) — type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.

If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d — the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di — the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them.

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)
	var aNum, bNum int
	mid := -1

	for left < right {
		mid = (left + right - 1)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if aSum + bSum <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			if right == mid + 1 {
				break
			}
			right = mid + 1
		} else {
			left = mid + 1
		}
	}
	if left >= len(aConv) {
		return -1, nil
	}
	mid = (left + right - 1) / 2
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[mid]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[mid]})
	}

	//fmt.Printf("resp: %v\n", resp)
	return mid, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
			minBIdx = idx
			minBConv = bConv[idx][0]
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)-1
	var aNum, bNum int

	for left <= right {
		mid := (left + right)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) - 1 && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if int64(aSum) + int64(bSum) <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			right = mid-1
		} else {
			left = mid+1
		}
	}
	if left == len(aConv) {
		return -1, nil
	}
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[left]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[left]})
	}

	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) [][]int {
	//fmt.Printf("aconv: %v, bconv: %v, aprices: %v, bprices: %v, k: %v, s: %v\n", aConv, bConv, aPrices, bPrices, k, numBurles)
	ai := 0
	bi := 0
	resp := [][]int{}
	for k > 0 {
		aMin := int64(math.MaxInt64)
		bMin := int64(math.MaxInt64)
		if ai < len(aPrices) {
			aMin = int64(aConv[0][0]) * int64(aPrices[ai][0])
		}
		if bi < len(bPrices) {
			bMin = int64(bConv[0][0]) * int64(bPrices[bi][0])
		}

		min := int64(math.Min(float64(aMin), float64(bMin)))
		//fmt.Printf("aMin: %v, bMin: %v, min: %v\n", aMin, bMin, min)
		if numBurles < min {
			return nil
		}

		numBurles -= min
		k -= 1
		if aMin < bMin || ((aMin == bMin) && (aConv[0][1] < bConv[0][1])) {
			//fmt.Printf("1: %v\n", []int{aPrices[ai][0], aPrices[ai][1], aConv[0][0], aConv[0][1], 1})
			resp = append(resp, []int{aPrices[ai][0], aPrices[ai][1], aConv[0][0], aConv[0][1], 1})
			ai += 1
		} else {
			//fmt.Printf("2: %v\n", []int{bPrices[bi][0], bPrices[bi][1], bConv[0][0], bConv[0][1], 2})
			resp = append(resp, []int{bPrices[bi][0], bPrices[bi][1], bConv[0][0], bConv[0][1], 2})
			bi += 1
		}
	}

	//fmt.Println(resp)
	sort.Slice(resp, func(i, j int) bool {
		return resp[i][3] < resp[i][3]
	})

	for true {
		curr := resp[len(resp)-1]
		currItemPrice := curr[0]
		currItemConvRate := curr[2]
		currItemDayIdx := curr[3]
		currItemType := curr[4]
		tmpBurles := numBurles + int64(currItemPrice)*int64(currItemConvRate)
		if currItemType == 1 {
			idx := sort.Search(len(aConv), func(i int) bool {
				return aConv[i][0] > currItemConvRate
			})

			for idx < len(aConv) {
				if aConv[idx][1] > currItemDayIdx {
					continue
				}
				if int64(currItemPrice)*int64(aConv[idx][0]) > tmpBurles {
					return  resp
				}
				respIdx := sort.Search(len(resp)-1, func(i int) bool {
					return resp[i][3] >= aConv[idx][1]
				})

				numBurles = tmpBurles - int64(currItemPrice)*int64(aConv[idx][0])
				resp = append(append(resp[0:respIdx], []int{curr[0], curr[1], aConv[idx][0], aConv[idx][1], curr[4]}),
					resp[respIdx:len(resp)-1]...)
				idx++
			}
			if idx == len(aConv) {
				return resp
			}
		} else {
			idx := sort.Search(currItemConvRate, func(i int) bool {
				return bConv[i][0] >= currItemConvRate
			})
			for idx < len(bConv) {
				if bConv[idx][1] > currItemDayIdx {
					continue
				}
				if int64(currItemPrice)*int64(bConv[idx][0]) > tmpBurles {
					return  resp
				}
				respIdx := sort.Search(len(resp)-1, func(i int) bool {
					return resp[i][3] >= bConv[idx][1]
				})
				numBurles = tmpBurles - int64(currItemPrice)*int64(bConv[idx][0])
				resp = append(append(resp[0:respIdx], []int{curr[0], curr[1], bConv[idx][0], bConv[idx][1], curr[4]}),
					resp[respIdx:len(resp)-1]...)
				idx++
			}
			if idx == len(bConv) {
				return resp
			}
		}
	}
	return nil

}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	sort.Slice(aConv, func (i, j int) bool {
		return aConv[i][0] < aConv[j][0]
	})
	sort.Slice(bConv, func (i, j int) bool {
		return bConv[i][0] < bConv[j][0]
	})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	resp := helper(aConv, bConv, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(resp[len(resp)-1][3]+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][3] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)
	var aNum, bNum int
	mid := -1

	for left < right {
		mid = (left + right - 1)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) - 1 && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if aSum + bSum <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			if right == mid + 1 {
				break
			}
			right = mid + 1
		} else {
			left = mid + 1
		}
	}
	if left >= len(aConv) {
		return -1, nil
	}
	mid = (left + right) / 2
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[mid]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[mid]})
	}

	//fmt.Printf("resp: %v\n", resp)
	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)
	var aNum, bNum int
	mid := -1

	for left < right {
		mid = (left + right - 1)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if aSum + bSum <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			if right == mid + 1 {
				break
			}
			right = mid + 1
		} else {
			left = mid + 1
		}
	}
	if left >= len(aConv) {
		return -1, nil
	}
	mid = (left + right - 1) / 2
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[mid]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[mid]})
	}

	//fmt.Printf("resp: %v\n", resp)
	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)-1
	var aNum, bNum int

	for left <= right {
		mid := (left + right)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := 0
		bSum := 0
		for idx := 0; idx < aNum; idx++ {
			aSum += aPrices[idx][0] * aConv[minAConvIdx[mid]][0]
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += bPrices[idx][0] * bConv[minBConvIdx[mid]][0]
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) - 1 && bNum > 0 {
				aNum++
				bNum--
				aSum += aPrices[aNum-1][0] * aConv[minAConvIdx[mid]][0]
				bSum -= bPrices[bNum][0] * bConv[minBConvIdx[mid]][0]
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if int64(aSum) + int64(bSum) <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			right = mid-1
		} else {
			left = mid+1
		}
	}
	if left == len(aConv) {
		return -1, nil
	}
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[left]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[left]})
	}

	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)
	var aNum, bNum int
	mid := (left + right)/2

	for left < right {
		mid = (left + right)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) - 1 && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if aSum + bSum <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			right = mid
		} else {
			left = mid+1
		}
	}
	if left >= len(aConv) {
		return -1, nil
	}
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[mid]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[mid]})
	}

	//fmt.Printf("resp: %v\n", resp)
	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) [][]int {
	//fmt.Printf("aconv: %v, bconv: %v, aprices: %v, bprices: %v, k: %v, s: %v\n", aConv, bConv, aPrices, bPrices, k, numBurles)
	ai := 0
	bi := 0
	resp := [][]int{}
	for k > 0 {
		aMin := int64(math.MaxInt64)
		bMin := int64(math.MaxInt64)
		if ai < len(aPrices) {
			aMin = int64(aConv[0][0]) * int64(aPrices[ai][0])
		}
		if bi < len(bPrices) {
			bMin = int64(bConv[0][0]) * int64(bPrices[bi][0])
		}

		min := int64(math.Min(float64(aMin), float64(bMin)))
		//fmt.Printf("aMin: %v, bMin: %v, min: %v\n", aMin, bMin, min)
		if numBurles < min {
			return nil
		}

		numBurles -= min
		k -= 1
		if aMin < bMin || ((aMin == bMin) && (aConv[0][1] < bConv[0][1])) {
			//fmt.Printf("1: %v\n", []int{aPrices[ai][0], aPrices[ai][1], aConv[0][0], aConv[0][1], 1})
			resp = append(resp, []int{aPrices[ai][0], aPrices[ai][1], aConv[0][0], aConv[0][1], 1})
			ai += 1
		} else {
			//fmt.Printf("2: %v\n", []int{bPrices[bi][0], bPrices[bi][1], bConv[0][0], bConv[0][1], 2})
			resp = append(resp, []int{bPrices[bi][0], bPrices[bi][1], bConv[0][0], bConv[0][1], 2})
			bi += 1
		}
	}

	//fmt.Println(resp)
	sort.Slice(resp, func(i, j int) bool {
		return resp[i][3] < resp[i][3]
	})

	for true {
		curr := resp[len(resp)-1]
		currItemPrice := curr[0]
		currItemConvRate := curr[2]
		currItemDayIdx := curr[3]
		currItemType := curr[4]
		tmpBurles := numBurles + int64(currItemPrice)*int64(currItemConvRate)
		//fmt.Printf("enter: curr: %v, numBurlesLeft: %v, tmpBurles: %v, resp: %v\n", curr, numBurles, tmpBurles, resp)
		if currItemType == 1 {
			idx := sort.Search(len(aConv), func(i int) bool {
				return aConv[i][0] > currItemConvRate
			})
			//fmt.Printf("1 aConv idx: %v\n", idx)

			for idx < len(aConv) {
				if aConv[idx][1] >= currItemDayIdx {
					idx++
					continue
				}
				if int64(currItemPrice)*int64(aConv[idx][0]) > tmpBurles {
					return  resp
				}
				respIdx := sort.Search(len(resp)-1, func(i int) bool {
					return resp[i][3] >= aConv[idx][1]
				})
				//fmt.Printf("1 idx: %v, resp: %v, dayIdx: %v, respIdx: %v\n", idx, resp, aConv[idx][1], respIdx)

				numBurles = tmpBurles - int64(currItemPrice)*int64(aConv[idx][0])
				tmp := make([][]int, len(resp))
				for i := 0; i < respIdx; i++ {
					tmp[i] = resp[i]
				}
				tmp[respIdx] = []int{curr[0], curr[1], aConv[idx][0], aConv[idx][1], curr[4]}
				for i := respIdx + 1; i < len(resp); i++ {
					tmp[i] = resp[i-1]
				}
				resp = tmp
				//resp = append(append(tmp[0:respIdx], []int{curr[0], curr[1], aConv[idx][0], aConv[idx][1], curr[4]}),
				//	tmp[respIdx:len(resp)-1]...)
				break
			}
			//fmt.Printf("1 idx: %v, resp: %v\n", idx, resp)
			if idx == len(aConv) {
				return resp
			}
		} else {
			idx := sort.Search(len(bConv), func(i int) bool {
				return bConv[i][0] >= currItemConvRate
			})
			//fmt.Printf("2 bConv idx: %v\n", idx)
			for idx < len(bConv) {
				if bConv[idx][1] >= currItemDayIdx {
					idx++
					continue
				}
				if int64(currItemPrice)*int64(bConv[idx][0]) > tmpBurles {
					return  resp
				}
				respIdx := sort.Search(len(resp)-1, func(i int) bool {
					return resp[i][3] >= bConv[idx][1]
				})
				//fmt.Printf("2 idx: %v, resp: %v, dayIdx: %v, respIdx: %v\n", idx, resp, bConv[idx][1], respIdx)
				numBurles = tmpBurles - int64(currItemPrice)*int64(bConv[idx][0])
				tmp := make([][]int, len(resp))
				for i := 0; i < respIdx; i++ {
					tmp[i] = resp[i]
				}
				tmp[respIdx] = []int{curr[0], curr[1], bConv[idx][0], bConv[idx][1], curr[4]}
				for i := respIdx + 1; i < len(resp); i++ {
					tmp[i] = resp[i-1]
				}
				resp = tmp
				//resp = append(append(resp[0:respIdx], []int{curr[0], curr[1], bConv[idx][0], bConv[idx][1], curr[4]}),
				//	resp[respIdx:len(resp)-1]...)
				break
			}
			if idx == len(bConv) {
				return resp
			}
			//fmt.Printf("2 idx: %v, resp: %v\n", idx, resp)
		}

	}
	return nil

}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	sort.Slice(aConv, func (i, j int) bool {
		return aConv[i][0] < aConv[j][0]
	})
	sort.Slice(bConv, func (i, j int) bool {
		return bConv[i][0] < bConv[j][0]
	})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	resp := helper(aConv, bConv, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(resp[len(resp)-1][3]+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][3] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)-1
	var aNum, bNum int

	for left < right {
		mid := (left + right)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := 0
		bSum := 0
		for idx := 0; idx < aNum; idx++ {
			aSum += aPrices[idx][0] * aConv[minAConvIdx[mid]][0]
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += bPrices[idx][0] * bConv[minBConvIdx[mid]][0]
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) - 1 && bNum > 0 {
				aNum++
				bNum--
				aSum += aPrices[aNum-1][0] * aConv[minAConvIdx[mid]][0]
				bSum -= bPrices[bNum][0] * bConv[minBConvIdx[mid]][0]
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if int64(aSum) + int64(bSum) <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			right = mid
		} else {
			left = mid+1
		}
	}
	if left == len(aConv) {
		return -1, nil
	}
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[left]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[left]})
	}

	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)
	var aNum, bNum int
	mid := -1

	for left < right {
		mid = (left + right - 1)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if aSum + bSum <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			if right == mid + 1 {
				break
			}
			right = mid + 1
		} else {
			left = mid + 1
		}
	}
	if left >= len(aConv) {
		return -1, nil
	}
	mid = (left + right - 1) / 2
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[mid]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[mid]})
	}

	//fmt.Printf("resp: %v\n", resp)
	return mid, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)
	var aNum, bNum int
	mid := -1

	for left < right {
		mid = (left + right - 1)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) - 1 && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if aSum + bSum <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			if right == mid + 1 {
				break
			}
			right = mid + 1
		} else {
			left = mid + 1
		}
	}
	if left >= len(aConv) {
		return -1, nil
	}
	mid = (left + right - 1) / 2
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[mid]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[mid]})
	}

	//fmt.Printf("resp: %v\n", resp)
	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}

package main

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

func helper(aConv [][]int, bConv [][]int, minAConvIdx []int, minBConvIdx []int, aPrices [][]int, bPrices [][]int, k int, numBurles int64) (int, [][]int) {

	//fmt.Printf("aconv: %v, bconv: %v, minaconvidx: %v, minbconvidx: %v, aprices: %v, bprices: %v, k: %v, numburles: %v\n",
	//	aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, numBurles)
	left := 0
	right := len(aConv)-1
	var aNum, bNum int
	mid := (left + right)/2

	for left <= right {
		mid = (left + right)/2

		if len(bPrices) >= k {
			aNum = 0
			bNum = k
		} else {
			aNum = k - len(bPrices)
			bNum = len(bPrices)
		}
		aSum := int64(0)
		bSum := int64(0)
		for idx := 0; idx < aNum; idx++ {
			aSum += int64(aPrices[idx][0]) * int64(aConv[minAConvIdx[mid]][0])
		}
		for idx := 0; idx < bNum; idx++ {
			bSum += int64(bPrices[idx][0]) * int64(bConv[minBConvIdx[mid]][0])
		}

		//fmt.Printf("left: %v, right: %v, mid: %v, aNum: %v, bNum: %v, aSum: %v, bSum: %v\n",
		//	left, right, mid, aNum, bNum, aSum, bSum)
		possible := false
		if int64(aSum) + int64(bSum) <= numBurles {
			possible = true
		} else {

			for aNum < len(aPrices) - 1 && bNum > 0 {
				aNum++
				bNum--
				aSum += int64(aPrices[aNum-1][0]) * int64(aConv[minAConvIdx[mid]][0])
				bSum -= int64(bPrices[bNum][0]) * int64(bConv[minBConvIdx[mid]][0])
				//fmt.Printf("anum: %v, bnum: %v, asum: %v, bsum: %v\n", aNum, bNum, aSum, bSum)
				if aSum + bSum <= numBurles {
					possible = true
					break
				}
			}
		}
		if possible {
			right = mid-1
		} else {
			left = mid+1
		}
	}
	if left == len(aConv) {
		return -1, nil
	}
	resp := [][]int{}
	for idx := 0; idx < aNum; idx++ {
		resp = append(resp, []int{aPrices[idx][0], aPrices[idx][1], minAConvIdx[mid]})
	}
	for idx := 0; idx < bNum; idx++ {
		resp = append(resp, []int{bPrices[idx][0], bPrices[idx][1], minBConvIdx[mid]})
	}

	return left, resp
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer([]byte{}, int(math.Pow(10, 8)))

	scanner.Scan()
	nmksStr := scanner.Text()
	nmksArr := strings.Split(nmksStr, " ")
	n, _ := strconv.Atoi(nmksArr[0])
	m, _ := strconv.Atoi(nmksArr[1])
	k, _ := strconv.Atoi(nmksArr[2])
	s, _ := strconv.Atoi(nmksArr[3])

	scanner.Scan()
	aConvStr := scanner.Text()
	aConvStrArr := strings.Split(aConvStr, " ")
	scanner.Scan()
	bConvStr := scanner.Text()
	bConvStrArr := strings.Split(bConvStr, " ")

	aConv := make([][]int, n)
	bConv := make([][]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(aConvStrArr[i])
		b, _ := strconv.Atoi(bConvStrArr[i])
		aConv[i] = []int{a, i}
		bConv[i] = []int{b, i}
	}
	//sort.Slice(aConv, func (i, j int) bool {
	//	return aConv[i][0] < aConv[j][0]
	//})
	//sort.Slice(bConv, func (i, j int) bool {
	//	return bConv[i][0] < bConv[j][0]
	//})

	aPrices := [][]int{}
	bPrices := [][]int{}
	for i := 0; i<m; i++ {
		scanner.Scan()
		arr := strings.Split(scanner.Text(), " ")
		curr, _ := strconv.Atoi(arr[1])
		if arr[0] == "1" {
			aPrices = append(aPrices, []int{curr, i})
		} else {
			bPrices = append(bPrices, []int{curr, i})
		}
	}
	sort.Slice(aPrices, func(i, j int) bool {
		return aPrices[i][0] < aPrices[j][0]
	})
	sort.Slice(bPrices, func(i, j int) bool {
		return bPrices[i][0] < bPrices[j][0]
	})

	minAConvIdx := make([]int, len(aConv))
	minAIdx := -1
	minAConv := math.MaxInt32
	for idx := range aConv {
		if aConv[idx][0] < minAConv {
			minAConvIdx[idx] = idx
			minAIdx = idx
			minAConv = aConv[idx][0]
		} else {
			minAConvIdx[idx] = minAIdx
		}
	}
	minBConvIdx := make([]int, len(bConv))
	minBIdx := -1
	minBConv := math.MaxInt32
	for idx := range bConv {
		if bConv[idx][0] <= minBConv {
			minBConvIdx[idx] = idx
		} else {
			minBConvIdx[idx] = minBIdx
		}
	}

	dayIdx, resp := helper(aConv, bConv, minAConvIdx, minBConvIdx, aPrices, bPrices, k, int64(s))
	if resp == nil {
		fmt.Println(-1)
		return
	}
	fmt.Println(dayIdx+1)
	for idx := range resp {
		fmt.Printf("%v %v\n", resp[idx][1] + 1, resp[idx][2] + 1)
	}

}


Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from 1 to n. Some words have the same meaning so there are k groups of words such that all the words in some group have the same meaning.Mahmoud knows that the i-th word can be sent with cost ai. For each word in his message, Mahmoud can either replace it with another word of the same meaning or leave it as it is. Can you help Mahmoud determine the minimum cost of sending the message?The cost of sending the message is the sum of the costs of sending every word in it.

The first line of input contains integers n, k and m (1 ≤ k ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of words in their language, the number of groups of words, and the number of words in Mahmoud's message respectively. The second line contains n strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It's guaranteed that the words are distinct. The third line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) where ai is the cost of sending the i-th word. The next k lines describe the groups of words of same meaning. The next k lines each start with an integer x (1 ≤ x ≤ n) which means that there are x words in this group, followed by x integers which represent the indices of words in this group. It's guaranteed that each word appears in exactly one group. The next line contains m space-separated words which represent Mahmoud's message. Each of these words appears in the list of language's words.

The only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning.

5 4 4
i loser am the second
100 1 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

5 4 4
i loser am the second
100 20 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

NoteIn the first sample, Mahmoud should replace the word "second" with the word "loser" because it has less cost so the cost will be 100+1+5+1=107.In the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116.

package main

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

func solve() {
	n := readInt()
	k := readInt()
	m := readInt()
	words := make([]string, n, n)
	for i := range words {
		words[i] = readString()
	}

	costs := make([]int64, n, n)
	for i := range costs {
		costs[i] = readInt64()
	}

	mcosts := make(map[string]int64)
	for i, w := range words {
		mcosts[w] = costs[i]
	}

	xcost := int64(0)
	for i := 0; i < k; i++ {
		nremap := readInt()
		remaps := make([]int, nremap, nremap)
		for j := range remaps {
			r := readInt() - 1
			remaps[j] = r
			if j == 0 {
				xcost = costs[r]
			} else if costs[r] < xcost {
				xcost = costs[r]
			}
		}

		// we can send each word in this group
		// at the minimum costs computed above
		for _, r := range remaps {
			mcosts[words[r]] = xcost
		}
	}

	sum := int64(0)
	for i := 0; i < m; i++ {
		sum += mcosts[readString()]
	}
	fmt.Println(sum)
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	K := io.NextInt()
	M := io.NextInt()

	S := make([]string, N)
	idx := make(map[string]int)
	for i := 0; i < N; i++ {
	  S[i] = io.NextString()
	  idx[S[i]] = i
	}

	A := io.NextLongArray(N)

// 	fmt.Println(idx)

	mins := make([]int64, N)
	for i, v := range A {
	  mins[i] = v
	}

	djs := NewDisjointSet(N)
	for i := 0; i < K; i++ {
	  X := io.NextInt()
	  Y := io.NextInt() - 1
	  for j := 1; j < X; j++ {
	    Z := io.NextInt() - 1
	    djs.union(Y, Z)
	    which := djs.find(Z)
	    mins[which] = Min(mins[which], A[Y], A[Z])
	   // mins[Y] = Min(mins[Y], A[Y], A[Z])
	   // mins[Z] = Min(mins[Z], A[Y], A[Z])
	  }
	}

// 	fmt.Println(mins)

	var cost int64
	for i := 0; i < M; i++ {
	  W := io.NextString()
	  which := djs.find(idx[W])
	  cost += mins[which]
	 // fmt.Printf("%s: %d %d %d\n", W, idx[W], which, mins[which])
	}
	io.Println(cost)
}

func Min(args ...int64) int64 {
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func FillLongArray(arr []int64, val int64) {
	for i := range arr {
		arr[i] = val
	}
}

type DisjointSet struct {
	parent []int
	rank []int
}

func NewDisjointSet(n int) *DisjointSet {
	dj := DisjointSet{
		parent: make([]int, n),
		rank: make([]int, n),
	}
	for i := 0; i < n; i++ {
		dj.parent[i] = i
	}
	return &dj
}

func (dj *DisjointSet) find(x int) int {
	if dj.parent[x] != x {
		dj.parent[x] = dj.find(dj.parent[x])
	}
	return dj.parent[x]
}

func (dj *DisjointSet) union(x, y int) bool {
	xr := dj.find(x)
	yr := dj.find(y)

	if xr == yr {
		return false
	}

	if dj.rank[xr] < dj.rank[yr] {
		dj.parent[xr] = yr
	} else if dj.rank[xr] > dj.rank[yr] {
		dj.parent[yr] = xr
	} else {
		dj.parent[xr] = yr
		dj.rank[yr]++
	}
	return true
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	K := io.NextInt()
	M := io.NextInt()

	S := make([]string, N)
	idx := make(map[string]int)
	for i := 0; i < N; i++ {
	  S[i] = io.NextString()
	  idx[S[i]] = i
	}

	A := io.NextLongArray(N)

// 	fmt.Println(idx)

	mins := make([]int64, N)
	for i, v := range A {
	  mins[i] = v
	}

	djs := NewDisjointSet(N)
	for i := 0; i < K; i++ {
	  X := io.NextInt()
	  Y := io.NextInt() - 1
	  for j := 1; j < X; j++ {
	    Z := io.NextInt() - 1
	    djs.union(Y, Z)
	   // which := djs.find(Z)
	   // mins[which] = Min(mins[which], A[Z])
	    mins[Y] = Min(mins[Y], A[Y], A[Z])
	    mins[Z] = Min(mins[Z], A[Y], A[Z])
	  }
	}

// 	fmt.Println(mins)

	var cost int64
	for i := 0; i < M; i++ {
	  W := io.NextString()
	  which := djs.find(idx[W])
	  cost += mins[which]
	 // fmt.Printf("%s: %d %d %d\n", W, idx[W], which, mins[which])
	}
	io.Println(cost)
}

func Min(args ...int64) int64 {
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func FillLongArray(arr []int64, val int64) {
	for i := range arr {
		arr[i] = val
	}
}

type DisjointSet struct {
	parent []int
	rank []int
}

func NewDisjointSet(n int) *DisjointSet {
	dj := DisjointSet{
		parent: make([]int, n),
		rank: make([]int, n),
	}
	for i := 0; i < n; i++ {
		dj.parent[i] = i
	}
	return &dj
}

func (dj *DisjointSet) find(x int) int {
	if dj.parent[x] != x {
		dj.parent[x] = dj.find(dj.parent[x])
	}
	return dj.parent[x]
}

func (dj *DisjointSet) union(x, y int) bool {
	xr := dj.find(x)
	yr := dj.find(y)

	if xr == yr {
		return false
	}

	if dj.rank[xr] < dj.rank[yr] {
		dj.parent[xr] = yr
	} else if dj.rank[xr] > dj.rank[yr] {
		dj.parent[yr] = xr
	} else {
		dj.parent[xr] = yr
		dj.rank[yr]++
	}
	return true
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
	N := io.NextInt()
	K := io.NextInt()
	M := io.NextInt()

	S := make([]string, N)
	idx := make(map[string]int)
	for i := 0; i < N; i++ {
	  S[i] = io.NextString()
	  idx[S[i]] = i
	}

	A := io.NextLongArray(N)

// 	fmt.Println(idx)

	mins := make([]int64, N)
	for i, v := range A {
	  mins[i] = v
	}

	djs := NewDisjointSet(N)
	for i := 0; i < K; i++ {
	  X := io.NextInt()
	  Y := io.NextInt() - 1
	  for j := 1; j < X; j++ {
	    Z := io.NextInt() - 1
	    djs.union(Y, Z)
	   // which := djs.find(Z)
	   // mins[which] = Min(mins[which], A[Z])
	    mins[Y] = Min(mins[Y], A[Y], A[Z])
	    mins[Z] = Min(mins[Z], A[Y], A[Z])
	  }
	}

// 	fmt.Println(mins)

	var cost int64
	for i := 0; i < M; i++ {
	  W := io.NextString()
	  which := idx[W]
	  cost += mins[which]
	 // fmt.Printf("%s: %d %d %d\n", W, idx[W], which, mins[which])
	}
	io.Println(cost)
}

func Min(args ...int64) int64 {
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func FillLongArray(arr []int64, val int64) {
	for i := range arr {
		arr[i] = val
	}
}

type DisjointSet struct {
	parent []int
	rank []int
}

func NewDisjointSet(n int) *DisjointSet {
	dj := DisjointSet{
		parent: make([]int, n),
		rank: make([]int, n),
	}
	for i := 0; i < n; i++ {
		dj.parent[i] = i
	}
	return &dj
}

func (dj *DisjointSet) find(x int) int {
	if dj.parent[x] != x {
		dj.parent[x] = dj.find(dj.parent[x])
	}
	return dj.parent[x]
}

func (dj *DisjointSet) union(x, y int) bool {
	xr := dj.find(x)
	yr := dj.find(y)

	if xr == yr {
		return false
	}

	if dj.rank[xr] < dj.rank[yr] {
		dj.parent[xr] = yr
	} else if dj.rank[xr] > dj.rank[yr] {
		dj.parent[yr] = xr
	} else {
		dj.parent[xr] = yr
		dj.rank[yr]++
	}
	return true
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}


Let us denote by $$$d(n)$$$ the sum of all divisors of the number $$$n$$$, i.e. $$$d(n) = \sum\limits_{k | n} k$$$.For example, $$$d(1) = 1$$$, $$$d(4) = 1+2+4=7$$$, $$$d(6) = 1+2+3+6=12$$$.For a given number $$$c$$$, find the minimum $$$n$$$ such that $$$d(n) = c$$$.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is characterized by one integer $$$c$$$ ($$$1 \le c \le 10^7$$$).

For each test case, output:    "-1" if there is no such $$$n$$$ that $$$d(n) = c$$$;  $$$n$$$, otherwise.

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	const mx int = 1e7
	d := make([]int, mx+1)
	d[1] = 1
	primes := make([]int, 0, 664579)
	for i := 2; i <= mx; i++ {
		if d[i] == 0 {
			d[i] = 1 + i
			primes = append(primes, i)
		}
		for _, p := range primes {
			v := p * i
			if v > mx {
				break
			}
			if i%p == 0 {
				x := i
				for {
					x /= p
					if x%p > 0 {
						break
					}
				}
				d[v] = d[i]*p + d[x]
				break
			}
			d[v] = d[i] * (1 + p)
		}
	}

	mi := make([]int, mx+1)
	for i := mx; i > 0; i-- {
		if d := d[i]; d <= mx {
			mi[d] = i
		}
	}

	for T := r(); T > 0; T-- {
		if m := mi[r()]; m > 0 {
			Fprintln(out, m)
		} else {
			Fprintln(out, "-1")
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	const mx int = 1e7
	d := make([]int, mx+1)
	d[1] = 1
	s := make([]int, mx+1)
	primes := make([]int, 0, 664579)
	for i := 2; i <= mx; i++ {
		if d[i] == 0 {
			s[i] = 1 + i
			d[i] = 1 + i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			if i%p == 0 {
				s[p*i] = s[i]*p + 1
				d[p*i] = d[i] / s[i] * s[p*i]
				break
			}
			s[p*i] = 1 + p
			d[p*i] = d[i] * (1 + p)
		}
	}

	mi := make([]int, mx+1)
	for i := mx; i > 0; i-- {
		if d := d[i]; d <= mx {
			mi[d] = i
		}
	}

	for T := r(); T > 0; T-- {
		if m := mi[r()]; m > 0 {
			Fprintln(out, m)
		} else {
			Fprintln(out, "-1")
		}
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"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()

	const mx int = 1e7
	d := make([]int, mx+1)
	d[1] = 1
	s := make([]int, mx+1)
	primes := make([]int, 0, 664579)
	for i := 2; i <= mx; i++ {
		if d[i] == 0 {
			s[i] = 1 + i
			d[i] = s[i]
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			if i%p == 0 {
				s[p*i] = s[i]*p + 1
				d[p*i] = d[i] / s[i] * s[p*i]
				break
			}
			s[p*i] = 1 + p
			d[p*i] = d[i] * s[p*i]
		}
	}

	mi := make([]int, mx+1)
	for i := 1; i <= mx; i++ {
		if d := d[i]; d <= mx && mi[d] == 0 {
			mi[d] = i
		}
	}

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		if mi[n] == 0 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, mi[n])
		}
	}

	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) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"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()

	const mx int = 1e7
	d := make([]int, mx+1)
	d[1] = 1
	s := make([]int, mx+1)
	primes := []int{}
	for i := 2; i <= mx; i++ {
		if d[i] == 0 {
			d[i] = 1 + i
			s[i] = 1 + i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			if i%p == 0 {
				s[p*i] = s[i]*p + 1
				d[p*i] = d[i] / s[i] * s[p*i]
				break
			}
			s[p*i] = 1 + p
			d[p*i] = d[i] * (p + 1)
		}
	}

	mi := make([]int, mx+1)
	for i := 1; i <= mx; i++ {
		if d := d[i]; d <= mx && mi[d] == 0 {
			mi[d] = i
		}
	}

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		if mi[n] == 0 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, mi[n])
		}
	}

	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) }

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	ans := [10000005]int{}
	f := [10000005]int{}
	for i:=1;i<=10000000;i++{
		for j:=i;j<=10000000;j+=i{
			f[j] += i
		}
		if f[i]<=10000000{
			if ans [f[i]]==0 {
				ans[f[i]] = i
			}
		}
	}
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var c int
		fmt.Fscan(in,&c)
		if ans[c]!=0{
			fmt.Fprintln(out,ans[c])
		}else{
			fmt.Fprintln(out,-1)
		}
	}
}

package main

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

const m = 10000001

func main() {
	in := bufio.NewReader(os.Stdin)
	sum := make([]int, m)
	for i:=1;i<m;i++ {
		for j:=i;j<m;j+=i {
			sum[j]+=i
		}
	}
	at := make([]int, m)
	for i := m-1; i>0;i-- {
		if(sum[i] < m){
			at[sum[i]] = i + 1
		}
	}
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var c int
		fmt.Fscan(in, &c)
		fmt.Println(at[c] - 1)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"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()

	const mx int = 1e7
	d := make([]int, mx+1)
	d[1] = 1
	mi := make([]int, mx+1)
	mi[1] =1
	s := make([]int, mx+1)
	primes := make([]int, 0, 664579)
	for i := 2; i <= mx; i++ {
		if d[i] == 0 {
			s[i] = 1 + i
			d[i] = s[i]
			if d := d[i]; d <= mx && mi[d] == 0 {
				mi[d] = i
			}
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			if i%p == 0 {
				s[p*i] = s[i]*p + 1
				d[p*i] = d[i] / s[i] * s[p*i]
				if d := d[p*i]; d <= mx && mi[d] == 0 {
					mi[d] = p*i
				}
				break
			}
			s[p*i] = 1 + p
			d[p*i] = d[i] * s[p*i]
			if d := d[p*i]; d <= mx && mi[d] == 0 {
				mi[d] = p*i
			}
		}
	}

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		if mi[n] == 0 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, mi[n])
		}
	}

	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) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"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()

	const mx int = 1e7
	d := make([]int, mx+1)
	d[1] = 1
	powp := make([]int, mx+1)
	s := make([]int, mx+1)
	primes := []int{}
	for i := 2; i <= mx; i++ {
		if d[i] == 0 {
			d[i] = 1 + i
			powp[i] = i
			s[i] = 1 + i
			primes = append(primes, i)
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			if i%p == 0 {
				d[p*i] = d[i] / s[i] * (s[i] + powp[i]*p)
				powp[p*i] = powp[i] * p
				s[p*i] = s[i] + powp[p*i]
				break
			}
			d[p*i] = d[i] * d[p]
			powp[p*i] = powp[i] * powp[p]
			s[p*i] = d[i] * d[p]
		}
	}

	mi := make([]int, mx+1)
	for i := 1; i <= mx; i++ {
		if d := d[i]; d <= mx && mi[d] == 0 {
			mi[d] = i
		}
	}

	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		if mi[n] == 0 {
			Fprintln(out, -1)
		} else {
			Fprintln(out, mi[n])
		}
	}

	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) }

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	ans := [10000005]int{}
	f := [10000005]int{}
	for i:=1;i<=10000000;i++{
		for j:=i;j<=10000000;j+=i{
			f[j] += i
		}
		if f[i]<=10000000{
			ans[f[i]]=i
		}
	}
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var c int
		fmt.Fscan(in,&c)
		if ans[c]!=0{
			fmt.Fprintln(out,ans[c])
		}else{
			fmt.Fprintln(out,-1)
		}
	}
}


Recently the construction of Berland collider has been completed. Collider can be represented as a long narrow tunnel that contains n particles. We associate with collider 1-dimensional coordinate system, going from left to right. For each particle we know its coordinate and velocity at the moment of start of the collider. The velocities of the particles don't change after the launch of the collider. Berland scientists think that the big bang will happen at the first collision of particles, whose velocities differs in directions. Help them to determine how much time elapses after the launch of the collider before the big bang happens.

The first line contains single integer n (1 ≤ n ≤ 5·105) — amount of particles in the collider. Next n lines contain description of particles. Each particle is described by two integers xi, vi ( - 109 ≤ xi, vi ≤ 109, vi ≠ 0) — coordinate and velocity respectively. All the coordinates are distinct. The particles are listed in order of increasing of coordinates. All the coordinates are in meters, and all the velocities — in meters per second. The negative velocity means that after the start of collider the particle will move to the left, and the positive — that the particle will move to the right.

If there will be no big bang, output -1. Otherwise output one number — how much time in seconds elapses after the launch of the collider before the big bang happens. Your answer must have a relative or absolute error less than 10 - 9.

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

type Particle struct {
	x, v float64
}

type Particles []Particle

func (ps *Particles) Len() int {
	return len(*ps)
}

func (ps *Particles) Swap(i, j int) {
	(*ps)[i], (*ps)[j] = (*ps)[j], (*ps)[i]
}

func (ps *Particles) Less(i, j int) bool {
	return (*ps)[i].x < (*ps)[j].x
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	ps := make(Particles, n)
	for i := 0; i < n; i++ {
		var x, v int
		fmt.Fscanf(reader, "%d %d\n", &x, &v)
		ps[i].x = float64(x)
		ps[i].v = float64(v)
	}
	sort.Sort(&ps)
	var l, r int64 = 0, 1e18 + 10
	var ans float64 = -1.0
	for l <= r {
		m := (l + r) / 2
		mid := float64(m) / float64(1000000000)
		var max float64 = -2e18 - 1
		found := false
		for i := 0; i < n; i++ {
			if ps[i].v > 0 {
				if ps[i].x+ps[i].v*mid > max {
					max = ps[i].x + ps[i].v*mid
				}
			} else {
				if ps[i].x+ps[i].v*mid <= max {
					found = true
					break
				}
			}
		}
		if found {
			ans = mid
			r = m - 1
		} else {
			l = m + 1
		}
	}
	if ans != -1 {
		fmt.Printf("%.9f", ans)
	} else {
		fmt.Printf("-1")
	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

type Particle struct {
	x, v float64
}

type Particles []Particle

func (ps *Particles) Len() int {
	return len(*ps)
}

func (ps *Particles) Swap(i, j int) {
	(*ps)[i], (*ps)[j] = (*ps)[j], (*ps)[i]
}

func (ps *Particles) Less(i, j int) bool {
	return (*ps)[i].x < (*ps)[j].x
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	ps := make(Particles, n)
	for i := 0; i < n; i++ {
		var x, v int
		fmt.Fscanf(reader, "%d %d\n", &x, &v)
		ps[i].x = float64(x)
		ps[i].v = float64(v)
	}
	sort.Sort(&ps)
	var l, r int64 = 0, 1e18 + 10
	var ans float64 = -1.0
	for l <= r {
		m := (l + r) / 2
		mid := float64(m) / float64(1000000000)
		max := -2e9 - 1
		found := false
		for i := 0; i < n; i++ {
			if ps[i].v > 0 {
				if ps[i].x+ps[i].v*mid > max {
					max = ps[i].x + ps[i].v*mid
				}
			} else {
				if ps[i].x+ps[i].v*mid <= max {
					found = true
					break
				}
			}
		}
		if found {
			ans = mid
			r = m - 1
		} else {
			l = m + 1
		}
	}
	if ans != -1 {
		fmt.Printf("%.9f", ans)
	} else {
		fmt.Printf("-1")
	}
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

type Particle struct {
	x, v int64
}

type Point struct {
	x     int64
	delta int
}

type Points []Point

func (ps *Points) Len() int {
	return len(*ps)
}

func (ps *Points) Swap(i, j int) {
	(*ps)[i], (*ps)[j] = (*ps)[j], (*ps)[i]
}

func (ps *Points) Less(i, j int) bool {
	return (*ps)[i].x < (*ps)[j].x
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	l2r := make([]Particle, 0, n)
	r2l := make([]Particle, 0, n)

	var minl2r, maxr2l int64 = 1000000001, -1000000001
	for i := 0; i < n; i++ {
		var x, v int64
		fmt.Fscanf(reader, "%d %d\n", &x, &v)
		x *= 1000000000
		if v > 0 {
			l2r = append(l2r, Particle{x, v})
			if x < minl2r {
				minl2r = x
			}
		} else {
			r2l = append(r2l, Particle{x, v})
			if x > maxr2l {
				maxr2l = x
			}
		}
	}
	if minl2r > maxr2l {
		fmt.Printf("-1")
		return
	}
	lPoint, rPoint := make(Points, 2*len(l2r)), make(Points, 2*len(r2l))
	var l, r int64 = 0, 1000000000000000001
	var ans int64 = 0
	for l <= r {
		mid := (r + l) / 2
		for i := 0; i < len(l2r); i++ {
			lPoint[i*2].x = l2r[i].x
			lPoint[i*2].delta = 1
			lPoint[i*2+1].x = l2r[i].x + l2r[i].v*mid
			lPoint[i*2+1].delta = -1
		}
		for i := 0; i < len(r2l); i++ {
			rPoint[i*2].x = r2l[i].x
			rPoint[i*2].delta = -1
			rPoint[i*2+1].x = r2l[i].x + r2l[i].v*mid
			rPoint[i*2+1].delta = 1
		}
		sort.Sort(&lPoint)
		sort.Sort(&rPoint)
		lcount, rcount := 0, 0
		found := false
		for i, j := 0, 0; i < len(lPoint) || j < len(rPoint); {
			if i < len(lPoint) && (j == len(rPoint) || lPoint[i].x <= rPoint[j].x) {
				lcount += lPoint[i].delta
				i++
			} else if j < len(rPoint) && (i == len(lPoint) || rPoint[j].x <= lPoint[i].x) {
				rcount += rPoint[j].delta
				j++
			}
			if lcount != 0 && rcount != 0 {
				found = true
				break
			}
		}
		if found {
			ans, r = mid, mid-1
		} else {
			l = mid + 1
		}
	}
	fmt.Printf("%.9f", float64(ans)/1000000000)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

type Particle struct {
	x, v int64
}

type Point struct {
	x     int64
	delta int
}

type Points []Point

func (ps *Points) Len() int {
	return len(*ps)
}

func (ps *Points) Swap(i, j int) {
	(*ps)[i], (*ps)[j] = (*ps)[j], (*ps)[i]
}

func (ps *Points) Less(i, j int) bool {
	return (*ps)[i].x < (*ps)[j].x
}

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewReader(bs)
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	l2r := make([]Particle, 0, n)
	r2l := make([]Particle, 0, n)

	var minl2r, maxr2l int64 = 1000000001, -1000000001
	for i := 0; i < n; i++ {
		var x, v int64
		fmt.Fscanf(reader, "%d %d\n", &x, &v)
		x *= 1000000000
		if v > 0 {
			l2r = append(l2r, Particle{x, v})
			if x < minl2r {
				minl2r = x
			}
		} else {
			r2l = append(r2l, Particle{x, v})
			if x > maxr2l {
				maxr2l = x
			}
		}
	}
	if minl2r > maxr2l {
		fmt.Printf("-1")
		return
	}
	lPoint, rPoint := make(Points, 2*len(l2r)), make(Points, 2*len(r2l))
	var l, r int64 = 0, 1000000000000000001
	var ans int64 = 0
	for l <= r {
		mid := (r + l) / 2
		for i := 0; i < len(l2r); i++ {
			lPoint[i*2].x = l2r[i].x
			lPoint[i*2].delta = 1
			lPoint[i*2+1].x = l2r[i].x + l2r[i].v*mid
			lPoint[i*2+1].delta = -1
		}
		for i := 0; i < len(r2l); i++ {
			rPoint[i*2].x = r2l[i].x
			rPoint[i*2].delta = -1
			rPoint[i*2+1].x = r2l[i].x + r2l[i].v*mid
			rPoint[i*2+1].delta = 1
		}
		sort.Sort(&lPoint)
		sort.Sort(&rPoint)
		lcount, rcount := 0, 0
		found := false
		for i, j := 0, 0; i < len(lPoint) || j < len(rPoint); {
			if i < len(lPoint) && j < len(rPoint) && lPoint[i].x == rPoint[j].x {
				found = true
				break
			}
			if i < len(lPoint) && (j == len(rPoint) || lPoint[i].x <= rPoint[j].x) {
				lcount += lPoint[i].delta
				i++
			} else if j < len(rPoint) && (i == len(lPoint) || rPoint[j].x <= lPoint[i].x) {
				rcount += rPoint[j].delta
				j++
			}
			if lcount != 0 && rcount != 0 {
				found = true
				break
			}
		}
		if found {
			ans, r = mid, mid-1
		} else {
			l = mid + 1
		}
	}
	fmt.Printf("%.9f", float64(ans)/1000000000)
}