package main

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

const mod int64 = 1e9 + 7

func binpow(a, n int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func sieve(n int) []int64 {
	mindiv := make([]int64, n+1)
	for x := 1; x <= n; x++ {
		mindiv[x] = int64(x)
	}

	for x := 2; x*x <= n; x++ {
		if mindiv[x] == int64(x) {
			for y := x * x; y <= n; y += x {
				mindiv[y] = int64(x)
			}
		}
	}

	return mindiv
}

var mindiv []int64 = sieve(2e5 + 1)

func factor(n int64) map[int64]int64 {
	degs := make(map[int64]int64)
	for n > 1 {
		md := mindiv[n]
		var deg int64
		for n%md == 0 {
			deg++
			n /= md
		}
		degs[md] = deg
	}
	return degs
}

func solveD(b []int64, qq [][2]int64) {
	a := make([]map[int64]int64, len(b))
	for i := range b {
		a[i] = make(map[int64]int64)
		for k, v := range factor(b[i]) {
			a[i][k] = v
		}
	}

	for _, q := range qq {
		i, x := q[0], q[1]
		for k, v := range factor(x) {
			a[i][k] += v
		}
	}

	minDeg := make(map[int64]int64)
	for k, v := range a[0] {
		minDeg[k] = v
	}
	for i := 1; i < len(a); i++ {
		for p, now := range a[i] {
			if now < minDeg[p] {
				minDeg[p] = now
			}
		}
		for p := range minDeg {
			if a[i][p] < minDeg[p] {
				minDeg[p] = a[i][p]
			}
		}
	}

	var res int64 = 1
	for k, v := range minDeg {
		res *= binpow(k, v)
		res %= mod
	}

	ans := make([]int64, 0)
	ans = append(ans, res)

	for i := len(qq) - 1; i >= 1; i-- {
		j, x := qq[i][0], qq[i][1]

		for p, d := range factor(x) {
			prevMin := minDeg[p]
			a[j][p] -= d
			if prevMin > a[j][p] {
				minDeg[p] = a[j][p]

				if minDeg[p] < 0 {
					panic("123")
				}

				diff := prevMin - minDeg[p]
				res *= binpow(p, diff*(mod-2))
				res %= mod
			}
		}

		ans = append(ans, res%mod)
	}

	for i := len(ans) - 1; i >= 0; i-- {
		printf("%d\n", ans[i])
	}
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		var n, q int
		scan(&n, &q)
		b := make([]int64, n)
		for i := range b {
			scan(&b[i])
		}
		qq := make([][2]int64, q)
		for i := range qq {
			scan(&qq[i][0], &qq[i][1])
			qq[i][0]--
		}
		solveD(b, qq)
	}
}

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

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1493D(_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 = 2e5
	lpf := [mx + 1]int{}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	n, q := r(), r()
	cnts := make([]map[int]int, n)
	for i := range cnts {
		cnts[i] = map[int]int{}
	}
	cc := [mx + 1][]int{}
	g := int64(1)
	add := func(i, v int) {
		cnt := cnts[i]
		for v > 1 {
			for p := lpf[v]; lpf[v] == p; v /= p {
				c := cnt[p]
				cnt[p]++
				if len(cc[p]) <= c {
					cc[p] = append(cc[p], 0)
				}
				if cc[p][c]++; cc[p][c] == n {
					g = g * int64(p) % (1e9 + 7)
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		add(i, r())
	}
	for ; q > 0; q-- {
		add(r()-1, r())
		Fprintln(out, g)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1493D(_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 = 2e5
	primes := make([]int, 0, 17984)
	lpf := [mx + 1]int{}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			primes = append(primes, i)
			lpf[i] = i
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
		}
	}

	n, q := r(), r()
	dCnt := [mx + 1][]int{}
	posCnt := make([]map[int]int, n)
	for i := range posCnt {
		posCnt[i] = map[int]int{}
	}
	g := int64(1)
	add := func(i, v int) {
		pc := posCnt[i]
		for v > 1 {
			for p := lpf[v]; lpf[v] == p; v /= p {
				c := pc[p]
				pc[p]++
				if len(dCnt[p]) <= c {
					dCnt[p] = append(dCnt[p], 0)
				}
				if dCnt[p][c]++; dCnt[p][c] == n {
					g = g * int64(p) % (1e9 + 7)
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		add(i, r())
	}
	for ; q > 0; q-- {
		add(r()-1, r())
		Fprintln(out, g)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1493D(_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 = 2e5
	primes := make([]int, 0, 17984)
	lpf := [mx + 1]int{}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			primes = append(primes, i)
			lpf[i] = i
		}
		for _, p := range primes {
			if p*i > mx {
				break
			}
			lpf[p*i] = p
			if i%p == 0 {
				break
			}
		}
	}

	n, q := r(), r()
	dCnt := [mx + 1][]int{}
	posCnt := make([]map[int]int, n)
	for i := range posCnt {
		posCnt[i] = map[int]int{}
	}
	g := int64(1)
	add := func(i, v int) {
		pc := posCnt[i]
		for v > 1 {
			for p := lpf[v]; lpf[v] == p; v /= p {
				c := pc[p]
				pc[p]++
				if len(dCnt[p]) <= c {
					dCnt[p] = append(dCnt[p], 0)
				}
				if dCnt[p][c]++; dCnt[p][c] == n {
					g = g * int64(p) % (1e9 + 7)
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		add(i, r())
	}
	for ; q > 0; q-- {
		add(r()-1, r())
		Fprintln(out, g)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1493D(_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 = 2e5
	lpf := [mx + 1]int{}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	n, q := r(), r()
	dCnt := [mx + 1][]int{}
	posCnt := make([]map[int]int, n)
	for i := range posCnt {
		posCnt[i] = map[int]int{}
	}
	g := int64(1)
	add := func(i, v int) {
		pc := posCnt[i]
		for v > 1 {
			for p := lpf[v]; lpf[v] == p; v /= p {
				c := pc[p]
				pc[p]++
				if len(dCnt[p]) <= c {
					dCnt[p] = append(dCnt[p], 0)
				}
				if dCnt[p][c]++; dCnt[p][c] == n {
					g = g * int64(p) % (1e9 + 7)
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		add(i, r())
	}
	for ; q > 0; q-- {
		add(r()-1, r())
		Fprintln(out, g)
	}
}

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

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
type vi struct{ v, i int }
type hp []*vi

func (h hp) Len() int            { return len(h) }
func (h hp) Less(i, j int) bool  { return h[i].v < h[j].v }
func (h hp) Swap(i, j int)       { h[i], h[j] = h[j], h[i]; h[i].i = i; h[j].i = j }
func (h *hp) Push(v interface{}) { *h = append(*h, v.(*vi)) }
func (h *hp) Pop() interface{}   { return nil }
func (h *hp) push(v int) *vi     { p := &vi{v, len(*h)}; heap.Push(h, p); return p }

func CF1493D(_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 = 2e5
	lpf := [mx + 1]int{}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	const mod int64 = 1e9 + 7
	pow := func(x int64, n int) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}

	n, q := r(), r()
	hs := [mx + 1]hp{}
	ps := make([]map[int]*vi, n)
	for i := range ps {
		ps[i] = map[int]*vi{}
		v := r()
		for v > 1 {
			p := lpf[v]
			e := 1
			for v /= p; lpf[v] == p; v /= p {
				e++
			}
			ps[i][p] = hs[p].push(e)
		}
	}
	g := int64(1)
	for p, h := range hs[:] {
		if len(h) == n {
			g = g * pow(int64(p), h[0].v) % mod
		}
	}
	for ; q > 0; q-- {
		i, v := r()-1, r()
		for v > 1 {
			p := lpf[v]
			e := 1
			for v /= p; lpf[v] == p; v /= p {
				e++
			}
			h := hs[p]
			before := 0
			if len(h) == n {
				before = h[0].v
			}
			if vi := ps[i][p]; vi == nil {
				ps[i][p] = h.push(e)
				hs[p] = h
			} else {
				vi.v += e
				heap.Fix(&h, vi.i)
			}
			if len(h) == n {
				g = g * pow(int64(p), h[0].v-before) % mod
			}
		}
		Fprintln(out, g)
	}
}

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

package main

import (
	"bufio"
	"container/heap"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
type vi struct{ v, i int }
type hp []*vi

func (h hp) Len() int            { return len(h) }
func (h hp) Less(i, j int) bool  { return h[i].v < h[j].v }
func (h hp) Swap(i, j int)       { h[i], h[j] = h[j], h[i]; h[i].i = i; h[j].i = j }
func (h *hp) Push(v interface{}) { *h = append(*h, v.(*vi)) }
func (h *hp) Pop() interface{}   { return nil }
func (h *hp) push(v int) *vi     { p := &vi{v, len(*h)}; heap.Push(h, p); return p }

func CF1493D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mx int = 2e5
	lpf := [mx + 1]int{}
	for i := 2; i <= mx; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}
	const mod int64 = 1e9 + 7
	pow := func(x int64, n int) int64 {
		res := int64(1)
		for ; n > 0; n >>= 1 {
			if n&1 > 0 {
				res = res * x % mod
			}
			x = x * x % mod
		}
		return res
	}

	var n, q, i, v int
	Fscan(in, &n, &q)
	hs := [mx + 1]hp{}
	ps := make([]map[int]*vi, n)
	for i := range ps {
		ps[i] = map[int]*vi{}
		Fscan(in, &v)
		for v > 1 {
			p := lpf[v]
			e := 1
			for v /= p; lpf[v] == p; v /= p {
				e++
			}
			ps[i][p] = hs[p].push(e)
		}
	}
	g := int64(1)
	for p, h := range hs[:] {
		if len(h) == n {
			g = g * pow(int64(p), h[0].v) % mod
		}
	}
	for ; q > 0; q-- {
		Fscan(in, &i, &v)
		for v > 1 {
			p := lpf[v]
			e := 1
			for v /= p; lpf[v] == p; v /= p {
				e++
			}
			h := hs[p]
			before := 0
			if len(h) == n {
				before = h[0].v
			}
			if vi := ps[i-1][p]; vi == nil {
				ps[i-1][p] = h.push(e)
				hs[p] = h
			} else {
				vi.v += e
				heap.Fix(&h, vi.i)
			}
			if len(h) == n {
				g = g * pow(int64(p), h[0].v-before) % mod
			}
		}
		Fprintln(out, g)
	}
}

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

package main

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

const mod int64 = 1e9 + 7

func binpow(a, n int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func sieve(n int) []int64 {
	mindiv := make([]int64, n+1)
	for x := 1; x <= n; x++ {
		mindiv[x] = int64(x)
	}

	for x := 2; x*x <= n; x++ {
		if mindiv[x] == int64(x) {
			for y := x * x; y <= n; y += x {
				mindiv[y] = int64(x)
			}
		}
	}

	return mindiv
}

var mindiv []int64 = sieve(2e5 + 1)

func factor(n int64) map[int64]int64 {
	degs := make(map[int64]int64)
	for n > 1 {
		md := mindiv[n]
		var deg int64
		for n%md == 0 {
			deg++
			n /= md
		}
		degs[md] = deg
	}
	return degs
}

func solveD(b []int64, qq [][2]int64) {
	a := make([]map[int64]int64, len(b))
	for i := range b {
		a[i] = make(map[int64]int64)
		for k, v := range factor(b[i]) {
			a[i][k] = v
		}
	}

	for _, q := range qq {
		i, x := q[0], q[1]
		for k, v := range factor(x) {
			a[i][k] += v
		}
	}

	minDeg := make(map[int64]int64)
	for k, v := range a[0] {
		minDeg[k] = v
	}
	for i := 1; i < len(a); i++ {
		for p, now := range a[i] {
			if prev, ok := minDeg[p]; ok && now < prev {
				minDeg[p] = now
			}
		}
	}

	var res int64 = 1
	for k, v := range minDeg {
		res *= binpow(k, v)
		res %= mod
	}

	ans := make([]int64, 0)
	ans = append(ans, res)

	for i := len(qq) - 1; i >= 1; i-- {
		j, x := qq[i][0], qq[i][1]

		for p, d := range factor(x) {
			prevMin := minDeg[p]
			a[j][p] -= d
			if prevMin > a[j][p] {
				minDeg[p] = a[j][p]

				if minDeg[p] < 0 {
					panic("123")
				}

				diff := prevMin - minDeg[p]
				res *= binpow(p, diff*(mod-2))
				res %= mod
			}
		}

		ans = append(ans, res%mod)
	}

	for i := len(ans) - 1; i >= 0; i-- {
		printf("%d\n", ans[i])
	}
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		var n, q int
		scan(&n, &q)
		b := make([]int64, n)
		for i := range b {
			scan(&b[i])
		}
		qq := make([][2]int64, q)
		for i := range qq {
			scan(&qq[i][0], &qq[i][1])
			qq[i][0]--
		}
		solveD(b, qq)
	}
}

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

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

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

const inf = 1 << 60

func gcd(a, b int64) int64 {
	for a > 0 && b > 0 {
		a, b = b, a%b
	}
	return a + b
}

type segtree []segtreenode

type segtreenode struct {
	node, l, r int

	g int64
}

func newSegtree(v []int64) segtree {
	tr := make(segtree, 4*len(v))
	tr.build(0, v, 0, len(v)-1)
	return tr
}

func (tree segtree) build(node int, v []int64, l, r int) {
	tree[node] = segtreenode{
		node: node, l: l, r: r,
	}
	if l == r {
		tree[node].g = v[l]
		return
	}
	m := l + (r-l)/2
	tree.build(2*node+1, v, l, m)
	tree.build(2*node+2, v, m+1, r)
	tree[node].g = gcd(tree[2*node+1].g, tree[2*node+2].g)
}

func (tree segtree) get(node, l, r int) int64 {
	if l > r {
		return inf
	}

	nl, nr := tree[node].l, tree[node].r

	if r < nl || nr < l {
		return inf
	}

	if l <= nl && nr <= r {
		return tree[node].g
	}

	return gcd(
		tree.get(2*node+1, l, r),
		tree.get(2*node+2, l, r),
	)
}

func (tree segtree) update(node, idx int, val int64) int64 {
	nl, nr := tree[node].l, tree[node].r

	if idx <= nl && nr <= idx {
		tree[node].g = val
	}
	return -1
}

package main

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

const mod int64 = 1e9 + 7

func binpow(a, n, mod int64) int64 {
	var res int64 = 1
	for n > 0 {
		if n&1 == 1 {
			res *= a
			res %= mod
		}
		n >>= 1
		a *= a
		a %= mod
	}
	return res % mod
}

func sieve(n int) []int64 {
	mindiv := make([]int64, n+1)
	for x := 1; x <= n; x++ {
		mindiv[x] = int64(x)
	}

	for x := 2; x*x <= n; x++ {
		if mindiv[x] == int64(x) {
			for y := x * x; y <= n; y += x {
				mindiv[y] = int64(x)
			}
		}
	}

	return mindiv
}

var mindiv []int64 = sieve(2e5 + 1)

func factor(n int64) map[int64]int64 {
	degs := make(map[int64]int64)
	for n > 1 {
		md := mindiv[n]
		var deg int64
		for n%md == 0 {
			deg++
			n /= md
		}
		degs[md] = deg
	}
	return degs
}

func solveD(b []int64, qq [][2]int64) {
	a := make([]map[int64]int64, len(b))
	for i := range b {
		a[i] = make(map[int64]int64)
		for k, v := range factor(b[i]) {
			a[i][k] = v
		}
	}

	for _, q := range qq {
		i, x := q[0], q[1]
		for k, v := range factor(x) {
			a[i][k] += v
		}
	}

	minDeg := make(map[int64]int64)
	for k, v := range a[0] {
		minDeg[k] = v
	}
	for i := 1; i < len(a); i++ {
		for k, v := range a[i] {
			if w, ok := minDeg[k]; ok && v < w {
				minDeg[k] = v
			}
		}
	}

	var res int64 = 1
	for k, v := range minDeg {
		res *= binpow(k, v, mod)
		res %= mod
	}

	ans := make([]int64, 0, len(qq))
	ans = append(ans, res)
	for i := len(qq) - 1; i >= 1; i-- {
		i, x := qq[i][0], qq[i][1]

		for p, d := range factor(x) {
			prev := minDeg[p]
			if prev > a[i][p]-d {
				a[i][p] -= d
				minDeg[p] = a[i][p]

				y := binpow(p, prev-minDeg[p], mod)
				y = binpow(y, mod-2, mod)

				res *= y
				res %= mod
			}
		}

		ans = append(ans, res)
	}

	for i := len(ans) - 1; i >= 0; i-- {
		printf("%d\n", ans[i])
	}
}

func main() {
	defer writer.Flush()
	//redirectInputToFile(reader, "input.txt")

	T := 1
	// scan(&T)
	for t := 0; t < T; t++ {
		var n, q int
		scan(&n, &q)
		b := make([]int64, n)
		for i := range b {
			scan(&b[i])
		}
		qq := make([][2]int64, q)
		for i := range qq {
			scan(&qq[i][0], &qq[i][1])
			qq[i][0]--
		}
		solveD(b, qq)
	}
}

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

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

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

const inf = 1 << 60

func gcd(a, b int64) int64 {
	for a > 0 && b > 0 {
		a, b = b, a%b
	}
	return a + b
}

type segtree []segtreenode

type segtreenode struct {
	node, l, r int

	g int64
}

func newSegtree(v []int64) segtree {
	tr := make(segtree, 4*len(v))
	tr.build(0, v, 0, len(v)-1)
	return tr
}

func (tree segtree) build(node int, v []int64, l, r int) {
	tree[node] = segtreenode{
		node: node, l: l, r: r,
	}
	if l == r {
		tree[node].g = v[l]
		return
	}
	m := l + (r-l)/2
	tree.build(2*node+1, v, l, m)
	tree.build(2*node+2, v, m+1, r)
	tree[node].g = gcd(tree[2*node+1].g, tree[2*node+2].g)
}

func (tree segtree) get(node, l, r int) int64 {
	if l > r {
		return inf
	}

	nl, nr := tree[node].l, tree[node].r

	if r < nl || nr < l {
		return inf
	}

	if l <= nl && nr <= r {
		return tree[node].g
	}

	return gcd(
		tree.get(2*node+1, l, r),
		tree.get(2*node+2, l, r),
	)
}

func (tree segtree) update(node, idx int, val int64) int64 {
	nl, nr := tree[node].l, tree[node].r

	if idx <= nl && nr <= idx {
		tree[node].g = val
	}
	return -1
}


package main

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

type node struct {
	l, r        int
	sum         int
	revChildren bool
}
type segTree []node

func (t segTree) _pushUp(o int) {
	lo, ro := t[o<<1], t[o<<1|1]
	t[o].sum = lo.sum + ro.sum
}

func (t segTree) _build(arr []int, pos uint8, o, l, r int) {
	t[o].l, t[o].r = l, r
	if l == r {
		t[o].sum = arr[l-1] >> pos & 1
		return
	}
	mid := (l + r) >> 1
	t._build(arr, pos, o<<1, l, mid)
	t._build(arr, pos, o<<1|1, mid+1, r)
	t._pushUp(o)
}

func (t segTree) _spread(o int) {
	if t[o].revChildren {
		lo, ro := &t[o<<1], &t[o<<1|1]
		lo.sum = lo.r - lo.l + 1 - lo.sum
		ro.sum = ro.r - ro.l + 1 - ro.sum
		lo.revChildren = !lo.revChildren
		ro.revChildren = !ro.revChildren
		t[o].revChildren = false
	}
}

func (t segTree) _rev(o, l, r int) {
	ol, or := t[o].l, t[o].r
	if l <= ol && or <= r {
		t[o].sum = or - ol + 1 - t[o].sum
		t[o].revChildren = !t[o].revChildren
		return
	}
	t._spread(o)
	mid := (ol + or) >> 1
	if l <= mid {
		t._rev(o<<1, l, r)
	}
	if mid < r {
		t._rev(o<<1|1, l, r)
	}
	t._pushUp(o)
}

func (t segTree) _query(o, l, r int) (res int) {
	if l <= t[o].l && t[o].r <= r {
		return t[o].sum
	}
	t._spread(o)
	mid := (t[o].l + t[o].r) >> 1
	if l <= mid {
		res += t._query(o<<1, l, r)
	}
	if mid < r {
		res += t._query(o<<1|1, l, r)
	}
	return
}

func (t segTree) init(arr []int, pos uint8) { t._build(arr, pos, 1, 1, len(arr)) }
func (t segTree) rev(l, r int)              { t._rev(1, l, r) }
func (t segTree) query(l, r int) int        { return t._query(1, l, r) }

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

	var n, q int
	Fscan(in, &n)
	arr := make([]int, n)
	for i := range arr {
		Fscan(in, &arr[i])
	}
	trees := make([]segTree, 20)
	for i := range trees {
		trees[i] = make(segTree, 4*n)
		trees[i].init(arr, uint8(i))
	}

	for Fscan(in, &q); q > 0; q-- {
		var op, l, r, x int
		Fscan(in, &op, &l, &r)
		if op == 1 {
			sum := int64(0)
			for i, t := range trees {
				sum += 1 << uint(i) * int64(t.query(l, r))
			}
			Fprintln(out, sum)
		} else {
			Fscan(in, &x)
			for i, t := range trees {
				if x>>uint(i)&1 == 1 {
					t.rev(l, r)
				}
			}
		}
	}
}

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

package main

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

type scanner struct {
	*bufio.Scanner
}

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

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

type SegmentTree []*Segment

type Segment struct {
	l, r  int
	label int
	freq  []int

	ls, rs *Segment
}

const maxbit = 25

func newSegment(l, r int) *Segment {
	return &Segment{
		l:     l,
		r:     r,
		label: 0,
		freq:  make([]int, maxbit),
		ls:    nil,
		rs:    nil,
	}
}

func (s Segment) next() []*Segment {
	var res []*Segment
	if s.ls != nil {
		res = append(res, s.ls)
	}
	if s.rs != nil {
		res = append(res, s.rs)
	}
	return res
}

func (s Segment) within(l, r int) bool {
	return (l <= s.l && s.r <= r)
}

func (s Segment) disjoint(l, r int) bool {
	return (r < s.l) || (s.r < l)
}

func (s *Segment) update(label int) {
	s.label ^= label
	n := s.r - s.l + 1
	for i, p2 := 0, 1; i < maxbit; i, p2 = i+1, p2*2 {
		if (label & p2) != 0 {
			s.freq[i] = n - s.freq[i]
		}
	}
}

func (s *Segment) populate() {
	if s.ls != nil {
		s.ls.update(s.label)
	}
	if s.rs != nil {
		s.rs.update(s.label)
	}
	s.label=0
}

func (s *Segment) refresh() {
	if s.ls == nil {
		return
	}
	for i := 0; i < maxbit; i++ {
		s.freq[i] = s.ls.freq[i] + s.rs.freq[i]
	}
}

func (s *Segment) summary() int64 {
	var res int64
	for i, p2 := 0, int64(1); i < maxbit; i, p2 = i+1, p2*2 {
		res += p2 * int64(s.freq[i])
	}
	return res
}

func Init(n int, a []int) SegmentTree {
	st := SegmentTree{}
	st = append(st, newSegment(1, n))
	for i := 0; i < len(st); i++ {
		s := st[i]
		l, r := s.l, s.r
		if l == r {
			continue
		}
		m := (l + r) / 2
		s.ls = newSegment(l, m)
		s.rs = newSegment(m+1, r)
		st = append(st, s.ls, s.rs)
	}
	return st
}

func (st SegmentTree) Query(l, r int) int64 {
	q := []*Segment{st[0]}
	var res int64

	for len(q) > 0 {
		s := q[0]
		q = q[1:]

		if s.within(l, r) {
			res += s.summary()
			continue
		}
		if s.disjoint(l, r) {
			continue
		}

		s.populate()
		q = append(q, s.next()...)
	}
	return res
}

func (st SegmentTree) Update(l, r int, label int) {
	// fmt.Printf("Updating %d %d %d\n", l, r, label)
	q := []*Segment{st[0]}
	p := []*Segment{}

	for len(q) > 0 {
		s := q[0]
		q = q[1:]

		if s.within(l, r) {
			s.update(label)
			continue
		}
		if s.disjoint(l, r) {
			continue
		}
		s.populate()
		p = append(p, s)
		q = append(q, s.next()...)
	}
	for _, s := range p {
		defer s.refresh()
	}
}

func main() {
	in := &scanner{bufio.NewScanner(os.Stdin)}
	in.Split(bufio.ScanWords)
	n := in.nextInt()
	a := make([]int, n+2)
	for i := 1; i <= n; i++ {
		a[i] = in.nextInt()
	}
	tree := Init(n, a)
	for i, v := range a {
		tree.Update(i, i, v)
	}
	// for _, s := range tree {
	// 	fmt.Println(*s)
	// }

	for m := in.nextInt(); m > 0; m-- {
		switch t := in.nextInt(); t {
		case 1:
			l, r := in.nextInt(), in.nextInt()
			// fmt.Println("Got", t, l, r)
			// fmt.Println("Answer", tree.Query(l, r))
			fmt.Println(tree.Query(l, r))
		case 2:
			l, r, x := in.nextInt(), in.nextInt(), in.nextInt()
			// fmt.Println("Got", t, l, r, x)
			// fmt.Println("Update")
			tree.Update(l, r, x)
		}
	}

}

package main

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

type scanner struct {
	*bufio.Scanner
}

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

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

var (
	label []int
	freq  [][]int
	a     []int

	n int
	m int
)

const maxbit = 24

func atof(a int) []int {
	var f []int
	for i := 0; i < maxbit; i++ {
		if (a & (1 << uint(i))) != 0 {
			f = append(f, i)
		}
	}
	return f
}

// evaluate
func eval(idx int) int64 {
	var res int64
	for i, f := range freq[idx] {
		res += (1 << uint(i)) * int64(f)
	}
	return res
}

func build(sl, sr, idx int) {
	if sl == sr {
		for _, i := range atof(a[sl]) {
			freq[idx][i] = 1
		}
		return
	}

	m, lidx, ridx := (sl+sr)>>1, idx<<1, idx<<1|1
	build(sl, m, lidx)
	build(m+1, sr, ridx)

	for i := 0; i < maxbit; i++ {
		freq[idx][i] = freq[lidx][i] + freq[ridx][i]
	}
}

func query(sl, sr, l, r, idx int) int64 {
	// fmt.Println(sl, sr, l, r, idx)
	// segment range is within query range ? report
	if l <= sl && sr <= r {
		return eval(idx)
	}
	// disjoint ? quit
	if r < sl || sr < l {
		return 0
	}
	// push label down
	m, lidx, ridx := (sr+sl)>>1, idx<<1, idx<<1|1
	updateInternal(sl, m, lidx, label[idx])
	updateInternal(m+1, sr, ridx, label[idx])
	label[idx] = 0

	return query(sl, m, l, r, lidx) + query(m+1, sr, l, r, ridx)
}

func updateInternal(sl, sr, idx, v int) {
	label[idx] ^= v
	for _, i := range atof(v) {
		freq[idx][i] = (sr - sl + 1) - freq[idx][i]
	}
}

func update(sl, sr, l, r, idx, v int) {
	// segment range is within query range ? report
	if l <= sl && sr <= r {
		updateInternal(sl, sr, idx, v)
		return
	}
	// disjoint ? quit
	if r < sl || sr < l {
		return
	}
	// push label down
	m, lidx, ridx := (sr+sl)>>1, idx<<1, idx<<1|1
	updateInternal(sl, m, lidx, label[idx])
	updateInternal(m+1, sr, ridx, label[idx])
	label[idx] = 0

	update(sl, m, l, r, lidx, v)
	update(m+1, sr, l, r, ridx, v)

	for i := 0; i < maxbit; i++ {
		freq[idx][i] = freq[lidx][i] + freq[ridx][i]
	}
}

func main() {
	in := &scanner{bufio.NewScanner(os.Stdin)}
	in.Split(bufio.ScanWords)

	n = in.nextInt()
	var offset int
	for offset = 1; offset <= n; offset <<= 1 {
	}
	label = make([]int, 2*offset)
	freq = make([][]int, 2*offset)
	a = make([]int, n+1)
	for i := 1; i < 2*offset; i++ {
		freq[i] = make([]int, maxbit)
	}
	for i := 1; i <= n; i++ {
		a[i] = in.nextInt()
	}

	build(1, n, 1)

	m = in.nextInt()
	for m > 0 {
		switch t := in.nextInt(); t {
		case 1:
			l, r := in.nextInt(), in.nextInt()
			fmt.Println(query(1, n, l, r, 1))
		case 2:
			l, r, v := in.nextInt(), in.nextInt(), in.nextInt()
			update(1, n, l, r, 1, v)
		}
		m--
	}
}

package main

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

var w *bufio.Writer

var sc *bufio.Scanner

const minus, zero byte = '-', '0'

func nextInt() int {
  var ans, fst int = 0, 0
  var neg bool = false
  sc.Scan()
  if sc.Bytes()[0] == minus {
    fst = 1
    neg = true
  }
  for _, j := range sc.Bytes()[fst:] {
    ans = ans*10 + int(j-zero)
  }
  if neg == true {
    ans = -ans
  }
  return ans
}

//SEGMENT TREE FOR BITS
const Z int = -3000000

type SegmentTree struct {
  l, r, color int
  value       uint64
  left, right *SegmentTree
}

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

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

func CombineValues(val1 uint64, val2 uint64) uint64 { return val1 + val2 }

func (tree *SegmentTree) PaintedSegmentValue(CL int) uint64 { // XOR OPERATION
  if CL == 0 {
    return tree.value
  } else {
    return -tree.value + uint64(tree.r-tree.l+1)
  }
}

func CombineColors(cl1 int, cl2 int) int { return cl1 ^ cl2 }

func Init(L int, R int, data []int) *SegmentTree {
  res := new(SegmentTree)
  res.l = L
  res.r = R
  res.color = Z
  if L == R {
    res.value = uint64(data[L])
    res.left = nil
    res.right = nil
  } else {
    m := (L + R) >> 1
    res.left = Init(L, m, data)
    res.right = Init(m+1, R, data)
    res.value = CombineValues(res.left.value, res.right.value)
  }
  return res
}

func (tree *SegmentTree) GetValue(L int, R int, cl int) uint64 {
  if L > R {
    return 0
  }
  cl2 := CombineColors(tree.color, cl)
  m := (tree.l + tree.r) >> 1
  if L == tree.l && R == tree.r {
    if cl2 == Z {
      return tree.value
    }
    return tree.PaintedSegmentValue(cl2)
  }
  return CombineValues(tree.left.GetValue(L, Min(R, m), cl2),
    tree.right.GetValue(Max(L, m+1), R, cl2))
}

func (tree *SegmentTree) Paint(L int, R int, CL int) {
  if tree.l > R || L > tree.r || R < tree.l {
    return
  }
  if tree.l == L && tree.r == R {
    tree.color = CombineColors(tree.color, CL)
  } else {
    if tree.color != Z && tree.left != nil && tree.right != nil { //push
      tree.left.color = CombineColors(tree.left.color, tree.color)
      tree.right.color = CombineColors(tree.right.color, tree.color)
      tree.color = Z
    }
    m := (tree.l + tree.r) >> 1
    tree.left.Paint(L, Min(R, m), CL)
    tree.right.Paint(Max(L, m+1), R, CL)
    var val1, val2 uint64 = 0, 0
    if tree.left.color == Z {
      val1 = tree.left.value
    } else {
      val1 = tree.left.PaintedSegmentValue(tree.left.color)
    }
    if tree.right.color == Z {
      val2 = tree.right.value
    } else {
      val2 = tree.right.PaintedSegmentValue(tree.right.color)
    }
    tree.value = CombineValues(val1, val2)
  }
}

//END SEGMENT TREE

func main() {

  w = bufio.NewWriter(os.Stdout)
  sc = bufio.NewScanner(os.Stdin)
  sc.Split(bufio.ScanWords)

  n := nextInt()
  bits := make([][]int, 20)
  for i := 0; i < 20; i++ {
    bits[i] = make([]int, n)
  }
  trees := make([]*SegmentTree, 20)
  for i := 0; i < n; i++ {
    x := nextInt()
    for j := uint(0); j < 20; j++ {
      if ((1 << j) & x) > 0 {
        bits[j][i] = 1
      }
    }
  }
  for i := 0; i < 20; i++ {
    trees[i] = Init(0, n-1, bits[i])
  }
  m := nextInt()
  for i := 0; i < m; i++ {
    t, l, r := nextInt(), nextInt()-1, nextInt()-1
    if t == 1 {
      var ans uint64 = 0
      for j := uint(0); j < 20; j++ {
        //w.WriteString(strconv.FormatUint(trees[j].GetValue(l, r, 0), 10))
        //w.WriteByte(' ')
        ans += trees[j].GetValue(l, r, 0) * (1 << j)
      }
      w.WriteString(strconv.FormatUint(ans, 10))
      w.WriteByte('\n')
    } else {
      x := nextInt()
      for j := uint(0); j < 20; j++ {
        if ((1 << j) & x) > 0 {
          trees[j].Paint(l, r, 1)
        }
      }
    }
  }
  w.Flush()
  //YOU FORGOT TO FLUSH!
}


package main

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

type kv struct {
	Key   int
	Value int
}

func main() {
	//rd, _ := os.Open("input")
	io := bufio.NewReader(os.Stdin)

	var t, n, a1, a2 int

	fmt.Fscan(io, &t)
	for ; t > 0; t-- {
		fmt.Fscan(io, &n)
		for i := 0; i < n/2; i++ {
			fmt.Fscan(io, &a1, &a2)
			fmt.Printf("%d %d ", -a2, a1)
		}
		fmt.Println()
	}
}

package main

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

func main() {
	io := bufio.NewReader(os.Stdin)
	var T, n, a1, a2 int

	fmt.Fscan(io, &T)
	for ; T > 0; T-- {
		fmt.Fscan(io, &n)
		for ; n>0; n-=2 {
			fmt.Fscan(io, &a1, &a2)
			fmt.Printf("%d %d ", a2, -a1)
		}
		fmt.Println()
	}

}

package main

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

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := 0; i < t; i++ {
		n := ReadInt()
		a := ReadInts(n)
		b := solve(n, a)
		for j := 0; j < n; j++ {
			if j > 0 {
				print(" ")
			}
			printf("%d", b[j])
		}
		println()
	}
}

func solve(n int, a []int) []int {
	b := make([]int, n)
	for i := 0; i < n/2; i++ {
		b[i*2], b[i*2+1] = -a[i*2+1], a[i*2]
	}
	return b
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func print(a ...interface{})            { fmt.Fprint(_w, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_w, f, a...) }
func println(a ...interface{})          { fmt.Fprintln(_w, a...) }
func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(_r, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt64())
	}
	return
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func main() {
	for tt := NextInt(); tt > 0; tt-- {
		n := NextInt()
		for i := 0; i < n / 2; i++ {
			a, b := NextInt(), NextInt()
			fmt.Printf("%d %d ", -b, a)
		}
		fmt.Println()
	}
}

package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	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 getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	t := getNextInt(scanner)
	for t > 0 {
		t--
		func() {
			n := getNextInt(scanner)
			aa := make([]int, n)
			for i := 0; i < n; i++ {
				aa[i] = getNextInt(scanner)
			}
			bb := make([]int, n)
			for i := 0; i < n>>1; i++ {
				bb[i] = aa[n-1-i]
				bb[n-1-i] = -aa[i] * bb[i] / aa[n-1-i]
			}
			sep := ""
			for i := 0; i < n; i++ {
				fmt.Fprintf(writer, "%s%d", sep, bb[i])
				sep = " "
			}
			fmt.Fprintln(writer, "")
		}()
	}
}

package main

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

func main() {
	var T int
	var results [][]int

	fmt.Scanf("%d\n", &T)
	in := bufio.NewReader(os.Stdin)
	for i:=0; i<T*2; i++ {
		line, err := in.ReadString('\n')
		line = strings.Replace(line, "\r", "", -1)
		line = strings.Replace(line, "\n", "", -1)
		strs := strings.Split(line, " ")
		nums := make([]int, len(strs))
		if len(strs) > 1 {
			for i, str := range strs {
				if nums[i], err = strconv.Atoi(str); err != nil {
					log.Fatal(err)
				}
			}
			results = append(results, nums)
		}
	}
	for il:=0; il <len(results); il++{
		rsd:=fn(results[il])
		for i:=0; i <len(rsd); i++{
			fmt.Printf("%d ",rsd[i])
		}
		fmt.Print("\n")
	}
}

func fn(a []int) []int{
	var b = make([]int, len(a))
	for i:=0; i<len(a)-1;i=i+2{
		b[i] = a[i+1]
		b[i+1] = -a[i]
	}
	return b
}

package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var n int
	fmt.Scan(&n)
	r := make([]string, n)
	for i := 0; i < n; i++ {
		var nn int
		fmt.Scan(&nn)
		arr := make([]int, nn)
		for j := 0; j < nn; j++ {
			fmt.Scan(&arr[j])
		}
		r = append(r, strings.Join(calculate(arr), " "))
	}
	for _, v := range r {
		fmt.Println(v)
	}
}
func calculate(a []int) []string {
	r := make([]string, len(a))
	for i := 0; i < len(a); i+=2 {
		r[i] = strconv.Itoa(-a[i+1])
	}
	for i := 1; i < len(a); i+=2 {
		r[i] = strconv.Itoa(a[i-1])
	}
	return r
}

package main

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

func main() {
	var T int
	var res [][]int
	fmt.Scanf("%d\n", &T)
	for i:=0; i<T; i++{

		in := bufio.NewReader(os.Stdin)
		prom, _ := in.ReadString('\n')

		le, _ := strconv.Atoi(strings.Replace(prom, "\n", "", -1))
		var mas = make([]int, le)
		prom, _ = in.ReadString('\n')
		promn := strings.Split(strings.Replace(prom, "\n", "", -1), " ")
		for g:=0; g<le; g++{
			mas[g] , _= strconv.Atoi(promn[g])
		}
		res=append(res, mas)
	}

	for il:=0; il <len(res); il++{
		rsd:=fn(res[il])
		for i:=0; i <len(rsd); i++{
			fmt.Printf("%d ",rsd[i])
		}
		fmt.Print("\n")
	}
}

func fn(a []int) []int{
	var b = make([]int, len(a))
	for i:=0; i<len(a)-1;{
		if a[i+1] % a[i] == 0{
			b[i] = a[i+1] / a[i]
			b[i+1] = -1
			i=i+2
		}
	}
	return b
}

package main

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

func main() {
	var T int
	var res [][]int
	fmt.Scanf("%d\n", &T)
	for i:=0; i<T; i++{
		var le = 0
		fmt.Scanf("%d\n", &le)
		var mas = make([]int, le)
		in := bufio.NewReader(os.Stdin)
		prom, _ := in.ReadString('\n')
		promn := strings.Split(strings.Replace(prom, "\n", "", -1), " ")
		for g:=0; g<le; g++{
			mas[g] , _= strconv.Atoi(promn[g])
		}
		res=append(res, mas)
	}

	for il:=0; il <len(res); il++{
		rsd:=fn(res[il])
		for i:=0; i <len(rsd); i++{
			fmt.Printf("%d ",rsd[i])
		}
		fmt.Print("\n")
	}
}

func fn(a []int) []int{
	var b = make([]int, len(a))
	for i:=0; i<len(a)-1;{
		if a[i+1] % a[i] == 0{
			b[i] = a[i+1] / a[i]
			b[i+1] = -1
			i=i+2
		}
	}
	return b
}

package main

import (
	"fmt"
)

func main() {
	var T, n, a1, a2 int

	fmt.Scan(&T)
	for i:=0; i<T; i++ {
		fmt.Scan(&n)
		for j:=0; j<n/2; j++ {
			fmt.Scan(&a1)
			fmt.Scan(&a2)
			fmt.Printf("%d -%d ", a2, a1)
		}
		fmt.Println()
	}

}

package main

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

func main() {
	defer _w.Flush()
	t := ReadInt()
	for i := 0; i < t; i++ {
		n := ReadInt()
		a := ReadInts(n)
		b := solve(n, a)
		for j := 0; j < n; j++ {
			if j > 0 {
				print(" ")
			}
			printf("%d", b[j])
		}
		println()
	}
}

func solve(n int, a []int) []int {
	s := 0
	for i := 0; i < n; i++ {
		s += a[i]
	}

	s1 := s - a[0]
	for b1 := -100; b1 <= 100; b1++ {
		if b1*a[0]%s1 == 0 {
			b := -b1 * a[0] / s1
			ans := make([]int, 0)
			ans = append(ans, b1)
			for i := 0; i < n-1; i++ {
				ans = append(ans, b)
			}
			return ans
		}
	}
	return nil
}

var _r, _w = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

func print(a ...interface{})            { fmt.Fprint(_w, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(_w, f, a...) }
func println(a ...interface{})          { fmt.Fprintln(_w, a...) }
func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(_r, a...); err != nil {
		panic(err)
	}
}
func ReadString() (s string) { Scan(&s); return }
func ReadInt() (i int)       { Scan(&i); return }
func ReadInts(n int) (a []int) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt())
	}
	return
}
func ReadInt64() (i int64) { Scan(&i); return }
func ReadInt64s(n int) (a []int64) {
	for i := 0; i < n; i++ {
		a = append(a, ReadInt64())
	}
	return
}

package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func main() {
	for tt := NextInt(); tt > 0; tt-- {
		n := NextInt()
		for i := 0; i < n / 2; i++ {
			a, b := NextInt(), NextInt()
			fmt.Printf("%d %d ", -b, -a)
		}
		fmt.Println()
	}
}


package main

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

func solve(n int, b, a int64, s []bool) int {
	var (
		i int
	)

	bCur, aCur := b, a
	for ; i < n; i++ {
		if aCur <= 0 && bCur <= 0 {
			break
		}

		f := s[i]
		if f && bCur > 0 && aCur < a {
			aCur++
			bCur--
		} else {
			if aCur > 0 {
				aCur--
			} else {
				bCur--
			}
		}

	}

	return i
}

func main() {
	var (
		n    int
		b, a int64
		s    []bool
	)

	fmt.Scan(&n, &b, &a)

	s = make([]bool, n)

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := range s {
		scanner.Scan()
		s[i] = '1' == (scanner.Bytes())[0]
	}

	r := solve(n, b, a, s)
	fmt.Println(r)
}

package main

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

func main() {
	defer os.Stdout.Sync()
	buf := bufio.NewReader(os.Stdin)
	var n, capa,capb int
	fmt.Fscanf(buf, "%d %d %d\n", &n,&capa,&capb)
	mat := make([]int, n+1)

	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i+1])
	}
	fmt.Fscanf(buf, "\n")
	a := capa
	b := capb
	for i:= 1;i <= n;i++{
		if mat[i] == 1{
			if b==0||(a > 0&&b < capb){
				a--
				b++
			}else{
				b--
			}
		}else{
			if b > 0{
				b--
			}else{
				a--
			}
		}
		if a==0&&b==0{
			fmt.Printf("%d\n",i)
			return
		}
	}
	fmt.Printf("%d\n",n)

}
/**
20 5 5
1 1 0 0 0 0 1 1 1 0 1 1 0 0 0 1 0 0 0 1
 */

// Author: sighduck
// URL: https://codeforces.com/contest/1154/problem/D

package main

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

func Solve(n int, b int, a int, s []int) int {
	segments := 0
	maxCapacity := a
	for _, elem := range s {
		if elem == 1 && b > 0 && a == maxCapacity {
			a -= 1
		} else if elem == 1 && b > 0 {
			a += 1
			b -= 1
		} else if elem == 1 && a > 0 {
			a -= 1
		} else if elem == 0 && a > 0 {
			a -= 1
		} else if elem == 0 && b > 0 {
			b -= 1
		} else {
			break
		}
		segments += 1
	}
	return segments
}

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

	var n, b, a int
	fmt.Fscanf(reader, "%d %d %d\n", &n, &b, &a)
	s := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &s[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, b, a, s))
}

