package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]int, n)

	for i := range a {
		a[i] = nextInt()
	}

	for i := range a {
		if i == 0 {
			println(a[1]-a[0], a[n-1]-a[0])
		} else if i == n-1 {
			println(a[n-1]-a[n-2], a[n-1]-a[0])
		} else {
			println(min(a[i+1]-a[i], a[i]-a[i-1]), max(a[n-1]-a[i], a[i]-a[0]))
		}
	}
}

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

	return b
}

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

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

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

func nextInt64() int64 {
	n, _ := strconv.ParseInt(next(), 0, 64)
	return n
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{})               { fmt.Fprintln(writer, a...) }
func print(a ...interface{})                 { fmt.Fprint(writer, a...) }
func printf(format string, a ...interface{}) { fmt.Fprintf(writer, format, a...) }

package main

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

var in *bufio.Scanner = bufio.NewScanner(os.Stdin)
var out *bufio.Writer = bufio.NewWriter(os.Stdout)

func main() {
	defer out.Flush()
	in.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]int, n + 2)
	for i := 1; i <= n; i++ {
		a[i] = nextInt()
	}
	a[0], a[n + 1] = a[2], a[n - 1]

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", min(abs(a[i] - a[i - 1]), abs(a[i] - a[i + 1])), max(abs(a[i] - a[1]), abs(a[i] - a[n])))
	}
}

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

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

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

func nextInt() int {
	in.Scan()
	res, _ := strconv.Atoi(in.Text())
	return res
}

package main

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

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

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

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

func main() {
    stdin := bufio.NewReader(os.Stdin)
    var n int
    fmt.Fscanf(stdin, "%d\n", &n)

    cities := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Fscanf(stdin, "%d", &cities[i])
    }

    fmt.Println(cities[1]-cities[0], cities[n-1]-cities[0])
    for i := 1; i < n-1; i++ {
        lo := min(cities[i]-cities[i-1], cities[i+1]-cities[i])
        hi := max(cities[n-1] - cities[i], cities[i] - cities[0])
        fmt.Println(lo, hi)
    }
    fmt.Println(cities[n-1]-cities[n-2], cities[n-1]-cities[0])
}

package main

import "fmt"

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 abs(v int) int {
    if v < 0 {
        return -v
    }
    return v
}

func main() {
    var n int
    fmt.Scanf("%d\n", &n)

    cities := make([]int, n)
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &cities[i])
    }

    fmt.Println(cities[1]-cities[0], cities[n-1]-cities[0])
    for i := 1; i < n-1; i++ {
        lo := min(cities[i]-cities[i-1], cities[i+1]-cities[i])
        hi := max(cities[n-1] - cities[i], cities[i] - cities[0])
        fmt.Println(lo, hi)
    }
    fmt.Println(cities[n-1]-cities[n-2], cities[n-1]-cities[0])
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	coordinats := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&coordinats[i])
	}

	for i := 0; i < n; i++ {
		min := int64(1e11 + 5)
		left := i - 1
		right := i + 1
		if left >= 0 {
			if coordinats[i]-coordinats[left] < min {
				min = coordinats[i] - coordinats[left]
			}
		}
		if right < n {
			if coordinats[right]-coordinats[i] < min {
				min = coordinats[right] - coordinats[i]
			}
		}

		max := coordinats[i] - coordinats[0]
		if max < coordinats[n-1]-coordinats[i] {
			max = coordinats[n-1] - coordinats[i]
		}

		fmt.Printf("%d %d\n", min, max)
	}
}

package main

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

func min(a1, a2 int) int {
	if a1 < a2 {
		return a1
	}

	return a2
}

func max(a1, a2 int) int {
	if a1 > a2 {
		return a1
	}

	return a2
}

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

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

	num := 0
	fmt.Fscanf(r, "%d\n", &num)
	coords := make([]int, num, num)
	for i := 0; i < num; i++ {
		fmt.Fscanf(r, "%d ", &coords[i])
	}

	sort.Ints(coords)

	cmin := coords[0]
	cmax := coords[num-1]

	minimal := coords[1] - cmin
	maximal := cmax - cmin

	fmt.Printf("%d %d\n", minimal, maximal)

	for i := 1; i < num-1; i++ {
		minimal = min(abs(coords[i]-coords[i-1]), abs(coords[i]-coords[i+1]))
		maximal = max(abs(coords[i]-cmax), abs(coords[i]-cmin))

		fmt.Printf("%d %d\n", minimal, maximal)
	}

	minimal = cmax - coords[num-2]
	maximal = cmax - cmin

	fmt.Printf("%d %d\n", minimal, maximal)
}

package main

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

func absolute(a int) int {
	if a < 0 {
		a *= -1
	}
	return a
}
func main() {
	var n, min, max, temp int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	positionsArr := make([]int, n)
	for index := 0; index < n; index++ {
		fmt.Fscan(reader, &positionsArr[index])
	}
	for index := 0; index < n; index++ {
		if index == 0 {
			min = absolute(positionsArr[index] - positionsArr[index+1])
			max = absolute(positionsArr[index] - positionsArr[n-1])
		} else if index == n-1 {
			min = absolute(positionsArr[index] - positionsArr[index-1])
			max = absolute(positionsArr[index] - positionsArr[0])
		} else {
			min = absolute(positionsArr[index] - positionsArr[index-1])
			temp = absolute(positionsArr[index] - positionsArr[index+1])
			if min > temp {
				min = temp
			}
			max = absolute(positionsArr[index] - positionsArr[n-1])
			temp = absolute(positionsArr[index] - positionsArr[0])
			if max < temp {
				max = temp
			}
		}
		fmt.Println(min, max)
	}
}

package main

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

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

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

func main() {
	stdin := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscanf(stdin, "%d\n", &n)

	cities := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(stdin, "%d", &cities[i])
	}

	fmt.Println(cities[1]-cities[0], cities[n-1]-cities[0])

	for i := 1; i < n-1; i++ {
		lo := min(cities[i]-cities[i-1], cities[i+1]-cities[i])
		hi := max(cities[n-1]-cities[i], cities[i]-cities[0])
		fmt.Println(lo, hi)

	}
	fmt.Println(cities[n-1]-cities[n-2], cities[n-1]-cities[0])

}

package main

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

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

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var n, j int
	var ox []float64

	n = readInt()
	for i := 0; i < n; i++ {
		j = readInt()
		ox = append(ox, float64(j))
	}

	fmt.Fprintln(writer, int64(ox[1]-ox[0]), int64(ox[n-1]-ox[0]))
	for i := 1; i < n-1; i++ {
		fmt.Fprintln(writer, int64(math.Min(ox[i+1]-ox[i], ox[i]-ox[i-1])), int64(math.Max(ox[n-1]-ox[i], ox[i]-ox[0])))
	}
	fmt.Fprintln(writer, int64(ox[n-1]-ox[n-2]), int64(ox[n-1]-ox[0]))
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	coordinats := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&coordinats[i])
	}

	for i := 0; i < n; i++ {
		min := int64(1e9 + 5)
		left := i - 1
		right := i + 1
		if left >= 0 {
			if coordinats[i]-coordinats[left] < min {
				min = coordinats[i] - coordinats[left]
			}
		}
		if right < n {
			if coordinats[right]-coordinats[i] < min {
				min = coordinats[right] - coordinats[i]
			}
		}

		max := coordinats[i] - coordinats[0]
		if max < coordinats[n-1]-coordinats[i] {
			max = coordinats[n-1] - coordinats[i]
		}

		fmt.Printf("%d %d\n", min, max)
	}
}

package main

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

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

func readInt() int {
	scanner.Scan()
	ans, _ := strconv.Atoi(scanner.Text())
	return ans
}

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	var n, j int
	var ox []float64

	n = readInt()
	for i := 0; i < n; i++ {
		j = readInt()
		ox = append(ox, float64(j))
	}

	fmt.Fprintln(writer, ox[1]-ox[0], ox[n-1]-ox[0])
	for i := 1; i < n-1; i++ {
		fmt.Fprintln(writer, math.Min(ox[i+1]-ox[i], ox[i]-ox[i-1]), math.Max(ox[n-1]-ox[i], ox[i]-ox[0]))
	}
	fmt.Fprintln(writer, ox[n-1]-ox[n-2], ox[n-1]-ox[0])
}


package main

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

var (
	sumall int64 = 0
)

