import java.util.*;
public class Main {
static long answer(int n,int m,HashMap<Integer,Long> map)
{
if(n==0)
return 1;
if(n<0)
return 0;
if(map.containsKey(n))
return map.get(n);
long way1=answer(n-1,m,map);
long way2=answer(n-m,m,map);
map.put(null, way1+way2);
return (way1+way2);
}
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int t=sc.nextInt();
while(t!=0) {
int n=sc.nextInt();
int m=sc.nextInt();
HashMap<Integer,Long> map=new HashMap<>();
System.out.println(answer(n,m,map));
t--; }
}
}