import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Solution
{

    static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[]args)throws IOException{
		try{

			new Solution().run();

		}catch(Exception e){
			return;
		}

	}
	static void run(){

		// FIRST CASE -- RETURN TYPE SOLVE
		int t = sc.nextInt();
		StringBuilder ans = new StringBuilder("");
		while(t-- > 0 ){
			ans.append(solve()).append("\n");

		}
		out.println(ans);

		// FOR SECOND CASE --  void solve()
		//while(t-- > 0 ){
		//solve();
		//}
	}
	static int solve(){
		int a = sc.nextInt();
        int b = sc.nextInt();
        int are = 2*a*b;
        int max = Math.max(a,b);
        int ans = max*max;

            while (ans < are) {
                max++;
                ans = max * max;
            }

		return ans;

	}

}


import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Solution
{

    static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);
	public static void main(String[]args)throws IOException{
		try{

			new Solution().run();

		}catch(Exception e){
			return;
		}

	}
	static void run(){

		// FIRST CASE -- RETURN TYPE SOLVE
		int t = sc.nextInt();
		StringBuilder ans = new StringBuilder("");
		while(t-- > 0 ){
			ans.append(solve());
			ans.append("\n");

		}
		System.out.println(ans);

		// FOR SECOND CASE --  void solve()
		//while(t-- > 0 ){
		//solve();
		//}
	}
	static int solve(){
		int a = sc.nextInt();
        int b = sc.nextInt();
        int are = 2*a*b;
        int min = Math.min(a,b);
        int max = Math.max(a,b);
        int ans = 0;
        if(2*min >=  max ){
            ans = (int)Math.pow((2*min),2);
        }else{
            ans = (int)Math.pow(max,2);
        }
		return ans;

	}

}


package rect;
import java.util.*;
public class Rect {

	public static void main(String[] args) {
		int n,i,min;
		double side,area;
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		int a[]=new int[n];
		int b[]=new int[n];
		int ar[]=new int[n];

		for(i=0;i<n;i++)
		{
			a[i]=sc.nextInt();
			b[i]=sc.nextInt();
		}
		for(i=0;i<n;i++)
		{
			min=Math.min(a[i], b[i]);
			side=2*min;
			area=Math.pow(side,2);
			if(area>2*a[i]*b[i])
			{
				ar[i]=(int)area;
			}
			else
			{
				ar[i]=(int) (Math.pow(Math.max(a[i],b[i]), 2));
			}
		}
		for(i=0;i<n;i++)
		{
			System.out.println(ar[i]);
		}
	}

}


import java.util.*;
public class Rect {

	public static void main(String[] args) {
		int n,i,min;
		double side,area;
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		int a[]=new int[n];
		int b[]=new int[n];
		int ar[]=new int[n];

		for(i=0;i<n;i++)
		{
			a[i]=sc.nextInt();
			b[i]=sc.nextInt();
		}
		for(i=0;i<n;i++)
		{
			min=Math.min(a[i], b[i]);
			side=2*min;
			area=Math.pow(side,2);
			if(area>2*a[i]*b[i])
			{
				ar[i]=(int)area;
			}
			else
			{
				ar[i]=(int) (Math.pow(Math.max(a[i],b[i]), 2));
			}
		}
		for(i=0;i<n;i++)
		{
			System.out.println(ar[i]);
		}
	}

}


import java.util.*;
public class Rect {

	public static void main(String[] args) {
		int n,i,min;
		double side,area;
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter");
		n=sc.nextInt();
		int a[]=new int[n];
		int b[]=new int[n];
		int ar[]=new int[n];

		for(i=0;i<n;i++)
		{
			a[i]=sc.nextInt();
			b[i]=sc.nextInt();
		}
		for(i=0;i<n;i++)
		{
			min=Math.min(a[i], b[i]);
			side=2*min;
			area=Math.pow(side,2);
			if(area>2*a[i]*b[i])
			{
				ar[i]=(int)area;
			}
			else
			{
				ar[i]=(int) (Math.pow(Math.max(a[i],b[i]), 2));
			}
		}
		for(i=0;i<n;i++)
		{
			System.out.println(ar[i]);
		}
	}

}


import java.util.*;
public class A1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t-- > 0) {
			int a = sc.nextInt();
                        int b = sc.nextInt();
                        int r=Math.min(a,b)*2>Math.max(a,b)?Math.min(a,b)*2:Math.max(a,b);
                        System.out.println(r*r);
		}
    }
}


import java.util.*;

public class A1{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt(),b=sc.nextInt();
            int r=Math.min(a,b)*2>Math.max(a,b)?Math.min(a,b)*2:Math.max(a,b);
            System.out.print(r*r);
        }
    }
}


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
     sort(Array, Array+n);

    for (int j=1; j<= n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans<<endl;
        }
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
     sort(Array, Array+n);

    for (int j=1; j<= n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans;
        return ans;
        }
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
     sort(Array, Array+n);

    for (int j=0; j< n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans;
        break;
        }
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
     sort(Array, Array+n);

    for (int j=1; j<= n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans<<endl;
        return ans;
        }
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n <= 1 || n >= 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a <= 1 || a >= 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
    sort(Array, Array+n);

    for (int j=1; j<= n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans<<endl;
        }
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
     sort(Array, Array+n);
    int index = Array[0]+1;
    for (int j=1; j< n; j++){
        if (Array[j] != index)
        {ans = index;
        cout<<ans;
        }
        index++;
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
     sort(Array, Array+n);

    for (int j=0; j< n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans<<endl;
        break;
        }
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
    sort(Array, Array+n);

    for (int j=1; j<= n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans<<endl;
        }
        }

    return 0;
    }


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; //
    if (n <= 1 || n >= 3000)
    {
        cout<<"N should be from 1 to 3000 only! Try again later"<<endl;
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a <= 1 || a >= 3000)
    {
        cout<<"No. of indexes should be from 1 to 3000 only! Try again later"<<endl;
        return 0;
    }
    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
    sort(Array, Array+n);

    for (int j=0; j< n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans<<endl;
        break;}
        }

    return 0;
    }


#include<iostream>
#include<math.h>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a;
    cin>>n; // T no. of trials
    if (n < 1 || n > 3000)
    {
        cout<<"N should be from 1 to 3000 only! Try again later"<<endl;
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        cout<<"No. of indexes should be from 1 to 3000 only! Try again later"<<endl;
        return 0;
    }
    bool* Array = new bool[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[a] = true;
        if( count < n)
        {
            cin>>a;
            count++;
        }
    }
    cout<<a+1<<endl;

    return 0;
}


#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;

int SumofSub ( int n, int sum);

int main(){
    int n, a, ans;
    cin>>n; // T no. of trials

    if (n < 1 || n > 3000)
    {
        return 0;
    }
    cin>>a; // number of elements in first trial

    if (a < 1 || a > 3000)
    {
        return 0;
    }

    int* Array = new int[n];
    int count =1;
    for (int i=0; i< n; i++){
        Array[i] = a;
        if( count < n)
        {
            cin>>a;
            count++;
        }
        }
     sort(Array, Array+n);

    for (int j=0; j< n; j++){
        if (Array[j] != j+1)
        {ans = j+1;
        cout<<ans<<endl;
        break;
        }
        }

    return 0;
    }


#include<iostream>
using namespace std;

int main(){
    int a[3001],i,j,n,b;
    cin>>n;

    for(i=0;i<n;i++){
    cin>>b;
    a[b]++;
    }
    for(i=1;i<=n;i++)
    if(a[i]==0){cout<<i;return 0;}
    }


#include<iostream>
using namespace std;

int main(){
    int a[3001],i,j,n,b;
    cin>>n;

    for(i=0;i<n;i++){
    cin>>b;
    a[b]++;
    }
    if(n==3000){cout<<"3001";return 0;}
    for(i=1;i<=n;i++)
    if(a[i]==0){cout<<i;return 0;}
    cout<<n+1;
    }


#include<iostream>
using namespace std;