func AddEdge(edges [][]int, from, to int) {
	if 0== len(edges[from]){
		edges[from] = make([]int,0)
	}
	edges[from] = append(edges[from],to)
}
func dfsSum(edges [][]int, sum, v, p []int64, root, pre int) {
	sum[root] = p[root]
	v[root] = 0
	for _, next := range edges[root] {
		if next == pre {
			continue
		}
		dfsSum(edges, sum, v, p, next, root)
		sum[root] += sum[next]
		v[root] += v[next] + sum[next]
	}
}
func dfs(edges [][]int, sum, v, p []int64, root, pre int, ppc int64) int64 {
	vans := v[root] + ppc +sumall-sum[root]
	ans := vans
//	fmt.Println("root--",root,"vrot",v[root],"ppc",ppc,"sumall",sumall,"sumroot",sum[root],"ans",ans)
	for _, next := range edges[root] {
		if next == pre {
			continue
		}
		pc := vans-v[next]-sum[next]
		tmp := dfs(edges, sum, v, p, next, root,pc)
		if tmp > ans{
			ans = tmp
		}
	}
	return ans

}
func main() {

	buf := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscanf(buf, "%d\n", &n)
	p := make([]int64, n+1)
	sum := make([]int64, n+1)
	v := make([]int64, n+1)
	edge := make([][]int, n+1)

	for i := 1; i <= n; i++ {
		fmt.Fscanf(buf, "%d", &p[i])
		sumall += p[i]
	}
	for i := 1; i <= n; i++ {
		var from, to int
		fmt.Fscanf(buf, "%d %d\n", &from, &to)
		AddEdge(edge, from, to)
		AddEdge(edge, to, from)
	}
	dfsSum(edge,sum,v,p,1,0)
//	fmt.Println("v",v,"sum=",sum,"edge=",edge)
	ans := dfs(edge,sum,v,p,1,0,0)
	fmt.Printf("%d\n",ans)
}

package main

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

var n int
var res int64
var adj map[int64][]int64
var w, parent, sum []int64
var seen []bool

func Connect (u, v int64) {
    adj[u] = append(adj[u], v)
    adj[v] = append(adj[v], u)
}

func BuildTree (u, rank int64) (int64, int64) {
    if seen[u] {
        return 0, 0
    }
    var re = w[u] * rank
    seen[u] = true
    sum[u] = w[u]
    for _, v := range(adj[u]) {
        if !seen[v] {
            parent[v] = u
            var a, b = BuildTree(v, rank + 1)
            re += a
            sum[u] += b
        }
    }
    return re, sum[u]
}

func Max (u, v int64) int64 {
    if u > v {
        return u
    } else {
        return v
    }
}

func Reroot (u, v, old, brand int64) int64 {
    var tmp1 = old + brand - 2 * sum[v]
    var res = Max(old, tmp1)
    seen[v] = true
    // fmt.Println("Reroot", u, "to", v, "new sum is", res, tmp1, old, sum[u], sum[v])
    for _, x := range(adj[v]) {
        if !seen[x] {
            var tmp2 = Reroot(v, x, tmp1, brand)
            res = Max(res, tmp2)
        }
    }
    return res
}

func main() {
    start := time.Now()
    defer func () {
        run := time.Since(start)
        _ = run
        // fmt.Println("Run in", run)
    }()

    stdin := bufio.NewReader(os.Stdin)

    fmt.Fscan(stdin, &n)

    w = make([]int64, n)
    parent = make([]int64, n)
    seen = make([]bool, n)
    sum = make([]int64, n)

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

    adj = make(map[int64][]int64, n)
    for i := 0; i < n-1; i++ {
        var u, v int64
        fmt.Fscan(stdin, &u, &v)
        Connect(u - 1, v - 1)
    }
    var s1, _ = BuildTree(0, 0)

    seen = make([]bool, n)
    seen[0] = true
    for _, x := range(adj[0]) {
        res = Max(res, Reroot(0, x, s1, sum[0]))
    }

    // fmt.Println(sum)
    // fmt.Println(parent)
    // fmt.Println(s1, s2)
    fmt.Println(res)
}

package main

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

var inp *bufio.Scanner
var out *bufio.Writer

var V, res, rootAns int64
var adjList [][]int64
var a, sum []int64

func dfs(u, p, dist int64) {
	rootAns += dist * a[u]
	sum[u] = a[u];
	for _, v := range adjList[u] {
		if v != p {
			dfs(v, u, dist + 1)
			sum[u] += sum[v]
		}
	}
}

func dfs2(u, p, cur int64) {
	for _, v := range adjList[u] {
		if v != p {
			nxt := cur + sum[0] - 2 * sum[v]
			res = max(res, cur + sum[0] - 2 * sum[v])
			dfs2(v, u, nxt)
		}
	}
}

func main() {
	io()
	defer out.Flush()

	V = nextInt()
	adjList = make([][]int64, V)
	a = make([]int64, V)
	sum = make([]int64, V)
	for i := int64(0); i < V; i++ {
		a[i] = nextInt()
	}
	for i := int64(0); i < V - 1; i++ {
		u := nextInt() - 1
		v := nextInt() - 1
		adjList[u] = append(adjList[u], v)
		adjList[v] = append(adjList[v], u)
	}

	dfs(0, -1, 0)
	res = rootAns
	dfs2(0, -1, rootAns)
	out.WriteString(fmt.Sprintf("%d", res))
}

func max (a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func io() {
	inp = bufio.NewScanner(os.Stdin)
	inp.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}
func nextString() string {
	inp.Scan()
	return inp.Text();
}

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

package main

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

var inp *bufio.Scanner
var out *bufio.Writer

var V int
var res, rootAns int64
var adjList [][]int
var a, sum []int64

func dfs(u, p, dist int) {
	rootAns += int64(dist) * a[u]
	sum[u] = a[u];
	for _, v := range adjList[u] {
		if v != p {
			dfs(v, u, dist + 1)
			sum[u] += sum[v]
		}
	}
}

func dfs2(u, p int, cur int64) {
	for _, v := range adjList[u] {
		if v != p {
			nxt := cur + sum[0] - 2 * sum[v]
			res = max(res, cur + sum[0] - 2 * sum[v])
			dfs2(v, u, nxt)
		}
	}
}

func main() {
	io()
	defer out.Flush()

	V = nextInt()
	adjList = make([][]int, V)
	a = make([]int64, V)
	sum = make([]int64, V)
	for i := 0; i < V; i++ {
		a[i] = nextInt64()
	}
	for i := 0; i < V - 1; i++ {
		u := nextInt() - 1
		v := nextInt() - 1
		adjList[u] = append(adjList[u], v)
		adjList[v] = append(adjList[v], u)
	}

	dfs(0, -1, 0)
	res = rootAns
	dfs2(0, -1, rootAns)
	out.WriteString(fmt.Sprintf("%d", res))
}

func max (a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func io() {
	inp = bufio.NewScanner(os.Stdin)
	inp.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}
func nextString() string {
	inp.Scan()
	return inp.Text();
}

func nextInt64() int64 {
	v, _ := strconv.ParseInt(nextString(), 10, 64)
	return int64(v)
}
func nextInt() int {
	v, _ := strconv.ParseInt(nextString(), 10, 64)
	return int(v)
}

package main

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

var t [][]int64
var tt int64
var s []int64
var ds []int64
var dp []int64
var as []int64

func main() {
	var n int64
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n)
	as = make([]int64, n + 1)
	for i := int64(1); i <= n; i++ {
		fmt.Fscan(reader, &as[i])
	}
	t = make([][]int64, n + 1)
	for i := int64(1); i < n; i++ {
		var p, q int64
		fmt.Fscan(reader, &p)
		fmt.Fscan(reader, &q)
		t[p] = append(t[p], q)
		t[q] = append(t[q], p)
	}
	s = make([]int64, n + 1)
	ds = make([]int64, n + 1)
	ds[0] = -1
	dfs1(1, 0)
	tt = 0
	for i, d := range ds {
		tt += d * as[i]
	}
	dp = make([]int64, n + 1)
	dfs2(1, 0)
	var rs int64
	for _, d := range dp {
		if d > rs {
			rs = d
		}
	}
	fmt.Println(rs)
}

func dfs1(r, p int64) {
	s[r] = as[r]
	ds[r] = ds[p] + 1
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs1(c, r)
		s[r] += s[c]
	}
}

func dfs2(r, p int64) {
	if r == 1 {
		dp[1] = tt
	} else {
		dp[r] = dp[p] - s[r] + s[1] - s[r]
	}
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs2(c, r)
	}
}

package main

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

var (
	n  int64
	t  [][]int64
	tt int64
	s  []int64
	ds []int64
	dp []int64
	as []int64
	rs int64
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n)
	as = make([]int64, n+1)
	for i := int64(1); i <= n; i++ {
		fmt.Fscan(reader, &as[i])
	}
	t = make([][]int64, n+1)
	for i := int64(1); i < n; i++ {
		var p, q int64
		fmt.Fscan(reader, &p)
		fmt.Fscan(reader, &q)
		t[p] = append(t[p], q)
		t[q] = append(t[q], p)
	}
	s = make([]int64, n+1)
	ds = make([]int64, n+1)
	ds[0] = -1
	dfs1(1, 0)
	tt = 0
	for i, d := range ds {
		tt += d * as[i]
	}
	dp = make([]int64, n+1)
	dfs2(1, 0)
	for _, d := range dp {
		if d > rs {
			rs = d
		}
	}
	fmt.Println(rs)
}

