#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 2;
const int mod = 1e9 + 7;
int n, m;
long long ans[maxn];
int cnt;
vector<int> edge[maxn][10];
void addedge(int u, int v, int num) {
  while (num >= 10) {
    edge[++cnt][num % 10].push_back(v);
    v = cnt;
    num /= 10;
  }
  edge[u][num].push_back(v);
}
bool vis[maxn];
queue<vector<int> > Que;
void SPFA(int s) {
  ans[s] = 0;
  vis[s] = true;
  vector<int> ss;
  ss.push_back(s);
  Que.push(ss);
  while (!Que.empty()) {
    vector<int> u = Que.front();
    Que.pop();
    for (int num = 0; num <= 9; num++) {
      vector<int> tmp;
      for (int i = 0; i < u.size(); i++)
        for (int j = 0; j < edge[u[i]][num].size(); j++) {
          int v = edge[u[i]][num][j];
          if (vis[v]) continue;
          vis[v] = true;
          tmp.push_back(v);
          ans[v] = (10LL * ans[u[i]] + num) % mod;
        }
      if (tmp.size()) Que.push(tmp);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  cnt = n;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v, i);
    addedge(v, u, i);
  }
  SPFA(1);
  for (int i = 2; i <= n; i++) printf("%I64d\n", ans[i]);
  return 0;
}