package main

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

func main() {
	defer os.Stdout.Sync()
	buf := bufio.NewReader(os.Stdin)
	var n, capa,capb int
	fmt.Fscanf(buf, "%d %d %d\n", &n,&capa,&capb)
	mat := make([]int, n+1)

	for i := 0; i < n; i++ {
		fmt.Fscanf(buf, "%d", &mat[i+1])
	}
	fmt.Fscanf(buf, "\n")
	a := capa
	b := capb
	for i:= 1;i <= n;i++{
		if b > 0{
			b--
		}else{
			a--
			if mat[i]==1{
				b++
			}
		}
		if a==0&&b==0{
			fmt.Printf("%d\n",i)
			return
		}
	}
	fmt.Printf("%d\n",n)

}


package main

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

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

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

func main() {
	defer writer.Flush()

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

	for t := 0; t < tcases; t++ {
		scanf("%d %d\n", &n, &k)

		var str string
		scanf("%s\n", &str)

		digits := make([]rune, n, n)
		for i, r := range str {
			digits[i] = r
		}

		counts := map[rune]int{}
		counts['1'] = 0
		counts['0'] = 0
		counts['?'] = 0

		for i := 0; i < k; i++ {
			counts[digits[i]]++
		}

		// printState(digits, counts)

		valid := true
		for i := 0; i < k; i++ {
			for j := i; j < n; j += k {
				if i == j {
					continue
				}
				// printf("-%d %d\n", i, j)
				if digits[j] == '?' {
					continue
				} else {
					if digits[i] == '?' {
						digits[i] = digits[j]
						counts['?']--
						counts[digits[i]]++
					} else {
						if digits[i] != digits[j] {
							valid = false
							break
						}
					}
				}
			}
			if !valid {
				break
			}
		}

		// printState(digits, counts)

		if counts['1'] > k/2 || counts['0'] > k/2 {
			valid = false
		}

		if valid {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

func printState(digits []rune, counts map[rune]int) {
	printf("-------\n")
	for i := 0; i < len(digits); i++ {
		printf("%c ", digits[i])
	}
	printf("\n")

	printf("1: %d\n0: %d\n?: %d\n", counts['1'], counts['0'], counts['?'])
}

package main

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

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

	var T, n, k int
	var s []byte
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &k, &s)
		for i, b := range s[k:] {
			if b != '?' {
				if s[i%k] == '?' {
					s[i%k] = b
				} else if s[i%k] != b {
					Fprintln(out, "NO")
					continue o
				}
			}
		}
		if bytes.Count(s[:k], []byte{'0'}) > k/2 || bytes.Count(s[:k], []byte{'1'}) > k/2 {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

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

package main

import "fmt"

const NO_CONSTRAINT = -1

func main() {
    var t int
    fmt.Scanln(&t)
    for ca := 0; ca < t; ca++ {
        var n, k int
		fmt.Scanln(&n, &k)
		var s string
		fmt.Scanln(&s)
		valid := true
		constraints := make([]int, k)
		for i := 0; i < k; i++ {
			constraints[i] = NO_CONSTRAINT
		}
		for i := 0; i < n; i++ {
			if s[i] != '?' {
				c := int(s[i] - '0')
				if constraints[i % k] == NO_CONSTRAINT {
					constraints[i % k] = c
				} else if constraints[i % k] == c {
				} else {
					valid = false
					break
				}
			}
		}
		var cnt [2]int
		for i := 0; i < k; i++ {
			if constraints[i] != NO_CONSTRAINT {
				cnt[constraints[i]]++
			}
		}
		if cnt[0] > k / 2 || cnt[1] > k / 2 {
			valid = false
		}

		if valid {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
    }
}

package main

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

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

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

func main() {
	defer writer.Flush()

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

	for t := 0; t < tcases; t++ {
		scanf("%d %d\n", &n, &k)

		var str string
		scanf("%s\n", &str)

		digits := make([]rune, n, n)
		for i, r := range str {
			digits[i] = r
		}

		counts := map[rune]int{}
		counts['1'] = 0
		counts['0'] = 0
		counts['?'] = 0

		for i := 0; i < k; i++ {
			counts[digits[i]]++
		}

		// printState(digits, counts)

		for i := 0; i < k; i++ {
			assignDigit(digits, counts, i, k)
		}

		// printState(digits, counts)

		valid := true
		for l, r := 0, k; r <= n; l, r = l+1, r+1 {
			if l > 0 {
				counts[digits[l-1]]--
			}

			assignDigit(digits, counts, r-1, k)
			if counts['?'] > 0 {
				panic("unhandled ?")
			}

			if counts['1'] > k/2 || counts['0'] > k/2 {
				// printf("BREAKING")
				// printState(digits, counts)
				valid = false
				break
			}

			if r < n {
				counts[digits[r]]++
			}
		}

		if valid {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

func assignDigit(digits []rune, counts map[rune]int, i int, k int) {
	if digits[i] != '?' {
		return
	}
	if counts['1'] < k/2 {
		digits[i] = '1'
		counts['1']++
	} else if counts['0'] < k/2 {
		digits[i] = '0'
		counts['0']++
	} else {
		panic("this should never happen!")
	}

	counts['?']--
}

func printState(digits []rune, counts map[rune]int) {
	printf("-------\n")
	for i := 0; i < len(digits); i++ {
		printf("%c ", digits[i])
	}
	printf("\n")

	printf("1: %d\n0: %d\n?: %d\n", counts['1'], counts['0'], counts['?'])
}


package main

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

var dp [1 << 7]int64
var id []int
var a [100005]int
var s [100005][7]int

func main() {
	n := ReadInt()
	p := ReadInt()
	k := ReadInt()
	for i := 0; i < n; i++ {
		a[i] = ReadInt()
		id = append(id, i);
	}
	for i := 0; i < n; i++ {
		for j := 0; j < p; j++ {
			s[i][j] = ReadInt()
		}
	}
	sort.Slice(id,  func(i, j int) bool {
		return a[id[i]] > a[id[j]]
	})
	for i := 0; i < (1 << 7); i++ {
		dp[i] = -1
	}
	dp[0] = 0
	for i := 0; i < n; i++ {
		for j := (1 << uint(p)) - 1; j >= 0; j-- {
			var cnt int = 0
			for m := 0; m < p; m++ {
				if ((uint(j) >> uint(m)) & 1) != 0 {
					cnt++;
				}
			}
			cnt = i - cnt
			if cnt < k && dp[j] != -1 {
				dp[j] += int64(a[id[i]])
			}
			for m := 0; m < p; m++ {
				if ((uint(j) >> uint(m)) & 1) != 0 {
					if dp[j^(1<<uint(m))] != -1 {
						dp[j] = max(dp[j], dp[j^(1<<uint(m))]+int64(s[id[i]][m]))
					}
				}
			}
		}
	}
	fmt.Println(dp[(1<<uint(p))-1])
}

////////////////////////////////////

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

var out chan string
var in *bufio.Scanner
var outWg *sync.WaitGroup

func ReadString() string {
	in.Scan()
	return in.Text()
}
func ReadStringSlice(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ReadString()
	}
	return s
}
func ReadInt() int {
	intStr := ReadString()
	i, _ := strconv.ParseInt(intStr, 10, 32)
	return int(i)
}
func ReadIntSlice(n int) []int {
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i] = ReadInt()
	}
	return arr
}
func init() {
	//set input
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(2e+5))
	in.Split(bufio.ScanWords)

	//set output
	out = make(chan string, 16)
	outWg = &sync.WaitGroup{}
	outWg.Add(1)

	writer := bufio.NewWriterSize(os.Stdout, int(2e+5))
	go func(write *bufio.Writer) {
		defer outWg.Done()
		defer write.Flush()

		for line := range out {
			write.WriteString(line + "\n")
		}
	}(writer)
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1010)

	n := NextInt()
	p := NextInt()
	k := NextInt()
	a := make([]int, n)
	perm := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
		perm[i] = i
	}
	s := make([][]int, n)
	var dp [2][][2]int64
	MAX_MASK := (1 << uint(p))
	dp[0] = make([][2]int64, MAX_MASK)
	dp[1] = make([][2]int64, MAX_MASK)
	for i := 0; i < n; i++ {
		s[i] = make([]int, p)
		for j := 0; j < p; j++ {
			s[i][j] = NextInt()
		}
	}
	sort.Slice(perm, func(i, j int) bool {
		return a[perm[i]] > a[perm[j]]
	})

	for i := 0; i < MAX_MASK; i++ {
		dp[1][i][0] = -int64(1000000000) * int64(100000000)
	}
	dp[1][0][0] = 0
	var res int64
	for i := 0; i < n; i++ {
		ind := i & 1
		pind := ind ^ 1
		for mask := 0; mask < MAX_MASK; mask++ {
			curr := dp[pind][mask][0]
			count := dp[pind][mask][1]
			if int(count) < k {
				count++
				curr += int64(a[perm[i]])
			}
			dp[ind][mask][0] = curr
			dp[ind][mask][1] = count
			for j := 0; j < p; j++ {
				if mask&(1<<uint(j)) == 0 {
					continue
				}
				nmask := mask & ^(1 << uint(j))
				curr := dp[pind][nmask][0]
				count := dp[pind][nmask][1]
				if dp[ind][mask][0] < curr+int64(s[perm[i]][j]) {
					dp[ind][mask][0] = curr + int64(s[perm[i]][j])
					dp[ind][mask][1] = count
				}
			}
			// fmt.Fprintf(os.Stderr, "%d %b = %d, %d\n", i, mask, dp[ind][mask][0], dp[ind][mask][1])
		}
		if dp[ind][MAX_MASK-1][0] > res {
			res = dp[ind][MAX_MASK-1][0]
		}
	}
	fmt.Fprintln(out, res)
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

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

type Network struct {
	n     int
	m     int
	edges int
	fst   []int
	nxt   []int
	end   []int
	cost  []int
	cap   []int
}

func (g *Network) Add(a, b, cap, cost int) {
	g.nxt[g.edges] = g.fst[a]
	g.fst[a] = g.edges
	g.end[g.edges] = b
	g.cap[g.edges] = cap
	g.cost[g.edges] = cost
	g.edges++
	g.nxt[g.edges] = g.fst[b]
	g.fst[b] = g.edges
	g.end[g.edges] = a
	g.cap[g.edges] = 0
	g.cost[g.edges] = -cost
	g.edges++
}

func (g *Network) Adjacent(a int, fn func(u int)) {
	for u := g.fst[a]; u != -1; u = g.nxt[u] {
		fn(u)
	}
}

func (g *Network) MainEdges(fn func(u int)) {
	for u := 0; u < g.edges; u += 2 {
		fn(u)
	}
}

func (g *Network) inf() int64 {
	return int64(20000000000)
}

func (g *Network) updatePotentials(d, phi []int64) {
	for i := 0; i < g.n; i++ {
		phi[i] += d[i]
	}
}

func (g *Network) relax(s, t int, p []int) (int, int64) {
	minCapacity := 0
	var pathCost int64

	for v, step := t, 0; v != s; step++ {
		u := p[v]
		if step == 0 || minCapacity > g.cap[u] {
			minCapacity = g.cap[u]
		}
		v = g.end[u^1]
	}

	for v := t; v != s; {
		u := p[v]
		g.cap[u] -= minCapacity
		g.cap[u^1] += minCapacity
		pathCost += int64(minCapacity) * int64(g.cost[u])
		v = g.end[u^1]
	}
	return minCapacity, pathCost
}

func (g *Network) afterPhase(s, t int, d []int64, p []int, phi []int64) (int, int64) {
	if d[t] == g.inf() {
		return 0, 0
	}
	g.updatePotentials(d, phi)

	return g.relax(s, t, p)
}

func (g *Network) levit(s, t int, d []int64, id []int, p []int, q []int) {
	for i := 0; i < g.n; i++ {
		d[i] = g.inf()
	}
	qh, qt := 0, 0
	d[s] = 0
	q[qt] = s
	qt++
	for qh != qt {
		v := q[qh]
		qh++
		if qh == g.n {
			qh = 0
		}
		// stderr("going to ver", d[v])
		id[v] = 2
		for u := g.fst[v]; u != -1; u = g.nxt[u] {
			to := g.end[u]
			cost := int64(g.cost[u])
			if g.cap[u] > 0 && d[to] > d[v]+cost {
				d[to] = d[v] + cost
				if id[to] == 0 {
					q[qt] = to
					qt++
					if qt == g.n {
						qt = 0
					}
				} else if id[to] == 2 {
					qh--
					if qh == -1 {
						qh = g.n - 1
					}
					q[qh] = to
				}
				id[to] = 1
				p[to] = u
			}
		}
	}
}

func (g *Network) dijkstra(s, t int, d, phi []int64, p []int, heap *Heap) {
	// stderr("dijkstra")
	var mPhi int64
	for i := 0; i < g.n; i++ {
		d[i] = g.inf()
		if i == 0 || mPhi < phi[i] {
			mPhi = phi[i]
		}
	}
	// stderr("maxPhi", mPhi)
	d[s] = 0
	heap.Clear()
	heap.Add(s)

	for heap.Size() > 0 {
		// stderr("heap size", heap.Size())
		v := heap.Pop()
		if d[v] >= g.inf() {
			// stderr("weird")
			continue
		}

		g.Adjacent(v, func(u int) {
			to := g.end[u]
			cost := int64(g.cost[u])
			if g.cap[u] > 0 && cost+phi[v]-phi[to] < 0 {
				// stderr("negative edge", v, to, s, t, cost, phi[v], phi[to], phi[v]-phi[to])
				panic("negative edge")
			}
			if g.cap[u] > 0 && d[to] > d[v]+cost+phi[v]-phi[to] {
				d[to] = d[v] + cost + phi[v] - phi[to]
				heap.Update(to)
				p[to] = u
			}
		})
	}
}

type HeapComparator func(i, j int) bool

type Heap struct {
	n    int
	a    []int
	ind  []int
	less HeapComparator
}

func (t *Heap) Add(key int) {
	if t.n == len(t.a) {
		panic("Preset bigger heap size")
	}
	t.a[t.n] = key
	t.ind[key] = t.n
	t.n++
	t.pushUp(t.n - 1)
}

func (t *Heap) Remove(key int) {
	ind := t.ind[key]
	if ind == -1 {
		return
	}
	t.swap(ind, t.n-1)
	t.n--
	t.ind[key] = -1
	t.pushDown(ind)
	t.pushUp(ind)
}

func (t *Heap) Size() int {
	return t.n
}

func (t *Heap) Clear() {
	for i := 0; i < t.n; i++ {
		t.ind[t.a[i]] = -1
	}
	t.n = 0
}

func (t *Heap) pushUp(i int) {
	for i > 0 && i < t.n {
		upper := (i - 1) >> 1
		if t.less(t.a[upper], t.a[i]) {
			return
		}
		t.swap(upper, i)
		i = upper
	}
}

func (t *Heap) swap(i, j int) {
	t.a[i], t.a[j] = t.a[j], t.a[i]
	t.ind[t.a[i]] = i
	t.ind[t.a[j]] = j
}

func (t *Heap) print(out io.Writer) {
	fmt.Fprintln(out, t.a[:t.n])
}

func (t *Heap) pushDown(i int) {
	for i < t.n {
		left, right := i*2+1, i*2+2
		tt := i
		if left < t.n && t.less(t.a[left], t.a[i]) {
			tt = left
		}
		if right < t.n && t.less(t.a[right], t.a[tt]) {
			tt = right
		}
		if tt == i {
			break
		}
		t.swap(i, tt)
		i = tt
	}
}

func (t *Heap) Update(key int) {
	i := t.ind[key]
	if i == -1 {
		t.Add(key)
	} else {
		if t.a[i] != key {
			panic("heap is corrupted, the existed key heap position is incorrect")
		}
		t.pushDown(i)
		t.pushUp(i)
	}
}

func (t *Heap) Top() int {
	if t.n == 0 {
		panic("no more elements in Heap")
	}
	return t.a[0]
}

func (t *Heap) Pop() int {
	if t.n == 0 {
		panic("no more elements in Heap")
	}
	key := t.a[0]
	t.swap(0, t.n-1)
	t.n--
	t.ind[key] = -1
	t.pushDown(0)
	return key
}

func NewHeap(cap int, less HeapComparator) *Heap {
	a := make([]int, cap)
	ind := make([]int, cap)
	for i := 0; i < cap; i++ {
		ind[i] = -1
	}
	return &Heap{0, a, ind, less}
}

func (g *Network) MinCostMaxFlow(s, t int) (int, int64) {
	d := make([]int64, g.n)
	p := make([]int, g.n)
	phi := make([]int64, g.n)
	id := make([]int, g.n)
	q := make([]int, g.n)

	var maxFlow int
	var minCost int64

	heap := NewHeap(g.n, func(i, j int) bool {
		return d[i] < d[j] || d[i] == d[j] && i < j
	})

	g.levit(s, t, d, id, p, q)
	// stderr("first max flow")
	flow, cost := g.afterPhase(s, t, d, p, phi)

	for maxFlow, minCost = flow, cost; flow != 0; maxFlow, minCost = maxFlow+flow, minCost+cost {
		// stderr("flow and cost are", flow, cost)
		//s, t int, d, phi []int64, p []int, heap *Heap
		// g.levit(s, t, d, id, p, q)
		g.dijkstra(s, t, d, phi, p, heap)
		flow, cost = g.afterPhase(s, t, d, p, phi)
	}
	return maxFlow, minCost
}

func NewNetwork(n, m int) *Network {
	g := &Network{n, m, 0, make([]int, n), make([]int, m), make([]int, m), make([]int, m), make([]int, m)}
	for i := 0; i < n; i++ {
		g.fst[i] = -1
	}
	return g
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1010)

	n := NextInt()
	p := NextInt()
	k := NextInt()
	s := 0
	s1 := s + 1
	t := 1 + 1 + n + p
	g := NewNetwork(t+1, (1+n+n*p+n+p)*2)
	g.Add(s, s1, p+k, 0)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
		g.Add(s1, s1+1+i, 1, 0)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < p; j++ {
			sij := NextInt()
			g.Add(s1+1+i, s1+1+n+j, 1, -(sij - a[i]))
		}
	}
	// var T int64
	for i := 0; i < p; i++ {
		g.Add(s1+1+n+i, t, 1, 0)
	}

	flow, cost := g.MinCostMaxFlow(s, t)
	stderr("flow", flow, "cost", cost)

	used := make([]bool, n)
	var result int64

	g.MainEdges(func(u int) {
		from := g.end[u^1]
		to := g.end[u]
		if from >= s1+1 && from <= s1+n &&
			to >= s1+n+1 && to <= s1+n+p && g.cap[u] == 0 {
			used[from-s1-1] = true
			result += int64(-g.cost[u]) + int64(a[from-s1-1])
		}
	})

	newLen := 0
	for i := 0; i < n; i++ {
		if !used[i] {
			a[newLen] = a[i]
			newLen++
		}
	}
	a = a[:newLen]
	n = newLen
	sort.Ints(a)
	for i := 0; i < k; i++ {
		result += int64(a[n-i-1])
	}

	fmt.Fprintln(out, result)
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1010)

	n := NextInt()
	p := NextInt()
	k := NextInt()
	a := make([]int, n)
	perm := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
		perm[i] = i
	}
	s := make([][]int, n)
	var dp [2][][2]int
	MAX_MASK := (1 << uint(p))
	dp[0] = make([][2]int, MAX_MASK)
	dp[1] = make([][2]int, MAX_MASK)
	for i := 0; i < n; i++ {
		s[i] = make([]int, p)
		for j := 0; j < p; j++ {
			s[i][j] = NextInt()
		}
	}
	sort.Slice(perm, func(i, j int) bool {
		return a[perm[i]] > a[perm[j]]
	})

	var res int
	for i := 0; i < n; i++ {
		ind := i & 1
		pind := ind ^ 1
		for mask := 0; mask < MAX_MASK; mask++ {
			curr := dp[pind][mask][0]
			count := dp[pind][mask][1]
			if count < k {
				count++
				curr += a[perm[i]]
			}
			dp[ind][mask][0] = curr
			dp[ind][mask][1] = count
			for j := 0; j < p; j++ {
				if mask&(1<<uint(j)) == 0 {
					continue
				}
				nmask := mask & ^(1 << uint(j))
				curr := dp[pind][nmask][0]
				count := dp[pind][nmask][1]
				if dp[ind][mask][0] < curr+s[perm[i]][j] {
					dp[ind][mask][0] = curr + s[perm[i]][j]
					dp[ind][mask][1] = count
				}
			}
			// fmt.Fprintf(os.Stderr, "%d %b = %d, %d\n", i, mask, dp[ind][mask][0], dp[ind][mask][1])
		}
		if dp[ind][MAX_MASK-1][0] > res {
			res = dp[ind][MAX_MASK-1][0]
		}
	}
	fmt.Fprintln(out, res)
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}
func NextInt64() int64 {
	bytes := NextBytes()
	i, err := Atoi64(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func Atoi64(b []byte) (int64, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	var n int64
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int64(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func reverseInts(a []int) {
	for l, r := 0, len(a)-1; l < r; l, r = l+1, r-1 {
		a[l], a[r] = a[r], a[l]
	}
}

func unique(a []int) int {
	newLen := 0
	n := len(a)
	for i := 0; i < n; i++ {
		if newLen == 0 || a[i] != a[newLen-1] {
			a[newLen] = a[i]
			newLen++
		}
	}
	return newLen
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1010)

	n := NextInt()
	p := NextInt()
	k := NextInt()
	a := make([]int, n)
	perm := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
		perm[i] = i
	}
	s := make([][]int, n)
	var dp [2][][2]int64
	MAX_MASK := (1 << uint(p))
	dp[0] = make([][2]int64, MAX_MASK)
	dp[1] = make([][2]int64, MAX_MASK)
	for i := 0; i < n; i++ {
		s[i] = make([]int, p)
		for j := 0; j < p; j++ {
			s[i][j] = NextInt()
		}
	}
	sort.Slice(perm, func(i, j int) bool {
		return a[perm[i]] > a[perm[j]]
	})

	var res int64
	for i := 0; i < n; i++ {
		ind := i & 1
		pind := ind ^ 1
		for mask := 0; mask < MAX_MASK; mask++ {
			curr := dp[pind][mask][0]
			count := dp[pind][mask][1]
			if int(count) < k {
				count++
				curr += int64(a[perm[i]])
			}
			dp[ind][mask][0] = curr
			dp[ind][mask][1] = count
			for j := 0; j < p; j++ {
				if mask&(1<<uint(j)) == 0 {
					continue
				}
				nmask := mask & ^(1 << uint(j))
				curr := dp[pind][nmask][0]
				count := dp[pind][nmask][1]
				if dp[ind][mask][0] < curr+int64(s[perm[i]][j]) {
					dp[ind][mask][0] = curr + int64(s[perm[i]][j])
					dp[ind][mask][1] = count
				}
			}
			// fmt.Fprintf(os.Stderr, "%d %b = %d, %d\n", i, mask, dp[ind][mask][0], dp[ind][mask][1])
		}
		if dp[ind][MAX_MASK-1][0] > res {
			res = dp[ind][MAX_MASK-1][0]
		}
	}
	fmt.Fprintln(out, res)
}


package main
import (
	"fmt"
	"bufio"
	"os"
	//"strconv"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	var s string
	var n,i int
	fmt.Scan(&n)
	fmt.Fscan(reader,&s)
	for i=0;s[i]=='.';i++ {}
	fmt.Print(i+1," ")
	for ;i<n;i++ {
		if s[i]!='R' {
			break
		}
	}
	if s[i]=='L' {
		fmt.Print(i)
	} else {
		fmt.Print(i+1)
	}
}

package main

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

var scanner *bufio.Scanner

func getI() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  getI()
  road := getS()
  var a, b int
  r, l := strings.IndexByte(road, 'R'), strings.IndexByte(road, 'L')
  if r == -1 {
    a = strings.LastIndex(road, "L")
    b = l-1
  } else {
    a = r
    if l == -1 {
      b = strings.LastIndex(road, "R")+1
    } else {
      b = l-1
    }
  }
  writer.WriteString(fmt.Sprintf("%d %d\n", a+1, b+1))
}

package main

import "fmt"

func main() {
    var n int
    var r string
    fmt.Scan(&n)
    fmt.Scan(&r)
    s, t := -1, -1
    for i := 0; i < len(r); i++ {
        if r[i] == 'R' {
            s, t = i, i
            for ; r[t] == 'R'; t++ {}
            if r[t] == 'L' { t-- }
            break
        } else if r[i] == 'L' {
            s, t = i, i-1
            for ; r[s] == 'L'; s++ {}
            s--
            break
        }
    }
    fmt.Println(s+1,t+1)
}

package main

import "fmt"

func main() {
    var n int
    var r string
    fmt.Scan(&n)
    fmt.Scan(&r)
    s, t := -1, -1
    for k, v := range r {
        if v == 'R' {
            if s == -1 { s = k+1 }
        } else if v == 'L' {
            t = k-1
        } else if v == '.' {
            t = k
        }
    }
    fmt.Println(s,t)
}

package main

import "fmt"

func main() {
    var n int
    var r string
    fmt.Scan(&n)
    fmt.Scan(&r)
    s, t := -1, -1
    for k, v := range r {
        if v == 'R' {
            if s == -1 { s = k+1 }
        } else if v == 'L' {
            t = k-1
        } else if (v == '.') && (s != -1) {
            t = k
            break
        }
    }
    fmt.Println(s,t)
}

package main

import "fmt"

func main() {
    var n int
    var r string
    fmt.Scan(&n)
    fmt.Scan(&r)
    s, t := -1, -1
    for k, v := range r {
        if v == 'R' {
            if s == -1 { s = k+1 }
        } else if v == 'L' {
            t = k-1
        } else if v == '.' {
            t = k
            break
        }
    }
    fmt.Println(s,t)
}

package main

import "fmt"

func main() {
    var n int
    var r string
    fmt.Scan(&n)
    fmt.Scan(&r)
    s, t := -1, -1
    for k, v := range r {
        if v == 'R' {
            if s == -1 { s = k+1 }
            t = k+1
        }
    }
    fmt.Println(s,t)
}

package main

import "fmt"

func main() {
    var n int
    var r string
    fmt.Scan(&n)
    fmt.Scan(&r)
    s, t := -1, -1
    for i := 0; i < len(r); i++ {
        if r[i] == 'R' {
            s, t = i, i
            for ; r[t] == 'R'; t++ {}
            if r[t] == 'L' { t-- }
            break
        } else if r[i] == 'L' {
            s, t = i, i
            for ; r[s] == 'L'; s++ {}
            s--
            break
        }
    }
    fmt.Println(s+1,t+1)
}


package main

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

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

	var T, a, b, x int64
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &a, &b, &x)
		if a > b {
			a, b = b, a
		}
		for a > 0 && x <= b {
			if x >= a && (b-x)%a == 0 {
				Fprintln(out, "YES")
				continue o
			}
			a, b = b%a, a
		}
		Fprintln(out, "NO")
	}
}

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

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var a,b,x int64
		fmt.Fscan(in,&a,&b,&x)
		if a<b{
			temp := a
			a = b
			b = temp
		}
		flag := false
		for a>=x{
			if b==0{
				break
			}
			id := a/b
			if (a-x)%b==0{
				flag = true
				break
			}
			na := a - b*id
			a = b
			b = na
		}
		if flag {
			fmt.Fprintln(out, "YES")
		}else{
			fmt.Fprintln(out,"NO")
		}
	}
}

