You are given two strings of equal length $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose two adjacent characters in any string and assign the value of the first character to the value of the second or vice versa.For example, if $$$s$$$ is "acbc" you can get the following strings in one operation:   "aabc" (if you perform $$$s_2 = s_1$$$);  "ccbc" (if you perform $$$s_1 = s_2$$$);  "accc" (if you perform $$$s_3 = s_2$$$ or $$$s_3 = s_4$$$);  "abbc" (if you perform $$$s_2 = s_3$$$);  "acbb" (if you perform $$$s_4 = s_3$$$); Note that you can also apply this operation to the string $$$t$$$.Please determine whether it is possible to transform $$$s$$$ into $$$t$$$, applying the operation above any number of times.Note that you have to answer $$$q$$$ independent queries.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 100$$$) — the number of queries. Each query is represented by two consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 \le |s| \le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 \le |t| \leq 100$$$, $$$|t| = |s|$$$) consisting of lowercase Latin letters.

For each query, print "YES" if it is possible to make $$$s$$$ equal to $$$t$$$, and "NO" otherwise. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as positive answer).

NoteIn the first query, you can perform two operations $$$s_1 = s_2$$$ (after it $$$s$$$ turns into "aabb") and $$$t_4 = t_3$$$ (after it $$$t$$$ turns into "aabb"). In the second query, the strings are equal initially, so the answer is "YES".In the third query, you can not make strings $$$s$$$ and $$$t$$$ equal. Therefore, the answer is "NO".

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n int
	fmt.Scan(&n)
	var (
		first string
		second string
	)
	can := make([]bool, n)
	for i := range can {
		fmt.Scan(&first)
		fmt.Scan(&second)
		if strings.ContainsAny(first, second) {
			can[i] = true
		}
	}
	for _, e := range can {
		if e {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

//HOWDY! IT'S ME! A HARD BLACKISH DICK MOVE!
package main

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

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

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

//str := PrintlnInts(X...)

/*******************************************************************/
func equalizable(s, t string) bool {
	table := make(map[byte]bool)
	n := len(s)
	for i := 0; i < n; i++ {
		table[s[i]] = true
	}
	for i := 0; i < n; i++ {
		if table[t[i]] {
			return true
		}
	}
	return false
}

func main() {
	for q := ReadInt(); q > 0; q-- {
		s := ReadString()
		t := ReadString()
		ans := "NO"
		if equalizable(s, t) {
			ans = "YES"
		}
		fmt.Println(ans)
	}
}

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

func min(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	min := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] < min {
			min = args[i]
		}
	}
	return min
}

func max(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

func _gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return _gcd(b, a%b)
}

func gcd(args ...int) int {
	if len(args) == 0 {
		panic("empty!")
	}
	gcd := args[0]
	for i := 1; i < len(args); i++ {
		gcd = _gcd(gcd, args[i])
	}
	return gcd
}

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9))
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

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

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

func readInt_64() int64 {
	return readIntNbits(64)
}

func ReadInt2_64() (int64, int64) {
	return readInt_64(), readInt_64()
}

func ReadInt3_64() (int64, int64, int64) {
	return readInt_64(), readInt_64(), readInt_64()
}

func readInt() int {
	return int(readIntNbits(32))
}

func readIntNbits(n int) int64 {
	i, err := strconv.ParseInt(ReadString(), 0, n)
	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
}

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

// PrintIntsLine returns integers string delimited by a space.
func PrintlnInts(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)
}

package main

import (
	"fmt"
)