func dfs1(r, p int64) {
	s[r] = as[r]
	ds[r] = ds[p] + 1
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs1(c, r)
		s[r] += s[c]
	}
}

func dfs2(r, p int64) {
	if r == 1 {
		dp[1] = tt
	} else {
		dp[r] = dp[p] - s[r] + s[1] - s[r]
	}
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs2(c, r)
	}
}

package main

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

type vertex struct {
	v                   int64
	conns               []*vertex
	costColor, sumColor int
	missing             *vertex
	costs, sums         map[*vertex]int64
	cost, sum           int64
}

func newVertex(v int64) *vertex {
	return &vertex{
		v:     v,
		costs: map[*vertex]int64{},
		sums:  map[*vertex]int64{},
		cost:  -1,
		sum:   -1,
	}
}

func (v *vertex) Cost(p *vertex) int64 {
	if v.costColor == 2 {
		return v.cost - v.costs[p]
	}
	if v.costColor == 1 {
		if v.missing == p {
			return v.cost
		}
		t := v.missing.Sum(v) + v.missing.Cost(v) + v.missing.Value()
		v.cost += t
		v.costs[v.missing] = t
		v.costColor = 2
		return v.Cost(p)
	}
	v.costColor = 1
	v.missing = p
	v.cost = 0
	for _, conn := range v.conns {
		if p == conn {
			continue
		}
		t := conn.Sum(v) + conn.Cost(v) + conn.Value()
		v.cost += t
		v.costs[conn] = t
	}
	if p == nil {
		v.costColor = 2
	}
	return v.cost
}
func (v *vertex) Sum(p *vertex) int64 {
	if v.sumColor == 2 {
		return v.sum - v.sums[p]
	}
	if v.sumColor == 1 {
		if v.missing == p {
			return v.sum
		}
		t := v.missing.Sum(v) + v.missing.Value()
		v.sum += t
		v.sums[v.missing] = t
		v.sumColor = 2
		return v.Sum(p)
	}
	v.sumColor = 1
	v.missing = p
	v.sum = 0
	for _, conn := range v.conns {
		if p == conn {
			continue
		}
		t := conn.Sum(v) + conn.Value()
		v.sum += t
		v.sums[conn] = t
	}
	if p == nil {
		v.sumColor = 2
	}
	return v.sum
}

func (v *vertex) Value() int64 {
	return v.v
}

func connect(l, r *vertex) {
	l.conns = append(l.conns, r)
	r.conns = append(r.conns, l)
}

func read() []*vertex {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	vs := make([]*vertex, 0, n)
	for i := 0; i < n; i++ {
		var v int64
		fmt.Fscan(r, &v)
		vs = append(vs, newVertex(v))
	}
	for i := 1; i < n; i++ {
		var a, b int
		fmt.Fscan(r, &a, &b)
		connect(vs[a-1], vs[b-1])
	}
	return vs
}

func main() {
	vs := read()
	var ans int64
	for _, v := range vs {
		if t := v.Cost(nil); ans < t {
			ans = t
		}
	}
	fmt.Println(ans)
}

package main

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

var t [][]int
var tt int
var s []int
var ds []int
var dp []int
var as []int

func main() {
	var n int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n)
	as = make([]int, n + 1)
	for i := 1; i <= n; i++ {
		fmt.Fscan(reader, &as[i])
	}
	t = make([][]int, n + 1)
	for i := 1; i < n; i++ {
		var p, q int
		fmt.Fscan(reader, &p)
		fmt.Fscan(reader, &q)
		t[p] = append(t[p], q)
		t[q] = append(t[q], p)
	}
	s = make([]int, n + 1)
	ds = make([]int, n + 1)
	ds[0] = -1
	dfs1(1, 0)
	tt = 0
	for i, d := range ds {
		tt += d * as[i]
	}
	dp = make([]int, n + 1)
	dfs2(1, 0)
	rs := 0
	for _, d := range dp {
		if d > rs {
			rs = d
		}
	}
	fmt.Println(rs)
}

func dfs1(r, p int) {
	s[r] = as[r]
	ds[r] = ds[p] + 1
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs1(c, r)
		s[r] += s[c]
	}
}

func dfs2(r, p int) {
	if r == 1 {
		dp[1] = tt
	} else {
		dp[r] = dp[p] - s[r] + s[1] - s[r]
	}
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs2(c, r)
	}
}

package main

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

var t [][]int
var tt int
var s []int
var ds []int
var dp []int
var as []int

func main() {
	var n int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n)
	as = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &as[i])
	}
	t = make([][]int, n)
	for i := 1; i < n; i++ {
		var p, q int
		fmt.Fscan(reader, &p)
		fmt.Fscan(reader, &q)
		t[p-1] = append(t[p-1], q-1)
		t[q-1] = append(t[q-1], p-1)
	}
	s = make([]int, n)
	ds = make([]int, n)
	dfs1(0, -1)
	tt = 0
	for i, d := range ds {
		tt += d * as[i]
	}
	dp = make([]int, n)
	dfs2(0, -1)
	rs := 0
	for _, d := range dp {
		if d > rs {
			rs = d
		}
	}
	fmt.Println(rs)
}

func dfs1(r, p int) {
	s[r] += as[r]
	if r == 0 {
		ds[0] = 0
	} else {
		ds[r] = ds[p] + 1
	}
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs1(c, r)
		s[r] += s[c]
	}
}

func dfs2(r, p int) {
	if r == 0 {
		dp[0] = tt
	} else {
		dp[r] = dp[p] - s[r] + s[0] - s[r]
	}
	for _, c := range t[r] {
		if c == p {
			continue
		}
		dfs2(c, r)
	}
}

package main

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

func main() {
	var n int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n)
	as := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &as[i])
	}
	t := make([]int, n)
	t[0] = -1
	for i := 0; i < n-1; i++ {
		var p, q int
		fmt.Fscan(reader, &p)
		fmt.Fscan(reader, &q)
		t[q-1] = p - 1
	}
	ds := make([]int, n)
	for i := range ds {
		ds[i] = -1
	}
	fmt.Println("1111")
	rs := 0
	for i := 0; i < n; i++ {
		// Build tree.
		tree(i, t)
		t[i] = -1
		// Init distances.
		ds[i] = 0
		dfs(i, t, ds)
		s := 0
		for i, d := range ds {
			s += d * as[i]
		}
		if s > rs {
			rs = s
		}
	}
	fmt.Println(rs)
}

func tree(r int, t []int) {
	if t[r] == -1 {
		return
	}
	tree(t[r], t)
	t[t[r]] = r
}

func dfs(r int, t []int, ds []int) {
	if t[r] != -1 {
		for i := range ds {
			if t[i] == -1 {
				continue
			}
			ds[i] = ds[t[i]] + 1
		}
	}
	for n := range t {
		if t[n] == r {
			dfs(n, t, ds)
		}
	}
}

package main

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

var n, res int
var adj map[int][]int
var w, parent, sum []int
var seen []bool

func Connect (u, v int) {
    adj[u] = append(adj[u], v)
    adj[v] = append(adj[v], u)
}

func BuildTree (u, rank int) (int, int) {
    if seen[u] {
        return 0, 0
    }
    var re = w[u] * rank
    seen[u] = true
    sum[u] = w[u]
    for _, v := range(adj[u]) {
        if !seen[v] {
            parent[v] = u
            var a, b = BuildTree(v, rank + 1)
            re += a
            sum[u] += b
        }
    }
    return re, sum[u]
}

func Max (u, v int) int {
    if u > v {
        return u
    } else {
        return v
    }
}

func Reroot (u, v, old, brand int) int {
    var tmp1 = old + brand - 2 * sum[v]
    var res = Max(old, tmp1)
    seen[v] = true
    // fmt.Println("Reroot", u, "to", v, "new sum is", res, tmp1, old, sum[u], sum[v])
    for _, x := range(adj[v]) {
        if !seen[x] {
            var tmp2 = Reroot(v, x, tmp1, brand)
            res = Max(res, tmp2)
        }
    }
    return res
}

func main() {
    start := time.Now()
    defer func () {
        run := time.Since(start)
        _ = run
        // fmt.Println("Run in", run)
    }()

    stdin := bufio.NewReader(os.Stdin)

    fmt.Fscan(stdin, &n)

    w = make([]int, n)
    parent = make([]int, n)
    seen = make([]bool, n)
    sum = make([]int, n)

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

    adj = make(map[int][]int, n)
    for i := 0; i < n-1; i++ {
        var u, v int
        fmt.Fscan(stdin, &u, &v)
        Connect(u - 1, v - 1)
    }
    var s1, _ = BuildTree(0, 0)

    seen = make([]bool, n)
    seen[0] = true
    for _, x := range(adj[0]) {
        res = Max(res, Reroot(0, x, s1, sum[0]))
    }

    // fmt.Println(sum)
    // fmt.Println(parent)
    // fmt.Println(s1, s2)
    fmt.Println(res)
}

