#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1000 * 1000 + 10;
int n;
int h[N], mark[N], cnt;
int ans[N];
vector<pair<int, int> > c[N];
int que[N], qen;
void solve(set<int> s, int cur) {
  for (auto x : s) {
    mark[x] = true;
    if (x <= n) ans[x] = cur;
  }
  map<int, short> mp;
  for (auto x : s)
    for (auto y : c[x])
      if (h[y.first] == h[x] + 1 && mark[y.first] == false) {
        if (mp.count(y.first) == 0)
          mp[y.first] = y.second;
        else
          mp[y.first] = min((int)mp[y.first], y.second);
      }
  set<int> child[10];
  for (auto x : s)
    for (auto y : c[x])
      if (h[y.first] == h[x] + 1 && mark[y.first] == false)
        child[mp[y.first]].insert(y.first);
  for (int i = 0; i < 10; i++)
    if (child[i].size()) solve(child[i], (1ll * cur * 10 + i) % mod);
}
void handle(int u, int v, int i) {
  int cur = v;
  while (i != 0) {
    if (i / 10 == 0) {
      c[u].push_back({cur, i % 10});
      break;
    }
    c[cnt].push_back({cur, i % 10});
    cur = cnt;
    cnt++;
    i /= 10;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int m;
  cin >> n >> m;
  cnt = n + 1;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    handle(u, v, i);
    handle(v, u, i);
  }
  memset(h, -1, sizeof h);
  que[qen++] = 1;
  h[1] = 0;
  for (int i = 0; i < qen; i++) {
    int s = que[i];
    for (auto x : c[s])
      if (h[x.first] == -1) {
        h[x.first] = h[s] + 1;
        que[qen++] = x.first;
      }
  }
  set<int> beg;
  beg.insert(1);
  solve(beg, 0);
  for (int i = 2; i <= n; i++) cout << ans[i] << '\n';
  return 0;
}