int main(){
    int a[3001],i,j,n,b;
    cin>>n;

    for(i=0;i<n;i++){
    cin>>b;
    a[b]++;
    }
    for(i=1;i<=n;i++)
    if(a[i]==0){cout<<i;return 0;}
    cout<<n+1;
    }


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {

    int t,n,x;
    cin>>t;
    x=t;
    vector <int> A;
    while(t--)
    {
        cin>>n;
        A.push_back(n);
    }
    sort(A.begin(),A.end());

    for(int i=1; i<=x; i++)
    {
        if(i!=A[i-1])   {cout<<i; break;}
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;

int main() {

    int prev;
    cin>>prev;
    vector<int>index;
    int x;

    for(int i=0; i<prev; i++)
    {
        cin>>x;
        index.push_back(x);
    }

    sort(index.begin(), index.end());

    bool flag=false;
    for(int i=1; i<=prev; i++)
    {
        if(i!=index[i-1])
        {
            cout<<i;
            i=prev+1;
            flag=true;
        }
    }

    if(!flag)
        cout<<prev+1;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {

    int t,n,x;
    cin>>t;
    x=t;
    if(t==1)
    {
        cin>>n;
        if(n==1)    {cout<<2; return 0;}
    }
    vector <int> A;
    while(t--)
    {
        cin>>n;
        A.push_back(n);
    }
    sort(A.begin(),A.end());

    for(int i=1; i<=x; i++)
    {
        if(i!=A[i-1])   {cout<<i; break;}
    }
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;

int main() {

    int prev;
    cin>>prev;
    vector<int>index;
    int x;

    for(int i=0; i<prev; i++)
    {
        cin>>x;
        index.push_back(x);
    }

    sort(index.begin(), index.end());

    for(int i=1; i<=prev; i++)
    {
        if(i!=index[i-1])
        {
            cout<<i;
            i=prev+1;
        }
    }

    cin>>x;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {

    int t,n,x;
    cin>>t;
    x=t;
    if(t==1)
    {
        cin>>n;
        if(n==1)    {cout<<2; return 0;}
    }
    vector <int> A;
    while(t--)
    {
        cin>>n;
        A.push_back(n);
    }
    sort(A.begin(),A.end());
    bool flag=true;
    int i;
    for(i=1; i<=x; i++)
    {
        if(i!=A[i-1])   {cout<<i; flag=false; break;}
    }
    if(flag) cout<<i;
    return 0;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector <int> x(n);
	int i;
	for (i = 0; i < n; i++)
	{
		cin >> x[i];
	}
	sort(x.begin(), x.begin() + n);
	for (i = 0; i < n-1; i++)
	{
		if (x[i] + 1 != x[i + 1])
		{
			cout << x[i] + 1 << endl;
			return 0;
		}
	}
	cout << x[i] + 1 << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector <int> x(n);
	int i;
	for (i = 0; i < n; i++)
	{
		cin >> x[i];
	}
	sort(x.begin(), x.begin() + n);
	for (i = 0; i < n-1; i++)
	{
		if (x[i] + 1 != x[i + 1])
		{
			cout << x[i] + 1 << endl;
			return 0;;
		}
	}
	return 0;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector <int> x(n);
	int i;
	for (i = 0; i < n; i++)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.begin() + n);
	if (x[0] > 1)
	{
		cout << 1 << endl;
	}
	for (i = 0; i < n-1; i++)
	{
		if (x[i] + 1 != x[i + 1])
		{
			cout << x[i] + 1 << endl;
			return 0;
		}
	}
	cout << x[i] + 1 << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector <int> x(n);
	int i;
	for (i = 0; i < n; i++)
	{
		cin >> x[i];
	}

	sort(x.begin(), x.begin() + n);
	if (x[0] > 1)
	{
		cout << 1 << endl;
		return 0;
	}
	for (i = 0; i < n-1; i++)
	{
		if (x[i] + 1 != x[i + 1])
		{
			cout << x[i] + 1 << endl;
			return 0;
		}
	}
	cout << x[i] + 1 << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	vector <int> x(n);
	int i;
	for (i = 0; i < n; i++)
	{
		cin >> x[i];
	}
	sort(x.begin(), x.begin() + n);
	for (i = 0; i < n-1; i++)
	{
		if (x[i] + 1 != x[i + 1])
		{
			cout << x[i] + 1 << endl;
			return 0;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main ()
{
    vector<int> index;
    int n;  //number of indexes;
    int tempInt;
    int nextIndex;  //output

    cin>>n;

    for(int temp = 0; temp < n; temp ++)
    {
        cin>>tempInt;
        index.push_back(tempInt);
    }

    sort(index.begin(), index.end() );

    if(n > 0 && n < 3001)
    {
    for(int temp = 0 ; temp < index.size(); temp++)
    {
        if(temp == index.size() -1 )
            nextIndex = index[temp] + 1; // user used continues numbers starting with 1

        else if (index[temp] != index[temp+1] - 1 ) //diffrenec != 1
            {
                nextIndex = index[temp] + 1;
                break;
        }
    }
    cout<<nextIndex<<endl;
    }

}//end main


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main ()
{
    vector<int> index;
    int n;  //number of indexes;
    int tempInt;
    int nextIndex;  //output

    cin>>n;

    if(n > 0 && n < 3001)
    for(int temp = 0; temp < n; temp ++)
    {
        cin>>tempInt;
        index.push_back(tempInt);
    }

    sort(index.begin(), index.end() );

    for(int temp = 0 ; temp < index.size(); temp++)
    {
        if(temp == index.size() -1 )
            nextIndex = index[temp] + 1; // user used continues numbers starting with 1

        else if (index[temp] != index[temp+1] - 1 ) //diffrenec != 1
            {
                nextIndex = index[temp] + 1;
                break;
        }
    }
    cout<<nextIndex<<endl;

}//end main


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main ()
{
	vector<int> index;
	int n;  //number of indexes;
	int tempInt;
	int nextIndex;  //output

	cin>>n;

	for(int temp = 0; temp < n; temp ++)
	{
		cin>>tempInt;
		index.push_back(tempInt);
	}

	sort(index.begin(), index.end() );

	if(n > 0 && n < 3001)
	{
	for(int temp = 0 ; temp < index.size(); temp++)
	{
		if(temp == index.size() -1 )
			nextIndex = index[temp] + 1; // user used continues numbers starting with 1

		else if (index[temp] != index[temp+1] - 1 ) //diffrenec != 1
			{
				nextIndex = index[temp] + 1;
				break;
		}
	}
	cout<<"next index = "<<nextIndex<<endl;
	}
	else
		cout<<"Wrong input\n";
}//end main


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main ()
{
    vector<int> index;
    int n;  //number of indexes;
    int tempInt;
    int nextIndex;  //output

    cin>>n;

    if(n > 0 && n < 3001)
    {
        for(int temp = 0; temp < n; temp ++)
    {
        cin>>tempInt;
        index.push_back(tempInt);
    }

    sort(index.begin(), index.end() );

        int testInt = 1;

    while(index.size() > 0)
    {

        if(testInt != index[testInt-1] )
        {
            nextIndex = testInt;
            break;
        }
        testInt++;
    }
    cout<<nextIndex<<endl;
    }

}//end main


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void main ()
{
    vector<int> index;
    int n;  //number of indexes;
    int tempInt;
    int nextIndex;  //output

    cin>>n;

    if(n > 0 && n < 3001)
    {
        for(int temp = 0; temp < n; temp ++)
    {
        cin>>tempInt;
        index.push_back(tempInt);
    }

    sort(index.begin(), index.end() );

    for(int temp = 0 ; temp < index.size(); temp++)
    {
        if(temp == index.size() -1 )
            nextIndex = index[temp] + 1; // user used continues numbers starting with 1

        else if (index[temp] != index[temp+1] - 1 ) //diffrenec != 1
            {
                nextIndex = index[temp] + 1;
                break;
        }
    }
    cout<<nextIndex<<endl;
    }

}//end main


//#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORN(i,a,b) for(int i=(a);i<=(b);i++)
#define ALL(v) (v).begin(),(v).end()
#define MP make_pair
#define FILL(v) memset(v,0,sizeof(v))
#define INF 1000*1000*1000
#define PI 3.141592653589793
#define EPS 1e-9

typedef long long LL;

int a[3005];

bool rez;

void main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n;
	cin>>n;
	FOR(i,0,n)
		cin>>a[i];
	sort(a,a+n);
	if (a[0]>1)
	{
	FOR(i,1,n)
		if (a[i]-a[i-1]>1)
		{
			rez=1;
			cout<<a[i-1]+1;
			break;
		}
	}
	else
	{
		rez=1;
		cout<<"1";
	}
	if (!rez) cout<<a[n-1]+1;

}


//#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORN(i,a,b) for(int i=(a);i<=(b);i++)
#define ALL(v) (v).begin(),(v).end()
#define MP make_pair
#define FILL(v) memset(v,0,sizeof(v))
#define INF 1000*1000*1000
#define PI 3.141592653589793
#define EPS 1e-9

typedef long long LL;

int a[3005];

bool rez;

void main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n;
	cin>>n;
	FOR(i,0,n)
		cin>>a[i];
	sort(a,a+n);
	FOR(i,1,n)
		if (a[i]-a[i-1]>1)
		{
			rez=1;
			cout<<a[i-1]+1;
			break;
		}
	if (!rez) cout<<a[n-1]+1;

}


//#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORN(i,a,b) for(int i=(a);i<=(b);i++)
#define ALL(v) (v).begin(),(v).end()
#define MP make_pair
#define PB push_back
#define FILL(v) memset(v,0,sizeof(v))
#define SQR(x) (x)*(x)
#define INF 1000*10000*10000
#define PI 3.141592653589793
#define EPS 1e-9

typedef long long int LLI;
typedef vector<int> VI;

int m[3000];

bool res;

void main()
{
		//freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
		int n;
		cin>>n;
		FOR(i,0,n) cin>>m[i];
		sort(m,m+n);
		if (m[0]>1)
		{
			cout<<"1";
			res=1;
		}
		else
			FOR(i,1,n)
				if (m[i]-m[i-1]>1)
				{
					cout<<m[i-1]+1;
					res=1;
					break;
				}
		if (!res) cout<<m[n-1]+1;

}


import sys

def input():    return sys.stdin.readline().strip()
def iinput():   return int(input())
def rinput():   return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))

n=iinput()
a=list(map(int,input().split()))
count=0
test=[]

for i in range(1,3001):
    test.append(i)

a.sort()
if(n==1 and a[0]==1):
    print(2)
else:
    for i in range(n):

        if(a[i]!=test[i]):
            count+=1
            break

    if(count==0):
        print(n+1)
    else:
        print(test[i])


import sys

def input():    return sys.stdin.readline().strip()
def iinput():   return int(input())
def rinput():   return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))

n=iinput()
a=list(map(int,input().split()))
count=0
test=[]

for i in range(1,3001):
    test.append(i)

a.sort()
if(n==1):
    print(2)
else:
    for i in range(n):
        count+=1
        if(a[i]!=test[i]):
            break

    if(count==n):
        print(n)
    else:
        print(test[i])


import sys

def input():    return sys.stdin.readline().strip()
def iinput():   return int(input())
def rinput():   return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))

n=iinput()
a=list(map(int,input().split()))
test=[]

for i in range(1,3001):
    test.append(i)

a.sort()
for i in range(n):
    if(a[i]!=test[i]):
        break

print(test[i])


import sys

def input():    return sys.stdin.readline().strip()
def iinput():   return int(input())
def rinput():   return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))

n=iinput()
a=list(map(int,input().split()))
count=0
test=[]

for i in range(1,3001):
    test.append(i)

a.sort()
if(n==1):
    print(2)
else:
    for i in range(n):

        if(a[i]!=test[i]):
            count+=1
            break

    if(count==0):
        print(n+1)
    else:
        print(test[i])


import sys

def input():    return sys.stdin.readline().strip()
def iinput():   return int(input())
def rinput():   return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))

n=iinput()
a=list(map(int,input().split()))
test=[]

for i in range(1,3001):
    test.append(i)

a.sort()
if(n==1):
    print(2)
else:
    for i in range(n):
        if(a[i]!=test[i]):
            break

    print(test[i])


import sys

def input():    return sys.stdin.readline().strip()
def iinput():   return int(input())
def rinput():   return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))

n=iinput()
a=list(map(int,input().split()))
count=0
test=[]

for i in range(1,3001):
    test.append(i)

a.sort()
if(n==1):
    print(2)
else:
    for i in range(n):
        count+=1
        if(a[i]!=test[i]):
            break

    if(count==n):
        print(n+1)
    else:
        print(test[i])


def main():
    count=int(input())
    arr=input().split(" ")
    store=[]
    for x in range(count):
        test=int(arr[x])
        bo=True
        for y in range(len(store)):
            if test<store[y]:
                bo=False
                break
        if bo:
            store.append(test)
        else:
            store=store[:y]+[test]+store[y:]
    base=1
    while True:
        if store[base-1]==base:
            base+=1
        elif len(store)==base:
            print(base+1)
            break
        else:
            print(base)
            break

main()


def main():
    count=int(input())
    arr=input().split(" ")
    store=[]
    for x in range(count):
        test=int(arr[x])
        bo=True
        for y in range(len(store)):
            if test<store[y]:
                bo=False
                break
        if bo:
            store.append(test)
        else:
            store=store[:y]+[test]+store[y:]
    if count==store[-1]:
        print(count+1)
    else:
        base=1
        while True:
            if store[base-1]==base:
                base+=1
            else:
                print(base)
                break

main()


def main():
    count=int(input())
    arr=input().split(" ")
    store=[]
    for x in range(count):
        test=int(arr[x])
        bo=True
        for y in range(len(store)):
            if test<store[y]:
                bo=False
                break
        if bo:
            store.append(test)
        else:
            store=store[:y]+[test]+store[y:]
    base=1
    while True:
        if store[base-1]==base:
            base+=1
        else:
            print(base)
            break

main()


n = int(input())
seq = tuple(map(int,input().strip().split(' ')))
t = [False] * (n+1)
for e in seq:
    if e <= n:
        t[e] = True
for i in range(1, n+1):
    if t[i] == False:
        print(i)
        exit()


n = int(input())
seq = tuple(map(int,input().strip().split(' ')))
t = [False] * (n+1)
for e in seq:
    if e <= n:
        t[e] = True
for i in range(1, n+1):
    if t[i] == False:
        print(i)
        exit()
if i == n:
    print(n+1)


n=int(input())
l=list(map(int,input().split()))
l.sort()
for i in range(1,l[n-1]+2):
    if(i not in l):
        print(i)
        break


n=int(input())
l=list(map(int,input().split()))
l.sort()
for i in range(1,l[n-1]+1):
    if(i not in l):
        print(i)
        break


n = int(input())
l = list(map(int,input().split()))
l.sort()
f = 0
if l[0] != 1:
	print(1)
	f = 1
else:
	for i in range(n-1):
		if ((l[i] + 1) != (l[i+1])):
			print(l[i]+1)
			f = 1
			break
if f == 0:
    print(l[n-1]+1)


n = int(input())
l = list(map(int,input().split()))
l.sort()
f = 0
if l[0] != 1:
	print(1)
	f = 1
else:
	for i in range(n-1):
		if ((l[i] + 1) != (l[i+1])):
			print(l[i]+1)
			f = 1
			break
if f == 0:
    print(l[n]+1)


n=int(input())
l=list(map(int,input().split()))
l.sort()
for i in range(1,100000000):
	if l[i-1]!=i:
		print(i)
		exit()


n=int(input())
l=list(map(int,input().split()))
l.sort()
for i in range(1,n+1):
	if l[i-1]!=i:
		print(i)
		exit()
print(n+1)


n = int(input())
a = [i + 1 for i in range(n)]
b = [int(i) for i in input().split()]
for i in b:
    if (i in a) and (i <= n):
        a[i - 1] = n + 1;
min = n + 1;
for i in a:
    if (min > i):
        min = i
print("%d\n" % min)


n = int(input())
a = [i + 1 for i in range(n)]
b = [int(i) for i in input().split()]
for i in b:
    if i in a:
        a[i - 1] = n + 1;
min = n + 1;
for i in a:
    if (min > i):
        min = i
print("%d\n" % min)


n = int(input())
a = [int(x) for x in input().split()]
dic = {}
for i in a :
    dic[i] = 1
for i in range(1,n+1):
    if i not in dic:
        print(i)
        break


n = int(input())
a = [int(x) for x in input().split()]
dic = {}
if n==1 and a[0]==1:
    print(a[0]+1)
elif n==1 :
    print(1)
else:
    for i in a :
        dic[i] = 1
    for i in range(1,n+1):
        if i not in dic:
            print(i)
            break


n = int(input())
a = [int(x) for x in input().split()]
dic = {}
if n==1 and a[0]==1:
    print(a[0]+1)
elif n==1 :
    print(1)
else:
    for i in a :
        dic[i] = 1
    for i in range(1,n+2):
        if i not in dic:
            print(i)
            break