package main

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

type pii struct {
	v, r int
	old  int
}

func mp(a, b int) pii {
	return pii{v: a, r: b, old: 0}
}

func main() {
	var stdin *bufio.Reader
	// LOCAL
	f, _ := os.Open("input.txt")
	stdin = bufio.NewReader(f)

	// ONLINE JUDGE
	stdin = bufio.NewReader(os.Stdin)

	var n, res, all, tmp int
	fmt.Fscan(stdin, &n)

	var w = make([]int, n)
	var sum = make([]int, n)
	var queue []pii
	var adj = make(map[int][]int, n)
	var parent = make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(stdin, &w[i])
		all += w[i]
		sum[i] = w[i]
	}
	for i := 0; i < n-1; i++ {
		var a, b int
		fmt.Fscan(stdin, &a, &b)
		a--
		b--
		adj[a] = append(adj[a], b)
	}

	sum[0] = w[0]
	queue = []pii{
		pii{v: 0, r: 0},
	}

	// DFS - Build Tree
	for len(queue) > 0 {
		var pop = queue[0]
		queue = queue[1:]

		tmp += w[pop.v] * pop.r
		for _, u := range adj[pop.v] {
			queue = append(queue, mp(u, pop.r+1))
			parent[u] = pop.v
		}
	}
	// Trace - build sum array
	for i := 0; i < n; i++ {
		var j = i
		for j > 0 {
			sum[parent[j]] += w[j]
			j = parent[j]
		}
	}
	sum[0] = all

	// DFS 2 - Moving sum around
	queue = nil
	res = tmp
	for _, u := range adj[0] {
		var newpii = mp(u, sum[u])
		newpii.old = tmp
		queue = append(queue, newpii)
	}
	for len(queue) > 0 {
		var pop = queue[0]
		queue = queue[1:]
		tmp = pop.old + all - 2*pop.r
		if tmp > res {
			res = tmp
		}
		for _, v := range adj[pop.v] {
			var newpii = mp(v, sum[v])
			newpii.old = tmp
			queue = append(queue, newpii)
		}
	}

	fmt.Println(res)

}

package main

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

type pii struct {
	v, r int64
	old  int64
}

func mp(a, b int64) pii {
	return pii{v: a, r: b, old: 0}
}

func main() {
	var stdin *bufio.Reader
	// LOCAL
	f, _ := os.Open("input.txt")
	stdin = bufio.NewReader(f)

	// ONLINE JUDGE
	stdin = bufio.NewReader(os.Stdin)

	var n int
	var res, all, tmp int64
	fmt.Fscan(stdin, &n)

	var w = make([]int64, n)
	var sum = make([]int64, n)
	var queue []pii
	var adj = make(map[int64][]int64, n)
	var parent = make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscan(stdin, &w[i])
		all += w[i]
		sum[i] = w[i]
	}
	for i := 0; i < n-1; i++ {
		var a, b int64
		fmt.Fscan(stdin, &a, &b)
		a--
		b--
		adj[a] = append(adj[a], b)
	}

	sum[0] = w[0]
	queue = []pii{
		pii{v: 0, r: 0},
	}

	// DFS - Build Tree
	for len(queue) > 0 {
		var pop = queue[0]
		queue = queue[1:]

		tmp += w[pop.v] * pop.r
		for _, u := range adj[pop.v] {
			queue = append(queue, mp(u, pop.r+1))
			parent[u] = int(pop.v)
		}
	}
	// Trace - build sum array
	for i := 0; i < n; i++ {
		var j = i
		for j > 0 {
			sum[parent[j]] += w[j]
			j = parent[j]
		}
	}
	sum[0] = all

	// DFS 2 - Moving sum around
	queue = nil
	res = tmp
	for _, u := range adj[0] {
		var newpii = mp(u, sum[u])
		newpii.old = tmp
		queue = append(queue, newpii)
	}
	for len(queue) > 0 {
		var pop = queue[0]
		queue = queue[1:]
		tmp = pop.old + all - 2*pop.r
		if tmp > res {
			res = tmp
		}
		for _, v := range adj[pop.v] {
			var newpii = mp(v, sum[v])
			newpii.old = tmp
			queue = append(queue, newpii)
		}
	}

	fmt.Println(res)

}

package main

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

var inp *bufio.Scanner
var out *bufio.Writer

var V, res int64
var adjList [][]int64
var a, sub, sum []int64

func dfs(u, p, dist int64) {
	sum[u] = a[u];
	for _, v := range adjList[u] {
		if v != p {
			sub[u] += a[v]
			dfs(v, u, dist + 1)
			sub[u] += 2 * sub[v]
			sum[u] += sum[v]
		}
	}
}

func dfs2(u, p, cur int64) {
	for _, v := range adjList[u] {
		if v != p {
			nxt := cur + sum[0] - 2 * sum[v]
			res = max(res, cur + sum[0] - 2 * sum[v])
			dfs2(v, u, nxt)
		}
	}
}

func main() {
	io()
	defer out.Flush()

	V = nextInt()
	adjList = make([][]int64, V)
	a = make([]int64, V)
	sub = make([]int64, V)
	sum = make([]int64, V)
	for i := int64(0); i < V; i++ {
		a[i] = nextInt()
	}
	for i := int64(0); i < V - 1; i++ {
		u := nextInt() - 1
		v := nextInt() - 1
		adjList[u] = append(adjList[u], v)
		adjList[v] = append(adjList[v], u)
	}

	dfs(0, -1, 0)
	res = sub[0]
	dfs2(0, -1, sub[0])
	out.WriteString(fmt.Sprintf("%d", res))
}

func max (a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func io() {
	inp = bufio.NewScanner(os.Stdin)
	inp.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}
func nextString() string {
	inp.Scan()
	return inp.Text();
}

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

package main

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

var inp *bufio.Scanner
var out *bufio.Writer

var V, res int
var adjList [][]int
var a, sub, sum []int

func dfs(u, p, dist int) {
	sum[u] = a[u];
	for _, v := range adjList[u] {
		if v != p {
			sub[u] += a[v]
			dfs(v, u, dist + 1)
			sub[u] += 2 * sub[v]
			sum[u] += sum[v]
		}
	}
}

func dfs2(u, p, cur int) {
	for _, v := range adjList[u] {
		if v != p {
			nxt := cur + sum[0] - 2 * sum[v]
			res = max(res, cur + sum[0] - 2 * sum[v])
			dfs2(v, u, nxt)
		}
	}
}

func main() {
	io()
	defer out.Flush()

	V = nextInt()
	adjList = make([][]int, V)
	a = make([]int, V)
	sub = make([]int, V)
	sum = make([]int, V)
	for i := 0; i < V; i++ {
		a[i] = nextInt()
	}
	for i := 0; i < V - 1; i++ {
		u := nextInt() - 1
		v := nextInt() - 1
		adjList[u] = append(adjList[u], v)
		adjList[v] = append(adjList[v], u)
	}

	dfs(0, -1, 0)
	res = sub[0]
	dfs2(0, -1, sub[0])
	out.WriteString(fmt.Sprintf("%d", res))
}

func max (a, b int) int {
	if a > b {
		return a
	}
	return b
}
func io() {
	inp = bufio.NewScanner(os.Stdin)
	inp.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}
func nextString() string {
	inp.Scan()
	return inp.Text();
}

func nextInt() int {
	v, _ := strconv.ParseInt(nextString(), 10, 64)
	return int(v)
}

package main

import (
	"fmt"
	"strings"
)

const input = `8
9 4 1 7 10 1 6 5
1 2
2 3
1 4
1 5
5 6
5 7
5 8
`

type vertex struct {
	val      int64
	connects []*vertex
}

func (v *vertex) Connect(rhv *vertex) {
	v.connects = append(v.connects, rhv)
}

func (v *vertex) Calculate(p *vertex, depth int) int64 {
	var res = v.val * int64(depth)
	for _, conn := range v.connects {
		if conn == p {
			continue
		}
		res += conn.Calculate(v, depth+1)
	}
	return res
}

func main() {
	sr := strings.NewReader(input)
	var n int
	if _, err := fmt.Fscan(sr, &n); err != nil {
		panic(err)
	}
	var vertices = make([]*vertex, 0, n)
	for i := 0; i < n; i++ {
		var v int64
		if _, err := fmt.Fscan(sr, &v); err != nil {
			panic(err)
		}
		vertices = append(vertices, &vertex{val: v})
	}
	for i := 0; i < n-1; i++ {
		var l, r int
		if _, err := fmt.Fscan(sr, &l, &r); err != nil {
			panic(err)
		}
		lv, rv := vertices[l-1], vertices[r-1]
		lv.Connect(rv)
		rv.Connect(lv)
	}
	var ans int64
	for _, v := range vertices {
		if t := v.Calculate(nil, 0); ans < t {
			ans = t
		}
	}
	fmt.Println(ans)
}


package main

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

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

	tc := readNum(reader)

	for tc > 0 {
		tc--
		A := readNNums(reader, 4)
		fmt.Println(solve(A[0], A[1], A[2], A[3]))
	}
}

