#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <limits>
#include <iomanip>
#include <bitset>

using namespace std;


#define INF 1000000000
#define MAXN 200010

int n, m;
vector<int> g[MAXN];
int dist[MAXN];


int dp[MAXN];

int search_ans(int u) {
    if (dp[u]==-1) {
        dp[u]=dist[u];
        int i;
        for (i=0; i<g[u].size(); i++) {
            int v=g[u][i];
            if (dist[v]>dist[u]) {
                dp[u]=min(dp[u], search_ans(v));
            }
            else {
                dp[u]=min(dp[u], dist[v]);
            }
        }
    }
    return dp[u];
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d", &n, &m);;
        int i, j;

        for (i=1; i<=n; i++) {
            dist[i]=-1;
            dp[i]=-1;
            g[i].clear();
        }

        int u, v;
        for (i=1; i<=m; i++) {
            scanf("%d %d", &u, &v);
            g[u].push_back(v);
        }



        queue<int> que;
        que.push(1);
        dist[1]=0;
        while (!que.empty()) {
            u = que.front();
            que.pop();
            for (i=0; i<g[u].size(); i++) {
                v = g[u][i];
                if (dist[v]==-1) {
                    dist[v]=dist[u]+1;
                    que.push(v);
                }
            }
        }

        for (i=1; i<=n; i++) {
            printf("%d ", search_ans(i));
        }
        printf("\n");

    }
    return 0;
}
