#include<stdio.h>
#define sign(x) x>=0?1:0

int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
      int n;
      scanf("%d",&n);

      long long int a[n],i,d=0;

      for(i=0;i<n;i++)
         scanf("%lld",&a[i]);

     long long int c=a[0];
      int s=sign(a[0]);

      for(i=1;i<n;i++)
         if(s!=sign(a[i]))
      {
         d+=c;
         s=sign(a[i]);
         c=a[i];
      }
      else if(c<a[i])
         c=a[i];

         printf("%lld\n",d+c);
   }

   return 0;

}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || (a[i]*a[i-1])<0)
             sum=sum+lmax;
             lmax=a[i];

        if(a[i]>lmax)
             lmax=a[i];

                                      }
printf("%lld\n",sum);

}
}


#include<stdio.h>
#define sign(x)x>=0?1:0
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=a[0];
int s=sign(a[0]);
for(long i=0;i<n;i++)
   {
    if(s!=sign(a[i]))
        { sum+=lmax ; s=sign(a[i]) ; lmax=a[i] ; }

    else if(a[i]>lmax)
         lmax=a[i];

                                      }
printf("%lld\n",sum+lmax);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
long long ts=0 ;
while(t--)
{
long n , k ;
scanf("%ld",&n);
long s[n] ;
long nn=0 , np=0;

long pi=0 , ni=0 ;
long po[n] , ne[n] ;
for(long i=0;i<n;i++)
  { scanf("%ld",&s[i]);
     ts=ts+s[i];
    if(s[i]>0)
       po[np++]=s[i];
    else
      ne[nn++]=s[i];

                       }
k = (np==nn ? 2*np : 2*(np>nn ? nn : np)+1 ) ;

if(np==nn)
  {
    printf("%lld\n",ts);
    continue;
    }
long t;
for(long i=0;i<np;i++)
   for(long j=0;j<(np-1);j++)
       if(po[j+1]>po[j])
          {
           t=po[j+1] ;
            po[j+1]=po[j];
            po[j]=t;
                      }

for(long i=0;i<nn;i++)
   for(long j=0;j<(nn-1);j++)
       if(ne[j+1]>ne[j])
          {
           t=ne[j+1] ;
            ne[j+1]=ne[j];
            ne[j]=t;
                      }
ts=0;
for(int i=0;i<((k-1)/2);i++)
 {
  ts=ts+po[i]+ne[i] ;
}

ts=ts+(np>nn ? po[((k-1)/2)] : ne[((k-1)/2)]) ;

printf("%lld\n",ts);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long long ts=0 ;
long n , k ;
scanf("%ld",&n);
long s[n] ;
long nn=0 , np=0;

long pi=0 , ni=0 ;
long po[n] , ne[n] ;
for(long i=0;i<n;i++)
  { scanf("%ld",&s[i]);
     ts=ts+s[i];
    if(s[i]>0)
       po[np++]=s[i];
    else
      ne[nn++]=s[i];

                       }
k = (np==nn ? 2*np : 2*(np>nn ? nn : np)+1 ) ;

if(np==nn)
  {
    printf("%lld\n",ts);
    continue;
    }
long t;
for(long i=0;i<np;i++)
   for(long j=0;j<(np-1);j++)
       if(po[j+1]>po[j])
          {
           t=po[j+1] ;
            po[j+1]=po[j];
            po[j]=t;
                      }

for(long i=0;i<nn;i++)
   for(long j=0;j<(nn-1);j++)
       if(ne[j+1]>ne[j])
          {
           t=ne[j+1] ;
            ne[j+1]=ne[j];
            ne[j]=t;
                      }
ts=0;
for(int i=0;i<((k-1)/2);i++)
 {
  ts=ts+po[i]+ne[i] ;
}

ts=ts+(np>nn ? po[((k-1)/2)] : ne[((k-1)/2)]) ;

printf("%lld\n",ts);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
long long ts=0 ;
while(t--)
{
long n , k ;
scanf("%ld",&n);
long s[n] ;
long nn=0 , np=0;

long pi=0 , ni=0 ;
long po[n] , ne[n] ;
for(long i=0;i<n;i++)
  { scanf("%ld",&s[i]);
     ts=ts+s[i];
    if(s[i]>0)
       po[np++]=s[i];
    else
      ne[nn++]=s[i];

                       }
long k = (np==nn ? 2*np : 2*(np>nn ? nn : np)+1 ) ;

if(np==nn)
  {
    printf("%lld\n",ts);
    continue;
    }
long t;
for(long i=0;i<np;i++)
   for(long j=0;j<(np-1);j++)
       if(po[j+1]>po[j])
          {
           t=po[j+1] ;
            po[j+1]=po[j];
            po[j]=t;
                      }

for(long i=0;i<nn;i++)
   for(long j=0;j<(nn-1);j++)
       if(ne[j+1]>ne[j])
          {
           t=ne[j+1] ;
            ne[j+1]=ne[j];
            ne[j]=t;
                      }
ts=0;
for(int i=0;i<((k-1)/2);i++)
 {
  ts=ts+po[i]+ne[i] ;
}

ts=ts+(np>nn ? po[((k-1)/2)] : ne[((k-1)/2)]) ;

printf("%lld\n",ts);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || (a[i]*a[i-1])<0 || i==(n-1))
          {  sum=sum+lmax;
             lmax=a[i];   }

        if(a[i]>lmax)
             lmax=a[i];

                                      }
printf("%lld\n",sum);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || (a[i]*a[i-1])<0)
          {  sum=sum+lmax;
             lmax=a[i];   }

        if(a[i]>lmax)
             lmax=a[i];

                                      }
printf("%lld\n",sum);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || ((a[i]*a[i-1])<0))
          {  sum=sum+lmax;
             lmax=a[i];   }

        if((a[i]*a[i-1])<0 && i==(n-1))
             sum=sum+a[n-1];
        if(a[i]>lmax)
             lmax=a[i];
         if(i==n-1 && (a[i]*a[i-1])>0)
           sum=sum+lmax;
                                      }
if(n==1)
  sum=a[0];
printf("%lld\n",sum);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || ((a[i]*a[i-1])<0))
          {  sum=sum+lmax;
             lmax=a[i];   }

        if((a[i]*a[i-1])<0 && i==(n-1))
             sum=sum+a[n-1];
        if(a[i]>lmax)
             lmax=a[i];
         if(i==n-1)
           sum=sum+lmax;
                                      }
if(n==1)
  sum=a[0];
printf("%lld\n",sum);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || ((a[i]*a[i-1])<0) || i==(n-1))
          {  sum=sum+lmax;
             lmax=a[i];   }

        if((a[i]*a[i-1])<0 && i==(n-1))
             sum=sum+a[n-1];
        if(a[i]>lmax)
             lmax=a[i];

                                      }
printf("%lld\n",sum);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || ((a[i]*a[i-1])<0) || i==(n-1))
          {  sum=sum+lmax;
             lmax=a[i];   }

        if(a[i]>lmax)
             lmax=a[i];

                                      }
printf("%lld\n",sum);

}
}


#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);

while(t--)
{
long n;
scanf("%ld",&n);

long a[n];
for(long i=0;i<n;i++)
   scanf("%ld",&a[i]);

long long sum=0;
long lmax=0;

for(long i=0;i<n;i++)
   {   if(i==0 || ((a[i]*a[i-1])<0) || i==(n-1))
          {  sum=sum+lmax;
             lmax=a[i];   }

        if((a[i]*a[i-1])<0 && i==(n-1))
             sum=sum+a[n-1];
        if(a[i]>lmax)
             lmax=a[i];

                                      }
if(n==1)
  sum=a[0];
printf("%lld\n",sum);

}
}


#include<stdio.h>

int main(){

        int t,i;
        scanf("%d",&t);
        for(i=1;i<=t;i++){
                long long n,j,sign,cursign;
                scanf("%lld",&n);
                long long arr[n];
                for(j=0;j<n;j++){
                        scanf("%lld",&arr[j]);
                }
                long long max=arr[0],output=0;
                if(arr[0]<0){
                        sign=0;
                        cursign=0;
                }
                else{
                        sign=1;
                        cursign=1;
                }
                if(n==1)
                        output=arr[0];
                else{
                        for(j=1;j<n;j++){

                                if(arr[j]<0)
                                        sign=0;
                                else
                                        sign=1;
                                if(sign==cursign){
                                        if(arr[j]>max)
                                                max=arr[j];
                                }
                                else{
                                        output+=max;
                                        if(cursign==1)
                                                cursign=0;
                                        else
                                                cursign=1;
                                        max=arr[j];
                                }
                                if(j==(n-1))
                                        output+=max;
                }

                }printf("%lld\n",output);

        }
        return 0;

}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>

#define MAX(a, b) (a>b?a:b)
#define MIN(a, b) (a<b?a:b)
#define MOD 1000000007

int compare(const void* a, const void* b) {
    return *(int*)a-*(int*)b;
}

void testCase() {
    int i, n;
    long long int sum=0;
    scanf("%d", &n);
    int a[n];
    for(i=0;i<n;i++) {
        scanf("%d", &a[i]);
    }
    for(i=0;i<n;i++) {
        int max;
        if(a[i] < 0) {
            max = a[i];
            i++;
            // printf("%d ", max);
            while(a[i] < 0 && i <= n-1) {
                if(a[i] > max) {
                    max = a[i];
                }
                i++;
                // printf("%d ", max);
            }
            i--;
        }
        else if(a[i] > 0) {
            max = a[i];
            i++;
            // printf("%d ", max);
            while(a[i] > 0 && i <= n-1) {
                if(a[i] > max) {
                    max = a[i];
                }
                i++;
                // printf("%d ", max);
            }
            i--;
        }
        // printf("%d ", max);
        sum += max;
    }
    printf("%lld\n", sum);
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        testCase();
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

 int isprime(long long p)
 { int i; double max=sqrt(p);
  for(i=2;i<=max;i++)
   if(p%i==0)break;
  if(i>max)return 1;
   return 0;
 }

 int gcd(long a, long b)
{ long x=(a<b)?a:b;
  long i;
  for(i=x;i>0;i--)
      if((a%i==0)&&(b%i==0))break;
    return i;
}

int comp(const void * a, const void * b)      //called when qsort is implemented for ascending order
{
   return ( *(int*)a - *(int*)b );
}

int issame(int *a,int *b,int l)         //checks if two arrays of same length are same after sorting them **
{ qsort(a,l,sizeof(int),comp);
  qsort(b,l,sizeof(int),comp);
  int i;
 for(i=0; i<l; i++)
    if(a[i]!=b[i]) break;
if(i==l)return 1;
 return 0;
  }

void swap(int *a, int *b)
{int x= *a;
 *a= *b;
*b=x;
}

long long  arrmax(long long  *a, int l) //to find max element in array
{ int i; int max= a[0];
  for(i=1;i<l;i++)
   if(a[i]>max)max=a[i];
return max;
}

long long  arrmin(long long  *a, int l) //to find min element in array
{ int i; int min= a[0];
  for(i=1;i<l;i++)
   if(a[i]<min)min=a[i];
return min;
}

long long  fact(int a)  //for factorial
 {int p=1;
  while(a>0)
  p*=a--;
  return p;
 }

 int sign(long long  a)
 {if(a>0)return 1;
  return 0;
 }

 int main(void)
 {int t;      // number of test cases. Put t=1 if no test cases
  scanf("%d",&t);
  while(t--)           // isprime(long) , gcd(long,long) , comp , int issame(int *a,int *b,int l) , void swap(int *a, int *b) , int arrmin(int *a, int l)  , long long fact(int a)
  {
  	int n;
  	scanf("%d",&n);
  	long long  a[n];
  	 int i;
  	  for(i=0;i<n;i++)
  	   scanf("%lld",a+i);
       int f=0; long long  s=0;
	 for(i=0;i<n;i++)
	 {if(sign(a[i])!=sign(a[i+1]) || i==n-1)
	 {  s+=arrmax(a+f,i-f+1);
	    f=i+1;
	   	 }}
	 printf("%lld \n",s);

  }
	return 0;
 }


#include<stdio.h>
struct temp
{
   long int c,b;
};

struct temp findmax(long int i,long int a[],long int n)
{
    struct temp x;
    long int k=i+1,o=i,m=a[i];
    if(a[i]>0)
    {
        while(k<n&&a[k]>0)
        {
            if(a[k]>=m)
                o=k;
            k++;
        }
    }
    else
    {
        while(k<n&&a[k]<0)
        {
            if(a[k]>=m)
                o=k;
            k++;
        }
    }
    x.c=k;
    x.b=o;

    return(x);

    }
int main()
{
    int t,j;
    scanf("%d",&t);
    for(j=0;j<t;j++)
    {
    struct temp h;
    long int a[200000],n,i,sum=0,k;
        scanf("%ld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%ld",&a[i]);
    }
    i=0;
    while(i<n)
    {
        h=findmax(i,a,n);
        k=h.b;
        sum=sum+a[k];
        i=h.c;
    }
        printf("%ld\n",sum);
    }
}


