main = interact $ unlines . map (show . dirichlet . map read . words) . takeWhile (/= "0 0 0") . lines

dirichlet [a,d,n] = (intersect' (iterate (+d) a) primes) !! (n-1)

intersect' xxs@(x:xs) yys@(y:ys)
 | x < y = intersect' xs yys
 | x > y = intersect' xxs ys
 | otherwise = x : intersect' xs ys

primes = 2:3:[x | i<-[1..], j<-[-1,1], let x = 6*i+j, isPrime x]
 where isPrime n = null [i | i<-takeWhile (\x -> x*x <= n) primes, rem n i == 0]