#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define maxx 30
int color[ maxx] ;
vector< int > G[ maxx] ;
void clear( )
{
for ( int i= 0 ; i< maxx; i++ )
G[ i] .clear ( ) ;
memset ( color,- 1 ,sizeof color) ;
}
void dfs( int s,int col)
{
color[ s] = col;
for ( int i= 0 ; i< G[ s] .size ( ) ; i++ )
{
if ( color[ G[ s] [ i] ] == - 1 )
{
dfs( G[ s] [ i] ,col) ;
}
}
}
int main( )
{
int t;
scanf ( "%d" ,& t) ;
scanf ( "\n " ) ;
while ( t-- )
{
clear( ) ;
char x;
scanf ( "%c" ,& x) ;
scanf ( "\n " ) ;
int n= ( int ) ( x- 'A' ) ;
string s;
while ( ( cin >> s) )
{
G[ s[ 0 ] - 'A' ] .pb ( s[ 1 ] - 'A' ) ;
G[ s[ 1 ] - 'A' ] .pb ( s[ 0 ] - 'A' ) ;
}
int connect= 0 ;
for ( int i= 0 ; i<= n; i++ )
{
if ( color[ i] == - 1 )
dfs( i,++ connect) ;
}
cout << connect<< endl;
}
return 0 ;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtYXh4IDMwCmludCBjb2xvclttYXh4XTsKdmVjdG9yPGludD5HW21heHhdOwp2b2lkIGNsZWFyKCkKewogICBmb3IoaW50IGk9MDtpPG1heHg7aSsrKQogICAgIEdbaV0uY2xlYXIoKTsKICAgbWVtc2V0KGNvbG9yLC0xLHNpemVvZiBjb2xvcik7ICAKfQp2b2lkIGRmcyhpbnQgcyxpbnQgY29sKQp7CiAgICBjb2xvcltzXT1jb2w7CiAgICBmb3IoaW50IGk9MDtpPEdbc10uc2l6ZSgpO2krKykKICAgIHsgIAogICAgICAgIGlmKGNvbG9yW0dbc11baV1dPT0tMSkKICAgICAgICB7ICAKICAgICAgICAgICAgZGZzKEdbc11baV0sY29sKTsKICAgICAgICB9CiAgICB9Cn0KaW50IG1haW4oKQp7CiAgICBpbnQgdDsKICAgIHNjYW5mKCIlZCIsJnQpOwogICAgc2NhbmYoIlxuIik7CiAgICB3aGlsZSh0LS0pCiAgICB7ICAgCiAgICAJY2xlYXIoKTsKICAgICAgICBjaGFyIHg7CiAgICAgICAgc2NhbmYoIiVjIiwmeCk7CiAgICAgICAgc2NhbmYoIlxuIik7CiAgICAgICAgaW50IG49KGludCkoeC0nQScpOwogICAgICAgIHN0cmluZyBzOwogICAgICAgIHdoaWxlKChjaW4+PnMpKQogICAgICAgIHsgIAogICAgICAgICAgICBHW3NbMF0tJ0EnXS5wYihzWzFdLSdBJyk7CiAgICAgICAgICAgIEdbc1sxXS0nQSddLnBiKHNbMF0tJ0EnKTsKICAgICAgICB9CiAgICAgICAgaW50IGNvbm5lY3Q9MDsKICAgICAgICBmb3IoaW50IGk9MDtpPD1uO2krKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKGNvbG9yW2ldPT0tMSkKICAgICAgICAgICAgICBkZnMoaSwrK2Nvbm5lY3QpOwogICAgICAgIH0KICAgICAgICBjb3V0PDxjb25uZWN0PDxlbmRsOwogICAgfQogICAgcmV0dXJuIDA7Cn0K
stdin
NwoKRQpBQgpDRQpEQgpFQwoKRwpBQgpDRQpEQgpFQwpFRgpHRwoKWgpBQwpBRApBRQpBSQpBTApBTQpBTgpBTwpBUQpBUwpBVgpBVwpBWApBWQpCRgpCSApCSgpCSwpCTApCTQpCTgpCTwpCUApCUQpCUgpCVQpCVgpCVwpCWQpDRQpDRwpDSQpDSgpDTQpDUApDVApDVQpDVgpDWQpERgpERwpESgpESwpETApETQpETgpETwpEUQpEVgpEWApFRgpFSApFSwpFTApFTQpFTgpFTwpFUgpFUwpFVApFVQpFWQpFWgpGRwpGSApGSQpGSgpGTQpGTgpGTwpGUApGUgpGVQpGVwpGWQpGWgpHSApHTApHTQpHUApHUQpHUgpHUwpHWgpISQpISgpISwpITApIUQpIUgpIUwpIVgpIVwpIWQpJSwpJTQp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