The mean of the numbers 3, 4, a, 5, 7, a, 5, 8, 5 and 9 is equal to the mode. Find the value of a and hence the median of the data.