You are given a program that consists of $$$n$$$ instructions. Initially a single variable $$$x$$$ is assigned to $$$0$$$. Afterwards, the instructions are of two types:   increase $$$x$$$ by $$$1$$$;  decrease $$$x$$$ by $$$1$$$. You are given $$$m$$$ queries of the following format:   query $$$l$$$ $$$r$$$ — how many distinct values is $$$x$$$ assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then the description of $$$t$$$ testcases follows. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $$$n$$$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $$$m$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the description of the query. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase print $$$m$$$ integers — for each query $$$l$$$, $$$r$$$ print the number of distinct values variable $$$x$$$ is assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order.

NoteThe instructions that remain for each query of the first testcase are:   empty program — $$$x$$$ was only equal to $$$0$$$;  "-" — $$$x$$$ had values $$$0$$$ and $$$-1$$$;  "---+" — $$$x$$$ had values $$$0$$$, $$$-1$$$, $$$-2$$$, $$$-3$$$, $$$-2$$$ — there are $$$4$$$ distinct values among them;  "+--+--+" — the distinct values are $$$1$$$, $$$0$$$, $$$-1$$$, $$$-2$$$.

package main

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

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

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

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	bb := scanner.Bytes()
	bb2 := []byte(string(bb))
	if !bytes.Equal(bb, bb2) {
		printf("%s\n", string(bb)+"|"+string(bb2))
	}
	return bb2
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return []byte(string(scanner.Bytes())) // <-- ?
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1024), 1e+9)
}

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return []byte(scanner.Text())
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

/*
URL:
https://codeforces.com/contest/1473/problem/D
*/

package main

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

var (
	println = fmt.Println

	tc   int
	n, m int
	R    []rune
)

func main() {
	defer stdout.Flush()

	tc = readi()
	for i := 0; i < tc; i++ {
		n, m = readi2()
		R = readrs()

		solve()
	}
}

func solve() {
	C := make([]int, n)
	for i := 0; i < n; i++ {
		if R[i] == '-' {
			C[i] = -1
		} else {
			C[i] = 1
		}
	}
	D := make([]int, n)
	D[0] = C[0]
	for i := 1; i < n; i++ {
		D[i] = D[i-1] + C[i]
	}
	// debugf("C: %v\n", C)
	// debugf("D: %v\n", D)

	// 命令の和を計算できるセグ木
	v1 := make([]S, n)
	for i := 0; i < n; i++ {
		v1[i] = S(C[i])
	}
	e1 := func() S { return 0 }
	m1 := func(a, b S) S { return a + b }
	sumSeg := NewSegtree(v1, e1, m1)

	// 答えに対するrange maxのセグ木
	v2 := make([]S, n)
	for i := 0; i < n; i++ {
		v2[i] = S(D[i])
	}
	e2 := func() S { return -INF_B30 }
	m2 := func(a, b S) S { return S(max(int(a), int(b))) }
	maxSeg := NewSegtree(v2, e2, m2)

	// 答えに対するrange minのセグ木
	v3 := make([]S, n)
	for i := 0; i < n; i++ {
		v3[i] = S(D[i])
	}
	e3 := func() S { return INF_B30 }
	m3 := func(a, b S) S { return S(min(int(a), int(b))) }
	minSeg := NewSegtree(v3, e3, m3)

	ans := make([]int, m)
	for i := 0; i < m; i++ {
		l, r := readi2()

		// 全部スキップしたときは0の一種類
		if l == 1 && r == n {
			ans[i] = 1
			continue
		}

		l--
		s := int(sumSeg.Prod(l, r))

		mini, maxi := 0, 0
		// 左側はそのまま
		if l > 0 {
			lMin := minSeg.Prod(0, l)
			lMax := maxSeg.Prod(0, l)
			chmin(&mini, int(lMin))
			chmax(&maxi, int(lMax))
		}
		// 右側はs加算する
		if r < n {
			rMin := minSeg.Prod(r, n)
			rMax := maxSeg.Prod(r, n)
			chmin(&mini, int(rMin)-s)
			chmax(&maxi, int(rMax)-s)
		}

		ans[i] = maxi - mini + 1
	}

	for i := 0; i < m; i++ {
		printf("%d\n", ans[i])
	}
}

// originated from: https://qiita.com/EmptyBox_0/items/2f8e3cf7bd44e0f789d5#segtree
// docs: https://atcoder.github.io/ac-library/production/document_ja/segtree.html