#include<stdio.h>
struct temp
{
   long int c,b;
};

struct temp findmax(long int i,long int a[],long int n)
{
    struct temp x;
    long int k=i+1,o=i,m=a[i];
    if(a[i]>0)
    {
        while(k<n&&a[k]>0)
        {
            if(a[k]>=m)
                o=k;
            k++;
        }
    }
    else
    {
        while(k<n&&a[k]<0)
        {
            if(a[k]>=m)
                o=k;
            k++;
        }
    }
    x.c=k;
    x.b=o;

    return(x);

    }
int main()
{
    int t,j;
    scanf("%d",&t);
    for(j=0;j<t;j++)
    {
    struct temp h;
    long int a[200000],n,i,sum=0,k;
        scanf("%ld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%ld",&a[i]);
    }
    i=0;
    while(i<n)
    {
        h=findmax(i,a,n);
        k=h.b;
        sum=sum+a[k];
        i=h.c;
    }
        printf("\n%ld",sum);
    }
}


#include<stdio.h>
int main()
{
    int t,j;
    scanf("%d",&t);
    for(j=0;j<t;j++)
    {
    int n,i,k=1;
    long long int a[200000];
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%lld",&a[i]);
    i=0;
    while(k<n)
    {
        if(a[i]*a[k]>0)
        {
            if(a[i]>a[k])
            {
                a[k]=0;
                k++;
            }
            else
            {
                a[i]=0;
                i=k;
                k++;
            }
        }
        else
        {
            i=k;
            k++;
        }
    }
    long long int sum=0;
    for(i=0;i<n;i++)
        sum=sum+a[i];
    printf("%lld\n",sum);
}
}


#include<stdio.h>
struct temp
{
   long long int c,b;
};

struct temp findmax(long long int i,long long int a[],long long int n)
{
    struct temp x;
   long long int k=i+1,o=i,m=a[i];
    if(a[i]>0)
    {
        while(k<n&&a[k]>0)
        {
            if(a[k]>=m)
                o=k;
            k++;
        }
    }
    else
    {
        while(k<n&&a[k]<0)
        {
            if(a[k]>=m)
                o=k;
            k++;
        }
    }
    x.c=k;
    x.b=o;

    return(x);

    }
int main()
{
    int t,j;
    scanf("%d",&t);
    for(j=0;j<t;j++)
    {
    struct temp h;
   long long int a[200000],n,i,sum=0,k;
        scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
    }
    i=0;
    while(i<n)
    {
        h=findmax(i,a,n);
        k=h.b;
        sum=sum+a[k];
        i=h.c;
    }
        printf("%lld\n",sum);
    }
}


#include<stdio.h>
int main()
{
    int t,j;
    scanf("%d",&t);
    for(j=0;j<t;j++)
    {
    int n,i,k=1;
    long long int a[200000];
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%lld",&a[i]);
    i=0;
    while(k<=n)
    {
        if(a[i]*a[k]>0)
        {
            if(a[i]>a[k])
            {
                a[k]=0;
                k++;
            }
            else
            {
                a[i]=0;
                i=k;
                k++;
            }
        }
        else
        {
            i=k;
            k++;
        }
    }
    long long int sum=0;
    for(i=0;i<n;i++)
        sum=sum+a[i];
    printf("%lld\n",sum);
}
}


#include<stdio.h>

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        long long i, num,j=-1,k=-1,sum=0;
        scanf("%lld",&num);
        long long in[num],jog[num],biyog[num];
        for(i=0; i<num; i++)
        {
            scanf("%lld",&in[i]);
        }

        for(i=0; i<num; i++)
        {
            if(i==0&&in[i]>0)
            {
                j++;
                jog[j]=in[i];
            }
            else if(i==0&&in[i]<0)
            {
                k++;
                biyog[k]=in[i];
            }
            else if(in[i]>0&&in[i-1]>0)
            {
                if(in[i]>jog[j])
                {
                    jog[j]=in[i];
                }
            }
            else if(in[i]>0&&in[i-1]<0)
            {
                j++;
                jog[j]=in[i];
            }
            else if(in[i]<0&&in[i-1]<0)
            {
                if(in[i]>biyog[k])
                {
                    biyog[k]=in[i];
                }
            }
            else if(in[i]<0&&in[i-1]>0)
            {
                k++;
                biyog[k]=in[i];
            }
        }
        if(j>=0)
        {
            for(i=0;i<=j;i++)
            {
                 sum=sum+jog[i];
            }
        }

        if(k>=0)
        {
            for(i=0;i<=k;i++)
            {
                 sum=sum+biyog[i];
            }
        }
        printf("%lld\n",sum);

    }
    return 0;
}


#include<stdio.h>
#define INT_MIN -2000000000
int max(int a,int b)
{
    return (a>b?a:b);
}
int main()
{

    int t;
    scanf("%d",&t);
    while(t--)
    {
      int n,a=0,mx=INT_MIN,mn=INT_MIN;
        long long int s=0;
        scanf("%d",&n);
        while(n--)
        {

            scanf("%d",&a);
            if(a<0)
            {

                mn=max(mn,a);
                if(mx==INT_MIN)
                mx=0;
                s+=mx;
                mx=INT_MIN;

            }
            else
            {
                mx=max(mx,a);
                if(mn==INT_MIN)
                mn=0;
                s+=mn;
                mn=INT_MIN;
            }
        }
        if(a>0)
        s+=mx;
        else
        s+=mn;
        printf("%lld\n",s);

    }
    return 0;
}


/*  AUTHOR: AKASH JAIN
*   EMAIL:  akash19jain@gmail.com
*   ID:     akash19jain
*   DATE:   22-04-2020 12:42:10
*/

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

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
#define SC1(x)          scanf("%lld",&x)
#define SC2(x,y)        scanf("%lld%lld",&x,&y)
#define SC3(x,y,z)      scanf("%lld%lld%lld",&x,&y,&z)
#define SCS(x)          scanf("\n%s", x)
#define SCA(a,n)        for(long long i=0;i<n;i++)    scanf("%lld",&a[i]);
#define PF1(x)          printf("%lld\n",x)
#define PF2(x,y)        printf("%lld %lld\n",x,y)
#define PF3(x,y,z)      printf("%lld %lld %lld\n",x,y,z)
#define PFA(a,n)        for(long long i=0;i<n;i++)    printf("%lld ",a[i]); printf("\n");
#define PFN             printf("\n")
#define PFS(x)          printf("%s\n",x)
#define REP(i,n)        for(long long i=0;i<(n);++i)
#define FOR(i,a,b)      for(long long i=(a);i<=(b);++i)
#define FORD(i,a,b)     for(long long i=(a);i>=(b);--i)
#define WHILE(n)        while(n--)
#define MEM(a, b)       memset(a, (b), sizeof(a))
#define ITOC(c)         ((char)(((ll)'0')+c))
#define MID(s,e)        (s+(e-s)/2)
#define SZ(a)           strlen(a)
#define PI              3.1415926535897932384626433832795
#define DEB(x)          printf("The value of \"%s\" is: %d\n",#x,x)
#define CASES           ll t;SC1(t);while(t--)
#define ABS(a)          ((a>0)?a:-(a))
#define SWAP(a,b)       ll z=a;a=b;b=z
#define SWAPC(a,b)      char z=a;a=b;b=z
#define FLSH            fflush(stdout)
#define faster          ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1<<29;
const ll MOD = 1000000007;
const ll FMOD = 998244353;
const ll MAX = 10000000005;
const ll MIN = -10000000005;
#define FILEIO(name) \  freopen(name".in", "r", stdin); \   freopen(name".out", "w", stdout);
int cmp(const void * a,const void * b);
long long maxv(long long a,long long b);
long long minv(long long a,long long b);
long long gcd(long long u,long long v);
long long digits(long long n);
bool ispoweroftwo(long long n);
bool isvowel(char x);
ll chartoint(char ch);
ll CEIL(ll x,ll y);
ll FLOOR(ll x,ll y);

int sign(ll a)
{
	if(a>0)
		return 1;
	return -1;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("F:\\COMPETITIVE-PROGRAMMING\\inp.txt", "r", stdin);
        freopen("F:\\COMPETITIVE-PROGRAMMING\\out.txt", "w", stdout);
    #endif

    CASES
    {
    	ll n;
    	SC1(n);
    	ll arr[n];
    	SCA(arr,n);

    	ll sum=0;
    	REP(i,n)
    	{
    		ll max=-1e10;
    		ll j=i;
    		while(j<n)
    		{
    			if(sign(arr[i])!=sign(arr[j]))
    				break;
    			max=maxv(max,arr[j]);
    			j++;
    		}
    		//DEB(max);
    		sum+=max;
    		i=j-1;;

    	}
    	PF1(sum);
    }
    return 0;
}

//qsort(arr,n,sizeof(arr[0]),cmp);
int cmp (const void * a, const void * b)
{
    if( *(ll*)a - *(ll*)b < 0 ) return -1;
    if( *(ll*)a - *(ll*)b > 0 ) return 1;
        return 0;
}
long long maxv(long long a,long long b)
{
    if(a>b) return a;
        return b;
}
long long minv(long long a,long long b)
{
    if(a<b) return a;
        return b;
}
long long gcd(long long u,long long v)
{
    if (v == 0) return u;
       return gcd(v, u%v);
}
long long digits(long long n)    //to calculate no of digits in a number
{
    return floor(log10(n))+1;
}
bool ispoweroftwo(long long x)
{
    return x && (!(x&(x-1)));
}
bool isvowel(char x)
{
    return(x=='a' || x== 'e' ||x=='i' || x== 'o' || x=='u' );
}
ll chartoint(char ch)
{
    if(ch>='A' && ch<='Z') return(ch-'A');
    else if(ch>='0' && ch<='9') return (ch-'0');
    else if(ch>='a' && ch<='z') return (ch-'a');
    else return 0;
}
ll CEIL(ll x,ll y)
{
    if(x%y==0) return(x/y);
    else return(x/y +1);
}

ll FLOOR(ll x,ll y)
{
    if(x%y==0) return(x/y);
    else return(x/y -1);
}


#include <stdio.h>
#include <math.h>
#define MAX(a,b) (((a)>(b))?(a):(b))
int main() {
    long long int t;
    scanf("%lld",&t);
    while(t--){
        long long int n,max=-2000000000,ft,sum=0,i;
        scanf("%lld",&n);
        long long int arr[n];
        for(i=0;i<n;i++)
        scanf("%lli",&arr[i]);
        ft=arr[0];
        for(i=0;i<n;i++){
            if(ft*arr[i]>0) max=MAX(max,arr[i]);
            else{
                sum=sum+max;
                ft= arr[i];
                max=arr[i];
            }
        }
        sum=sum+max; //bcos last updated max reh gaya hrr case me.
        printf("%lli\n",sum);
    }
	//code
	return 0;
}


#include <stdio.h>
#include <math.h>
#define MAX(a,b) (((a)>(b))?(a):(b))
int main() {
    long long int t,max=-2000000000,ft,sum=0;
    scanf("%lld",&t);
    while(t--){
        long long int n;
        scanf("%lld",&n);
        long long int arr[n];
        for(i=0;i<n;i++)
        scanf("%lld",&arr[i]);
        ft=a[0];
        for(i=0;i<n;i++){
            if(ft*arr[i]>0) max=MAX(max,arr[i]);
            else{
                sum=sum+max;
                ft= arr[i];
                max=arr[i];
            }
        }
        sum=sum+max; //bcos last updated max reh gaya hrr case me.
        printf("%lld\n",sum);
    }
	//code
	return 0;
}


