     SELECT c.cid, bid 
       FROM (SELECT MIN(cid) AS cid, t, a, i FROM c GROUP BY t, a, i) tc
       JOIN c ON tc.t=c.t AND tc.a=c.a AND tc.i=c.i
       JOIN b ON b.pa = c.pa and b.rt IS NULL
   ORDER BY c.cid, bid ASC
      LIMIT 1
;