// type of monoid
type S int

type E func() S
type Merger func(a, b S) S
type Compare func(v S) bool

func NewSegtree(v []S, e E, m Merger) *Segtree {
	seg := new(Segtree)
	seg.n = len(v)
	seg.log = seg._ceilPow2(seg.n)
	seg.size = 1 << uint(seg.log)
	seg.d = make([]S, 2*seg.size)
	seg.e = e
	seg.merger = m
	for i := range seg.d {
		seg.d[i] = seg.e()
	}
	for i := 0; i < seg.n; i++ {
		seg.d[seg.size+i] = v[i]
	}
	for i := seg.size - 1; i >= 1; i-- {
		seg._update(i)
	}
	return seg
}

type Segtree struct {
	n      int
	size   int
	log    int
	d      []S
	e      E
	merger Merger
}

// Set sets a[p] = x
// Time complexity: O(logn)
func (seg *Segtree) Set(p int, x S) {
	p += seg.size
	seg.d[p] = x
	for i := 1; i <= seg.log; i++ {
		seg._update(p >> uint(i))
	}
}

// Get returns a[p]
// Time complexity: O(1)
func (seg *Segtree) Get(p int) S {
	return seg.d[p+seg.size]
}

// Prod returns op(a[l:r]...)
// Time complexity: O(logn)
func (seg *Segtree) Prod(l, r int) S {
	sml, smr := seg.e(), seg.e()
	l += seg.size
	r += seg.size
	for l < r {
		if (l & 1) == 1 {
			sml = seg.merger(sml, seg.d[l])
			l++
		}
		if (r & 1) == 1 {
			r--
			smr = seg.merger(seg.d[r], smr)
		}
		l >>= 1
		r >>= 1
	}
	return seg.merger(sml, smr)
}

// AllProd returns op(a...)
// Time complexity: O(1)
func (seg *Segtree) AllProd() S {
	return seg.d[1]
}

// Time complexity: O(logn)
func (seg *Segtree) MaxRight(l int, cmp Compare) int {
	if l == seg.n {
		return seg.n
	}
	l += seg.size
	sm := seg.e()
	for {
		for l%2 == 0 {
			l >>= 1
		}
		if !cmp(seg.merger(sm, seg.d[l])) {
			for l < seg.size {
				l = 2 * l
				if cmp(seg.merger(sm, seg.d[l])) {
					sm = seg.merger(sm, seg.d[l])
					l++
				}
			}
			return l - seg.size
		}
		sm = seg.merger(sm, seg.d[l])
		l++
		if l&-l == l {
			break
		}
	}
	return seg.n
}

// Time complexity: O(logn)
func (seg *Segtree) MinLeft(r int, cmp Compare) int {
	if r == 0 {
		return 0
	}
	r += seg.size
	sm := seg.e()
	for {
		r--
		for r > 1 && r%2 != 0 {
			r >>= 1
		}
		if !cmp(seg.merger(seg.d[r], sm)) {
			for r < seg.size {
				r = 2*r + 1
				if cmp(seg.merger(seg.d[r], sm)) {
					sm = seg.merger(seg.d[r], sm)
					r--
				}
			}
			return r + 1 - seg.size
		}
		sm = seg.merger(seg.d[r], sm)
		if r&-r == r {
			break
		}
	}
	return 0
}

func (seg *Segtree) _update(k int) {
	seg.d[k] = seg.merger(seg.d[2*k], seg.d[2*k+1])
}

func (seg *Segtree) _ceilPow2(n int) int {
	x := 0
	for (1 << uint(x)) < n {
		x++
	}
	return x
}

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

/********** common constants **********/

const (
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPH_N  = 26
	INF_I64 = math.MaxInt64
	INF_B60 = 1 << 60
	INF_I32 = math.MaxInt32
	INF_B30 = 1 << 30
	NIL     = -1
	EPS     = 1e-10
)

/********** bufio setting **********/

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

// mod can calculate a right residual whether value is positive or negative.
func mod(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

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

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

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

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

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

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

	return s
}

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

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

	if e == 0 {
		return 1
	}

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

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

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

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

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

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

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

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  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()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

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

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

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

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

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

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

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

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

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

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

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

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

package main

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

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

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

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

