#include "bits/stdc++.h"

using namespace std;
#define int long long
#define endl "\n"
#define PII pair<int,int>
#define PIII pair<int,PII>
const int MOD = 1e9 + 7;
const int N = 3e5;

void slu() {
    string t;
    cin >> t;
    int cnt = 0;
    int sum = 0;
    for (int i = t.size() - 1; i >= 0; i--) {
        sum += t[i] - '0';
    }
    auto it = t.end();
    it--;
    while (it != t.begin()) {
        int k = *it - '0';
        if ((sum - k) % 3 == 0) {
            t.erase(it);
            while (*t.begin() == '0')t.erase(t.begin());
            if (t.empty())break;

            sum -= k;
            cnt++;
            it = --t.end();
        } else {
            it--;
            if (it == t.begin()) {
                k = *it - '0';
                if ((sum - k) % 3 == 0) {
                    t.erase(it);
                    while (*t.begin() == '0')t.erase(t.begin());
                    if (t.empty())break;
                    sum -= k;
                    cnt++;
                    it = --t.end();
                }
            }
        }
    }
//    if (t.empty())cnt--;
    cout << cnt << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
//    T = 1;
    while (T--)slu();

}