#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<memory>
#include<iomanip>
#include<algorithm>
#include<utility>
#define M_PI 3.14159265358979323846
#define ll unsigned long long
#define ld long double
#define int long long
#define E exp(1.0)
using namespace std;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) {
	return a * b / gcd(a, b);
}
template<class T>
T MIN(const T& a, const T& b) {
	return a < b ? a : b;
}
template<class T>
T MAX(const T& a, const T& b) {
	return a > b ? a : b;
}
int diff(int a, int b, int c, int d) {
	//cout << a << " " << b << " " << c << " " << d << endl;
	if(a<=c){
		if (b >= d) return d - c + 1;
		else if (b >= c) {
			return b - c + 1;
		}
	}
	else {
		if (b <= d) return b - a + 1;
		else if (a <= d) {
			return d - a + 1;
		}
	}
	return 0;
}
signed main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		string a;
		cin >> a;
		vector<vector<int>>pre(n + 1);
		vector<vector<int>>fix(n + 2);
		fix[n + 1] = { 0,0,0 };
		pre[0] = { 0,0,0 };
		for (int i = 0; i < n; i++) {
			pre[i + 1].resize(3);
			int p = pre[i][0];
			if (a[i] == '-') p--;
			else p++;
			if (p >= pre[i][1] && p <= pre[i][2]) pre[i + 1] = { p,pre[i][1],pre[i][2] };
			else if (p < pre[i][1]) pre[i + 1] = { p,p,pre[i][2] };
			else pre[i + 1] = { p,pre[i][1],p };
		}
		int cnt = 0, mx = 0, mi = 0;
		for (int i = n - 1; i >= 0; i--) {
			if (a[i] == '-') {
				cnt--;mi--, mx--;
				mx = max((int)0, mx);
			}
			else {
				cnt++;mx++, mi++;
				mi = min((int)0, mi);
			}
			if(a[i]=='-') mi = min(mi, (int)-1);
			else mx = max(mx, (int)1);
			//cout << i + 1 << " " << mi << " " << mx << endl;
			fix[i + 1] = { cnt,mi,mx };
		}
		for (int i = 0; i < m; i++) {
			int l, r;
			cin >> l >> r;
			int sum = pre[l - 1][2] - pre[l - 1][1] + 1 + fix[r + 1][2] - fix[r + 1][1] + 1;
			sum -= diff(pre[l - 1][1], pre[l - 1][2], fix[r + 1][1]+ pre[l - 1][0], fix[r + 1][2]+ pre[l - 1][0]);
			cout << sum << endl;
		}
	}
	return 0;
}/*
 */