func solve(a, b, c, d int) int64 {
	A, B, C, D := int64(a), int64(b), int64(c), int64(d)
	return (C-A)*(D-B) + 1
}

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

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

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

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

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

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

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

	return i
}

package main

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

var bytes []byte
var l, max int

func fastScan() int {
	b := bytes[l]

	// Check if the bytes is not a number
	for b < 48 || b > 57 {
		l++
		b = bytes[l]
	}

	result := 0
	for 47 < b && b < 58 {
		result = (result << 3) + (result << 1) + int(b-48)

		l++
		if l > max {
			return result
		}
		b = bytes[l]
	}
	return result
}

func main(){
	bytes, _ = ioutil.ReadAll(bufio.NewReader(os.Stdin))
	max = len(bytes) - 1

	ws := bufio.NewWriter(os.Stdout)

	T := fastScan()
	for ; T > 0; T-- {
		x1 := fastScan()
		y1 := fastScan()
		x2 := fastScan()
		y2 := fastScan()
		dx := int64(x2 - x1)
		dy := int64(y2 - y1)
		ws.WriteString(strconv.FormatInt(int64(1) + dx*dy, 10) + "\n")
	}
	ws.Flush()
}

package main

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

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

type writerImpl struct {
	*bufio.Writer
}

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

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

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

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

type wordScannerImpl struct {
	*bufio.Scanner
}

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

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

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

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

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

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

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

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

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

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

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

func main() {
	defer writer.Flush()
	t := reader.Int64()
	for ; t > 0; t-- {
		x1 := reader.Int64()
		y1 := reader.Int64()
		x2 := reader.Int64()
		y2 := reader.Int64()
		writer.Printf("%d\n", sol(x1,y1,x2,y2))
	}
}

func sol(x1, y1, x2, y2 int64) int64 {
	return (x2-x1)*(y2-y1)+1
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		x1, y1, x2, y2 := int64(0), int64(0), int64(0), int64(0)
		fmt.Fscanf(in, "%d %d %d %d\n", &x1, &y1, &x2, &y2)
		x := x2 - x1
		y := y2 - y1
		top := min(x, y)
		ans := int64(0)
		if top >= 1 {
			if x == y {
				ans += top
				ans += (top) * (top - 1)
			} else {
				ans += (1 + top) * top
				ans += (x + y - 1 - top*2) * top
			}
		}
		fmt.Fprintf(out, "%d\n", ans+1)
	}
	return
}

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

/*
URL:
https://codeforces.com/contest/1358/problem/C
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	t              int
	x1, y1, x2, y2 int64
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		x1, y1, x2, y2 := ReadInt64_4()

		u := x2 - x1
		r := y2 - y1
		a := u + r

		mini := ArithmeticSequenceSum(1, 1, u)
		maxi := ArithmeticSequenceSum(a, -1, u)
		ans := maxi - mini + 1
		fmt.Println(ans)
	}
}

// ArithmeticSequenceSum returns a sum of an arithmetic sequence.
// a: 初項, d: 公差, n: 項数
func ArithmeticSequenceSum(a, d, n int64) int64 {
	return (2*a + (n-1)*d) * n / 2
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

/*
URL:
https://codeforces.com/contest/1358/problem/C
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	t              int
	x1, y1, x2, y2 int64
)

func main() {
	t = ReadInt()
	for i := 0; i < t; i++ {
		x1, y1, x2, y2 := ReadInt64_4()

		u := x2 - x1
		r := y2 - y1
		a := u + r

		mini := ArithmeticSequenceSum(1, 1, u)
		maxi := ArithmeticSequenceSum(a, -1, u)
		fmt.Println(maxi - mini + 1)
	}
}

// ArithmeticSequenceSum returns a sum of an arithmetic sequence.
// a: 初項, d: 公差, n: 項数
func ArithmeticSequenceSum(a, d, n int64) int64 {
	return (2*a + (n-1)*d) * n / 2
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

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

	var t, x1, y1, x, y int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &x1, &y1, &x, &y)
		x -= x1
		y -= y1
		if x > y {
			x, y = y, x
		}
		Fprintln(out, int64(x)*int64(y)+1)
	}
}

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

package main

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

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

	var t, x1, y1, x, y int64
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &x1, &y1, &x, &y)
		x -= x1
		y -= y1
		if x > y {
			x, y = y, x
		}
		Fprintln(out, x*y+1)
	}
}

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

package main

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

func main() {
	defer out.Flush()
	for t := ScanInt(); t > 0; t-- {
		P(solve())
	}
}

func solve() int64 {
	x1, x2, y1, y2 := ScanInt64(), ScanInt64(), ScanInt64(), ScanInt64()
	return (y1-x1)*(y2-x2) + 1
}

/*********************** I/O ***********************/
var in *bufio.Scanner
var out *bufio.Writer

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}

func P(args ...interface{}) {
	print("\n", args...)
}

func PS(args ...interface{}) {
	print(" ", args...)
	print("\n", nil)
}

func print(seperator string, args ...interface{}) {
	for _, arg := range args {
		str := ""
		switch v := arg.(type) {
		case byte:
			str = string(v)
		case rune:
			str = string(v)
		case int:
			str = strconv.Itoa(v)
		case []int:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.Itoa(v[i]) + " ")
			}
			str = sb.String()
		case int64:
			str = strconv.FormatInt(v, 10)
		case []int64:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.FormatInt(v[i], 10) + " ")
			}
			str = sb.String()
		case string:
			str = v
		default:
			panic("invalid print arg: " + fmt.Sprintf("%v", arg))

		}
		out.WriteString(str + seperator)
	}
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanWords(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanInt() int { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt()
	}
	return a
}
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt64()
	}
	return a
}

/*********************** Utils int64 ***********************/
func MnMx64(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn64(args ...int64) int64 { mn, _ := MnMx64(args...); return mn }
func Mx64(args ...int64) int64 { _, mx := MnMx64(args...); return mx }
func LCM64(a, b int64) int64   { return a / GCD64(a, b) * b }
func GCD64(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD64(b%a, a)
}
func SortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func Abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

/*********************** Utils int ***********************/
func MnMx(args ...int) (int, int) {
	minVal, maxVal := math.MaxInt32, math.MinInt32
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int) int { mn, _ := MnMx(args...); return mn }
func Mx(args ...int) int { _, mx := MnMx(args...); return mx }
func LCM(a, b int) int   { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

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

	var t, x1, y1, x, y int64
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &x1, &y1, &x, &y)
		x -= x1
		y -= y1
		if x > y {
			x, y = y, x
		}
		if x == 0 {
			Fprintln(out, 1)
		} else {
			Fprintln(out, x*(y-x+2))
		}
	}
}

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

package main

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

func main() {
	defer out.Flush()
	for t := ScanInt(); t > 0; t-- {
		P(solve())
	}
}

func solve() int64 {
	x1, x2, y1, y2 := ScanInt64(), ScanInt64(), ScanInt64(), ScanInt64()
	return (x2-x1)*(y2-y1) + 1
}

/*********************** I/O ***********************/
var in *bufio.Scanner
var out *bufio.Writer

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}

func P(args ...interface{}) {
	print("\n", args...)
}

func PS(args ...interface{}) {
	print(" ", args...)
	print("\n", nil)
}

func print(seperator string, args ...interface{}) {
	for _, arg := range args {
		str := ""
		switch v := arg.(type) {
		case byte:
			str = string(v)
		case rune:
			str = string(v)
		case int:
			str = strconv.Itoa(v)
		case []int:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.Itoa(v[i]) + " ")
			}
			str = sb.String()
		case int64:
			str = strconv.FormatInt(v, 10)
		case []int64:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.FormatInt(v[i], 10) + " ")
			}
			str = sb.String()
		case string:
			str = v
		default:
			panic("invalid print arg: " + fmt.Sprintf("%v", arg))

		}
		out.WriteString(str + seperator)
	}
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanWords(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanInt() int { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt()
	}
	return a
}
func ScanInt64() int64 { i, _ := strconv.ParseInt(ScanWord(), 10, 64); return i }
func ScanInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt64()
	}
	return a
}

