Pick pairs of sequences with certain properties Assume $q\in\mathbb{N}=\{1,2,\dots\}$ to be the size of some field $F$ and consider all sequences $\mathbf{a}$ of length $k-1$ with elements generated from $F=\{0,1,\dots q-1\}$. There are $q^{k-1}$ of them, say $\mathbf{a}_1, \mathbf{a}_2, \dots, \mathbf{a}_{q^{k-1}}$
Now take each of these sequences and attach the sum of its digits modulo $q$ to the end of the sequence to get the $\mathbf{b}$-sequences of the form
$$
\mathbf{b}_{ij}=
\left\{
 \begin{array}{ll}
 \mathbf{a}_{ij}, & \text{if}\ j\in\{1,\dots,k-1\} \\
 \sum\limits_{m=1}^{k-1}a_{im}\ (\mathrm{mod}\ q), & \text{if}\ j=k\\
 \end{array}
\right.
$$
for all $i\in\{1,\dots,q^{k-1}\}$. In the above definition, I have assumed that $\mathbf{a}_{ij}$ denotes the $j$-th element of the sequence $\mathbf{a}_i$ and that the indexing starts from 1.
To demonstrate this, let $q=2$, $k=3$. Then the sequences would be
\begin{eqnarray*}
\mathbf{b}_1&=&000\\
\mathbf{b}_2&=&011\\
\mathbf{b}_3&=&101\\
\mathbf{b}_4&=&110
\end{eqnarray*}
My problem is the following: Let $w\in\mathbb{N}$ with $2\leq w\leq k$. How many ordered pairs ($\mathbf{b}_i,\mathbf{b}_j$) for $i\neq j$ of $b$-sequences differ at exactly $w$ positions and coincide at the remaining $k-w$ positions? Denote this quantity by $N_w$
By saying ordered pairs I mean that if ($\mathbf{b}_i,\mathbf{b}_j$) is a valid pair for a specific value of $w$ then ($\mathbf{b}_j,\mathbf{b}_i$) is also a valid pair for that $w$ and should be counted.
For the above example and for $w=2$ the idea is to take the sequences $\mathbf{b}_1$, $\mathbf{b}_2$, $\mathbf{b}_3$ and $\mathbf{b}_4$ one at a time and see which of the remaining sequences differ by the current sequence at exactly $w=2$ positions. In this case, all of the pairs ($\mathbf{b}_1,\mathbf{b}_2$), ($\mathbf{b}_1,\mathbf{b}_3$), ($\mathbf{b}_1,\mathbf{b}_4$), ($\mathbf{b}_2,\mathbf{b}_1$), ($\mathbf{b}_2,\mathbf{b}_3$), ($\mathbf{b}_2,\mathbf{b}_4$), ($\mathbf{b}_3,\mathbf{b}_1$), ($\mathbf{b}_3,\mathbf{b}_2$), ($\mathbf{b}_3,\mathbf{b}_4$), ($\mathbf{b}_4,\mathbf{b}_1$), ($\mathbf{b}_4,\mathbf{b}_2$), ($\mathbf{b}_4,\mathbf{b}_3$) satisfy the condition for $w=2$. So the answer is $N_w=N_2=12$ pairs.
It seems that the general formula for $w=2$ is 
$$N_2={k\choose 2}q^{k-1}(q-1)$$
My thought was simple: first fix two positions $m,n$ for the simple case of $m\neq k$ and $n\neq k$, and take a sequence $\mathbf{b}_i$. Then find all $\mathbf{b}_j$'s that differ from $\mathbf{b}_i$ at these positions only. Since $\mathbf{b}_i$ is fixed you have $q-1$ options for $m$-th position but for the $n$-th position you have only one value that would give the same sum of digits modulo $q$. Taking all possible $m,n$ gives ${k\choose 2}$ options and then taking each sequence gives $q^{k-1}$ options. 
For $w=3$ I think that the answer is
$$N_3={k\choose 3}q^{k-1}(q-1)(q-2)$$
but I cannot prove it. However 
$$N_4={k\choose 4}q^{k-1}(q-1)(q-2)(q-3)$$
seems to be wrong and I cannot generalize the idea.
 A: Let $a_1,a_2,\ldots,a_n$ be sequences of length $n\ge 1$ with $a_i \in F, i=1,2,\ldots,n$. Let $S(n)$ be the number of such sequences with all $a_i \neq 0$. Let $Z(n)$ be the number of such sequences with all $a_i \neq 0$ and $\sum_{i=1}^{n}a_i = 0 \pmod q$.
Obviously, we have $S(n)=(q-1)^n$. It's also easy to see that $Z(1)=0$.
We have the following recurrence relation:
For $n \ge 1$: 
$$Z(n+1) = S(n) - Z(n)$$
Proof: Any sequence $a_1,a_2,\ldots,a_{n+1}, a_i \neq 0, i=1,2,\ldots,n+1, \sum_{i=1}^{n+1}a_i = 0 \pmod q$ has $\sum_{i=1}^{n}a_i \neq 0 \pmod q$, as otherwise we'd have $a_{n+1} = 0 \pmod q$ and thus $a_{n+1} = 0$.
In the other direction, any sequence $a_1,a_2,\ldots,a_{n}, a_i \neq 0, i=1,2,\ldots,n, \sum_{i=1}^{n}a_i \neq 0 \pmod q$ can be extended by exactly one $a_{n+1} \in F, a_{n+1} \neq 0$ such that $\sum_{i=1}^{n+1}a_i = 0 \pmod q$; we have $a_{n+1}=-\sum_{i=1}^{n}a_i \pmod q$ (which exists and is unique in $F$), and the condition $\sum_{i=1}^{n}a_i \neq 0 \pmod q$ makes sure that $a_{n+1} \neq 0$. $\blacksquare $.
With mathematical induction, it is now easy to prove that
Lemma 1:
$$Z(n) = (q-1)\frac{(q-1)^{n-1}-(-1)^{n-1}}{q}$$
This gives $Z(2)=q-1, Z(3)=(q-1)(q-2),$ but $Z(4)=(q-1)(q^2-3q+3)$, which threw off the OP.
But what does this have to do with the original problem?
First, it's easy to see that $Z(n)$ also counts the number of sequences with $a_i \neq f_i$ and $\sum_{i=1}^{n}a_i=\sum_{i=1}^{n}f_i \pmod q$, where $f_i \in F, i=1,2,\ldots,n$ is a fixed sequence of numbers from $F$. Choosing $a'_i=a_i-f_i \pmod q, i=1,2,\ldots,n$ transforms this problem into the former one.
Second, for the original problem, select any $b_i$: This allows $q^{k-1}$ choices.
The $w$ differences between $b_i$ and any potential $b_j$ can occur in 2 ways:


*

*The $w$ differences are all before index $k$, and

*There are $w-1$ differences before index $k$, and one difference in index $k$.


For possibility 1, we have to choose the $w$ indices among the $k-1$ existing, this leads to $k-1 \choose w$ index choices. In each of those $w$ indices ($s_1, s_2, \ldots, s_w$), the sequence $b_j$ must differ from $b_i$ ($b_{is_r} \neq b_{js_r}, r=1,2,\ldots,w$) , but the sum of the values must be equal ($\sum_{r=1}^w b_{is_r} = \sum_{r=1}^w b_{js_r} \pmod q$), because at index $k$ we have equality between $b_i$ and $b_j$, which means $\sum_{r=1}^{k-1} b_{ir} = \sum_{r=1}^{k-1} b_{jr} \pmod q$. But since $b_i$ and $b_j$ only differ in the index set $s_r, r=1,2,\ldots w$, the latter is the same as the former.
That s exactly the problem we tackled above, that means once the index set is chosen, we have Z(w) possibilities to choose the elements of $b_j$ that are supposed to be different from $b_i$. Alltogether this means
$${k-1 \choose w} Z(w) $$
different $b_j$ for possibility 1.
For possibility 2, we have to choose the $w-1$ indices for difference between $b_i$ and $b_j$, so $k-1 \choose w-1$ possibilites to arrive at an index set $s_r, r=1,2,\ldots,w-1$. But now, since we want a difference also in the 'calculated' index $k$, we want the sums $\sum_{r=1}^{w-1} b_{is_r}$ and $\sum_{r=1}^{w-1} b_{js_r} $ to be different $\pmod q$.The number of sequences to be chosen thus is $S(w-1) - Z(w-1)$: All such sequences minus the number of sequences where the sums are the same. We know from the proof above that $S(w-1) - Z(w-1) = Z(w)$. Taken together with the number choices for the index set, we see that the number of sequences $b_j$ under possibility 2 is 
$${k-1 \choose w-1} Z(w)$$
Adding the values for possibiliites 1 and 2, we get that for each $b_i$ there are exactly
$${k-1 \choose w} Z(w)  + {k-1 \choose w-1} Z(w) = ({k-1 \choose w}  + {k-1 \choose w-1}) Z(w) = {k \choose w} Z(w)$$
$b_j$ that differ from $b_i$ in exactly $w$ indices.
Recalling that there are $q^{k-1}$ possible $b_i$, this makes the count of ordered pairs $(b_i,b_j)$ with $b_i,b_j$ differing in exactly $w$ indices
$$N_w = {k \choose w}q^{k-1} Z(w) = {k \choose w}q^{k-1} (q-1)\frac{(q-1)^{w-1}-(-1)^{w-1}}{q} $$
As I wrote above, the special form of $Z(w)$ for small $w$ might one lead to a different assumption on the general form of $Z(w)$