package main

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

var a, b, x int64

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)

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

		a := solve()

		fmt.Println(a)
	}
}

type pair struct {
	v, id int
}

type ByV []*pair

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

type item struct {
	la, ra int
	lb, rb int
}

const MAX int = 1e6

func solve() string {
	if a < b {
		temp := a
		a = b
		b = temp

	}
	if x > a && x > b {
		return "NO"
	}

	if x == a || x == b {
		return "YES"
	}

	for ;; {
		if b < x {
			if (a - x) % b == 0 {
				return "YES"
			} else {
				return "NO"
			}

		} else {
			k := a/b

			temp := a

			a = b
			b = temp - k * b

			if b == x {
				return "YES"
			}
			if b== 0 {
				return "NO"
			}
		}
	}

}

func gcdExtended(a int64, b int64, x *int64, y *int64) int64 {

	// Base Case
	if a == 0 {
		*x, *y = 0,1
		return b
	}

	// To store results of recursive call
	var x1, y1 int64
	gcd := gcdExtended(b%a, a, &x1, &y1);

	// Update x and y using results of recursive
	// call
	*x = y1 - (b/a)*x1;
	*y = x1
	return gcd
}

func add(idx int, delta int, bit *[]int, n int) {
	for ; idx <= n; idx += idx & -idx {
		(*bit)[idx] += delta
	}
}

