#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class T>
inline bool setmin(T &a, T b) {
  if (a > b) return a = b, 1;
  return 0;
}
template <class T>
inline bool setmax(T &a, T b) {
  if (a < b) return a = b, 1;
  return 0;
}
template <class T>
inline T fast(T a, T b, T mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
inline T russian(T a, T b, T mod) {
  long long res = 0;
  while (b) {
    if (b & 1) res = (res + a) % mod;
    a = (a + a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
istream &operator>>(istream &os, vector<T> &container) {
  for (auto &u : container) os >> u;
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &container) {
  for (auto &u : container) os << u << " ";
  return os;
}
template <typename T>
inline T gcd(T a, T b) {
  while (b) swap(a %= b, b);
  return a;
}
const long long T = 1 << 20;
const long long INF = 1e9 + 7;
const long long mod = 998244353;
const long long BIG_INF = 1e18 + 7;
const long long N = 1e6 + 7;
const int LOG = 17;
int n, m, k, q;
vector<vector<pair<int, int>>> G(N);
int counter;
void gen(int pocz, int kon, int val) {
  int pom = val;
  for (int i = 0; i < 2; i++) {
    val = pom;
    int akt = kon;
    while (val) {
      if (val < 10)
        G[pocz].push_back({akt, val});
      else {
        G[counter].push_back({akt, val % 10});
        akt = counter++;
      }
      val /= 10;
    }
    swap(pocz, kon);
  }
}
int odw[N];
int dist[N];
int ans[N];
vector<vector<pair<int, int>>> DAG(N);
void solve(vector<int> &zbior, long long modulo) {
  vector<int> id;
  id.resize((int)(zbior).size());
  fill((id).begin(), (id).end(), 0);
  for (auto &u : zbior) {
    ans[u] = modulo;
    sort((DAG[u]).begin(), (DAG[u]).end());
  }
  for (int i = 0; i <= 9; i++) {
    vector<int> next;
    for (int j = 0; j < ((int)(zbior).size()); j++) {
      while (id[j] < (int)(DAG[zbior[j]]).size() &&
             i == DAG[zbior[j]][id[j]].first) {
        if (!odw[DAG[zbior[j]][id[j]].second]) {
          odw[DAG[zbior[j]][id[j]].second] = true;
          next.push_back(DAG[zbior[j]][id[j]].second);
        }
        id[j]++;
      }
    }
    if ((int)(next).size()) solve(next, (modulo * 10 + i) % INF);
  }
}
void gen_dag() {
  queue<int> kol;
  kol.push(1);
  odw[1] = 1;
  while ((int)(kol).size()) {
    int akt = kol.front();
    kol.pop();
    for (auto &u : G[akt])
      if (!odw[u.first]) {
        odw[u.first] = 1;
        dist[u.first] = dist[akt] + 1;
        kol.push(u.first);
        DAG[akt].push_back({u.second, u.first});
      } else if (dist[akt] + 1 == dist[u.first])
        DAG[akt].push_back({u.second, u.first});
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  counter = n + 1;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    gen(a, b, i);
  }
  gen_dag();
  vector<int> start = {1};
  fill(odw, odw + N, 0);
  solve(start, 0);
  for (int i = 2; i <= n; i++) cout << ans[i] << '\n';
}
