#include<bits/stdc++.h> //Mr_Mandal
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
//Extracting floor number from room number
int floorNumber(string roomNumber)
{
return int(roomNumber[4]) - 48;
}
//Calculating minimum needed time between two rooms
int calculateLiftTime(string roomNum1, string roomNum2)
{
int ans = 0;
int floorNum1 = floorNumber(roomNum1);
int floorNum2 = floorNumber(roomNum2);
//When both rooms are in the same Building
if(roomNum1[0] == roomNum2[0])
{
ans = abs(floorNum1 - floorNum2);
}
//When rooms are in different buildings
else
{
int timeWithBridge = 0;
int timeWithPlaaza = 0;
//Calculation if Baba takes the bridge on 5th floor
timeWithBridge += abs(5-floorNum1);
timeWithBridge += abs(5-floorNum2);
//Calculation if Baba takes the plaaza on 1st floor
timeWithPlaaza += abs(1-floorNum1);
timeWithPlaaza += abs(1-floorNum2);
//Taking the minimum time as Baba is Efficient
ans = min(timeWithBridge, timeWithPlaaza);
}
return ans;
}
void solve()
{
int n;
cin >> n;
cin.ignore(); //flushing End Of Line
string firstRoom;
getline(cin, firstRoom);
n--; //Decreasing total class number by 1
int ans = 0; //Total Time
//Adding the time to getting into the first classroom
ans += abs(1 - floorNumber(firstRoom));
string prevRoom = firstRoom;
while(n--)
{
string currRoom;
getline(cin, currRoom);
ans += calculateLiftTime(prevRoom, currRoom);
//Keeping a track of previous classroom number
prevRoom = currRoom;
}
//Adding the time to getting down first floor
ans += abs(1 - floorNumber(prevRoom));
cout<<ans<<"\n";
}
int main()
{
int t;
cin >> t;
while(t--)
solve();
return 0;
}