/*********************** Utils int64 ***********************/
func MnMx64(args ...int64) (int64, int64) {
	minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64)
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn64(args ...int64) int64 { mn, _ := MnMx64(args...); return mn }
func Mx64(args ...int64) int64 { _, mx := MnMx64(args...); return mx }
func LCM64(a, b int64) int64   { return a / GCD64(a, b) * b }
func GCD64(a, b int64) int64 {
	if a == 0 {
		return b
	}
	return GCD64(b%a, a)
}
func SortInt64Slice(a []int64) { sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) }
func Abs64(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

/*********************** Utils int ***********************/
func MnMx(args ...int) (int, int) {
	minVal, maxVal := math.MaxInt32, math.MinInt32
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int) int { mn, _ := MnMx(args...); return mn }
func Mx(args ...int) int { _, mx := MnMx(args...); return mx }
func LCM(a, b int) int   { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

package main

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

func main() {
	defer out.Flush()
	for t := ScanInt(); t > 0; t-- {
		P(solve())
	}
}

func solve() int64 {
	x1, x2, y1, y2 := ScanInt(), ScanInt(), ScanInt(), ScanInt()
	return int64(x2-x1)*int64(y2-y1) + 1
}

/*********************** I/O ***********************/
var in *bufio.Scanner
var out *bufio.Writer

func init() {
	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)
	out = bufio.NewWriter(os.Stdout)
}

func P(args ...interface{}) {
	print("\n", args...)
}

func PS(args ...interface{}) {
	print(" ", args...)
	print("\n", nil)
}

func print(seperator string, args ...interface{}) {
	for _, arg := range args {
		str := ""
		switch v := arg.(type) {
		case byte:
			str = string(v)
		case rune:
			str = string(v)
		case int:
			str = strconv.Itoa(v)
		case []int:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.Itoa(v[i]) + " ")
			}
			str = sb.String()
		case int64:
			str = strconv.FormatInt(v, 10)
		case []int64:
			sb := strings.Builder{}
			for i := 0; i < len(v); i++ {
				sb.WriteString(strconv.FormatInt(v[i], 10) + " ")
			}
			str = sb.String()
		case string:
			str = v
		default:
			panic("invalid print arg: " + fmt.Sprintf("%v", arg))

		}
		out.WriteString(str + seperator)
	}
}

func ScanWord() string { in.Scan(); return in.Text() }
func ScanWords(n int) []string {
	s := make([]string, n)
	for i := 0; i < n; i++ {
		s[i] = ScanWord()
	}
	return s
}
func ScanInt() int { i, _ := strconv.Atoi(ScanWord()); return i }
func ScanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ScanInt()
	}
	return a
}

func MnMx(args ...int) (int, int) {
	minVal, maxVal := math.MaxInt32, math.MinInt32
	for _, v := range args {
		if v < minVal {
			minVal = v
		}
		if v > maxVal {
			maxVal = v
		}
	}
	return minVal, maxVal
}
func Mn(args ...int) int { mn, _ := MnMx(args...); return mn }
func Mx(args ...int) int { _, mx := MnMx(args...); return mx }
func LCM(a, b int) int   { return a / GCD(a, b) * b }
func GCD(a, b int) int {
	if a == 0 {
		return b
	}
	return GCD(b%a, a)
}
func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}


package main

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

func main() {
	var wg sync.WaitGroup
	var n, m, x, cnt, flag int
	scr := bufio.NewReader(os.Stdin)
	ocr := bufio.NewWriter(os.Stdout)
	fmt.Fscanln(scr, &n, &m)

	A := make([][]int, n)
	for i := 0; i < n; i++ {
		A[i] = make([]int, m)
	}
	B := make([][]int, n)
	for i := 0; i < n; i++ {
		B[i] = make([]int, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &A[i][j])
		}
		fmt.Fscanln(scr, &x)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &B[i][j])
		}
		fmt.Fscanln(scr, &x)
	}
	wg.Add(1)
	go right(A, B, &flag, n, m, &wg)

	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if A[j][i] != B[j][i] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			flag = 2
			break
		}
	}
	wg.Wait()
	if flag == 2 {
		fmt.Fprintln(ocr, "NO")
		ocr.Flush()
	} else {
		fmt.Fprintln(ocr, "YES")
		ocr.Flush()
	}
}

func right(A, B [][]int, flag *int, n, m int, wg *sync.WaitGroup) {
	defer wg.Done()
	var cnt int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if A[i][j] != B[i][j] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			*flag = 2
			break
		}
	}
}

package main

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

func main() {
	var n, m, x, cnt, flag int
	scr := bufio.NewReader(os.Stdin)
	ocr := bufio.NewWriter(os.Stdout)
	fmt.Fscanln(scr, &n, &m)

	A := make([][]int, n)
	for i := 0; i < n; i++ {
		A[i] = make([]int, m)
	}
	B := make([][]int, n)
	for i := 0; i < n; i++ {
		B[i] = make([]int, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &A[i][j])
		}
		fmt.Fscanln(scr, &x)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &B[i][j])
		}
		fmt.Fscanln(scr, &x)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if A[i][j] != B[i][j] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			flag = 2
			break
		}
	}

	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if A[j][i] != B[j][i] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			flag = 2
			break
		}
	}
	if flag == 2 {
		fmt.Fprintln(ocr, "NO")
		ocr.Flush()
	} else {
		fmt.Fprintln(ocr, "YES")
		ocr.Flush()
	}
}

package main

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

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

	var n, m int
	fmt.Fscan(in, &n, &m)

	rowsA := make([]int, n)
	colsA := make([]int, m)
	for i := range rowsA {
		for j := range colsA {
			var val int
			fmt.Fscan(in, &val)

			rowsA[i] ^= val
			colsA[j] ^= val
		}
		in.ReadLine()
	}

	rowsB := make([]int, n)
	colsB := make([]int, m)
	for i := range rowsB {
		for j := range colsB {
			var val int
			fmt.Fscan(in, &val)

			rowsB[i] ^= val
			colsB[j] ^= val
		}
		in.ReadLine()
	}

	for i := 0; i < n; i++ {
		if rowsA[i] != rowsB[i] {
			//fmt.Println("rows:", i, rowsA[i], rowsB[i])
			fmt.Println("No")
			return
		}
	}
	for i := 0; i < m; i++ {
		if colsA[i] != colsB[i] {
			//fmt.Println("cols:", i, colsA[i], colsB[i])
			fmt.Println("No")
			return
		}
	}

	fmt.Println("Yes")
}

package main

import (
	"fmt"
)

func main() {
	var n, m, x, cnt, flag int
	fmt.Scanln(&n, &m)

	A := make([][]int, n)
	for i := 0; i < n; i++ {
		A[i] = make([]int, m)
	}
	B := make([][]int, n)
	for i := 0; i < n; i++ {
		B[i] = make([]int, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &A[i][j])
		}
		fmt.Scanf("&d", &x)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Scanf("%d", &B[i][j])
		}
		fmt.Scanf("&d", &x)
	}

	go right(A, B, &flag, n, m)

	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if A[j][i] != B[j][i] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			flag = 2
			break
		}
	}
	if flag == 2 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

func right(A, B [][]int, flag *int, n, m int) {
	var cnt int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if A[i][j] != B[i][j] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			*flag = 2
			break
		}
	}
}

package main

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

func main() {
	var wg sync.WaitGroup
	var n, m, x, cnt, flag int
	scr := bufio.NewReader(os.Stdin)
	ocr := bufio.NewWriter(os.Stdout)
	fmt.Fscanln(scr, &n, &m)

	A := make([][]int, n)
	for i := 0; i < n; i++ {
		A[i] = make([]int, m)
	}
	B := make([][]int, n)
	for i := 0; i < n; i++ {
		B[i] = make([]int, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &A[i][j])
		}
		fmt.Fscanln(scr, &x)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &B[i][j])
		}
		fmt.Fscanln(scr, &x)
	}
	fmt.Println(A, B)
	wg.Add(1)
	go right(A, B, &flag, n, m, &wg)

	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if A[j][i] != B[j][i] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			flag = 2
			break
		}
	}
	wg.Wait()
	if flag == 2 {
		fmt.Fprintln(ocr, "NO")
		ocr.Flush()
	} else {
		fmt.Fprintln(ocr, "YES")
		ocr.Flush()
	}
}

func right(A, B [][]int, flag *int, n, m int, wg *sync.WaitGroup) {
	defer wg.Done()
	var cnt int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if A[i][j] != B[i][j] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			*flag = 2
			break
		}
	}
}

package main

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

