#include <bits/stdc++.h>

typedef long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 21;
const int inf = (int)1e9 + 123;

using namespace std;

int n, m;
bool g[N][N];
int u, v;
ll dp[(1 << N)][N];
ll ans;

ll solve(int s) {
    memset(dp, 0, sizeof dp);
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
        }
    }
    ll res = 0;
    for (int mask = 2; mask < (1 << s); mask++)
        if (__builtin_popcount(mask) > 1)
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    res += dp[mask][i];
    return res;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> u >> v;
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    cout << ans / 2;
    return 0;
}


#include <bits/stdc++.h>

typedef unsigned long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 20;
const int inf = (int)1e9 + 123;

using namespace std;

int n, m;
bool g[N][N];
int u, v;
ll dp[(1 << N)][N];
ll ans;

ll solve(int s) {
    memset(dp, 0, sizeof dp);
    ll res = 0;
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
            res += dp[mask][to];
        }
    }
    return res / 2;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    printf("%I64d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

typedef long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 21;
const int inf = (int)1e9 + 123;

using namespace std;

int n, m;
bool g[N][N];
int u, v;
int dp[(1 << N)][N];
int ans;

int solve(int s) {
    memset(dp, 0, sizeof dp);
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
        }
    }
    int res = 0;
    for (int mask = 2; mask < (1 << s); mask++)
        if (__builtin_popcount(mask) > 1)
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    res += dp[mask][i];
    return res;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> u >> v;
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    cout << ans / 2;
    return 0;
}


#include <bits/stdc++.h>

typedef unsigned long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 20;
const int inf = (int)1e9 + 123;

using namespace std;

int n, m;
bool g[N][N];
int u, v;
ll dp[(1 << N)][N];
ll ans;

ll solve(int s) {
    memset(dp, 0, sizeof dp);
    ll res = 0;
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
            res += dp[mask][to];
        }
    }
    return res;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    printf("%I64d\n", ans / 2);
    return 0;
}


#include <bits/stdc++.h>

typedef unsigned long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 20;
const int inf = (int)1e9 + 123;

using namespace std;

int n, m;
bool g[N][N];
int u, v;
ll dp[(1 << N)][N];
ll ans;

ll solve(int s) {
    memset(dp, 0, sizeof dp);
    ll res = 0;
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
            if (g[to][s])
                res += dp[mask][to];
        }
    }
    return res / 2;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    printf("%I64d\n", ans);
    return 0;
}


#include <bits/stdc++.h>

typedef long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 20;
const int inf = (int)1e9 + 123;

using namespace std;

ll n, m;
ll g[N][N];
ll u, v;
ll dp[(1 << N)][N];
ll ans;

ll solve(int s) {
    memset(dp, 0, sizeof dp);
    ll res = 0;
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
            res += dp[mask][to];
        }
    }
    return res;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> u >> v;
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    cout << ans / 2;
    return 0;
}


#include <bits/stdc++.h>

typedef long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 19;
const int inf = (int)1e9 + 123;

using namespace std;

int n, m;
bool g[N][N];
int u, v;
ll dp[(1 << N)][N];
ll ans;

ll solve(int s) {
    memset(dp, 0, sizeof dp);
    ll res = 0;
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
            res += dp[mask][to];
        }
    }
    return res;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    printf("%I64d\n", ans / 2);
    return 0;
}


#include <bits/stdc++.h>

typedef long long ll;

#define sz(a) (int)((a).size())
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair

const int N = 20;
const int inf = (int)1e9 + 123;

using namespace std;

int n, m;
bool g[N][N];
int u, v;
ll dp[(1 << N)][N];
ll ans;

ll solve(int s) {
    memset(dp, 0, sizeof dp);
    ll res = 0;
    for (int mask = 1; mask < (1 << s); mask++) {
        if (__builtin_popcount(mask) == 1) {
            for (int i = 0; i < s; i++)
                if ((mask & (1 << i)) > 0)
                    dp[mask][i] = g[s][i];
            continue;
        }
        for (int to = 0; to < s; to++) {
            if ((mask & (1 << to)) == 0)
                continue;
            for (int j = 0; j < s; j++) {
                if (j == to || (mask & (1 << j)) == 0)
                    continue;
                dp[mask][to] += dp[(mask ^ (1 << to))][j] * g[j][to];
            }
            res += dp[mask][to];
        }
    }
    return res;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &u, &v);
        g[u - 1][v - 1] = g[v - 1][u - 1] = 1;
    }
    for (int i = 0; i < n; i++)
        ans += solve(i);
    printf("%I64d\n", ans / 2);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=20;
int n,m;
LL ans,dp[600000][N];
bool G[N][N];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int a,b; scanf("%d%d",&a,&b);
        a--; b--;
        G[a][b]=G[b][a]=1;
    }
    for(int i=0;i<n;i++) dp[1<<i][i]=1;
    int tot=(1<<n);
    for(int i=1;i<tot;i++){
        int st=n+1,cnt=0;
        for(int j=0;j<n;j++) if(i&(1<<j))st=min(st,j), cnt++;
        for(int j=0;j<n;j++) if(dp[i][j]){
            if(G[j][st]&&cnt>=3)ans+=dp[i][j];//form ring here
            for(int k=0;k<n;k++) if(G[j][k]&&k>st){
                if(!(i&(1<<k))) dp[i^(1<<k)][k]+=dp[i][j];//set+k, k be lst pt
            }
        }
    }
    cout<<ans/2<<endl;//*** from the 2direction of ring
    return 0;
}


#include <cstdio>
#include <algorithm>
using namespace std;

int n,m,a[19][19];
long long ans,dp[1<<19][19];

int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        x--;y--;
        a[x][y]=1;
        a[y][x]=1;
        dp[1<<x|1<<y][max(x,y)]=1;
    }
    int mask=(1<<n)-1;
    for(int r=0;r<=mask;r++){
        int s=__builtin_ctz(r);
        for(int t=s+1;t<n;t++) if(dp[r][t]){
            if(a[t][s]) ans+=dp[r][t];
            for(int o=s+1;o<n;o++)
                if(a[t][o] && ~r&1<<o) dp[r|1<<o][o]+=dp[r][t];
        }
    }
    printf("%I64d\n",(ans-m)/2);
}


#include <cstdio>
#include <algorithm>
using namespace std;

// dp[mask][t]表示从某个点出发，访问了mask这些点且最后到达t点的总方案数
// 对于任意某个环，为了避免重复计算，只要取环上编号最小的点作为起点即可
// 最后的ans要排除掉m个a->b->a型的环，另外起点有两个方向可走因此还要除2
int n,m,a[19][19];
long long ans,dp[1<<19][19];

int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        a[x-1][y-1]=1;
        a[y-1][x-1]=1;
        dp[1<<x-1|1<<y-1][max(x,y)-1]=1;
    }
    int mask=(1<<n)-1;
    for(int r=0;r<=mask;r++){
        int s=__builtin_ctz(r);
        for(int t=s+1;t<n;t++) if(dp[r][t]){
            if(a[t][s]) ans+=dp[r][t];
            for(int o=s+1;o<n;o++)
                if(a[t][o] && ~r&1<<o) dp[r|1<<o][o]+=dp[r][t];
        }
    }
    printf("%I64d\n",(ans-m)/2);
}


#include <cstdio>
#include <algorithm>
using namespace std;

// dp[mask][t]表示从某个点出发，访问了mask这些点且最后到达t点的总方案数
// 对于任意某个环，为了避免重复计算，只要取环上编号最小的点作为起点即可
// 最后的ans要排除掉m个a->b->a型的环，另外起点有两个方向可走因此还要除2
int n,m,a[19][19],dp[1<<19][19];

int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        a[x-1][y-1]=1;
        a[y-1][x-1]=1;
        dp[1<<x-1|1<<y-1][max(x,y)-1]=1;
    }
    int mask=(1<<n)-1;
    long long ans=0;
    for(int r=0;r<=mask;r++){
        int s=__builtin_ctz(r);
        for(int t=s+1;t<n;t++) if(dp[r][t]){
            if(a[t][s]) ans+=dp[r][t];
            for(int o=s+1;o<n;o++)
                if(a[t][o] && ~r&1<<o) dp[r|1<<o][o]+=dp[r][t];
        }
    }
    printf("%I64d\n",(ans-m)/2);
}


#include <iostream>

#define STATES 524288
#define NODES 19

using namespace std;

bool E[NODES][NODES];
int C[20];
int N, M;
int W[STATES][NODES][NODES];

