← Home
Looking at this problem, I need to find all strings t ≠ "a" such that string s can be partitioned into substrings where each is either "a" or t, and at least one substring equals t.

Let me think about this carefully.

First, if s consists entirely of 'a's, then t must be some string of 'a's (like "aa", "aaa", etc.) that divides s. For s = "aaaa" (length 4), t could be "aa", "aaa", "aaaa" - wait, "aa" works: "aa"+"aa", "aaa" doesn't partition "aaaa" well... actually "a"+"aaa" works. And "aaaa" works. So for all-a strings, t = "a"^k where k divides... no, we can mix "a" and t.

Let me reconsider. If s has length n, and s = a^{p1} t a^{p2} t ... a^{pm} t a^{p_{m+1}} where m ≥ 1 occurrences of t appear, separated by runs of 'a's (possibly zero 'a's).

So t appears at certain non-overlapping positions in s, and everything between (and before/after) is all 'a's.

Key insight: t must be a substring that, when we remove all occurrences of t from s (placed contiguously to cover s), the remaining characters are all 'a'.

Let me think about this differently. For a candidate t of length L, we greedily scan s: if the next L characters match t, consume them. Otherwise, if the current character is 'a', consume it. Otherwise fail. We need at least one t used.

Given the constraints (|s| up to 2×10^5, sum up to 3×10^5), I need an efficient approach.

For each possible length L of t, and each possible starting occurrence pattern, I need to figure out valid t's.

Let me think about it by considering all possible ways t can appear. The non-'a' characters in s must all be covered by copies of t. So let me find the positions of non-'a' characters. If all characters are 'a', it's a special case.

For the non-all-a case: the non-'a' characters must be covered by t. So t's placements must cover all non-'a' positions. Between t placements, there can be any number of 'a's.

I'll try a different approach: enumerate all possible lengths L for t, and for each L, determine how many distinct valid t's exist.

Given the complexity, I'll implement a solution that for each possible t-length, tries to determine valid t strings by greedily parsing s.

Let me implement a hashing-based approach:

For each candidate length L (1 to |s|), greedily parse s: skip 'a's, and when hitting a non-'a' character, the next L characters must be t. Collect all these substrings and check they're identical. If valid and t ≠ "a", count it.

For the all-'a' case, any length L from 2 to n works if we can partition: s has length n, we need n = k*L + (n - k*L) where remaining are single 'a's, so n - k*L ≥ 0 and k ≥ 1. Any L from 1 to n works, and t = "aaa...a" (L a's). Excluding t="a", that's L=2..n, giving n-1 answers.

For the non-all-a case with the greedy approach, for each L, the positions of t are forced (greedily). Then check all extracted substrings match. Use hashing for O(1) comparison.

But L ranges up to n, so worst case O(n^2). However, the non-'a' characters constrain things: if there are m non-'a' chars, the number of valid lengths might be limited.

Actually worst case is still O(n^2). Let me just implement it carefully with hashing and hope it passes within time.

<solution>

```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func solve(s string) int {
	n := len(s)
	// Find positions of non-'a' characters
	nonA := []int{}
	for i := 0; i < n; i++ {
		if s[i] != 'a' {
			nonA = append(nonA, i)
		}
	}
	if len(nonA) == 0 {
		return n - 1
	}

	ans := 0
	// For each length L, greedily determine t
	for L := 1; L <= n; L++ {
		// Greedy parse
		valid := true
		tUsed := 0
		tStart := -1
		i := 0
		for i < n {
			if s[i] == 'a' {
				i++
			} else {
				if i+L > n {
					valid = false
					break
				}
				if tStart == -1 {
					tStart = i
				} else {
					if s[tStart:tStart+L] != s[i:i+L] {
						valid = false
						break
					}
				}
				tUsed++
				i += L
			}
		}
		if valid && tUsed > 0 {
			t := s[tStart : tStart+L]
			if t != "a" {
				ans++
			}
		}
	}
	return ans
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		var s string
		fmt.Fscan(reader, &s)
		fmt.Fprintln(writer, solve(s))
	}
}
```

</solution>