func main() {
	var wg sync.WaitGroup
	var n, m, x, cnt, flag int
	scr := bufio.NewReader(os.Stdin)
	ocr := bufio.NewWriter(os.Stdout)
	fmt.Fscanln(scr, &n, &m)

	A := make([][]int, n)
	for i := 0; i < n; i++ {
		A[i] = make([]int, m)
	}
	B := make([][]int, n)
	for i := 0; i < n; i++ {
		B[i] = make([]int, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &A[i][j])
		}
		fmt.Fscanf(scr, "&d", &x)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			fmt.Fscanf(scr, "%d", &B[i][j])
		}
		fmt.Fscanf(scr, "&d", &x)
	}

	wg.Add(1)
	go right(A, B, &flag, n, m, &wg)

	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if A[j][i] != B[j][i] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			flag = 2
			break
		}
	}
	wg.Wait()
	if flag == 2 {
		fmt.Fprintln(ocr, "NO")
	} else {
		fmt.Fprintln(ocr, "YES")
	}
	ocr.Flush()
}

func right(A, B [][]int, flag *int, n, m int, wg *sync.WaitGroup) {
	defer wg.Done()
	var cnt int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if A[i][j] != B[i][j] {
				cnt++
			}
		}
		if cnt%2 != 0 {
			*flag = 2
			break
		}
	}
}


package main

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

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 Println(a ...interface{}) {
	fmt.Println(a...)
}

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

func main() {
	var n int
	scan(&n)
	c:=make([]int,n)
	t:=make([]int,n)
	for i:=0;i<n;i++{
		scan(&c[i])
	}
	for i:=0;i<n;i++{
		scan(&t[i])
	}
	if c[0]!=t[0] {
		Println("No")
		return
	}
	for i:=0;i<n-1;i++{
		c[i]=c[i+1]-c[i]
		t[i]=t[i+1]-t[i]
	}
	c=c[:n-1]
	t=t[:n-1]
	sort.Ints(c)
	sort.Ints(t)
	for i:=0;i<n-1;i++{
		if c[i]!=t[i]{
			Println("No")
			return
		}
	}
	Println("Yes")
}

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"math/big"
	"os"
	"sort"
)

var reader *bufio.Reader
var writer *bufio.Writer

var inputFile = flag.String("input", "", "")

func init() {
	flag.Parse()
	var i io.Reader
	if *inputFile != "" {
		i, _ = os.Open(*inputFile)
	} else {
		i = os.Stdin
	}
	reader = bufio.NewReaderSize(i, 1<<20)
	writer = bufio.NewWriterSize(os.Stdout, 1<<20)
}

func println(a ...interface{}) {
	_, _ = fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	_, _ = fmt.Fprintf(writer, format, a...)
}

func scanf(format string, a ...interface{}) {
	_, _ = fmt.Fscanf(reader, format, a...)
}

func scan(a interface{}) {
	_, _ = fmt.Fscan(reader, a)
}

func NextInt64() int64 {
	var res int64
	scan(&res)
	return res
}

func NextInt() int {
	var res int
	scan(&res)
	return res
}

func NextIntArray(n int) []int {
	res := make([]int, n)
	for i := range res {
		scan(&res[i])
	}
	return res
}

func NextString() string {
	var res string
	scan(&res)
	return res
}

func NextBigInt() *big.Int {
	n := new(big.Int)
	n, _ = n.SetString(NextString(), 10)
	return n
}

func main() {
	defer writer.Flush()

	n := NextInt()

	s := NextIntArray(n)
	t := NextIntArray(n)

	if s[0] != t[0] || s[n - 1] != t[n - 1] {
		println("No")
		return
	}

	var u, v []int

	for i := 0; i + 1 < n; i++ {
		u = append(u, s[i + 1] - s[i])
		v = append(v, t[i + 1] - t[i])
	}

	sort.Ints(u)
	sort.Ints(v)

	for i, x := range u {
		if v[i] != x {
			println("No")
			return
		}
	}

	println("Yes")

}

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func main() {
	var n int
	fmt.Fscan(in, &n)
	c := make([]int, n)
	for i := range c {
		fmt.Fscan(in, &c[i])
	}
	t := make([]int, n)
	for i := range t {
		fmt.Fscan(in, &t[i])
	}

	if c[0] != t[0] {
		fmt.Println("No")
		return
	}

	cd := make(map[int]int)
	for i := range c[1:] {
		cd[c[i+1]-c[i]]++
	}

	td := make(map[int]int)
	for i := range t[1:] {
		td[t[i+1]-t[i]]++
	}

	if reflect.DeepEqual(cd, td) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

package main

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

func main() {
	buf := bufio.NewReader(os.Stdin)
	var n ,first,last int
	fmt.Fscanf(buf, "%d\n", &n)
	var pre = -1
	cmap := make(map[int]int)
	for i := 0; i < n; i++ {
		var v int
		fmt.Fscanf(buf, "%d", &v)
		if i == 0 {
			pre = v
			first = v
			continue
		}
		if i == n-1{
			last = v
		}
		cmap[v-pre] = cmap[v-pre] + 1
		pre = v
	}
	fmt.Fscanf(buf, "\n")
	ans := true
	for i := 0; i < n; i++ {
		var v int
		fmt.Fscanf(buf, "%d", &v)
		if ans == false{
			continue
		}
		if i == 0 {
			pre = v
			if v != first{
				ans = false
			}
			continue
		}
		if i == n-1&&last != v{
			ans = false
		}
		cmap[v-pre] = cmap[v-pre] - 1
		if cmap[v-pre] < 0{
			ans = false
		}else if cmap[v-pre]==0{
			delete(cmap,v-pre)
		}
		pre = v
	}
	if len(cmap) > 0{
		ans = false
	}
	if ans {
		fmt.Printf("Yes\n")
	} else {
		fmt.Printf("No\n")
	}

}
/**
3
1 3 4
2 4 5
 */

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1110E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, p, v int
	Fscan(in, &n, &p)
	p0 := p
	c := make(map[int]int, n-1)
	for i := 1; i < n; i++ {
		Fscan(in, &v)
		c[v-p]++
		p = v
	}
	Fscan(in, &p)
	if p != p0 {
		Fprint(out, "No")
		return
	}
	for n--; n > 0; n-- {
		Fscan(in, &v)
		c[v-p]--
		p = v
	}
	for _, v := range c {
		if v != 0 {
			Fprint(out, "No")
			return
		}
	}
	Fprint(out, "Yes")
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, p, v int
	Fscan(in, &n, &p)
	p0 := p
	c := make(map[int]int, n-1)
	for i := 1; i < n; i++ {
		Fscan(in, &v)
		c[v-p]++
		p = v
	}
	Fscan(in, &p)
	if p != p0 {
		Print("No")
		return
	}
	for n--; n > 0; n-- {
		Fscan(in, &v)
		c[v-p]--
		p = v
	}
	for _, v := range c {
		if v != 0 {
			Print("No")
			return
		}
	}
	Print("Yes")
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, p, v int
	Fscan(in, &n, &p)
	p0 := p
	c := map[int]int{}
	for i := 1; i < n; i++ {
		Fscan(in, &v)
		c[v-p]++
		p = v
	}
	Fscan(in, &p)
	if p != p0 {
		Print("No")
		return
	}
	for n--; n > 0; n-- {
		Fscan(in, &v)
		c[v-p]--
		p = v
	}
	for _, v := range c {
		if v != 0 {
			Print("No")
			return
		}
	}
	Print("Yes")
}

package main

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

var in = bufio.NewReaderSize(os.Stdin, 2<<16)

