#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > al[1000005];
int w[1000005];
int extra = 100005;
queue<int> q;
long long MOD = 1000000007;
int main() {
  memset(w, -1, sizeof(w));
  scanf("%d%d", &n, &m);
  int a, b;
  int curr, val;
  for (int x = 1; x <= m; x++) {
    scanf("%d%d", &a, &b);
    curr = a;
    val = x;
    while (val >= 10) {
      al[extra].push_back(pair<int, int>(curr, val % 10));
      curr = extra++;
      val /= 10;
    }
    al[b].push_back(pair<int, int>(curr, val));
    curr = b;
    val = x;
    while (val >= 10) {
      al[extra].push_back(pair<int, int>(curr, val % 10));
      curr = extra++;
      val /= 10;
    }
    al[a].push_back(pair<int, int>(curr, val));
  }
  q.push(1);
  w[1] = 0;
  int node;
  long long weight;
  vector<pair<int, int> > super;
  while (!q.empty()) {
    super.clear();
    weight = w[q.front()];
    while (!q.empty() && weight == w[q.front()]) {
      node = q.front(), q.pop();
      for (vector<pair<int, int> >::iterator it = al[node].begin();
           it != al[node].end(); it++) {
        super.push_back(*it);
      }
    }
    sort(super.begin(), super.end(), [](pair<int, int> i, pair<int, int> j) {
      return i.second < j.second;
    });
    for (vector<pair<int, int> >::iterator it = super.begin();
         it != super.end(); it++) {
      if (w[(*it).first] == -1) {
        w[(*it).first] = (weight * 10 + (*it).second) % MOD;
        q.push((*it).first);
      }
    }
  }
  for (int x = 2; x <= n; x++) {
    printf("%d\n", w[x]);
  }
}