func main() {
	var tt int
	var a, b string
	var res bool
	fmt.Scan(&tt)
	for tt != 0 {
		res = false
		arr := make([]bool, 30)
		fmt.Scan(&a, &b)
		for i := 0; i < len(a); i++ {
			arr[int(a[i])-97] = true
		}

		for i := 0; i < len(b); i++ {
			if arr[int(b[i])-97] == true {
				res = true
				break
			}
		}
		if res {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
		tt--
	}
}

package main

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

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

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

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	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
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// DigitSum returns digit sum of a decimal number.
// DigitSum only accept a positive integer.
func DigitSum(n int) int {
	if n < 0 {
		return -1
	}

	res := 0

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

	return res
}

// DigitNumOfDecimal returns digits number of n.
// n is non negative number.
func DigitNumOfDecimal(n int) int {
	res := 0

	for n > 0 {
		n /= 10
		res++
	}

	return res
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int) int {
	return (a + (b - 1)) / b
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

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

/********** 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 MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var q int

func main() {
	q = ReadInt()

	for i := 0; i < q; i++ {
		S := ReadRuneSlice()
		T := ReadRuneSlice()

		smemo := [ALPHABET_NUM]int{}
		tmemo := [ALPHABET_NUM]int{}

		for j := 0; j < len(S); j++ {
			s, t := S[j], T[j]
			smemo[s-'a']++
			tmemo[t-'a']++
		}

		flag := false
		for j := 0; j < ALPHABET_NUM; j++ {
			if smemo[j] > 0 && tmemo[j] > 0 {
				flag = true
				break
			}
		}
		if flag {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

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

package main

import (
	"fmt"
)

func main() {
	var tt int
	var a, b string
	var res bool
	fmt.Scan(&tt)
	for tt != 0 {
		res = false
		arr := make([]bool, 30)
		fmt.Scan(&a, &b)
		for i := 0; i < len(a); i++ {
			arr[int(a[i])-97] = true
		}

		for i := 0; i < len(b); i++ {
			if arr[int(b[i])-97] == true {
				res = true
				break
			}
		}
		if res {
			println("YES")
		} else {
			println("NO")
		}
		tt--
	}
}

package main

import (
	"fmt"
)

func main() {
	var tt int
	var a, b string
	var res bool
	fmt.Scan(&tt)
	for tt != 0 {
		res = false
		arr := make([]bool, 30)
		fmt.Scan(&a, &b)
		for i := 0; i < len(a); i++ {
			arr[int(a[i])-97] = true
		}

		for i := 0; i < len(b); i++ {
			if arr[int(b[i])-97] == true {
				res = true
				break
			}
		}
		if res {
			print("YES\n")
		} else {
			print("NO\n")
		}
		tt--
	}
	return
}


You've got a string s = s1s2... s|s| of length |s|, consisting of lowercase English letters. There also are q queries, each query is described by two integers li, ri (1 ≤ li ≤ ri ≤ |s|). The answer to the query is the number of substrings of string s[li... ri], which are palindromes.String s[l... r] = slsl + 1... sr (1 ≤ l ≤ r ≤ |s|) is a substring of string s = s1s2... s|s|.String t is called a palindrome, if it reads the same from left to right and from right to left. Formally, if t = t1t2... t|t| = t|t|t|t| - 1... t1.

The first line contains string s (1 ≤ |s| ≤ 5000). The second line contains a single integer q (1 ≤ q ≤ 106) — the number of queries. Next q lines contain the queries. The i-th of these lines contains two space-separated integers li, ri (1 ≤ li ≤ ri ≤ |s|) — the description of the i-th query. It is guaranteed that the given string consists only of lowercase English letters.

Print q integers — the answers to the queries. Print the answers in the order, in which the queries are given in the input. Separate the printed numbers by whitespaces.

NoteConsider the fourth query in the first test case. String s[4... 6] = «aba». Its palindrome substrings are: «a», «b», «a», «aba».

package main

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

const N = 5005
var n, q int
var s string
var f[N][N] bool
var cnt[N][N] int
var ret[N][N] int

func main()  {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	fmt.Fscanf(reader, "%s\n", &s)
	n = len(s)
	fmt.Fscanf(reader, "%d\n",  &q)
	for i := 1; i <= n; i++ {
		f[i][i] = true
	}

	for i := 1; i < n; i++ {
		if s[i - 1] == s[i] {
			f[i][i + 1] = true
		}
	}
	for d := 3; d <= n; d++ {
		for i := 1; i <= n - d + 1; i++ {
			var j = i + d - 1
			if s[i - 1] == s[j - 1] {
				f[i][j] = f[i + 1][j - 1]
			}
		}
	}

	//fmt.Println(f[1])

	for i := 1; i <= n; i++ {
		for j := i; j <= n; j++ {
			cnt[i][j] = cnt[i][j - 1]
			if f[i][j] {
				cnt[i][j]++
			}
		}
		//fmt.Println("cnt[", i, "]=", cnt[i])
	}

	for j := 1; j <= n; j++ {
		for i := j; i >= 1; i-- {
			ret[i][j] = ret[i + 1][j] + cnt[i][j]
		}
	}

	for kk := 0; kk < q; kk++ {
		var u, v int
		fmt.Fscanf(reader, "%d %d\n", &u, &v)
		fmt.Fprintf(writer, "%d\n", ret[u][v])
	}

}


You might have remembered Theatre square from the problem 1A. Now it's finally getting repaved.The square still has a rectangular shape of $$$n \times m$$$ meters. However, the picture is about to get more complicated now. Let $$$a_{i,j}$$$ be the $$$j$$$-th square in the $$$i$$$-th row of the pavement.You are given the picture of the squares:  if $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black;  if $$$a_{i,j} = $$$ ".", then the $$$j$$$-th square in the $$$i$$$-th row should be white. The black squares are paved already. You have to pave the white squares. There are two options for pavement tiles:  $$$1 \times 1$$$ tiles — each tile costs $$$x$$$ burles and covers exactly $$$1$$$ square;  $$$1 \times 2$$$ tiles — each tile costs $$$y$$$ burles and covers exactly $$$2$$$ adjacent squares of the same row. Note that you are not allowed to rotate these tiles or cut them into $$$1 \times 1$$$ tiles. You should cover all the white squares, no two tiles should overlap and no black squares should be covered by tiles.What is the smallest total price of the tiles needed to cover all the white squares?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of testcases. Then the description of $$$t$$$ testcases follow. The first line of each testcase contains four integers $$$n$$$, $$$m$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 100$$$; $$$1 \le m \le 1000$$$; $$$1 \le x, y \le 1000$$$) — the size of the Theatre square, the price of the $$$1 \times 1$$$ tile and the price of the $$$1 \times 2$$$ tile. Each of the next $$$n$$$ lines contains $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th line is $$$a_{i,j}$$$. If $$$a_{i,j} = $$$ "*", then the $$$j$$$-th square in the $$$i$$$-th row should be black, and if $$$a_{i,j} = $$$ ".", then the $$$j$$$-th square in the $$$i$$$-th row should be white. It's guaranteed that the sum of $$$n \times m$$$ over all testcases doesn't exceed $$$10^5$$$.

For each testcase print a single integer — the smallest total price of the tiles needed to cover all the white squares in burles.

NoteIn the first testcase you are required to use a single $$$1 \times 1$$$ tile, even though $$$1 \times 2$$$ tile is cheaper. So the total price is $$$10$$$ burles.In the second testcase you can either use two $$$1 \times 1$$$ tiles and spend $$$20$$$ burles or use a single $$$1 \times 2$$$ tile and spend $$$1$$$ burle. The second option is cheaper, thus the answer is $$$1$$$.The third testcase shows that you can't rotate $$$1 \times 2$$$ tiles. You still have to use two $$$1 \times 1$$$ tiles for the total price of $$$20$$$.In the fourth testcase the cheapest way is to use $$$1 \times 1$$$ tiles everywhere. The total cost is $$$6 \cdot 3 = 18$$$.

package main

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

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 main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		line := readNNums(reader, 4)
		n, m, x, y := line[0], line[1], line[2], line[3]
		B := make([][]byte, n)
		for i := 0; i < n; i++ {
			B[i], _ = reader.ReadBytes('\n')
		}
		fmt.Println(solve(n, m, B, x, y))
	}
}

func solve(n, m int, board [][]byte, x, y int) int64 {
	// row independent with each other
	X, Y := int64(x), int64(y)
	find := func(r int) int64 {
		// 前两个cell的cost
		var A, B int64

		for j := 0; j < m; j++ {
			if board[r][j] == '*' {
				A = B
				continue
			}
			if j == 0 || board[r][j-1] == '*' {
				// 只能使用x
				A, B = B, B+X
				continue
			}
			//也能使用y
			A, B = B, min(A+Y, B+X)
		}
		return B
	}

	var res int64

	for i := 0; i < n; i++ {
		res += find(i)
	}

	return res
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscanf(in, "%d\n", &t)
	for tt := 0; tt < t; tt++ {
		n, m, x, y := 0, 0, 0, 0
		fmt.Fscanf(in, "%d %d %d %d\n", &n, &m, &x, &y)
		if y > 2*x {
			y = 2 * x
		}
		ans := 0
		s := ""
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%s\n", &s)
			cur := 0
			for j := 0; j < len(s); j++ {
				if s[j] == '*' {
					ans += cur/2*y + cur%2*x
					cur = 0
				} else {
					cur++
				}
			}
			ans += cur/2*y + cur%2*x
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

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

package main

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

func main() {
	s := NewScanner()
	t := s.NextInt()

	for i := 0; i < t; i++ {
		n, _, x, y := s.NextInt(), s.NextInt(), s.NextInt(), s.NextInt()

		if y > 2*x {
			y = 2 * x
		}

		var total int
		var have bool
		for j := 0; j < n; j++ {
			have = false
			a := s.NextString()
			for _, c := range a {
				if c == '.' {
					if have {
						total += y
						have = false
					} else {
						have = true
					}
				} else {
					if have {
						total += x
					}
					have = false
				}
			}

			if have {
				total += x
			}
		}

		fmt.Println(total)
	}
}

type scanner struct {
	*bufio.Scanner
}

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

func (s *scanner) NextInt() int {
	s.Scan()
	i, _ := strconv.Atoi(s.Text())
	return i
}

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

func (s *scanner) NextString() string {
	s.Scan()
	return s.Text()
}

// https://codeforces.com/contest/1358/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m, x,y int
	fmt.Fscan(input, &n, &m, &x, &y)
	sc := make([]int, 0)
	for i := 0; i < n; i++ {
	  var s string
	  fmt.Fscan(input, &s)
	  cc := 0
	  for j := 0; j < len(s); j++ {
	    if s[j] == '.' {
	      cc++
	      continue
	    }
	    if cc > 0 {
		sc = append(sc, cc)
	    }
	    cc = 0
	  }
	  if cc > 0 {
		sc = append(sc, cc)
	  }
	}
	//fmt.Println(x,y, sc)
	ans := 0
	for i := 0; i < len(sc); i++ {
	  ans += min(((sc[i]/2)*2)*x, (sc[i]/2) * y)
	  if sc[i] % 2 > 0 {
	    ans+=x
	  }
	}

	fmt.Println(ans)

}

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

package main

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

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.NextInt64()
	for ; t > 0; t-- {
		n, m, x, y := reader.NextInt(), reader.NextInt(), reader.NextInt(),reader.NextInt()
		aa := make([][]string, n)
		for i:=range aa {
			aa[i]=make([]string,m)
		}
		for i:=0;i<len(aa);i++{
			input:=reader.NextString()
			inputarr:=strings.Split(input,"")
			for j:=0;j<len(aa[0]);j++{
				aa[i][j]=inputarr[j]
			}
		}
		// for i := 0; i < len(aa); i++ {
		// 	aa[i] = reader.NextInt64()
		// }
		writer.Printf("%d\n", sol(n,m,x,y,aa))
	}
}

func sol(n,m,x,y int, arr [][]string) int {
	// if m==0 {
	// 	return 0;
	// }
	dots:=0
	pairs:=0
	for i:=0;i<len(arr);i++ {
		for j:=0;j<len(arr[0]);j++{
			if(arr[i][j]=="*"){
				continue;
			}
			if(arr[i][j]=="."){
				if j!=len(arr[0])-1 && arr[i][j+1]=="."{
					pairs++
					arr[i][j]="-"
					arr[i][j+1]="-"
				}else{
					dots++
					arr[i][j]="-"
				}
			}
		}
	}
	if(y>=2*x){
		return x*dots+ 2*pairs*x
	}
	return dots*x + pairs*y

}

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	NextInt() int
	NextInt64() int64
	NextString() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) NextInt() int {
	impl.Scan()
	bb := impl.Bytes()
	i := 0
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextInt64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) NextString() string {
	impl.Scan()
	return impl.Text()
}
/*********************** Utils ***********************/

func S(v interface{}) string { return fmt.Sprintf("%v", v) }
func SArr(a []int) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.Itoa(a[i]) + " ")
	}
	return sb.String()
}
func SArr64(a []int64) string {
	sb := strings.Builder{}
	sb.Grow(len(a) * 9)
	for i := range a {
		sb.WriteString(strconv.FormatInt(a[i], 10) + " ")
	}
	return sb.String()
}
func MnMx(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int64) int64 { mn, _ := MnMx(args...); return mn }
func Mx(args ...int64) int64 { _, mx := MnMx(args...); return mx }
func LCM(a, b int64) int64   { return a / GCD(a, b) * b }
func GCD(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func sortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

package main

import (
	"fmt"
)

func main() {
	var t, m, n, x, y, ans, single, double int
	var tile string
	fmt.Scanf("%d\n", &t)

	for j := 0; j < t; j++ {
		fmt.Scanf("%d %d %d %d\n", &n, &m, &x, &y)
		single = 0
		double = 0
		for i := 0; i < n; i++ {
			fmt.Scanf("%s\n", &tile)
			var prev byte
			for k := 0; k < m; k++ {
				if tile[k] == '.' {
					if prev == '.' {
						single--
						double++
						prev = '*'
					} else {
						single++
						prev = '.'
					}
				} else {
					prev = '*'
				}
			}
		}
		if x*2 <= y {
			ans = x*single + 2*x*double
		} else {
			ans = x*single + y*double
		}
		fmt.Println(ans)
	}
}

/*
URL:
https://codeforces.com/contest/1359/problem/B
*/

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 (
	t int

	n, m, x, y int64
	S          [][]rune
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		n, m, x, y = ReadInt64_4()
		S = [][]rune{}
		for j := int64(0); j < n; j++ {
			row := ReadRuneSlice()
			S = append(S, row)
		}

		solve()
	}
}