func main() {
	var n int
	fmt.Fscan(in, &n)
	c := make([]int64, n)
	for i := range c {
		fmt.Fscan(in, &c[i])
	}
	t := make([]int64, n)
	for i := range t {
		fmt.Fscan(in, &t[i])
	}

	for i := 0; i < n; i++ {
		if c[i] == t[i] {
			continue
		}
		if i == 0 || i == n-1 {
			fmt.Println("No")
			return
		}
		if c[i-1]+c[i+1]-c[i] == t[i] {
			c[i] = t[i]
			i -= 2
		}
	}
	for i := range c {
		if c[i] != t[i] {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1110E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, p, v int
	Fscan(in, &n, &p)
	c := make(map[int]int, n-1)
	for i := 1; i < n; i++ {
		Fscan(in, &v)
		c[v-p]++
		p = v
	}
	Fscan(in, &p)
	for n--; n > 0; n-- {
		Fscan(in, &v)
		c[v-p]--
		p = v
	}
	for _, v := range c {
		if v != 0 {
			Fprint(out, "No")
			return
		}
	}
	Fprint(out, "Yes")
}

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


package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func sum(t []int, r int) int {
	result := 0
	for ; r >= 0; r = (r & (r + 1)) - 1 {
		result += t[r]
	}
	return result
}

func inc(t []int, i, delta int) {
	for ; i < len(t); i = (i | (i + 1)) {
		t[i] += delta
	}
}

func set(t []int, i, value int) {
	inc(t, i, value-sum2(t, i, i))
}

func sum2(t []int, l, r int) int {
	return sum(t, r) - sum(t, l-1)
}

func nth(t []int, n int) int {
	return sort.Search(len(t), func(j int) bool {
		return sum(t, j) >= n
	})
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, op := make([]int, m), make([]int, n)
	for i := 0; i < m; i++ {
		a[i] = ni32()
	}
	b := []int{}
	for i := 0; i < n; i++ {
		op[i] = ni32()
		if op[i] != -1 {
			b = append(b, op[i])
		}
	}

	t := make([]int, len(b))
	cur, length := 0, 0

	for i := 0; i < n; i++ {
		if op[i] != -1 {
			set(t, cur, 1)
			cur++
			length++
		} else {
			length2 := length
			tmp := []int{}
			for j := 0; j < m && a[j] <= length; j++ {
				pos := nth(t, a[j])
				if pos < len(t) {
					tmp = append(tmp, pos)
					length2--
				} else {
					break
				}
			}
			for j := 0; j < len(tmp); j++ {
				set(t, tmp[j], 0)
			}
			length = length2
		}
	}

	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		for i := 0; i < len(b); i++ {
			if sum2(t, i, i) == 1 {
				pri32(b[i])
			}
		}
		endl()
	}
}

package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

var t []int

func sum(r int) int {
	result := 0
	for ; r >= 0; r = (r & (r + 1)) - 1 {
		result += t[r]
	}
	return result
}

func inc(i, delta int) {
	for ; i < len(t); i = (i | (i + 1)) {
		t[i] += delta
	}
}

func set(i, value int) {
	inc(i, value-sum2(i, i))
}

func sum2(l, r int) int {
	return sum(r) - sum(l-1)
}

func nth(n int) int {
	return sort.Search(len(t), func(j int) bool {
		return sum(j) >= n
	})
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, op := make([]int, m), make([]int, n)
	for i := 0; i < m; i++ {
		a[i] = ni32()
	}
	b := []int{}
	for i := 0; i < n; i++ {
		op[i] = ni32()
		if op[i] != -1 {
			b = append(b, op[i])
		}
	}

	t = make([]int, len(b))
	cur, length := 0, 0

	for i := 0; i < n; i++ {
		if op[i] != -1 {
			set(cur, 1)
			cur++
			length++
		} else {
			length2 := length
			tmp := []int{}
			for j := 0; j < m && a[j] <= length; j++ {
				pos := nth(a[j])
				if pos < len(t) {
					tmp = append(tmp, pos)
					length2--
				} else {
					break
				}
			}
			for j := 0; j < len(tmp); j++ {
				set(tmp[j], 0)
			}
			length = length2
		}
	}

	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		for i := 0; i < len(b); i++ {
			if sum2(i, i) == 1 {
				pri32(b[i])
			}
		}
		endl()
	}
}

package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

var t []int

func sum(r int) int {
	result := 0
	for ; r >= 0; r = (r & (r + 1)) - 1 {
		result += t[r]
	}
	return result
}

func inc(i, delta int) {
	for ; i < len(t); i = (i | (i + 1)) {
		t[i] += delta
	}
}

func set(i, value int) {
	inc(i, value-sum2(i, i))
}

func sum2(l, r int) int {
	return sum(r) - sum(l-1)
}

func nth(n int) int {
	return sort.Search(len(t), func(j int) bool {
		return sum(j) >= n
	})
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, op := make([]int, m), make([]int, n)
	for i := 0; i < m; i++ {
		a[i] = ni32()
	}
	b := []int{}
	for i := 0; i < n; i++ {
		op[i] = ni32()
		if op[i] != -1 {
			b = append(b, op[i])
		}
	}

	t = make([]int, len(b))
	tmp, ct := make([]int, len(b)), 0
	cur, length := 0, 0

	for i := 0; i < n; i++ {
		if op[i] != -1 {
			set(cur, 1)
			cur++
			length++
		} else {
			length2 := length
			ct = 0
			for j := 0; j < m && a[j] <= length; j++ {
				pos := nth(a[j])
				if pos < len(t) {
					tmp[ct] = pos
					ct++
					length2--
				} else {
					break
				}
			}
			for j := 0; j < ct; j++ {
				set(tmp[j], 0)
			}
			length = length2
		}
	}

	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		for i := 0; i < len(b); i++ {
			if sum2(i, i) == 1 {
				pri32(b[i])
			}
		}
		endl()
	}
}

package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, t := make([]int, m), make([]int, n)
	st := make([]int, m)
	for i := 0; i < m; i++ {
		a[i] = ni32()
		if i == 0 {
			st[i] = a[i]
		} else {
			st[i] = max(st[i-1], a[i])
		}
	}
	b := []int{}
	for i := 0; i < n; i++ {
		t[i] = ni32()
		if t[i] != -1 {
			b = append(b, t[i])
		}
	}
	start, length := 0, 0
	for i := 0; i < n; i++ {
		if t[i] != -1 {
			length++
		} else {
			dl := sort.SearchInts(a, length+1)
			start += dl
			length -= dl
		}
	}
	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		pri32(b[start : start+length]...)
		endl()
	}
}

package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, t := make([]int, m), make([]int, n)
	st := make([]int, m)
	for i := 0; i < m; i++ {
		a[i] = ni32()
		if i == 0 {
			st[i] = a[i]
		} else {
			st[i] = max(st[i-1], a[i])
		}
	}
	b := []int{}
	for i := 0; i < n; i++ {
		t[i] = ni32()
		if t[i] != -1 {
			b = append(b, t[i])
		}
	}
	start, length := 0, 0
	for i := 0; i < n; i++ {
		if t[i] != -1 {
			length++
		} else {
			dl := sort.SearchInts(st, length+1)
			start += dl
			length -= dl
		}
	}
	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		pri32(b[start : start+length]...)
		endl()
	}
}

package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, t := make([]int, m), make([]int, n)
	st := make([]int, m)
	for i := 0; i < m; i++ {
		a[i] = ni32()
		if i == 0 {
			st[i] = a[i]
		} else {
			st[i] = max(st[i-1], a[i])
		}
	}
	b := []int{}
	for i := 0; i < n; i++ {
		t[i] = ni32()
		if t[i] != -1 {
			b = append(b, t[i])
		}
	}
	start, length := 0, 0
	for i := 0; i < n; i++ {
		if t[i] != -1 {
			length++
		} else {
			dl := min(sort.SearchInts(st, length+1), length)
			start += dl
			length -= dl
		}
	}
	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		pri32(b[start : start+length]...)
		endl()
	}
}

package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, t := make([]int, m), make([]int, n)
	st := make([]int, m)
	for i := 0; i < m; i++ {
		a[i] = ni32()
		if i == 0 {
			st[i] = a[i]
		} else {
			st[i] = max(st[i-1], a[i])
		}
	}
	b := []int{}
	for i := 0; i < n; i++ {
		t[i] = ni32()
		if t[i] != -1 {
			b = append(b, t[i])
		}
	}
	start, length := 0, 0
	for i := 0; i < n; i++ {
		if t[i] != -1 {
			length++
		} else {
			dl := max(sort.SearchInts(st, length+1), length)
			start += dl
			length -= dl
		}
	}
	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		pri32(b[start : start+length]...)
		endl()
	}
}

package main

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

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

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

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

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

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

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

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

func next() string { sc.Scan(); return sc.Text() }

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

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

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

func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

var t []int

func sum(r int) int {
	result := 0
	for ; r >= 0; r = (r & (r + 1)) - 1 {
		result += t[r]
	}
	return result
}

func inc(i, delta int) {
	for ; i < len(t); i = (i | (i + 1)) {
		t[i] += delta
	}
}

func set(i, value int) {
	inc(i, value-sum2(i, i))
}

func sum2(l, r int) int {
	return sum(r) - sum(l-1)
}

func nth(n int) int {
	return sort.Search(len(t), func(j int) bool {
		return sum(j) >= n
	})
}

func main() {
	defer w.Flush()
	n, m := ni32(), ni32()
	a, op := make([]int, m), make([]int, n)
	for i := 0; i < m; i++ {
		a[i] = ni32()
	}
	b := []int{}
	for i := 0; i < n; i++ {
		op[i] = ni32()
		if op[i] != -1 {
			b = append(b, op[i])
		}
	}

	t = make([]int, len(b))
	cur, length := 0, 0

	for i := 0; i < n; i++ {
		if op[i] != -1 {
			set(cur, 1)
			cur++
			length++
		} else {
			length2 := length
			for j := 0; j < m && a[j] <= length; j++ {
				fmt.Println(j, a[j])
				pos := nth(a[j])
				if pos < len(t) {
					set(pos, 0)
					length2--
				} else {
					break
				}
			}
			length = length2
		}
	}

	if length == 0 {
		w.WriteString("Poor stack!\n")
	} else {
		for i := 0; i < len(b); i++ {
			if sum2(i, i) == 1 {
				pri32(b[i])
			}
		}
		endl()
	}
}