n=int(input())
a=0
l=sorted(list(map(int,input().split())))
for i in l:
    a=a+1
    if i!=a:
        print(a)
        break


n=int(input())
a=0
b=0
l=sorted(list(map(int,input().split())))
for i in l:
    a=a+1
    if i!=a:
        print(a)
        b=1
        break
if b==0:
    print(a+1)


n=int(input())

arr=[int(x) for x in (input().split())]
arr.sort()
for i in range(1,n):
    if arr[i]-arr[i-1]>1:
        print(arr[i-1]+1)
        exit


n=int(input())

arr=[int(x) for x in (input().split())]
arr.sort()
if n==1:
    if arr[0]==1:
        print(2)
    else:
        print(1)
else:
    for i in range(1,n):
         if arr[i]-arr[i-1]>1:
             print(arr[i-1]+1)
             exit


n=int(input())

arr=[int(x) for x in (input().split())]
arr.sort()
if n==1:
    if arr[0]==1:
        print(2)
    else:
        print(1)
else:
    for i in range(1,n):
         if arr[i]-arr[i-1]>1:
             print(arr[i-1]+1)
             break
         elif i==n-1:
              print(arr[i] + 1)


n=int(input())

arr=[int(x) for x in (input().split())]
arr.sort()
if n==1:
    if arr[0]==1:
        print(2)
    else:
        print(1)
else:
    if arr[0]!=1:
        print(1)
    else:
         for i in range(1,n):
            if arr[i]-arr[i-1]>1:
                print(arr[i-1]+1)
                break
            elif i==n-1:
                print(arr[i] + 1)


n=int(input())

arr=[int(x) for x in (input().split())]
arr.sort()
if n==1:
    if arr[0]==1:
        print(2)
    else:
        print(1)
else:
    for i in range(1,n):
         if arr[i]-arr[i-1]>1:
             print(arr[i-1]+1)
             exit
         elif i==n-1:
              print(arr[i] + 1)


input();a=list(map(int,input().split()));a.append(30001);a.sort();x=1
for i in a:
    if i!=x:print(x);break
    else:x+=1


input();a=list(map(int,input().split()));a.sort();x=1
for i in a:
    if i!=x:print(x);break
    else:x+=1


n = int(input())

k = list(map(int,input().split()))

k.sort()

for i in range(1,len(k)+1):

    if i not in k:
        print(i)
        break


n = input()

k = list(map(int,input().split()))

k.sort()

for i in range(1,len(k)+1):

    if i not in k:
        print(i)
        break


n = int(input())

k = list(map(int,input().split()))

k.sort()

for i in range(1,5000):

    if i not in k:
        print(i)
        break


def test(n,a):
    lst = []
    ans = 0

    if n==3000 :
        ans = 3001
        return ans
    else:
        for i in range(3001):
            lst.append(False)

        for j in a:
            lst[j] = True

        for j in lst:
            ans = ans+1
            if j == False:
                return ans
                break

n = int(input(''))
a = list(map(int,input('').split(" ")))
a = [i-1 for i in a]

print(test(n,a))


lst = []
ans = 0
for i in range(3000):
    lst.append(False)

n = int(input(''))
a = list(map(int,input('').split(" ")))
a = [i-1 for i in a]

for j in a:
    lst[j] = True

for j in lst:
    ans = ans+1
    if j == False:
        print(ans)
        break


lst = []
ans = 0
for i in range(3000):
    lst.append(False)

n = int(input(''))
a = list(map(int,input('').split(" ")))
a = [i-1 for i in a]

for j in a:
    lst[j] = True

for j in lst:
    ans = ans+1
    if j == False:
        print(ans)
        break

print(lst)


n=int(input())
a=list(map(int,input().split()))
v=[False]*3002
for i in a:
    v[i]=True
for i in range(1,3002):
    if(v[i]==False):
        print(i)
        break


n=int(input())
a=list(map(int,input().split()))
v=[False]*3001
for i in a:
    v[i]=True
for i in range(1,3000):
    if(v[i]==False):
        print(i)
        break


n = int(input())
li = list(map(int,input().split()))
li.sort()
i = 0
while True:
    if li[i] != i+1:
        print(i+1)
        quit()
    i += 1


n = int(input())
li = list(map(int,input().split()))
li.sort()
i = 0
while i < n:
    if li[i] != i+1:
        print(i+1)
        quit()
    i += 1
print(i+1)


n = int(input())
l = list(map(int, input().split(" ")))
max = max(l)
exist =  False

for i in range(1,max):
    if i not in l :
        print(i)
        exist =True
        break
if exist==False:
    print(max+1)


n = int(input())
l = list(map(int, input().split(" ")))
max = max(l)
if n== 1 :
    print(l[0]+1)
else:

    for i in range(1,max):
        if i not in l :
            print(i)
            break


n = int(input())
l = list(map(int, input().split(" ")))
max = max(l)
for i in range(1,max):
    if i not in l :
        print(i)
        break


n = int(input())
l = list(map(int, input().split(" ")))
max = max(l)
for i in range(1,max+1):
    if i not in l :
        print(i)
        break


n = list(map(int, input().split()))[0]
indices = list(map(int, input().split()))

indices.sort()

def run():
    last_index = indices[0]
    for i in indices[1:]:
        if i != last_index + 1:
            return last_index + 1
        last_index = i
    return indices[-1] + 1

print(run())


n = list(map(int, input().split()))[0]
indices = list(map(int, input().split()))

indices.sort()

def run():
    last_index = indices[0]
    for i in indices[1:]:
        if i != last_index + 1:
            return last_index + 1
        last_index = i
    return i + 1

print(run())


n = list(map(int, input().split()))[0]
indices = list(map(int, input().split()))

indices.sort()

def run():
    last_index = indices[0]
    if last_index > 1:
        return 1
    for i in indices[1:]:
        if i != last_index + 1:
            return last_index + 1
        last_index = i
    return indices[-1] + 1

print(run())


#27A
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
br = True
if len(a) == 1:
    if min(a) > 1:
        print(1)
        br = False
else:
    for i in range(0, len(a)-1):
        if min(a) > 1:
            print(1)
            br = False
            break
        if a[i] != a[i+1] - 1:
            print(i + 2)
            br = False
            break

if br:
    print(len(a) + 1)


#27A
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
br = True
for i in range(0, len(a) - 1):
    if a[0] > 1:
        print(1)
    if a[i] != a[i+1] - 1:
        print(i + 2)
        br = False
        break

if br:
    print(len(a) + 1)


#27A
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
br = True
for i in range(0, len(a) - 1):
    if a[0] > 1:
        print(1)
        br = False
        break
    if a[i] != a[i+1] - 1:
        print(i + 2)
        br = False
        break

if br:
    print(len(a) + 1)


#27A
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
br = True
for i in range(0, len(a) - 1):
    if a[0] > 1:
        print(1)
        break
    if a[i] != a[i+1] - 1:
        print(i + 2)
        br = False
        break

if br:
    print(len(a) + 1)


#27A
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
br = True
for i in range(0, len(a) - 1):
    if a[i] != a[i+1] - 1:
        print(i + 1)
        br = False
        break

if br:
    print(len(a) + 1)


#27A
n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
br = True
for i in range(0, len(a) - 1):
    if a[i] != a[i+1] - 1:
        print(i + 2)
        br = False
        break

if br:
    print(len(a) + 1)


#!/usr/bin/env python3

n = int(input())
a = list(map(int, input().split()))
a.sort()

answer = 1
index = 0
while (a[index] == answer):
  answer += 1
  index += 1

print(answer)


#!/usr/bin/env python3

n = int(input())
a = list(map(int, input().split()))
a.sort()
a.append(-1)

answer = 1
index = 0
while (a[index] == answer):
  answer += 1
  index += 1

print(answer)


input(); vals = [*map(int, input().split())]
vals.sort()

for i in range(1, len(vals)):
    if vals[i] - vals[i-1] != 1:
        print(vals[i-1] + 1)
        exit(0)


input(); vals = [*map(int, input().split())]
vals.sort()

if vals[0] != 1:
    print(1)

else:
    for i in range(1, len(vals)):
        if vals[i] - vals[i-1] != 1:
            print(vals[i-1] + 1)
            exit(0)

    print(vals[-1]+1)


input(); vals = [*map(int, input().split())]
vals.sort()

for i in range(1, len(vals)):
    if vals[i] - vals[i-1] != 1:
        print(vals[i-1] + 1)
        exit(0)

print(vals[-1]+1)


n,i=int(input()),1
#cont=sorted([int(item) for item in input().split()])
for item in sorted([int(item) for item in input().split()]):
    if i!=item:
        break
    i+=1
print(i)


#include <iostream>
#include<set>
using namespace std;

int main()
{
    int n,item,i=1,maxItem=1;
    cin>>n;
    set<int> myset;
    for(int i=0; i<n; i++)
    {
        cin>>item;
        if(maxItem<item)
            maxItem=item;
        myset.insert(item);
    }
    /*
    for ( int item:myset)
    {
        if(i!=item)
            break;
        i++;
    }*/
    for(; i<=maxItem; i++)
        if(!myset.count(i))break;
    cout<<i;
    return 0;
}


n,i=int(input()),1
cont=sorted([int(item) for item in input().split()])
maxVal = max(cont)
while i <= maxVal:
    if i not in cont:
        break
    i+=1

print(i)


def seq(n,l):
    a= l[0]
    if a!=1:
        return 1

    for i in range(1,n):
        if l[i]!=a+1:
            return a+1
        else:
            a=a+1
    return l[-1]+1

n= int(input())
l= list(map(int, input().split()))
l.sort()
print(seq(n,l))


def seq(n,l):
    a= l[0]
    for i in range(1,n):
        if l[i]!=a+1:
            return a+1
        else:
            a=a+1
    return l[-1]+1

n= int(input())
l= list(map(int, input().split()))
l.sort()
print(seq(n,l))


n=int(input())
l=list(map(int,input().split()))
l.sort()
j=0
ans=0
for i in range(1,3000):
    if i==l[j]:
        j=j+1 if j<len(l)-1 else j
    else:
        ans=i
        break
print(ans)


n=int(input())
l=list(map(int,input().split()))
l.sort()
j=0
ans=0
for i in range(1,3002):
    if i==l[j]:
        j=j+1 if j<len(l)-1 else j
    else:
        ans=i
        break
print(ans)


 #include <bits/stdc++.h>

using namespace std;

const int N = 3e5;

int no, k, n, startsAt[N + 1], len[N + 1], id[N + 1], Rank[N + 1], minkIdx[N + 1];
string s, tmp;
long long ans[N + 1];
set<int> st;
vector<int> stringIdxSuff[N + 1];

struct SuffixArray {
    string a;
    int N, m;
    vector<int> SA, LCP, x, y, w, c;

    SuffixArray(string _a, int m) : a(" " + _a), N(a.length()), m(m),
            SA(N), LCP(N), x(N), y(N), w(max(m, N)), c(N) {
        a[0] = 0;
        DA();
        kasaiLCP();
        #define REF(X) { rotate(X.begin(), X.begin()+1, X.end()); X.pop_back(); }
        REF(SA); REF(LCP);
        a = a.substr(1, a.size());
        for(int i = 0; i < (int) SA.size(); ++i) --SA[i];
        #undef REF
    }

    inline bool cmp (const int a, const int b, const int l) { return (y[a] == y[b] && y[a + l] == y[b + l]); }

    void Sort() {
        for(int i = 0; i < m; ++i) w[i] = 0;
        for(int i = 0; i < N; ++i) ++w[x[y[i]]];
        for(int i = 0; i < m - 1; ++i) w[i + 1] += w[i];
        for(int i = N - 1; i >= 0; --i) SA[--w[x[y[i]]]] = y[i];
    }

    void DA() {
        for(int i = 0; i < N; ++i) x[i] = a[i], y[i] = i;
        Sort();
        for(int i, j = 1, p = 1; p < N; j <<= 1, m = p) {
            for(p = 0, i = N - j; i < N; i++) y[p++] = i;
            for (int k = 0; k < N; ++k) if (SA[k] >= j) y[p++] = SA[k] - j;
            Sort();
            for(swap(x, y), p = 1, x[SA[0]] = 0, i = 1; i < N; ++i)
                x[SA[i]] = cmp(SA[i - 1], SA[i], j) ? p - 1 : p++;
        }
    }

