#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> A(n);
  int x, y;
  int nn = n;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    x--, y--;
    string s = to_string(i + 1);
    int na = s.size();
    int last = x;
    for (int j = 0; j < na - 1; j++) {
      A.emplace_back();
      A[last].emplace_back(s[j] - '0', nn);
      last = nn;
      nn++;
    }
    A[last].emplace_back(s[na - 1] - '0', y);
    last = y;
    for (int j = 0; j < na - 1; j++) {
      A.emplace_back();
      A[last].emplace_back(s[j] - '0', nn);
      last = nn;
      nn++;
    }
    A[last].emplace_back(s[na - 1] - '0', x);
  }
  const int mod = 1e9 + 7;
  vector<int> dis(nn, -1);
  dis[0] = 0;
  queue<vector<int>> q;
  q.push({0});
  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    vector<vector<int>> D(10);
    for (int v : u) {
      for (auto c : A[v]) {
        if (dis[c.second] == -1) {
          D[c.first].emplace_back(c.second);
        }
      }
    }
    int d = dis[u[0]] * 10LL % mod;
    for (int dd = 0; dd < 10; dd++) {
      vector<int> v;
      for (auto c : D[dd]) {
        if (dis[c] == -1) {
          dis[c] = (d + dd) % mod;
          v.emplace_back(c);
        }
      }
      if ((int)v.size() > 0) {
        q.push(v);
      }
    }
  }
  for (int i = 1; i < n; i++) {
    cout << dis[i] << "\n";
  }
  return 0;
}