func scan() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func sint64() int64 {
	bb := scan()
	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 suint64() uint64 {
	bb := scan()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	return string(scan())
}

func sbytes() []byte {
	return scan()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func sint64() int64 {
	if !scanner.Scan() {
		os.Exit(1)
	}
	bb := scanner.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 suint64() uint64 {
	if !scanner.Scan() {
		os.Exit(1)
	}
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Text()
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

func rint64() int64 {
	var i int64
	fmt.Fscan(reader, &i)
	return i
}

func rbytes() []byte {
	var b []byte
	fmt.Fscan(reader, &b)
	return b
}

func main() {
	defer writer.Flush()
	for t := rint64(); t > 0; t-- {
		n := rint64()
		m := rint64()
		p := rbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{rint64(), rint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.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 suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

const INF = 1000000000

func Max(args ...int) int {
	ret := -INF
	for _, cur := range args {
		if ret < cur {
			ret = cur
		}
	}
	return ret
}

func Min(args ...int) int {
	ret := INF
	for _, cur := range args {
		if ret > cur {
			ret = cur
		}
	}
	return ret
}

func main() {
	defer writer.Flush()

	var T int
	fmt.Fscan(reader, &T)

	for test := 1; test <= T; test++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var s string
		fmt.Fscan(reader, &s)

		pref_sum := make([]int, n+1)
		pref_max := make([]int, n+1)
		pref_min := make([]int, n+1)

		suff_max := make([]int, n+2)
		suff_min := make([]int, n+2)

		for i := 1; i <= n+1; i++ {
			if i <= n {
				pref_max[i] = -INF
				pref_min[i] = INF
			}

			suff_max[i] = -INF
			suff_min[i] = INF
		}

		get_val := func(pos int) int {
			if s[pos] == '+' {
				return 1
			} else {
				return -1
			}
		}

		for i := 1; i <= n; i++ {
			pref_sum[i] = pref_sum[i-1] + get_val(i-1)
			pref_max[i] = Max(pref_max[i-1], pref_sum[i])
			pref_min[i] = Min(pref_min[i-1], pref_sum[i])
		}

		for i := n; i >= 1; i-- {
			suff_max[i] = Max(suff_max[i+1], pref_sum[i])
			suff_min[i] = Min(suff_min[i+1], pref_sum[i])
		}

		//fmt.Println(pref_sum)
		for q := 1; q <= m; q++ {
			var l, r int
			fmt.Fscan(reader, &l, &r)

			mi := Min(pref_min[l-1], suff_min[r+1]-pref_sum[r]+pref_sum[l-1])
			mx := Max(0, pref_max[l-1], suff_max[r+1]-pref_sum[r]+pref_sum[l-1])

			//fmt.Println(pref_min[l-1], suff_min[r+1], mi, mx)

			fmt.Fprintf(writer, "%d\n", mx-mi+1)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
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 abs(x int)int{
	if x<0{
		return -x
	}
	return x
}
func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	fmt.Fscan(in, &t)
	for u := 1; u <= t; u++ {
		var n, m int
		fmt.Fscan(in, &n, &m)
		var s []byte
		fmt.Fscan(in, &s)
		prel := make([]int, n+5)
		premi := make([]int, n+5)
		prema := make([]int, n+5)
		for i := 1; i <= n; i++ {
			prel[i] = 0
			premi[i] = 0
			prema[i] = 0
			v := 0
			if s[i-1] == '+' {
				v = 1
			} else {
				v = -1
			}
			if i == 0 {
				prel[i] = v
				premi[i] = min(premi[i], prel[i])
				prema[i] = max(prema[i], prel[i])
			} else {
				prel[i] = prel[i-1] + v
				premi[i] = min(premi[i-1], prel[i])
				prema[i] = max(prema[i-1], prel[i])
			}
		}

		suf := make([]int,n+5)
		for i:=n;i>=1;i--{
			if s[i-1]=='-'{
				suf[i]=suf[i+1]-1
			}else{
				if suf[i+1]==0{
					suf[i]=0
				}else{
					suf[i]=suf[i+1]+1
				}
			}
		}
		sufma := make([]int,n+5)
		for i:=n;i>=1;i--{
			if s[i-1]=='+'{
				sufma[i]=sufma[i+1]+1
			}else{
				if sufma[i+1]==0{
					sufma[i]=0
				}else{
					sufma[i]=sufma[i+1]-1
				}
			}
		}

		for i:=1;i<=m;i++{
			var x, y int
			fmt.Fscan(in,&x,&y)
			ans:=1
			now := prel[x-1]
			if prema[x-1]>=0{
				ans += prema[x-1]
				if now + sufma[y+1]>prema[x-1]{
					ans += now + sufma[y+1] - prema[x-1]
				}
			}
			if premi[x-1]<=0{
				ans += abs(premi[x-1])
				if now + suf[y+1]<premi[x-1]{
					ans += abs(now+suf[y+1])-abs(premi[x-1])
				}
			}
			fmt.Fprintln(out,ans)
		}
	}
}

/*
* Filename: D/main.go
* Timestamp: 25-05-2021 13:16:38
* Author: Fadhil Musaad
 */

package main

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

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

func scan(a ...interface{})    { fmt.Fscan(reader, a...) }
func scanln(a ...interface{})  { fmt.Fscanln(reader, a...) }
func print(a ...interface{})   { fmt.Fprint(writer, a...) }
func println(a ...interface{}) { fmt.Fprintln(writer, a...) }

func solve(_r io.Reader, _w io.Writer) {
	reader = bufio.NewReader(_r)
	writer = bufio.NewWriter(_w)
	defer writer.Flush()

	var t int
	scanln(&t)
	for ; t > 0; t-- {
		var n, m int
		scanln(&n, &m)
		var s string
		scanln(&s)

		prefmax := make([]int, n+2)
		prefmin := make([]int, n+2)
		suffmax := make([]int, n+2)
		suffmin := make([]int, n+2)
		prefmax[0] = 0
		prefmin[0] = 0
		suffmax[n+1] = 0
		suffmin[n+1] = 0
		cur := 0
		arr := make([]int, n+2)
		arr2 := make([]int, n+2)

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

		min := func(a, b int) int {
			if a < b {
				return a
			}
			return b
		}
		for i := 1; i <= n; i++ {
			if s[i-1] == '-' {
				cur--
			} else {
				cur++
			}
			arr[i] = cur
			prefmin[i] = min(prefmin[i-1], cur)
			prefmax[i] = max(prefmax[i-1], cur)
		}
		cur = 0
		for i := n; i > 0; i-- {
			arr2[i] = cur
			suffmin[i] = min(suffmin[i+1], cur)
			suffmax[i] = max(suffmax[i+1], cur)
			if s[i-1] == '+' {
				cur--
			} else {
				cur++
			}
		}

		for ; m > 0; m-- {
			var l, r int
			scanln(&l, &r)
			mxa := prefmax[l-1]
			mna := prefmin[l-1]
			mxb := suffmax[r]
			mnb := suffmin[r]
			mxb -= arr2[r]
			mnb -= arr2[r]
			mxb += arr[l-1]
			mnb += arr[l-1]
			println(max(mxa, mxb) - min(mna, mnb) + 1)
		}
	}
}

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

package main

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

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

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

func solve(x1, y1, x2, y2 int) int {
	//fmt.Printf("x1=%d, y1=%d, x2=%d, y2=%d\n",x1, y1, x2, y2)
	if x2 == 0 && y2 == 0 {
		return y1-x1+1
	}

	if y1 < x2 || y2 < x1 {
		return y1-x1+1 + y2-x2 +1
	}

	return Max_Int(y1, y2 ) - Min_Int(x1, x2) +1
}

func main(){
	in := bufio.NewReader(os.Stdin)
	var t int;
	fmt.Fscan(in, &t)
	in.ReadString('\n')
	for (t>0){
		t--
		var n, m int
		fmt.Fscan(in, &n, &m)
		in.ReadString('\n')
		s, _ := in.ReadString('\n')

		a := make([]int, n+1)
		minx := make([]int, n+1)
		maxx := make([]int, n+1)
		miny := make([]int, n+1)
		maxy := make([]int, n+1)
		a[0]=0; minx[0]=0; maxx[0]=0; miny[0]=0; maxy[0]=0;
		for i:=1; i<=n; i++{
			if s[i-1] == '-'{
				a[i] = a[i-1]-1;
			}else{
				a[i] = a[i-1]+1;
			}
		}

		for i:=1; i<=n; i++{
			minx[i] = Min_Int(minx[i-1], a[i])
			maxx[i] = Max_Int(maxx[i-1], a[i])
		}
		miny[n]= a[n]; maxy[n] = a[n];
		for i:=n-1; i>0; i--{
			miny[i] = Min_Int(miny[i+1], a[i])
			maxy[i] = Max_Int(maxy[i+1], a[i])
		}

		var l, r int
		for i:=0; i<m; i++{
			fmt.Fscan(in, &l, &r)
			in.ReadString('\n')
			lmin := minx[l-1]
			lmax := maxx[l-1]

			if (r < n) {
				d := a[r] - a[l-1]
				rmin := miny[r+1]-d
				rmax := maxy[r+1]-d
				fmt.Println(solve(lmin, lmax, rmin, rmax))
			}else{
				fmt.Println(solve(lmin, lmax, 0, 0))
			}
		}
	}

}

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()

	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		solve()
	}
}

func solve() {
	var n, m, l, r int
	var s string
	fmt.Fscan(in, &n, &m, &s)

	sum := make([]int, n+2)
	minPre := make([]int, n+2)
	maxPre := make([]int, n+2)
	for i := 1; i <= n; i++ {
		if s[i-1] == '+' {
			sum[i] = sum[i-1] + 1
		} else {
			sum[i] = sum[i-1] - 1
		}
		maxPre[i] = max(maxPre[i-1], sum[i])
		minPre[i] = min(minPre[i-1], sum[i])
	}
	minSuf := make([]int, n+2)
	maxSuf := make([]int, n+2)
	for i := n; i >= 1; i-- {
		if s[i-1] == '+' {
			minSuf[i] = min(1, minSuf[i+1]+1)
			maxSuf[i] = max(1, maxSuf[i+1]+1)
		} else {
			minSuf[i] = min(-1, minSuf[i+1]-1)
			maxSuf[i] = max(-1, maxSuf[i+1]-1)
		}
	}

	// fmt.Fprintln(out, sum)
	// fmt.Fprintln(out, minPre)
	// fmt.Fprintln(out, maxPre)
	// fmt.Fprintln(out, minSuf)
	// fmt.Fprintln(out, maxSuf)

	for i := 0; i < m; i++ {
		fmt.Fscan(in, &l, &r)
		l--
		r++

		mn := minPre[l]
		mx := maxPre[l]
		if r <= n {
			mn = min(mn, sum[l]+minSuf[r])
			mx = max(mx, sum[l]+maxSuf[r])
		}
		fmt.Fprintln(out, mx-mn+1)
	}
}

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
}

package main

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

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

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

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	bb := scanner.Bytes()
	bb2 := []byte(string(bb))
	if !bytes.Equal(bb, bb2) {
		printf("%s\n", string(bb)+"|"+string(bb2))
	}
	return bb
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes() // <-- ?
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1024), 1e+9)
}

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

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

func suint64() uint64 {
	bytes := sbytes()
	i := uint64(0)
	for _, b := range bytes {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sbytes() []byte {
	if !scanner.Scan() || scanner.Err() != nil {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func scan() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func sint64() int64 {
	bb := scan()
	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 suint64() uint64 {
	bb := scan()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	return string(scan())
}

func sbytes() []byte {
	return scan()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func scan() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func sint64() int64 {
	bb := scan()
	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 suint64() uint64 {
	bb := scan()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	return string(scan())
}

func sbytes() []byte {
	return scan()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func scan() {
	if !scanner.Scan() {
		os.Exit(1)
	}
}

func sint64() int64 {
	scan()
	bb := scanner.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 suint64() uint64 {
	scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scan()
	return scanner.Text()
}

func sbytes() []byte {
	scan()
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func sint64() int64 {
	if !scanner.Scan() {
		os.Exit(1)
	}
	bb := scanner.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 suint64() uint64 {
	if !scanner.Scan() {
		os.Exit(1)
	}
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Text()
}

func sbytes() []byte {
	if !scanner.Scan() {
		os.Exit(1)
	}
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.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 suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

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

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.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 suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

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

func sbytes() []byte {
	scanner.Scan()
	return scanner.Bytes()
}

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		m := sint64()
		p := sbytes()
		q := make([][2]int64, m)
		for i := range q {
			q[i] = [2]int64{sint64(), sint64()}
		}
		for _, i := range sol(n, m, p, q) {
			printf("%d\n", i)
		}
	}
}

func sol(n, m int64, p []byte, q [][2]int64) []int64 {
	currFromLeft := make([]int64, n+1)
	for i := range p {
		if p[i] == '+' {
			currFromLeft[i+1] = currFromLeft[i] + 1
		} else {
			currFromLeft[i+1] = currFromLeft[i] - 1
		}
	}
	lmax := make([]int64, n+1)
	lmin := make([]int64, n+1)
	for i := range p {
		lmax[i+1] = max(lmax[i], currFromLeft[i])
		lmin[i+1] = min(lmin[i], currFromLeft[i])
	}
	rmax := make([]int64, n+1)
	rmin := make([]int64, n+1)
	rmax[len(rmax)-1] = currFromLeft[len(currFromLeft)-1]
	rmin[len(rmin)-1] = currFromLeft[len(currFromLeft)-1]
	for i := len(p) - 1; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], currFromLeft[i])
		rmin[i] = min(rmin[i+1], currFromLeft[i])
	}
	ret := make([]int64, m)
	for i := range ret {
		ret[i] = 1
		l := q[i][0]
		r := q[i][1]
		delta := currFromLeft[r] - currFromLeft[l-1]
		high := max(lmax[l-1], rmax[r]-delta)
		low := min(lmin[l-1], rmin[r]-delta)
		ret[i] = high - low + 1
	}
	return ret
}

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

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

package main

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

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

func Max(args ...int) int {
	ret := math.MinInt32
	for _, cur := range args {
		if ret < cur {
			ret = cur
		}
	}
	return ret
}

func Min(args ...int) int {
	ret := math.MaxInt32
	for _, cur := range args {
		if ret > cur {
			ret = cur
		}
	}
	return ret
}

func main() {
	defer writer.Flush()

	var T int
	fmt.Fscan(reader, &T)

	for test := 1; test <= T; test++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var s string
		fmt.Fscan(reader, &s)

		pref_sum := make([]int, n+1)
		pref_max := make([]int, n+1)
		pref_min := make([]int, n+1)

		suff_sum := make([]int, n+2)
		suff_max := make([]int, n+2)
		suff_min := make([]int, n+2)

		for i := 1; i <= n; i++ {
			pref_max[i] = math.MinInt32
			pref_min[i] = math.MaxInt32

			suff_max[i] = math.MinInt32
			suff_min[i] = math.MaxInt32
		}

		get_val := func(pos int) int {
			if s[pos] == '+' {
				return 1
			} else {
				return -1
			}
		}

		for i := 1; i <= n; i++ {
			pref_sum[i] = pref_sum[i-1] + get_val(i-1)
			suff_sum[n-i+1] = suff_sum[n-i+2] + get_val(n-i)

			pref_max[i] = Max(pref_max[i-1], pref_sum[i])
			pref_min[i] = Min(pref_min[i-1], pref_sum[i])

			suff_max[n-i+1] = Max(suff_max[n-i+2], suff_sum[n-i+1])
			suff_min[n-i+1] = Min(suff_min[n-i+2], suff_sum[n-i+1])
		}

		for q := 1; q <= m; q++ {
			var l, r int
			fmt.Fscan(reader, &l, &r)

			mi := Min(pref_min[l-1], suff_min[r+1], suff_min[r+1]+pref_sum[l-1])
			mx := Max(0, pref_max[l-1], suff_max[r+1], suff_max[r+1]+pref_sum[l-1])

			fmt.Fprintf(writer, "%d\n", mx-mi+1)
		}
	}
}

package main

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

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

func Max(args ...int) int {
	ret := math.MinInt32
	for _, cur := range args {
		if ret < cur {
			ret = cur
		}
	}
	return ret
}

func Min(args ...int) int {
	ret := math.MaxInt32
	for _, cur := range args {
		if ret > cur {
			ret = cur
		}
	}
	return ret
}

func main() {
	defer writer.Flush()

	var T int
	fmt.Fscan(reader, &T)

	for test := 1; test <= T; test++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		var s string
		fmt.Fscan(reader, &s)

		pref_sum := make([]int, n+1)
		pref_max := make([]int, n+1)
		pref_min := make([]int, n+1)

		suff_sum := make([]int, n+2)
		suff_max := make([]int, n+2)
		suff_min := make([]int, n+2)

		for i := 1; i <= n; i++ {
			pref_max[i] = math.MinInt32
			pref_min[i] = math.MaxInt32

			suff_max[i] = math.MinInt32
			suff_min[i] = math.MaxInt32
		}

		get_val := func(pos int) int {
			if s[pos] == '+' {
				return 1
			} else {
				return -1
			}
		}

		for i := 1; i <= n; i++ {
			pref_sum[i] = pref_sum[i-1] + get_val(i-1)
			suff_sum[n-i+1] = suff_sum[n-i+2] + get_val(n-i)

			pref_max[i] = Max(pref_max[i-1], pref_sum[i])
			pref_min[i] = Min(pref_min[i-1], pref_sum[i])

			suff_max[n-i+1] = Max(suff_max[n-i+2], suff_sum[n-i+1])
			suff_min[n-i+1] = Min(suff_min[n-i+2], suff_sum[n-i+1])
		}

		for q := 1; q <= m; q++ {
			var l, r int
			fmt.Fscan(reader, &l, &r)

			mi := Min(pref_min[l-1], suff_min[r+1], suff_min[r+1]+pref_sum[l-1])
			mx := Max(pref_max[l-1], suff_max[r+1], suff_max[r+1]+pref_sum[l-1])

			fmt.Fprintf(writer, "%d\n", mx-mi+1)
		}
	}
}


Sherlock Holmes and Dr. Watson played some game on a checkered board n × n in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers.For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8 + 3 + 6 + 7 = 24, sum of its row numbers equals 9 + 5 + 3 + 2 = 19, and 24 > 19.

The first line contains an integer n (1 ≤ n ≤ 30). Each of the following n lines contain n space-separated integers. The j-th number on the i-th line represents the number on the square that belongs to the j-th column and the i-th row on the board. All number on the board are integers from 1 to 100.

Print the single number — the number of the winning squares.

NoteIn the first example two upper squares are winning.In the third example three left squares in the both middle rows are winning:5 7 8 49 5 3 21 6 6 49 5 7 3

package main

import (
	"fmt"
	//"sort"
)

func main() {
	var n,m,ct int
	fmt.Scan(&n)
	r:=make([]int,30)
	c:=make([]int,30)

	for i:=0;i<n;i++ {
		for j:=0;j<n;j++{
			fmt.Scan(&m)
			r[i]+=m
			c[j]+=m
		}
	}

	for i:=0;i<n;i++ {
		for j:=0;j<n;j++{
			if r[i]<c[j] {
				ct++
			}
		}
	}
	fmt.Println(ct)

}

package main

import "fmt"

func main() {
    var n int
    var a [35][35]int
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            fmt.Scan(&a[i][j])
        }
    }
    ans := 0
    for i := 0; i < n; i++ {
        for j := 0; j < n; j++ {
            col, row := 0, 0
            for k := 0; k < n; k++ {
                col += a[k][j]
                row += a[i][k]
            }
            if col > row { ans++ }
        }
    }
    fmt.Println(ans)
}


An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products Ks·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.You should calculate the power of t given subarrays.

First line contains two integers n and t (1 ≤ n, t ≤ 200000) — the array length and the number of queries correspondingly. Second line contains n positive integers ai (1 ≤ ai ≤ 106) — the elements of the array. Next t lines contain two positive integers l, r (1 ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.

Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).

NoteConsider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):  Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n, q, l, r int
	Fscan(in, &n, &q)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	type query struct{ bid, l, r, qid int }
	qs := make([]query, q)
	blockSize := int(math.Round(math.Sqrt(float64(n))))
	for i := range qs {
		Fscan(in, &l, &r)
		qs[i] = query{l / blockSize, l, r + 1, i}
	}
	sort.Slice(qs, func(i, j int) bool {
		a, b := qs[i], qs[j]
		if a.bid != b.bid {
			return a.bid < b.bid
		}
		if a.bid&1 == 0 {
			return a.r < b.r
		}
		return a.r > b.r
	})

	sum, cnt, l, r := int64(0), [1e6 + 1]int{}, 1, 1
	update := func(i, d int) {
		v := a[i-1]
		sum += int64(2*d*cnt[v]+1) * int64(v)
		cnt[v] += d
	}
	ans := make([]interface{}, q)
	for _, q := range qs {
		for ; r < q.r; r++ {
			update(r, 1)
		}
		for ; l < q.l; l++ {
			update(l, -1)
		}
		for l > q.l {
			l--
			update(l, 1)
		}
		for r > q.r {
			r--
			update(r, -1)
		}
		ans[q.qid] = sum
	}
	Fprint(out, ans...)
}