func get(idx int, bit *[]int) int {
	ret := 0
	for ; idx > 0; idx -= idx & -idx {
		ret += (*bit)[idx];
	}
	return ret
}

//A -  1 3 5 3 1

//B -  4 3 6 1

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


package main

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

func dfs(cell [][]byte, i, j int) bool {
	switch cell[i][j] {
	case '#':
		return false
	case '$':
		return true
	}
	cell[i][j] = '#'
	return j+1 < len(cell[i]) && dfs(cell, i, j+1) ||
		i+1 < len(cell) && dfs(cell, i+1, j)
}

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

	var n, m int
	Fscan(in, &n, &m)
	cell := make([][]byte, n)
	for i := range cell {
		Fscan(in, &cell[i])
	}

	ans := 0
	cell[n-1][m-1] = '$'
	if dfs(cell, 0, 0) {
		ans++
		cell[0][0] = '.'
		if dfs(cell, 0, 0) {
			ans++
		}
	}
	Fprint(out, ans)
}

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

package main

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

const (
	maxn = 1000000
)

var n int
var m int
var s [maxn]string
var mark [maxn]bool
var pre_x [maxn]int
var pre_y [maxn]int
var blocked [maxn]bool

func valid(x int, y int) bool {
	return x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '#' && !blocked[get_index(x, y)]
}