    void kasaiLCP() {
        for (int i = 0; i < N; i++) c[SA[i]] = i;
        for (int i = 0, j, k = 0; i < N; LCP[c[i++]] = k)
            if (c[i] > 0) for (k ? k-- : 0, j = SA[c[i] - 1]; a[i + k] == a[j + k]; k++);
            else k = 0;
    }
};

const int LOG_MAX = 20;

int sparse_table[N + 1][LOG_MAX], log2Val[N + 1];

void preprocess(SuffixArray& sa){
	for(int i = 2; i <= n; i ++) {
		log2Val[i] = log2Val[i >> 1] + 1;
	}
	for(int i = 0; i < n; i ++){
		sparse_table[i][0] = sa.LCP[i];
	}
	for(int j = 1; j < LOG_MAX; j ++){
		for(int i = 0; i + (1 << j) - 1 < n; i ++){
			sparse_table[i][j] = min(sparse_table[i][j-1], sparse_table[i + (1 << (j-1) )][j-1]);
		}
	}
	if(k == 1) minkIdx[no - 1] = no - 1;
	else minkIdx[no - 1] = -1;
	st.insert(no - 1);
	for(int i = no; i < n; i ++) {
		int strId = id[sa.SA[i]];
		if(st.size() == 0) {
			minkIdx[i] = -1;
			continue;
		}
		int smallestIdx = *st.begin();
		// int i1 = lower_bound(stringIdxSuff[strId].begin(), stringIdxSuff[strId].end(), smallestIdx) - stringIdxSuff[strId].begin();
		// int i2 = lower_bound(stringIdxSuff[strId].begin(), stringIdxSuff[strId].end(), i) - stringIdxSuff[strId].begin();
		for(int idx : stringIdxSuff[strId]) {
			st.erase(idx);
		}
		stringIdxSuff[strId].push_back(i);
		st.insert(i);
		while(st.size() > k) {
			st.erase(st.begin());
		}
		if(st.size() < k) minkIdx[i] = -1;
		else minkIdx[i] = *st.begin();
	}
}

inline int sparseQuery(int l, int r){
	int length = r - l + 1, log_val = log2Val[length];
	return min(sparse_table[l][log_val], sparse_table[r - (1 << log_val) + 1][log_val]);
}

inline int leftMostIndex(int idxInSuffArray, int len) {
	// int lo = 0, mid, hi = idxInSuffArray + 1;
	// while(lo < hi) {
	// 	mid = (lo + hi) >> 1;
	// 	if(sparseQuery(mid, idxInSuffArray) >= len) hi = mid;
	// 	else lo = mid + 1;
	// }
	// return lo - 1;
	if(!idxInSuffArray) return idxInSuffArray;
	int lo = 0, mid, hi = idxInSuffArray - 1;
	while(lo < hi) {
		mid = lo + (hi - lo) / 2;
		if(sparseQuery(mid + 1, idxInSuffArray) >= len) {
			hi = mid;
		}
		else lo = mid + 1;
	}
	if(sparseQuery(lo + 1, idxInSuffArray) < len) {
		return idxInSuffArray;
	}
	return lo;
}

inline int rightMostIndex(int idxInSuffArray, int len) {
	// int lo = idxInSuffArray, mid, hi = n - 1;
	// while(lo < hi) {
	// 	mid = (lo + hi + 1) >> 1;
	// 	if(sparseQuery(idxInSuffArray + 1, mid) >= len) lo = mid;
	// 	else hi = mid - 1;
	// }
	// return lo;
	if(idxInSuffArray == n - 1) return idxInSuffArray;
	int lo = idxInSuffArray + 1, mid, hi = n - 1;
	while(lo < hi) {
		mid = lo + (hi - lo + 1) / 2;
		if(sparseQuery(idxInSuffArray + 1, mid) >= len) {
			lo = mid;
		}
		else hi = mid - 1;
	}
	if(sparseQuery(idxInSuffArray + 1, lo) < len) {
		return idxInSuffArray;
	}
	return lo;
}

int main(int argc, char const *argv[]){

	std::ios_base::sync_with_stdio(false);

	cin >> no >> k;

	for(int i = 0; i < no; i ++) {
		cin >> tmp;
		startsAt[i] = s.size();
		len[i] = tmp.size();
		for(int j = startsAt[i]; j < startsAt[i] + len[i]; j ++) {
			id[j] = i;
		}
		s += tmp + "#";
	}

	s.pop_back();
	n = s.size();

	SuffixArray sa(s, 128);

	for(int i = 0; i < n; i ++) {
		Rank[sa.SA[i]] = i;
		//stringIdxSuff[id[sa.SA[i]]].push_back(i);
	}

	preprocess(sa);

	for(int i = no - 1; i < n; i ++) {
		int suf = sa.SA[i];
		int idString = id[suf];
		int sufLen = len[idString] - (suf - startsAt[idString]);
		int lo = 0, mid, hi = sufLen;
		while(lo < hi) {
			mid = lo + (hi - lo + 1) / 2;
			int leftIdx = leftMostIndex(i, mid);
			int rightIdX = rightMostIndex(i, mid);
			if(minkIdx[rightIdX] >= leftIdx) {
				lo = mid;
			}
			else hi = mid - 1;
		}
		ans[idString] += lo;
	}

	for(int i = 0; i < no; i ++) {
		cout << ans[i] << ' ';
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int N = 1e5 + 10, SIG = 27;

int n, k;
char *head[N];
char s[N + N];
int len[N];

struct Node {
  Node *ch[SIG], *fa;
  int cnt;
  int ed;
  int len;
  Node() : len(0), fa(NULL), cnt(0), ed(0) {
    memset(ch, 0, sizeof(ch));
  }
} pool[(N << 1) + 5], *last, *root;

namespace SAM {

int cnt;

void init() {
  if (cnt)
    for (int i = 0; i < cnt; ++i)
      pool[i] = Node();
  cnt = 1;
  root = &pool[0];
  last = root;
}

void add(int c, int stamp) {
  Node *np = &pool[cnt++];
  np->cnt = 1;
  np->ed = stamp;
  np->len = last->len + 1;
  Node *p = last;
  while (p && !p->ch[c]) {
    p->ch[c] = np;
    p = p->fa;
  }
  if (!p) {
    np->fa = root;
  } else {
    Node *q = p->ch[c];
    if (q->len != p->len + 1) {
      Node *nq = &pool[cnt++];
      nq->len = p->len + 1;
      for (int i = 0; i < SIG; ++i) nq->ch[i] = q->ch[i];
      nq->fa = q->fa;
      nq->cnt = q->cnt;
      nq->ed = q->ed;
      //nq->sz should be 0 because q->fa = nq, in later code q->sz will be added to q->fa->sz i.e. nq->sz, so no nq->sz = q->sz
      //nq->sz = q->sz;
      q->fa = np->fa = nq;
      while (p && p->ch[c] == q) {
	p->ch[c] = nq;
	p = p->fa;
      }
    } else {
      np->fa = q;
    }
    Node *p = np;
    while (p) {
      if (p->ed < stamp) {
        p->cnt++;
        p->ed = stamp;
      }
      p = p->fa;
    }
  }
  last = np;
}

llint go(char *s, int n) {
  auto p = root;
  llint ret = 0;
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    if (p->ch[c]) {
      p = p->ch[c];
    } else {
      for (; p && !p->ch[c]; p = p->fa);
      if (p) {
        p = root;
      } else {
        p = p->ch[c];
      }
    }
    while (p) {
      if (p->cnt >= k) {
        eput(p->cnt);
        eput(p->len);
        ret += p->len;
        break;
      }
      p = p->fa;
    }
  }
  return ret;
}

}

void run() {
  scanf("%d%d", &n, &k);
  char *p = s;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", p);
    head[i] = p;
    len[i] = strlen(p);
    p += len[i] + 1;
  }

  SAM::init();
  for (int i = 1; i <= n; ++i) {
    len[i] = strlen(head[i]);
    for (int j = 0; j < len[i]; ++j)
      SAM::add(head[i][j] - 'a', i);
    SAM::add(26, i);
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld%c", SAM::go(head[i], len[i]), " \n"[i == n]);
  }

}

int main() {
  run();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int N = 2e5 + 10;

set <int> *merge(set <int> *a, set <int> *b) {
  if (a->size() < b->size()) swap(a, b);
  a->insert((*b).begin(), (*b).end());
  delete b;
  return a;
}

struct Node {
  Node *go[27], *par;
  int val, id;
  vector <Node *> ch;
  Node() {
    memset(go, 0, sizeof(go));
    val = 0;
    par = 0;
    id = -1;
  }
  int n_include;

  set <int> *dfs() {
    set <int> *st = new set <int>();
    if (id != -1) st->insert(id);
    for (auto it = ch.begin(); it != ch.end(); ++it) {
      st = merge(st, (*it)->dfs());
    }
    n_include = st->size();
    return st;
  }

};

Node pool[N * 2 + 10], *cur = pool;
Node *root, *last;

void init() {
  root = last = cur++;
}

void extend(int w, int id = -1) {
  Node *p = last, *np = cur++;
  np->val = p->val + 1;
  np->id = id;
  while (p && p->go[w] == 0) p->go[w] = np, p = p->par;
  if (p == 0) {
    np->par = root;
  } else {
    Node *q = p->go[w];
    if (q->val == p->val + 1)
      np->par = q;
    else {
      Node *nq = cur++;
      memcpy(nq->go, q->go, sizeof(q->go));
      nq->val = p->val + 1;
      nq->par = q->par;
      q->par = np->par = nq;
      while (p && p->go[w] == q)
        p->go[w] = nq, p = p->par;
    }
  }
  last = np;
}

string a[N];
int n, k;

void run() {
  init();
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    for (int j = 0; j < a[i].size(); ++j) {
      extend(a[i][j] - 'a', i);
    }
    extend(26);
  }

  for (Node *i = pool; i != cur; ++i) {
    if (i->par)
      i->par->ch.push_back(i);
  }

  root->dfs();

  for (int i = 0; i < n; ++i) {
    string &s = a[i];
    int l = 0;
    Node *c = root;
    llint ans = 0;
    for (int j = 0; j < s.size(); ++j) {
      int w = s[j] - 'a';
      while (c && c->go[w] == 0) {
        c = c->par;
        if (c)
          l = c->val;
      }
      if (c) {
        c = c->go[w];
        ++l;
      } else {
        c = root;
        l = 0;
      }
      while (c && c->n_include < k) {
        c = c->par;
        if (c) l = c->val;
        else l = 0;
      }
      ans += l;
    }
    cout << ans << " ";
  }

  cout << endl;
}