func solve() {
	if 2*x <= y {
		num := int64(0)
		for i := int64(0); i < n; i++ {
			for j := int64(0); j < m; j++ {
				if S[i][j] == '.' {
					num++
				}
			}
		}

		fmt.Println(num * x)
		return
	}

	ans := int64(0)
	for i := int64(0); i < n; i++ {
		comp, cnts := RunLengthEncoding(S[i])
		for j := 0; j < len(comp); j++ {
			if comp[j] == '.' {
				num := int64(cnts[j])
				c := num / 2

				ans += y * c
				if num%2 == 1 {
					ans += x
				}
			}
		}
	}
	fmt.Println(ans)
}

// RunLengthEncoding returns encoded slice of an input.
func RunLengthEncoding(S []rune) ([]rune, []int) {
	runes := []rune{}
	lengths := []int{}

	l := 0
	for i := 0; i < len(S); i++ {
		// 1文字目の場合保持
		if i == 0 {
			l = 1
			continue
		}

		if S[i-1] == S[i] {
			// 直前の文字と一致していればインクリメント
			l++
		} else {
			// 不一致のタイミングで追加し、長さをリセットする
			runes = append(runes, S[i-1])
			lengths = append(lengths, l)
			l = 1
		}
	}
	runes = append(runes, S[len(S)-1])
	lengths = append(lengths, l)

	return runes, lengths
}

// RunLengthDecoding decodes RLE results.
func RunLengthDecoding(S []rune, L []int) []rune {
	if len(S) != len(L) {
		panic("S, L are not RunLengthEncoding results")
	}

	res := []rune{}

	for i := 0; i < len(S); i++ {
		for j := 0; j < L[i]; j++ {
			res = append(res, S[i])
		}
	}

	return res
}

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

