Joe's PIP will pay $4000 X80% = $3200 and his Med Pay will pay the remainder, $800. Ted cannot use Joe's PIP (because you can only use your own PIP coverage) and he cannot use his PIP coverage because he is out-of-state in an unowned vehicle. But Ted is eligible to use Joe's Med Pay so he will also have $1000 toward his medical expense.
Joe = $3200 + $800 = $4000 (total cost of his medical expense)
Ted = $1000
Total = $5000