int main() {
  run();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int N = 1e5 + 10, SIG = 27, L = 20;

namespace SA {
int sa[N], rk[N], ht[N], s[N << 1], t[N << 1], p[N], cnt[N], cur[N];
#define pushS(x) sa[cur[s[x]]--] = x
#define pushL(x) sa[cur[s[x]]++] = x
#define inducedSort(v) fill_n(sa, n, -1); fill_n(cnt, m, 0); \
  for (int i = 0; i < n; ++i) cnt[s[i]]++; \
  for (int i = 1; i < m; ++i) cnt[i] += cnt[i - 1]; \
  for (int i = 0; i < m; ++i) cur[i] = cnt[i] - 1; \
  for (int i = n1 - 1; ~i; --i) pushS(v[i]); \
  for (int i = 1; i < m; ++i) cur[i] = cnt[i - 1]; \
  for (int i = 0; i < n; ++i) if (sa[i] > 0 && t[sa[i] - 1]) pushL(sa[i] - 1); \
  for (int i = 0; i < m; ++i) cur[i] = cnt[i] - 1; \
  for (int i = n - 1; ~i; --i) if (sa[i] > 0 && !t[sa[i] - 1]) pushS(sa[i] - 1)

void sais(int n, int m, int *s, int *t, int *p) {
  int n1 = t[n - 1] = 0, ch = rk[0] = -1, *s1 = s + n;
  for (int i = n - 1; ~i; --i) t[i] = s[i] == s[i + 1] ? t[i + 1] : s[i] > s[i + 1];
  for (int i = 1; i < n; ++i) rk[i] = t[i - 1] && !t[i] ? (p[n1] = i, n1++) : -1;
  inducedSort(p);
  for (int i = 0, x, y; i < n; ++i) if (~(x = rk[sa[i]])) {
    if (ch < 1 || p[x + 1] - p[x] != p[y + 1] - p[y]) ch++;
    else for (int j = p[x], k = p[y]; j <= p[x + 1]; ++j, ++k)
      if ((s[j] << 1 | t[j]) != (s[k] << 1 | t[k])) {ch++; break;}
    s1[y = x] = ch;
  }
  if (ch + 1 < n1) sais(n1, ch + 1, s1, t + n, p + n1);
  else for (int i = 0; i < n1; ++i) sa[s1[i]] = i;
  for (int i = 0; i < n1; ++i) s1[i] = p[sa[i]];
  inducedSort(s1);
}
template<typename T>
int mapCharToInt(int n, const T *str) {
  int m = *max_element(str, str + n);
  fill_n(rk, m + 1, 0);
  for (int i = 0; i < n; ++i) rk[str[i]] = 1;
  for (int i = 0; i < m; ++i) rk[i + 1] += rk[i];
  for (int i = 0; i < n; ++i) s[i] = rk[str[i]] - 1;
  return rk[m];
}
template<typename T>
void suffixArray(int n, const T *str) {
  int m = mapCharToInt(++n, str);
  sais(n, m, s, t, p);
  for (int i = 0; i < n; ++i) rk[sa[i]] = i;
  for (int i = 0, h = ht[0] = 0; i < n - 1; ++i) {
    int j = sa[rk[i] - 1];
    while (i + h < n && j + h < n && s[i + h] == s[j + h]) ++h;
    if (ht[rk[i]] = h) h--;
  }
}
}

int n, k;
char *head[N];
char s[N + N];
int a[N + N];
int len[N];
int bl[N], bl1[N];
llint ans[N];
int len1[N], lenn[N];

struct Rmq {
  int n;
  int mn[N][L];

  void init(int a[N], int _n) {
    n = _n;
    for (int i = 2; i <= n; ++i) {
      mn[i][0] = a[i];
    }
    for (int j = 1; j < L; ++j) {
      for (int i = 2; i <= n; ++i) {
        int t = i + (1 << (j - 1));
        if (t > n) t = n;
        mn[i][j] = min(mn[i][j - 1], mn[t][j - 1]);
      }
    }
  }

  int rmq(int a, int b) {
    int d = b - a + 1;
    int t = 31 - __builtin_clz(d);
    return min(mn[a][t], mn[b - (1 << t) + 1][t]);
  }

  int pre(int a, int k) {
    int l = 1, r = a;
    while (l + 1 < r) {
      int mid = (l + r) >> 1;
      if (rmq(mid, a) >= k) {
        r = mid;
      } else {
        l = mid;
      }
    }
    return r;
  }

  int nxt(int a, int k) {
    int l = a, r = n + 1;
    while (l + 1 < r) {
      int mid = (l + r) >> 1;
      if (rmq(a, mid) >= k) {
        l = mid;
      } else {
        r = mid;
      }
    }
    return l;
  }

} rmq;

int ll[N];

void go_ll(int nn) {
  int l = 1;
  map <int, int> mp;
  bool ok = 0;

  for (int r = 1; r <= nn; ++r) {
    if (bl[r] != 0)
      mp[bl[r]] = r;
    for (; l <= r; ++l) {
      if (mp.size() >= k) {
        ok = 1;
        if (bl[l] == 0) {
        } else if (mp[bl[l]] == l) {
          mp.erase(bl[l]);
        }
        eput(l, bl[l]);
        eput(mp.size());
      } else {
        break;
      }
    }
    if (ok)
      ll[r] = l - 1;
    else
      ll[r] = -1;
  }

}

bool check(int x, int i) {
  int l;
  if (i == 1) l = 1;
  else {
    l = rmq.pre(i, x);
    if (rmq.rmq(l, i) >= x) --l;
  }
  int r;
  if (i == rmq.n) r = rmq.n;
  else {
    int t = rmq.nxt(i + 1, x);
    eput("hi", i, t, rmq.rmq(i + 1, t));
    if (rmq.rmq(i + 1, t) >= x) {
      r = t;
    } else {
      r = i;
    }
  }

  eput("ed", x, i, l, r);
  return ll[r] >= l;
}

int binary_s(int i) {
  int l = 0, r = lenn[i] + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    eput(mid, i, check(mid, i));
    if (check(mid, i)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  return l;
}

void run() {
  scanf("%d%d", &n, &k);
  char *p = s;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", p);
    head[i] = p;
    len[i] = strlen(p);
    p += len[i] + 1;
  }

  int nn = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < len[i]; ++j) {
      bl1[nn] = i;
      len1[nn] = len[i] - j;
      a[nn++] = head[i][j] - 'a';
    }
    a[nn++] = i + 26;
  }

  SA::suffixArray(nn, a);

  for (int i = 0; i <= nn; ++i) eput(SA::sa[i]);
  for (int i = 1; i <= nn; ++i) eput(SA::rk[i]);
  for (int i = 1; i <= nn; ++i) eput(SA::ht[i]);
  SA::ht[1] = 0;

  for (int i = 1; i <= nn; ++i) {
    bl[i] = bl1[SA::sa[i]];
    lenn[i] = len1[SA::sa[i]];
  }

  rmq.init(SA::ht, nn);
  go_ll(nn);

  for (int i = 1; i <= nn; ++i) eput(i, ll[i]);

  for (int i = 1; i <= nn; ++i) {
    int t = bl[i];
    int l = binary_s(i);
    eput(t, i, l);
    ans[t] += l;
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld%c", ans[i], " \n"[i == n]);
  }

}

int main() {
  run();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int N = 2e5 + 10, SIG = 27, L = 20;

namespace SA {
int sa[N], rk[N], ht[N], s[N << 1], t[N << 1], p[N], cnt[N], cur[N];
#define pushS(x) sa[cur[s[x]]--] = x
#define pushL(x) sa[cur[s[x]]++] = x
#define inducedSort(v) fill_n(sa, n, -1); fill_n(cnt, m, 0); \
  for (int i = 0; i < n; ++i) cnt[s[i]]++; \
  for (int i = 1; i < m; ++i) cnt[i] += cnt[i - 1]; \
  for (int i = 0; i < m; ++i) cur[i] = cnt[i] - 1; \
  for (int i = n1 - 1; ~i; --i) pushS(v[i]); \
  for (int i = 1; i < m; ++i) cur[i] = cnt[i - 1]; \
  for (int i = 0; i < n; ++i) if (sa[i] > 0 && t[sa[i] - 1]) pushL(sa[i] - 1); \
  for (int i = 0; i < m; ++i) cur[i] = cnt[i] - 1; \
  for (int i = n - 1; ~i; --i) if (sa[i] > 0 && !t[sa[i] - 1]) pushS(sa[i] - 1)

void sais(int n, int m, int *s, int *t, int *p) {
  int n1 = t[n - 1] = 0, ch = rk[0] = -1, *s1 = s + n;
  for (int i = n - 1; ~i; --i) t[i] = s[i] == s[i + 1] ? t[i + 1] : s[i] > s[i + 1];
  for (int i = 1; i < n; ++i) rk[i] = t[i - 1] && !t[i] ? (p[n1] = i, n1++) : -1;
  inducedSort(p);
  for (int i = 0, x, y; i < n; ++i) if (~(x = rk[sa[i]])) {
    if (ch < 1 || p[x + 1] - p[x] != p[y + 1] - p[y]) ch++;
    else for (int j = p[x], k = p[y]; j <= p[x + 1]; ++j, ++k)
      if ((s[j] << 1 | t[j]) != (s[k] << 1 | t[k])) {ch++; break;}
    s1[y = x] = ch;
  }
  if (ch + 1 < n1) sais(n1, ch + 1, s1, t + n, p + n1);
  else for (int i = 0; i < n1; ++i) sa[s1[i]] = i;
  for (int i = 0; i < n1; ++i) s1[i] = p[sa[i]];
  inducedSort(s1);
}
template<typename T>
int mapCharToInt(int n, const T *str) {
  int m = *max_element(str, str + n);
  fill_n(rk, m + 1, 0);
  for (int i = 0; i < n; ++i) rk[str[i]] = 1;
  for (int i = 0; i < m; ++i) rk[i + 1] += rk[i];
  for (int i = 0; i < n; ++i) s[i] = rk[str[i]] - 1;
  return rk[m];
}
template<typename T>
void suffixArray(int n, const T *str) {
  int m = mapCharToInt(++n, str);
  sais(n, m, s, t, p);
  for (int i = 0; i < n; ++i) rk[sa[i]] = i;
  for (int i = 0, h = ht[0] = 0; i < n - 1; ++i) {
    int j = sa[rk[i] - 1];
    while (i + h < n && j + h < n && s[i + h] == s[j + h]) ++h;
    if (ht[rk[i]] = h) h--;
  }
}
}

int n, k;
char *head[N];
char s[N];
int a[N];
int len[N];
int bl[N], bl1[N];
llint ans[N];
int len1[N], lenn[N];

struct Rmq {
  int n;
  int mn[N][L];

  void init(int a[N], int _n) {
    n = _n;
    for (int i = 2; i <= n; ++i) {
      mn[i][0] = a[i];
    }
    for (int j = 1; j < L; ++j) {
      for (int i = 2; i <= n; ++i) {
        int t = i + (1 << (j - 1));
        if (t > n) t = n;
        mn[i][j] = min(mn[i][j - 1], mn[t][j - 1]);
      }
    }
  }

  int rmq(int a, int b) {
    int d = b - a + 1;
    int t = 31 - __builtin_clz(d);
    return min(mn[a][t], mn[b - (1 << t) + 1][t]);
  }

  int pre(int a, int k) {
    int l = 1, r = a;
    while (l + 1 < r) {
      int mid = (l + r) >> 1;
      if (rmq(mid, a) >= k) {
        r = mid;
      } else {
        l = mid;
      }
    }
    return r;
  }

  int nxt(int a, int k) {
    int l = a, r = n + 1;
    while (l + 1 < r) {
      int mid = (l + r) >> 1;
      if (rmq(a, mid) >= k) {
        l = mid;
      } else {
        r = mid;
      }
    }
    return l;
  }

} rmq;

int ll[N];

void go_ll(int nn) {
  int l = 1;
  map <int, int> mp;
  bool ok = 0;

  for (int r = 1; r <= nn; ++r) {
    if (bl[r] != 0)
      mp[bl[r]] = r;
    for (; l <= r; ++l) {
      if (mp.size() >= k) {
        ok = 1;
        if (bl[l] == 0) {
        } else if (mp[bl[l]] == l) {
          mp.erase(bl[l]);
        }
        eput(l, bl[l]);
        eput(mp.size());
      } else {
        break;
      }
    }
    if (ok)
      ll[r] = l - 1;
    else
      ll[r] = -1;
  }

}

bool check(int x, int i) {
  int l;
  if (i == 1) l = 1;
  else {
    l = rmq.pre(i, x);
    if (rmq.rmq(l, i) >= x) --l;
  }
  int r;
  if (i == rmq.n) r = rmq.n;
  else {
    int t = rmq.nxt(i + 1, x);
    eput("hi", i, t, rmq.rmq(i + 1, t));
    if (rmq.rmq(i + 1, t) >= x) {
      r = t;
    } else {
      r = i;
    }
  }

  eput("ed", x, i, l, r);
  return ll[r] >= l;
}

int binary_s(int i) {
  int l = 0, r = lenn[i] + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    eput(mid, i, check(mid, i));
    if (check(mid, i)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  return l;
}

void run() {
  scanf("%d%d", &n, &k);
  char *p = s;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", p);
    head[i] = p;
    len[i] = strlen(p);
    p += len[i] + 1;
  }

  int nn = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < len[i]; ++j) {
      bl1[nn] = i;
      len1[nn] = len[i] - j;
      a[nn++] = head[i][j] - 'a';
    }
    a[nn++] = i + 26;
  }

  SA::suffixArray(nn, a);

  for (int i = 0; i <= nn; ++i) eput(SA::sa[i]);
  for (int i = 1; i <= nn; ++i) eput(SA::rk[i]);
  for (int i = 1; i <= nn; ++i) eput(SA::ht[i]);
  SA::ht[1] = 0;

  for (int i = 1; i <= nn; ++i) {
    bl[i] = bl1[SA::sa[i]];
    lenn[i] = len1[SA::sa[i]];
  }

  rmq.init(SA::ht, nn);
  go_ll(nn);

  for (int i = 1; i <= nn; ++i) eput(i, ll[i]);

  for (int i = 1; i <= nn; ++i) {
    int t = bl[i];
    int l = binary_s(i);
    eput(t, i, l);
    ans[t] += l;
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld%c", ans[i], " \n"[i == n]);
  }

}

