.htaccess RewriteRule to convert query-type URL structure to directory-type URL
Solution 1:
You would need to break up the RewriteRule extract each part of the uri i.e. test, 789 and 2; like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /letters.php?q=$1&d=$2&t=$3 [NC]
There is a good article on .htaccess tip and tricks that shows how rewrite rules work.