#include <stdio.h>
#include <math.h>
#define MAX(a,b) (((a)>(b))?(a):(b))
int main() {
    long long int t,max=-2000000000,ft,sum=0,i;
    scanf("%lld",&t);
    while(t--){
        long long int n;
        scanf("%lld",&n);
        long long int arr[n];
        for(i=0;i<n;i++)
        scanf("%lld",&arr[i]);
        ft=arr[0];
        for(i=0;i<n;i++){
            if(ft*arr[i]>0) max=MAX(max,arr[i]);
            else{
                sum=sum+max;
                ft= arr[i];
                max=arr[i];
            }
        }
        sum=sum+max; //bcos last updated max reh gaya hrr case me.
        printf("%lld\n",sum);
    }
	//code
	return 0;
}


#include<stdio.h>
#include<stdlib.h>

#define ll long long

int main()
{
	ll *a, t, n, k, max, sum;
	scanf("%lld", &t);
	for(int i=0;i<t;i++)
	{
		sum=0;
		k=0;
		scanf("%lld", &n);
		a = (ll *)calloc(n , sizeof(ll));
		for(int j=0;j<n;j++)
			scanf("%lld", &a[j]);
		max = a[0];
		while(k < n)
		{
			if( a[k]*max<0 )
			{
//				printf("If happened  value of a[k] rn is:%d  ", a[k]);
				sum += max;
//				printf("%d ", k);
//				printf("the sum rn is:%d ", sum);
				max = a[k];
			}
			else
			{
//				printf("Else happpened  value of a[k] rn is:%d  ", a[k]);
				max = max>a[k]?max:a[k];
			}
			k++;
		}
//		printf("finally k=%d ", k);
//		printf("pre-final sum=%d final value of max=%d ", sum, max);
		sum += max;
		printf("%lld\n", sum);
	}
	return 0;
}


#include<stdio.h>
int main()
{
    int t,i,j,n;
    long long c,r,sum=0,s;
    scanf("%d",&t);

    while(t--)
    { sum=0;
    s=0;
     scanf("%d",&n);
    scanf("%lli",&c);
    if(n==1)
        printf("%lli\n",c);
        continue;
    if(c>0)
        j=1;
    else
        j=-1;
        s=c;

        for(i=1;i<n;i++)
        {

          scanf("%lli",&c);
          if(j*c>0)
          {
              if(s<c)
                s=c;
          }
           if(j*c<0||i==n-1)
          {

            sum=sum+s;
            s=c;

            if(i==n-1&&j*c<0)
                sum=sum+s;
            j=-1*j;
          }
        }
        printf("%lli\n",sum);

    }
}


#include<stdio.h>
int main()
{
    int t,i,j,n;
    long long c,r,sum=0,s;
    scanf("%d",&t);

    while(t--)
    { sum=0;
    s=0;
     scanf("%d",&n);
    scanf("%lli",&c);
    if(n==1)
        {
            printf("%lli\n",c);
        continue;
        }
    if(c>0)
        j=1;
    else
        j=-1;
        s=c;

        for(i=1;i<n;i++)
        {

          scanf("%lli",&c);
          if(j*c>0)
          {
              if(s<c)
                s=c;
          }
           if(j*c<0||i==n-1)
          {

            sum=sum+s;
            s=c;

            if(i==n-1&&j*c<0)
                sum=sum+s;
            j=-1*j;
          }
        }
        printf("%lli\n",sum);

    }
}


#include<stdio.h>
int main()
{
    int t,i,j,n;
    long long c,r,sum=0,s;
    scanf("%d",&t);

    while(t--)
    { sum=0;
    s=0;
     scanf("%d",&n);
    scanf("%lli",&c);
    if(c>0)
        j=1;
    else
        j=-1;
        s=c;

        for(i=1;i<n;i++)
        {

          scanf("%lli",&c);
          if(j*c>0)
          {
              if(s<c)
                s=c;
          }
           if(j*c<0||i==n-1)
          {

            sum=sum+s;
            s=c;

            if(i==n-1&&j*c<0)
                sum=sum+s;
            j=-1*j;
          }
        }
        printf("%lli\n",sum);

    }
}


#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>

#define MAX(x, y) ((x) > (y) ? (x) : (y))

int main()
{
    int32_t t;
    scanf("%d", &t);

    int32_t *n[t];
    int32_t sz[t];
    int32_t i;
    for (i = 0; i < t; ++i) {
	scanf("%d", &sz[i]);
	n[i] = calloc(sz[i], sizeof(long));

	int j;
	for (j = 0; j < sz[i]; ++j)
	    scanf("%d", &n[i][j]);
    }

    for (i = 0; i < t; ++i) {
	int32_t *cur = &n[i][0];
	int32_t *end = &n[i][sz[i]];

	int32_t neg = INT32_MIN;
	int32_t pos = INT32_MIN;
	int64_t s = 0;

	while (cur < end) {
	    while (cur < end && *cur > 0) {
		if (*cur > pos) pos = *cur;
		++cur;
	    }

            while (cur < end && *cur < 0) {
              if (*cur > neg) neg = *cur;
              ++cur;
            }

	    if (pos != INT32_MIN) {
		s += pos;
		pos = INT32_MIN;
	    }
	    if (neg != INT32_MIN) {
		s += neg;
		neg = INT32_MIN;
	    }
	}

	printf("%" PRId64 "\n", s);
    }
}


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define MAX(x, y) ((x) > (y) ? (x) : (y))

int main()
{
    int t;
    scanf("%d", &t);

    long *n[t];
    int sz[t];
    int i;
    for (i = 0; i < t; ++i) {
	scanf("%d", &sz[i]);
	n[i] = calloc(sz[i], sizeof(long));

	int j;
	for (j = 0; j < sz[i]; ++j)
	    scanf("%ld", &n[i][j]);
    }

    for (i = 0; i < t; ++i) {
	long *cur = &n[i][0];
	long *end = &n[i][sz[i]];

	int neg = INT_MIN;
	int pos = INT_MIN;
	long s = 0;

	while (cur < end) {
	    while (cur < end && *cur > 0) {
		if (*cur > pos) pos = *cur;
		++cur;
	    }

            while (cur < end && *cur < 0) {
              if (*cur > neg)
                neg = *cur;
              ++cur;
            }

	    if (pos != INT_MIN) {
		s += pos;
		pos = INT_MIN;
	    }
	    if (neg != INT_MIN) {
		s += neg;
		neg = INT_MIN;
	    }
	}

	printf("%ld\n", s);
    }
}


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define MAX(x, y) ((x) > (y) ? (x) : (y))