func get_index(x int, y int) int {
	return x*m + y
}

func dfs(x int, y int) {
	mark[get_index(x, y)] = true
	if valid(x+1, y) && !mark[get_index(x+1, y)] {
		pre_x[get_index(x+1, y)] = x
		pre_y[get_index(x+1, y)] = y

		dfs(x+1, y)
	}
	if valid(x, y+1) && !mark[get_index(x, y+1)] {
		pre_x[get_index(x, y+1)] = x
		pre_y[get_index(x, y+1)] = y
		dfs(x, y+1)
	}
}

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

	if !(x == n-1 && y == m-1) {
		blocked[get_index(x, y)] = true
	}

	x2 := pre_x[get_index(x, y)]
	y2 := pre_y[get_index(x, y)]

	block_path(x2, y2)

}

func main() {

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	fmt.Fscan(in, &n, &m)

	for i := 0; i < n; i++ {
		fmt.Fscan(in, &s[i])
	}

	dfs(0, 0)

	if !mark[get_index(n-1, m-1)] {
		fmt.Println(0)
	} else {
		response := 1
		block_path(n-1, m-1)

		for i := 0; i < n; i++ {
			for j := 0; j < m; j++ {
				mark[get_index(i, j)] = false
			}
		}

		dfs(0, 0)
		if mark[get_index(n-1, m-1)] {
			response += 1
		}

		fmt.Println(response)
	}
}