/*********** 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 (
	"bufio"
	"fmt"
	"os"
)

var nTest, n, m, x, y int
var a [100][1000]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &nTest)

	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &m, &x, &y)
		fmt.Fscanln(in)
		cntDot := 0

		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscanf(in, "%c", &a[i][j])
				if a[i][j] == '.' {
					cntDot++
				}
			}
			fmt.Fscanln(in)
		}

		if 2*x <= y {
			fmt.Fprintln(out, x*cntDot)
			continue
		}

		cnt1x2 := 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == '.' && j < m-1 && a[i][j+1] == '.' {
					a[i][j] = '*'
					a[i][j+1] = '*'
					cnt1x2++
				}
			}
		}
		fmt.Fprintln(out, x*(cntDot-cnt1x2*2)+y*cnt1x2)
	}
	out.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var T, n, m, x, y int
	var s string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m, &x, &y)
		if y > 2*x {
			y = 2 * x
		}
		ans := 0
		for ; n > 0; n-- {
			Fscan(in, &s)
			for _, s := range strings.Split(s, "*") {
				if s != "" {
					ans += len(s)/2*y + len(s)&1*x
				}
			}
		}
		Println(ans)
	}
}

package main

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

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%v\n", &t)
	for now := 0; now < t; now++ {
		var n, m, x, y int
		fmt.Fscanf(in, "%v %v %v %v\n", &n, &m, &x, &y)
		var cc, c int
		for i := 0;i < n;i++ {
			var s string
			fmt.Fscanf(in, "%v\n", &s)
			var l int
			for j := 0;j < m;j++ {
				if s[j] == '.' {
					if l == 1 {
						l = 0
						cc++
					} else {
						l = 1
					}
				} else {
					if l == 1 {
						c++
					}
					l = 0
				}
			}
			if l == 1 {
				c++
			}
		}
		fmt.Fprintln(out, min(x * (2 * cc + c), y * cc + c * x))
	}
	return
}

package main

import "fmt"
import "bufio"
import "os"

func main() {
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)

	var n, m, x, y int

	for i:=0; i< t; i++ {
		fmt.Fscanf(buf, "%d", &n)
		fmt.Fscanf(buf, "%d", &m)
		fmt.Fscanf(buf, "%d", &x)
		fmt.Fscanf(buf, "%d\n", &y)

		var s string
		sum := 0
		for j := 0; j< n; j++ {
			fmt.Fscanf(buf, "%s\n", &s)
			sum += getMinCost(s, x, y)
		}
		fmt.Println(sum)
	}
}

func getMinCost(s string, x, y int) int {

	cost := 0

	for i:=0; i<len(s); i++ {
		if i+1 <= len(s) -1 && s[i] == '.' && s[i+1] == '.' {
			c := min(x*2, y);
			cost += c;
			i++;
		} else if s[i]=='.' {
			cost += x;
		}
	}

	return cost
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

package main

import (
	"fmt"
	"bufio"
	"os"
)
var fin *bufio.Reader = bufio.NewReader(os.Stdin)
var fout *bufio.Writer = bufio.NewWriter(os.Stdout)
func max(a int, b int) int { if(a > b) { return a } else { return b }}
func main() {
	defer fout.Flush()
	var asdf int
	fmt.Fscan(fin, &asdf)
	for ; asdf > 0; asdf-- {
		var n,m,x,y int
		fmt.Fscan(fin, &n, &m, &x, &y)
		if 2*x < y {
			y = 2*x
		}
		ans := 0
		for ; n > 0; n-- {
			var s string
			fmt.Fscan(fin, &s)
			for i := 0; i < m; i++ {
				if s[i] == '*' {
					continue
				}
				if i != m-1 && s[i+1] == '.' {
					ans += y
					i++
				} else {
					ans += x
				}
			}
		}
		fmt.Fprintln(fout, ans)
	}
}

package main

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

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

	var t int
	fmt.Fscan(r, &t)
	for ; t > 0; t-- {
		var n, m, x, y int
		fmt.Fscan(r, &n, &m, &x, &y)

		var ans int
		for i := 0; i < n; i++ {
			var s string
			fmt.Fscan(r, &s)
			var j int
			for j < m {
				if s[j] == '.' {
					if j+1 < m && s[j+1] == '.' {
						if x*2 > y {
							ans += y
						} else {
							ans += x * 2
						}
						j += 2
					} else {
						ans += x
						j++
					}
				} else {
					j++
				}
			}
		}

		fmt.Fprintln(w, ans)
	}
}

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for i := 0; i < t; i++ {
		testcase(scanner, writer)
	}
}

func testcase(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	m := getNextInt(scanner)
	x := getNextInt(scanner)
	y := getNextInt(scanner)
	var ans int
	for i := 0; i < n; i++ {
		s := getNextString(scanner)
		for j := 0; j < m; j++ {
			if s[j] == '*' {
				continue
			}
			if strings.Index(s[j:], "..") == 0 {
				ans += minint(x<<1, y)
				j++
				continue
			}
			ans += x
		}
	}
	fmt.Fprintln(writer, ans)
}

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

package main

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

type BufferedWriter interface {
	Printf(format string, a ...interface{})
	Flush()
}

type writerImpl struct {
	*bufio.Writer
}

func NewBufferedWriter(writer io.Writer) BufferedWriter {
	return &writerImpl{Writer: bufio.NewWriter(writer)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

type WordScanner interface {
	Int64() int64
	UInt64() uint64
	String() string
}

type wordScannerImpl struct {
	*bufio.Scanner
}

func NewWordScanner(reader io.Reader) WordScanner {
	s := bufio.NewScanner(reader)
	s.Split(bufio.ScanWords)
	// adjust the following size as needed
	// 2 << 17 is good enough to scan strings of len 2*10^5
	// it could go as high as 2 << 26 before cf judge breaks
	size := 2 << 20
	buf := make([]byte, size)
	s.Buffer(buf, size)
	return &wordScannerImpl{Scanner: s}
}

func (impl *wordScannerImpl) Int64() int64 {
	impl.Scan()
	bb := impl.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) UInt64() uint64 {
	impl.Scan()
	bb := impl.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func (impl *wordScannerImpl) String() string {
	impl.Scan()
	return impl.Text()
}

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

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

const (
	INF = math.MaxInt64
	MOD = 1e9 + 7
)

var reader = NewWordScanner(os.Stdin)
var writer = NewBufferedWriter(os.Stdout)

func main() {
	defer writer.Flush()
	t := reader.Int64()
	for ; t > 0; t-- {
		n := reader.Int64()
		_ = reader.Int64()
		x := reader.Int64()
		y := reader.Int64()
		a := make([][]byte, n)
		for i := range a {
			a[i] = []byte(reader.String())
		}
		writer.Printf("%d\n", sol(a, x, y))
	}
}

func sol(a [][]byte, x, y int64) int64 {
	// placing a 1x1 tile is more price efficient
	// so just count all the dots
	if x*2 <= y {
		ret := int64(0)
		for i := range a {
			for j := range a[i] {
				if a[i][j] == '.' {
					ret += x
				}
			}
		}
		return ret
	}

	// placing a 1x2 tile is more price efficient
	ret := int64(0)
	for i := range a {
		for j := range a[i] {
			if a[i][j] == '.' {
				// try horizontal 1x2 tile
				if j < len(a[i])-1 && a[i][j+1] == '.' {
					ret += y
					a[i][j] = '*'
					a[i][j+1] = '*'
				} else {
					ret += x
					a[i][j] = '*'
				}
			}
		}
	}
	return ret
}

// https://codeforces.com/contest/1358/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m, x,y int
	fmt.Fscan(input, &n, &m, &x, &y)
	sc := make([]int, n)
	for i := 0; i < n; i++ {
	  var s string
	  fmt.Fscan(input, &s)
	  for j := 0; j < len(s); j++ {
	    if s[j] == '.' {
	      sc[i]++
	    }
	  }
	}
	//fmt.Println(x,y, sc)
	ans := 0
	for i := 0; i < n; i++ {
	  ans += min(((sc[i]>>1)<<1)*x, sc[i]/2 * y)
	  if sc[i] % 2 > 0 {
	    ans+=x
	  }
	}

	fmt.Println(ans)

}

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

// https://codeforces.com/contest/1358/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m, x,y int
	fmt.Fscan(input, &n, &m, &x, &y)
	sc := make([]int, n)
	for i := 0; i < n; i++ {
	  var s string
	  fmt.Fscan(input, &s)
	  for j := 0; j < len(s); j++ {
	    if s[j] == '.' {
	      sc[i]++
	    }
	  }
	}
	//fmt.Println(x,y, sc)
	ans := 0
	for i := 0; i < n; i++ {
	  ans += min(sc[i]*x, sc[i]/2 * y)
	  if sc[i] % 2 > 0 {
	    ans+=x
	  }
	}

	fmt.Println(ans)

}

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

// https://codeforces.com/contest/1358/problem/A
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	var (
		T int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &T)
	for i := 0; i < T; i++ {
		solver()
	}
}

func solver() {
	var n, m, x,y int
	fmt.Fscan(input, &n, &m, &x, &y)
	sc := make([]int, n)
	for i := 0; i < n; i++ {
	  var s string
	  fmt.Fscan(input, &s)
	  for j := 0; j < len(s); j++ {
	    if s[j] == '.' {
	      sc[i]++
	    }
	  }
	}
	//fmt.Println(x,y, sc)
	ans := 0
	for i := 0; i < n; i++ {
	  ans += min(sc[i]/2*x, sc[i]/2 * y)
	  if sc[i] % 2 > 0 {
	    ans+=x
	  }
	}

	fmt.Println(ans)

}

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

package main

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

var nTest, n, m, x, y int
var a [100][1000]int

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &nTest)

	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &m, &x, &y)
		fmt.Fscanln(in)
		cntDot := 0

		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				fmt.Fscanf(in, "%c", &a[i][j])
				if a[i][j] == '.' {
					cntDot++
				}
			}
			fmt.Fscanln(in)
		}

		if 2*x <= y {
			fmt.Fprintln(out, x*cntDot)
			out.Flush()
			return
		}

		cnt1x2 := 0
		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				if a[i][j] == '.' && j < m-1 && a[i][j+1] == '.' {
					a[i][j] = '*'
					a[i][j+1] = '*'
					cnt1x2++
				}
			}
		}
		fmt.Fprintln(out, x*(cntDot-cnt1x2*2)+y*cnt1x2)
	}
	out.Flush()
}


A revolution took place on the Buka Island. New government replaced the old one. The new government includes n parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island.The island can be conventionally represented as two rectangles a × b and c × d unit squares in size correspondingly. The rectangles are located close to each other. At that, one of the sides with the length of a and one of the sides with the length of c lie on one line. You can see this in more details on the picture.  The i-th party is entitled to a part of the island equal to xi unit squares. Every such part should fully cover several squares of the island (it is not allowed to cover the squares partially) and be a connected figure. A "connected figure" presupposes that from any square of this party one can move to any other square of the same party moving through edge-adjacent squares also belonging to that party.Your task is to divide the island between parties.

The first line contains 5 space-separated integers — a, b, c, d and n (1 ≤ a, b, c, d ≤ 50, b ≠ d, 1 ≤ n ≤ 26). The second line contains n space-separated numbers. The i-th of them is equal to number xi (1 ≤ xi ≤ a × b + c × d). It is guaranteed that .

If dividing the island between parties in the required manner is impossible, print "NO" (without the quotes). Otherwise, print "YES" (also without the quotes) and, starting from the next line, print max(b, d) lines each containing a + c characters. To mark what square should belong to what party, use lowercase Latin letters. For the party that is first in order in the input data, use "a", for the second one use "b" and so on. Use "." for the squares that belong to the sea. The first symbol of the second line of the output data should correspond to the square that belongs to the rectangle a × b. The last symbol of the second line should correspond to the square that belongs to the rectangle c × d. If there are several solutions output any.

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var a, b, c, d, n int
	fmt.Fscanf(reader, "%d %d %d %d %d\n", &a, &b, &c, &d, &n)
	var h int
	if b > d {
		h = b
	} else {
		h = d
	}
	m := make([][]byte, h)
	for i := 0;i < h;i ++ {
		m[i] = make([]byte, a + c)
		for j := 0;j < a + c;j ++ {
			m[i][j] = '.'
		}
	}
	seq := make([][2]int, 0, a * b + c * d)
	for i := b - 1; i >= 0;i -- {
		if i % 2 == 0 {
			for j := 0;j < a;j ++ {
				seq = append(seq, [2]int{i, j})
			}
		} else {
			for j := a - 1;j >= 0;j -- {
				seq = append(seq, [2]int{i, j})
			}
		}
	}
	for i := 0; i < d;i ++ {
		if i % 2 == 0 {
			for j := 0;j < c;j ++ {
				seq = append(seq, [2]int{i, j + a})
			}
		} else {
			for j := c - 1;j >= 0;j -- {
				seq = append(seq, [2]int{i, j + a})
			}
		}
	}
	count := 0
	x := make([]int, n)
	for i := 0;i < n - 1;i ++ {
		fmt.Fscanf(reader, "%d ", &x[i])
	}
	fmt.Fscanf(reader, "%d", &x[n - 1])
	for i := 0;i < n;i ++ {
		for j := 0;j < x[i];j, count = j + 1, count + 1 {
			m[seq[count][0]][seq[count][1]] = byte(i + int('a'))
		}
	}
	fmt.Printf("YES\n")
	for i := 0;i < h;i ++ {
		for j := 0;j < a + c;j ++ {
			fmt.Printf("%c", m[i][j])
		}
		fmt.Printf("\n")
	}
}

package main

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

func main() {
	bs, _ := ioutil.ReadAll(os.Stdin)
	reader := bytes.NewBuffer(bs)

	var a, b, c, d, n int
	fmt.Fscanf(reader, "%d %d %d %d %d\n", &a, &b, &c, &d, &n)
	var h int
	if b > d {
		h = b
	} else {
		h = d
	}
	m := make([][]byte, h)
	for i := 0;i < h;i ++ {
		m[i] = make([]byte, a + c)
		for j := 0;j < a + c;j ++ {
			m[i][j] = '.'
		}
	}
	seq := make([][2]int, 0, a * b + c * d)
	for i := b - 1; i >= 0;i -- {
		if i % 2 == 0 {
			for j := 0;j < a;j ++ {
				seq = append(seq, [2]int{i, j})
			}
		} else {
			for j := a - 1;j >= 0;j -- {
				seq = append(seq, [2]int{i, j})
			}
		}
	}
	for i := 0; i < d;i ++ {
		if i % 2 == 0 {
			for j := 0;j < c;j ++ {
				seq = append(seq, [2]int{i, j + a})
			}
		} else {
			for j := c - 1;j >= 0;j -- {
				seq = append(seq, [2]int{i, j + a})
			}
		}
	}
	count := 0
	x := make([]int, n)
	for i := 0;i < n - 1;i ++ {
		fmt.Fscanf(reader, "%d ", &x[i])
	}
	fmt.Fscanf(reader, "%d", &x[n - 1])
	for i := 0;i < n;i ++ {
		for j := 0;j < x[i];j, count = j + 1, count + 1 {
			m[seq[count][0]][seq[count][1]] = byte(i + int('a'))
		}
	}
	for i := 0;i < h;i ++ {
		for j := 0;j < a + c;j ++ {
			fmt.Printf("%c", m[i][j])
		}
		fmt.Printf("\n")
	}
}


You are given a grid with $$$n$$$ rows and $$$m$$$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $$$k > 0$$$ written on it, then exactly $$$k$$$ of its neighboring cells have a number greater than $$$0$$$ written on them. Note that if the number in the cell is $$$0$$$, there is no such restriction on neighboring cells.You are allowed to take any number in the grid and increase it by $$$1$$$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.Two cells are considered to be neighboring if they have a common edge.

The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 300$$$)  — the number of rows and columns, respectively. The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line $$$a_{i, j}$$$ is the number written in the $$$j$$$-th cell of the $$$i$$$-th row ($$$0 \le a_{i, j} \le 10^9$$$). It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$10^5$$$.

If it is impossible to obtain a good grid, print a single line containing "NO". Otherwise, print a single line containing "YES", followed by $$$n$$$ lines each containing $$$m$$$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them.

NoteIn the first test case, we can obtain the resulting grid by increasing the number in row $$$2$$$, column $$$3$$$ once. Both of the cells that contain $$$1$$$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $$$$$$0\;1\;0\;0$$$$$$ $$$$$$0\;2\;1\;0$$$$$$ $$$$$$0\;0\;0\;0$$$$$$ All of them are accepted as valid answers.In the second test case, it is impossible to make the grid good.In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

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

var dx = [4]int{0, 0, -1, 1}
var dy = [4]int{-1, 1, 0, 0}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("b.in")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	t := nextInt()
outer:
	for ; t > 0; t-- {
		n, m := nextInt(), nextInt()
		arr := make([][]int, n)
		for i := 0; i < n; i++ {
			arr[i] = make([]int, m)
			for j := 0; j < m; j++ {
				arr[i][j] = nextInt()
			}
		}
		for i, u := range arr {
			for j, v := range u {
				count := 0

				for k := 0; k < 4; k++ {
					nx, ny := i+dx[k], j+dy[k]
					if nx < 0 || ny < 0 || nx >= n || ny >= m {
						continue
					}
					count++
				}
				if count < v {
					printf("NO\n")
					continue outer
				}
				arr[i][j] = count
			}
		}

		printf("YES\n")
		for _, u := range arr {
			for _, v := range u {
				printf("%d ", v)
			}
			printf("\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--
		n, m := readTwoNums(reader)
		G := make([][]int, n)
		for i := 0; i < n; i++ {
			G[i] = readNNums(reader, m)
		}
		good := solve(n, m, G)
		if good {
			buf.WriteString("YES\n")
			for i := 0; i < n; i++ {
				for j := 0; j < m; j++ {
					if j < m-1 {
						buf.WriteString(fmt.Sprintf("%d ", G[i][j]))
					} else {
						buf.WriteString(fmt.Sprintf("%d\n", G[i][j]))
					}
				}
			}
		} else {
			buf.WriteString("NO\n")
		}
	}
	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, m int, G [][]int) bool {
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			var cnt int
			if i > 0 {
				cnt++
			}
			if i < n-1 {
				cnt++
			}
			if j > 0 {
				cnt++
			}
			if j < m-1 {
				cnt++
			}
			if G[i][j] > cnt {
				return false
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			var cnt int
			if i > 0 {
				cnt++
			}
			if i < n-1 {
				cnt++
			}
			if j > 0 {
				cnt++
			}
			if j < m-1 {
				cnt++
			}
			G[i][j] = cnt
		}
	}
	return true
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscan(in, &t)
	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(in, &n)
		fmt.Fscan(in, &m)
		possible := true
		for j := 1; j <= n; j++ {
			for k := 1; k <= m; k++ {
				var a int
				fmt.Fscan(in, &a)
				if a > 4 {
					possible = false
				} else if ((j == 1 && k == 1) || (j == n && k == 1) || (j == 1 && k == m) || (j == n && k == m)) && a > 2 {
					possible = false
				} else if (j == 1 || k == 1 || j == n || k == m) && a > 3 {
					possible = false
				}
			}
		}
		if !possible {
			fmt.Println("NO")
		} else {
			fmt.Println("YES")
			for j := 1; j <= n; j++ {
				for k := 1; k <= m; k++ {
					if (j == 1 && k == 1) || (j == n && k == 1) || (j == 1 && k == m) || (j == n && k == m) {
						fmt.Print(2, " ")
					} else if j == 1 || k == 1 || j == n || k == m {
						fmt.Print(3, " ")
					} else {
						fmt.Print(4, " ")
					}
				}
				fmt.Println()
			}
		}
	}
}

package main

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

func check(i, j, n, m int) int {
	cntNeighbors := 4
	// top .. bottom overflows
	if i-1 < 0 {
		cntNeighbors--
	} else if i+1 >= n {
		cntNeighbors--
	}
	// left .. right overflows
	if j-1 < 0 {
		cntNeighbors--
	} else if j+1 >= m {
		cntNeighbors--
	}
	return cntNeighbors
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		n, m := 0, 0
		good := true
		fmt.Fscan(in, &n, &m)
		grid := make([][]int, n)
		for i := 0; i < n; i++ {
			grid[i] = make([]int, m)
			for j := 0; j < m; j++ {
				a := 0
				fmt.Fscan(in, &a)
				// have already a wrong answer -> immediately leave after the new input consumption
				if !good {
					continue
				}
				cnt := check(i, j, n, m)
				if a <= cnt {
					grid[i][j] = cnt
				} else {
					good = false
				}
			}
		}
		if !good {
			fmt.Fprintln(out, "NO")
			continue
		}
		fmt.Fprintln(out, "YES")
		for i := 0; i < n; i++ {
			fmt.Fprintf(out, "%d", grid[i][0])
			for j := 1; j < m; j++ {
				fmt.Fprintf(out, " %d", grid[i][j])
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

func check(i, j, n, m int) int {
	cntNeighbors := 4
	// top .. bottom overflows
	if i-1 < 0 {
		cntNeighbors--
	} else if i+1 >= n {
		cntNeighbors--
	}
	// left .. right overflows
	if j-1 < 0 {
		cntNeighbors--
	} else if j+1 >= m {
		cntNeighbors--
	}
	return cntNeighbors
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		n, m := 0, 0
		// 'not good' grid flag
		fail := 0
		fmt.Fscan(in, &n, &m)
		grid := make([][]int, n)
		for i := 0; i < n; i++ {
			grid[i] = make([]int, m)
			for j := 0; j < m; j++ {
				a := 0
				fmt.Fscan(in, &a)
				cnt := check(i, j, n, m)
				if a <= cnt {
					grid[i][j] = cnt
				} else {
					fail |= 1
				}
			}
		}
		if fail == 1 {
			fmt.Fprintln(out, "NO")
			continue
		}
		fmt.Fprintln(out, "YES")
		for i := 0; i < n; i++ {
			fmt.Fprintf(out, "%d", grid[i][0])
			for j := 1; j < m; j++ {
				fmt.Fprintf(out, " %d", grid[i][j])
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

func check(i, j, n, m int) int {
	cntNeighbors := 4
	// top .. bottom overflows
	if i-1 < 0 {
		cntNeighbors--
	} else if i+1 >= n {
		cntNeighbors--
	}
	// left .. right overflows
	if j-1 < 0 {
		cntNeighbors--
	} else if j+1 >= m {
		cntNeighbors--
	}
	return cntNeighbors
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		n, m := 0, 0
		good := true
		fmt.Fscan(in, &n, &m)
		grid := make([][]int, n)
		// perform a full grid completion ( max value for each cell )
		for i := 0; i < n; i++ {
			grid[i] = make([]int, m)
			for j := 0; j < m; j++ {
				a := 0
				fmt.Fscan(in, &a)
				// have already a wrong answer
				// -> immediately leave after the new input consumption
				if !good {
					continue
				}
				cnt := check(i, j, n, m)
				if a <= cnt {
					grid[i][j] = cnt
				} else {
					good = false
				}
			}
		}
		if !good {
			fmt.Fprintln(out, "NO")
			continue
		}
		fmt.Fprintln(out, "YES")
		for i := 0; i < n; i++ {
			fmt.Fprintf(out, "%d", grid[i][0])
			for j := 1; j < m; j++ {
				fmt.Fprintf(out, " %d", grid[i][j])
			}
			fmt.Fprintln(out)
		}
	}
}

package main

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

/*
5
3 4
0 0 0 0
0 1 0 0
0 0 0 0
2 2
3 0
0 0
2 2
0 0
0 0
2 3
0 0 0
0 4 0
4 4
0 0 0 0
0 2 0 1
0 0 0 0
0 0 0 0

YES
0 0 0 0
0 1 1 0
0 0 0 0
NO
YES
0 0
0 0
NO
YES
0 1 0 0
1 4 2 1
0 2 0 0
1 3 1 0
*/

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

	var T, rows, columns, target int64
	var result [][]int64

	for Fscan(in, &T); T > int64(0); T-- {
		Fscan(in, &rows)
		Fscan(in, &columns)
		result = make([][]int64, rows)
		for i := int64(0); i < rows; i++ {
			result[i] = make([]int64, columns)
		}

		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				Fscan(in, &target)
				result[i][j] = target
			}
		}

		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				if i == 0 || i == rows-1 || j == 0 || j == columns-1 {
					if (i == 0 && j == 0) || (i == rows-1 && j == 0) || (i == 0 && j == columns-1) || (i == rows-1 && j == columns-1) {
						if result[i][j] > 2 {
							Fprintln(out, "No")
							goto BREAK
						} else {
							result[i][j] = 2
						}
					} else {
						if result[i][j] > 3 {
							Fprintln(out, "NO")
							goto BREAK
						} else {
							result[i][j] = 3
						}
					}
				} else {
					if result[i][j] > 4 {
						Fprintln(out, "NO")
						goto BREAK
					} else {
						result[i][j] = 4
					}
				}
			}
		}

		Fprintln(out, "YES")
		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				Fprintf(out, "%d ", result[i][j])
			}
			Fprintf(out, "\n")
		}

	BREAK:
	}

}

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

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

