F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Rikka with Mutex

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 347    Accepted Submission(s): 138


Problem Description
Sometimes, technical terms implicate some life philosophy. Mutex is one of them. On your way to dream, you may be locked by some difficulties, and you need someone to stop his step, and help you get through them.

To help you know better about the life philosophy inside mutex, Rikka comes up with a simple task. Maybe some of you know little about mutex, so she uses another scene to replace it.

There are $n$ gates in a row, several people in the left side of the gates and all of them want to go to the right side. There are two kinds of gates: black and white. These people share energy, which is represented by a non-negative number $E$. Initially, $E =0 $.

If one person walks through a white gate, he will gain one point of energy, i.e., $E$ will be added by $1$. And if one person walks through a black gate, he will lose one point of energy, i.e., $E$ will be subtracted by $1$. Since $E$ must be a non-negative integer, if $E=0$, no one can walk through a black gate until someone walks through a white gate. You can assume there won't be two people moving at the same time and all the people are selfless.

We use P to represent a black gate, V to represent a white gate and use a PV string to represent the row. Initially, all the people are at the beginning of the string, and all of them want to go through the whole string. But unfortunately, sometimes it may be impossible. So, they want to send at least one person to the right side.

Your task is to find out the minimal number of people which this group needs to achieve this goal.

For example, if the row is VPP, they need at least two people: The first person walk through the first white gate and the second person can use this point of energy to go through the whole string.
 

Input
The first line contains a single numner $t(1 \leq t \leq 10^3)$, the number of the testcases.

For each testcase, the first line contains a PV string $s(1 \leq |s| \leq 10^5)$ describing the gates.

The input guarantees that there are at most $30$ testcases with $|S| > 1000$.
 

Output
For each testcase, output a single integer, the answer. And if it is impossible, output $-1$.

 

Sample Input
4 VPP VPPVVVVPPPPPPPP VPPPPPPPPPPPPPP P
 

Sample Output
2 3 14 -1
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 22:53:37, Gzip enabled