#include <bits/stdc++.h>
using namespace std;
void solve() {
  string a;
  int x;
  cin >> a >> x;
  int n = a.length();
  vector<int> o(n, -1);
  for (int i = 0; i < n; i++) {
    if (i - x < 0 && i + x < n) {
      if (o[i + x] == -1) o[i + x] = a[i] - '0';
      if (o[i + x] != a[i] - '0') {
        cout << "-1";
        goto fail;
      }
    } else if (i + x >= n && i - x >= 0) {
      if (o[i - x] == -1) o[i - x] = a[i] - '0';
      if (o[i - x] != a[i] - '0') {
        cout << "-1";
        goto fail;
      }
    } else if (i + x >= n && i - x < 0) {
      if (a[i] == '1') {
        cout << "-1";
        goto fail;
      }
    } else if (i - x >= 0 && i + x < n) {
      if (a[i] == '0') {
        if (o[i + x] == -1) o[i + x] = 0;
        if (o[i - x] == -1) o[i - x] = 0;
        if (o[i - x] == 1) {
          cout << "-1";
          goto fail;
        }
        if (o[i + x] == 1) {
          cout << "-1";
          goto fail;
        }
      }
      if (a[i] == '1') {
        if (o[i - x] == -1) o[i - x] = 1;
        if (o[i - x] == 0) o[i + x] = 1;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (o[i] == -1) o[i] = 1;
    cout << o[i];
  }
fail:
  cout << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  cin >> q;
  while (q--) {
    solve();
  }
  return 0;
}