int main()
{
    cin >> N >> M;
    for(int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        E[a-1][b-1] = E[b-1][a-1] = true;
    }

    int ans = 0;

    /*for(int i = 0; i < N; i++)
        W[(1 << i)][make_pair(i,i)] = 1;

    for(int m = 0; m < (1 << N); m++)
        for(int f = 0; f < N; f++)
            for(int l = 0; l < N; l++)
            {
                if(!(m & (1 << l)))
                    continue;
                if(!(m & (1 << f)))
                    continue;
                if(f == l)
                    continue;

                int p = m - (1 << l);

                for(int i = 0; i < N; i++)
                    if((p & (1 << i)) && E[i][l])
                        W[m][make_pair(f,l)] += W[p][make_pair(f,i)];

                if(E[f][l] && (m - (1 << f) - (1 << l)))
                {
                    C[__builtin_popcount(m)] += W[m][make_pair(f,l)];
                    //cout << "Adding " << W[m][make_pair(f,l)] << " from " << m << "," << f << "," << l << "\n";
                }
            }

    for(int i = 1; i <= N; i++)
        ans += C[i] / (2*i);*/

    cout << ans;
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define vii vector<pair<int,int>>

const ll INF = 200005;
const ll MOD = 1000000007;

// --------------------------------------------------------------------------

bool E[20][20];
int N, M, W[1000000][20];

int main()
{
    cin >> N >> M;

    while(M--)
    {
        int a, b;
        cin >> a >> b;
        E[a-1][b-1] = E[b-1][a-1] = true;
    }

    f(i,0,N-1) W[1<<i][i] = 1;
    int ans = 0;

    f(m,0,(1<<N)-1) f(l,0,N-1)
    {
        int st = -1;

        f(i,0,N-1)
            if(m & (1 << i))
            {
                st = i;
                break;
            }

        if(st < 0) continue;

        if(E[l][st] && __builtin_popcount(m) > 2) ans += W[m][l];

        f(i,st+1,N-1) if(!(m & (1<<i)) && E[l][i]) W[m + (1<<i)][i] += W[m][l];
    }

    cout << ans/2;
}


#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

#define all(x) x.begin(), x.end()
#define f(i,a,b) for(int i = (a); i <= (b); i++)
#define fd(i,a,b) for(int i = (a); i >= (b); i--)
#define mp make_pair
#define faster_io() ios_base::sync_with_stdio(false)
#define pb push_back
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define vii vector<pair<int,int>>

const ll INF = 200005;
const ll MOD = 1000000007;

// --------------------------------------------------------------------------

bool E[20][20];
int N, M;
ll W[1000000][20];

int main()
{
    cin >> N >> M;

    while(M--)
    {
        int a, b;
        cin >> a >> b;
        E[a-1][b-1] = E[b-1][a-1] = true;
    }

    f(i,0,N-1) W[1<<i][i] = 1;
    ll ans = 0;

    f(m,0,(1<<N)-1) f(l,0,N-1)
    {
        int st = -1;

        f(i,0,N-1)
            if(m & (1 << i))
            {
                st = i;
                break;
            }

        if(st < 0) continue;

        if(E[l][st] && __builtin_popcount(m) > 2) ans += W[m][l];

        f(i,st+1,N-1) if(!(m & (1<<i)) && E[l][i]) W[m + (1<<i)][i] += W[m][l];
    }

    cout << ans/2;
}


#include <iostream>

#define STATES 524288
#define NODES 19

bool E[NODES][NODES];
int N, M;
int W[STATES][NODES];
using namespace std;

int main()
{
    cin >> N >> M;
    for(int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        E[a-1][b-1] = E[b-1][a-1] = true;
    }

    int ans = 0;

    for(int i = 0; i < N; i++)
        W[1 << i][i] = 1;

    for(int m = 0; m < (1 << N); m++)
        for(int l = 0; l < N; l++)
        {
            if(!(m & (1 << l)))
                continue;

            int f;
            for(f = 0; !(m & (1 << f)); f++);
            if(l == f)
                continue;

            int p = m - (1 << l);

            for(int i = 0; i < N; i++)
                if((p & (1 << i)) && E[i][l])
                    W[m][l] += W[p][i];

            if(__builtin_popcount(m) > 2)
                ans += E[f][l] * W[m][l];
        }

    cout << ans / 2;
}


#include <iostream>

#define STATES 524288
#define NODES 19

bool E[NODES][NODES];
int N, M;
long long W[STATES][NODES];
using namespace std;

int main()
{
    cin >> N >> M;
    for(int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        E[a-1][b-1] = E[b-1][a-1] = true;
    }

    long long ans = 0;

    for(int i = 0; i < N; i++)
        W[1 << i][i] = 1;

    for(int m = 0; m < (1 << N); m++)
        for(int l = 0; l < N; l++)
        {
            if(!(m & (1 << l)))
                continue;

            int f;
            for(f = 0; !(m & (1 << f)); f++);
            if(l == f)
                continue;

            int p = m - (1 << l);

            for(int i = 0; i < N; i++)
                if((p & (1 << i)) && E[i][l])
                    W[m][l] += W[p][i];

            if(__builtin_popcount(m) > 2)
                ans += W[m][l] * E[f][l];
        }

    cout << ans / 2;
}


#include <iostream>
#include <map>

#define STATES 524288
#define NODES 20

using namespace std;

bool E[NODES][NODES];
int C[NODES];
int N, M;
map<pair<int,int>, int> W[STATES];

int main()
{
    cin >> N >> M;
    for(int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        E[a-1][b-1] = E[b-1][a-1] = true;
    }

    int ans = 0;

    for(int i = 0; i < N; i++)
        W[(1 << i)][make_pair(i,i)] = 1;

    for(int m = 0; m < (1 << N); m++)
        for(int f = 0; f < N; f++)
            for(int l = 0; l < N; l++)
            {
                if(!(m & (1 << l)))
                    continue;
                if(!(m & (1 << f)))
                    continue;
                if(f == l)
                    continue;

                int p = m - (1 << l);

                for(int i = 0; i < N; i++)
                    if((p & (1 << i)) && E[i][l])
                        W[m][make_pair(f,l)] += W[p][make_pair(f,i)];

                if(E[f][l] && (m - (1 << f) - (1 << l)))
                {
                    C[__builtin_popcount(m)] += W[m][make_pair(f,l)];
                    //cout << "Adding " << W[m][make_pair(f,l)] << " from " << m << "," << f << "," << l << "\n";
                }
            }

    for(int i = 1; i <= N; i++)
        ans += C[i] / (2*i);

    cout << ans;
}


#include <iostream>

#define STATES 524288
#define NODES 20
#define EDGES 171

using namespace std;

bool E[NODES][NODES];
int C[NODES];
int N, M;
int W[STATES][EDGES];

int main()
{
    cin >> N >> M;
    for(int i = 0; i < M; i++)
    {
        int a, b;
        cin >> a >> b;
        E[a-1][b-1] = E[b-1][a-1] = true;
    }

    int ans = 0;

    /*for(int i = 0; i < N; i++)
        W[(1 << i)][make_pair(i,i)] = 1;

    for(int m = 0; m < (1 << N); m++)
        for(int f = 0; f < N; f++)
            for(int l = 0; l < N; l++)
            {
                if(!(m & (1 << l)))
                    continue;
                if(!(m & (1 << f)))
                    continue;
                if(f == l)
                    continue;

                int p = m - (1 << l);

                for(int i = 0; i < N; i++)
                    if((p & (1 << i)) && E[i][l])
                        W[m][make_pair(f,l)] += W[p][make_pair(f,i)];

                if(E[f][l] && (m - (1 << f) - (1 << l)))
                {
                    C[__builtin_popcount(m)] += W[m][make_pair(f,l)];
                    //cout << "Adding " << W[m][make_pair(f,l)] << " from " << m << "," << f << "," << l << "\n";
                }
            }

    for(int i = 1; i <= N; i++)
        ans += C[i] / (2*i);*/

    cout << ans;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool edge[20][20];
long long dp[1<<20][20];
int count1(int mask){
    int res = 0;
    while (mask){
        if (mask&1){
            res += 1;

        }
        mask >>= 1;
    }
    return res;
}

int bit(int id, int mask){
    for (int i=0;i<id;i++) mask >>= 1;
    if (mask&1) return 1;
    else return 0;
}

int first(int mask){
    int id = 0;
    while (mask%2==0) {
        mask >>= 1;
        id ++;
    }
    return id;
}
int main() {
    int n,m;
    cin >> n >> m;
    //cout << count(1024) << endl;
    int a,b;
    for(int i=0;i<m;i++){
        cin >> a >> b;
        a--;b--;
        edge[a][b] = true;
        edge[b][a] = true;
    }
    for (int mask = 1;mask< 1<<n;mask++){
        for (int i=0;i<n;i++){
            if (count1(mask)==1 && bit(i,mask)==1) dp[mask][i] = 1;
            else if (count1(mask)>1 && bit(i,mask)==1 && i!=first(mask)){
                for (int j=0;j<n;j++){
                    if (edge[j][i]) dp[mask][i] += dp[mask^(1<<i)][j];
                }
            }
            else dp[mask][i] = 0;
        }
    }
    long long ans = 0;
    for (int mask = 1;mask< 1<<n;mask++){
        for (int i=0;i<n;i++){
       //     cout << dp[mask][i] << " ";
            if (count1(mask)>=3 && edge[first(mask)][i]) ans += dp[mask][i];
        }
     //   cout << endl;
    }
    cout << ans/2 << endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <cstdio>
#include <utility>
#include <iomanip>
#include <cmath>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int maxn = 19;
LL dp[1<<maxn][maxn];
bool g[maxn][maxn];
vector<int>edge[maxn];
int bits[1<<maxn];
inline bool has(int i,int j){
    return (i>>j)&1;
}
inline int lowbit(int n){
    int c=0;
    while(n>0){
        if(n&1)
            break;
        ++c;
        n>>=1;
    }
    return c;
}
int main(){
    ios::sync_with_stdio(false);
    int i,j,k,n,m,u,v;
    cin>>n>>m;
    int tot=1<<n;
    for(i=1;i<tot;++i){
        bits[i]=bits[i>>1]+(i&1);
    }
    for(i=0;i<m;++i){
        cin>>u>>v;
        --u;
        --v;
        int t=(1<<u)|(1<<v);
        g[u][v]=g[v][u]=true;
        dp[t][max(u,v)]=1;
    }
    LL ans=0;
    for(i=1;i<tot;++i){
        u=lowbit(i);
        for(v=u+1;v<n;++v){
            if(!has(i,v))
                continue;
            if(g[u][v]&&bits[i]>2){
                ans += dp[i][v];
            }
            for(k=u+1;k<n;++k){
                if(!has(i,k)&&g[v][k]){
                    j=i^(1<<k);
                    dp[j][k] += dp[i][v];
                }
            }
        }
    }
    ans>>=1;
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <cstdio>
#include <utility>
#include <iomanip>
#include <cmath>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int maxn = 19;
LL dp[1<<maxn][maxn];
bool g[maxn][maxn];
vector<int>edge[maxn];
int bits[1<<maxn];
int lowbit[1<<maxn];
inline bool has(int i,int j){
    return (i>>j)&1;
}
int main(){
    ios::sync_with_stdio(false);
    int i,j,k,n,m,u,v;
    cin>>n>>m;
    int tot=1<<n;
    for(i=1;i<tot;++i){
        bits[i]=bits[i>>1]+(i&1);
        if(i&1){
            lowbit[i]=0;
        }else{
            lowbit[i]=lowbit[i>>1]+1;
        }
    }
    for(i=0;i<m;++i){
        cin>>u>>v;
        --u;
        --v;
        int t=(1<<u)|(1<<v);
        g[u][v]=g[v][u]=true;
        //cout<<"ASdf "<<t<<endl;
        dp[t][max(u,v)]=1;
    }
    LL ans=0;
    for(i=1;i<tot;++i){
        u=lowbit[i];
        for(v=u+1;v<n;++v){
            if(!has(i,v))
                continue;
            if(g[u][v]&&bits[i]>2){
                ans += dp[i][v];
            }
            for(k=u+1;k<n;++k){
                if(!has(i,k)&&g[v][k]){
                    j=i^(1<<k);
                    dp[j][k] += dp[i][v];
                }
            }
        }
    }
    ans>>=1;
    cout<<ans<<endl;
    return 0;
}


#include <iostream>

using namespace std;

int g[20][20];
long long dp[600000][20] = {0};

int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; ++i)
		dp[1 << i][i] = 1;
	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		--a, --b;
		g[a][b] = g[b][a] = 1;
	}
	long long ans = 0;
	for (int i = 1; i < (1 << n); ++i) {
		int first = 0;
		for (int j = 0; j < n; ++j)
			if (i & (1 << j)) {
				first = j;
				break;
			}
		for (int j = first; j < n; ++j)
			if ((i & (1 << j)) && dp[i][j])
				for (int k = first; k < n; ++k)
					if (g[j][k]) {
						if (i & (1 << k)) {
							if ((1 << j | 1 << k) == i)
								continue;
							if (k == first)
								ans += dp[i][j];
						}
						else
							dp[i | (1 << k)][k] += dp[i][j];
					}
	}
	cout << ans / 2 << endl;
	return 0;
}


// .... .... .....!
// ...... ......!
// .... ....... ..... ..!

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

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fer(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define rof(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define sz(x) (int((x).size()))
#define pb push_back
#define all(X) (X).begin(),(X).end()
#define X first
#define Y second

template<class P, class Q> inline void smin(P &a, Q b) { if (b < a) a = b; }
template<class P, class Q> inline void smax(P &a, Q b) { if (a < b) a = b; }

typedef long long ll;
typedef pair<int, int> pii;

////////////////////////////////////////////////////////////////////////////////

const int maxn = 20;

int n, m;
int g[maxn][maxn];
ll dp[1<<maxn][maxn];
ll ans;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> n >> m;
	rep(i, m) {
		int u, v; cin >> u >> v; u--, v--;
		g[u][v] = g[v][u] = 1;
	}

	rep(i, n) dp[1<<i][i] = 1;
	rep(mask, 1<<n) rep(last, n) if((mask >> last & 1) && ((mask & -mask) != (1 << last))) {
		rep(prev, n) if(g[prev][last] && ((mask ^ (1 << last)) >> prev & 1))
			dp[mask][last] += dp[mask ^ (1 << last)][prev];
			int start = n;
		rof(i, n, 0) if(mask >> i & 1) start = i;

		if(__builtin_popcount(mask) >= 3 && g[last][start]) ans += dp[mask][last];
	}

	ans /= 2;
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

#define REP(i, n) for (int (i) = (0); (i) < (n); (i)++)

const int N = 19;
int n, m;
vector<int> g[N];
LL memo[(1 << N) + 5][N];
int first;

vector<pair<int, int> > visited;

LL dp(int cur_mask, int i) {
	LL &res = memo[cur_mask][i];

	if (res != -1) return res;
	visited.push_back(make_pair(cur_mask, i));

	res = 0;

	if (__builtin_popcount(cur_mask) == 1) return res = 0;
	if (__builtin_popcount(cur_mask) == 2) return res = 1;

	REP(j, g[i].size()) {
		int from = g[i][j];
		if (from != first && (cur_mask & (1 << from))) res += dp(cur_mask ^ (1 << i), from);
	}

	return res;
}

LL solve(int mask) {
	if (__builtin_popcount(mask) < 3) return 0;

	REP(i, n) if (mask & (1 << i)) {
		first = i;
		break;
	}

	LL ans = 0;

	//memset(memo, -1, sizeof(memo));
	visited.clear();

	REP(i, g[first].size()) {
		int to = g[first][i];
		if (mask & (1 << to)) ans += dp(mask, to);
	}

	REP(i, visited.size()) memo[visited[i].first][visited[i].second] = -1;

	return ans / 2;
}

void print(int mask) {
	REP(i, n) if (mask & (1 << i)) cout << i + 1 << " ";
	cout << endl;
}

int main() {
	cin >> n >> m;
	REP(i, m) {
		int from, to;
		cin >> from >> to;
		from --, to --;

		g[from].push_back(to);
		g[to].push_back(from);
	}

	memset(memo, -1, sizeof(memo));

	LL ans = 0;
	for(int mask = 1; mask < (1 << n); mask++) {
		LL t = solve(mask);

		//print(mask);
		//cout << " " << t << endl;

		ans += t;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

#define REP(i, n) for (int (i) = (0); (i) < (n); (i)++)
#define FOR(i, a, n) for (int (i) = (a); (i) < (n); (i)++)

const int N = 19;
int n, m;
vector<int> g[N];
LL dp[(1<<N) + 15][N];

int main() {
	cin >> n >> m;
	REP(i, m) {
		int from, to;
		cin >> from >> to;
		from --, to --;

		g[from].push_back(to);
		g[to].push_back(from);
	}

	FOR(mask, 1, (1 << n)) {
		int first = -1;
		REP(i, n) if (mask & (1 << i)) first = i;

		REP(i, n) {
			if (!(mask & (1 << i))) continue;

			if (__builtin_popcount(mask) == 1)  {
				dp[mask][i] = 1;
				continue;
			}

			if (i == first) continue;

			LL sum = 0;
			REP(j, g[i].size()) {
				int from = g[i][j];

				if (!(mask & (1 << from))) continue;

				sum += dp[mask ^ (1 << i)][from];
			}

			dp[mask][i] = sum;
		}
	}

	LL ans = 0;

	FOR(mask, 1, (1 << n)) if (__builtin_popcount(mask) >= 3) {
		int first = -1;
		REP(i, n) if (mask & (1 << i)) first = i;

		REP(i, g[first].size()) {
			int to = g[first][i];

			ans += dp[mask][to];
		}
	}

	cout << ans / 2 << endl;

	return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cerr <<endl; }}
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<typename T1, typename T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { out << "(" << pair.X << ", " << pair.Y << ")"; return out; }

const int N = 19;
const int K = 1 << 19;
bool edge[N][N];
LL t[K][N];

int main(){
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    int n, m; cin >> n >> m;
    REP(i, n) REP(j, n) edge[i][j] = false;
    REP(i, m) {
        int x, y; cin >> x >> y; --x; --y;
        edge[x][y] = true;
        edge[y][x] = true;
    }

    if (n == 1) {
        printf("1\n");
        return 0;
    }
    if (n == 2) {
        if (m == 0) {
            printf("0\n");
        } else {
            printf("1\n");
        }
        return 0;
    }

    int k = 1 << n;
    REP(kk, k) REP(i, n) t[kk][i] = 0;
    t[1][0] = 1;
/*
    printf("%d %d %d\n", n, m, k);
    REP(i, n) {
        REP(j, n) printf("%d ", edge[i][j]);
        printf("\n");
    }
*/
    REP(kk, k) {
        REP(i, n) if (kk & (1 << i)) {
            REP(j, n) if (edge[i][j]) {
                t[kk][i] += t[kk - (1 << i)][j];
            }
        }
    }

    LL sum = 0;
    REP(i, n) sum += t[k-1][i];
    cout << sum << endl;

    return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(it, (x)) cerr <<*it <<", "; cerr <<endl; }}
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<typename T1, typename T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { out << "(" << pair.X << ", " << pair.Y << ")"; return out; }

const int N = 19;
const int K = 1 << 19;
bool edge[N][N];
LL t[K][N];

int main(){
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    int n, m; cin >> n >> m;
    REP(i, n) REP(j, n) edge[i][j] = false;
    REP(i, m) {
        int x, y; cin >> x >> y; --x; --y;
        edge[x][y] = true;
        edge[y][x] = true;
    }

    if (n == 1) {
        printf("0\n");
        return 0;
    }
    if (n == 2) {
        printf("0\n");
        return 0;
    }

/*
    printf("%d %d %d\n", n, m, k);
    REP(i, n) {
        REP(j, n) printf("%d ", edge[i][j]);
        printf("\n");
    }
*/
    LL sum = 0;
    REP(s, n) {
        int k = 1 << s;
        REP(kk, k << 1) REP(i, s+1) t[kk][i] = 0;
        t[0][s] = 1;
        REP(kk, k) {
            REP(i, s) if (kk & (1 << i)) {
                REP(j, s+1) if (edge[i][j]) {
                    t[kk][i] += t[kk - (1 << i)][j];
                }
                if ((kk != (1 << i)) && (edge[i][s])) {
                    sum += t[kk][i];
                }
            }
        }
    }

    cout << sum / 2 << endl;

    return 0;
}


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

const int MAXN = 30;
vector<int> G[MAXN];
bool vis[MAXN];
int a[MAXN], n, m, c, st;

void dfs(int u, int dep) {
    if (dep == m && st == u) {
        ++ c;
        return;
    }
    for (auto &v : G[u]) {
        if (vis[v]) continue;
        vis[v] = true; a[dep] = v;
        dfs(v, dep + 1);
        vis[v] = false;
    }
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; ++ i) G[i].clear();
    for (int i = 0; i < m; ++ i) {
        int u, v; cin >> u >> v;
        -- u, -- v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int ret = 0;
    for (m = 3; m <= n; ++ m) {
        c = 0;
        for (st = 0; st < n; ++ st) {
            memset(vis, 0, sizeof(vis));
            dfs(st, 0);
        }
        //cout << m << ": " << c << endl;
        ret += c / m;
    }
    cout << ret / 2 << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 20;
bool G[MAXN][MAXN];
LL dp[1<<MAXN][MAXN];
int n, m;

int main() {
    cin >> n >> m;
    for (int i = 0; i < m; ++ i) {
        int u, v; cin >> u >> v;
        -- u, -- v;
        G[u][v] = G[v][u] = 1;
        dp[(1 << u) | (1 << v)][max(u, v)] = 1;
    }
    LL ret = 0;
    for (int mask = 0, s = 1 << n; mask < s; ++ mask) {
        int x = __builtin_ctz(mask);
        for (int y = x + 1; y < n; ++ y) if (dp[mask][y]) {
            if (G[x][y]) ret += dp[mask][y];
            for (int z = x + 1; z < n; ++ z) {
                if (G[y][z] && ~mask & 1 << z) dp[mask | 1 << z][z] += dp[mask][y];
            }
        }
    }
    cout << (ret - m) / 2 << endl;
    return 0;
}


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

#define MAXN 20

short N, M, orig;
long long ct;

vector< short >colleg[MAXN];
long long sol[1<<MAXN][MAXN];
short padre[MAXN];

long long Dfs(short n, int bitmask) {
	if(sol[bitmask][n] != -1) return sol[bitmask][n];
	long long ans=0;
	for(auto i:colleg[n]) {
		if(i < orig) continue;
		if((bitmask & (1<<i))!=0) { if(padre[n]!=i && i==orig) ++ans;  }
		else { padre[i]=n; ans+=Dfs(i, bitmask | (1<<i) ); }
	}
	return sol[bitmask][n]=ans;
}
int main() {
	ios::sync_with_stdio(false);
	cin >> N >> M;
	short a, b;
	for(short i=0; i < M; i++) {
		cin >> a >> b;
		colleg[a].emplace_back( b);
		colleg[b].emplace_back( a);
	}
	for(short i=1; i <= N; i++) {
		memset(sol, -1, sizeof sol);
		memset(padre, -1, sizeof padre);
		orig=i;
		int b=0;
		ct += Dfs(i, b | (1<<i) );
	}
	cout<<ct/2<<endl;
	return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL
#include <pprint.hpp>
#endif
#define endl '\n';
#define pb push_back
#define mod 1000000007
#define ll long long int
#define all(v) v.begin(),v.end()
ll power(ll a,ll b){ll z=1;while(b){if(b&1){z*=a;z%=mod;}a*=a;a%=mod;b/=2;}return z%mod;}
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define test_case 1

void solve(){
	string s;
	cin>>s;
	ll n=s.length();
	ll low=0,high=1e9,ans=-1;
	while(low<=high){
		ll m=(low+high)/2,i=0;
		ll val=m;
		for(;i<n;++i){
			if(s[i]=='+')
				val++;
			else
				val--;
			if(val<0)
				break;
		}
		if(i<n){
			low=m+1;
		}
		else{
			ans=m;
			high=m-1;
		}
	}
	assert(ans!=-1);
	// cout<<ans<<endl;
	ans--;
	ll ex=ans;
	ll res=n,i=0;
	for(;i<n && ans>=0;++i){
		if(s[i]=='+')
			ans++;
		else
			ans--;
	}
	while(i>=0 && ex>=0){
		res += i;
		--i;
		--ex;
	}
	cout<<res<<endl;
}

int main(){
	#ifdef LOCAL
		freopen("in.txt" , "r" , stdin);
	#else
		ios_base::sync_with_stdio(false);	cin.tie(NULL);
	#endif

	int t=1;
	if(test_case)	cin>>t;
	while(t--){
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL
#include <pprint.hpp>
#endif
#define endl '\n';
#define pb push_back
#define mod 1000000007
#define ll long long int
#define all(v) v.begin(),v.end()
ll power(ll a,ll b){ll z=1;while(b){if(b&1){z*=a;z%=mod;}a*=a;a%=mod;b/=2;}return z%mod;}
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define test_case 1

void solve(){
	string s;
	cin>>s;
	ll n=s.length();
	ll low=0,high=1e9,ans=-1;
	while(low<=high){
		ll m=(low+high)/2,i=0;
		ll val=m;
		for(;i<n;++i){
			if(s[i]=='+')
				val++;
			else
				val--;
			if(val<0)
				break;
		}
		if(i<n){
			low=m+1;
		}
		else{
			ans=m;
			high=m-1;
		}
	}
	assert(ans!=-1);
	// cout<<ans<<endl;
	ll res=n;
	ll val=ans;
	for(int i=0;i<n && val>0;++i){
		res += val;
		if(s[i]=='-'){
			val--;
		}
		if(s[i]=='+'){
			res -=val;
			int j=i,cnt=0;
			while(j<n && s[j]=='+'){
				++cnt;
				res +=val;
				++j;
			}
			while(j<n && cnt>0){
				res+=val;
				--cnt;
				++j;
			}
			i=j;
		}
	}
	cout<<res<<endl;
}

int main(){
	#ifdef LOCAL
		freopen("in.txt" , "r" , stdin);
	#else
		ios_base::sync_with_stdio(false);	cin.tie(NULL);
	#endif

	int t=1;
	if(test_case)	cin>>t;
	while(t--){
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL
#include <pprint.hpp>
#endif
#define endl '\n';
#define pb push_back
#define mod 1000000007
#define ll long long int
#define all(v) v.begin(),v.end()
ll power(ll a,ll b){ll z=1;while(b){if(b&1){z*=a;z%=mod;}a*=a;a%=mod;b/=2;}return z%mod;}
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define test_case 1

void solve(){
	string s;
	cin>>s;
	ll n=s.length();
	ll low=0,high=1e9,ans=-1;
	while(low<=high){
		ll m=(low+high)/2,i=0;
		ll val=m;
		for(;i<n;++i){
			if(s[i]=='+')
				val++;
			else
				val--;
			if(val<0)
				break;
		}
		if(i<n){
			low=m+1;
		}
		else{
			ans=m;
			high=m-1;
		}
	}
	assert(ans!=-1);
	// cout<<ans<<endl;
	ll res=n;
	ll val=ans;
	for(int i=0;i<n && val>=0;++i){
		res += val;
		if(s[i]=='-'){
			val--;
		}
		if(s[i]=='+'){
			int j=i,cnt=0;
			while(j<n && s[j]=='+'){
				++cnt;
				res +=val;
				++j;
			}
			while(j<n && cnt>0){
				res+=val;
				--cnt;
				++j;
			}
			i=j;
		}
	}
	cout<<res<<endl;
}

int main(){
	#ifdef LOCAL
		freopen("in.txt" , "r" , stdin);
	#else
		ios_base::sync_with_stdio(false);	cin.tie(NULL);
	#endif

	int t=1;
	if(test_case)	cin>>t;
	while(t--){
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL
#include <pprint.hpp>
#endif
#define endl '\n';
#define pb push_back
#define mod 1000000007
#define ll long long int
#define all(v) v.begin(),v.end()
ll power(ll a,ll b){ll z=1;while(b){if(b&1){z*=a;z%=mod;}a*=a;a%=mod;b/=2;}return z%mod;}
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define test_case 1

void solve(){
	string s;
	cin>>s;
	ll n=s.length();
	ll low=0,high=1e9,ans=-1;
	while(low<=high){
		ll m=(low+high)/2,i=0;
		ll val=m;
		for(;i<n;++i){
			if(s[i]=='+')
				val++;
			else
				val--;
			if(val<0)
				break;
		}
		if(i<n){
			low=m+1;
		}
		else{
			ans=m;
			high=m-1;
		}
	}
	assert(ans!=-1);
	// cout<<ans<<endl;
	ll res=n;
	ll val=ans;
	for(int i=0;i<n;++i){
		res += val;
		if(s[i]=='-'){
			val--;
		}
		if(s[i]=='+'){
			int j=i,cnt=0;
			while(j<n && s[j]=='+'){
				++cnt;
				res +=val;
				++j;
			}
			while(j<n && cnt>0){
				res+=val;
				--cnt;
				++j;
			}
			i=j;
		}
	}
	cout<<res<<endl;
}

int main(){
	#ifdef LOCAL
		freopen("in.txt" , "r" , stdin);
	#else
		ios_base::sync_with_stdio(false);	cin.tie(NULL);
	#endif

	int t=1;
	if(test_case)	cin>>t;
	while(t--){
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>
#ifdef LOCAL
#include <pprint.hpp>
#endif
#define endl '\n';
#define pb push_back
#define mod 1000000007
#define ll long long int
#define all(v) v.begin(),v.end()
ll power(ll a,ll b){ll z=1;while(b){if(b&1){z*=a;z%=mod;}a*=a;a%=mod;b/=2;}return z%mod;}
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define test_case 1

void solve(){
	string s;
	cin>>s;
	ll n=s.length();
	ll low=0,high=n+1,ans=0;
	while(low<=high){
		ll m=(low+high)/2,i=0;
		ll val=m;
		for(;i<n;++i){
			if(s[i]=='+')
				val++;
			else
				val--;
			if(val<0)
				break;
		}
		if(i<n){
			low=m+1;
		}
		else{
			ans=m;
			high=m-1;
		}
	}
	ans--;
	ll ex=ans;
	ll res=n,i=0;
	for(i=0;i<n && ans>=0;++i){
		if(s[i]=='+')
			ans++;
		else
			ans--;
	}
	while(i>=0 && ex>=0){
		res += i;
		--i;
		ex--;
	}
	cout<<res<<endl;
}

int main(){
	#ifdef LOCAL
		freopen("in.txt" , "r" , stdin);
	#else
		ios_base::sync_with_stdio(false);	cin.tie(NULL);
	#endif

	int t=1;
	if(test_case)	cin>>t;
	while(t--){
		solve();
	}

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 1000000
using namespace std;

void solve(){
	string s; cin>>s;
	int n= s.length();

	int cs=0, ans=0;
	ll i;
	for(i=0; i<n; i++){
		if(s[i]=='-'){
			cs--;
		}
		else{
			cs++;
		}
		if(cs<0){
			ans+=i+1;
			cs=0;
		}
	}

	cout<<ans+n<<endl;
}

int main(){
	ll t; cin>>t; cout<<"t"<<endl;
	while(t--){

		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 1000000
using namespace std;

void solve(){
	string s; cin>>s;
	int n= s.length();

	ll cs=0, ans=0;

	for(ll i=0; i<n; i++){
		if(s[i]=='-'){
			cs--;
		}
		else{
			cs++;
		}
		if(cs<0){
			ans+=i+1;
			cs=0;
		}
	}

	cout<<ans+n<<endl;
}

int main(){
	ll t; cin>>t;
	while(t--){

		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 100000
using namespace std;

void solve(){
	string s; cin>>s;
	ll res = 0; ll init = 0;
	while(1){
		ll curr = init;
		bool ok = true;
		for(ll i=0; i<s.length(); i++){
			res++;
			if(s[i]=='+'){
				curr++;
			}
			else{
				curr--;
			}

			if(curr<0){
				ok = false;
				break;
			}
		}
		if(ok){
			break;
		}

		init++;
	}

	cout<<res<<endl;

}

int main(){
	ll t; cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 1000000
using namespace std;

void solve(){
	string s; cin>>s;
	int n= s.length();

	int cs=0, ans=0;
	ll i;
	for(i=0; i<n; i++){
		if(s[i]=='-'){
			cs--;
			if(cs<0){
				ans+=i+1;
				cs=0;
			}
		}
		else{
			cs++;
		}
	}

	if(cs<=0){
		ans+= n;
	}

//	if(cs*(-1)==i){
//		cout<<"adding "<<n<<endl;
//		ans+=n;
//	}

	cout<<ans<<endl;
}

int main(){
	ll t; cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 1000000
using namespace std;

void solve(){
	string s; cin>>s;
	int n= s.length();

	int cs=0, ans=0;
	ll i;
	for(i=0; i<n; i++){
		if(s[i]=='-'){
			cs--;
		}
		else{
			cs++;
		}
		if(cs<0){
			ans+=i+1;
			cs=0;
		}
	}

	cout<<ans+n<<endl;
}

int main(){
	ll t; cin>>t;
	while(t--){

		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 1000000
using namespace std;

void solve(){
	string s; cin>>s;
	int n= s.length();

	int cs=0, ans=0;
	ll i;
	for(i=0; i<n; i++){
		if(s[i]=='-'){
			cs--;
		}
		else{
			cs++;
		}

		if(cs<0){
			ans+=i+1;
			cs++;
		}
	}

	if(cs<=0){
		ans+= n;
	}

//	if(cs*(-1)==i){
//		cout<<"adding "<<n<<endl;
//		ans+=n;
//	}

	cout<<ans<<endl;
}

int main(){
	ll t; cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 1000000
using namespace std;

void solve(){
	string s; cin>>s;
	int n= s.size();

	int cs=0, ans=0;
	ll i;
	for(i=0; i<n; i++){
		if(s[i]=='-'){
			cs--;
		}
		else{
			cs++;
		}
		if(cs<0){
			ans+=i+1;
			cs=0;
		}
	}

	cout<<ans+n<<endl;
}

int main(){
	ll t; cin>>t; cout<<"t"<<endl;
	while(t--){

		solve();
	}
}


#include<bits/stdc++.h>
#define ll long long
#define MAX 1000000
using namespace std;

void solve(){
	string s; cin>>s;
	int n= s.length();

	int cs=0, ans=0;
	ll i;
	for(i=0; i<n; i++){
		if(s[i]=='-'){
			cs--;
		}
		else{
			cs++;
		}

		if(cs<0){
			ans+=i+1;
			cs=0;
		}
	}

	if(cs<=0){
		ans+= n;
	}

//	if(cs*(-1)==i){
//		cout<<"adding "<<n<<endl;
//		ans+=n;
//	}

	cout<<ans<<endl;
}

int main(){
	ll t; cin>>t;
	while(t--){
		solve();
	}
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int m=0, res=0;
        for (size_t i = 0; i < s.size(); i++)
        {
            if(s[i]=='+')   m++;
            else    m--;
            if(m<0){
                res+=(i+1);
                m=0;
            }
        }
        res +=s.size();
        cout<<res<<"\n";
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int res= 0;
        int init =0;
        while(true){
            int cur =init;
            bool ok =true;
            for (size_t i = 0; i < s.size(); i++)
            {
                res++;
                if(s[i]=='+')   cur++;
                else            cur--;
                //cout<<cur<<" ";
                if(cur<0){
                    ok =false;
                    break;
                }
            }
            if(ok)  break;
            init++;
        }
        cout<<res<<"\n";
    }

}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        long long int m=0, res=0;
        for (size_t i = 0; i < s.size(); i++)
        {
            if(s[i]=='+')   m++;
            else    m--;
            if(m<0){
                res+=(i+1);
                m=0;
            }
        }
        res +=s.size();
        cout<<res<<"\n";
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int pend=0, ptotal=0;
        bool f=0;
        for (int i =  s.size() - 1; i >= 0; i--)
        {
            if(s[i]=='-')   f=1;
            if(s[i]=='+' && f==0){
                pend++;
                ptotal++;
            }
            else if(s[i]=='+' && f)     ptotal++;
        }
        //cout<<ptotal<<pend<<"   ";
        int m=s.size()-2*ptotal+pend;
        int res= m*(m+1)/2;
        res += s.size();
        cout<<res<<"\n";
    }
}

/*
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int res= 0;
        int init =0;
        while(true){
            int cur =init;
            bool ok =true;
            for (size_t i = 0; i < s.size(); i++)
            {
                res++;
                if(s[i]=='+')   cur++;
                else            cur--;
                //cout<<cur<<" ";
                if(cur<0){
                    ok =false;
                    break;
                }
            }
            if(ok)  break;
            init++;
        }
        cout<<res<<"\n";
    }

}
*/


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

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int m=0, res=0;
        for (size_t i = 0; i < s.size(); i++)
        {
            if(s[i]=='+')   m++;
            else    m--;
            if(m<0) res+=(i+1);
        }
        res +=s.size();
        cout<<res<<"\n";
    }
}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int res= 0;
        int init =0;
        int a=50;
        while(a--){
            int cur =init;
            bool ok =true;
            for (size_t i = 0; i < s.size(); i++)
            {
                res++;
                if(s[i]=='+')   cur++;
                else            cur--;
                //cout<<cur<<" ";
                if(cur<0){
                    ok =false;
                    break;
                }
            }
            if(ok)  break;
            init++;
        }
        cout<<res<<"\n";
    }

}


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

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int pend=0, ptotal=0;
        bool f=0;
        for (int i =  s.size() - 1; i >= 0; i--)
        {
            if(s[i]=='-')   f=1;
            if(s[i]=='+' && f==0){
                pend++;
                ptotal++;
            }
            else if(s[i]=='+' && f)     ptotal++;
        }
        //cout<<ptotal<<pend<<"   ";
        int res=0;
        int m=s.size()-2*ptotal+pend;
        if(m<0) res=s.size();
        else{
            res= m*(m+1)/2;
            res += s.size();
        }
        cout<<res<<"\n";
    }
}

/*
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;   cin>>s;
        int res= 0;
        int init =0;
        while(true){
            int cur =init;
            bool ok =true;
            for (size_t i = 0; i < s.size(); i++)
            {
                res++;
                if(s[i]=='+')   cur++;
                else            cur--;
                //cout<<cur<<" ";
                if(cur<0){
                    ok =false;
                    break;
                }
            }
            if(ok)  break;
            init++;
        }
        cout<<res<<"\n";
    }

}
*/


#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        long long p=0,res=0,resAnt=0;
        for(int i=1;i<=s.size();i++){
            res++;
            if(s[i-1]=='-'){
                p--;
            }
            else{
                p++;
            }
           // cout << res << " " << resAnt << endl;
            if(p<0){
                p=0;
                res+=resAnt;
                resAnt=res;
                res=i;
            }
            //cout << res << " " << resAnt << endl;
            //cout << endl;
        }
        res+=resAnt;
        cout << res << endl;

    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{

    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        long long j = 0, p = 0, res = 0, resAnt = 0;
        for (int i = 1; i <= s.size(); i++)
        {
            res++;
            if (s[i - 1] == '-')
            {
                p--;
            }
            else
            {
                p++;
            }
            // cout << res << " " << resAnt << endl;
            if (p < 0)
            {
                p = 0;
                j = i;
                res += resAnt;
                resAnt = res;
                res = i;
            }
            //cout << res << " " << resAnt << endl;
            //cout << endl;
        }
        res += resAnt;
        cout << res << endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    ios::sync_with_stdio(false);
    int t;
    scanf("%d ",&t);
    while(t--){
        char s[1000016];
        scanf("%s",s);
        //printf("%s %d",s,strlen(s));
        long long res=0;
        for(long long init=0;;init++){
            long long cur = init;
            bool ok = true;
            for(int i = 0;i<strlen(s);i++){
                res = res + 1;
                //cout << res << " ";
                if(s[i] == '+'){
                    //cout << " a ";
                    cur = cur + 1;
                }
                else{
                    //cout << " b ";
                    cur = cur - 1;
                }
                if (cur < 0){
                    //cout << " c ";
                    ok = false;
                    break;
                }
            }
            if (ok){
                break;
            }

        }

        printf("%d\n",res);

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        int j=0,p=0,res=0,resAnt=0;
        for(int i=1;i<=s.size();i++){
            res++;
            if(s[i-1]=='-'){
                p--;
            }
            else{
                p++;
            }
           // cout << res << " " << resAnt << endl;
            if(p<0){
                p=0;
                j=i;
                res+=resAnt;
                resAnt=res;
                res=i;
            }
            //cout << res << " " << resAnt << endl;
            //cout << endl;
        }
        res+=resAnt;
        cout << res << endl;

    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        long long j=0,p=0,res=0,resAnt=0;
        for(int i=1;i<=s.size();i++){
            res++;
            if(s[i-1]=='-'){
                p--;
            }
            else{
                p++;
            }
           // cout << res << " " << resAnt << endl;
            if(p<0){
                p=0;
                j=i;
                res+=resAnt;
                resAnt=res;
                res=i;
            }
            //cout << res << " " << resAnt << endl;
            //cout << endl;
        }
        res+=resAnt;
        cout << res << endl;

    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{

    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        long long j = 0, p = 0, res = 0, resAnt = 0;
        for (int i = 1; i <= s.size(); i++)
        {
            res++;
            if (s[i - 1] == '-')
            {
                p--;
            }
            else
            {
                p++;
            }
            // cout << res << " " << resAnt << endl;
            if (p < 0)
            {
                p = 0;
                j = i;
                res += resAnt;
                resAnt = res;
                res = i;
            }
            //cout << res << " " << resAnt << endl;
            //cout << endl;
        }
        res += resAnt;
        cout << res << endl;
    }

    return 0;


#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        long long res=0;
        for(long long init=0;;init++){
            long long cur = init;
            bool ok = true;
            for(int i = 0;i<s.size();i++){
                res = res + 1;
                //cout << res << " ";
                if(s[i] == '+'){
                    //cout << " a ";
                    cur = cur + 1;
                }
                else{
                    //cout << " b ";
                    cur = cur - 1;
                }
                if (cur < 0){
                    //cout << " c ";
                    ok = false;
                    break;
                }
            }
            if (ok){
                break;
            }

        }
        cout << res << endl;

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        int j=0,p=0,res=0;
        for(int i=0;i<s.size();i++){
            res++;
            if(s[i]=='-'){
                p--;
            }
            else{
                p++;
            }
            if(p<0){
                p=0;
                res+=i+1;
            }
        }
        cout << res << endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main(){

    int t;
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        int j=0,p=0,res=0;
        for(int i=0;i<s.size();i++){
            //cout << s[i] << " ";
            if(s[i]=='-'){
                //cout << 1 << " ";
                p--;
            }
            else{
                //cout << 2 << " ";
                p++;
            }
            if(p<0){
                //cout << 3 << " ";
                j++;
                p=j;
                i=-1;
            }
            //cout << endl;
            res++;
        }
        cout << res << endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{

    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        long long j = 0, p = 0, res = 0;
        for (int i = 0; i < s.size(); i++)
        {
            res++;
            if (s[i] == '-')
            {
                p--;
            }
            else
            {
                p++;
            }
            if (p < 0)
            {
                p = 0;
                res += i + 1;
            }
        }
        cout << res << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				plus++;
 			}
 			else
 			{
 				minus++;
 			}
 		}
 		int cnt = 0;
 		int ans = 0;
 		int f = 0;
 		for(int i = 0 ; i < n;i++)
 		{
 			if(s[i]=='+')
 			{
 				cnt+=1;
 				continue;
 			}
 			else
 			{
 				cnt-=1;
 			}
 			if(cnt<0)
 			{
 				f = 1;
 				ans+=min(i+1,n);
 			}
 		}
 		if(!f)
 		{
 			ans = n;
 		}
 		if(plus==0)
 		{
 			ans+=n;
 		}
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		vi pre(n,0);
 		int mi = INT_MAX;
 		map<int,int> mm;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				if(i==0)
 					pre[i] = 1;
 				else
 				pre[i] = pre[i-1] + 1;
 			}
 			else
 			{
 				if(i==0)
 					pre[i] = -1;
 				else
 				pre[i] = pre[i-1] - 1;
 			}
 			if(mm[pre[i]]==0)
 				mm[pre[i]] = i+1;
 			mi = min(pre[i],mi);
 		}
 		for(int i = 0 ;i<n;i++ )
 			pre[i]*=-1;
 		int till = abs(mi-0);
 		int ans = 0;
 		//db(till);
 		for(int cnt = 0 ; cnt <= till;cnt++)
 		{
 			if(mm[-1*cnt-1]==0)
 				ans+=n;
 			else
 				ans+=(mm[(-1*cnt)-1]);
 		//	db(ans);
 		}
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		vi v;
 		int cnt = 0;
 		int mi = INT_MAX;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				cnt++;
 				plus++;
 			}
 			else
 			{
 				cnt--;
 				minus++;
 				v.pb(i+1);
 			}
 			mi = min(cnt,mi);
 		}
 		if(mi>0)
 			mi = 0;
 		mi*=-1;
 		int ind = 0;
 		int co = 0;
 		int ans = 0,f = 0;
 		for(int i=0;i<n;i++)
 		{
 			if(s[i]=='+')
 			{
 				co+=1;
 			}
 			else
 			{
 				co-=1;
 				if(co<0 && ind<=mi)
 				ans+=(i+1),f=1,ind++;
 			}
 		}
 		while(ind<=mi)
 		{
 			ans+=n;
 			ind++;
 		}
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		vi v;
 		int cnt = 0;
 		int mi = INT_MAX;
 		int pre[n] = {0};
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				if(i==0)
 					pre[i] = 1;
 				else
 					pre[i] = pre[i-1] + 1;
 				cnt++;
 				plus++;
 			}
 			else
 			{
 				if(i==0)
 					pre[i] = -1;
 				else
 					pre[i] = pre[i-1] - 1;
 				cnt--;
 				minus++;
 				v.pb(i+1);
 			}
 			mi = min(cnt,mi);
 		}
 		if(mi>0)
 			mi = 0;
 		mi*=-1;
 		for(int i = 0 ; i < n;i++)
 			pre[i]*=-1;
 		int ind = 0;
 		int co = 0;
 		int ans = 0,f = 0;
 		for(int i=0;i<n;i++)
 		{
 			if(s[i]=='+')
 			{
 				co+=1;
 				continue;
 			}
 			else
 			{
 				co-=1;
 			}
 			if(ind-pre[i]<0)
 			{
 				ans+=(i+1);
 				f = 1;
 				ind++;
 			}
 		}
 		while(ind<=mi)
 		{
 			ans+=n;
 			ind++;
 		}
 		if(!f)
 			ans = n;
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		vi v;
 		int cnt = 0;
 		int mi = INT_MAX;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				cnt++;
 				plus++;
 			}
 			else
 			{
 				cnt--;
 				minus++;
 				v.pb(i+1);
 			}
 			mi = min(cnt,mi);
 		}
 		if(minus==0)
 			mi = 0;
 		mi*=-1;
 		int ind = 0;
 		int co = 0;
 		int ans = 0,f = 0;
 		for(int i=0;i<n;i++)
 		{
 			if(s[i]=='+')
 			{
 				co+=1;
 			}
 			else
 			{
 				co-=1;
 				if(co<0 && ind<=mi)
 				ans+=(i+1),f=1,ind++;
 			}
 		}
 		while(ind<=mi)
 		{
 			ans+=n;
 			ind++;
 		}
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		vi v;
 		int cnt = 0;
 		int mi = INT_MAX;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				cnt++;
 				plus++;
 			}
 			else
 			{
 				cnt--;
 				minus++;
 				v.pb(i+1);
 			}
 			mi = min(cnt,mi);
 		}
 		if(minus==0)
 			mi = 0;
 		mi*=-1;
 		int ind = 0;
 		int co = 0;
 		int ans = 0,f = 0;
 		for(int i=0;i<n;i++)
 		{
 			if(s[i]=='+')
 			{
 				co+=1;
 			}
 			else
 			{
 				co-=1;
 				if(co<0 && ind<=mi)
 				ans+=(v[ind++]),f=1;
 			}
 		}
 		while(ind<=mi)
 		{
 			ans+=n;
 			ind++;
 		}
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		vi v;
 		int cnt = 0;
 		int mi = INT_MAX;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				cnt++;
 				plus++;
 			}
 			else
 			{
 				cnt--;
 				minus++;
 				v.pb(i+1);
 			}
 			mi = min(cnt,mi);
 		}
 		mi*=-1;
 		int ind = 0;
 		int co = 0;
 		int ans = 0,f = 0;
 		for(int i=0;i<n;i++)
 		{
 			if(s[i]=='+')
 			{
 				co+=1;
 			}
 			else
 			{
 				co-=1;
 				if(co<0 && ind<=mi)
 				ans+=(v[ind++]),f=1;
 			}
 		}
 		if(plus==0 || !f || plus==minus)
 			ans+=n;
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				plus++;
 			}
 			else
 			{
 				minus++;
 			}
 		}
 		int cnt = 0;
 		int ans = 0;
 		int f = 0;
 		int mi = -1;
 		int eval = 0;
 		for(int i = 0 ; i < n;i++)
 		{
 			if(s[i]=='+')
 			{
 				cnt+=1;
 				continue;
 			}
 			else
 			{
 				cnt-=1;
 			}
 			if(cnt<0 && eval<minus)
 			{
 				f = 1;
 				eval++;
 				ans+=min(i+1,n);
 			}
 		}
 		if(!f)
 			ans = n;
 		if(plus==0)
 		{
 			ans+=n;
 		}
 		cout << ans << "\n";
 	}
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ld              long double
#define mod             1000000007
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vpii            vector<pair<int,int>>
#define fr(i,a,b)     for(int i = a; i < b; i += 1)
#define all(x)          x.begin(),x.end()
#define sz(a)           (int)(a.size())
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define input(a)      for(auto it = a.begin(); it != a.end(); it++) { int value; cin >> value; *it = value;}
#define print(a)        for(auto it = a.begin() ; it != a.end() ; it++){ cout << *it << " " ;}cout << '\n';
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

#define SS                      stringstream
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
void FASTIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

signed main()
{
    FASTIO();
 	w(t)
 	{
 		string s;
 		cin >> s;
 		int n = s.size();
 		int plus = 0;
 		int minus = 0;
 		vi v;
 		int cnt = 0;
 		int mi = INT_MAX;
 		for(int i = 0 ; i <n;i++)
 		{
 			if(s[i]=='+')
 			{
 				cnt++;
 				plus++;
 			}
 			else
 			{
 				cnt--;
 				minus++;
 				v.pb(i+1);
 			}
 			mi = min(cnt,mi);
 		}
 		mi*=-1;
 		int ind = 0;
 		int co = 0;
 		int ans = 0,f = 0;
 		for(int i=0;i<n;i++)
 		{
 			if(s[i]=='+')
 			{
 				co+=1;
 			}
 			else
 			{
 				co-=1;
 				if(co<0 && ind<=mi)
 				ans+=(v[ind++]),f=1;
 			}
 		}
 		if(plus==0 || !f)
 			ans+=n;
 		cout << ans << "\n";
 	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
// Ashishgup moves first.
#define as cout << "Ashishgup" << "\n"
#define fa cout << "FastestFinger" << "\n"
int main(){
    int t; cin >> t;
    while(t--){
        int n; cin >> n;
        if(n==1){
            fa; continue;
        }
        if(n&1 || n==2) {
            as; continue;
        }
        int tmp = n;
        while(tmp%2==0) tmp /= 2;
        if(tmp==1){
            fa; continue;
        }
        if(n%4==0){
            as; continue;
        }
        tmp = n/2;
        bool flag = true;
        for(int i = 3; i <= ceil(sqrt(tmp)); i++)
            if(tmp%i==0) {
                as; flag = false; break;
            }
        if(flag) fa;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        ll n = s.size(), plus = 0, minus = 0;
        for(int i = 0; i < n; i++){
            (s[i]=='+') ? (plus++) : (minus++);
        }
        if(plus>=minus){
            cout << n << "\n"; continue;
        }
        if(minus==n){
            cout << (n*(n+1))/2 + n << "\n"; continue;
        }
        ll cur = 0;
        vector<ll> pre(n+1);
        for(int i = 0; i < n; i++){
            (s[i]=='+') ? (cur++) : (cur--);
            if(i==n-1 && cur>=0) pre[i+1] = n;
            if(cur<0){
                cur++; pre[i+1] = i+1;
            }
        }
        ll ans = 0;
        for(int i = 1; i <= n; i++) ans += pre[i];
        cout << ans << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        ll n = s.size(), cur = 0, ans = s.size();
        for(int i = 0; i < n; i++){
            (s[i]=='+') ? (cur++) : (cur--);
            if(cur<0){
                ans += i+1; cur++;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
ll n;
int main(){
    int t; cin >> t;
    while(t--){
        cin >> s;
        n = s.size();
        s.insert(s.begin(),'X');
        ll res = 0, cur = 0;
        ll mn = INT_MAX;
        bool all = true;
        for(int i = 1; i<=n; i++){
            if(s[i]=='+'){
                cur++; all = false;
            }
            else cur--;
            mn = min(mn, cur);
        }
        if(cur>=0){
            cout << n << "\n"; continue;
        }
        if(all){
            cout << (n*(n+1))/2 + n << "\n";
            continue;
        }
        for(int i = 1; i<=n; i++){
            if(s[i]=='+'){
                cur++; continue;
            }
            else{
                cur--;
            }
            if(cur<0){
                res += i;
            }
        }
        if(cur<0 && s[n]=='+'){
            res += n;
        }
        cout << res << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        ll n = s.size(), cur = 0;
        vector<ll> pre(n+1);
        for(int i = 0; i < n; i++){
            (s[i]=='+') ? (cur++) : (cur--);
            if(cur<0){
                pre[i+1] = i+1;
                if(i == n-1){
                    pre[0] = n; break;
                }
                cur++;
            }
        }
        if(cur>=0) pre[n] = n;
        ll ans = 0;
        for(int i = 0; i <= n; i++) ans += pre[i];
        cout << ans << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        ll n = s.size(), cur = 0;
        vector<ll> pre(n+1);
        for(int i = 0; i < n; i++){
            (s[i]=='+') ? (cur++) : (cur--);
            if(i==n-1 && cur>=0) pre[i+1] = n;
            if(cur<0){
                cur++; pre[i+1] = i+1;
            }
        }
        ll ans = 0;
        for(int i = 1; i <= n; i++) ans += pre[i];
        cout << ans << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        ll n = s.size(), cur = 0;
        if(count(s.begin(), s.end(), s[0])==n && s[0]=='-'){
            cout << (n*(n+1))/2 + n << "\n"; continue;
        }
        vector<ll> pre(n+1);
        for(int i = 0; i < n; i++){
            (s[i]=='+') ? (cur++) : (cur--);
            if(i==n-1 && cur>=0) pre[i+1] = n;
            if(cur<0){
                cur++; pre[i+1] = i+1;
            }
        }
        ll ans = 0;
        for(int i = 1; i <= n; i++) ans += pre[i];
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(){
  string s;
  cin >> s;
  int pref[s.length() + 1];
  pref[0] = 0;
  int myMin = 100000;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(myMin, pref[i]);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length() << endl;
    return;
  }
  int arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    func();
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(){
  string s;
  cin >> s;
  int pref[s.length() + 1];
  pref[0] = 0;
  int myMin = 100000000;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(myMin, pref[i]);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length() << endl;
    return;
  }
  int arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    func();
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(string s){
  int pref[s.length() + 1];
  pref[0] = 0;
  long myMin = 100000000;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    if(myMin > pref[i]){myMin = pref[i];}
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length();
    return;
  }
  int arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    func(s);
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(string s){
  int pref[s.length() + 1];
  pref[0] = 0;
  long myMin = 100000000;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(pref[i],myMin);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length();
    return;
  }
  int arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    func(s);
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(){
  string s;
  cin >> s;
  long long pref[s.length() + 1];
  pref[0] = 0;
  long long myMin = 10000000000;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(myMin, pref[i]);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length() << endl;
    return;
  }
  long long arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    func();
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(){
  string s;
  cin >> s;
  long long pref[s.length() + 1];
  pref[0] = 0;
  long long myMin = 0;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(myMin, pref[i]);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length() << endl;
    return;
  }
  long long arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    func();
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(){
  string s;
  cin >> s;
  int pref[s.length() + 1];
  pref[0] = 0;
  long long myMin = 100000000;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(pref[i],myMin);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length();
    return;
  }
  int arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    func();
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(){
  string s;
  cin >> s;
  long long pref[s.length() + 1];
  pref[0] = 0;
  long long myMin = 0;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(myMin, pref[i]);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length() << endl;
    return;
  }
  long long arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  long long counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    func();
  }
}


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
void func(string s){
  int pref[s.length() + 1];
  pref[0] = 0;
  int myMin = 100000000;
  for(int i = 1; i <= s.length(); i++){
    if(s[i-1] == '+'){pref[i] = pref[i-1] + 1;}
    if(s[i-1] == '-'){pref[i] = pref[i-1] - 1;}
    myMin = min(pref[i],myMin);
    //cout << pref[i] << " ";
  }
  //cout << endl;
  if(myMin >= 0){
    cout << s.length();
    return;
  }
  int arr[-1 * myMin + 1];
  //maps the minimum value to the first occurence
  for(int i = 0; i < -1 * myMin + 1; i++){
    arr[i] = -1;
  }
  for(int i = 1; i <= s.length(); i++){
    if(pref[i] < 0 ){
      if(arr[-1 * pref[i]]==-1){
        arr[-1 * pref[i]] = i;
        //cout << arr[-1 * pref[i]] << endl;
        //cout << arr[-1 * pref[i]] << endl;
      }
    }
  }
  int counter = 0;
  for(int i = 1; i < -1 * myMin + 1; i++){
    counter += arr[i];
  }
  cout << counter + s.length() << endl;

}
int main() {
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    func(s);
  }
}


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

void solve()
{
    string s;
    cin>>s;
    long long res = 0;
    int curr;
    while(true)
    {
        curr = i;
        bool ok = true;
        for(int i = 0; i < s.length(); i++)
        {
            res += 1;
            if(s[i] == '+')
            curr += 1;
            else curr -= 1;
            if(curr < 0)
            {
                ok = false;
                break;
            }

        }
        if(ok)
        {
            break;
        }
    }
    cout<<res<<endl;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


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

void solve()
{
    string s;
    cin>>s;
    long long curr , res;
    curr = res = 0;
    for(int i = 0; i < s.length(); i++)
    {
        if(s[i] == '+') curr++;
        else curr--;
        if(curr < 0)
        {
            curr = 0;
            ans += i+1;
        }
    }
    ans += s.length();
    cout<<ans<<endl;return;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


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

void solve()
{
    string s;
    cin>>s;
    long long res = 0;
    int curr;
    for(int i = 0; i < (2 * s.length()); i++)
    {
        curr = i;
        bool ok = true;
        for(int i = 0; i < s.length(); i++)
        {
            res += 1;
            if(s[i] == '+')
            curr += 1;
            else curr -= 1;
            if(curr < 0)
            {
                ok = false;
                break;
            }

        }
        if(ok)
        {
            break;
        }
    }
    cout<<res<<endl;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}