#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long M = (long long)1e9 + 7;
const double pi = 2 * acos(0.0);
const double esp = 1e-9;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  int test = 1;
  cin >> test;
  for (int cs = 1; cs <= test; ++cs) {
    string s, ts, w;
    int x;
    s += '#';
    cin >> ts >> x;
    s += ts;
    w += '#';
    for (int i = 1; i <= (int)ts.size(); ++i) w += '1';
    bool ck = true;
    for (int i = 1; i < (int)s.size(); ++i) {
      if (s[i] == '0') {
        if (i - x > 0) w[i - x] = '0';
        if (i + x < (int)s.size()) w[i + x] = '0';
      }
    }
    for (int i = 1; i < (int)w.size(); ++i) {
      if (i + x <= (int)s.size() - 1) {
        if ((s[i] == '1' && w[i + x] == '1')) {
          continue;
        }
        if (s[i] != '1' && w[i + x] == '1') {
          ck = false;
          break;
        }
      }
      if (i - x >= 1) {
        if (s[i] == '1' && w[i - x] == '1') {
          continue;
        }
        if (s[i] != '1' && w[i - x] == '1') {
          ck = false;
          break;
        }
      }
      if ((i + x >= (int)s.size() && i - x < 1)) {
        if (s[i] == '1') {
          ck = false;
          break;
        }
      } else {
        if (i + x < (int)s.size())
          if (s[i] != '0') {
            ck = false;
            break;
          }
        if (i - x > 0)
          if (s[i] != '0') {
            ck = false;
            break;
          }
      }
    }
    if (!ck) {
      cout << -1;
    } else {
      for (int i = 1; i < (int)w.size(); ++i) cout << w[i];
    }
    cout << '\n';
  }
  return 0;
}
