import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class Main {
    static void solve() {
        String s = in.next();
        int n = s.length();
        int ans = 0, m = 0;
        int[] b = new int[4];
        for (int i = 0; i < n; i++) {
            m = s.charAt(i) - '0';
            if (m != 0 && m % 3 == 0) {
                b[3]++;
            }
            else{
                b[m % 3]++;
            }
        }
        m = 0;
        for (int i = 1; i < 4; ++i) {
            m += i * b[i];
        }
        if (m % 3 != 0) {
            if (b[m % 3] == 0) {
                out.println(0);
                return;
            }
            ++ans;
            --b[m % 3];
        }
        if (b[m % 3] == 0 && (s.charAt(0) - '0') % 3 == m % 3) {
            if (b[0] == n - 1) {
                out.println(0);
                return;
            }
            for (int i = 1; i < n && s.charAt(i) == '0'; ++i) {
                --b[0];
            }
            if (b[0] == 0 && b[1] == 0 && b[2] == 0) {
                --ans;
            }
        }
        ans += b[0] + b[3];
        ans = Math.min(ans,n-1);
        out.println(ans);
    }

    public static void main(String[] args) {
        int T = in.nextInt();
        while (T-- > 0) {
            solve();
        }
        out.flush();
    }

    static FastReader in = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    static class FastReader {
        static BufferedReader br;
        static StringTokenizer st;

        FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            String str = "";
            while (st == null || !st.hasMoreElements()) {
                try {
                    str = br.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                st = new StringTokenizer(str);
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}