Program
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 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$$$. 
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.
import os
from io import BytesIO, IOBase
import sys

def main():
    for i in range(int(input())):
        (n, m) = map(int, input().split())
        s = input()
        g1max = [0]
        g1min = [0]
        sumo1 = [0]
        mino = 0
        maxo = 0
        c = 0
        for j in range(n):
            if s[j] == '+':
                c = c + 1
            else:
                c = c - 1
            if maxo < c:
                maxo = c
            if mino > c:
                mino = c
            g1max.append(maxo)
            g1min.append(mino)
            sumo1.append(c)
        g2max = [0]
        g2min = [0]
        sumo2 = [0]
        mino = 0
        maxo = 0
        c = 0
        for j in range(n - 1, -1, -1):
            if s[j] == '+':
                c = c + 1
            else:
                c = c - 1
            if maxo < c:
                maxo = c
            if mino > c:
                mino = c
            g2max.append(maxo)
            g2min.append(mino)
            sumo2.append(c)
        for j in range(m):
            (l, r) = map(int, input().split())
            print(1 + max(g1max[l - 1], sumo1[l - 1] + sumo2[n - r] - g2min[n - r]) + abs(min(g1min[l - 1], sumo1[l - 1] - (g2max[n - r] - sumo2[n - r]))))
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
if __name__ == '__main__':
    main()
def pref(ins):
    x = 0
    Min = 0
    Max = 0
    Fin_ = [0]
    Min_ = [0]
    Max_ = [0]
    for I in ins:
        if I == '-':
            x -= 1
            Min = min(Min, x)
        else:
            x += 1
            Max = max(Max, x)
        Fin_.append(x)
        Min_.append(Min)
        Max_.append(Max)
    return (Fin_, Min_, Max_)

def suff(ins):
    x = 0
    Min = 0
    Max = 0
    Min_ = [0]
    Max_ = [0]
    for I in ins[::-1]:
        if I == '-':
            x -= 1
            Min -= 1
            Max = max(0, Max - 1)
        else:
            x += 1
            Min = min(0, Min + 1)
            Max += 1
        Min_.append(Min)
        Max_.append(Max)
    return (Min_, Max_)
t = int(input().strip())
answers = ''
for _ in range(t):
    (n, m) = map(int, input().strip().split())
    A = input().strip()
    (f_1, m_1, M_1) = pref(A)
    (m_2, M_2) = suff(A)
    for __ in range(m):
        (l, r) = map(int, input().strip().split())
        Min = min(m_1[l - 1], f_1[l - 1] + m_2[n - r])
        Max = max(M_1[l - 1], f_1[l - 1] + M_2[n - r])
        answers += str(Max - Min + 1) + '\n'
print(answers)
import sys
input = sys.stdin.readline

def main():
    t = int(input())
    for _ in range(t):
        (n, m) = map(int, input().split())
        mini = [0] * (n + 1)
        maxi = [0] * (n + 1)
        xi = [0] * (n + 1)
        s = input()
        x = 0
        for i in range(1, n + 1):
            if s[i - 1] == '-':
                x -= 1
            else:
                x += 1
            xi[i] = x
            if i > 0:
                mini[i] = min(mini[i - 1], x)
                maxi[i] = max(maxi[i - 1], x)
        x = 0
        minif = [0] * (n + 1)
        maxif = [0] * (n + 1)
        xf = [0] * (n + 1)
        for i in range(1, n + 1):
            if s[n - (i - 1) - 1] == '+':
                x -= 1
            else:
                x += 1
            xf[i] = x
            if i > 0:
                minif[i] = min(minif[i - 1], x)
                maxif[i] = max(maxif[i - 1], x)
        for req in range(m):
            (l, r) = map(int, input().split())
            (min1, max1, x1) = (mini[l - 1], maxi[l - 1], xi[l - 1])
            (min2, max2) = (minif[n - r] + x1 - xf[n - r], maxif[n - r] + x1 - xf[n - r])
            sys.stdout.write(str(max(max1, max2) - min(min1, min2) + 1) + '\n')
main()