int main() {
  run();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int N = 1e5 + 10, SIG = 27;

int n, k;
char *head[N];
char s[N + N];
int len[N];

struct Node {
  Node *ch[SIG], *fa;
  int cnt;
  int ed;
  int len;
  Node() : len(0), fa(NULL), cnt(0), ed(0) {
    memset(ch, 0, sizeof(ch));
  }
} pool[(N << 1) + 5], *last, *root;

namespace SAM {

int cnt;

void init() {
  if (cnt)
    for (int i = 0; i < cnt; ++i)
      pool[i] = Node();
  cnt = 1;
  root = &pool[0];
  last = root;
}

void add(int c, int stamp) {
  Node *np = &pool[cnt++];
  np->cnt = 1;
  np->ed = stamp;
  np->len = last->len + 1;
  Node *p = last;
  while (p && !p->ch[c]) {
    p->ch[c] = np;
    p = p->fa;
  }
  if (!p) {
    np->fa = root;
  } else {
    Node *q = p->ch[c];
    if (q->len != p->len + 1) {
      Node *nq = &pool[cnt++];
      nq->len = p->len + 1;
      for (int i = 0; i < SIG; ++i) nq->ch[i] = q->ch[i];
      nq->fa = q->fa;
      nq->cnt = q->cnt;
      nq->ed = q->ed;
      //nq->sz should be 0 because q->fa = nq, in later code q->sz will be added to q->fa->sz i.e. nq->sz, so no nq->sz = q->sz
      //nq->sz = q->sz;
      q->fa = np->fa = nq;
      while (p && p->ch[c] == q) {
	p->ch[c] = nq;
	p = p->fa;
      }
    } else {
      np->fa = q;
    }
    Node *p = np;
    while (p) {
      if (p->ed < stamp) {
        p->cnt++;
        p->ed = stamp;
      }
      p = p->fa;
    }
  }
  last = np;
}

llint go(char *s, int n) {
  auto p = root;
  llint ret = 0;
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    if (p->ch[c]) {
      p = p->ch[c];
    } else {
      for (; p && !p->ch[c]; p = p->fa);
      if (p) {
        p = root;
      } else {
        p = p->ch[c];
      }
    }
    while (p) {
      if (p->cnt >= k) {
        eput(p->cnt);
        eput(p->len);
        ret += min(i + 1, p->len);
        break;
      }
      p = p->fa;
    }
    if (!p) p = root;
  }
  return ret;
}

}

void run() {
  scanf("%d%d", &n, &k);
  char *p = s;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", p);
    head[i] = p;
    len[i] = strlen(p);
    p += len[i] + 1;
  }

  SAM::init();
  for (int i = 1; i <= n; ++i) {
    len[i] = strlen(head[i]);
    for (int j = 0; j < len[i]; ++j)
      SAM::add(head[i][j] - 'a', i);
    SAM::add(26, i);
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld%c", SAM::go(head[i], len[i]), " \n"[i == n]);
  }

}

int main() {
  run();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int N = 1e5 + 10, SIG = 27;

int n, k;
char *head[N];
char s[N + N];
int len[N];

struct Node {
  Node *ch[SIG], *fa;
  int cnt;
  int ed;
  int len;
  Node() : len(0), fa(NULL), cnt(0), ed(0) {
    memset(ch, 0, sizeof(ch));
  }
} pool[((N + N) << 1) + 5], *last, *root;

namespace SAM {

int cnt;

void init() {
  if (cnt)
    for (int i = 0; i < cnt; ++i)
      pool[i] = Node();
  cnt = 1;
  root = &pool[0];
  last = root;
}

void add(int c, int stamp) {
  Node *np = &pool[cnt++];
  np->cnt = 1;
  np->ed = stamp;
  np->len = last->len + 1;
  Node *p = last;
  while (p && !p->ch[c]) {
    p->ch[c] = np;
    p = p->fa;
  }
  if (!p) {
    np->fa = root;
  } else {
    Node *q = p->ch[c];
    if (q->len != p->len + 1) {
      Node *nq = &pool[cnt++];
      nq->len = p->len + 1;
      for (int i = 0; i < SIG; ++i) nq->ch[i] = q->ch[i];
      nq->fa = q->fa;
      nq->cnt = q->cnt;
      nq->ed = q->ed;
      //nq->sz should be 0 because q->fa = nq, in later code q->sz will be added to q->fa->sz i.e. nq->sz, so no nq->sz = q->sz
      //nq->sz = q->sz;
      q->fa = np->fa = nq;
      while (p && p->ch[c] == q) {
	p->ch[c] = nq;
	p = p->fa;
      }
    } else {
      np->fa = q;
    }
    Node *p = np;
    while (p) {
      if (p->ed < stamp) {
        p->cnt++;
        p->ed = stamp;
      }
      p = p->fa;
    }
  }
  last = np;
}

llint go(char *s, int n) {
  auto p = root;
  llint ret = 0;
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    if (p->ch[c]) {
      p = p->ch[c];
    } else {
      for (; p && !p->ch[c]; p = p->fa);
      if (p) {
        p = root;
      } else {
        p = p->ch[c];
      }
    }
    while (p) {
      if (p->cnt >= k) {
        eput(p->cnt);
        eput(p->len);
        ret += min(i + 1, p->len);
        break;
      }
      p = p->fa;
    }
    if (!p) p = root;
  }
  return ret;
}

}

void run() {
  scanf("%d%d", &n, &k);
  char *p = s;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", p);
    head[i] = p;
    len[i] = strlen(p);
    p += len[i] + 1;
  }

  SAM::init();
  for (int i = 1; i <= n; ++i) {
    len[i] = strlen(head[i]);
    for (int j = 0; j < len[i]; ++j)
      SAM::add(head[i][j] - 'a', i);
    SAM::add(26, i);
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld%c", SAM::go(head[i], len[i]), " \n"[i == n]);
  }

}

int main() {
  run();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

using llint = long long;

const int N = 1e5 + 10, SIG = 27;

int n, k;
char *head[N];
char s[N + N];
int len[N];

struct Node {
  Node *ch[SIG], *fa;
  int cnt;
  int ed;
  int len;
  Node() : len(0), fa(NULL), cnt(0), ed(0) {
    memset(ch, 0, sizeof(ch));
  }
} pool[(N << 1) + 5], *last, *root;

namespace SAM {

int cnt;

void init() {
  if (cnt)
    for (int i = 0; i < cnt; ++i)
      pool[i] = Node();
  cnt = 1;
  root = &pool[0];
  last = root;
}

void add(int c, int stamp) {
  Node *np = &pool[cnt++];
  np->cnt = 1;
  np->ed = stamp;
  np->len = last->len + 1;
  Node *p = last;
  while (p && !p->ch[c]) {
    p->ch[c] = np;
    p = p->fa;
  }
  if (!p) {
    np->fa = root;
  } else {
    Node *q = p->ch[c];
    if (q->len != p->len + 1) {
      Node *nq = &pool[cnt++];
      nq->len = p->len + 1;
      for (int i = 0; i < SIG; ++i) nq->ch[i] = q->ch[i];
      nq->fa = q->fa;
      nq->cnt = q->cnt;
      nq->ed = q->ed;
      //nq->sz should be 0 because q->fa = nq, in later code q->sz will be added to q->fa->sz i.e. nq->sz, so no nq->sz = q->sz
      //nq->sz = q->sz;
      q->fa = np->fa = nq;
      while (p && p->ch[c] == q) {
	p->ch[c] = nq;
	p = p->fa;
      }
    } else {
      np->fa = q;
    }
    Node *p = np;
    while (p) {
      if (p->ed < stamp) {
        p->cnt++;
        p->ed = stamp;
      }
      p = p->fa;
    }
  }
  last = np;
}

llint go(char *s, int n) {
  auto p = root;
  llint ret = 0;
  for (int i = 0; i < n; ++i) {
    int c = s[i] - 'a';
    if (p->ch[c]) {
      p = p->ch[c];
    } else {
      for (; p && !p->ch[c]; p = p->fa);
      if (p) {
        p = root;
      } else {
        p = p->ch[c];
      }
    }
    while (p) {
      if (p->cnt >= k) {
        eput(p->cnt);
        eput(p->len);
        ret += min(i + 1, p->len);
        break;
      }
      p = p->fa;
    }
  }
  return ret;
}

}

void run() {
  scanf("%d%d", &n, &k);
  char *p = s;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", p);
    head[i] = p;
    len[i] = strlen(p);
    p += len[i] + 1;
  }

  SAM::init();
  for (int i = 1; i <= n; ++i) {
    len[i] = strlen(head[i]);
    for (int j = 0; j < len[i]; ++j)
      SAM::add(head[i][j] - 'a', i);
    SAM::add(26, i);
  }

  for (int i = 1; i <= n; ++i) {
    printf("%lld%c", SAM::go(head[i], len[i]), " \n"[i == n]);
  }

}

