#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
//	freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==root&&deep[lca3]<=deep[root])ins(1,n,1,1,n,x);
			else
			if (lca1==lca2&&lca1!=lca3)ins(1,n,1,l[lca3],r[lca3],x);
			else{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}
		}else{
			int lca1=getlca(v,root);
			if (root==v)printf("%I64d\n",get(1,n,1,1,n));
			else
			if (lca1!=v)printf("%I64d\n",get(1,n,1,l[v],r[v]));
			else{
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}

		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca3!=root){
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}else ins(1,n,1,1,n,x);
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (deep[lca3]>deep[lca1])ins(1,n,1,l[lca3],r[lca3],x);
			else
			if (lca1==root)ins(1,n,1,1,n,x);
			else{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==root)ins(1,n,1,1,n,x);
			else
			if (lca1==v||lca2==u||deep[lca3]<=deep[lca1])
			{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}else
				ins(1,n,1,l[lca3],r[lca3],x);
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==root)ins(1,n,1,1,n,x);
			else
			if (lca1==v||lca2==u||deep[lca3]<=deep[lca1])
			{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}else
				ins(1,n,1,l[lca3],r[lca3],x);
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==root){
				if (lca2!=root)ins(1,n,1,1,n,x);
				else
					ins(1,n,1,l[lca3],r[lca3],x);
			}else{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==root&&deep[lca3]<=deep[root])ins(1,n,1,1,n,x);
			else
			if (lca1==v||lca2==u||deep[lca3]<=deep[lca1])
			{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}else
				ins(1,n,1,l[lca3],r[lca3],x);
		}else{
			int lca1=getlca(v,root);
			if (lca1!=v||root==v)printf("%I64d\n",get(1,n,1,l[v],r[v]));
			else{
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}

		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==lca2&&lca1!=root)ins(1,n,1,l[lca3],r[lca3],x);
			else
			if (lca1==root)ins(1,n,1,1,n,x);
			else{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==lca2&&lca1!=u&&lca2!=v)ins(1,n,1,l[lca3],r[lca3],x);
			else
			if (lca1==root)ins(1,n,1,1,n,x);
			else{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 200000
using namespace std;
int g[N],a[N*2][2],v[N],deep[N],fa[N][20],l[N],r[N],to[N];
long long f[N*3],sig[N*3];
int n,q;
void ins(int x,int y){
	static int sum=1;
	a[++sum][0]=y,a[sum][1]=g[x],g[x]=sum;
}
void dfs(int x){
	static int sum=0;
	to[l[x]=++sum]=x;
	deep[x]++;
	for (int i=0;fa[fa[x][i]][i];i++)fa[x][i+1]=fa[fa[x][i]][i];
	for (int i=g[x];i;i=a[i][1])
		if (a[i][0]!=fa[x][0]){
			deep[a[i][0]]=deep[x];
			fa[a[i][0]][0]=x;
			dfs(a[i][0]);
		}
	r[x]=sum;
}
void build(int l,int r,int s){
	if (l==r){
		f[s]=v[to[l]];
		return;
	}
	build(l,(l+r)/2,s+s);
	build((l+r)/2+1,r,s+s+1);
	f[s]=f[s+s]+f[s+s+1];
}
void down(int l,int r,int s){
	if (sig[s]){
		f[s]+=(r-l+1)*sig[s];
		if (l!=r)sig[s+s]+=sig[s],sig[s+s+1]+=sig[s];
		sig[s]=0;
	}
}
void ins(int l,int r,int s,int ll,int rr,int x){
	down(l,r,s);
	if (r<ll||rr<l)return;
	if (ll<=l&&r<=rr){
		sig[s]=x;
		down(l,r,s);
		return;
	}
	ins(l,(l+r)/2,s+s,ll,rr,x),ins((l+r)/2+1,r,s+s+1,ll,rr,x);
	f[s]=f[s+s]+f[s+s+1];
}
long long get(int l,int r,int s,int ll,int rr){
	down(l,r,s);
	if (r<ll||rr<l)return 0;
	if (ll<=l&&r<=rr)return f[s];
	return get(l,(l+r)/2,s+s,ll,rr)+get((l+r)/2+1,r,s+s+1,ll,rr);
}
int getlca(int x,int y){
	int i=19;
	if (deep[x]<deep[y])swap(x,y);
	while (deep[x]!=deep[y]){
		while (deep[fa[x][i]]<deep[y])i--;
		x=fa[x][i];
	}
	i=19;
	while (x!=y){
		while (fa[x][i]==fa[y][i]&&i)i--;
		x=fa[x][i],y=fa[y][i];
	}
	return x;
}
int up(int x,int y){
	int i=19;
	while (deep[x]!=y){
		while (deep[fa[x][i]]<y)i--;
		x=fa[x][i];
	}
	return x;
}
int main(){
	//freopen("e.in","r",stdin);
	scanf("%d %d",&n,&q);
	for (int i=1;i<=n;i++)scanf("%d",&v[i]);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d %d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	dfs(1);
	build(1,n,1);
	int root=1;
	while (q--){
		int sig,u,v,x;
		scanf("%d %d",&sig,&v);
		if (sig==1){
			root=v;
		}else
		if (sig==2){
			scanf("%d %d",&u,&x);
			int lca1=getlca(u,root);
			int lca2=getlca(v,root);
			int lca3=getlca(u,v);
			if (deep[lca1]<deep[lca2])swap(u,v),swap(lca1,lca2);
			if (lca1==root&&deep[lca3]<=deep[root])ins(1,n,1,1,n,x);
			else
			if (lca1==v||lca2==u||deep[lca3]<=deep[lca1])
			{
				int lca4=up(root,deep[lca1]+1);
				ins(1,n,1,1,n,x);
				ins(1,n,1,l[lca4],r[lca4],-x);
			}else
				ins(1,n,1,l[lca3],r[lca3],x);
		}else{
			int lca1=getlca(v,root);
			if (lca1==v&&root!=v){
				int lca2=up(root,deep[v]+1);
				printf("%I64d\n",get(1,n,1,1,n)-get(1,n,1,l[lca2],r[lca2]));
			}else
				printf("%I64d\n",get(1,n,1,l[v],r[v]));
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;//which means policy based DS

#define endl            "\n"
#define int             long long
#define ff              first
#define ss              second
#define fl(i,a,b)       for(int i=a; i<b; i++)
#define bfl(i,a,b)      for(int i=a-1; i>=b; i--)
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define vt(type)        vector<type>
#define omniphantom     ios_base::sync_with_stdio(false);   cin.tie(NULL); cout.tie(NULL);
#define mii             map<int,int>
#define pqb             priority_queue<int>
//Below is implementation of min heap
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#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 ini             const int
#define sz(v)           ((int)(v).size())
#define LEFT(n) (2*n)
#define RIGHT(n) (2*n+1)
#define ppii            pair<pii,pii>

const double pi = acos(-1.0);

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

void solve()
{
    string s;
    cin>>s;
    int n = s.length();
    map<int,int> my;
    int cnt = 0;
    fl(i,0,n){
        s[i]=='+'?cnt++:cnt--;
        if(cnt<0 && !my[cnt]) my[cnt]=(i+1);
    }
    int ans = 0, i = 0;
    while(true){
        if(my[-(i+1)]){
            ans+=my[-(i+1)];
            i++;
        }
        else{
            ans+=s.length();
            cout<<ans<<endl;
            return;
        }
    }
}

int32_t main()
{
	omniphantom
    w(t) solve();
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;//which means policy based DS

#define endl            "\n"
#define int             long long
#define ff              first
#define ss              second
#define fl(i,a,b)       for(int i=a; i<b; i++)
#define bfl(i,a,b)      for(int i=a-1; i>=b; i--)
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define vt(type)        vector<type>
#define omniphantom     ios_base::sync_with_stdio(false);   cin.tie(NULL); cout.tie(NULL);
#define mii             map<int,int>
#define pqb             priority_queue<int>
//Below is implementation of min heap
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#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 ini             const int
#define sz(v)           ((int)(v).size())
#define LEFT(n) (2*n)
#define RIGHT(n) (2*n+1)
#define ppii            pair<pii,pii>

const double pi = acos(-1.0);

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

void solve()
{
    string s;
    cin>>s;
    int n = s.length();
    int cnt = 0, ans = 0 ,res = 0;
    fl(i,0,n){
        if(cnt<0){
            if(s[i]=='-'){
                res++;
                ans+=res;
                cnt--;
            }
            else{
                if(s[i-1]=='-') cnt=0;
                res++;
                ans+=res;
            }
        }
        else{
            if(s[i]=='-') cnt--;
            else cnt++;
            res++;
            ans++;
        }
    }
    if(s.length()==count(s.begin(),s.end(),'-')) ans+=s.length();
    cout<<ans<<endl;
}

int32_t main()
{
	omniphantom
    w(t) solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)

	{
		cin>>s;
		ll sum=0;
		ll cnt=0;
		for(int i=0;i<s.size();++i)
		{
			if(s[i]=='+') sum++;
			else sum--;
			if(sum<0) cnt+=i+1,sum=0;
		}
		if(sum>=0) cnt+=s.size();
		cout<<cnt<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)

	{
		cin>>s;
		int sum=0;
		ll cnt=0;
		for(int i=0;i<s.size();++i)
		{
			if(s[i]=='+') sum++;
			else sum--;
			if(sum<0) cnt+=i+1,sum=0;
		}
		if(sum==0) cnt+=s.size();
		cout<<cnt<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>s;
		res=0;
		for(int i=0;i<inf;++i)
		{
			int  cur=i;
			bool ok=true;
			for(int j=0;j<s.size();++j)
			{
				res+=1;
				if(s[j]=='-') cur-=1;
				else cur+=1;
				if(cur<0)
				{
					ok=false;
					break;
				}
			}
			if(ok) break;
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)

	{
		cin>>s;
		ll sum=0;
		ll cnt=0;
		for(int i=0;i<s.size();++i)
		{
			if(s[i]=='+') sum++;
			else sum--;
			if(sum<0) cnt+=i+1,sum=0;
		}
		if(sum==0) cnt+=s.size();
		cout<<cnt<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)

	{
		cin>>s;
		int sum=0;
		ll cnt=0;
		for(int i=0;i<s.size();++i)
		{
			if(s[i]=='+') sum++;
			else sum--;
			if(sum<0) cnt+=i+1,sum=0;
		}
		if(sum<=0) cnt+=s.size();
		cout<<cnt<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>s;
		res=0;
		for(int i=0;i<s.size()+1;++i)
		{
			int  cur=i;
			bool ok=true;
			for(int j=0;j<s.size();++j)
			{
				res+=1;
				if(s[j]=='-') cur-=1;
				else cur+=1;
				if(cur<0)
				{
					ok=false;
					break;
				}
			}
			if(ok) break;
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>s;
		res=0;
		for(int i=0;i<inf;++i)
		{
			int  cur=i;
			bool ok=true;
			for(int j=0;j<inf;++j)
			{
				res+=1;
				if(s[j]=='-') cur-=1;
				else cur+=1;
				if(cur<0)
				{
					ok=false;
					break;
				}
			}
			if(ok) break;
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll res,t;
string s;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>s;
		int sum=0;
		for(int i=0;i<s.size();++i)
		{
			if(s[i]=='+') sum++;
			else sum--;
		}
		sum=abs(sum);
		res=0;
		for(int i=0;i<sum+1;++i)
		{
			int  cur=i;
			bool ok=true;
			for(int j=0;j<s.size();++j)
			{
				res+=1;
				if(s[j]=='-') cur-=1;
				else cur+=1;
				if(cur<0)
				{
					ok=false;
					break;
				}
			}
			if(ok) break;
		}
		cout<<res<<endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		int cur = 0, init = 0;
		vector<int> prefix(s.size() + 1);
		prefix[0] = 1;
		for(int i = 0; i < s.size(); i ++){
			if(s[i] == '+'){
				cur ++;
			}else{
				cur --;
			}
			if(cur + init < 0){
				prefix[0] ++;
				prefix[i + 1] --;
				init ++;
			}
		}
		long long res = 0;
		for(int i = 1; i < s.size(); i ++){
			prefix[i] += prefix[i - 1];
		}
		for(int i = 0; i < s.size(); i ++){
			res += prefix[i];
		}
		cout << res << endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		int number_of_minuses = 0;
		int number_of_pluses = 0;
		for(int i = 0; i < s.size(); i ++){
			if(s[i] == '-'){
				number_of_minuses ++;
			}else{
				number_of_pluses ++;
			}
		}
		if(number_of_pluses >= number_of_minuses){
			cout << s.size() << endl;
		}else{
			int initial_pluses = 0;
			int initial_minuses = 0;
			int first_plus = -1;
			if(s[0] == '-'){
				initial_minuses = 1;
				for(int i = 1; i < s.size(); i ++){
					if(s[i] == '-'){
						initial_minuses ++;
					}else if(s[i] == '+'){
						first_plus = i;
						break;
					}
				}
				if(first_plus == -1 && s.size() > 2){
					cout << initial_minuses * s.size() << endl;
				}else if(first_plus == - 1 && s.size() <= 2){
					cout << initial_minuses * s.size() + 1 << endl;
				}else{
					cout << (initial_minuses - 1) * s.size() + first_plus + 1 << endl;
				}
			}else{
				initial_pluses = 1;
				int i = 1;
				while(s[i] == '+' && i < s.size()){
					initial_pluses ++;
					i ++;
				}
				while(s[i] == '-' && i < s.size()){
					initial_minuses ++;
					i ++;
				}
				if(i < s.size()){
					first_plus = i;
				}
				if(first_plus == -1){
					cout << (initial_minuses - initial_pluses) * s.size() << endl;
				}else{
					cout << (initial_minuses - initial_pluses) * s.size() + first_plus + 1 << endl;
				}
			}
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		int number_of_minuses = 0;
		int number_of_pluses = 0;
		for(int i = 0; i < s.size(); i ++){
			if(s[i] == '-'){
				number_of_minuses ++;
			}else{
				number_of_pluses ++;
			}
		}
		if(number_of_pluses >= number_of_minuses){
			cout << s.size() << endl;
		}else{
			int initial_pluses = 0;
			int initial_minuses = 0;
			int first_plus = -1;
			if(s[0] == '-'){
				initial_minuses = 1;
				for(int i = 1; i < s.size(); i ++){
					if(s[i] == '-'){
						initial_minuses ++;
					}else if(s[i] == '+'){
						first_plus = i;
						break;
					}
				}
				if(first_plus == -1){
					cout << initial_minuses * s.size() + 1 << endl;
				}else{
					cout << (initial_minuses - 1) * s.size() + first_plus + 1 << endl;
				}
			}else{
				initial_pluses = 1;
				int i = 1;
				while(s[i] == '+' && i < s.size()){
					initial_pluses ++;
					i ++;
				}
				while(s[i] == '-' && i < s.size()){
					initial_minuses ++;
					i ++;
				}
				if(i < s.size()){
					first_plus = i;
				}
				if(first_plus == -1){
					cout << (initial_minuses - initial_pluses) * s.size() << endl;
				}else{
					cout << (initial_minuses - initial_pluses) * s.size() + first_plus + 1 << endl;
				}
			}
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int pnm(string s){
	int res = 0;
	int init = 0;
	while(true){
		int cur = init;
		bool ok = true;
		for(int i = 0; i < s.size(); i ++){
			res += 1;
			if(s[i] == '+')
				cur = cur + 1;
			else
				cur = cur - 1;
			if(cur < -1){
				ok = false;
				break;
			}
		}
		if(ok){
			break;
		}
		init ++;
	}
	return res + 1;
}
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		if(s[0] == '-'){
			cout << pnm(s) << endl;
		}else{
			cout << pnm(s) - 1 << endl;
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		int number_of_minuses = 0;
		int number_of_pluses = 0;
		for(int i = 0; i < s.size(); i ++){
			if(s[i] == '-'){
				number_of_minuses ++;
			}else{
				number_of_pluses ++;
			}
		}
		if(number_of_pluses >= number_of_minuses){
			cout << s.size() << endl;
		}else{
			int initial_pluses = 0;
			int initial_minuses = 0;
			int first_plus = -1;
			if(s[0] == '-'){
				initial_minuses = 1;
				for(int i = 1; i < s.size(); i ++){
					if(s[i] == '-'){
						initial_minuses ++;
					}else if(s[i] == '+'){
						first_plus = i;
						break;
					}
				}
				if(first_plus == -1 && first_plus > -1){
					cout << initial_minuses * s.size() << endl;
				}else if(first_plus == -1 && first_pluses == -1){
					cout << s.size() + 1 << endl;
				}else{
					cout << (initial_minuses - 1) * s.size() + first_plus + 1 << endl;
				}
			}else{
				initial_pluses = 1;
				int i = 1;
				while(s[i] == '+' && i < s.size()){
					initial_pluses ++;
					i ++;
				}
				while(s[i] == '-' && i < s.size()){
					initial_minuses ++;
					i ++;
				}
				if(i < s.size()){
					first_plus = i;
				}
				if(first_plus == -1){
					cout << (initial_minuses - initial_pluses) * s.size() << endl;
				}else{
					cout << (initial_minuses - initial_pluses) * s.size() + first_plus + 1 << endl;
				}
			}
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		int number_of_minuses = 0;
		int number_of_pluses = 0;
		for(int i = 0; i < s.size(); i ++){
			if(s[i] == '-'){
				number_of_minuses ++;
			}else{
				number_of_pluses ++;
			}
		}
		if(number_of_pluses >= number_of_minuses){
			cout << s.size() << endl;
		}else{
			int initial_pluses = 0;
			int initial_minuses = 0;
			int first_plus = -1;
			if(s[0] == '-'){
				initial_minuses = 1;
				for(int i = 1; i < s.size(); i ++){
					if(s[i] == '-'){
						initial_minuses ++;
					}else{
						first_plus = i;
						break;
					}
				}
				if(first_plus == -1){
					cout << initial_minuses * s.size() << endl;
				}else{
					cout << (initial_minuses - 1) * s.size() + first_plus + 1 << endl;
				}
			}else{
				initial_pluses = 1;
				int i = 1;
				while(s[i] == '+' && i < s.size()){
					initial_pluses ++;
					i ++;
				}
				while(s[i] == '-' && i < s.size()){
					initial_minuses ++;
					i ++;
				}
				if(i < s.size()){
					first_plus = i;
				}
				if(first_plus == -1){
					cout << (initial_minuses - initial_pluses) * s.size() << endl;
				}else{
					cout << (initial_minuses - initial_pluses) * s.size() + first_plus + 1 << endl;
				}
			}
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		int number_of_minuses = 0;
		int number_of_pluses = 0;
		for(int i = 0; i < s.size(); i ++){
			if(s[i] == '-'){
				number_of_minuses ++;
			}else{
				number_of_pluses ++;
			}
		}
		if(number_of_pluses >= number_of_minuses){
			cout << s.size() << endl;
		}else{
			int initial_pluses = 0;
			int initial_minuses = 0;
			int first_plus = -1;
			if(s[0] == '-'){
				initial_minuses = 1;
				for(int i = 1; i < s.size(); i ++){
					if(s[i] == '-'){
						initial_minuses ++;
					}else if(s[i] == '+'){
						first_plus = i;
						break;
					}
				}
				if(first_plus == -1 && s.size() > 1){
					cout << initial_minuses * s.size() << endl;
				}else if(first_plus == -1 && s.size() == 1){
					cout << "2" << endl;
				}else{
					cout << (initial_minuses - 1) * s.size() + first_plus + 1 << endl;
				}
			}else{
				initial_pluses = 1;
				int i = 1;
				while(s[i] == '+' && i < s.size()){
					initial_pluses ++;
					i ++;
				}
				while(s[i] == '-' && i < s.size()){
					initial_minuses ++;
					i ++;
				}
				if(i < s.size()){
					first_plus = i;
				}
				if(first_plus == -1){
					cout << (initial_minuses - initial_pluses) * s.size() << endl;
				}else{
					cout << (initial_minuses - initial_pluses) * s.size() + first_plus + 1 << endl;
				}
			}
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int pnm(string s){
	int res = 0;
	int init = 0;
	while(true){
		int cur = init;
		bool ok = true;
		for(int i = 0; i < s.size(); i ++){
			res += 1;
			if(s[i] == '+')
				cur = cur + 1;
			else
				cur = cur - 1;
			if(cur < 0){
				ok = false;
				break;
			}
		}
		if(ok){
			break;
		}
		init ++;
	}
	return res + 1;
}
int main(){
	int t;
	cin >> t;
	while(t --){
		string s;
		cin >> s;
		/*
		if(s[0] == '-'){
			cout << pnm(s) << endl;
		}else{
			cout << pnm(s) - 1 << endl;
		}
		*/
		cout << pnm(s) - 1 << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,min1=INT_MAX,a;
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			//if(s[i]=='-') a=i+1;
			if(min1>=sum){ min1=sum; a=i;}
			//min1=min(min1,sum);
		}
		if(min1>=0) cout<<s.length()<<"\n";
		else if((int)s.length()==abs(sum)) cout<<sum*sum<<"\n";
		else{
			ll su=0;
			for(ll i=0;i<abs(sum);i++){
				su+=i+1;
			}
			su+=a+1;
			cout<<su<<"\n";
		}
		/*ll res=0;
		bool ok=true;
		for(ll j=0;j<inf;j++){
			ll cur=j;
			ok=true;
			for(ll i=0;i<s.length();i++){
				res++;
				if(s[i]=='+') cur++;
				else cur--;
				if(cur<0){ ok=false; break;}
			}
			if(ok) break;
		}
		cout<<res<<"\n";*/
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,min1=INT_MAX,a;
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			//if(s[i]=='-') a=i+1;
			if(min1>=sum){ min1=sum; a=i;}
			//min1=min(min1,sum);
		}
		if(min1>=0) cout<<s.length()<<"\n";
		else if((int)s.length()==abs(sum)) cout<<sum*sum<<"\n";
		else{
			ll su=0;
			for(ll i=0;i<abs(sum);i++){
				su+=i+1;
			}
			su+=a;
			cout<<su<<"\n";
		}
		/*ll res=0;
		bool ok=true;
		for(ll j=0;j<inf;j++){
			ll cur=j;
			ok=true;
			for(ll i=0;i<s.length();i++){
				res++;
				if(s[i]=='+') cur++;
				else cur--;
				if(cur<0){ ok=false; break;}
			}
			if(ok) break;
		}
		cout<<res<<"\n";*/
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,min1=INT_MAX,a;
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			if(s[i]=='-') a=i+1;
			min1=min(min1,sum);
		}
		if(min1>=0) cout<<s.length()<<"\n";
		else if((int)s.length()==abs(sum)) cout<<sum*sum<<"\n";
		else{
			ll su=0;
			for(ll i=0;i<abs(sum);i++){
				su+=i+1;
			}
			su+=a;
			cout<<su<<"\n";
		}
		/*ll res=0;
		bool ok=true;
		for(ll j=0;j<inf;j++){
			ll cur=j;
			ok=true;
			for(ll i=0;i<s.length();i++){
				res++;
				if(s[i]=='+') cur++;
				else cur--;
				if(cur<0){ ok=false; break;}
			}
			if(ok) break;
		}
		cout<<res<<"\n";*/
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,min1=INT_MAX,a;
		ll d[s.length()];
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			d[i]=sum;
			if(min1>=sum){ min1=sum; a=i;}
		}
		if(min1>=0) cout<<s.length()<<"\n";
		else if((int)s.length()==abs(sum)) cout<<sum*sum<<"\n";
		else{
			ll su=0,z=0;
			for(ll i=0;i<(int)s.length();i++){
				if(d[i]<=z) su+=i+1;
				if(sum==d[i]) break;
				//else if(sum==d[i]) break;
				z=d[i];
			}
			su+=s.length();
			cout<<su<<"\n";
		}
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,min1=INT_MAX;
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			min1=min(min1,sum);
		}
		if(min1>=0) cout<<s.length()<<"\n";
		else if((int)s.length()==abs(sum)) cout<<sum*sum<<"\n";
		else cout<<(s.length()*(-1)*sum)-1<<"\n";
		/*ll res=0;
		bool ok=true;
		for(ll j=0;j<inf;j++){
			ll cur=j;
			ok=true;
			for(ll i=0;i<s.length();i++){
				res++;
				if(s[i]=='+') cur++;
				else cur--;
				if(cur<0){ ok=false; break;}
			}
			if(ok) break;
		}
		cout<<res<<"\n";*/
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,z=0,ans=0;
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			d[i]=sum;
			if(z>sum){ z=sum; ans+=i+1;}
		}
		ans+=s.length();
		cout<<ans<<"\n";
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll res=0;
		bool ok=true;
		for(ll j=0;j<inf;j++){
			ll cur=j;
			ok=true;
			for(ll i=0;i<s.length();i++){
				res++;
				if(s[i]=='+') cur++;
				else cur--;
				if(cur<0){ ok=false; break;}
			}
			if(ok) break;
		}
		cout<<res<<"\n";
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,min1=INT_MAX,a;
		ll d[s.length()];
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			d[i]=sum;
			if(min1>=sum){ min1=sum; a=i;}
		}
		if(min1>=0) cout<<s.length()<<"\n";
		else if((int)s.length()==abs(sum)) cout<<sum*sum<<"\n";
		else{
			ll su=0,z=0;
			for(ll i=0;i<(int)s.length();i++){
				if(d[i]<z) su+=i+1;
				if(sum==d[i]) break;
				//else if(sum==d[i]) break;
				z=d[i];
			}
			su+=s.length();
			cout<<su<<"\n";
		}
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,min1=INT_MAX,a;
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			if(s[i]=='-') a=i+1;
			min1=min(min1,sum);
		}
		if(min1>=0) cout<<s.length()<<"\n";
		else if((int)s.length()==abs(sum)) cout<<sum*sum<<"\n";
		else{
			ll su=0;
			for(ll i=0;i<abs(sum);i++){
				su+=i+1;
			}
			su+=s.length();
			cout<<su<<"\n";
		}
		/*ll res=0;
		bool ok=true;
		for(ll j=0;j<inf;j++){
			ll cur=j;
			ok=true;
			for(ll i=0;i<s.length();i++){
				res++;
				if(s[i]=='+') cur++;
				else cur--;
				if(cur<0){ ok=false; break;}
			}
			if(ok) break;
		}
		cout<<res<<"\n";*/
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
const ll M=1e9+7;
const ll inf=1e18+5;
using namespace std;
void fast(){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);}
//b.to_ulong()
void solve(){
	ll t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll sum=0,z=0,ans=0;
		for(ll i=0;i<(int)s.length();i++){
			if(s[i]=='+') sum++;
			else sum--;
			if(z>sum){ z=sum; ans+=i+1;}
		}
		ans+=s.length();
		cout<<ans<<"\n";
	}
 }
int main(){
	fast();
    solve();
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
int N = 1e6 + 10;

int main(){
    int t;
    cin >> t;
    char s[N];
    while (t --){
        scanf("%s", s + 1);
        int n = strlen(s + 1);
        ll ans = 0, sum = 0, minx = 0;
        for (int i = 1; i <= n; ++ i){
            if (s[i] == '+') ++ sum;
            else -- sum;
            if (sum < minx) {
                minx = sum;
                ans += i;//本次break之前序列执行了i步
            }
        }
        ans += n;//最后一次不会break，序列全部执行
        cout << ans << '\n';
    }
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int k = 0; k < n; k++){
        int ans = 0;
        string s;
        cin >> s;
        int maxv = 0;
        int val = 0;
        for(int z = 0; z < s.size(); z++){
            if(s[z] == '+'){
                val -= 1;
                maxv = val > maxv ? val : maxv;
            }else{
                val += 1;
                maxv = val > maxv ? val : maxv;
            }
        }
        if(maxv <= 0){
            cout << s.size() << endl;
            continue;
        }
        for(int i = 0; i <= maxv; i++){
            bool ok = true;
            int curr = i;
            for(int ii = 0; ii < s.size(); ii++){
                ans += 1;
                if(s[ii] == '+'){
                    curr += 1;
                }else{
                    curr -= 1;
                }
                if(curr < 0){
                    ok = false;
                    break;
                }
            }
            if(ok){
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int k = 0; k < n; k++){
        int ans = 0;
        string s;
        cin >> s;
        int maxv = 0;
        int val = 0;

        for(int z = 0; z < s.size(); z++){
            if(s[z] == '+'){
                val -= 1;
                maxv = val > maxv ? val : maxv;
            }else{
                val += 1;
                maxv = val > maxv ? val : maxv;
            }
        }

        if(maxv == 0){
            cout << s.size() << endl;
            continue;
        }
        val = 0;                                                                                for(int z = 0; z < s.size(); z++){
        if(s[z] == '+'){
            val -= 1;
            }else{
                val += 1;
            }
            if(val == maxv){
                ans += s.size() - z + 1;
                break;
            }else{
                ans += maxv - z + 1;
            }
        }

        cout << ans << endl;
        return 0;

    }
}


#include <iostream>
#include <string>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int k = 0; k < n; k++){
        int ans = 0;
        string s;
        cin >> s;
        int maxv = 0;
        int val = 0;
        for(int z = 0; z < s.size(); z++){
            if(s[z] == '+'){
                val += 1;
            }else{
                val -= 1;
            }
            if(val < maxv){
                maxv = val;
                ans += z + 1;
            }
        }
        ans += s.size();
        cout << ans << endl;
        return 0;
    }
}


#include <iostream>
#include <string>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int k = 0; k < n; k++){
        int ans = 0;
        string s;
        cin >> s;
        for(int i = 0; i < 100000000; i++){
            bool ok = true;
            int curr = i;
            for(int ii = 0; ii < s.size(); ii++){
                ans += 1;
                if(s[ii] == '+'){
                    curr += 1;
                }else{
                    curr -= 1;
                }
                if(curr < 0){
                    ok = false;
                    break;
                }
            }
            if(ok){
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int k = 0; k < n; k++){
        int ans = 0;
        string s;
        cin >> s;
        int maxv = 0;
        int val = 0;
        for(int z = 0; z < s.size(); z++){
            if(s[z] == '+'){
                val += 1;
            }else{
                val -= 1;
            }
            if(val < maxv){
                maxv = val;
                ans += z + 1;
            }
        }
        ans += s.size();
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int k = 0; k < n; k++){
        int ans = 0;
        string s;
        cin >> s;
        for(int i = 0; i < 100000000; i++){
            bool ok = true;
            int curr = i;
            for(int ii = 0; ii < s.size(); ii++){
                ans += 1;
                if(s[i] == '+'){
                    curr += 1;
                }else{
                    curr -= 1;
                }
                if(curr < 0){
                    ok = false;
                    break;
                }
            }
            if(ok){
                break;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define pi acos(-1.0)
#define bsort(v) sort(v.begin(),v.end())
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007
#define N 10000001
#define F first
#define S second
#define MAX 500050
#define ALL(v) v.begin(),v.end()

using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

int main()
{
    FAST
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int n=s.size();

        int cnt=0;
        ull res=0;
        ll prev=2;
        for(int i=0; i<n; i++)
        {
            if(s[i]=='+' &&cnt<=0)
            {
                res++;
                cnt=1;
            }
            else if(s[i]=='+')
            {
                res++;
                cnt++;
            }
            else
            {
                cnt--;
                if(cnt>=0)
                    res++;
                else
                {
                    res+=prev;
                    prev++;
                }
            }
            //cout<<res<<endl;
        }
        cout<<res<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int sz=s.size();
        unsigned long long res=0;
        //sort(s.begin(),s.end());
        int plu=0,mi=0;
        for(int i=0;i<sz;i++)
        {
            if(s[i]=='+')
                plu++;
            else
                mi++;
        }
        if(plu>=mi)
        {
            cout<<sz<<endl;
        }
        else{
        for(int j=0;j<1000;j++)
        {
            int cur=j;
            int ok=1;
            for(int i=0;i<sz;i++)
            {

                res++;
                if(s[i]=='+')
                    cur++;
                else
                    cur--;
                if(cur<0)
                {
                    ok=0;
                    break;
                }
            }
            if(ok)
                break;
                //cout<<res<<endl;

        }
        cout<<res<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int sz=s.size();
        unsigned long long res=0;
        //sort(s.begin(),s.end());
        for(int j=0;j<1000;j++)
        {
            int cur=j;
            int ok=1;
            for(int i=0;i<sz;i++)
            {

                res++;
                if(s[i]=='+')
                    cur++;
                else
                    cur--;
                if(cur<0)
                {
                    ok=0;
                    break;
                }
            }
            if(ok)
                break;
                //cout<<res<<endl;

        }
        cout<<res<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int tc;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int sz=s.size();
        unsigned long long res=0;
        for(int j=0;j<10000;j++)
        {
            int cur=j;
            int ok=1;
            for(int i=0;i<sz;i++)
            {

                res++;
                if(s[i]=='+')
                    cur++;
                else
                    cur--;
                if(cur<0)
                {
                    ok=0;
                    break;
                }
            }
            if(ok)
                break;
                //cout<<res<<endl;

        }
        cout<<res<<endl;
    }
}


#include<bits/stdc++.h>
#define pi acos(-1.0)
#define bsort(v) sort(v.begin(),v.end())
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007
#define N 10000001
#define F first
#define S second
#define MAX 500050
#define ALL(v) v.begin(),v.end()

using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

int main()
{
    FAST
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int n=s.size();
        if(s[0]=='+')
            cout<<n<<endl;
        else
        {
            int cnt=0;
            for(int i=0;i<n;i++)
            {
                if(s[i]=='+')
                    break;
                else
                    cnt++;
            }
            ull res=0;
            ll prev=2;
            for(int i=1;i<=cnt;i++)
            {
                res+=prev;
                prev++;
            }
            res+=(n-cnt);
            cout<<res<<endl;
        }

    }
    return 0;
}


#include<bits/stdc++.h>
#define pi acos(-1.0)
#define bsort(v) sort(v.begin(),v.end())
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007
#define N 10000001
#define F first
#define S second
#define MAX 500050
#define ALL(v) v.begin(),v.end()

using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

int main()
{
    FAST
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int n=s.size();
        ll res=0;
       // sort(ALL(s));
        for(int j=0;j<999;j++)
        {
            int cur=j;
            int ok=1;
            for(int i=0;i<n;i++)
            {
                res++;
                if(s[i]=='+')
                    cur++;
                else
                    cur--;
                if(cur<0)
                {
                    ok=0;
                    break;
                }
            }
            if(ok)
                break;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include<bits/stdc++.h>
#define pi acos(-1.0)
#define bsort(v) sort(v.begin(),v.end())
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007
#define N 10000001
#define F first
#define S second
#define MAX 500050
#define ALL(v) v.begin(),v.end()

using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

int main()
{
    FAST
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int n=s.size();

        int cnt=0;
        ull res=0;
        ll prev=2;
        for(int i=0; i<n; i++)
        {
            if(s[i]=='+' &&cnt<=0)
            {
                res++;
                cnt=1;
            }
            else if(s[i]=='+')
            {
                res++;
                cnt++;
            }
            else
            {
                cnt--;
                if(cnt>=0)
                    res++;
                else
                {
                    res+=prev;
                }
            }
            prev++;
            //cout<<res<<endl;
        }
        cout<<res<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define pi acos(-1.0)
#define bsort(v) sort(v.begin(),v.end())
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007
#define N 10000001
#define F first
#define S second
#define MAX 500050
#define ALL(v) v.begin(),v.end()

using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

int main()
{
    FAST
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int n=s.size();

        int cnt=0;
        for(int i=0; i<n; i++)
        {
            if(s[i]=='+')
                break;
            else
                cnt++;
        }
        ull res=0;
        ll prev=2;
        for(int i=1; i<=cnt; i++)
        {
            res+=prev;
            prev++;
        }
        res+=(n-cnt);
        cout<<res<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define pi acos(-1.0)
#define bsort(v) sort(v.begin(),v.end())
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MOD 1000000007
#define N 10000001
#define F first
#define S second
#define MAX 500050
#define ALL(v) v.begin(),v.end()

using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}

int main()
{
    FAST
    int tc=1;
    cin>>tc;
    while(tc--)
    {
        string s;
        cin>>s;
        int n=s.size();
        ull res=0;
       // sort(ALL(s));
        for(int j=0;j<999;j++)
        {
            int cur=j;
            int ok=1;
            for(int i=0;i<n;i++)
            {
                res++;
                if(s[i]=='+')
                    cur++;
                else
                    cur--;
                if(cur<0)
                {
                    ok=0;
                    break;
                }
            }
            if(ok)
                break;
        }
        cout<<res<<endl;

    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        string s1 = " " + s;
        ll ans = 0;
        for(ll i = 0; i <= INT_MAX; i++)
        {
            ll flag = 0;
            ll sum = 0;
            for(ll j = 1; j < s1.size(); j++)
            {
                if(s1[j] == '-')
                {
                    sum -= 1;
                }
                else
                {
                    sum += 1;
                }

                if(i + sum < 0)
                {
                    ans += j;
                    flag = 1;
                    break;
                }
            }
            if(!flag)
            {
                ans += (s1.size() - 1);
                break;
            }
        }
        cout << ans << endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        string s1 = " " + s;
        vector< ll > a(s1.size());
        ll mx = INT_MIN;
        ll prev = 0;
        for(ll i = 1; i <= s1.size(); i++)
        {
            ll num;
            if(s1[i] == '+')
            {
                num = 1;
            }
            else
            {
                num = -1;
            }
            a[i] = prev + num;
            prev = a[i];
            mx = max(mx, abs(prev));
        }

        //cout << s1.size() << endl;

        ll ans = 0;
        for(ll i = 0; i <= mx; i++)
        {
            ll flag = 0;
            for(ll j = 1; j < s1.size(); j++)
            {
                if(i + a[j] < 0)
                {
                    ans += j;
                    flag = 1;
                    break;
                }
            }
            if(!flag)
            {
                ans += (s1.size() - 1);
                break;
            }
        }
        cout << ans << endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        string s1 = " " + s;
        vector< ll > a(s1.size() + 1);
        ll prev = 0;
        for(ll i = 1; i <= s1.size(); i++)
        {
            ll num;
            if(s1[i] == '+')
            {
                num = 1;
            }
            else
            {
                num = -1;
            }
            a[i] = prev + num;
            prev = a[i];
        }

        //cout << s1.size() << endl;

        ll ans = 0;
        ll flag = 0;
        for(ll j = 0; j < s1.size() - 1; j++)
        {
            if(j + a[j + 1] < 0)
            {
                ans += (j + 1);
                flag = 1;
            }
            //cout << "ans is: " << ans << endl;
        }

        ans += (s1.size() - 1);

        cout << ans << endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        ll ans = 0;
        ll temp = 0;
        for(ll i = 0; i < s.size(); i++)
        {
            if(s[i] == '+'){temp++;}
            else{temp--;}

            if(temp < 0)
            {
                ans += (i + 1);
                temp = 0;
            }
        }
        cout << ans + s.size()<< endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        string s1 = " " + s;
        ll ans = 0;
        for(ll i = 0; i <= s1.size(); i++)
        {
            ll flag = 0;
            ll sum = 0;
            for(ll j = 1; j < s1.size(); j++)
            {
                if(s1[j] == '-')
                {
                    sum -= 1;
                }
                else
                {
                    sum += 1;
                }

                if(i + sum < 0)
                {
                    ans += j;
                    flag = 1;
                    break;
                }
            }
            if(!flag)
            {
                ans += (s1.size() - 1);
                break;
            }
        }
        cout << ans << endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        string s1 = " " + s;
        vector< ll > a(s1.size() + 1);
        ll mx = INT_MIN;
        ll prev = 0;
        for(ll i = 1; i <= s1.size(); i++)
        {
            ll num;
            if(s1[i] == '+')
            {
                num = 1;
            }
            else
            {
                num = -1;
            }
            a[i] = prev + num;
            prev = a[i];
            mx = max(mx, abs(prev));
        }

        //cout << s1.size() << endl;

        ll ans = 0;
        for(ll i = 0; i <= mx; i++)
        {
            ll flag = 0;
            for(ll j = 1; j < s1.size(); j++)
            {
                if(i + a[j] < 0)
                {
                    ans += j;
                    flag = 1;
                    break;
                }
            }
            if(!flag)
            {
                ans += (s1.size() - 1);
                break;
            }
        }
        cout << ans << endl;

    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        string s1 = " " + s;
        vector< ll > a(s1.size() + 1);
        ll prev = 0;
        for(ll i = 1; i <= s1.size(); i++)
        {
            ll num;
            if(s1[i] == '+')
            {
                num = 1;
            }
            else
            {
                num = -1;
            }
            a[i] = prev + num;
            prev = a[i];
        }

        //cout << s1.size() << endl;

        ll ans = 0;
        ll flag = 0;
        for(ll j = 1; j < s1.size(); j++)
        {
            if(j - 1 + a[j] < 0)
            {
                ans += j;
            }
            else
            {
                break;
            }
            //cout << "ans is: " << ans << endl;
        }

        ans += (s1.size() - 1);

        cout << ans << endl;

    }
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll t;
    cin >> t;
    rep(i, t) {
        string s;
        cin >> s;
        s.push_back(' ');
        ll minus = 0LL, plus = 0LL;
        ll ans = 0LL;
        ll start = -1LL;
        rep(j, s.size()) {
            if(s[j] == '-') {
                if(start == -1LL) start = j;
                minus++;
            } else {
                if(minus > 0LL || s[j] == ' ') {
                    ll temp = max(0LL, minus - plus);
                    start += temp;
                    ans += temp * start + plus + minus + temp * (temp + 1LL) / 2LL;
                    plus = max(0LL, plus - minus);
                    minus = 0LL;
                    start = -1LL;
                }
                if(s[j] == '+') plus++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll t;
    cin >> t;
    rep(i, t) {
        string s;
        cin >> s;
        s.push_back(' ');
        ll minus = 0LL, plus = 0LL;
        ll ans = 0LL;
        ll start = 0LL;
        rep(j, s.size()) {
            if(s[j] == '-') {
                minus++;
            } else {
                if(minus > 0LL || s[j] == ' ') {
                    ll temp = max(0LL, minus - plus);
                    ans += temp * start + plus + minus + temp * (temp + 1LL) / 2LL;
                    plus = max(0LL, plus - minus);
                    minus = 0LL;
                    start = j + 1LL;
                }
                if(s[j] == '+') plus++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll t;
    cin >> t;
    rep(i, t) {
        string s;
        cin >> s;
        s.push_back(' ');
        ll minus = 0LL, plus = 0LL;
        ll ans = 0LL;
        ll start = -1LL;
        rep(j, s.size()) {
            if(s[j] == '-') {
                if(start == -1LL) start = j;
                minus++;
            } else {
                if(minus > 0LL || s[j] == ' ') {
                    ll temp = max(0LL, minus - plus);
                    start += minus - temp;
                    ans += temp * start + plus + minus + temp * (temp + 1LL) / 2LL;
                    plus = max(0LL, plus - minus);
                    minus = 0LL;
                    start = -1LL;
                }
                if(s[j] == '+') plus++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll t;
    cin >> t;
    rep(i, t) {
        string s;
        cin >> s;
        ll cur = 0, mn = 0, res = s.size();
        rep(i, s.size()) {
            if(s[i] == '+') cur++;
            else cur--;
            if(cur < mn) {
                mn = cur;
                res += i + 1;
            }
        }
        cout << res << endl;
    }
    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll t;
    cin >> t;
    rep(i, t) {
        string s;
        cin >> s;
        s.push_back(' ');
        ll minus = 0LL, plus = 0LL;
        ll ans = 0LL;
        iter(c, s) {
            if(c == '-') {
                minus++;
            } else {
                if(minus > 0LL) {
                    ll temp = max(0LL, minus - plus);
                    ans += plus + minus + temp * (temp + 1LL) / 2LL;
                    plus = max(0LL, plus - minus);
                    minus = 0LL;
                }
                if(c == '+') plus++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll t;
    cin >> t;
    rep(i, t) {
        string s;
        cin >> s;
        s.push_back(' ');
        ll minus = 0LL, plus = 0LL;
        ll ans = 0LL;
        iter(c, s) {
            if(c == '-') {
                minus++;
            } else {
                if(minus > 0LL || c == ' ') {
                    ll temp = max(0LL, minus - plus);
                    ans += plus + minus + temp * (temp + 1LL) / 2LL;
                    plus = max(0LL, plus - minus);
                    minus = 0LL;
                }
                if(c == '+') plus++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll t;
    cin >> t;
    rep(i, t) {
        string s;
        cin >> s;
        s.push_back(' ');
        ll minus = 0LL, plus = 0LL;
        ll ans = 0LL;
        ll start = -1LL;
        rep(j, s.size()) {
            if(s[j] == '-') {
                if(start == -1LL) start = j;
                minus++;
            } else {
                if(minus > 0LL || s[j] == ' ') {
                    ll temp = max(0LL, minus - plus);
                    ans += temp * start + plus + minus + temp * (temp + 1LL) / 2LL;
                    if(temp > 0) ans += start;
                    plus = max(0LL, plus - minus);
                    minus = 0LL;
                    start = -1LL;
                }
                if(s[j] == '+') plus++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{

 ll cur = i;
 bool ok = true;
 for(ll j=0;j<s.size();j++)
 {
     res++;
     if(s[j]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
         break;
     }

 }

 if(ok==true)
 {
     break;
 }
i++;
if(i>=2*s.size()){break;}

}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{

 ll cur = i;
 bool ok = true;
 for(ll j=0;j<s.size();j++)
 {
     res++;
     if(s[j]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
         break;
     }

 }

 if(ok==true)
 {
     break;
 }
i++;
if(i==11){break;}
}
cout<<res<<endl;

}
return 0;
}


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

int pref[1010101];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);

int t;
cin>>t;
while(t!=0)
{
    t--;
string s;
cin>>s;
memset(pref,0,sizeof(pref));

for(int i=0;i<s.size();i++)
{
if(s[i]=='+'){pref[i]=1;}
else{pref[i]=-1;}

}
ll ans=0;
ll aux=0;
for (int i=0;i<s.size();i++)
{

    if(i==s.size()-1  and pref[i]<0 and aux==0){ans+=2*(i+1);}
    else if(i==s.size()-1 and (pref[i]<0 and aux>0 or pref[i]>=0)){ans+=i+1;}

 else if(pref[i]>0){aux+=pref[i];}
 else if(pref[i]<0 and aux>0){aux--; if (aux<0){aux=0;}}
 else if(pref[i]<0 and aux==0){ans+=i+1;}
}
cout<<ans<<endl;
}

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res=0;
int pull=0;
for(int i=0;i<s.size();i++)
{
    if(s[i]=='-' and pull<=0){res+=i+1; pull--;}
     if(s[i]=='-' and pull>0){res++; pull--;}
    if(s[i]=='+' and pull>=0){res++; pull++;}
        if(s[i]=='+' and pull<0){res+=i+1; pull=1;}
        if(i==s.size()-1 and pull<0){res+=i+1;}
}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{
 ll cur = i;
 bool ok = true;
 for(int j=0;j<s.size();j++)
 {
     res++;
     if(s[i]=='+'){cur++;}
     else{cur--;}
     if(cur<0)
     {
         ok=false;
        i++; break;
     }

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

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{
 ll cur = i;
 bool ok = true;
 for(int j=0;j<s.size();j++)
 {
     res++;
     if(s[i]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
        i=i+1; break;
     }

 }

 if(ok)
 {
     break;
 }

}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{
 ll cur = i;
 bool ok = true;
 for(int j=0;j<s.size();j++)
 {
     res++;
     if(s[i]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
        i++; break;
     }

 }

 if(ok==true)
 {
     break;
 }
 i++;
}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{
 ll cur = i;
 bool ok = true;
 for(int j=0;j<s.size();j++)
 {
     res++;
     if(s[j]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
        i++; break;
     }

 }

 if(ok==true)
 {
     break;
 }

}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{

 ll cur = i;
 bool ok = true;
 for(ll j=0;j<s.size();j++)
 {
     res++;
     if(s[j]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
         break;
     }

 }

 if(ok==true)
 {
     break;
 }
i++;
if(i==8100){break;}
}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{

 ll cur = i;
 bool ok = true;
 for(int j=0;j<s.size();j++)
 {
     res++;
     if(s[j]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
         break;
     }

 }

 if(ok==true)
 {
     break;
 }
i++;
}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{
 ll cur = i;
 bool ok = true;
 for(int j=0;j<s.size();j++)
 {
     res=res+1;
     if(s[i]=='+'){cur=cur+1;}
     else{cur=cur-1;
     if(cur<0)
     {
         ok=false;
        i=i+1; break;
     }
     }

 }

 if(ok)
 {
     break;
 }

}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{

 ll cur = i;
 bool ok = true;
 for(ll j=0;j<s.size();j++)
 {
     res++;
     if(s[j]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
         break;
     }

 }

 if(ok==true)
 {
     break;
 }
i++;
if(i==1100){break;}
}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string d;
cin>>d;
string s;
s.push_back('0');
for(int j=0;j<d.size();j++)
{
    s.push_back(d[j]);
}
ll res = 0;
ll i=0;
while(true)
{

 ll cur = i;
 bool ok = true;
 for(ll j=1;j<s.size();j++)
 {
     res++;
     if(s[j]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
         break;
     }

 }

 if(ok)
 {
     break;
 }
i++;

}
cout<<res<<endl;

}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main ()
{
int t;
cin>>t;
while(t!=0)
{
t--;
string s;
cin>>s;
ll res = 0;
ll i=0;
while(true)
{
 ll cur = i;
 bool ok = true;
 for(int j=0;j<s.size();j++)
 {
     res++;
     if(s[i]=='+'){cur=cur+1;}
     else{cur=cur-1;}
     if(cur<0)
     {
         ok=false;
        i++; break;
     }

 }

 if(ok==true)
 {
     break;
 }

}
cout<<res<<endl;

}
return 0;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    string s;
    long long t;
    cin >> t;
    for (long long q = 0; q < t; q++) {
        cin >> s;
        long long res = 0, init = 0, cur, si, plus = 0, minus = 0;
        bool ok = true;
        si = s.size();
        for (long long i = 0; i < si; i++) {
            if (s[i] == '+') {
                plus++;
            }
            else {
                minus++;
            }
        }
        while (true) {
            //if (init >= minus) {
                //res += si;
                //break;
            //}
            //else {
                cur = init;
                ok = true;
                long long  minus2 = minus; // plus2 = plus,
                for (long long i = 0; i < s.size(); i++) {
                    if (cur >= minus2) {
                        res += si - i;
                        break;
                    }
                    res++;
                    if (s[i] == '+') {
                        cur++;
                        //plus2--;
                    }
                    else {
                        cur--;
                        minus2--;
                    }
                    if (cur < 0) {
                        ok = false;
                        break;
                    }
                }
                if (ok) {
                    break;
                }
                init++;
            //}
        }
        cout << res << endl;
    }
    return 0;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    string s;
    long long t;
    cin >> t;
    for (long long q = 0; q < t; q++) {
        cin >> s;
        long long res = 0, init = 0, cur;
        bool ok = true;
        while (true) {
            cur = init;
            ok = true;
            for (long long i = 0; i < s.size(); i++) {
                res++;
                if (s[i] == '+') {
                    cur++;
                }
                else {
                    cur--;
                }
                if (cur < 0) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                break;
            }
            init++;
        }
        cout << res << endl;
    }
    return 0;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    string s;
    long long t;
    cin >> t;
    for (long long q = 0; q < t; q++) {
        cin >> s;
        long long res = 0, init = 0, cur, si, plus = 0, minus = 0;
        bool ok = true;
        si = s.size();
        for (long long i = 0; i < si; i++) {
            if (s[i] == '+') {
                plus++;
            }
            else {
                minus++;
            }
        }
        while (true) {
            //if (init >= minus) {
                //res += si;
                //break;
            //}
            //else {
                cur = init;
                ok = true;
                long long plus2 = plus, minus2 = minus;
                for (long long i = 0; i < s.size(); i++) {
                    if (cur + plus2 >= minus2) {
                        res += si - i;
                        break;
                    }
                    res++;
                    if (s[i] == '+') {
                        cur++;
                        plus2--;
                    }
                    else {
                        cur--;
                        minus2--;
                    }
                    if (cur < 0) {
                        ok = false;
                        break;
                    }
                }
                if (ok) {
                    break;
                }
                init++;
            //}
        }
        cout << res << endl;
    }
    return 0;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    string s;
    long long t;
    cin >> t;
    for (long long q = 0; q < t; q++) {
        cin >> s;
        long long res = 0, init = 0, cur, si, plus = 0, minus = 0;
        bool ok = true;
        si = s.size();
        for (long long i = 0; i < si; i++) {
            if (s[i] == '+') {
                plus++;
            }
            else {
                minus++;
            }
        }
        while (true) {
            //if (init >= minus) {
                //res += si;
                //break;
            //}
            //else {
                cur = init;
                ok = true;
                long long  minus2 = minus; // plus2 = plus,
                for (long long i = 0; i < s.size();) {
                    if (cur >= minus2) {
                        res += si - i;
                        break;
                    }
                    while (s[i] == '+' && i < si) {
                        cur++;
                        i++;
                        res++;
                    }
                    while (s[i] == '-' && i < si && cur >= 0) {
                        cur--;
                        i++;
                        minus2--;
                        res++;
                    }
                    if (cur < 0) {
                        ok = false;
                        break;
                    }
                }
                if (ok) {
                    break;
                }
                init++;
            //}
        }
        cout << res << endl;
    }
    return 0;
}