SCUSA Region ACM Balloon Logo ICPC Masthead
2008 South Central USA Regional Programming Contest

Another Brick in the Wall

Introduction

After years as a brick-layer, you've been called upon to analyze the structural integrity of various brick walls built by the Tetrad Corporation. Instead of using regular-sized bricks, the Tetrad Corporation seems overly fond of bricks made out of strange shapes. The structural integrity of a wall can be approximated by the fewest number of bricks that could be removed to create a gap from the top to the bottom. Can you determine that number for various odd walls created by Tetrad?

Input

Input to this problem will begin with a line containing a single integer X (1 ≤ X ≤ 100) indicating the number of data sets. Each data set consists of two components:

Output

For each data set, output the fewest number of bricks to remove to create a gap that leads from some point at the top of the wall, to some point at the bottom of the wall. Assume that bricks are in fixed locations and do not "fall" if bricks are removed from beneath them. A gap consists of contiguous units of removed bricks; each unit of a gap must be adjacent (diagonals do not count) to another unit of the gap.

Sample Input

3
5 7
AABBCCD
EFFGGHH
IIJJKKL
MNNOOPP
QQRRSST
5 7
AABBCCD
AFFBGGD
IIJBKKD
MNNOOPD
QQRRSST
6 7
ABCDEAB
ABCFEAB
AEAABAB
ACDAEEB
FFGAHIJ
KLMANOP
Sample Output
5
2
2