int main()
{
    int t;
    scanf("%d", &t);

    long *n[t];
    int sz[t];
    int i;
    for (i = 0; i < t; ++i) {
	scanf("%d", &sz[i]);
	n[i] = calloc(sz[i], sizeof(long));

	int j;
	for (j = 0; j < sz[i]; ++j)
	    scanf("%ld", &n[i][j]);
    }

    for (i = 0; i < t; ++i) {
	long *cur = &n[i][0];
	long *end = &n[i][sz[i]];

	long neg = LONG_MIN;
	long pos = LONG_MIN;
	long s = 0;

	while (cur < end) {
	    while (cur < end && *cur > 0) {
		if (*cur > pos) pos = *cur;
		++cur;
	    }

            while (cur < end && *cur < 0) {
              if (*cur > neg)
                neg = *cur;
              ++cur;
            }

	    if (pos != LONG_MIN) {
		s += pos;
		pos = LONG_MIN;
	    }
	    if (neg != LONG_MIN) {
		s += neg;
		neg = LONG_MIN;
	    }
	}

	printf("%ld\n", s);
    }
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<assert.h>
#include <math.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384

int main()
{
    int t;
    scanf("%d",&t);

    while(t-->0)
    {
       long long int n,i,q,j;
long long int c=0,k,mx;

        scanf("%lld",&n);

    long long int a[n];

       for(i=0;i<n;i++)
       {
           scanf("%lld",&a[i]);
       }

       for(i=0;i<n;)
       {
          j=i+1;
          mx=a[i];
          while(j<n&&a[j]*a[i]>0){
            if(a[j]>mx)
                mx=a[j];
            j++;
          }
          c+=mx;
          i=j;
       }

printf("%lld\n",c);

    }
    return 0;
    }


#include<stdio.h>

int main(){
     int t,k,j,i,n,tut;
	long long int maxsum,arr[100000];
	scanf("%d",&t);

	while(t--){
		scanf("%d",&n);
		for(i=0;i<n;i++)scanf("%lld",&arr[i]);
		k=0;
		maxsum=arr[0];
		tut=0;

		for(i=0;i<n-1;i++){
			if(arr[i]*arr[i+1]<0 && k==0){
				maxsum+=arr[i+1];
				k=1;
				tut=i+1;
			//	printf("a");
			}

			else if(arr[i]*arr[i+1]>0 && k==1){
			if(arr[i+1]>arr[tut]){
			  maxsum+=(arr[i+1]-arr[tut]);
	          tut=i+1;
		//	printf("b");
			 }
			}

			else if(arr[i]*arr[i+1]<0 && k==1){
				maxsum+=arr[i+1];
				k=0;
				tut=i+1;
			//	printf("c");
			}

			else if(arr[i]*arr[i+1]>0 && k==0){
			if(arr[i+1]>arr[tut]){
			maxsum=maxsum+(arr[i+1]-arr[tut]);
			tut=i+1;
		//	printf("d");
		     }
			}
			//printf("%lld\n",maxsum);
		}

      printf("%lld\n",maxsum);
	}

	return 0;
}


#include<stdio.h>
int main(){
	long long int t,k,j,i,n,tut,maxsum,arr[100000];
	scanf("%lld",&t);
	while(t--){

		scanf("%lld",&n);
		for(i=0;i<n;i++)scanf("%lld",&arr[i]);
		k=0;
		maxsum=arr[0];
		tut=0;
		for(i=0;i<n-1;i++){

			if(arr[i]*arr[i+1]<0 && k==0){
				maxsum+=arr[i+1];
				k=1;
				tut=i+1;
			//	printf("a");
			}

			else if(arr[i]*arr[i+1]>0 && k==1){
			if(arr[i+1]>arr[tut]){
			  maxsum+=(arr[i+1]-arr[i]);
	          tut=i+1;
			//printf("b");
			 }
			}

			else if(arr[i]*arr[i+1]<0 && k==1){
				maxsum+=arr[i+1];
				k=0;
				tut=i+1;
			//	printf("c");
			}

			else if(arr[i]*arr[i+1]>0 && k==0){
			if(arr[i+1]>arr[tut]){
			maxsum=maxsum+(arr[i+1]-arr[i]);
			tut=i+1;
			//printf("d");
		     }
			}
			//printf("%lld\n",maxsum);
		}

      printf("res %lld\n",maxsum);
	}

	return 0;


#include<stdio.h>

int main(){
	long long int t,k,j,i,n,tut,maxsum,arr[100000];
	scanf("%lld",&t);
	while(t--){

		scanf("%lld",&n);
		for(i=0;i<n;i++)scanf("%lld",&arr[i]);
		k=0;
		maxsum=arr[0];
		tut=0;
		for(i=0;i<n-1;i++){

			if(arr[i]*arr[i+1]<0 && k==0){
				maxsum+=arr[i+1];
				k=1;
				tut=i+1;
			//	printf("a");
			}

			else if(arr[i]*arr[i+1]>0 && k==1){
			if(arr[i+1]>arr[tut]){
			  maxsum+=(arr[i+1]-arr[i]);
	          tut=i+1;
			//printf("b");
			 }
			}

			else if(arr[i]*arr[i+1]<0 && k==1){
				maxsum+=arr[i+1];
				k=0;
				tut=i+1;
			//	printf("c");
			}

			else if(arr[i]*arr[i+1]>0 && k==0){
			if(arr[i+1]>arr[tut]){
			maxsum=maxsum+(arr[i+1]-arr[i]);
			tut=i+1;
			//printf("d");
		     }
			}
			//printf("%lld\n",maxsum);
		}

      printf("res %lld\n",maxsum);
	}

	return 0;
}


#include<stdio.h>

int main(){
	long long int t,k,j,i,n,tut,maxsum,arr[200000];
	scanf("%lld",&t);
	while(t--){

		scanf("%lld",&n);
		for(i=0;i<n;i++)scanf("%lld",&arr[i]);
		k=0;
		maxsum=arr[0];
		tut=0;
		for(i=0;i<n-1;i++){

			if(arr[i]*arr[i+1]<0 && k==0){
				maxsum+=arr[i+1];
				k=1;
				tut=i+1;
			//	printf("a");
			}

			else if(arr[i]*arr[i+1]>0 && k==1){
			if(arr[i+1]>arr[tut]){
			  maxsum+=(arr[i+1]-arr[tut]);
	          tut=i+1;
		//	printf("b");
			 }
			}

			else if(arr[i]*arr[i+1]<0 && k==1){
				maxsum+=arr[i+1];
				k=0;
				tut=i+1;
			//	printf("c");
			}

			else if(arr[i]*arr[i+1]>0 && k==0){
			if(arr[i+1]>arr[tut]){
			maxsum=maxsum+(arr[i+1]-arr[tut]);
			tut=i+1;
		//	printf("d");
		     }
			}
			//printf("%lld\n",maxsum);
		}

      printf("%lld\n",maxsum);
	}

	return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main(){
    long long int *a, *b, i, sum, count1, t, n, x, y, j;
    scanf("%lld", &t);
    while(t--){
        sum=0;count1=0;
        scanf("%lld", &n);
        a=(long long int *)malloc(n*sizeof(long long int));
        b=(long long int *)malloc(n*sizeof(long long int));

        for(i=0;i<n;i++)
         scanf("%lld", &a[i]);
        for(i=0,j=0 ;i<n;i++){
          if(i==0){
           b[j]=a[i];
           count1++;
          }

          else{
          if(a[i]>0){
            if(b[j]<0){
             b[++j]=a[i];
             count1++;
            }
            else if(b[j]>0 && a[i]>b[j])
             b[j]=a[i];
            else
             continue;
          }
          else{
            if(b[j]>0){
             b[++j]=a[i];
             count1++;
            }
            else if(b[j]<0 && a[i]>b[j])
             b[j]=a[i];
            else
             continue;
          }
          }

        }
        for(j=0;j<count1;j++)
         sum+=b[j];
        printf("%lld\n", sum);
        free(a);
        free(b);
    }
}


#pragma GCC optimize("O3")
#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
enum SegType{SUM,MIN,MAX};
template<typename T>
struct SegTree{
    int num=0, bt;
    vector<T> node, lazy;
    function<T(const T &,const T &)> calc1;
    function<T(const int &, const int &, const int &, const int &, const T &)> calc2;
    function<T(const T &)> calc3;
    T _init;

    void init(int num,T n,SegType st){
        int i = 0, j = 1;
        this->num = num;
        if(st==SegType::MAX){
            this->_init = LLONG_MIN;
            this->calc1 = [](const T &a, const T &b) { return max(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::MIN){
            this->_init = LLONG_MAX;
            this->calc1 = [](const T &a, const T &b) { return min(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::SUM){
            this->_init = 0;
            this->calc1 = [](const T &a,const T &b) { return a + b; };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return (r - l + 1) * n; };
            this->calc3 = [](const T &n) { return n / 2; };
        }
        this->_init = n;

        for (j = 1; j < num;)
            j *= 2;
        this->bt = j - 1;

        node.resize(j * 2 - 1, n);
        lazy.resize(j * 2 - 1, 0);
        /*
        for (int i = 0; i < num; i++){
            node[bt + i] = n;
        }
        for (int i = bt - 1; i >= 0;i--){
            node[i]=calc1(node[i * 2 + 1], node[i * 2 + 2]);
        }*/
    }
    void addd(int x,int y,T n){
        if(x>y)
            return;
        _addd(x, y, n, 0, 0, this->bt);
    }
    void _addd(const int& x,const int& y,const T& n,int p,int l,int r){
        if (r < x || y < l)
            return;
        if(x<=l&&r<=y){
            lazy[p] += calc2(x, y, l, r, n);
            evall(p);
        }else{
            if(lazy[p]!=0)
                evall(p);
            _addd(x, y, n, p * 2 + 1, l, (l + r) / 2);
            _addd(x, y, n, p * 2 + 2, (l + r) / 2 + 1, r);
            node[p] = calc1(node[p * 2 + 1], node[p * 2 + 2]);
        }
    }
    inline void evall(int x){
        node[x] += lazy[x];
        if(x<bt){
            lazy[x * 2 + 1] += calc3(lazy[x]);
            lazy[x * 2 + 2] += calc3(lazy[x]);
        }
        lazy[x] = 0;
    }
    void sett(int x,T n){
        int i = bt+x;
        node[i] = n;
        while(i!=0){
            i = (i - 1) / 2;
            node[i] = calc1(node[i * 2 + 1],node[i * 2 + 2]);
        }
    }
    inline T gett(int x){
        T ret=_init;
        _gett(x, x, 0, 0, this->bt, ret);
        return ret;
    }
    T gett(int x,int y){
        T ret=_init;
        if(x>y)
            return ret;
        _gett(x, y, 0, 0, this->bt, ret);
        return ret;
    }
    void _gett(const int& x,const int& y,int p,int l,int r,T& ret){
        //printf("%d,%d,%d,%d\n", x, y, l, r);
        if(r<x||y<l)
            return;
        if(lazy[p]!=0)
            evall(p);
        if(x<=l&&r<=y){
            ret = calc1(ret, node[p]);
            //printf("%d,%d,%d,%d:%lld\n", x, y, l, r, ret);
            return;
        }
        _gett(x, y, p * 2 + 1, l, (l + r) / 2, ret);
        _gett(x, y, p * 2 + 2, (l + r) / 2 + 1, r, ret);
    }
};

ll N,mx;
char S[1010101];
char A[1010101];
pll cv[1010101];
SegTree<ll> sgok,sgnum;
ll ans[1010101];
ll i,j,k;

void addd(ll l,ll r,ll n){
    sgok.addd(l, r, n);
    sgnum.addd(l, r, n);
}

ll op(ll& i, char a){

    if(a=='R'){
        i++;
    }else if(a=='L'){
        i--;
    }else if(A[i]!=a){
        if(A[i]=='('){
            addd(0, i, 1);
            addd(i + 1, mx, -1);
        }else if(A[i]==')'){
            addd(0, i, -1);
            addd(i + 1, mx, 1);
        }
        if(a=='('){
            addd(0, i, -1);
            addd(i + 1, mx, 1);
        }else if(a==')'){
            addd(0, i, 1);
            addd(i + 1, mx, -1);
        }
        A[i] = a;
    }
    //printf("a%lld\n", sg.gett(0));
    if(sgok.gett(0,mx)==0){
        return sgnum.gett(0, mx) / 2;
    }
    return -1;
}

int main(){
    scanf("%lld%s", &N,S);
    mx = N + 5;
    sgnum.init(mx + 1, -1e15, SegType::MAX);
    sgok.init(mx + 1, 1e15, SegType::MIN);
    sgnum.addd(0, mx, 1e15);
    sgok.addd(0, mx, -1e15);
    j = 0;
    fornum(i,0,N){
        ans[i] = op(j, S[i]);
    }
    fornum(i,0,N){
        printf("%lld ", ans[i]);
    }
    return 0;
}


//#pragma GCC optimize("O3")
#include<bits/stdc++.h>

#define ll int
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
enum SegType{SUM,MIN,MAX};
template<typename T>
struct SegTree{
    int num=0, bt;
    vector<T> node, lazy;
    function<T(const T &,const T &)> calc1;
    function<T(const int &, const int &, const int &, const int &, const T &)> calc2;
    function<T(const T &)> calc3;
    T _init;

    void init(int num,T n,SegType st){
        int i = 0, j = 1;
        this->num = num;
        if(st==SegType::MAX){
            //this->_init = LLONG_MIN;
            this->calc1 = [](const T &a, const T &b) { return max(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::MIN){
            //this->_init = LLONG_MAX;
            this->calc1 = [](const T &a, const T &b) { return min(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::SUM){
            //this->_init = 0;
            this->calc1 = [](const T &a,const T &b) { return a + b; };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return (r - l + 1) * n; };
            this->calc3 = [](const T &n) { return n / 2; };
        }
        this->_init = n;

        for (j = 1; j < num;)
            j *= 2;
        this->bt = j - 1;

        node.resize(j * 2 - 1, n);
        lazy.resize(j * 2 - 1, 0);
        /*
        for (int i = 0; i < num; i++){
            node[bt + i] = n;
        }
        for (int i = bt - 1; i >= 0;i--){
            node[i]=calc1(node[i * 2 + 1], node[i * 2 + 2]);
        }*/
    }
    void addd(int x,int y,T n){
        if(x>y)
            return;
        _addd(x, y, n, 0, 0, this->bt);
    }
    void _addd(const int& x,const int& y,const T& n,int p,int l,int r){
        if (r < x || y < l)
            return;
        if(x<=l&&r<=y){
            lazy[p] += calc2(x, y, l, r, n);
            evall(p);
        }else{
            if(lazy[p]!=0)
                evall(p);
            _addd(x, y, n, p * 2 + 1, l, (l + r) / 2);
            _addd(x, y, n, p * 2 + 2, (l + r) / 2 + 1, r);
            node[p] = calc1(node[p * 2 + 1], node[p * 2 + 2]);
        }
    }
    inline void evall(int x){
        node[x] += lazy[x];
        if(x<bt){
            lazy[x * 2 + 1] += calc3(lazy[x]);
            lazy[x * 2 + 2] += calc3(lazy[x]);
        }
        lazy[x] = 0;
    }
    void sett(int x,T n){
        int i = bt+x;
        node[i] = n;
        while(i!=0){
            i = (i - 1) / 2;
            node[i] = calc1(node[i * 2 + 1],node[i * 2 + 2]);
        }
    }
    inline T gett(int x){
        T ret=_init;
        _gett(x, x, 0, 0, this->bt, ret);
        return ret;
    }
    T gett(int x,int y){
        T ret=_init;
        if(x>y)
            return ret;
        _gett(x, y, 0, 0, this->bt, ret);
        return ret;
    }
    void _gett(const int& x,const int& y,int p,int l,int r,T& ret){
        //printf("%d,%d,%d,%d\n", x, y, l, r);
        if(r<x||y<l)
            return;
        if(lazy[p]!=0)
            evall(p);
        if(x<=l&&r<=y){
            ret = calc1(ret, node[p]);
            //printf("%d,%d,%d,%d:%lld\n", x, y, l, r, ret);
            return;
        }
        _gett(x, y, p * 2 + 1, l, (l + r) / 2, ret);
        _gett(x, y, p * 2 + 2, (l + r) / 2 + 1, r, ret);
    }
};

ll N,mx;
char S[1010101];
char A[1010101];
SegTree<ll> sgok,sgnum;
ll ans[1010101];
ll i,j,k;

inline void addd(ll l,ll r,ll n){
    sgok.addd(l, r, n);
    sgnum.addd(l, r, n);
}

ll op(ll& i, char a){
    if(a=='R'){
        i++;
    }else if(a=='L'){
        if(i>0)
            i--;
    }else if(A[i]!=a){
        ll b = 0;
        if(A[i]=='('){
            b++;
        }else if(A[i]==')'){
            b--;
        }
        if(a=='('){
            b--;
        }else if(a==')'){
            b++;
        }
        addd(0, i, b);
        addd(i + 1, mx, -b);
        A[i] = a;
    }
    //printf("a%lld\n", sg.gett(0));
    if(sgok.gett(0,mx)==0){
        return sgnum.gett(0, mx) / 2;
    }
    return -1;
}

int main(){
    scanf("%d%s", &N,S);
    mx = 0;
    j = 0;
    fornum(i,0,N){
        if(S[i]=='R'){
            j++;
            mx = max(j, mx);
        }else if(S[i]=='L'){
            if(j>0)
                j--;
        }
    }
    mx += 2;
    sgnum.init(mx + 1, -1e9, SegType::MAX);
    sgok.init(mx + 1, 1e9, SegType::MIN);
    sgnum.addd(0, mx, 1e9);
    sgok.addd(0, mx, -1e9);
    j = 0;
    fornum(i,0,N){
        ans[i] = op(j, S[i]);
    }
    fornum(i,0,N){
        printf("%d ", ans[i]);
    }
    return 0;
}


//#pragma GCC optimize("O3")
#include<bits/stdc++.h>

#define ll int
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
enum SegType{SUM,MIN,MAX};
template<typename T>
struct SegTree{
    int num=0, bt;
    vector<T> node, lazy;
    function<T(const T &,const T &)> calc1;
    function<T(const int &, const int &, const int &, const int &, const T &)> calc2;
    function<T(const T &)> calc3;
    T _init;

    void init(int num,T n,SegType st){
        int i = 0, j = 1;
        this->num = num;
        if(st==SegType::MAX){
            //this->_init = LLONG_MIN;
            this->calc1 = [](const T &a, const T &b) { return max(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::MIN){
            //this->_init = LLONG_MAX;
            this->calc1 = [](const T &a, const T &b) { return min(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::SUM){
            //this->_init = 0;
            this->calc1 = [](const T &a,const T &b) { return a + b; };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return (r - l + 1) * n; };
            this->calc3 = [](const T &n) { return n / 2; };
        }
        this->_init = n;

        for (j = 1; j < num;)
            j *= 2;
        this->bt = j - 1;

        node.resize(j * 2 - 1, n);
        lazy.resize(j * 2 - 1, 0);
        /*
        for (int i = 0; i < num; i++){
            node[bt + i] = n;
        }
        for (int i = bt - 1; i >= 0;i--){
            node[i]=calc1(node[i * 2 + 1], node[i * 2 + 2]);
        }*/
    }
    void addd(int x,int y,T n){
        if(x>y)
            return;
        _addd(x, y, n, 0, 0, this->bt);
    }
    void _addd(const int& x,const int& y,const T& n,int p,int l,int r){
        if (r < x || y < l)
            return;
        if(x<=l&&r<=y){
            lazy[p] += calc2(x, y, l, r, n);
            evall(p);
        }else{
            if(lazy[p]!=0)
                evall(p);
            _addd(x, y, n, p * 2 + 1, l, (l + r) / 2);
            _addd(x, y, n, p * 2 + 2, (l + r) / 2 + 1, r);
            node[p] = calc1(node[p * 2 + 1], node[p * 2 + 2]);
        }
    }
    inline void evall(int x){
        node[x] += lazy[x];
        if(x<bt){
            lazy[x * 2 + 1] += calc3(lazy[x]);
            lazy[x * 2 + 2] += calc3(lazy[x]);
        }
        lazy[x] = 0;
    }
    void sett(int x,T n){
        int i = bt+x;
        node[i] = n;
        while(i!=0){
            i = (i - 1) / 2;
            node[i] = calc1(node[i * 2 + 1],node[i * 2 + 2]);
        }
    }
    inline T gett(int x){
        T ret=_init;
        _gett(x, x, 0, 0, this->bt, ret);
        return ret;
    }
    T gett(int x,int y){
        T ret=_init;
        if(x>y)
            return ret;
        _gett(x, y, 0, 0, this->bt, ret);
        return ret;
    }
    void _gett(const int& x,const int& y,int p,int l,int r,T& ret){
        //printf("%d,%d,%d,%d\n", x, y, l, r);
        if(r<x||y<l)
            return;
        if(lazy[p]!=0)
            evall(p);
        if(x<=l&&r<=y){
            ret = calc1(ret, node[p]);
            //printf("%d,%d,%d,%d:%lld\n", x, y, l, r, ret);
            return;
        }
        _gett(x, y, p * 2 + 1, l, (l + r) / 2, ret);
        _gett(x, y, p * 2 + 2, (l + r) / 2 + 1, r, ret);
    }
};

ll N,mx;
char S[1010101];
char A[1010101];
SegTree<ll> sgok,sgnum;
ll ans[1010101];
ll i,j,k;

void addd(ll l,ll r,ll n){
    sgok.addd(l, r, n);
    sgnum.addd(l, r, n);
}

ll op(ll& i, char a){
    if(a=='R'){
        i++;
    }else if(a=='L'){
        if(i>0)
            i--;
    }else if(A[i]!=a){
        ll b = 0;
        if(A[i]=='('){
            b++;
        }else if(A[i]==')'){
            b--;
        }
        if(a=='('){
            b--;
        }else if(a==')'){
            b++;
        }
        addd(0, i, b);
        addd(i + 1, mx, -b);
        A[i] = a;
    }
    //printf("a%lld\n", sg.gett(0));
    if(sgok.gett(0,mx)==0){
        return sgnum.gett(0, mx) / 2;
    }
    return -1;
}

int main(){
    scanf("%d%s", &N,S);
    mx = N + 5;
    sgnum.init(mx + 1, -1e9, SegType::MAX);
    sgok.init(mx + 1, 1e9, SegType::MIN);
    sgnum.addd(0, mx, 1e9);
    sgok.addd(0, mx, -1e9);
    j = 0;
    fornum(i,0,N){
        ans[i] = op(j, S[i]);
    }
    fornum(i,0,N){
        printf("%d ", ans[i]);
    }
    return 0;
}


#pragma GCC optimize("O3")
#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
enum SegType{SUM,MIN,MAX};
template<typename T>
struct SegTree{
    int num=0, bt;
    vector<T> node, lazy;
    function<T(const T &,const T &)> calc1;
    function<T(const int &, const int &, const int &, const int &, const T &)> calc2;
    function<T(const T &)> calc3;
    T _init;

    void init(int num,T n,SegType st){
        int i = 0, j = 1;
        this->num = num;
        if(st==SegType::MAX){
            this->_init = LLONG_MIN;
            this->calc1 = [](const T &a, const T &b) { return max(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::MIN){
            this->_init = LLONG_MAX;
            this->calc1 = [](const T &a, const T &b) { return min(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::SUM){
            this->_init = 0;
            this->calc1 = [](const T &a,const T &b) { return a + b; };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return (r - l + 1) * n; };
            this->calc3 = [](const T &n) { return n / 2; };
        }
        this->_init = n;

        for (j = 1; j < num;)
            j *= 2;
        this->bt = j - 1;

        node.resize(j * 2 - 1, n);
        lazy.resize(j * 2 - 1, 0);
        /*
        for (int i = 0; i < num; i++){
            node[bt + i] = n;
        }
        for (int i = bt - 1; i >= 0;i--){
            node[i]=calc1(node[i * 2 + 1], node[i * 2 + 2]);
        }*/
    }
    void addd(int x,int y,T n){
        if(x>y)
            return;
        _addd(x, y, n, 0, 0, this->bt);
    }
    void _addd(const int& x,const int& y,const T& n,int p,int l,int r){
        if (r < x || y < l)
            return;
        if(x<=l&&r<=y){
            lazy[p] += calc2(x, y, l, r, n);
            evall(p);
        }else{
            if(lazy[p]!=0)
                evall(p);
            _addd(x, y, n, p * 2 + 1, l, (l + r) / 2);
            _addd(x, y, n, p * 2 + 2, (l + r) / 2 + 1, r);
            node[p] = calc1(node[p * 2 + 1], node[p * 2 + 2]);
        }
    }
    inline void evall(int x){
        node[x] += lazy[x];
        if(x<bt){
            lazy[x * 2 + 1] += calc3(lazy[x]);
            lazy[x * 2 + 2] += calc3(lazy[x]);
        }
        lazy[x] = 0;
    }
    void sett(int x,T n){
        int i = bt+x;
        node[i] = n;
        while(i!=0){
            i = (i - 1) / 2;
            node[i] = calc1(node[i * 2 + 1],node[i * 2 + 2]);
        }
    }
    inline T gett(int x){
        T ret=_init;
        _gett(x, x, 0, 0, this->bt, ret);
        return ret;
    }
    T gett(int x,int y){
        T ret=_init;
        if(x>y)
            return ret;
        _gett(x, y, 0, 0, this->bt, ret);
        return ret;
    }
    void _gett(const int& x,const int& y,int p,int l,int r,T& ret){
        //printf("%d,%d,%d,%d\n", x, y, l, r);
        if(r<x||y<l)
            return;
        if(lazy[p]!=0)
            evall(p);
        if(x<=l&&r<=y){
            ret = calc1(ret, node[p]);
            //printf("%d,%d,%d,%d:%lld\n", x, y, l, r, ret);
            return;
        }
        _gett(x, y, p * 2 + 1, l, (l + r) / 2, ret);
        _gett(x, y, p * 2 + 2, (l + r) / 2 + 1, r, ret);
    }
};

ll N,mx;
char S[1010101];
char A[1010101];
SegTree<ll> sgok,sgnum;
ll ans[1010101];
ll i,j,k;

void addd(ll l,ll r,ll n){
    sgok.addd(l, r, n);
    sgnum.addd(l, r, n);
}

ll op(ll& i, char a){
    if(a=='R'){
        i++;
    }else if(a=='L'){
        if(i>0)
            i--;
    }else if(A[i]!=a){
        ll b = 0;
        if(A[i]=='('){
            b++;
        }else if(A[i]==')'){
            b--;
        }
        if(a=='('){
            b--;
        }else if(a==')'){
            b++;
        }
        addd(0, i, b);
        addd(i + 1, mx, -b);
        A[i] = a;
    }
    //printf("a%lld\n", sg.gett(0));
    if(sgok.gett(0,mx)==0){
        return sgnum.gett(0, mx) / 2;
    }
    return -1;
}

int main(){
    scanf("%lld%s", &N,S);
    mx = N + 5;
    sgnum.init(mx + 1, -1e15, SegType::MAX);
    sgok.init(mx + 1, 1e15, SegType::MIN);
    sgnum.addd(0, mx, 1e15);
    sgok.addd(0, mx, -1e15);
    j = 0;
    fornum(i,0,N){
        ans[i] = op(j, S[i]);
    }
    fornum(i,0,N){
        printf("%lld ", ans[i]);
    }
    return 0;
}


#pragma GCC optimize("O3")
#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
enum SegType{SUM,MIN,MAX};
template<typename T>
struct SegTree{
    int num=0, bt;
    vector<T> node, lazy;
    function<T(const T &,const T &)> calc1;
    function<T(const int &, const int &, const int &, const int &, const T &)> calc2;
    function<T(const T &)> calc3;
    T _init;

    void init(int num,T n,SegType st){
        int i = 0, j = 1;
        this->num = num;
        if(st==SegType::MAX){
            this->_init = LLONG_MIN;
            this->calc1 = [](const T &a, const T &b) { return max(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::MIN){
            this->_init = LLONG_MAX;
            this->calc1 = [](const T &a, const T &b) { return min(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::SUM){
            this->_init = 0;
            this->calc1 = [](const T &a,const T &b) { return a + b; };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return (r - l + 1) * n; };
            this->calc3 = [](const T &n) { return n / 2; };
        }
        this->_init = n;

        for (i = 0; j <= num;i++)
            j *= 2;
        bt = j - 1;

        node.resize(j * 2 - 1, n);
        lazy.resize(j * 2 - 1, 0);
        for (int i = 0; i < num; i++){
            node[bt + i] = n;
        }
        for (int i = bt - 1; i >= 0;i--){
            node[i]=calc1(node[i * 2 + 1], node[i * 2 + 2]);
        }
    }
    void addd(int x,int y,T n){
        if(x>y)
            swap(x, y);
        _addd(x, y, n, 0, 0, this->bt);
    }
    void _addd(const int& x,const int& y,const T& n,int p,int l,int r){
        if (r < x || y < l)
            return;
        if(x<=l&&r<=y){
            lazy[p] += calc2(x, y, l, r, n);
            evall(p);
        }else{
            _addd(x, y, n, p * 2 + 1, l, (l + r) / 2);
            _addd(x, y, n, p * 2 + 2, (l + r) / 2 + 1, r);
            node[p] = calc1(node[p * 2 + 1], node[p * 2 + 2]);
        }
    }
    inline void evall(int x){
        node[x] += lazy[x];
        if(x<bt){
            lazy[x * 2 + 1] += calc3(lazy[x]);
            lazy[x * 2 + 2] += calc3(lazy[x]);
        }
        lazy[x] = 0;
    }
    void sett(int x,T n){
        int i = bt+x;
        node[i] = n;
        while(i!=0){
            i = (i - 1) / 2;
            node[i] = calc1(node[i * 2 + 1],node[i * 2 + 2]);
        }
    }
    inline T gett(int x){
        T ret=_init;
        _gett(x, x, 0, 0, this->bt, ret);
        return ret;
    }
    T gett(int x,int y){
        T ret=_init;
        if(x>y)
            swap(x, y);
        _gett(x, y, 0, 0, this->bt, ret);
        return ret;
    }
    void _gett(const int& x,const int& y,int p,int l,int r,T& ret){
        //printf("%d,%d,%d,%d\n", x, y, l, r);
        if(r<x||y<l)
            return;
        if(lazy[p]!=0)
            evall(p);
        if(x<=l&&r<=y){
            ret = calc1(ret, node[p]);
            //printf("%d,%d,%d,%d:%lld\n", x, y, l, r, ret);
            return;
        }
        _gett(x, y, p * 2 + 1, l, (l + r) / 2, ret);
        _gett(x, y, p * 2 + 2, (l + r) / 2 + 1, r, ret);
    }
};

ll N,mx;
char S[1010101];
char A[1010101];
pll cv[1010101];
SegTree<ll> sg;
ll ans[1010101];
ll i,j,k;

ll op(ll& i, char a){
    if(a=='R'){
        i++;
    }else if(a=='L'){
        i--;
    }else{
        if(A[i]=='('){
            sg.addd(0, i, 1);
            sg.addd(i + 1, mx, -1);
        }else if(A[i]==')'){
            sg.addd(0, i, -1);
            sg.addd(i + 1, mx, 1);
        }
        if(a=='('){
            sg.addd(0, i, -1);
            sg.addd(i + 1, mx, 1);
        }else if(a==')'){
            sg.addd(0, i, 1);
            sg.addd(i + 1, mx, -1);
        }
        A[i] = a;
    }
    //printf("a%lld\n", sg.gett(0));
    if(sg.gett(0)==0){
        return sg.gett(0, mx) / 2;
    }
    return -1;
}

int main(){
    scanf("%lld%s", &N,S);
    mx = N + 5;
    sg.init(mx+1, -1e15, SegType::MAX);
    sg.addd(0, mx, 1e15);
    j = 0;
    fornum(i,0,N){
        ans[i] = op(j, S[i]);
        //printf("%lld:%s\n",i, A);
    }
    fornum(i,0,N){
        printf("%lld ", ans[i]);
    }
    return 0;
}


//#pragma GCC optimize("O3")
#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
enum SegType{SUM,MIN,MAX};
template<typename T>
struct SegTree{
    int num=0, bt;
    vector<T> node, lazy;
    function<T(const T &,const T &)> calc1;
    function<T(const int &, const int &, const int &, const int &, const T &)> calc2;
    function<T(const T &)> calc3;
    T _init;

    void init(int num,T n,SegType st){
        int i = 0, j = 1;
        this->num = num;
        if(st==SegType::MAX){
            this->_init = LLONG_MIN;
            this->calc1 = [](const T &a, const T &b) { return max(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::MIN){
            this->_init = LLONG_MAX;
            this->calc1 = [](const T &a, const T &b) { return min(a, b); };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return n; };
            this->calc3 = [](const T &n) { return n; };
        }else if(st==SegType::SUM){
            this->_init = 0;
            this->calc1 = [](const T &a,const T &b) { return a + b; };
            this->calc2 = [](const int &x, const int &y, const int &l, const int &r, const T &n) { return (r - l + 1) * n; };
            this->calc3 = [](const T &n) { return n / 2; };
        }
        this->_init = n;

        for (j = 1; j < num;)
            j *= 2;
        this->bt = j - 1;

        node.resize(j * 2 - 1, n);
        lazy.resize(j * 2 - 1, 0);
        /*
        for (int i = 0; i < num; i++){
            node[bt + i] = n;
        }
        for (int i = bt - 1; i >= 0;i--){
            node[i]=calc1(node[i * 2 + 1], node[i * 2 + 2]);
        }*/
    }
    void addd(int x,int y,T n){
        if(x>y)
            return;
        _addd(x, y, n, 0, 0, this->bt);
    }
    void _addd(const int& x,const int& y,const T& n,int p,int l,int r){
        if (r < x || y < l)
            return;
        if(x<=l&&r<=y){
            lazy[p] += calc2(x, y, l, r, n);
            evall(p);
        }else{
            if(lazy[p]!=0)
                evall(p);
            _addd(x, y, n, p * 2 + 1, l, (l + r) / 2);
            _addd(x, y, n, p * 2 + 2, (l + r) / 2 + 1, r);
            node[p] = calc1(node[p * 2 + 1], node[p * 2 + 2]);
        }
    }
    inline void evall(int x){
        node[x] += lazy[x];
        if(x<bt){
            lazy[x * 2 + 1] += calc3(lazy[x]);
            lazy[x * 2 + 2] += calc3(lazy[x]);
        }
        lazy[x] = 0;
    }
    void sett(int x,T n){
        int i = bt+x;
        node[i] = n;
        while(i!=0){
            i = (i - 1) / 2;
            node[i] = calc1(node[i * 2 + 1],node[i * 2 + 2]);
        }
    }
    inline T gett(int x){
        T ret=_init;
        _gett(x, x, 0, 0, this->bt, ret);
        return ret;
    }
    T gett(int x,int y){
        T ret=_init;
        if(x>y)
            return ret;
        _gett(x, y, 0, 0, this->bt, ret);
        return ret;
    }
    void _gett(const int& x,const int& y,int p,int l,int r,T& ret){
        //printf("%d,%d,%d,%d\n", x, y, l, r);
        if(r<x||y<l)
            return;
        if(lazy[p]!=0)
            evall(p);
        if(x<=l&&r<=y){
            ret = calc1(ret, node[p]);
            //printf("%d,%d,%d,%d:%lld\n", x, y, l, r, ret);
            return;
        }
        _gett(x, y, p * 2 + 1, l, (l + r) / 2, ret);
        _gett(x, y, p * 2 + 2, (l + r) / 2 + 1, r, ret);
    }
};

ll N,mx;
char S[1010101];
char A[1010101];
SegTree<ll> sgok,sgnum;
ll ans[1010101];
ll i,j,k;

void addd(ll l,ll r,ll n){
    sgok.addd(l, r, n);
    sgnum.addd(l, r, n);
}

ll op(ll& i, char a){
    if(a=='R'){
        i++;
    }else if(a=='L'){
        if(i>0)
            i--;
    }else if(A[i]!=a){
        ll b = 0;
        if(A[i]=='('){
            b++;
        }else if(A[i]==')'){
            b--;
        }
        if(a=='('){
            b--;
        }else if(a==')'){
            b++;
        }
        addd(0, i, b);
        addd(i + 1, mx, -b);
        A[i] = a;
    }
    //printf("a%lld\n", sg.gett(0));
    if(sgok.gett(0,mx)==0){
        return sgnum.gett(0, mx) / 2;
    }
    return -1;
}

int main(){
    scanf("%lld%s", &N,S);
    mx = N + 5;
    sgnum.init(mx + 1, -1e15, SegType::MAX);
    sgok.init(mx + 1, 1e15, SegType::MIN);
    sgnum.addd(0, mx, 1e15);
    sgok.addd(0, mx, -1e15);
    j = 0;
    fornum(i,0,N){
        ans[i] = op(j, S[i]);
    }
    fornum(i,0,N){
        printf("%lld ", ans[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v) {
	if (u[v] && (v << 1 | 1) < 4*N) {
		updVertex(v << 1, u[v]);
		updVertex(v << 1 | 1, u[v]);
		u[v] = 0;
	}
}

vertex build(int v, int tl, int tr) {
	if (tl + 1 == tr)
		return t[v] = { M,0 };
	int mid = tl + tr >> 1;
	vertex l = build(v << 1, tl, mid);
	vertex r = build(v << 1 | 1, mid, tr);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,0 };
	if (tl == ql&&tr == qr)
		return updVertex(v, val);
	push(v);
	int mid = tl + tr >> 1;
	vertex l = upd(v << 1, tl, mid, ql, min(mid, qr), val);
	vertex r = upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex get(int v, int tl, int tr, int ql, int qr) {
	if (ql >= qr)
		return{ M,0 };
	if (tl == ql&&tr == qr)
		return t[v];
	push(v);
	int mid = tl + tr >> 1;
	vertex l = get(v << 1, tl, mid, ql, min(mid, qr));
	vertex r = get(v << 1 | 1, mid, tr, max(ql, mid), qr);
	return{ min(l.mn,r.mn),max(l.mx,r.mx) };
}

void solve() {
	int n;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = -1;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		if (str[pos] == '(')
			upd(1, 0, N, pos, N, -1);
		else if (str[pos] == ')')
			upd(1, 0, N, pos, N, 1);

		if (c == '(') {
			upd(1, 0, N, pos, N, 1);
		}
		else if (c == ')') {
			upd(1, 0, N, pos, N, -1);
		}
		str[pos] = c;
		int lastmn = get(1, 0, N, n + 1, n + 3).mn;
		int lastmx = get(1, 0, N, n + 1, n + 3).mx;
		int mn = get(1, 0, N, 0, N).mn;
		int mx = get(1, 0, N, 0, N).mx;
		if (mn < 0 || lastmn != 0 || lastmx != 0)
			ans = -1;
		else
			ans = mx + 1;
		cout << ans << ' ';
	}
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v) {
	if (u[v] && (v << 1 | 1) < 4*N) {
		updVertex(v << 1, u[v]);
		updVertex(v << 1 | 1, u[v]);
		u[v] = 0;
	}
}

vertex build(int v, int tl, int tr) {
	if (tl + 1 == tr)
		return t[v] = { M,-M };
	int mid = tl + tr >> 1;
	vertex l = build(v << 1, tl, mid);
	vertex r = build(v << 1 | 1, mid, tr);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,-M };
	if (tl == ql&&tr == qr)
		return updVertex(v, val);
	push(v);
	int mid = tl + tr >> 1;
	upd(v << 1, tl, mid, ql, min(mid, qr), val);
	upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	t[v].mn = min(t[v << 1].mn, t[v << 1 | 1].mn);
	t[v].mx = max(t[v << 1].mx, t[v << 1 | 1].mx);
	return t[v];
}

vertex get(int v, int tl, int tr, int ql, int qr) {
	if (ql >= qr)
		return{ M,-M };
	if (tl == ql&&tr == qr)
		return t[v];
	push(v);
	int mid = tl + tr >> 1;
	vertex l = get(v << 1, tl, mid, ql, min(mid, qr));
	vertex r = get(v << 1 | 1, mid, tr, max(ql, mid), qr);
	return{ min(l.mn,r.mn),max(l.mx,r.mx) };
}

void solve() {
	int n, cnt = 0;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = 0;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		if (str[pos] == '(')
			upd(1, 0, N, pos, N, -1), cnt--;
		else if (str[pos] == ')')
			upd(1, 0, N, pos, N, 1), cnt++;

		if (c == '(') {
			upd(1, 0, N, pos, N, 1), cnt++;
		}
		else if (c == ')') {
			upd(1, 0, N, pos, N, -1), cnt--;
		}
		str[pos] = c;
		int mn = t[1].mn;
		int mx = t[1].mx;
		if (mn < 0 || cnt)
			ans = -1;
		else
			ans = mx;
		cout << ans << ' ';
	}
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v) {
	if (u[v] && (v << 1 | 1) < 4*N) {
		updVertex(v << 1, u[v]);
		updVertex(v << 1 | 1, u[v]);
		u[v] = 0;
	}
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,-M };
	if (tl == ql&&tr == qr)
		return updVertex(v, val);
	push(v);
	int mid = tl + tr >> 1;
	upd(v << 1, tl, mid, ql, min(mid, qr), val);
	upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	t[v].mn = min(t[v << 1].mn, t[v << 1 | 1].mn);
	t[v].mx = max(t[v << 1].mx, t[v << 1 | 1].mx);
	return t[v];
}

void solve() {
	int n, cnt = 0;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = 0;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		int val = 0;

		if (str[pos] == '(')
			val--, cnt--;
		else if (str[pos] == ')')
			val++, cnt++;

		if (c == '(') {
			val++, cnt++;
		}
		else if (c == ')') {
			val--, cnt--;
		}

		if (val)
			upd(1, 0, N, pos, N, val);
		str[pos] = c;
		int mn = t[1].mn;
		int mx = t[1].mx;
		if (mn < 0 || cnt)
			ans = -1;
		else
			ans = mx;
		cout << ans << ' ';
	}
}//


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v) {
	if (u[v] && (v << 1 | 1) < 4*N) {
		updVertex(v << 1, u[v]);
		updVertex(v << 1 | 1, u[v]);
		u[v] = 0;
	}
}

vertex build(int v, int tl, int tr) {
	if (tl + 1 == tr)
		return t[v] = { M,-M };
	int mid = tl + tr >> 1;
	vertex l = build(v << 1, tl, mid);
	vertex r = build(v << 1 | 1, mid, tr);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,-M };
	if (tl == ql&&tr == qr)
		return updVertex(v, val);
	push(v);
	int mid = tl + tr >> 1;
	upd(v << 1, tl, mid, ql, min(mid, qr), val);
	upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	t[v].mn = min(t[v << 1].mn, t[v << 1 | 1].mn);
	t[v].mx = max(t[v << 1].mx, t[v << 1 | 1].mx);
	return t[v];
}

vertex get(int v, int tl, int tr, int ql, int qr) {
	if (ql >= qr)
		return{ M,-M };
	if (tl == ql&&tr == qr)
		return t[v];
	push(v);
	int mid = tl + tr >> 1;
	vertex l = get(v << 1, tl, mid, ql, min(mid, qr));
	vertex r = get(v << 1 | 1, mid, tr, max(ql, mid), qr);
	return{ min(l.mn,r.mn),max(l.mx,r.mx) };
}

void solve() {
	int n, cnt = 0;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = -1;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		if (str[pos] == '(')
			upd(1, 0, N, pos, N, -1), cnt--;
		else if (str[pos] == ')')
			upd(1, 0, N, pos, N, 1), cnt++;

		if (c == '(') {
			upd(1, 0, N, pos, N, 1), cnt++;
		}
		else if (c == ')') {
			upd(1, 0, N, pos, N, -1), cnt--;
		}
		str[pos] = c;
		int mn = t[1].mn;
		int mx = t[1].mx;
		if (mn < 0 || cnt)
			ans = -1;
		else
			ans = mx;
		cout << ans << ' ';
	}
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int tl, int tr, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v, int tl, int tr) {
	if (u[v] && (v << 1 | 1) < N) {
		int mid = tl + tr >> 1;
		updVertex(v << 1, tl, mid, u[v]);
		updVertex(v << 1 | 1, mid, tr, u[v]);
		u[v] = 0;
	}
}

vertex build(int v, int tl, int tr) {
	if (tl + 1 == tr)
		return t[v] = { M,0 };
	int mid = tl + tr >> 1;
	vertex l = build(v << 1, tl, mid);
	vertex r = build(v << 1 | 1, mid, tr);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,0 };
	if (tl == ql&&tr == qr)
		return updVertex(v, tl, tr, val);
	push(v, tl, tr);
	int mid = tl + tr >> 1;
	vertex l = upd(v << 1, tl, mid, ql, min(mid, qr), val);
	vertex r = upd(v << 1 | 1, mid, qr, max(ql, mid), qr, val);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex get(int v, int tl, int tr, int ql, int qr) {
	if (ql >= qr)
		return{ M,0 };
	if (tl == ql&&tr == qr)
		return t[v];
	push(v, tl, tr);
	int mid = tl + tr >> 1;
	vertex l = get(v << 1, tl, mid, ql, min(mid, qr));
	vertex r = get(v << 1 | 1, mid, tr, max(ql, mid), qr);
	return{ min(l.mn,r.mn),max(l.mx,r.mx) };
}

void solve() {
	int n;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = -1;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		if (str[pos] == '(')
			upd(1, 0, N, pos, N, -1);
		else if (str[pos] == ')')
			upd(1, 0, N, pos, N, 1);

		if (c == '(') {
			upd(1, 0, N, pos, N, 1);
		}
		else if (c == ')') {
			upd(1, 0, N, pos, N, -1);
		}
		int lastmn = get(1, 0, N, n + 1, n + 3).mn;
		int lastmx = get(1, 0, N, n + 1, n + 3).mx;
		int mn = get(1, 0, N, 0, N).mn;
		int mx = get(1, 0, N, 0, N).mx;
		if (mn < 0 || lastmn != 0 || lastmx != 0)
			ans = -1;
		else
			ans = mx + 1;
		cout << ans << ' ';
	}
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v) {
	if (u[v]) {
		updVertex(v << 1, u[v]);
		updVertex(v << 1 | 1, u[v]);
		u[v] = 0;
	}
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,-M };
	if (tl == ql&&tr == qr)
		return updVertex(v, val);
	push(v);
	int mid = tl + tr >> 1;
	upd(v << 1, tl, mid, ql, min(mid, qr), val);
	upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	t[v].mn = min(t[v << 1].mn, t[v << 1 | 1].mn);
	t[v].mx = max(t[v << 1].mx, t[v << 1 | 1].mx);
	return t[v];
}

void solve() {
	int n, cnt = 0;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = 0;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		int val = 0;

		if (str[pos] == '(')
			val--, cnt--;
		else if (str[pos] == ')')
			val++, cnt++;

		if (c == '(') {
			val++, cnt++;
		}
		else if (c == ')') {
			val--, cnt--;
		}

		if (val)
			upd(1, 0, N, pos, N, val);
		str[pos] = c;
		int mn = t[1].mn;
		int mx = t[1].mx;
		if (mn < 0 || cnt)
			ans = -1;
		else
			ans = mx;
		cout << ans << ' ';
	}
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v) {
	if (u[v] && (v << 1 | 1) < 4*N) {
		updVertex(v << 1, u[v]);
		updVertex(v << 1 | 1, u[v]);
		u[v] = 0;
	}
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,-M };
	if (tl == ql&&tr == qr)
		return updVertex(v, val);
	push(v);
	int mid = tl + tr >> 1;
	upd(v << 1, tl, mid, ql, min(mid, qr), val);
	upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	t[v].mn = min(t[v << 1].mn, t[v << 1 | 1].mn);
	t[v].mx = max(t[v << 1].mx, t[v << 1 | 1].mx);
	return t[v];
}

void solve() {
	int n, cnt = 0;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = 0;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		int val = 0;

		if (str[pos] == '(')
			val--, cnt--;
		else if (str[pos] == ')')
			val++, cnt++;

		if (c == '(') {
			val++, cnt++;
		}
		else if (c == ')') {
			val--, cnt--;
		}

		if (val)
			upd(1, 0, N, pos, N, val);
		str[pos] = c;
		int mn = t[1].mn;
		int mx = t[1].mx;
		if (mn < 0 || cnt)
			ans = -1;
		else
			ans = mx;
		cout << ans << ' ';
	}
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(2e5+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int tl, int tr, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v, int tl, int tr) {
	if (u[v] && (v << 1 | 1) < N) {
		int mid = tl + tr >> 1;
		updVertex(v << 1, tl, mid, u[v]);
		updVertex(v << 1 | 1, mid, tr, u[v]);
		u[v] = 0;
	}
}

vertex build(int v, int tl, int tr) {
	if (tl + 1 == tr)
		return t[v] = { M,0 };
	int mid = tl + tr >> 1;
	vertex l = build(v << 1, tl, mid);
	vertex r = build(v << 1 | 1, mid, tr);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,0 };
	push(v, tl, tr);
	if (tl == ql&&tr == qr)
		return updVertex(v, tl, tr, val);
	int mid = tl + tr >> 1;
	vertex l = upd(v << 1, tl, mid, ql, min(mid, qr), val);
	vertex r = upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex get(int v, int tl, int tr, int ql, int qr) {
	if (ql >= qr)
		return{ M,0 };
	push(v, tl, tr);
	if (tl == ql&&tr == qr)
		return t[v];
	int mid = tl + tr >> 1;
	vertex l = get(v << 1, tl, mid, ql, min(mid, qr));
	vertex r = get(v << 1 | 1, mid, tr, max(ql, mid), qr);
	return{ min(l.mn,r.mn),max(l.mx,r.mx) };
}

void solve() {
	int n;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = -1;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		if (str[pos] == '(')
			upd(1, 0, N, pos, N, -1);
		else if (str[pos] == ')')
			upd(1, 0, N, pos, N, 1);

		if (c == '(') {
			upd(1, 0, N, pos, N, 1);
		}
		else if (c == ')') {
			upd(1, 0, N, pos, N, -1);
		}
		str[pos] = c;
		int lastmn = get(1, 0, N, n + 1, n + 3).mn;
		int lastmx = get(1, 0, N, n + 1, n + 3).mx;
		int mn = get(1, 0, N, 0, N).mn;
		int mx = get(1, 0, N, 0, N).mx;
		if (mn < 0 || lastmn != 0 || lastmx != 0)
			ans = -1;
		else
			ans = mx + 1;
		cout << ans << ' ';
	}
}


#include <bits/stdc++.h>
#include <unordered_map>

using namespace std;

inline void solve();

//#define FILE

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	int t;
	cin >> t;
	while (t--) {
		auto time = clock();
		solve();
		printf("\n\n%f\n\n", 1.*(clock() - time) / CLOCKS_PER_SEC);
	}
#else
#ifdef FILE
	freopen(".in", "rt", stdin);
	freopen(".out", "wt", stdout);
#endif
	solve();
#endif
}

typedef unsigned int ui;
typedef long long ll;
typedef long double ld;
#define double ld
//#define int ll
#define INF int(1e18)
#define MOD int(1e9+7)
#define EPS 1e-7
#define N int(1e6+5)
#define M int(1e9+5)

int a[N], u[4 * N];
struct vertex {
	int mn, mx;
};

vertex t[4 * N];

vertex updVertex(int v, int val) {
	u[v] += val;
	t[v].mn += val;
	t[v].mx += val;
	return t[v];
}

void push(int v) {
	if (u[v] && (v << 1 | 1) < 4*N) {
		updVertex(v << 1, u[v]);
		updVertex(v << 1 | 1, u[v]);
		u[v] = 0;
	}
}

vertex build(int v, int tl, int tr) {
	if (tl + 1 == tr)
		return t[v] = { M,0 };
	int mid = tl + tr >> 1;
	vertex l = build(v << 1, tl, mid);
	vertex r = build(v << 1 | 1, mid, tr);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex upd(int v, int tl, int tr, int ql, int qr, int val) {
	if (ql >= qr)
		return{ M,0 };
	push(v);
	if (tl == ql&&tr == qr)
		return updVertex(v, val);
	int mid = tl + tr >> 1;
	vertex l = upd(v << 1, tl, mid, ql, min(mid, qr), val);
	vertex r = upd(v << 1 | 1, mid, tr, max(ql, mid), qr, val);
	return t[v] = { min(l.mn,r.mn),max(l.mx,r.mx) };
}

vertex get(int v, int tl, int tr, int ql, int qr) {
	if (ql >= qr)
		return{ M,0 };
	push(v);
	if (tl == ql&&tr == qr)
		return t[v];
	int mid = tl + tr >> 1;
	vertex l = get(v << 1, tl, mid, ql, min(mid, qr));
	vertex r = get(v << 1 | 1, mid, tr, max(ql, mid), qr);
	return{ min(l.mn,r.mn),max(l.mx,r.mx) };
}

void solve() {
	int n;
	string s;
	cin >> n >> s;
	string str(n, ' ');
	int pos = 0;
	int ans = -1;
	for (auto c : s) {
		if (c == 'L') {
			if (pos)
				pos--;
			cout << ans << ' ';
			continue;
		}
		if (c == 'R') {
			pos++;
			cout << ans << ' ';
			continue;
		}

		if (str[pos] == '(')
			upd(1, 0, N, pos, N, -1);
		else if (str[pos] == ')')
			upd(1, 0, N, pos, N, 1);

		if (c == '(') {
			upd(1, 0, N, pos, N, 1);
		}
		else if (c == ')') {
			upd(1, 0, N, pos, N, -1);
		}
		str[pos] = c;
		int lastmn = get(1, 0, N, n + 1, n + 3).mn;
		int lastmx = get(1, 0, N, n + 1, n + 3).mx;
		int mn = get(1, 0, N, 0, N).mn;
		int mx = get(1, 0, N, 0, N).mx;
		if (mn < 0 || lastmn != 0 || lastmx != 0)
			ans = -1;
		else
			ans = mx + 1;
		cout << ans << ' ';
	}
}


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 2000006;

int pos, n, l = N - 6;
int d[4 * N];
char a[N];
pair<int, int> t[4 * N];

int getVal(char x)
{
    if (x == '(')
        return 1;
    if (x == ')')
        return -1;
    return 0;
}

void push(int v, int l, int r, int f = 1)
{
    if (d[v] == 0)
        return;
    if (l != r)
    {
        d[v * 2] += d[v];
        d[v * 2 + 1] += d[v];
        if (f)
        {
            int m = (l + r) / 2;
            push(v * 2, l, m, 0);
            push(v * 2 + 1, m + 1, r, 0);
        }
    }
    t[v].first += d[v];
    t[v].second += d[v];
    d[v] = 0;
}

pair<int, int> merge(pair<int, int> a, pair<int, int> b)
{
    return MP(max(a.first, b.first), min(a.second, b.second));
}

void add(int v, int l, int r, int i, int j, int val)
{
    push(v, l, r);
    if (i > j)
        return;
    if (l == i && r == j)
    {
        d[v] += val;
        push(v, l, r);
        return;
    }
    int m = (l + r) / 2;
    add(v * 2, l, m, i, min(j, m), val);
    add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
    t[v] = merge(t[v * 2], t[v * 2 + 1]);
}

pair<int, int> get(int v, int l, int r, int i, int j)
{
    push(v, l, r);
    if (i > j)
        return MP(-mod, mod);
    if (l == i && r == j)
        return t[v];
    int m = (l + r) / 2;
    return merge(
        get(v * 2, l, m, i, min(j, m)),
        get(v * 2 + 1, m + 1, r, max(m + 1, i), j));
}

int main()
{
    pos = 1;
    cin >> n;
    for (int i = 1; i <= l; i++)
        a[i] = 'a';
    while (n--)
    {
        char x;
        scanf(" %c", &x);
        if (x == 'L')
            pos--;
        else if (x == 'R')
            pos++;
        else
        {
            add(1, 1, l, pos, l, -getVal(a[pos]));
            a[pos] = x;
            add(1, 1, l, pos, l, getVal(a[pos]));
        }
        pair<int, int> p = get(1, 1, l, 1, l);
        if (p.second >= 0 && get(1, 1, l, l, l).first == 0)
            printf("%d ", p.first);
        else
            printf("-1 ");
    }
    cout << endl;

    return 0;
}

/*

4
a(R)

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 1000006;

int pos, n;
int d[4 * N];
char a[N];
pair<int, int> t[4 * N];

int getVal(char x)
{
    if (x == '(')
        return 1;
    if (x == ')')
        return -1;
    return 0;
}

void push(int v, int l, int r, int f = 1)
{
    if (d[v] == 0)
        return;
    if (l != r)
    {
        d[v * 2] += d[v];
        d[v * 2 + 1] += d[v];
        if (f)
        {
            int m = (l + r) / 2;
            push(v * 2, l, m, 0);
            push(v * 2 + 1, m + 1, r, 0);
        }
    }
    t[v].first += d[v];
    t[v].second += d[v];
    d[v] = 0;
}

pair<int, int> merge(pair<int, int> a, pair<int, int> b)
{
    return MP(max(a.first, b.first), min(a.second, b.second));
}

void add(int v, int l, int r, int i, int j, int val)
{
    assert(v <= 4 * n);
    push(v, l, r);
    if (i > j)
        return;
    if (l == i && r == j)
    {
        d[v] += val;
        push(v, l, r);
        return;
    }
    int m = (l + r) / 2;
    add(v * 2, l, m, i, min(j, m), val);
    add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
    t[v] = merge(t[v * 2], t[v * 2 + 1]);
}

pair<int, int> get(int v, int l, int r, int i, int j)
{
    push(v, l, r);
    if (i > j)
        return MP(-mod, mod);
    if (l == i && r == j)
        return t[v];
    int m = (l + r) / 2;
    return merge(
        get(v * 2, l, m, i, min(j, m)),
        get(v * 2 + 1, m + 1, r, max(m + 1, i), j));
}

int main()
{
    pos = 1;
    cin >> n;
    for (int i = 1; i <= n; i++)
        a[i] = 'a';
    for (int i = 1; i <= n; i++)
    {
        char x;
        scanf(" %c", &x);
        if (x == 'L')
            pos--;
        else if (x == 'R')
            pos++;
        else
        {
            add(1, 1, n, pos, n, getVal(x) - getVal(a[pos]));
            a[pos] = x;
        }

        pair<int, int> p = get(1, 1, n, 1, n);
        if (p.second >= 0 && get(1, 1, n, n, n).first == 0)
            printf("%d ", p.first);
        else
            printf("-1 ");
    }
    cout << endl;

    return 0;
}

/*

5
RRRR)

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 1000006;

int pos, n;
int d[4 * N];
char a[N];
pair<int, int> t[4 * N];

int getVal(char x)
{
	if (x == '(')
		return 1;
	if (x == ')')
		return -1;
	return 0;
}

void push(int v, int l, int r, int f = 1)
{
	if (d[v] == 0)
		return;
	if (l != r)
	{
		d[v * 2] += d[v];
		d[v * 2 + 1] += d[v];
		if (f)
		{
			int m = (l + r) / 2;
			push(v * 2, l, m, 0);
			push(v * 2 + 1, m + 1, r, 0);
		}
	}
	t[v].first += d[v];
	t[v].second += d[v];
	d[v] = 0;
}

pair<int, int> merge(pair<int, int> a, pair<int, int> b)
{
	return MP(max(a.first, b.first), min(a.second, b.second));
}

void add(int v, int l, int r, int i, int j, int val)
{
	push(v, l, r);
	if (i > j)
		return;
	if (l == i && r == j)
	{
		d[v] += val;
		push(v, l, r);
		return;
	}
	int m = (l + r) / 2;
	add(v * 2, l, m, i, min(j, m), val);
	add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
	t[v] = merge(t[v * 2], t[v * 2 + 1]);
}

pair<int, int> get(int v, int l, int r, int i, int j)
{
	push(v, l, r);
	if (i > j)
		return MP(-mod, mod);
	if (l == i && r == j)
		return t[v];
	int m = (l + r) / 2;
	return merge(
		get(v * 2, l, m, i, min(j, m)),
		get(v * 2 + 1, m + 1, r, max(m + 1, i), j));
}
char X[N];
int main()
{
	pos = 1;
	cin >> n;
	scanf("%s", X);
	for (int i = 1; i <= n; i++)
		a[i] = 'a';
	for (int i = 1; i <= n; i++)
	{
		char x = X[i - 1];
		if (x == 'L')
		{
			if (pos > 1)
				pos--;
		}
		else if (x == 'R')
			pos++;
		else
		{
			add(1, 1, n, pos, n, getVal(x) - getVal(a[pos]));
			a[pos] = x;
		}

		pair<int, int> p = get(1, 1, n, 1, n);

		if (p.second >= 0 && get(1, 1, n, n, n).first == 0)
			printf("%d ", p.first);
		else
			printf("-1 ");
	}
	cout << endl;

	return 0;
}

/*

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 1000006;

int pos, n, maxn = N - 2;
int d[4 * N];
char a[N];
pair<int, int> t[4 * N];

int getVal(char x)
{
    if (x == '(')
        return 1;
    if (x == ')')
        return -1;
    return 0;
}

void push(int v, int l, int r, int f = 1)
{
    if (v > 4 * maxn)
    {
        cout << v << endl;
        return;
    }
    if (d[v] == 0)
        return;
    if (l != r)
    {
        d[v * 2] += d[v];
        d[v * 2 + 1] += d[v];
        if (f)
        {
            int m = (l + r) / 2;
            push(v * 2, l, m, 0);
            push(v * 2 + 1, m + 1, r, 0);
        }
    }
    t[v].first += d[v];
    t[v].second += d[v];
    d[v] = 0;
}

pair<int, int> merge(pair<int, int> a, pair<int, int> b)
{
    return MP(max(a.first, b.first), min(a.second, b.second));
}

void add(int v, int l, int r, int i, int j, int val)
{
    push(v, l, r);
    if (i > j)
        return;
    if (l == i && r == j)
    {
        d[v] += val;
        push(v, l, r);
        return;
    }
    int m = (l + r) / 2;
    add(v * 2, l, m, i, min(j, m), val);
    add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
    t[v] = merge(t[v * 2], t[v * 2 + 1]);
}

pair<int, int> get(int v, int l, int r, int i, int j)
{
    push(v, l, r);
    if (i > j)
        return MP(-mod, mod);
    if (l == i && r == j)
        return t[v];
    int m = (l + r) / 2;
    return merge(
        get(v * 2, l, m, i, min(j, m)),
        get(v * 2 + 1, m + 1, r, max(m + 1, i), j));
}

int main()
{
    pos = 1;
    cin >> n;
    for (int i = 1; i <= maxn; i++)
        a[i] = 'a';
    while (n--)
    {
        char x;
        scanf(" %c", &x);
        if (x == 'L')
            pos--;
        else if (x == 'R')
            pos++;
        else
        {
            add(1, 1, maxn, pos, maxn, getVal(x) - getVal(a[pos]));
            a[pos] = x;
        }

        pair<int, int> p = get(1, 1, maxn, 1, maxn);
        if (p.second >= 0 && get(1, 1, maxn, maxn, maxn).first == 0)
            printf("%d ", p.first);
        else
            printf("-1 ");
    }
    cout << endl;

    return 0;
}

/*

5
RRRR)

*/


/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int N = 1000006;

int pos, n, l = N - 3;
int d[4 * N];
char a[N];
pair<int, int> t[4 * N];

int getVal(char x)
{
    if (x == '(')
        return 1;
    if (x == ')')
        return -1;
    return 0;
}

void push(int v, int l, int r, int f = 1)
{
    if (d[v] == 0)
        return;
    if (l != r)
    {
        d[v * 2] += d[v];
        d[v * 2 + 1] += d[v];
        if (f)
        {
            int m = (l + r) / 2;
            push(v * 2, l, m, 0);
            push(v * 2 + 1, m + 1, r, 0);
        }
    }
    t[v].first += d[v];
    t[v].second += d[v];
    d[v] = 0;
}

pair<int, int> merge(pair<int, int> a, pair<int, int> b)
{
    return MP(max(a.first, b.first), min(a.second, b.second));
}

void add(int v, int l, int r, int i, int j, int val)
{
    push(v, l, r);
    if (i > j)
        return;
    if (l == i && r == j)
    {
        d[v] += val;
        push(v, l, r);
        return;
    }
    int m = (l + r) / 2;
    add(v * 2, l, m, i, min(j, m), val);
    add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
    t[v] = merge(t[v * 2], t[v * 2 + 1]);
}

pair<int, int> get(int v, int l, int r, int i, int j)
{
    push(v, l, r);
    if (i > j)
        return MP(-mod, mod);
    if (l == i && r == j)
        return t[v];
    int m = (l + r) / 2;
    return merge(
        get(v * 2, l, m, i, min(j, m)),
        get(v * 2 + 1, m + 1, r, max(m + 1, i), j));
}

int main()
{
    pos = 1;
    cin >> n;
    for (int i = 1; i <= l; i++)
        a[i] = 'a';
    while (n--)
    {
        char x;
        scanf(" %c", &x);
        if (x == 'L')
            pos--;
        else if (x == 'R')
            pos++;
        else
        {
            add(1, 1, l, pos, l, -getVal(a[pos]));
            a[pos] = x;
            add(1, 1, l, pos, l, getVal(a[pos]));
        }
        pair<int, int> p = get(1, 1, l, 1, l);
        if (p.second >= 0 && get(1, 1, l, l, l).first == 0)
            printf("%d ", p.first);
        else
            printf("-1 ");
    }
    cout << endl;

    return 0;
}

/*

4
a(R)

*/