package main

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

func dfs(cell [][]byte, i, j int) bool {
	switch cell[i][j] {
	case '#':
		return false
	case '$':
		return true
	}
	cell[i][j] = '#'
	return j+1 < len(cell[i]) && dfs(cell, i, j+1) ||
		i+1 < len(cell) && dfs(cell, i+1, j)
}

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

	var n, m int
	Fscan(in, &n, &m)
	cell := make([][]byte, n)
	for i := range cell {
		Fscan(in, &cell[i])
	}

	ans := 0
	cell[n-1][m-1] = '$'
	if dfs(cell, 0, 0) {
		ans++
	}
	cell[0][0] = '.'
	if dfs(cell, 0, 0) {
		ans++
	}
	Fprint(out, ans)
}

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

package main

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

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

var n, m int
var a [][]int
var mp map[int]int

func dfs(i, j, k int) bool {
	if i >= n || j >= m {
		return false
	}
	if i == n-1 && j == m-1 {
		return true
	}
	if a[i][j] == -1 {
		return false
	}
	if a[i][j] != 0 {
		return true
	}

	ret := false
	if dfs(i+1, j, k+1) {
		ret = true
	}
	if dfs(i, j+1, k+1) {
		ret = true
	}
	if ret == true && !(i == 0 && j == 0) {
		a[i][j] = k
		mp[k]++
		return true
	}
	a[i][j] = -1
	return false
}

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

	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a = make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Fscanf(reader, "%c", &c)
			if c == '.' {
				a[i][j] = 0
			} else {
				a[i][j] = -1
			}
		}
		fmt.Fscanln(reader)
	}

	mp = make(map[int]int)
	dfs(0, 0, 0)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF || mp[n+m-3] == 0 {
		ans = 0
	}
	fmt.Println(ans)

	return
}

