Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	for s, c := getNextString(scanner), getNextString(scanner); c != "X"; s, c = getNextString(scanner), getNextString(scanner) {
		if strings.Index(s, "_") != -1 {
			fmt.Fprintln(writer, translate(strings.Split(s, "_"), c))
			continue
		}
		ss := make([]string, 0)
		for len(s) > 0 {
			index := nextupper(s[1:])
			ss = append(ss, s[:index+1])
			s = s[index+1:]
		}
		fmt.Fprintln(writer, translate(ss, c))
	}
}
func translate(ss []string, c string) string {
	switch c {
	case "U":
		return u(ss)
	case "L":
		return l(ss)
	case "D":
		return d(ss)
	}
	return ""
}

func nextupper(s string) int {
	n := len(s)
	for i := 0; i < n; i++ {
		if s[i:i+1] == strings.ToUpper(s[i:i+1]) {
			return i
		}
	}
	return n
}
func u(ss []string) string {
	n := len(ss)
	for i := 0; i < n; i++ {
		ss[i] = fmt.Sprintf("%s%s", strings.ToUpper(ss[i][0:1]), ss[i][1:])
	}
	return strings.Join(ss, "")
}
func l(ss []string) string {
	n := len(ss)
	ss[0] = strings.ToLower(ss[0])
	for i := 1; i < n; i++ {
		ss[i] = fmt.Sprintf("%s%s", strings.ToUpper(ss[i][0:1]), ss[i][1:])
	}
	return strings.Join(ss, "")
}
func d(ss []string) string {
	n := len(ss)
	for i := 0; i < n; i++ {
		ss[i] = strings.ToLower(ss[i])
	}
	return strings.Join(ss, "_")
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

string name;
char type;

int main(){
  while(cin >> name >> type){
    if(type == 'X') break;
    if(name.find("_") != std::string::npos){
      if(type == 'U') name[0] -= 32;
      rep(i, name.size()){
	if(name[i] != '_') continue;
	name.erase(name.begin()+i);
	name[i] -= 32;
      }
    }else{
      if(type == 'L') name[0] += 32;
      else if(type == 'U') name[0] -= 32;
      else{
	if(name[0] < 97) name[0] += 32;
	rep(i, name.size()){
	  if(name[i] > 90) continue;
	  name[i] += 32;
	  name.insert(i, "_");
	}
      }
    }
    cout << name << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;

int main(){
  string str;
  char S;
  while(1){
    cin>>str;
    cin>>S;

    if(S=='X') break;

    else  if(S=='L'){
      if(str[0]>='A' && str[0]<='Z') str[0]+=32;
      for(int i=0;i<str.size();i++){
	if(str[i]=='_') str[i+1]-=32;
      }
      for(int i=0;i<str.size();i++){
	if(str[i]=='_') continue;
	else cout<<str[i];
      }
      cout<<endl;
    }
    
    else  if(S=='U') {
      if(str[0]>='a' && str[0]<='z') str[0]-=32;
    
    for(int i=0;i<str.size();i++){
    if(str[i]=='_') str[i+1]-=32;
  }
  for(int i=0;i<str.size();i++){
	if(str[i]=='_') continue;
	else cout<<str[i];
      }
      cout<<endl;
      
    }
    
  else  if(S=='D'){
    for(int i=0;i<str.size();i++){
      if(str[i]>=65 && str[i]<=90){
	str[i]+=32;
      }
      if(i>0 && str[i+1]>=65 && str[i+1]<=90){
	cout<<str[i]<<"_";
      }
      else cout<<str[i];
    }
    cout<<endl;
  }
  }
  return 0;
  }

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cctype>
#include <iterator>
using namespace std;

vector<string> parseL(string s) {
    const int L = s.length();
    int i = 0, j = 0;
    vector<string> res;
    while(true) {
        if(L <= j) break;
        j++;
        while(j < L && islower(s[j])) j++;
        res.push_back(s.substr(i, j - i));
        i = j;
    }
    return res;
}

vector<string> parseU(string s) {
    s[0] = tolower(s[0]);
    vector<string> res = parseL(s);
    res[0][0] = toupper(res[0][0]);
    return res;
}

vector<string> parseD(string s) {
    const int L = s.length();
    int i = 0;
    vector<string> res;
    while(true) {
        string t;
        while(i < L && s[i] != '_') {
            t.push_back(s[i]);
            i++;
        }
        res.push_back(t);
        if(i == L) break;
        i++;
    }
    return res;
}

void tr(vector<string> vec) {
    copy(vec.begin(), vec.end(), ostream_iterator<string>(cerr, "_"));
}

int main() {
    for(string text, type; cin >> text >> type, type != "X"; ) {
        vector<string> vec;
        if(find(text.begin(), text.end(), '_') != text.end()) {
            vec = parseD(text);
        }
        else if(isupper(text[0])) {
            vec = parseU(text);
        }
        else {
            vec = parseL(text);
        }

        if(type == "L") {
            for(int j = 0; j < (int)vec[0].size(); j++) cout << (char)tolower(vec[0][j]);
            for(int i = 1; i < (int)vec.size(); i++) {
                cout << (char)toupper(vec[i][0]);
                for(int j = 1; j < (int)vec[i].size(); j++) {
                    cout << (char)tolower(vec[i][j]);
                }
            }
        }
        else if(type == "U") {
            for(int i = 0; i < (int)vec.size(); i++) {
                cout << (char)toupper(vec[i][0]);
                for(int j = 1; j < (int)vec[i].size(); j++) {
                    cout << (char)tolower(vec[i][j]);
                }
            }
        }
        else {
            for(int j = 0; j < (int)vec[0].size(); j++) cout << (char)tolower(vec[0][j]);
            for(int i = 1; i < (int)vec.size(); i++) {
                cout << '_';
                for(int j = 0; j < (int)vec[i].size(); j++) {
                    cout << (char)tolower(vec[i][j]);
                }
            }
        }
        cout << endl;
    }
}

C++: #include<iostream>

using namespace std;

string toL(string in,int n){
  string out;
  for(int i = 0; i < in.length(); i++){
    if(in[i] == '_'){
      if(in[i+1] >= 'a' && in[i+1] <= 'z'){
	out += in[i+1]-('a' - 'A');
      }else out += in[i+1];
      i++;
    }else{
      //  if(in[i] >= 'A' && in[i] <= 'Z') out += in[i]+('a'-'A');
      //else out += in[i];
      out += in[i];
    }
  }
  if(n && out[0] >= 'A' && out[0] <= 'Z') out[0] += ('a'-'A');
  if(!n && out[0] >= 'a' && out[0] <= 'z' ) out[0] -= ('a'-'A'); 
  return out;
}

string toD(string in){
  string out;
  for(int i = 0; i < in.length(); i++){
    if(in[i] >= 'A' && in[i] <= 'Z'){
      if(i)out += "_";
      out += in[i] + ('a'-'A');
    }else out += in[i];
  }
  return out;
}
int main(){
  
  string in;
  char c;
  while(cin >> in >> c){
    if(c == 'X') break;
    if(c == 'L') in = toL(in,1);
    if(c == 'U') in = toL(in,0);
    if(c == 'D') in = toD(in);
    cout << in << endl;
  }
  return 0;
}