var err error

func nextInt() (n int) {
	for {
		_, err = scanf("%v", &n)
		if err == nil {
			return n
		}
		if err.Error() == "EOF" {
			return 0
		}
	}
}

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

var dx = [4]int{0, 0, -1, 1}
var dy = [4]int{-1, 1, 0, 0}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("b.in")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	t := nextInt()
outer:
	for ; t > 0; t-- {
		n, m := nextInt(), nextInt()
		arr := make([][]int, n)
		for i := 0; i < n; i++ {
			arr[i] = make([]int, m)
			for j := 0; j < m; j++ {
				arr[i][j] = nextInt()
			}
		}
		for i, u := range arr {
			for j, v := range u {
				if v == 0 {
					continue
				}
				count := 0
				for k := 0; k < 4; k++ {
					nx, ny := i+dx[k], j+dy[k]
					if nx < 0 || ny < 0 || nx >= n || ny >= m {
						continue
					}
					if arr[nx][ny] == 0 {
						count++
					}
				}

				if v > count {
					printf("NO\n")
					continue outer
				}
				arr[i][j] = count
			}
		}
		printf("YES\n")
		for _, u := range arr {
			for _, v := range u {
				printf("%d ", v)
			}
			printf("\n")
		}
	}
}

package main

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

/*
5
3 4
0 0 0 0
0 1 0 0
0 0 0 0
2 2
3 0
0 0
2 2
0 0
0 0
2 3
0 0 0
0 4 0
4 4
0 0 0 0
0 2 0 1
0 0 0 0
0 0 0 0

YES
0 0 0 0
0 1 1 0
0 0 0 0
NO
YES
0 0
0 0
NO
YES
0 1 0 0
1 4 2 1
0 2 0 0
1 3 1 0
*/

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

	var T, rows, columns, target int64
	var result [][]int64

	for Fscan(in, &T); T > int64(0); T-- {
		Fscan(in, &rows)
		Fscan(in, &columns)
		result = make([][]int64, rows)
		for i := int64(0); i < rows; i++ {
			result[i] = make([]int64, columns)
		}

		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				Fscan(in, &target)
				result[i][j] = target
			}
		}

		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				if i == 0 || i == rows-1 || j == 0 || j == columns-1 {
					if (i == 0 && j == 0) || (i == rows-1 && j == 0) || (i == 0 && j == columns-1) || (i == rows-1 && j == columns-1) {
						if result[i][j] > 2 {
							Fprintln(out, "FALSE")
							goto BREAK
						} else {
							result[i][j] = 2
						}
					} else {
						if result[i][j] > 3 {
							Fprintln(out, "FALSE")
							goto BREAK
						} else {
							result[i][j] = 3
						}
					}
				} else {
					if result[i][j] > 4 {
						Fprintln(out, "FALSE")
						goto BREAK
					} else {
						result[i][j] = 4
					}
				}
			}
		}

		Fprintln(out, "YES")
		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				Fprintln(out, result[i][j])
			}
		}

	BREAK:
	}

}

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

