It's a trick question, because the phrasing is altered in the second sentence.
Look at it this way: I say to you:
- I like you. You are a smart person.
In the above, I already mentioned "you" in the first sentence, but I didn't mention "smart person" in the first one. Not only that, but if I had said "You are the smart person," that would mean that I think there is no other smart person except you.
Now, back to your example. "Test" was already mentioned, but "really hard test " wasn't:
In the second sentence, the subject is "it", a pronoun, which stands in for "the test already mentioned". "It" is then equated to "really hard test", which hadn't been already mentioned.
So, you see, the use of a pronoun (namely "it") changes everything.
This is a subtle distinction, so don't be worried because the question (that you mentioned) fooled you.
A question
(another one, not yet mentioned) as subtle as that one
(the one you mentioned) might fool any English learner (you or another one).
But IF the second sentence had actually used "test" as the subject, "the" would have been appropriate, just as you would expect based on the explanation you cited.
- The test was a really hard test.
But that repeats "test" too many times (three, counting once in the first sentence). That's why the example shortened to:
- It was a really hard test.
one could also say:
- The test was really hard.
or:
- The test was a really hard one.
Each of the above three ways uses "test" twice in two sentences, which, strictly speaking, is not necessary. The below is shorter still, and is how an AmE speaker would probably say the second sentence:
(In the above, by eliminating "test" altogether, the native speaker completely avoids the question of a vs. the !)
Keep studying and asking. And I suggest you try posting questions on English Language Learners Stack Exchange, and reviewing others' questions there as well.