#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define LL long longusing namespace std;const int maxn=5678;const int INF=1<<28;int T,k,n,ai[maxn],dp[1010][maxn];void init(){ scanf("%d%d",&k,&n); k+=8; for(int i=n; i>=1; i--) { scanf("%d",&ai[i]); }}void DYGANDQYM(){ for(int i=0; i<=k; i++) { for(int j=0; j<=n; j++) { if(i==0)dp[i][j]=0; else dp[i][j]=INF; } } // cout<<INF<<endl; for(int i=1; i<=k; i++) { for(int j=3; j<=n; j++) { if(j>=3*i) { dp[i][j]=dp[i][j-1]; int tm=dp[i-1][j-2]+(ai[j]-ai[j-1])*(ai[j]-ai[j-1]); if(tm<dp[i][j])dp[i][j]=tm; } } } printf("%dn",dp[k][n]);}int main(){ while(scanf("%d",&T)!=EOF) { for(int i=1; i<=T; i++) { init(); DYGANDQYM(); } } return 0;}