#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int mod = 1e9 + 7;
int n, m, tot, dis[N * 5], digit[10];
vector<int> g[N * 5][10];
queue<vector<int> > q;
int main() {
  scanf("%d%d", &n, &m);
  tot = n;
  for (int ww = 1; ww <= m; ++ww) {
    int u, v, w = ww;
    scanf("%d%d", &u, &v);
    if (w < 10) {
      g[u][w].push_back(v);
      g[v][w].push_back(u);
    } else {
      int l = ++tot;
      int r = l;
      int d = 0;
      while (w) {
        digit[++d] = w % 10;
        w /= 10;
      }
      for (int i = d - 1; i > 1; --i) {
        g[tot][digit[i]].push_back(tot + 1);
        r = ++tot;
      }
      g[u][digit[d]].push_back(l);
      g[r][digit[1]].push_back(u);
      g[v][digit[d]].push_back(l);
      g[r][digit[1]].push_back(v);
    }
  }
  memset(dis, -1, sizeof(dis));
  q.push(vector<int>(1, 1));
  dis[1] = 0;
  while (!q.empty()) {
    vector<int> vec = q.front();
    q.pop();
    for (int i = 0; i <= 9; ++i) {
      vector<int> nxt;
      for (auto u : vec) {
        for (auto v : g[u][i]) {
          if (dis[v] == -1) {
            dis[v] = (dis[u] * 10ll + i) % mod;
            nxt.push_back(v);
          }
        }
      }
      if (!nxt.empty()) q.push(nxt);
    }
  }
  for (int i = 2; i <= n; ++i) printf("%d\n", dis[i]);
  return 0;
}