package main

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

/*
5
3 4
0 0 0 0
0 1 0 0
0 0 0 0
2 2
3 0
0 0
2 2
0 0
0 0
2 3
0 0 0
0 4 0
4 4
0 0 0 0
0 2 0 1
0 0 0 0
0 0 0 0

YES
0 0 0 0
0 1 1 0
0 0 0 0
NO
YES
0 0
0 0
NO
YES
0 1 0 0
1 4 2 1
0 2 0 0
1 3 1 0
*/

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

	var T, rows, columns, target int64
	var result [][]int64

	for Fscan(in, &T); T > int64(0); T-- {
		Fscan(in, &rows)
		Fscan(in, &columns)
		result = make([][]int64, rows)
		for i := int64(0); i < rows; i++ {
			result[i] = make([]int64, columns)
		}

		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				Fscan(in, &target)
				result[i][j] = target
			}
		}

		for i := int64(0); i < rows; i++ {
			for j := int64(0); j < columns; j++ {
				if i == 0 || i == rows-1 || j == 0 || j == columns-1 {
					if (i == 0 && j == 0) || (i == rows-1 && j == 0) || (i == 0 && j == columns-1) || (i == rows-1 && j == columns-1) {
						if result[i][j] > 2 {
							Fprintln(out, "No")
							goto BREAK
						} else {
							result[i][j] = 2
						}
					} else {
						if result[i][j] > 3 {
							Fprintln(out, "NO")
							goto BREAK
						} else {
							result[i][j] = 3
						}
					}
				} else {
					if result[i][j] > 4 {
						Fprintln(out, "NO")
						goto BREAK
					} else {
						result[i][j] = 4
					}
				}
			}
		}

		Fprintln(out, "YES")
		for i := int64(0); i < rows; i++ {
			Fprintln(out, result[i])
		}

	BREAK:
	}

}

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


Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the $$$i$$$-th of them will start during the $$$x_i$$$-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes $$$x_1, x_2, \dots, x_n$$$, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute).Ivan can choose two properties for the alarm clock — the first minute it will ring (let's denote it as $$$y$$$) and the interval between two consecutive signals (let's denote it by $$$p$$$). After the clock is set, it will ring during minutes $$$y, y + p, y + 2p, y + 3p$$$ and so on.Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of $$$p$$$. He has to pick it among the given values $$$p_1, p_2, \dots, p_m$$$ (his phone does not support any other options for this setting).So Ivan has to choose the first minute $$$y$$$ when the alarm clock should start ringing and the interval between two consecutive signals $$$p_j$$$ in such a way that it will ring during all given minutes $$$x_1, x_2, \dots, x_n$$$ (and it does not matter if his alarm clock will ring in any other minutes).Your task is to tell the first minute $$$y$$$ and the index $$$j$$$ such that if Ivan sets his alarm clock with properties $$$y$$$ and $$$p_j$$$ it will ring during all given minutes $$$x_1, x_2, \dots, x_n$$$ or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any.

