Selecting Demonstrations for Many-Shot In-Context Learning via Gradient Matching