COUPON--getting wrong o/p though my solution is giving the right one

//Though I am getting the correct output it shows wrong output…someone please help

#include<stdio.h>
int main()
{int m,n,i,j,pp,t,k;
scanf("%d",&t);
while(t–)
{

	scanf("%d%d",&m,&n);
	int p[m][n],d[m][n],min=0,pos=0;
	
	for(i=0;i<m;i++)
	for(j=0;j<n;j++)
	scanf("%d",&p[i][j]);
	
	for(i=0;i<m;i++)
	for(j=0;j<n;j++)
	scanf("%d",&d[i][j]);  
	
	min=p[0][0];
	for(i=1;i<m;i++)
	{
		if(p[i][0]<min)
		{
		min=p[i][0];
		pos=i;
	}
	}
	//printf("min::%d\n",min);
	//printf("pos:%d",pos);
	i=pos;
	for(k=1;k<n;k++)
	{
		pp=p[i][k]-d[i][k-1];
		if(pp<0)
		pp=0;
			//printf("pp[%d][%d]::%d\n",i,k,pp);
		for(j=0;j<m;j++)
		{	//printf("p[%d][%d]::%d\n",j,k,p[j][k]);
			if(p[j][k]<pp)
			{
			pp=p[j][k];
			i=j;
		    }
		}
	
			min+=pp;
			//printf("min value::%d\n",min);
			
		}
		
	
	printf("%d",min);
}

}

//