int main() {
  run();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=200010;
typedef long long ll;
int n,m,tot,head[N],st[N],ed[N],col[N],siz[N];
ll val[N],ans[N];
char s[N],tem[N];
struct Edge{
	int suiv,ver;
}e[N];

inline void lnk(int x,int y)
{
	e[++tot].ver=y;
	e[tot].suiv=head[x];
	head[x]=tot;
}

struct SuffixAutomaton{
	int cnt,fa[N],len[N],c[N][26];
	SuffixAutomaton(){cnt=1;}
	inline int* operator [](const int&x){return c[x];}
//	inline int extend(int lst,int ch,int id)
//	{
//		if(c[lst][ch]&&len[lst]+1==len[c[lst][ch]])
//		{
//			for(int pos=c[lst][ch];pos && col[pos]!=id;pos=fa[pos])
//				siz[pos]++,col[pos]=id;
//			return c[lst][ch];
//		}
//		int p=lst,now=++cnt,fg=0,res;
//		len[now]=len[p]+1,bel[now]=id;
//		while(p && !c[p][ch])c[p][ch]=now,p=fa[p];
//		if(!p)fa[now]=1,res=now;
//		else
//		{
//			int q=c[p][ch],nou;
//			if(len[q]==len[p]+1)fa[now]=q;
//			else
//			{
//				if(p==lst)fg=1;
//				nou=++cnt;len[nou]=len[p]+1;
//				siz[nou]=siz[q],col[nou]=col[q];
//				memcpy(c[nou],c[q],sizeof(c[q]));
//				fa[nou]=fa[q];fa[q]=fa[now]=nou;
//				while(p && c[p][ch]==q)c[p][ch]=nou,p=fa[p];
//			}
//			res=fg?nou:now;
//		}
//		for(int pos=p;pos && col[pos]!=id;pos=fa[pos])
//			siz[pos]++,col[pos]=id;
//		return res;
//	}
	inline int extend(int lst,int ch)
	{
		if(c[lst][ch] && len[lst]+1==len[c[lst][ch]])
			return c[lst][ch];
		int p=lst,now=++cnt,fg=0;
		len[now]=len[p]+1;
		while(p && !c[p][ch])c[p][ch]=now,p=fa[p];
		if(!p)return fa[now]=1,now;
		int q=c[p][ch],nou;
		if(len[q]==len[p]+1)fa[now]=q;
		else
		{
			if(p==lst)fg=1;
			nou=++cnt;len[nou]=len[p]+1;
			memcpy(c[nou],c[q],sizeof(c[q]));
			fa[nou]=fa[q];fa[q]=fa[now]=nou;
			while(p && c[p][ch]==q)c[p][ch]=nou,p=fa[p];
		}
		return fg?nou:now;
	}
}t;

inline void update(int p,int id)
{
	for(int pos=p;pos && col[pos]!=id;pos=t.fa[pos])
		siz[pos]++,col[pos]=id;
}

inline void dfs(int x)
{
	val[x]+=val[t.fa[x]];
	for(int i=head[x];i;i=e[i].suiv)
		dfs(e[i].ver);
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1,lst,len;i<=n;i++)
	{
		scanf("%s",tem+1);
		len=strlen(tem+1);lst=1;
		st[i]=ed[i-1]+1,ed[i]=st[i]+len-1;
		for(int j=1;j<=len;j++)
			lst=t.extend(lst,tem[j]-'a'),s[st[i]+j-1]=tem[j];
	}
	for(int i=1,pos=1;i<=n;i++,pos=1)
		for(int j=st[i];j<=ed[i];j++)
			pos=t[pos][s[j]-'a'],update(pos,i);
	for(int i=2;i<=t.cnt;i++)
		lnk(t.fa[i],i),val[i]=siz[i]>=m?t.len[i]-t.len[t.fa[i]]:0;
	dfs(1);
	for(int i=1,pos=1;i<=n;i++,pos=1)
		for(int j=st[i];j<=ed[i];j++)
			pos=t[pos][s[j]-'a'],ans[i]+=val[pos];
	for(int i=1;i<=n;i++)printf("%lld\n",ans[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=100005;
int p[MX],pn[MX],c[20][MX],cn[MX],cnt[MX];
char s[MX],t[MX];
int ln[MX],pos[MX],K[MX],Q[MX],last[MX],idx[MX];
ll ans[MX];
set <int> id;
void sort_cyclic_shifts(char s[],int n)
{
    memset(cnt,0,sizeof(cnt));
    for(int i=0;i<n;i++) cnt[s[i]]++;
    for(int i=1;i<256;i++) cnt[i]+=cnt[i-1];
    for(int i=0;i<n;i++) p[--cnt[s[i]]]=i;
    int classes=1;
    for(int i=0;i<n;i++){
        if(i && s[p[i]]!=s[p[i-1]]) classes++;
        c[0][p[i]]=classes-1;
    }
    for(int k=0;(1<<k)<n;k++){
        for(int i=0;i<n;i++){
            pn[i]=p[i]-(1<<k);
            if(pn[i]<0) pn[i]+=n;
        }
        fill(cnt,cnt+classes,0);
        for(int i=0;i<n;i++) cnt[c[k][pn[i]]]++;
        for(int i=1;i<classes;i++) cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;i--) p[--cnt[c[k][pn[i]]]]=pn[i];
        classes=1,cn[p[0]]=0;
        pair <int,int> x,y;
        for(int i=1;i<n;i++){
            x={c[k][p[i]],c[k][(p[i]+(1<<k))%n]};
            y={c[k][p[i-1]],c[k][(p[i-1]+(1<<k))%n]};
            if(x!=y) classes++;
            cn[p[i]]=classes-1;
        }
        for(int i=0;i<n;i++) c[k+1][i]=cn[i];
    }
}
void calc(int n)
{
    int i=1,k=1;
    for(int i=1;i<=n;i++){
        if((1<<k)>i) K[i]=k-1;
        else K[i]=k++;
    }
}
void construct(int n,int k)
{
    int sz=0;
    memset(last,-1,sizeof(last));
    for(int i=0;i<n;i++){
        int y=idx[p[i]];
        if(last[y]<0){
            if(sz<k) id.insert(i),last[y]=i,sz++;
            else{
                int x=*(id.begin());
                last[idx[p[x]]]=-1,id.erase(x);
                id.insert(i),last[y]=i;
            }
        }
        else{
            id.erase(last[y]);
            id.insert(i),last[y]=i;
        }
        Q[i]=(sz==k)?*(id.begin()):-1;
    }
}
int compare(int i,int j,int l,int k,int n){
    pair<int,int> a={c[k][i],c[k][(i+l-(1<<k))%n]};
    pair<int,int> b={c[k][j],c[k][(j+l-(1<<k))%n]};
    return (a==b)?0:((a<b)?-1:1);
}
int comp(int i,int j,int k,int l,int n)
{
    int l1=j-i+1,l2=l-k+1;
    if(l1<=l2) return compare(k,i,l1,K[l1],n);
    int c=compare(k,i,l2,K[l2],n);
    return c?c:-1;
}
int lb(int l,int r,int i,int j,int n)
{
    if(r-l<4){
        for(int k=l;k<=r;k++){
            int c=comp(i,j,p[k],n-1,n);
            if(c==0) return k;
        }
        return -1;
    }
    int m=(l+r)>>1;
    int c=comp(i,j,p[m],n-1,n);
    if(c==0){
        int d=comp(i,j,p[m-1],n-1,n);
        if(d<0) return m;
        return lb(l,m-1,i,j,n);
    }
    if(c<1) return lb(m+1,r,i,j,n);
    return lb(l,m-1,i,j,n);
}
int ub(int l,int r,int i,int j,int n)
{
    if(r-l<4){
        for(int k=r;k>=l;k--){
            int c=comp(i,j,p[k],n-1,n);
            if(c==0) return k;
        }
        return -1;
    }
    int m=(l+r)>>1;
    int c=comp(i,j,p[m],n-1,n);
    if(c==0){
        int d=comp(i,j,p[m+1],n-1,n);
        if(d>0) return m;
        return ub(m+1,r,i,j,n);
    }
    if(c<1) return ub(m+1,r,i,j,n);
    return ub(l,m-1,i,j,n);
}
bool f(int i,int j,int n)
{
    int l=lb(0,n-1,i,j,n);
    if(l==-1) return 0;
    int r=ub(0,n-1,i,j,n);
    return (Q[r]>=l);
}
int main()
{
    int n,k,m=0;
    scanf("%d %d",&n,&k);
    strcpy(s,"");
    for(int i=0;i<n;i++){
        scanf("%s",t);
        ln[i]=strlen(t);
        strcat(t,"$");
        pos[i]=i?pos[i-1]+ln[i-1]+1:0;
        strcat(s,t),m+=ln[i]+1;
        for(int j=pos[i];j<pos[i]+ln[i];j++) idx[j]=i;
    }
    calc(100000);
    sort_cyclic_shifts(s,m);
    construct(m,k);
    for(int i=0;i<n;i++){
        int p=pos[i],q=pos[i]+ln[i]-1;
        while(p<=q){
            int l=p,r=q;
            ll len=-1;
            while(true){
                if(r-l<4){
                    for(int j=r;j>=l;j--){
                        bool z=f(p,j,m);
                        if(z){
                            len=j-p+1;
                            break;
                        }
                    }
                    break;
                }
                int mid=(l+r)>>1;
                bool z=f(p,mid,m);
                if(z) l=mid;
                else r=mid-1;
            }
            if(len>0) ans[i]+=len;
            p++;
        }
    }
    for(int i=0;i<n;i++) printf("%lld ",ans[i]);
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=200005;
int p[MX],pn[MX],c[20][MX],cn[MX],cnt[MX];
char s[MX],t[MX];
int ln[MX],pos[MX],K[MX],Q[MX],last[MX],idx[MX];
ll ans[MX];
set <int> id;
void sort_cyclic_shifts(char s[],int n)
{
    memset(cnt,0,sizeof(cnt));
    for(int i=0;i<n;i++) cnt[s[i]]++;
    for(int i=1;i<256;i++) cnt[i]+=cnt[i-1];
    for(int i=0;i<n;i++) p[--cnt[s[i]]]=i;
    int classes=1;
    for(int i=0;i<n;i++){
        if(i && s[p[i]]!=s[p[i-1]]) classes++;
        c[0][p[i]]=classes-1;
    }
    for(int k=0;(1<<k)<n;k++){
        for(int i=0;i<n;i++){
            pn[i]=p[i]-(1<<k);
            if(pn[i]<0) pn[i]+=n;
        }
        fill(cnt,cnt+classes,0);
        for(int i=0;i<n;i++) cnt[c[k][pn[i]]]++;
        for(int i=1;i<classes;i++) cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;i--) p[--cnt[c[k][pn[i]]]]=pn[i];
        classes=1,cn[p[0]]=0;
        pair <int,int> x,y;
        for(int i=1;i<n;i++){
            x={c[k][p[i]],c[k][(p[i]+(1<<k))%n]};
            y={c[k][p[i-1]],c[k][(p[i-1]+(1<<k))%n]};
            if(x!=y) classes++;
            cn[p[i]]=classes-1;
        }
        for(int i=0;i<n;i++) c[k+1][i]=cn[i];
    }
}
void calc(int n)
{
    int i=1,k=1;
    for(int i=1;i<=n;i++){
        if((1<<k)>i) K[i]=k-1;
        else K[i]=k++;
    }
}
void construct(int n,int k)
{
    int sz=0;
    memset(last,-1,sizeof(last));
    for(int i=0;i<n;i++){
        int y=idx[p[i]];
        if(last[y]<0){
            if(sz<k) id.insert(i),last[y]=i,sz++;
            else{
                int x=*(id.begin());
                last[idx[p[x]]]=-1,id.erase(x);
                id.insert(i),last[y]=i;
            }
        }
        else{
            id.erase(last[y]);
            id.insert(i),last[y]=i;
        }
        Q[i]=(sz==k)?*(id.begin()):-1;
    }
}
int compare(int i,int j,int l,int k,int n){
    pair<int,int> a={c[k][i],c[k][(i+l-(1<<k))%n]};
    pair<int,int> b={c[k][j],c[k][(j+l-(1<<k))%n]};
    return (a==b)?0:((a<b)?-1:1);
}
int comp(int i,int j,int k,int l,int n)
{
    int l1=j-i+1,l2=l-k+1;
    if(l1<=l2) return compare(k,i,l1,K[l1],n);
    int c=compare(k,i,l2,K[l2],n);
    return c?c:-1;
}
int lb(int l,int r,int i,int j,int n)
{
    if(r-l<4){
        for(int k=l;k<=r;k++){
            int c=comp(i,j,p[k],n-1,n);
            if(c==0) return k;
        }
        return -1;
    }
    int m=(l+r)>>1;
    int c=comp(i,j,p[m],n-1,n);
    if(c==0){
        int d=comp(i,j,p[m-1],n-1,n);
        if(d<0) return m;
        return lb(l,m-1,i,j,n);
    }
    if(c<1) return lb(m+1,r,i,j,n);
    return lb(l,m-1,i,j,n);
}
int ub(int l,int r,int i,int j,int n)
{
    if(r-l<4){
        for(int k=r;k>=l;k--){
            int c=comp(i,j,p[k],n-1,n);
            if(c==0) return k;
        }
        return -1;
    }
    int m=(l+r)>>1;
    int c=comp(i,j,p[m],n-1,n);
    if(c==0){
        int d=comp(i,j,p[m+1],n-1,n);
        if(d>0) return m;
        return ub(m+1,r,i,j,n);
    }
    if(c<1) return ub(m+1,r,i,j,n);
    return ub(l,m-1,i,j,n);
}
bool f(int i,int j,int n)
{
    int l=lb(0,n-1,i,j,n);
    if(l==-1) return 0;
    int r=ub(0,n-1,i,j,n);
    return (Q[r]>=l);
}
int main()
{
    int n,k,m=0;
    scanf("%d %d",&n,&k);
    strcpy(s,"");
    for(int i=0;i<n;i++){
        scanf("%s",t);
        ln[i]=strlen(t);
        strcat(t,"$");
        pos[i]=i?pos[i-1]+ln[i-1]+1:0;
        strcat(s+m,t),m+=ln[i]+1;
        for(int j=pos[i];j<pos[i]+ln[i];j++) idx[j]=i;
    }
    calc(100000);
    sort_cyclic_shifts(s,m);
    construct(m,k);
    for(int i=0;i<n;i++){
        int p=pos[i],q=pos[i]+ln[i]-1;
        while(p<=q){
            int l=p,r=q;
            ll len=-1;
            while(true){
                if(r-l<4){
                    for(int j=r;j>=l;j--){
                        bool z=f(p,j,m);
                        if(z){
                            len=j-p+1;
                            break;
                        }
                    }
                    break;
                }
                int mid=(l+r)>>1;
                bool z=f(p,mid,m);
                if(z) l=mid;
                else r=mid-1;
            }
            if(len>0) ans[i]+=len;
            p++;
        }
    }
    for(int i=0;i<n;i++) printf("%lld ",ans[i]);
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=200005;
int p[MX],pn[MX],c[20][MX],cn[MX],cnt[MX];
char s[MX],t[MX];
int ln[MX],pos[MX],K[MX],Q[MX],last[MX],idx[MX];
ll ans[MX];
set <int> id;
void sort_cyclic_shifts(char s[],int n)
{
    memset(cnt,0,sizeof(cnt));
    for(int i=0;i<n;i++) cnt[s[i]]++;
    for(int i=1;i<256;i++) cnt[i]+=cnt[i-1];
    for(int i=0;i<n;i++) p[--cnt[s[i]]]=i;
    int classes=1;
    for(int i=0;i<n;i++){
        if(i && s[p[i]]!=s[p[i-1]]) classes++;
        c[0][p[i]]=classes-1;
    }
    for(int k=0;(1<<k)<n;k++){
        for(int i=0;i<n;i++){
            pn[i]=p[i]-(1<<k);
            if(pn[i]<0) pn[i]+=n;
        }
        fill(cnt,cnt+classes,0);
        for(int i=0;i<n;i++) cnt[c[k][pn[i]]]++;
        for(int i=1;i<classes;i++) cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;i--) p[--cnt[c[k][pn[i]]]]=pn[i];
        classes=1,cn[p[0]]=0;
        pair <int,int> x,y;
        for(int i=1;i<n;i++){
            x={c[k][p[i]],c[k][(p[i]+(1<<k))%n]};
            y={c[k][p[i-1]],c[k][(p[i-1]+(1<<k))%n]};
            if(x!=y) classes++;
            cn[p[i]]=classes-1;
        }
        for(int i=0;i<n;i++) c[k+1][i]=cn[i];
    }
}
void calc(int n)
{
    int i=1,k=1;
    for(int i=1;i<=n;i++){
        if((1<<k)>i) K[i]=k-1;
        else K[i]=k++;
    }
}
void construct(int n,int k)
{
    int sz=0;
    memset(last,-1,sizeof(last));
    for(int i=0;i<n;i++){
        int y=idx[p[i]];
        if(last[y]<0){
            if(sz<k) id.insert(i),last[y]=i,sz++;
            else{
                int x=*(id.begin());
                last[idx[p[x]]]=-1,id.erase(x);
                id.insert(i),last[y]=i;
            }
        }
        else{
            id.erase(last[y]);
            id.insert(i),last[y]=i;
        }
        Q[i]=(sz==k)?*(id.begin()):-1;
    }
}
int compare(int i,int j,int l,int k,int n){
    pair<int,int> a={c[k][i],c[k][(i+l-(1<<k))%n]};
    pair<int,int> b={c[k][j],c[k][(j+l-(1<<k))%n]};
    return (a==b)?0:((a<b)?-1:1);
}
int comp(int i,int j,int k,int l,int n)
{
    int l1=j-i+1,l2=l-k+1;
    if(l1<=l2) return compare(k,i,l1,K[l1],n);
    int c=compare(k,i,l2,K[l2],n);
    return c?c:-1;
}
int lb(int l,int r,int i,int j,int n)
{
    if(r-l<4){
        for(int k=l;k<=r;k++){
            int c=comp(i,j,p[k],n-1,n);
            if(c==0) return k;
        }
        return -1;
    }
    int m=(l+r)>>1;
    int c=comp(i,j,p[m],n-1,n);
    if(c==0){
        int d=comp(i,j,p[m-1],n-1,n);
        if(d<0) return m;
        return lb(l,m-1,i,j,n);
    }
    if(c<1) return lb(m+1,r,i,j,n);
    return lb(l,m-1,i,j,n);
}
int ub(int l,int r,int i,int j,int n)
{
    if(r-l<4){
        for(int k=r;k>=l;k--){
            int c=comp(i,j,p[k],n-1,n);
            if(c==0) return k;
        }
        return -1;
    }
    int m=(l+r)>>1;
    int c=comp(i,j,p[m],n-1,n);
    if(c==0){
        int d=comp(i,j,p[m+1],n-1,n);
        if(d>0) return m;
        return ub(m+1,r,i,j,n);
    }
    if(c<1) return ub(m+1,r,i,j,n);
    return ub(l,m-1,i,j,n);
}
bool f(int i,int j,int n)
{
    int l=lb(0,n-1,i,j,n);
    if(l==-1) return 0;
    int r=ub(0,n-1,i,j,n);
    return (Q[r]>=l);
}
int main()
{
    int n,k,m=0;
    scanf("%d %d",&n,&k);
    strcpy(s,"");
    for(int i=0;i<n;i++){
        scanf("%s",t);
        ln[i]=strlen(t);
        strcat(t,"$");
        pos[i]=i?pos[i-1]+ln[i-1]+1:0;
        strcat(s,t),m+=ln[i]+1;
        for(int j=pos[i];j<pos[i]+ln[i];j++) idx[j]=i;
    }
    calc(100000);
    sort_cyclic_shifts(s,m);
    construct(m,k);
    for(int i=0;i<n;i++){
        int p=pos[i],q=pos[i]+ln[i]-1;
        while(p<=q){
            int l=p,r=q;
            ll len=-1;
            while(true){
                if(r-l<4){
                    for(int j=r;j>=l;j--){
                        bool z=f(p,j,m);
                        if(z){
                            len=j-p+1;
                            break;
                        }
                    }
                    break;
                }
                int mid=(l+r)>>1;
                bool z=f(p,mid,m);
                if(z) l=mid;
                else r=mid-1;
            }
            if(len>0) ans[i]+=len;
            p++;
        }
    }
    for(int i=0;i<n;i++) printf("%lld ",ans[i]);
    printf("\n");
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <set>
#include <iostream>
#include <cstdio>
#include <cassert>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;

const int MAXN = 250001;

int n,k;
string temp;
int str[MAXN],pos[MAXN],lcp[MAXN],sa[MAXN],gap,tmp[MAXN];
int owner[MAXN],base;
long long ans[MAXN];
int parent[MAXN];
long long len[MAXN];
int sz[MAXN];
set<int> stk[MAXN];
vector<int> p[MAXN];
long long sp[MAXN];
int chk[MAXN];
void init(){
    for(int i=0;i<MAXN;i++)parent[i]=i,p[i].push_back(i);
}

int find(int u){
    return u == parent[u] ? u : parent[u] = find(parent[u]);
}
void mrg(int u,int v){
    u=find(u) , v=find(v);
    int cost1 = p[u].size() + stk[u].size();
    int cost2 = p[v].size() + stk[v].size();
    if(cost1 > cost2){
        swap(u,v);
    }
    set<int> :: iterator it;
    for(it = stk[u].begin();it!=stk[u].end();it++){
        stk[v].insert(*it);
    }
    vector<int> ::iterator jt;
    for(jt = p[u].begin();jt!=p[u].end();jt++){
        p[v].push_back(*jt);
    }

    parent[u]=v;
    stk[u].clear();
    p[u].clear();
}
bool cmp(int i,int j){
    return pos[i]!=pos[j] ? pos[i] < pos[j] : pos[i+gap]<pos[j+gap];
}
void build_sa(){
    int N = temp.size();
    for(int i=0;i<N;i++)sa[i]=i,pos[i]=str[i];
    pos[N] = -1;
    for(gap=1;;gap*=2){
        sort(sa,sa+N,cmp);
        for(int i=1;i<N;i++) tmp[i] = tmp[i-1] + cmp(sa[i-1],sa[i]);
        for(int i=0;i<N;i++) pos[sa[i]] = tmp[i];
        if(tmp[N-1] == N-1)break;
    }
}
void build_lcp(){
    int N =temp.size();
    for(int i=0,k=0;i<N;i++){
        if(pos[i] == N-1)continue;
        for(int j = sa[pos[i]+1];str[i+k] == str[j+k];){
            k++;
        }
        lcp[pos[i]] = k;
        if(k)k--;
    }
}
vector<pair<int,int> > vx[MAXN];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    int cur = -2;
    init();
    for(int i=1;i<=n;i++){
        string ttt; cin>>ttt;
        sp[i] = ttt.size();
        temp+=ttt;
        for(int j = cur+2 ;j<temp.size();j++){
            owner[j] = i;
            cur = j;
        }

        temp+="#";
    }
    if(k>n){
        for(int i=1;i<=n;i++){
            cout<<0<<' ';
        }
        exit(0);
    }
    if(k==1){
        for(int i=1;i<=n;i++){
            cout<<1LL * sp[i] *(sp[i]+1)/2<<' ';
        }
        exit(0);
    }
    int cnt=100;
    for(int i=0;i<temp.size();i++){
        if(temp[i]>='a' && temp[i] <='z'){
            str[i] = temp[i]-'a';
        }
        else str[i] = ++cnt;
    }
    build_sa();
    build_lcp();
    for(int i=0;i<(int)temp.size()-1;i++){
        vx[lcp[i]].push_back(make_pair(i,i+1));
    }
    for(int i=0;i<temp.size();i++){
        stk[i].insert(owner[i]);
    }
    for(int i=temp.size()-1;i >= 1;i--){
        for(int j=0;j<vx[i].size();j++){
            int x = sa[vx[i][j].first];
            int y = sa[vx[i][j].second];
            mrg(x,y);
        }
        for(int j=0;j<vx[i].size();j++){
            int x = sa[vx[i][j].first];
            int y = sa[vx[i][j].second];
            int root = find(x);
            if(stk[root].size() >=k){
                vector<int>::iterator it;
                set<int>::iterator jt;
                for(it = p[root].begin();it!=p[root].end();it++){
                    ans[owner[*it]]+=i;
                }
                p[root].clear();
            }
        }
    }
    for(int i=1;i<=n;i++){
        cout<<ans[i]<<' ';
    }
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <set>
#include <iostream>
#include <cstdio>
#include <cassert>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;

const int MAXN = 400001;

int n,k;
string temp;
int str[MAXN],pos[MAXN],lcp[MAXN],sa[MAXN],gap,tmp[MAXN];
int owner[MAXN],base;
long long ans[MAXN];
int parent[MAXN];
long long len[MAXN];
int sz[MAXN];
set<int> stk[MAXN],p[MAXN];
long long sp[MAXN];
int chk[MAXN];
void init(){
    for(int i=0;i<MAXN;i++)parent[i]=i,p[i].insert(i);
}

int find(int u){
    return u == parent[u] ? u : parent[u] = find(parent[u]);
}
void mrg(int u,int v){
    u=find(u) , v=find(v);
    if(stk[u].size() > stk[v].size()){
        swap(u,v);
    }
    set<int> :: iterator it;
    for(it = stk[u].begin();it!=stk[u].end();it++){
        stk[v].insert(*it);
    }

    for(it = p[u].begin();it!=p[u].end();it++){
        p[v].insert(*it);
    }

    parent[u]=v;
    stk[u].clear();
    p[u].clear();
}
bool cmp(int i,int j){
    return pos[i]!=pos[j] ? pos[i] < pos[j] : pos[i+gap]<pos[j+gap];
}
void build_sa(){
    int N = temp.size();
    for(int i=0;i<N;i++)sa[i]=i,pos[i]=str[i];
    pos[N] = -1;
    for(gap=1;;gap*=2){
        sort(sa,sa+N,cmp);
        for(int i=1;i<N;i++) tmp[i] = tmp[i-1] + cmp(sa[i-1],sa[i]);
        for(int i=0;i<N;i++) pos[sa[i]] = tmp[i];
        if(tmp[N-1] == N-1)break;
    }
}
void build_lcp(){
    int N =temp.size();
    for(int i=0,k=0;i<N;i++){
        if(pos[i] == N-1)continue;
        for(int j = sa[pos[i]+1];str[i+k] == str[j+k];){
            k++;
        }
        lcp[pos[i]] = k;
        if(k)k--;
    }
}
vector<pair<int,int> > vx[MAXN];
int main(){
    //freopen("input.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    int cur = -2;
    init();
    for(int i=1;i<=n;i++){
        string ttt; cin>>ttt;
        sp[i] = ttt.size();
        temp+=ttt;
        for(int j = cur+2 ;j<temp.size();j++){
            owner[j] = i;
            cur = j;
        }

        temp+="#";
    }
    if(k>n){
        for(int i=1;i<=n;i++){
            cout<<0<<' ';
        }
        exit(0);
    }
    if(k==1){
        for(int i=1;i<=n;i++){
            cout<<1LL * sp[i] *(sp[i]+1)/2<<' ';
        }
        exit(0);
    }
    int cnt=100;
    for(int i=0;i<temp.size();i++){
        if(temp[i]>='a' && temp[i] <='z'){
            str[i] = temp[i]-'a';
        }
        else str[i] = ++cnt;
    }
    build_sa();
    build_lcp();
    for(int i=0;i<(int)temp.size()-1;i++){
        vx[lcp[i]].push_back(make_pair(i,i+1));
    }
    for(int i=0;i<temp.size();i++){
        stk[i].insert(owner[i]);
    }
    for(int i=temp.size()-1;i >= 1;i--){
        for(int j=0;j<vx[i].size();j++){
            int x = sa[vx[i][j].first];
            int y = sa[vx[i][j].second];
            mrg(x,y);
        }
        for(int j=0;j<vx[i].size();j++){
            int x = sa[vx[i][j].first];
            int y = sa[vx[i][j].second];
            int root = find(x);
            if(stk[root].size() >=k){
                set<int>::iterator it;
                set<int>::iterator jt;
                for(it = p[root].begin();it!=p[root].end();it++){
                    ans[owner[*it]]+=i;
                }
                p[root].clear();
            }

        }
    }
    for(int i=1;i<=n;i++){
        cout<<ans[i]<<' ';
    }
    return 0;
}