package main

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

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

var n, m int
var a [][]int
var mp map[int]int

func dfs(i, j, k int) bool {
	if i >= n || j >= m {
		return false
	}
	if i == n-1 && j == m-1 {
		return true
	}
	if a[i][j] == -1 {
		return false
	}
	if a[i][j] != 0 {
		return true
	}
	ret := false
	if dfs(i+1, j, k+1) {
		ret = true
	}
	if dfs(i, j+1, k+1) {
		ret = true
	}
	if ret == true && !(i == 0 && j == 0) {
		a[i][j] = k
		mp[k]++
		return true
	}
	a[i][j] = -1
	return false
}

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

	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a = make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Fscanf(reader, "%c", &c)
			if c == '.' {
				a[i][j] = 0
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	mp = make(map[int]int)
	dfs(0, 0, 0)
	// fmt.Println(a)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF || mp[n+m-2] == 0 {
		ans = 0
	}
	fmt.Println(ans)

	return
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) && a[i][j] != INF {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF || mp[n+m-3] == INF {
		ans = 0
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) && a[i][j] != INF {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF || mp[n+m-3] != INF {
		ans = 0
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

var n, m int
var a [][]int
var mp map[int]int

func dfs(i, j, k int) bool {
	if i >= n || j >= m {
		return false
	}
	if i == n-1 && j == m-1 {
		return true
	}
	if a[i][j] == -1 {
		return false
	}
	ret := false
	if dfs(i+1, j, k+1) {
		ret = true
	}
	if dfs(i, j+1, k+1) {
		ret = true
	}
	if ret == true && !(i == 0 && j == 0) {
		mp[k]++
		return true
	}
	return false
}

func main() {
	fmt.Scanf("%d %d\n", &n, &m)
	a = make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = 0
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	mp = make(map[int]int)
	dfs(0, 0, 0)
	// fmt.Println(mp)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF {
		ans = 0
	}
	fmt.Println(ans)

	return
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			fmt.Println(c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanf("%c", &c)
	}

	fmt.Println(a)

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) {
				mp[a[i][j]]++
			}
		}
	}

	fmt.Println(a)
	fmt.Println(mp)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanf("%c", &c)
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) && a[i][j] != INF {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF || mp[n+m-3] == 0 {
		ans = 0
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d%d", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanf("%c", &c)
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

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

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

var n, m int
var a [][]int
var mp map[int]int

func dfs(i, j, k int) bool {
	if i >= n || j >= m {
		return false
	}
	if i == n-1 && j == m-1 {
		return true
	}
	if a[i][j] == -1 {
		return false
	}
	if a[i][j] != 0 {
		return true
	}

	ret := false
	if dfs(i+1, j, k+1) {
		ret = true
	}
	if dfs(i, j+1, k+1) {
		ret = true
	}
	if ret == true && !(i == 0 && j == 0) {
		a[i][j] = k
		mp[k]++
		return true
	}
	a[i][j] = -1
	return false
}

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

	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a = make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Fscanf(reader, "%c", &c)
			if c == '.' {
				a[i][j] = 0
			} else {
				a[i][j] = -1
			}
		}
		fmt.Fscanf(reader, "%c", &c)
	}

	mp = make(map[int]int)
	dfs(0, 0, 0)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF || mp[n+m-3] == 0 {
		ans = 0
	}
	fmt.Println(ans)

	return
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanf("%c", &c)
	}

	fmt.Println(a)

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) {
				mp[a[i][j]]++
			}
		}
	}

	fmt.Println(a)
	fmt.Println(mp)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) && a[i][j] != INF {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF {
		ans = 0
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 && a[i-1][j] != INF {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 && a[i][j-1] != INF {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) && a[i][j] != INF {
				mp[a[i][j]]++
			}
		}
	}

	if a[n-1][m-1] == INF {
		fmt.Println(0)
		return
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF {
		ans = 0
	}
	fmt.Println(ans)
	return
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) && a[i][j] != INF {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			fmt.Println(c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	fmt.Println(a)

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) {
				mp[a[i][j]]++
			}
		}
	}

	fmt.Println(a)
	fmt.Println(mp)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

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

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