The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 3 \cdot 10^5, 1 \le m \le 3 \cdot 10^5$$$) — the number of events and the number of possible settings for the interval between signals. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \le x_i \le 10^{18}$$$), where $$$x_i$$$ is the minute when $$$i$$$-th event starts. It is guaranteed that all $$$x_i$$$ are given in increasing order (i. e. the condition $$$x_1 < x_2 < \dots < x_n$$$ holds). The third line of the input contains $$$m$$$ integers $$$p_1, p_2, \dots, p_m$$$ ($$$1 \le p_j \le 10^{18}$$$), where $$$p_j$$$ is the $$$j$$$-th option for the interval between two consecutive signals.

If it's impossible to choose such values $$$y$$$ and $$$j$$$ so all constraints are satisfied, print "NO" in the first line. Otherwise print "YES" in the first line. Then print two integers $$$y$$$ ($$$1 \le y \le 10^{18}$$$) and $$$j$$$ ($$$1 \le j \le m$$$) in the second line, where $$$y$$$ is the first minute Ivan's alarm clock should start ringing and $$$j$$$ is the index of the option for the interval between two consecutive signals (options are numbered from $$$1$$$ to $$$m$$$ in the order they are given input). These values should be chosen in such a way that the alarm clock will ring during all given minutes $$$x_1, x_2, \dots, x_n$$$. If there are multiple answers, you can print any.

