#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
map<vector<string>, int> mp = {
{{"..#####..", ".##...##.", "##.....##", "##.....##", "##.....##", ".##...##.", "..#####..", "........."}, 0},
{{"....##...", "..####...", "....##...", "....##...", "....##...", "....##...", "..######.", "........."}, 1},
{{".#######.", "##.....##", ".......##", ".#######.", "##.......", "##.......", "#########", "........."}, 2},
{{".#######.", "##.....##", ".......##", ".#######.", ".......##", "##.....##", ".#######.", "........."}, 3},
{{"##.......", "##....##.", "##....##.", "##....##.", "#########", "......##.", "......##.", "........."}, 4},
{{".########", ".##......", ".##......", ".#######.", ".......##", ".##....##", "..######.", "........."}, 5},
{{".#######.", "##.....##", "##.......", "########.", "##.....##", "##.....##", ".#######.", "........."}, 6},
{{".########", ".##....##", ".....##..", "....##...", "...##....", "...##....", "...##....", "........."}, 7},
{{".#######.", "##.....##", "##.....##", ".#######.", "##.....##", "##.....##", ".#######.", "........."}, 8},
{{".#######.", "##.....##", "##.....##", ".########", ".......##", "##.....##", ".#######.", "........."}, 9}
};
void solve(void) {
vector<string> v(8);
for (int i=0; i<8; i++) cin >> v[i];
cout << mp[v];
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
while (n--) solve();
return 0;
}