var n, m int
var a [][]int
var mp map[int]int

func dfs(i, j, k int) bool {
	if i >= n || j >= m {
		return false
	}
	if i == n-1 && j == m-1 {
		return true
	}
	if a[i][j] == -1 {
		return false
	}
	if a[i][j] != 0 {
		return true
	}

	ret := false
	if dfs(i+1, j, k+1) {
		ret = true
	}
	if dfs(i, j+1, k+1) {
		ret = true
	}
	if ret == true && !(i == 0 && j == 0) {
		a[i][j] = k
		mp[k]++
		return true
	}
	a[i][j] = -1
	return false
}

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

	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a = make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Fscanf(reader, "%c", &c)
			if c == '.' {
				a[i][j] = 0
			} else {
				a[i][j] = -1
			}
		}
		// fmt.Fscanf(reader, "%c", &c)
	}

	mp = make(map[int]int)
	dfs(0, 0, 0)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF || mp[n+m-3] == 0 {
		ans = 0
	}
	fmt.Println(ans)

	return
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanf("%c", &c)
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) {
				mp[a[i][j]]++
			}
		}
	}

	fmt.Println(a)
	fmt.Println(mp)

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

const (
	// INF 无穷大
	INF = 0x3f3f3f2f
)

func main() {
	var n, m int
	fmt.Scanf("%d %d\n", &n, &m)
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		var c byte
		for j := 0; j < m; j++ {
			fmt.Scanf("%c", &c)
			if c == '.' {
				a[i][j] = INF
			} else {
				a[i][j] = -1
			}
		}
		fmt.Scanln()
	}

	a[0][0] = 0
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if a[i][j] == -1 {
				continue
			}
			if i > 0 && a[i-1][j] != -1 && a[i-1][j] != INF {
				a[i][j] = a[i-1][j] + 1
			} else if j > 0 && a[i][j-1] != -1 && a[i][j-1] != INF {
				if a[i][j] > a[i][j-1]+1 {
					a[i][j] = a[i][j-1] + 1
				}
			}
			if !(i == 0 && j == 0 || i == n-1 && j == m-1) && a[i][j] != INF {
				mp[a[i][j]]++
			}
		}
	}

	ans := INF
	for _, v := range mp {
		if ans > v {
			ans = v
		}
	}
	if ans == INF {
		ans = 0
	}
	fmt.Println(ans)
}

package main

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

func dfs(cell [][]byte, i, j int) bool {
	switch cell[i][j] {
	case '#':
		return false
	case '$':
		return true
	}
	cell[i][j] = '#'
	return j+1 < len(cell[i]) && dfs(cell, i, j+1) ||
		i+1 < len(cell) && dfs(cell, i+1, j)
}

func dfsR(cell [][]byte, i, j int) bool {
	switch cell[i][j] {
	case '#':
		return false
	case '$':
		return true
	}
	cell[i][j] = '#'
	return j > 0 && dfsR(cell, i, j-1) ||
		i > 0 && dfsR(cell, i-1, j)
}

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

	var n, m int
	Fscan(in, &n, &m)
	containForest := false
	cell := make([][]byte, n)
	for i := range cell {
		Fscan(in, &cell[i])
		containForest = containForest && bytes.Contains(cell[i], []byte{'#'})
	}

	if n == 1 || m == 1 {
		if containForest {
			Fprint(out, 0)
		} else {
			Fprint(out, 1)
		}
		return
	}

	ans := 0
	cell[n-1][m-1] = '$'
	if dfs(cell, 0, 1) {
		ans++
	}
	cell[0][0] = '$'
	if dfsR(cell, n-1, m-2) {
		ans++
	}
	Fprint(out, ans)
}

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