package main

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

func solve(j Jam) {
	n, m := j.Int(), j.Int()
	x := make([]uint64, n, n)
	periodGcd := uint64(0)
	for i := range x {
		x[i] = uint64(j.Int())
		if i > 0 {
			period := x[i] - x[i-1]
			if i == 1 {
				periodGcd = period
			} else {
				periodGcd = gcd(periodGcd, period)
			}
		}
	}
	for i := int64(1); i <= m; i++ {
		pi := uint64(j.Int())
		modulo := x[0] % pi
		if periodGcd%pi > 0 {
			continue
		}
		valid := true
		for _, xi := range x {
			if xi%pi != modulo {
				valid = false
				break
			}
		}
		if valid {
			j.P("YES\n%d %d\n", x[0], i)
			return
		}
	}
	j.P("NO\n")
}

func main() {
	jam, closeFunc := JamNew()
	defer closeFunc()
	solve(jam)
}

type Jam interface {
	Scanner() *bufio.Scanner
	Writer() *bufio.Writer
	Close()

	Str() string
	Int() int64
	Float() float64

	P(format string, values ...interface{})
	PF(format string, values ...interface{})
}

func JamNew() (Jam, func()) {
	if len(os.Args) > 1 {
		panic("running with input file path is not supported")
	}

	var scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 0, 1024*1024), 1024*1024)

	var writer = bufio.NewWriter(os.Stdout)
	jam := &jam{scanner, writer}
	return jam, jam.Close
}

type jam struct {
	sc *bufio.Scanner
	wr *bufio.Writer
}

func (j *jam) Close() {
	if err := j.wr.Flush(); err != nil {
		panic(err)
	}
}

func (j *jam) Scanner() *bufio.Scanner {
	return j.sc
}

func (j *jam) Writer() *bufio.Writer {
	return j.wr
}

func (j *jam) Str() string {
	if !j.sc.Scan() {
		panic("failed to scan next token")
	}

	return j.sc.Text()
}

func (j *jam) Int() int64 {
	if !j.sc.Scan() {
		panic("failed to scan next token")
	}

	res, err := strconv.ParseInt(j.sc.Text(), 10, 64)
	if err != nil {
		panic(err)
	}

	return res
}

func (j *jam) Float() float64 {
	j.sc.Scan()
	res, err := strconv.ParseFloat(j.sc.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}

func (j *jam) P(format string, values ...interface{}) {
	_, err := fmt.Fprintf(j.wr, format, values...)
	if err != nil {
		panic(err)
	}
}

func (j *jam) PF(format string, values ...interface{}) {
	_, err := fmt.Fprintf(j.wr, format, values...)
	if err != nil {
		panic(err)
	}
	if err = j.wr.Flush(); err != nil {
		panic(err)
	}
}

func gcd(x, y uint64) uint64 {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

/*
URL:
https://codeforces.com/problemset/problem/1155/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 (
	n, m int
	X, P []int64
)

func main() {
	n, m = ReadInt2()
	X = ReadInt64Slice(n)
	P = ReadInt64Slice(m)

	g := int64(0)
	for i := 1; i < n; i++ {
		g = Gcd(g, X[i]-X[i-1])
	}

	p := -1
	for i := 0; i < m; i++ {
		if g%P[i] == 0 {
			p = i + 1
			break
		}
	}

	if p == -1 {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")
	fmt.Println(X[0], p)
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int64) int64 {
	if a < 0 || b < 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	if a == 0 {
		return b
	}
	if b == 0 {
		return a
	}

	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int64) int64 {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

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

/*********** 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 (
	"bufio"
	"fmt"
	"os"
)

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

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

func getGcd(a, b int64) int64 {
	for b > 0 {
		a, b = b, a%b
	}
	return a
}

func main() {
	defer writer.Flush() // STDOUT MUST BE FLUSHED MANUALLY!!!
	var n, m int64
	scan(&n, &m)
	evt, intr := make([]int64, n), make([]int64, m)

	for i := int64(0); i < n; i++ {
		scan(&evt[i])

		if i != 0 {
			evt[i] -= evt[0]
		}
	}

	for i := int64(0); i < m; i++ {
		scan(&intr[i])
	}

	// SOLVE
	yval := evt[0]
	evt[0] = 0
	gcd := int64(-1)

	for idx, val := range evt {
		if idx == 0 {
			continue
		}
		if idx == 1 {
			gcd = val
			continue
		}

		gcd = getGcd(gcd, val)

		if gcd == 1 {
			break
		}
	}

	// Find GCD (or a multiplier) in the pointers.

	for idx, val := range intr {
		if gcd%val == 0 {
			print("YES\n")
			printf("%d %d", yval, idx+1)
			return
		}
	}

	print("NO\n")
}