#include <bits/stdc++.h>
using namespace std;

#define int long long 
#define endl "\n"
#define MOD 1000000007
#define INF LLONG_MAX
#define MINF LLONG_MIN
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);

#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define si set<int>

int pow(int x, int y, int mod) { 
    int temp; 
    if(y == 0) 
        return 1; 
    
    temp = (pow(x, y / 2, mod)) % mod; 
    
    if (y % 2 == 0) 
        return (temp * temp) % mod; 
    else
        return (((x * temp) % mod) * temp) % mod; 
} 

void solve() {
    
    int n, m; cin >> n >> m;

    vector<int> arr(n + 1);
    arr[0] = 0;
    for(int i = 1; i <= n; i ++) {

        char ch; cin >> ch;

        if(ch == '+')
            arr[i] = arr[i - 1] + 1;
        else
            arr[i] = arr[i - 1] - 1;
    }

    vector<int> prevMin(n + 1), prevMax(n + 1), nextMin(n + 1), nextMax(n + 1);

    stack<int> s;
    nextMin[n] = arr[n]; 
    for(int i = n - 1; i >= 0; i --) {

        nextMin[i] = min(arr[i], nextMin[i + 1]);
    }

    nextMax[n] = arr[n];
    for(int i = n - 1; i >= 0; i --) {

        nextMax[i] = max(arr[i], nextMax[i + 1]);
    }

    prevMax[0] = arr[0];

    for(int i = 1; i <= n; i ++)
        prevMax[i] = max(arr[i], prevMax[i - 1]);

    prevMin[0] = arr[0];
    for(int i = 1; i <= n; i ++)
        prevMin[i] = min(arr[i], prevMin[i - 1]);

    for(int i = 0; i < m; i ++) {

        int l, r; cin >> l >> r;

        if(r == n) {
            int ans = abs(prevMax[l - 1]) + abs(prevMin[l - 1]) + 1;
            cout << ans << endl;
            continue;;
        }

        int a, b, c, d;

        int x, y;

        c = nextMax[r] - arr[r], d = arr[r] - nextMin[r];
        a = prevMax[l - 1], b = prevMin[l - 1];

        x = max(a, arr[l - 1] + c);
        y = min(b, arr[l - 1] - d);

        cout << abs(x) + abs(y) + 1 << endl;
    }

}

int32_t main() {

    FASTIO
    int t; 
    cin >> t;
    // t = 1;
    while(t --) 
        solve();
}